1 """Support for Yale devices."""
3 from __future__
import annotations
5 from pathlib
import Path
6 from typing
import cast
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
21 from .const
import DOMAIN, PLATFORMS
22 from .data
import YaleData
23 from .gateway
import YaleGateway
24 from .util
import async_create_yale_clientsession
26 type YaleConfigEntry = ConfigEntry[YaleData]
30 """Set up yale from a config entry."""
33 await config_entry_oauth2_flow.async_get_config_entry_implementation(
37 oauth_session = config_entry_oauth2_flow.OAuth2Session(hass, entry, implementation)
38 yale_gateway =
YaleGateway(Path(hass.config.config_dir), session, oauth_session)
41 except (RequireValidation, InvalidAuth)
as err:
42 raise ConfigEntryAuthFailed
from err
43 except TimeoutError
as err:
45 except (YaleApiError, ClientResponseError, CannotConnect)
as err:
46 raise ConfigEntryNotReady
from err
47 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
52 """Unload a config entry."""
53 return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
57 hass: HomeAssistant, entry: YaleConfigEntry, yale_gateway: YaleGateway
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)
68 entry.async_on_unload(data.async_stop)
69 await data.async_setup()
73 hass: HomeAssistant, config_entry: YaleConfigEntry, device_entry: dr.DeviceEntry
75 """Remove yale config entry from a device if its no longer present."""
78 for identifier
in device_entry.identifiers
79 if identifier[0] == DOMAIN
80 and config_entry.runtime_data.get_device(identifier[1])
aiohttp.ClientSession async_create_yale_clientsession(HomeAssistant hass)
bool async_unload_entry(HomeAssistant hass, YaleConfigEntry entry)
bool async_setup_entry(HomeAssistant hass, YaleConfigEntry entry)
bool async_remove_config_entry_device(HomeAssistant hass, YaleConfigEntry config_entry, dr.DeviceEntry device_entry)
None async_setup_yale(HomeAssistant hass, YaleConfigEntry entry, YaleGateway yale_gateway)