Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for deCONZ devices."""
2 
3 from __future__ import annotations
4 
5 from homeassistant.config_entries import ConfigEntry
6 from homeassistant.const import EVENT_HOMEASSISTANT_STOP
7 from homeassistant.core import HomeAssistant
8 from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
9 from homeassistant.helpers import config_validation as cv
10 from homeassistant.helpers.typing import ConfigType
11 
12 from .config_flow import get_master_hub
13 from .const import CONF_MASTER_GATEWAY, DOMAIN, PLATFORMS
14 from .deconz_event import async_setup_events, async_unload_events
15 from .errors import AuthenticationRequired, CannotConnect
16 from .hub import DeconzHub, get_deconz_api
17 from .services import async_setup_services
18 
19 CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
20 
21 
22 async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
23  """Set up services."""
25  return True
26 
27 
28 async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
29  """Set up a deCONZ bridge for a config entry.
30 
31  Load config, group, light and sensor data for server information.
32  Start websocket for push notification of state changes from deCONZ.
33  """
34  hass.data.setdefault(DOMAIN, {})
35 
36  if not config_entry.options:
37  await async_update_master_hub(hass, config_entry)
38 
39  try:
40  api = await get_deconz_api(hass, config_entry)
41  except CannotConnect as err:
42  raise ConfigEntryNotReady from err
43  except AuthenticationRequired as err:
44  raise ConfigEntryAuthFailed from err
45 
46  hub = hass.data[DOMAIN][config_entry.entry_id] = DeconzHub(hass, config_entry, api)
47  await hub.async_update_device_registry()
48 
49  config_entry.add_update_listener(hub.async_config_entry_updated)
50 
51  await async_setup_events(hub)
52  await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
53 
54  api.start()
55 
56  config_entry.async_on_unload(
57  hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, hub.shutdown)
58  )
59 
60  return True
61 
62 
63 async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
64  """Unload deCONZ config entry."""
65  hub: DeconzHub = hass.data[DOMAIN].pop(config_entry.entry_id)
67 
68  if hass.data[DOMAIN] and hub.master:
69  await async_update_master_hub(hass, config_entry)
70  new_master_hub = next(iter(hass.data[DOMAIN].values()))
71  await async_update_master_hub(hass, new_master_hub.config_entry)
72 
73  return await hub.async_reset()
74 
75 
77  hass: HomeAssistant, config_entry: ConfigEntry
78 ) -> None:
79  """Update master hub boolean.
80 
81  Called by setup_entry and unload_entry.
82  Makes sure there is always one master available.
83  """
84  try:
85  master_hub = get_master_hub(hass)
86  master = master_hub.config_entry == config_entry
87  except ValueError:
88  master = True
89 
90  options = {**config_entry.options, CONF_MASTER_GATEWAY: master}
91 
92  hass.config_entries.async_update_entry(config_entry, options=options)
DeconzHub get_master_hub(HomeAssistant hass)
Definition: config_flow.py:55
DeconzSession get_deconz_api(HomeAssistant hass, ConfigEntry config_entry)
Definition: api.py:20
bool async_unload_entry(HomeAssistant hass, ConfigEntry config_entry)
Definition: __init__.py:63
None async_update_master_hub(HomeAssistant hass, ConfigEntry config_entry)
Definition: __init__.py:78
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:22
bool async_setup_entry(HomeAssistant hass, ConfigEntry config_entry)
Definition: __init__.py:28
None async_setup_services(HomeAssistant hass)
Definition: __init__.py:72