Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for the Skybell HD Doorbell."""
2 
3 from __future__ import annotations
4 
5 import asyncio
6 
7 from aioskybell import Skybell
8 from aioskybell.exceptions import SkybellAuthenticationException, SkybellException
9 
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, Platform
12 from homeassistant.core import HomeAssistant
13 from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
14 from homeassistant.helpers.aiohttp_client import async_get_clientsession
15 
16 from .const import DOMAIN
17 from .coordinator import SkybellDataUpdateCoordinator
18 
19 PLATFORMS = [
20  Platform.BINARY_SENSOR,
21  Platform.CAMERA,
22  Platform.LIGHT,
23  Platform.SENSOR,
24  Platform.SWITCH,
25 ]
26 
27 
28 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
29  """Set up Skybell from a config entry."""
30  email = entry.data[CONF_EMAIL]
31  password = entry.data[CONF_PASSWORD]
32 
33  api = Skybell(
34  username=email,
35  password=password,
36  get_devices=True,
37  cache_path=hass.config.path(f"./skybell_{entry.unique_id}.pickle"),
38  session=async_get_clientsession(hass),
39  )
40  try:
41  devices = await api.async_initialize()
42  except SkybellAuthenticationException as ex:
43  raise ConfigEntryAuthFailed from ex
44  except SkybellException as ex:
45  raise ConfigEntryNotReady(f"Unable to connect to Skybell service: {ex}") from ex
46 
47  device_coordinators: list[SkybellDataUpdateCoordinator] = [
48  SkybellDataUpdateCoordinator(hass, device) for device in devices
49  ]
50  await asyncio.gather(
51  *[
52  coordinator.async_config_entry_first_refresh()
53  for coordinator in device_coordinators
54  ]
55  )
56  hass.data.setdefault(DOMAIN, {})[entry.entry_id] = device_coordinators
57  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
58 
59  return True
60 
61 
62 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
63  """Unload a config entry."""
64  if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
65  hass.data[DOMAIN].pop(entry.entry_id)
66  return unload_ok
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:28
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:62
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)