Compare commits

...

4 Commits

Author SHA1 Message Date
Necroneco
6b750a8b95
Merge 506bd9f52e into f4d591b4d3 2025-12-27 22:14:30 +02:00
Li Shuzhen
f4d591b4d3
perf: remove unused device info (#1574)
Some checks failed
Tests / check-rule-format (push) Has been cancelled
Validate / validate-hassfest (push) Has been cancelled
Validate / validate-hacs (push) Has been cancelled
Validate / validate-lint (push) Has been cancelled
Validate / validate-setup (push) Has been cancelled
2025-12-26 08:42:23 +08:00
caibinqing
506bd9f52e
fix pylint 2025-04-07 11:19:19 +08:00
caibinqing
7c0caa9df7
fix: add migration step for config entry 2025-04-07 10:54:09 +08:00
6 changed files with 123 additions and 22 deletions

View File

@ -349,3 +349,101 @@ async def async_remove_config_entry_device(
_LOGGER.info(
'remove device, %s, %s', identifiers[1], device_entry.id)
return True
async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry):
"""Migrate old entry."""
_LOGGER.debug(
'Migrating configuration from version %s.%s',
config_entry.version,
config_entry.minor_version,
)
if config_entry.version > 1:
# This means the user has downgraded from a future version
return False
if config_entry.version == 1:
await _migrate_v1_to_v2(hass, config_entry)
_LOGGER.debug(
'Migration to configuration version %s.%s successful',
config_entry.version,
config_entry.minor_version,
)
return True
async def _migrate_v1_to_v2(hass: HomeAssistant, config_entry: ConfigEntry):
def ha_persistent_notify(
notify_id: str, title: Optional[str] = None,
message: Optional[str] = None
) -> None:
"""Send messages in Notifications dialog box."""
if title:
persistent_notification.async_create(
hass=hass, message=message or '',
title=title, notification_id=notify_id)
else:
persistent_notification.async_dismiss(
hass=hass, notification_id=notify_id)
entry_id = config_entry.entry_id
entry_data = dict(config_entry.data)
ha_persistent_notify(
notify_id=f'{entry_id}.oauth_error', title=None, message=None)
miot_client: MIoTClient = await get_miot_instance_async(
hass=hass, entry_id=entry_id,
entry_data=entry_data,
persistent_notify=ha_persistent_notify)
# Spec parser
spec_parser = MIoTSpecParser(
lang=entry_data.get(
'integration_language', DEFAULT_INTEGRATION_LANGUAGE),
storage=miot_client.miot_storage,
loop=miot_client.main_loop
)
await spec_parser.init_async()
# Manufacturer
manufacturer: DeviceManufacturer = DeviceManufacturer(
storage=miot_client.miot_storage,
loop=miot_client.main_loop)
await manufacturer.init_async()
er = entity_registry.async_get(hass)
for _, info in miot_client.device_list.items():
spec_instance = await spec_parser.parse(urn=info['urn'])
if not isinstance(spec_instance, MIoTSpecInstance):
continue
device: MIoTDevice = MIoTDevice(
miot_client=miot_client,
device_info={
**info, 'manufacturer': manufacturer.get_name(
info.get('manufacturer', ''))},
spec_instance=spec_instance)
device.spec_transform()
# Update unique_id
for platform, entities in device.entity_list.items():
for entity in entities:
if not isinstance(entity.spec, MIoTSpecService):
continue
old_unique_id = device.gen_service_entity_id_v1(
ha_domain=DOMAIN,
siid=entity.spec.iid,
)
entity_id = er.async_get_entity_id(
platform, DOMAIN, old_unique_id
)
if entity_id is None:
continue
new_unique_id = device.gen_service_entity_id(
ha_domain=DOMAIN,
siid=entity.spec.iid,
description=entity.spec.description,
)
er.async_update_entity(entity_id, new_unique_id=new_unique_id)
hass.config_entries.async_update_entry(config_entry, version=2)

View File

@ -109,7 +109,7 @@ _LOGGER = logging.getLogger(__name__)
class XiaomiMihomeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Xiaomi Home config flow."""
# pylint: disable=unused-argument, inconsistent-quotes
VERSION = 1
VERSION = 2
MINOR_VERSION = 1
DEFAULT_AREA_NAME_RULE = 'room'
_main_loop: asyncio.AbstractEventLoop

View File

@ -1256,7 +1256,15 @@ class MIoTClient:
) -> None:
_LOGGER.info(
'gateway devices list changed, %s, %s', mips.group_id, did_list)
payload: dict = {'filter': {'did': did_list}}
payload: dict = {
'filter': {
'did': did_list
},
'info': [
'name', 'model', 'urn',
'online', 'specV2Access', 'pushAvailable'
]
}
gw_list = await mips.get_dev_list_async(
payload=json.dumps(payload))
if gw_list is None:
@ -1590,7 +1598,14 @@ class MIoTClient:
if not mips.mips_state:
_LOGGER.debug('local mips disconnect, skip refresh, %s', group_id)
return
gw_list: dict = await mips.get_dev_list_async()
payload: dict = {
'info': [
'name', 'model', 'urn',
'online', 'specV2Access', 'pushAvailable'
]
}
gw_list: dict = await mips.get_dev_list_async(
payload=json.dumps(payload))
if gw_list is None:
_LOGGER.error(
'refresh gw devices with group_id failed, %s, %s',

View File

@ -601,15 +601,8 @@ class MIoTHttpClient:
'bssid': device.get('bssid', None),
'order_time': device.get('orderTime', 0),
'fw_version': device.get('extra', {}).get(
'fw_version', 'unknown'),
}
if isinstance(device.get('extra', None), dict) and device['extra']:
device_infos[did]['fw_version'] = device['extra'].get(
'fw_version', None)
device_infos[did]['mcu_version'] = device['extra'].get(
'mcu_version', None)
device_infos[did]['platform'] = device['extra'].get(
'platform', None)
}
next_start_did = res_obj.get('next_start_did', None)
if res_obj.get('has_more', False) and next_start_did:

View File

@ -345,6 +345,11 @@ class MIoTDevice:
f'{ha_domain}.{self._model_strs[0][:9]}_{self.did_tag}_'
f'{self._model_strs[-1][:20]}')
def gen_service_entity_id_v1(self, ha_domain: str, siid: int) -> str:
return (
f'{ha_domain}.{self._model_strs[0][:9]}_{self.did_tag}_'
f'{self._model_strs[-1][:20]}_s_{siid}')
def gen_service_entity_id(self, ha_domain: str, siid: int,
description: str) -> str:
return (

View File

@ -1394,19 +1394,9 @@ class MipsLocalClient(_MipsClient):
continue
device_list[did] = {
'did': did,
'name': name,
'urn': urn,
'model': model,
'online': info.get('online', False),
'icon': info.get('icon', None),
'fw_version': None,
'home_id': '',
'home_name': '',
'room_id': info.get('roomId', ''),
'room_name': info.get('roomName', ''),
'specv2_access': info.get('specV2Access', False),
'push_available': info.get('pushAvailable', False),
'manufacturer': model.split('.')[0],
'push_available': info.get('pushAvailable', False)
}
return device_list