Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The loqed integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 import re
7 
8 import aiohttp
9 from loqedAPI import loqed
10 
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.const import Platform
13 from homeassistant.core import HomeAssistant
14 from homeassistant.exceptions import ConfigEntryNotReady
15 from homeassistant.helpers.aiohttp_client import async_get_clientsession
16 
17 from .const import DOMAIN
18 from .coordinator import LoqedDataCoordinator
19 
20 PLATFORMS: list[str] = [Platform.LOCK, Platform.SENSOR]
21 
22 
23 _LOGGER = logging.getLogger(__name__)
24 
25 
26 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
27  """Set up loqed from a config entry."""
28  websession = async_get_clientsession(hass)
29  host = entry.data["bridge_ip"]
30  apiclient = loqed.APIClient(websession, f"http://{host}")
31  api = loqed.LoqedAPI(apiclient)
32 
33  try:
34  lock = await api.async_get_lock(
35  entry.data["lock_key_key"],
36  entry.data["bridge_key"],
37  int(entry.data["lock_key_local_id"]),
38  re.sub(
39  r"LOQED-([a-f0-9]+)\.local", r"\1", entry.data["bridge_mdns_hostname"]
40  ),
41  )
42  except (
43  TimeoutError,
44  aiohttp.ClientError,
45  ) as ex:
46  raise ConfigEntryNotReady(f"Unable to connect to bridge at {host}") from ex
47  coordinator = LoqedDataCoordinator(hass, api, lock, entry)
48  await coordinator.ensure_webhooks()
49 
50  await coordinator.async_config_entry_first_refresh()
51 
52  hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
53 
54  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
55  return True
56 
57 
58 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
59  """Unload a config entry."""
60  coordinator: LoqedDataCoordinator = hass.data[DOMAIN][entry.entry_id]
61 
62  if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
63  hass.data[DOMAIN].pop(entry.entry_id)
64 
65  await coordinator.remove_webhooks()
66 
67  return unload_ok
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:26
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:58
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)