Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for Yale devices."""
2 
3 from __future__ import annotations
4 
5 from pathlib import Path
6 from typing import cast
7 
8 from aiohttp import ClientResponseError
9 from yalexs.const import Brand
10 from yalexs.exceptions import YaleApiError
11 from yalexs.manager.const import CONF_BRAND
12 from yalexs.manager.exceptions import CannotConnect, InvalidAuth, RequireValidation
13 from yalexs.manager.gateway import Config as YaleXSConfig
14 
15 from homeassistant.config_entries import ConfigEntry
16 from homeassistant.const import EVENT_HOMEASSISTANT_STOP
17 from homeassistant.core import HomeAssistant
18 from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
19 from homeassistant.helpers import config_entry_oauth2_flow, device_registry as dr
20 
21 from .const import DOMAIN, PLATFORMS
22 from .data import YaleData
23 from .gateway import YaleGateway
24 from .util import async_create_yale_clientsession
25 
26 type YaleConfigEntry = ConfigEntry[YaleData]
27 
28 
29 async def async_setup_entry(hass: HomeAssistant, entry: YaleConfigEntry) -> bool:
30  """Set up yale from a config entry."""
31  session = async_create_yale_clientsession(hass)
32  implementation = (
33  await config_entry_oauth2_flow.async_get_config_entry_implementation(
34  hass, entry
35  )
36  )
37  oauth_session = config_entry_oauth2_flow.OAuth2Session(hass, entry, implementation)
38  yale_gateway = YaleGateway(Path(hass.config.config_dir), session, oauth_session)
39  try:
40  await async_setup_yale(hass, entry, yale_gateway)
41  except (RequireValidation, InvalidAuth) as err:
42  raise ConfigEntryAuthFailed from err
43  except TimeoutError as err:
44  raise ConfigEntryNotReady("Timed out connecting to yale api") from err
45  except (YaleApiError, ClientResponseError, CannotConnect) as err:
46  raise ConfigEntryNotReady from err
47  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
48  return True
49 
50 
51 async def async_unload_entry(hass: HomeAssistant, entry: YaleConfigEntry) -> bool:
52  """Unload a config entry."""
53  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
54 
55 
56 async def async_setup_yale(
57  hass: HomeAssistant, entry: YaleConfigEntry, yale_gateway: YaleGateway
58 ) -> None:
59  """Set up the yale component."""
60  config = cast(YaleXSConfig, entry.data)
61  await yale_gateway.async_setup({**config, CONF_BRAND: Brand.YALE_GLOBAL})
62  await yale_gateway.async_authenticate()
63  await yale_gateway.async_refresh_access_token_if_needed()
64  data = entry.runtime_data = YaleData(hass, yale_gateway)
65  entry.async_on_unload(
66  hass.bus.async_listen(EVENT_HOMEASSISTANT_STOP, data.async_stop)
67  )
68  entry.async_on_unload(data.async_stop)
69  await data.async_setup()
70 
71 
73  hass: HomeAssistant, config_entry: YaleConfigEntry, device_entry: dr.DeviceEntry
74 ) -> bool:
75  """Remove yale config entry from a device if its no longer present."""
76  return not any(
77  identifier
78  for identifier in device_entry.identifiers
79  if identifier[0] == DOMAIN
80  and config_entry.runtime_data.get_device(identifier[1])
81  )
aiohttp.ClientSession async_create_yale_clientsession(HomeAssistant hass)
Definition: util.py:23
bool async_unload_entry(HomeAssistant hass, YaleConfigEntry entry)
Definition: __init__.py:51
bool async_setup_entry(HomeAssistant hass, YaleConfigEntry entry)
Definition: __init__.py:29
bool async_remove_config_entry_device(HomeAssistant hass, YaleConfigEntry config_entry, dr.DeviceEntry device_entry)
Definition: __init__.py:74
None async_setup_yale(HomeAssistant hass, YaleConfigEntry entry, YaleGateway yale_gateway)
Definition: __init__.py:58