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,
)
def update_data():
"""Fetch data from the subdevice."""
data = {}
for sub_device in gateway.gateway_device.devices.values():
try:
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
def update_data_factory(sub_device):
"""Create update function for a subdevice."""
async def async_update_data():
"""Fetch data from the subdevice using async_add_executor_job."""
return await hass.async_add_executor_job(update_data)
"""Fetch data from the subdevice."""
try:
await hass.async_add_executor_job(sub_device.update)
except GatewayException as ex:
_LOGGER.error("Got exception while fetching the state: %s", ex)
return {ATTR_AVAILABLE: False}
return {ATTR_AVAILABLE: True}
return async_update_data
coordinator_dict = {}
for sub_device in gateway.gateway_device.devices.values():
# Create update coordinator
coordinator = DataUpdateCoordinator(
coordinator_dict[sub_device.sid] = DataUpdateCoordinator(
hass,
_LOGGER,
name=name,
update_method=async_update_data,
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:

View file

@ -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]

View file

@ -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)
)

View file

@ -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

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 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(