2019-02-13 20:21:14 +00:00
|
|
|
"""Support for deCONZ devices."""
|
2021-12-01 17:59:52 +00:00
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import cast
|
|
|
|
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-02-06 13:32:17 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_API_KEY,
|
|
|
|
CONF_HOST,
|
|
|
|
CONF_PORT,
|
|
|
|
EVENT_HOMEASSISTANT_STOP,
|
|
|
|
)
|
2021-12-01 17:59:52 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2022-02-23 12:10:35 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
2021-12-01 17:59:52 +00:00
|
|
|
import homeassistant.helpers.entity_registry as er
|
2018-01-01 16:08:13 +00:00
|
|
|
|
2019-04-05 00:48:24 +00:00
|
|
|
from .config_flow import get_master_gateway
|
2022-02-23 12:10:35 +00:00
|
|
|
from .const import CONF_GROUP_ID_BASE, CONF_MASTER_GATEWAY, DOMAIN, PLATFORMS
|
|
|
|
from .deconz_event import async_setup_events, async_unload_events
|
|
|
|
from .errors import AuthenticationRequired, CannotConnect
|
|
|
|
from .gateway import DeconzGateway, get_deconz_session
|
2019-09-14 13:15:06 +00:00
|
|
|
from .services import async_setup_services, async_unload_services
|
2018-01-01 16:08:13 +00:00
|
|
|
|
2018-02-14 00:23:04 +00:00
|
|
|
|
2021-12-01 17:59:52 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
2018-04-23 16:00:16 +00:00
|
|
|
"""Set up a deCONZ bridge for a config entry.
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
Load config, group, light and sensor data for server information.
|
|
|
|
Start websocket for push notification of state changes from deCONZ.
|
|
|
|
"""
|
2021-07-06 15:18:54 +00:00
|
|
|
hass.data.setdefault(DOMAIN, {})
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2021-02-06 13:32:17 +00:00
|
|
|
await async_update_group_unique_id(hass, config_entry)
|
|
|
|
|
2019-04-05 00:48:24 +00:00
|
|
|
if not config_entry.options:
|
2019-09-05 23:38:00 +00:00
|
|
|
await async_update_master_gateway(hass, config_entry)
|
2018-04-23 16:00:16 +00:00
|
|
|
|
2022-02-23 12:10:35 +00:00
|
|
|
try:
|
|
|
|
api = await get_deconz_session(hass, config_entry.data)
|
|
|
|
except CannotConnect as err:
|
|
|
|
raise ConfigEntryNotReady from err
|
|
|
|
except AuthenticationRequired as err:
|
|
|
|
raise ConfigEntryAuthFailed from err
|
|
|
|
|
|
|
|
gateway = hass.data[DOMAIN][config_entry.entry_id] = DeconzGateway(
|
|
|
|
hass, config_entry, api
|
|
|
|
)
|
2022-07-09 18:30:48 +00:00
|
|
|
await gateway.async_update_device_registry()
|
2021-10-01 18:31:38 +00:00
|
|
|
|
2022-02-23 12:10:35 +00:00
|
|
|
config_entry.add_update_listener(gateway.async_config_entry_updated)
|
2018-08-24 17:37:22 +00:00
|
|
|
|
2022-02-23 12:10:35 +00:00
|
|
|
await async_setup_events(gateway)
|
2022-07-09 18:30:48 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
|
2019-09-25 16:56:31 +00:00
|
|
|
|
2022-02-23 12:10:35 +00:00
|
|
|
if len(hass.data[DOMAIN]) == 1:
|
|
|
|
async_setup_services(hass)
|
|
|
|
|
|
|
|
api.start()
|
|
|
|
|
2021-04-20 16:14:11 +00:00
|
|
|
config_entry.async_on_unload(
|
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, gateway.shutdown)
|
|
|
|
)
|
2019-09-14 13:15:06 +00:00
|
|
|
|
2018-01-01 16:08:13 +00:00
|
|
|
return True
|
2018-04-29 14:16:20 +00:00
|
|
|
|
|
|
|
|
2021-12-01 17:59:52 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
2018-04-29 14:16:20 +00:00
|
|
|
"""Unload deCONZ config entry."""
|
2022-02-23 12:10:35 +00:00
|
|
|
gateway: DeconzGateway = hass.data[DOMAIN].pop(config_entry.entry_id)
|
|
|
|
async_unload_events(gateway)
|
2019-04-05 00:48:24 +00:00
|
|
|
|
|
|
|
if not hass.data[DOMAIN]:
|
2021-10-01 18:31:38 +00:00
|
|
|
async_unload_services(hass)
|
2019-05-27 04:56:00 +00:00
|
|
|
|
2019-04-05 00:48:24 +00:00
|
|
|
elif gateway.master:
|
2019-09-05 23:38:00 +00:00
|
|
|
await async_update_master_gateway(hass, config_entry)
|
2019-04-05 00:48:24 +00:00
|
|
|
new_master_gateway = next(iter(hass.data[DOMAIN].values()))
|
2019-09-05 23:38:00 +00:00
|
|
|
await async_update_master_gateway(hass, new_master_gateway.config_entry)
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2018-10-31 21:38:04 +00:00
|
|
|
return await gateway.async_reset()
|
2019-04-05 00:48:24 +00:00
|
|
|
|
|
|
|
|
2021-12-01 17:59:52 +00:00
|
|
|
async def async_update_master_gateway(
|
|
|
|
hass: HomeAssistant, config_entry: ConfigEntry
|
|
|
|
) -> None:
|
2019-09-05 23:38:00 +00:00
|
|
|
"""Update master gateway boolean.
|
2019-04-05 00:48:24 +00:00
|
|
|
|
|
|
|
Called by setup_entry and unload_entry.
|
|
|
|
Makes sure there is always one master available.
|
|
|
|
"""
|
2021-12-01 17:59:52 +00:00
|
|
|
try:
|
|
|
|
master_gateway = get_master_gateway(hass)
|
|
|
|
master = master_gateway.config_entry == config_entry
|
|
|
|
except ValueError:
|
|
|
|
master = True
|
|
|
|
|
2019-09-25 16:56:31 +00:00
|
|
|
options = {**config_entry.options, CONF_MASTER_GATEWAY: master}
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2019-09-25 16:56:31 +00:00
|
|
|
hass.config_entries.async_update_entry(config_entry, options=options)
|
2021-02-06 13:32:17 +00:00
|
|
|
|
|
|
|
|
2021-12-01 17:59:52 +00:00
|
|
|
async def async_update_group_unique_id(
|
|
|
|
hass: HomeAssistant, config_entry: ConfigEntry
|
|
|
|
) -> None:
|
2021-02-06 13:32:17 +00:00
|
|
|
"""Update unique ID entities based on deCONZ groups."""
|
2022-02-23 12:10:35 +00:00
|
|
|
if not (group_id_base := config_entry.data.get(CONF_GROUP_ID_BASE)):
|
2021-02-06 13:32:17 +00:00
|
|
|
return
|
|
|
|
|
2022-02-23 12:10:35 +00:00
|
|
|
old_unique_id = cast(str, group_id_base)
|
2021-12-01 17:59:52 +00:00
|
|
|
new_unique_id = cast(str, config_entry.unique_id)
|
2021-02-06 13:32:17 +00:00
|
|
|
|
|
|
|
@callback
|
2021-12-01 17:59:52 +00:00
|
|
|
def update_unique_id(entity_entry: er.RegistryEntry) -> dict[str, str] | None:
|
2021-02-06 13:32:17 +00:00
|
|
|
"""Update unique ID of entity entry."""
|
|
|
|
if f"{old_unique_id}-" not in entity_entry.unique_id:
|
|
|
|
return None
|
|
|
|
return {
|
|
|
|
"new_unique_id": entity_entry.unique_id.replace(
|
|
|
|
old_unique_id, new_unique_id
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-01 17:59:52 +00:00
|
|
|
await er.async_migrate_entries(hass, config_entry.entry_id, update_unique_id)
|
2021-02-06 13:32:17 +00:00
|
|
|
data = {
|
|
|
|
CONF_API_KEY: config_entry.data[CONF_API_KEY],
|
|
|
|
CONF_HOST: config_entry.data[CONF_HOST],
|
|
|
|
CONF_PORT: config_entry.data[CONF_PORT],
|
|
|
|
}
|
|
|
|
hass.config_entries.async_update_entry(config_entry, data=data)
|