diff --git a/homeassistant/components/devolo_home_control/__init__.py b/homeassistant/components/devolo_home_control/__init__.py index f1f00e9abe7..e2b1ffc5d6d 100644 --- a/homeassistant/components/devolo_home_control/__init__.py +++ b/homeassistant/components/devolo_home_control/__init__.py @@ -41,18 +41,21 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool raise ConfigEntryNotReady gateway_ids = await hass.async_add_executor_job(mydevolo.get_gateway_ids) - gateway_id = gateway_ids[0] try: zeroconf_instance = await zeroconf.async_get_instance(hass) - hass.data[DOMAIN]["homecontrol"] = await hass.async_add_executor_job( - partial( - HomeControl, - gateway_id=gateway_id, - zeroconf_instance=zeroconf_instance, - url=conf[CONF_HOMECONTROL], + hass.data[DOMAIN][entry.entry_id] = [] + for gateway_id in gateway_ids: + hass.data[DOMAIN][entry.entry_id].append( + await hass.async_add_executor_job( + partial( + HomeControl, + gateway_id=gateway_id, + zeroconf_instance=zeroconf_instance, + url=conf[CONF_HOMECONTROL], + ) + ) ) - ) except ConnectionError as err: raise ConfigEntryNotReady from err @@ -62,9 +65,10 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool ) def shutdown(event): - hass.data[DOMAIN]["homecontrol"].websocket_disconnect( - f"websocket disconnect requested by {EVENT_HOMEASSISTANT_STOP}" - ) + for gateway in hass.data[DOMAIN][entry.entry_id]: + gateway.websocket_disconnect( + f"websocket disconnect requested by {EVENT_HOMEASSISTANT_STOP}" + ) # Listen when EVENT_HOMEASSISTANT_STOP is fired hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, shutdown) @@ -72,19 +76,21 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool return True -async def async_unload_entry(hass, config_entry): +async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool: """Unload a config entry.""" unload = all( await asyncio.gather( *[ - hass.config_entries.async_forward_entry_unload(config_entry, platform) + hass.config_entries.async_forward_entry_unload(entry, platform) for platform in PLATFORMS ] ) ) - - await hass.async_add_executor_job( - hass.data[DOMAIN]["homecontrol"].websocket_disconnect + await asyncio.gather( + *[ + hass.async_add_executor_job(gateway.websocket_disconnect) + for gateway in hass.data[DOMAIN][entry.entry_id] + ] ) - del hass.data[DOMAIN]["homecontrol"] + hass.data[DOMAIN].pop(entry.entry_id) return unload diff --git a/homeassistant/components/devolo_home_control/binary_sensor.py b/homeassistant/components/devolo_home_control/binary_sensor.py index 1d581d2c0fb..fa9e5789f12 100644 --- a/homeassistant/components/devolo_home_control/binary_sensor.py +++ b/homeassistant/components/devolo_home_control/binary_sensor.py @@ -28,29 +28,30 @@ async def async_setup_entry( """Get all binary sensor and multi level sensor devices and setup them via config entry.""" entities = [] - for device in hass.data[DOMAIN]["homecontrol"].binary_sensor_devices: - for binary_sensor in device.binary_sensor_property: - entities.append( - DevoloBinaryDeviceEntity( - homecontrol=hass.data[DOMAIN]["homecontrol"], - device_instance=device, - element_uid=binary_sensor, - ) - ) - for device in hass.data[DOMAIN]["homecontrol"].devices.values(): - if hasattr(device, "remote_control_property"): - for remote in device.remote_control_property: - for index in range( - 1, device.remote_control_property[remote].key_count + 1 - ): - entities.append( - DevoloRemoteControl( - homecontrol=hass.data[DOMAIN]["homecontrol"], - device_instance=device, - element_uid=remote, - key=index, - ) + for gateway in hass.data[DOMAIN][entry.entry_id]: + for device in gateway.binary_sensor_devices: + for binary_sensor in device.binary_sensor_property: + entities.append( + DevoloBinaryDeviceEntity( + homecontrol=gateway, + device_instance=device, + element_uid=binary_sensor, ) + ) + for device in gateway.devices.values(): + if hasattr(device, "remote_control_property"): + for remote in device.remote_control_property: + for index in range( + 1, device.remote_control_property[remote].key_count + 1 + ): + entities.append( + DevoloRemoteControl( + homecontrol=gateway, + device_instance=device, + element_uid=remote, + key=index, + ) + ) async_add_entities(entities, False) diff --git a/homeassistant/components/devolo_home_control/climate.py b/homeassistant/components/devolo_home_control/climate.py index 0cc2340dc18..d3d9bd9a3fc 100644 --- a/homeassistant/components/devolo_home_control/climate.py +++ b/homeassistant/components/devolo_home_control/climate.py @@ -22,20 +22,21 @@ async def async_setup_entry( """Get all cover devices and setup them via config entry.""" entities = [] - for device in hass.data[DOMAIN]["homecontrol"].multi_level_switch_devices: - for multi_level_switch in device.multi_level_switch_property: - if device.device_model_uid in [ - "devolo.model.Thermostat:Valve", - "devolo.model.Room:Thermostat", - "devolo.model.Eurotronic:Spirit:Device", - ]: - entities.append( - DevoloClimateDeviceEntity( - homecontrol=hass.data[DOMAIN]["homecontrol"], - device_instance=device, - element_uid=multi_level_switch, + for gateway in hass.data[DOMAIN][entry.entry_id]: + for device in gateway.multi_level_switch_devices: + for multi_level_switch in device.multi_level_switch_property: + if device.device_model_uid in [ + "devolo.model.Thermostat:Valve", + "devolo.model.Room:Thermostat", + "devolo.model.Eurotronic:Spirit:Device", + ]: + entities.append( + DevoloClimateDeviceEntity( + homecontrol=gateway, + device_instance=device, + element_uid=multi_level_switch, + ) ) - ) async_add_entities(entities, False) diff --git a/homeassistant/components/devolo_home_control/cover.py b/homeassistant/components/devolo_home_control/cover.py index 46f3df34314..7ed217c7cb9 100644 --- a/homeassistant/components/devolo_home_control/cover.py +++ b/homeassistant/components/devolo_home_control/cover.py @@ -19,16 +19,17 @@ async def async_setup_entry( """Get all cover devices and setup them via config entry.""" entities = [] - for device in hass.data[DOMAIN]["homecontrol"].multi_level_switch_devices: - for multi_level_switch in device.multi_level_switch_property: - if multi_level_switch.startswith("devolo.Blinds"): - entities.append( - DevoloCoverDeviceEntity( - homecontrol=hass.data[DOMAIN]["homecontrol"], - device_instance=device, - element_uid=multi_level_switch, + for gateway in hass.data[DOMAIN][entry.entry_id]: + for device in gateway.multi_level_switch_devices: + for multi_level_switch in device.multi_level_switch_property: + if multi_level_switch.startswith("devolo.Blinds"): + entities.append( + DevoloCoverDeviceEntity( + homecontrol=gateway, + device_instance=device, + element_uid=multi_level_switch, + ) ) - ) async_add_entities(entities, False) diff --git a/homeassistant/components/devolo_home_control/light.py b/homeassistant/components/devolo_home_control/light.py index 9c532be2dc2..f046caea794 100644 --- a/homeassistant/components/devolo_home_control/light.py +++ b/homeassistant/components/devolo_home_control/light.py @@ -17,16 +17,17 @@ async def async_setup_entry( """Get all light devices and setup them via config entry.""" entities = [] - for device in hass.data[DOMAIN]["homecontrol"].multi_level_switch_devices: - for multi_level_switch in device.multi_level_switch_property.values(): - if multi_level_switch.switch_type == "dimmer": - entities.append( - DevoloLightDeviceEntity( - homecontrol=hass.data[DOMAIN]["homecontrol"], - device_instance=device, - element_uid=multi_level_switch.element_uid, + for gateway in hass.data[DOMAIN][entry.entry_id]: + for device in gateway.multi_level_switch_devices: + for multi_level_switch in device.multi_level_switch_property.values(): + if multi_level_switch.switch_type == "dimmer": + entities.append( + DevoloLightDeviceEntity( + homecontrol=gateway, + device_instance=device, + element_uid=multi_level_switch.element_uid, + ) ) - ) async_add_entities(entities, False) diff --git a/homeassistant/components/devolo_home_control/sensor.py b/homeassistant/components/devolo_home_control/sensor.py index c5ad9feb88a..d432560e8b8 100644 --- a/homeassistant/components/devolo_home_control/sensor.py +++ b/homeassistant/components/devolo_home_control/sensor.py @@ -29,35 +29,37 @@ async def async_setup_entry( """Get all sensor devices and setup them via config entry.""" entities = [] - for device in hass.data[DOMAIN]["homecontrol"].multi_level_sensor_devices: - for multi_level_sensor in device.multi_level_sensor_property: - entities.append( - DevoloGenericMultiLevelDeviceEntity( - homecontrol=hass.data[DOMAIN]["homecontrol"], - device_instance=device, - element_uid=multi_level_sensor, - ) - ) - for device in hass.data[DOMAIN]["homecontrol"].devices.values(): - if hasattr(device, "consumption_property"): - for consumption in device.consumption_property: - for consumption_type in ["current", "total"]: - entities.append( - DevoloConsumptionEntity( - homecontrol=hass.data[DOMAIN]["homecontrol"], - device_instance=device, - element_uid=consumption, - consumption=consumption_type, - ) + for gateway in hass.data[DOMAIN][entry.entry_id]: + for device in gateway.multi_level_sensor_devices: + for multi_level_sensor in device.multi_level_sensor_property: + entities.append( + DevoloGenericMultiLevelDeviceEntity( + homecontrol=gateway, + device_instance=device, + element_uid=multi_level_sensor, ) - if hasattr(device, "battery_level"): - entities.append( - DevoloBatteryEntity( - homecontrol=hass.data[DOMAIN]["homecontrol"], - device_instance=device, - element_uid=f"devolo.BatterySensor:{device.uid}", ) - ) + for device in gateway.devices.values(): + if hasattr(device, "consumption_property"): + for consumption in device.consumption_property: + for consumption_type in ["current", "total"]: + entities.append( + DevoloConsumptionEntity( + homecontrol=gateway, + device_instance=device, + element_uid=consumption, + consumption=consumption_type, + ) + ) + if hasattr(device, "battery_level"): + entities.append( + DevoloBatteryEntity( + homecontrol=gateway, + device_instance=device, + element_uid=f"devolo.BatterySensor:{device.uid}", + ) + ) + async_add_entities(entities, False) diff --git a/homeassistant/components/devolo_home_control/switch.py b/homeassistant/components/devolo_home_control/switch.py index 32da37bcc38..ecd505d9c98 100644 --- a/homeassistant/components/devolo_home_control/switch.py +++ b/homeassistant/components/devolo_home_control/switch.py @@ -11,21 +11,22 @@ async def async_setup_entry( hass: HomeAssistantType, entry: ConfigEntry, async_add_entities ) -> None: """Get all devices and setup the switch devices via config entry.""" - devices = hass.data[DOMAIN]["homecontrol"].binary_switch_devices - entities = [] - for device in devices: - for binary_switch in device.binary_switch_property: - # Exclude the binary switch which also has multi_level_switches here, - # because those are implemented as light entities now. - if not hasattr(device, "multi_level_switch_property"): - entities.append( - DevoloSwitch( - homecontrol=hass.data[DOMAIN]["homecontrol"], - device_instance=device, - element_uid=binary_switch, + + for gateway in hass.data[DOMAIN][entry.entry_id]: + for device in gateway.binary_switch_devices: + for binary_switch in device.binary_switch_property: + # Exclude the binary switch which also has multi_level_switches here, + # because those are implemented as light entities now. + if not hasattr(device, "multi_level_switch_property"): + entities.append( + DevoloSwitch( + homecontrol=gateway, + device_instance=device, + element_uid=binary_switch, + ) ) - ) + async_add_entities(entities)