mirror of
				https://github.com/XiaoMi/ha_xiaomi_home.git
				synced 2025-11-04 03:12:08 +08:00 
			
		
		
		
	feat: support remove device (#622)
* feat: support remove device * feat: simplify the unsub logic * feat: update notify after rm device
This commit is contained in:
		@@ -308,3 +308,43 @@ async def async_remove_entry(
 | 
			
		||||
    await miot_cert.remove_user_cert_async()
 | 
			
		||||
    await miot_cert.remove_user_key_async()
 | 
			
		||||
    return True
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def async_remove_config_entry_device(
 | 
			
		||||
    hass: HomeAssistant,
 | 
			
		||||
    config_entry: ConfigEntry,
 | 
			
		||||
    device_entry: device_registry.DeviceEntry
 | 
			
		||||
) -> bool:
 | 
			
		||||
    """Remove the device."""
 | 
			
		||||
    miot_client: MIoTClient = await get_miot_instance_async(
 | 
			
		||||
        hass=hass, entry_id=config_entry.entry_id)
 | 
			
		||||
 | 
			
		||||
    if len(device_entry.identifiers) != 1:
 | 
			
		||||
        _LOGGER.error(
 | 
			
		||||
            'remove device failed, invalid identifiers, %s, %s',
 | 
			
		||||
            device_entry.id, device_entry.identifiers)
 | 
			
		||||
        return False
 | 
			
		||||
    identifiers = list(device_entry.identifiers)[0]
 | 
			
		||||
    if identifiers[0] != DOMAIN:
 | 
			
		||||
        _LOGGER.error(
 | 
			
		||||
            'remove device failed, invalid domain, %s, %s',
 | 
			
		||||
            device_entry.id, device_entry.identifiers)
 | 
			
		||||
        return False
 | 
			
		||||
    device_info = identifiers[1].split('_')
 | 
			
		||||
    if len(device_info) != 2:
 | 
			
		||||
        _LOGGER.error(
 | 
			
		||||
            'remove device failed, invalid device info, %s, %s',
 | 
			
		||||
            device_entry.id, device_entry.identifiers)
 | 
			
		||||
        return False
 | 
			
		||||
    did = device_info[1]
 | 
			
		||||
    if did not in miot_client.device_list:
 | 
			
		||||
        _LOGGER.error(
 | 
			
		||||
            'remove device failed, device not found, %s, %s',
 | 
			
		||||
            device_entry.id, device_entry.identifiers)
 | 
			
		||||
        return False
 | 
			
		||||
    # Remove device
 | 
			
		||||
    await miot_client.remove_device_async(did)
 | 
			
		||||
    device_registry.async_get(hass).async_remove_device(device_entry.id)
 | 
			
		||||
    _LOGGER.info(
 | 
			
		||||
        'remove device, %s, %s, %s', device_info[0], did, device_entry.id)
 | 
			
		||||
    return True
 | 
			
		||||
 
 | 
			
		||||
@@ -848,6 +848,30 @@ class MIoTClient:
 | 
			
		||||
        _LOGGER.debug('client unsub device state, %s', did)
 | 
			
		||||
        return True
 | 
			
		||||
 | 
			
		||||
    async def remove_device_async(self, did: str) -> None:
 | 
			
		||||
        if did not in self._device_list_cache:
 | 
			
		||||
            return
 | 
			
		||||
        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)
 | 
			
		||||
        # Storage
 | 
			
		||||
        await self._storage.save_async(
 | 
			
		||||
            domain='miot_devices',
 | 
			
		||||
            name=f'{self._uid}_{self._cloud_server}',
 | 
			
		||||
            data=self._device_list_cache)
 | 
			
		||||
        # Update notify
 | 
			
		||||
        self.__request_show_devices_changed_notify()
 | 
			
		||||
 | 
			
		||||
    def __get_exec_error_with_rc(self, rc: int) -> str:
 | 
			
		||||
        err_msg: str = self._i18n.translate(key=f'error.common.{rc}')
 | 
			
		||||
        if not err_msg:
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user