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 homeassistant.config_entries import ConfigEntry
|
2024-03-15 19:09:44 +00:00
|
|
|
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
|
|
|
from homeassistant.core import HomeAssistant
|
2022-02-23 12:10:35 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
2018-01-01 16:08:13 +00:00
|
|
|
|
2019-04-05 00:48:24 +00:00
|
|
|
from .config_flow import get_master_gateway
|
2024-03-15 19:09:44 +00:00
|
|
|
from .const import CONF_MASTER_GATEWAY, DOMAIN, PLATFORMS
|
2022-02-23 12:10:35 +00:00
|
|
|
from .deconz_event import async_setup_events, async_unload_events
|
|
|
|
from .errors import AuthenticationRequired, CannotConnect
|
2024-03-10 07:25:12 +00:00
|
|
|
from .hub import DeconzHub, get_deconz_api
|
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
|
|
|
|
|
|
|
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:
|
2024-03-13 21:49:49 +00:00
|
|
|
api = await get_deconz_api(hass, config_entry)
|
2022-02-23 12:10:35 +00:00
|
|
|
except CannotConnect as err:
|
|
|
|
raise ConfigEntryNotReady from err
|
|
|
|
except AuthenticationRequired as err:
|
|
|
|
raise ConfigEntryAuthFailed from err
|
|
|
|
|
2022-09-02 01:33:55 +00:00
|
|
|
if not hass.data[DOMAIN]:
|
|
|
|
async_setup_services(hass)
|
|
|
|
|
2024-03-10 07:25:12 +00:00
|
|
|
gateway = hass.data[DOMAIN][config_entry.entry_id] = DeconzHub(
|
2022-02-23 12:10:35 +00:00
|
|
|
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
|
|
|
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."""
|
2024-03-10 07:25:12 +00:00
|
|
|
gateway: DeconzHub = hass.data[DOMAIN].pop(config_entry.entry_id)
|
2022-02-23 12:10:35 +00:00
|
|
|
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)
|