Handle multiple setups of devolo Home Control (#41752)
This commit is contained in:
parent
22db1a509d
commit
13b2d10194
7 changed files with 123 additions and 110 deletions
|
@ -41,11 +41,13 @@ 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(
|
||||
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,
|
||||
|
@ -53,6 +55,7 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool
|
|||
url=conf[CONF_HOMECONTROL],
|
||||
)
|
||||
)
|
||||
)
|
||||
except ConnectionError as err:
|
||||
raise ConfigEntryNotReady from err
|
||||
|
||||
|
@ -62,7 +65,8 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool
|
|||
)
|
||||
|
||||
def shutdown(event):
|
||||
hass.data[DOMAIN]["homecontrol"].websocket_disconnect(
|
||||
for gateway in hass.data[DOMAIN][entry.entry_id]:
|
||||
gateway.websocket_disconnect(
|
||||
f"websocket disconnect requested by {EVENT_HOMEASSISTANT_STOP}"
|
||||
)
|
||||
|
||||
|
@ -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
|
||||
|
|
|
@ -28,16 +28,17 @@ 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 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=hass.data[DOMAIN]["homecontrol"],
|
||||
homecontrol=gateway,
|
||||
device_instance=device,
|
||||
element_uid=binary_sensor,
|
||||
)
|
||||
)
|
||||
for device in hass.data[DOMAIN]["homecontrol"].devices.values():
|
||||
for device in gateway.devices.values():
|
||||
if hasattr(device, "remote_control_property"):
|
||||
for remote in device.remote_control_property:
|
||||
for index in range(
|
||||
|
@ -45,7 +46,7 @@ async def async_setup_entry(
|
|||
):
|
||||
entities.append(
|
||||
DevoloRemoteControl(
|
||||
homecontrol=hass.data[DOMAIN]["homecontrol"],
|
||||
homecontrol=gateway,
|
||||
device_instance=device,
|
||||
element_uid=remote,
|
||||
key=index,
|
||||
|
|
|
@ -22,7 +22,8 @@ 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 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",
|
||||
|
@ -31,7 +32,7 @@ async def async_setup_entry(
|
|||
]:
|
||||
entities.append(
|
||||
DevoloClimateDeviceEntity(
|
||||
homecontrol=hass.data[DOMAIN]["homecontrol"],
|
||||
homecontrol=gateway,
|
||||
device_instance=device,
|
||||
element_uid=multi_level_switch,
|
||||
)
|
||||
|
|
|
@ -19,12 +19,13 @@ 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 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=hass.data[DOMAIN]["homecontrol"],
|
||||
homecontrol=gateway,
|
||||
device_instance=device,
|
||||
element_uid=multi_level_switch,
|
||||
)
|
||||
|
|
|
@ -17,12 +17,13 @@ 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 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=hass.data[DOMAIN]["homecontrol"],
|
||||
homecontrol=gateway,
|
||||
device_instance=device,
|
||||
element_uid=multi_level_switch.element_uid,
|
||||
)
|
||||
|
|
|
@ -29,22 +29,23 @@ 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 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=hass.data[DOMAIN]["homecontrol"],
|
||||
homecontrol=gateway,
|
||||
device_instance=device,
|
||||
element_uid=multi_level_sensor,
|
||||
)
|
||||
)
|
||||
for device in hass.data[DOMAIN]["homecontrol"].devices.values():
|
||||
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=hass.data[DOMAIN]["homecontrol"],
|
||||
homecontrol=gateway,
|
||||
device_instance=device,
|
||||
element_uid=consumption,
|
||||
consumption=consumption_type,
|
||||
|
@ -53,11 +54,12 @@ async def async_setup_entry(
|
|||
if hasattr(device, "battery_level"):
|
||||
entities.append(
|
||||
DevoloBatteryEntity(
|
||||
homecontrol=hass.data[DOMAIN]["homecontrol"],
|
||||
homecontrol=gateway,
|
||||
device_instance=device,
|
||||
element_uid=f"devolo.BatterySensor:{device.uid}",
|
||||
)
|
||||
)
|
||||
|
||||
async_add_entities(entities, False)
|
||||
|
||||
|
||||
|
|
|
@ -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 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=hass.data[DOMAIN]["homecontrol"],
|
||||
homecontrol=gateway,
|
||||
device_instance=device,
|
||||
element_uid=binary_switch,
|
||||
)
|
||||
)
|
||||
|
||||
async_add_entities(entities)
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue