Split miio gateway coordinator (#69755)

This commit is contained in:
starkillerOG 2022-05-19 11:14:07 +02:00 committed by GitHub
parent bad245a856
commit cfe9ea033a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 26 deletions

View File

@ -405,36 +405,35 @@ async def async_setup_gateway_entry(hass: HomeAssistant, entry: ConfigEntry) ->
hw_version=gateway_info.hardware_version, hw_version=gateway_info.hardware_version,
) )
def update_data(): def update_data_factory(sub_device):
"""Fetch data from the subdevice.""" """Create update function for a subdevice."""
data = {}
for sub_device in gateway.gateway_device.devices.values(): async def async_update_data():
"""Fetch data from the subdevice."""
try: try:
sub_device.update() await hass.async_add_executor_job(sub_device.update)
except GatewayException as ex: except GatewayException as ex:
_LOGGER.error("Got exception while fetching the state: %s", ex) _LOGGER.error("Got exception while fetching the state: %s", ex)
data[sub_device.sid] = {ATTR_AVAILABLE: False} return {ATTR_AVAILABLE: False}
else: return {ATTR_AVAILABLE: True}
data[sub_device.sid] = {ATTR_AVAILABLE: True}
return data
async def async_update_data(): return async_update_data
"""Fetch data from the subdevice using async_add_executor_job."""
return await hass.async_add_executor_job(update_data)
# Create update coordinator coordinator_dict = {}
coordinator = DataUpdateCoordinator( for sub_device in gateway.gateway_device.devices.values():
hass, # Create update coordinator
_LOGGER, coordinator_dict[sub_device.sid] = DataUpdateCoordinator(
name=name, hass,
update_method=async_update_data, _LOGGER,
# Polling interval. Will only be polled if there are subscribers. name=name,
update_interval=UPDATE_INTERVAL, 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] = { hass.data[DOMAIN][entry.entry_id] = {
CONF_GATEWAY: gateway.gateway_device, CONF_GATEWAY: gateway.gateway_device,
KEY_COORDINATOR: coordinator, KEY_COORDINATOR: coordinator_dict,
} }
for platform in GATEWAY_PLATFORMS: for platform in GATEWAY_PLATFORMS:

View File

@ -169,4 +169,4 @@ class XiaomiGatewayDevice(CoordinatorEntity, Entity):
if self.coordinator.data is None: if self.coordinator.data is None:
return False return False
return self.coordinator.data[self._sub_device.sid][ATTR_AVAILABLE] return self.coordinator.data[ATTR_AVAILABLE]

View File

@ -132,9 +132,11 @@ async def async_setup_entry(
) )
# Gateway sub devices # Gateway sub devices
sub_devices = gateway.devices sub_devices = gateway.devices
coordinator = hass.data[DOMAIN][config_entry.entry_id][KEY_COORDINATOR]
for sub_device in sub_devices.values(): for sub_device in sub_devices.values():
if sub_device.device_type == "LightBulb": if sub_device.device_type == "LightBulb":
coordinator = hass.data[DOMAIN][config_entry.entry_id][KEY_COORDINATOR][
sub_device.sid
]
entities.append( entities.append(
XiaomiGatewayBulb(coordinator, sub_device, config_entry) XiaomiGatewayBulb(coordinator, sub_device, config_entry)
) )

View File

@ -681,8 +681,10 @@ async def async_setup_entry(
) )
# Gateway sub devices # Gateway sub devices
sub_devices = gateway.devices sub_devices = gateway.devices
coordinator = hass.data[DOMAIN][config_entry.entry_id][KEY_COORDINATOR]
for sub_device in sub_devices.values(): 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(): for sensor, description in SENSOR_TYPES.items():
if sensor not in sub_device.status: if sensor not in sub_device.status:
continue continue

View File

@ -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 = hass.data[DOMAIN][config_entry.entry_id][CONF_GATEWAY]
# Gateway sub devices # Gateway sub devices
sub_devices = gateway.devices sub_devices = gateway.devices
coordinator = hass.data[DOMAIN][config_entry.entry_id][KEY_COORDINATOR]
for sub_device in sub_devices.values(): for sub_device in sub_devices.values():
if sub_device.device_type != "Switch": if sub_device.device_type != "Switch":
continue continue
coordinator = hass.data[DOMAIN][config_entry.entry_id][KEY_COORDINATOR][
sub_device.sid
]
switch_variables = set(sub_device.status) & set(GATEWAY_SWITCH_VARS) switch_variables = set(sub_device.status) & set(GATEWAY_SWITCH_VARS)
if switch_variables: if switch_variables:
entities.extend( entities.extend(