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,18 +41,21 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool
|
||||||
raise ConfigEntryNotReady
|
raise ConfigEntryNotReady
|
||||||
|
|
||||||
gateway_ids = await hass.async_add_executor_job(mydevolo.get_gateway_ids)
|
gateway_ids = await hass.async_add_executor_job(mydevolo.get_gateway_ids)
|
||||||
gateway_id = gateway_ids[0]
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
zeroconf_instance = await zeroconf.async_get_instance(hass)
|
zeroconf_instance = await zeroconf.async_get_instance(hass)
|
||||||
hass.data[DOMAIN]["homecontrol"] = await hass.async_add_executor_job(
|
hass.data[DOMAIN][entry.entry_id] = []
|
||||||
partial(
|
for gateway_id in gateway_ids:
|
||||||
HomeControl,
|
hass.data[DOMAIN][entry.entry_id].append(
|
||||||
gateway_id=gateway_id,
|
await hass.async_add_executor_job(
|
||||||
zeroconf_instance=zeroconf_instance,
|
partial(
|
||||||
url=conf[CONF_HOMECONTROL],
|
HomeControl,
|
||||||
|
gateway_id=gateway_id,
|
||||||
|
zeroconf_instance=zeroconf_instance,
|
||||||
|
url=conf[CONF_HOMECONTROL],
|
||||||
|
)
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
|
||||||
except ConnectionError as err:
|
except ConnectionError as err:
|
||||||
raise ConfigEntryNotReady from err
|
raise ConfigEntryNotReady from err
|
||||||
|
|
||||||
|
@ -62,9 +65,10 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool
|
||||||
)
|
)
|
||||||
|
|
||||||
def shutdown(event):
|
def shutdown(event):
|
||||||
hass.data[DOMAIN]["homecontrol"].websocket_disconnect(
|
for gateway in hass.data[DOMAIN][entry.entry_id]:
|
||||||
f"websocket disconnect requested by {EVENT_HOMEASSISTANT_STOP}"
|
gateway.websocket_disconnect(
|
||||||
)
|
f"websocket disconnect requested by {EVENT_HOMEASSISTANT_STOP}"
|
||||||
|
)
|
||||||
|
|
||||||
# Listen when EVENT_HOMEASSISTANT_STOP is fired
|
# Listen when EVENT_HOMEASSISTANT_STOP is fired
|
||||||
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, shutdown)
|
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
|
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 a config entry."""
|
||||||
unload = all(
|
unload = all(
|
||||||
await asyncio.gather(
|
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
|
for platform in PLATFORMS
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
await asyncio.gather(
|
||||||
await hass.async_add_executor_job(
|
*[
|
||||||
hass.data[DOMAIN]["homecontrol"].websocket_disconnect
|
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
|
return unload
|
||||||
|
|
|
@ -28,29 +28,30 @@ async def async_setup_entry(
|
||||||
"""Get all binary sensor and multi level sensor devices and setup them via config entry."""
|
"""Get all binary sensor and multi level sensor devices and setup them via config entry."""
|
||||||
entities = []
|
entities = []
|
||||||
|
|
||||||
for device in hass.data[DOMAIN]["homecontrol"].binary_sensor_devices:
|
for gateway in hass.data[DOMAIN][entry.entry_id]:
|
||||||
for binary_sensor in device.binary_sensor_property:
|
for device in gateway.binary_sensor_devices:
|
||||||
entities.append(
|
for binary_sensor in device.binary_sensor_property:
|
||||||
DevoloBinaryDeviceEntity(
|
entities.append(
|
||||||
homecontrol=hass.data[DOMAIN]["homecontrol"],
|
DevoloBinaryDeviceEntity(
|
||||||
device_instance=device,
|
homecontrol=gateway,
|
||||||
element_uid=binary_sensor,
|
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 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)
|
async_add_entities(entities, False)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -22,20 +22,21 @@ async def async_setup_entry(
|
||||||
"""Get all cover devices and setup them via config entry."""
|
"""Get all cover devices and setup them via config entry."""
|
||||||
entities = []
|
entities = []
|
||||||
|
|
||||||
for device in hass.data[DOMAIN]["homecontrol"].multi_level_switch_devices:
|
for gateway in hass.data[DOMAIN][entry.entry_id]:
|
||||||
for multi_level_switch in device.multi_level_switch_property:
|
for device in gateway.multi_level_switch_devices:
|
||||||
if device.device_model_uid in [
|
for multi_level_switch in device.multi_level_switch_property:
|
||||||
"devolo.model.Thermostat:Valve",
|
if device.device_model_uid in [
|
||||||
"devolo.model.Room:Thermostat",
|
"devolo.model.Thermostat:Valve",
|
||||||
"devolo.model.Eurotronic:Spirit:Device",
|
"devolo.model.Room:Thermostat",
|
||||||
]:
|
"devolo.model.Eurotronic:Spirit:Device",
|
||||||
entities.append(
|
]:
|
||||||
DevoloClimateDeviceEntity(
|
entities.append(
|
||||||
homecontrol=hass.data[DOMAIN]["homecontrol"],
|
DevoloClimateDeviceEntity(
|
||||||
device_instance=device,
|
homecontrol=gateway,
|
||||||
element_uid=multi_level_switch,
|
device_instance=device,
|
||||||
|
element_uid=multi_level_switch,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
|
||||||
|
|
||||||
async_add_entities(entities, False)
|
async_add_entities(entities, False)
|
||||||
|
|
||||||
|
|
|
@ -19,16 +19,17 @@ async def async_setup_entry(
|
||||||
"""Get all cover devices and setup them via config entry."""
|
"""Get all cover devices and setup them via config entry."""
|
||||||
entities = []
|
entities = []
|
||||||
|
|
||||||
for device in hass.data[DOMAIN]["homecontrol"].multi_level_switch_devices:
|
for gateway in hass.data[DOMAIN][entry.entry_id]:
|
||||||
for multi_level_switch in device.multi_level_switch_property:
|
for device in gateway.multi_level_switch_devices:
|
||||||
if multi_level_switch.startswith("devolo.Blinds"):
|
for multi_level_switch in device.multi_level_switch_property:
|
||||||
entities.append(
|
if multi_level_switch.startswith("devolo.Blinds"):
|
||||||
DevoloCoverDeviceEntity(
|
entities.append(
|
||||||
homecontrol=hass.data[DOMAIN]["homecontrol"],
|
DevoloCoverDeviceEntity(
|
||||||
device_instance=device,
|
homecontrol=gateway,
|
||||||
element_uid=multi_level_switch,
|
device_instance=device,
|
||||||
|
element_uid=multi_level_switch,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
|
||||||
|
|
||||||
async_add_entities(entities, False)
|
async_add_entities(entities, False)
|
||||||
|
|
||||||
|
|
|
@ -17,16 +17,17 @@ async def async_setup_entry(
|
||||||
"""Get all light devices and setup them via config entry."""
|
"""Get all light devices and setup them via config entry."""
|
||||||
entities = []
|
entities = []
|
||||||
|
|
||||||
for device in hass.data[DOMAIN]["homecontrol"].multi_level_switch_devices:
|
for gateway in hass.data[DOMAIN][entry.entry_id]:
|
||||||
for multi_level_switch in device.multi_level_switch_property.values():
|
for device in gateway.multi_level_switch_devices:
|
||||||
if multi_level_switch.switch_type == "dimmer":
|
for multi_level_switch in device.multi_level_switch_property.values():
|
||||||
entities.append(
|
if multi_level_switch.switch_type == "dimmer":
|
||||||
DevoloLightDeviceEntity(
|
entities.append(
|
||||||
homecontrol=hass.data[DOMAIN]["homecontrol"],
|
DevoloLightDeviceEntity(
|
||||||
device_instance=device,
|
homecontrol=gateway,
|
||||||
element_uid=multi_level_switch.element_uid,
|
device_instance=device,
|
||||||
|
element_uid=multi_level_switch.element_uid,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
|
||||||
|
|
||||||
async_add_entities(entities, False)
|
async_add_entities(entities, False)
|
||||||
|
|
||||||
|
|
|
@ -29,35 +29,37 @@ async def async_setup_entry(
|
||||||
"""Get all sensor devices and setup them via config entry."""
|
"""Get all sensor devices and setup them via config entry."""
|
||||||
entities = []
|
entities = []
|
||||||
|
|
||||||
for device in hass.data[DOMAIN]["homecontrol"].multi_level_sensor_devices:
|
for gateway in hass.data[DOMAIN][entry.entry_id]:
|
||||||
for multi_level_sensor in device.multi_level_sensor_property:
|
for device in gateway.multi_level_sensor_devices:
|
||||||
entities.append(
|
for multi_level_sensor in device.multi_level_sensor_property:
|
||||||
DevoloGenericMultiLevelDeviceEntity(
|
entities.append(
|
||||||
homecontrol=hass.data[DOMAIN]["homecontrol"],
|
DevoloGenericMultiLevelDeviceEntity(
|
||||||
device_instance=device,
|
homecontrol=gateway,
|
||||||
element_uid=multi_level_sensor,
|
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,
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
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)
|
async_add_entities(entities, False)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -11,21 +11,22 @@ async def async_setup_entry(
|
||||||
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities
|
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Get all devices and setup the switch devices via config entry."""
|
"""Get all devices and setup the switch devices via config entry."""
|
||||||
devices = hass.data[DOMAIN]["homecontrol"].binary_switch_devices
|
|
||||||
|
|
||||||
entities = []
|
entities = []
|
||||||
for device in devices:
|
|
||||||
for binary_switch in device.binary_switch_property:
|
for gateway in hass.data[DOMAIN][entry.entry_id]:
|
||||||
# Exclude the binary switch which also has multi_level_switches here,
|
for device in gateway.binary_switch_devices:
|
||||||
# because those are implemented as light entities now.
|
for binary_switch in device.binary_switch_property:
|
||||||
if not hasattr(device, "multi_level_switch_property"):
|
# Exclude the binary switch which also has multi_level_switches here,
|
||||||
entities.append(
|
# because those are implemented as light entities now.
|
||||||
DevoloSwitch(
|
if not hasattr(device, "multi_level_switch_property"):
|
||||||
homecontrol=hass.data[DOMAIN]["homecontrol"],
|
entities.append(
|
||||||
device_instance=device,
|
DevoloSwitch(
|
||||||
element_uid=binary_switch,
|
homecontrol=gateway,
|
||||||
|
device_instance=device,
|
||||||
|
element_uid=binary_switch,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
|
||||||
async_add_entities(entities)
|
async_add_entities(entities)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue