mirror of
https://github.com/XiaoMi/ha_xiaomi_home.git
synced 2026-01-17 15:41:15 +08:00
Compare commits
4 Commits
20ec0b2b96
...
c8c2139d70
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c8c2139d70 | ||
|
|
7a459de766 | ||
|
|
506bd9f52e | ||
|
|
7c0caa9df7 |
@ -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)
|
||||
|
||||
@ -105,7 +105,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
|
||||
|
||||
@ -879,16 +879,7 @@ class MIoTClient:
|
||||
sub_from = self._sub_source_list.pop(did, None)
|
||||
# Unsub
|
||||
if sub_from:
|
||||
if sub_from == 'cloud':
|
||||
self._mips_cloud.unsub_prop(did=did)
|
||||
self._mips_cloud.unsub_event(did=did)
|
||||
elif sub_from == 'lan':
|
||||
self._miot_lan.unsub_prop(did=did)
|
||||
self._miot_lan.unsub_event(did=did)
|
||||
elif sub_from in self._mips_local:
|
||||
mips = self._mips_local[sub_from]
|
||||
mips.unsub_prop(did=did)
|
||||
mips.unsub_event(did=did)
|
||||
self.__unsub_from(sub_from, did)
|
||||
# Storage
|
||||
await self._storage.save_async(
|
||||
domain='miot_devices',
|
||||
@ -936,6 +927,39 @@ class MIoTClient:
|
||||
delay_sec, lambda: self._main_loop.create_task(
|
||||
self.refresh_user_cert_async()))
|
||||
|
||||
@final
|
||||
def __unsub_from(self, sub_from: str, did: str) -> None:
|
||||
mips: Any = None
|
||||
if sub_from == 'cloud':
|
||||
mips = self._mips_cloud
|
||||
elif sub_from == 'lan':
|
||||
mips = self._miot_lan
|
||||
elif sub_from in self._mips_local:
|
||||
mips = self._mips_local[sub_from]
|
||||
if mips is not None:
|
||||
try:
|
||||
mips.unsub_prop(did=did)
|
||||
mips.unsub_event(did=did)
|
||||
except RuntimeError as e:
|
||||
if 'Event loop is closed' in str(e):
|
||||
# Ignore unsub exception when loop is closed
|
||||
pass
|
||||
else:
|
||||
raise
|
||||
|
||||
@final
|
||||
def __sub_from(self, sub_from: str, did: str) -> None:
|
||||
mips = None
|
||||
if sub_from == 'cloud':
|
||||
mips = self._mips_cloud
|
||||
elif sub_from == 'lan':
|
||||
mips = self._miot_lan
|
||||
elif sub_from in self._mips_local:
|
||||
mips = self._mips_local[sub_from]
|
||||
if mips is not None:
|
||||
mips.sub_prop(did=did, handler=self.__on_prop_msg)
|
||||
mips.sub_event(did=did, handler=self.__on_event_msg)
|
||||
|
||||
@final
|
||||
def __update_device_msg_sub(self, did: str) -> None:
|
||||
if did not in self._device_list_cache:
|
||||
@ -967,27 +991,9 @@ class MIoTClient:
|
||||
return
|
||||
# Unsub old
|
||||
if from_old:
|
||||
if from_old == 'cloud':
|
||||
self._mips_cloud.unsub_prop(did=did)
|
||||
self._mips_cloud.unsub_event(did=did)
|
||||
elif from_old == 'lan':
|
||||
self._miot_lan.unsub_prop(did=did)
|
||||
self._miot_lan.unsub_event(did=did)
|
||||
elif from_old in self._mips_local:
|
||||
mips = self._mips_local[from_old]
|
||||
mips.unsub_prop(did=did)
|
||||
mips.unsub_event(did=did)
|
||||
self.__unsub_from(from_old, did)
|
||||
# Sub new
|
||||
if from_new == 'cloud':
|
||||
self._mips_cloud.sub_prop(did=did, handler=self.__on_prop_msg)
|
||||
self._mips_cloud.sub_event(did=did, handler=self.__on_event_msg)
|
||||
elif from_new == 'lan':
|
||||
self._miot_lan.sub_prop(did=did, handler=self.__on_prop_msg)
|
||||
self._miot_lan.sub_event(did=did, handler=self.__on_event_msg)
|
||||
elif from_new in self._mips_local:
|
||||
mips = self._mips_local[from_new]
|
||||
mips.sub_prop(did=did, handler=self.__on_prop_msg)
|
||||
mips.sub_event(did=did, handler=self.__on_event_msg)
|
||||
self.__sub_from(from_new, did)
|
||||
self._sub_source_list[did] = from_new
|
||||
_LOGGER.info(
|
||||
'device sub changed, %s, from %s to %s', did, from_old, from_new)
|
||||
|
||||
@ -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 (
|
||||
|
||||
Loading…
Reference in New Issue
Block a user