diff --git a/homeassistant/components/xiaomi_miio/__init__.py b/homeassistant/components/xiaomi_miio/__init__.py index 68d3fe04653..e2dc36ff568 100644 --- a/homeassistant/components/xiaomi_miio/__init__.py +++ b/homeassistant/components/xiaomi_miio/__init__.py @@ -405,36 +405,35 @@ async def async_setup_gateway_entry(hass: HomeAssistant, entry: ConfigEntry) -> hw_version=gateway_info.hardware_version, ) - def update_data(): - """Fetch data from the subdevice.""" - data = {} - for sub_device in gateway.gateway_device.devices.values(): + def update_data_factory(sub_device): + """Create update function for a subdevice.""" + + async def async_update_data(): + """Fetch data from the subdevice.""" try: - sub_device.update() + await hass.async_add_executor_job(sub_device.update) except GatewayException as ex: _LOGGER.error("Got exception while fetching the state: %s", ex) - data[sub_device.sid] = {ATTR_AVAILABLE: False} - else: - data[sub_device.sid] = {ATTR_AVAILABLE: True} - return data + return {ATTR_AVAILABLE: False} + return {ATTR_AVAILABLE: True} - async def async_update_data(): - """Fetch data from the subdevice using async_add_executor_job.""" - return await hass.async_add_executor_job(update_data) + return async_update_data - # Create update coordinator - coordinator = DataUpdateCoordinator( - hass, - _LOGGER, - name=name, - update_method=async_update_data, - # Polling interval. Will only be polled if there are subscribers. - update_interval=UPDATE_INTERVAL, - ) + coordinator_dict = {} + for sub_device in gateway.gateway_device.devices.values(): + # Create update coordinator + coordinator_dict[sub_device.sid] = DataUpdateCoordinator( + hass, + _LOGGER, + name=name, + update_method=update_data_factory(sub_device), + # Polling interval. Will only be polled if there are subscribers. + update_interval=UPDATE_INTERVAL, + ) hass.data[DOMAIN][entry.entry_id] = { CONF_GATEWAY: gateway.gateway_device, - KEY_COORDINATOR: coordinator, + KEY_COORDINATOR: coordinator_dict, } for platform in GATEWAY_PLATFORMS: diff --git a/homeassistant/components/xiaomi_miio/gateway.py b/homeassistant/components/xiaomi_miio/gateway.py index ebfb37ab8fb..6d0984a9aeb 100644 --- a/homeassistant/components/xiaomi_miio/gateway.py +++ b/homeassistant/components/xiaomi_miio/gateway.py @@ -169,4 +169,4 @@ class XiaomiGatewayDevice(CoordinatorEntity, Entity): if self.coordinator.data is None: return False - return self.coordinator.data[self._sub_device.sid][ATTR_AVAILABLE] + return self.coordinator.data[ATTR_AVAILABLE] diff --git a/homeassistant/components/xiaomi_miio/light.py b/homeassistant/components/xiaomi_miio/light.py index acb726e7c05..e97c6e76503 100644 --- a/homeassistant/components/xiaomi_miio/light.py +++ b/homeassistant/components/xiaomi_miio/light.py @@ -132,9 +132,11 @@ async def async_setup_entry( ) # Gateway sub devices sub_devices = gateway.devices - coordinator = hass.data[DOMAIN][config_entry.entry_id][KEY_COORDINATOR] for sub_device in sub_devices.values(): if sub_device.device_type == "LightBulb": + coordinator = hass.data[DOMAIN][config_entry.entry_id][KEY_COORDINATOR][ + sub_device.sid + ] entities.append( XiaomiGatewayBulb(coordinator, sub_device, config_entry) ) diff --git a/homeassistant/components/xiaomi_miio/sensor.py b/homeassistant/components/xiaomi_miio/sensor.py index 0beac2c0041..df0c953d62a 100644 --- a/homeassistant/components/xiaomi_miio/sensor.py +++ b/homeassistant/components/xiaomi_miio/sensor.py @@ -681,8 +681,10 @@ async def async_setup_entry( ) # Gateway sub devices sub_devices = gateway.devices - coordinator = hass.data[DOMAIN][config_entry.entry_id][KEY_COORDINATOR] for sub_device in sub_devices.values(): + coordinator = hass.data[DOMAIN][config_entry.entry_id][KEY_COORDINATOR][ + sub_device.sid + ] for sensor, description in SENSOR_TYPES.items(): if sensor not in sub_device.status: continue diff --git a/homeassistant/components/xiaomi_miio/switch.py b/homeassistant/components/xiaomi_miio/switch.py index 968abceb57c..05d6543e93d 100644 --- a/homeassistant/components/xiaomi_miio/switch.py +++ b/homeassistant/components/xiaomi_miio/switch.py @@ -369,10 +369,12 @@ async def async_setup_other_entry(hass, config_entry, async_add_entities): gateway = hass.data[DOMAIN][config_entry.entry_id][CONF_GATEWAY] # Gateway sub devices sub_devices = gateway.devices - coordinator = hass.data[DOMAIN][config_entry.entry_id][KEY_COORDINATOR] for sub_device in sub_devices.values(): if sub_device.device_type != "Switch": continue + coordinator = hass.data[DOMAIN][config_entry.entry_id][KEY_COORDINATOR][ + sub_device.sid + ] switch_variables = set(sub_device.status) & set(GATEWAY_SWITCH_VARS) if switch_variables: entities.extend(