Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The deako integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from pydeako import Deako, DeakoDiscoverer, FindDevicesError
8 
9 from homeassistant.components import zeroconf
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import Platform
12 from homeassistant.core import HomeAssistant
13 from homeassistant.exceptions import ConfigEntryNotReady
14 
15 _LOGGER: logging.Logger = logging.getLogger(__name__)
16 
17 PLATFORMS: list[Platform] = [Platform.LIGHT]
18 
19 type DeakoConfigEntry = ConfigEntry[Deako]
20 
21 
22 async def async_setup_entry(hass: HomeAssistant, entry: DeakoConfigEntry) -> bool:
23  """Set up deako."""
24  _zc = await zeroconf.async_get_instance(hass)
25  discoverer = DeakoDiscoverer(_zc)
26 
27  connection = Deako(discoverer.get_address)
28 
29  await connection.connect()
30  try:
31  await connection.find_devices()
32  except FindDevicesError as exc:
33  _LOGGER.warning("Error finding devices: %s", exc)
34  await connection.disconnect()
35  raise ConfigEntryNotReady(exc) from exc
36 
37  # If deako devices are advertising on mdns, we should be able to get at least one device
38  devices = connection.get_devices()
39  if len(devices) == 0:
40  await connection.disconnect()
41  raise ConfigEntryNotReady(devices)
42 
43  entry.runtime_data = connection
44 
45  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
46 
47  return True
48 
49 
50 async def async_unload_entry(hass: HomeAssistant, entry: DeakoConfigEntry) -> bool:
51  """Unload a config entry."""
52  await entry.runtime_data.disconnect()
53 
54  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_setup_entry(HomeAssistant hass, DeakoConfigEntry entry)
Definition: __init__.py:22
bool async_unload_entry(HomeAssistant hass, DeakoConfigEntry entry)
Definition: __init__.py:50