2019-02-13 20:21:14 +00:00
|
|
|
"""Support for August devices."""
|
2024-03-08 13:51:32 +00:00
|
|
|
|
2022-03-15 18:05:56 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-06-10 19:50:11 +00:00
|
|
|
from typing import cast
|
2018-02-18 08:24:51 +00:00
|
|
|
|
2024-06-10 19:50:11 +00:00
|
|
|
from aiohttp import ClientResponseError
|
2024-06-09 10:30:41 +00:00
|
|
|
from path import Path
|
2021-03-22 05:35:12 +00:00
|
|
|
from yalexs.exceptions import AugustApiAIOHTTPError
|
2024-06-09 10:30:41 +00:00
|
|
|
from yalexs.manager.exceptions import CannotConnect, InvalidAuth, RequireValidation
|
|
|
|
from yalexs.manager.gateway import Config as YaleXSConfig
|
2021-03-16 23:22:07 +00:00
|
|
|
|
2024-06-10 19:50:11 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2024-06-19 14:21:04 +00:00
|
|
|
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
2024-06-10 19:50:11 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
|
|
|
from homeassistant.helpers import device_registry as dr
|
2018-02-18 08:24:51 +00:00
|
|
|
|
2024-06-10 19:50:11 +00:00
|
|
|
from .const import DOMAIN, PLATFORMS
|
|
|
|
from .data import AugustData
|
2020-02-25 18:18:15 +00:00
|
|
|
from .gateway import AugustGateway
|
2023-10-25 04:08:41 +00:00
|
|
|
from .util import async_create_august_clientsession
|
2020-02-08 19:22:48 +00:00
|
|
|
|
2024-05-17 13:42:58 +00:00
|
|
|
type AugustConfigEntry = ConfigEntry[AugustData]
|
2024-05-02 12:39:02 +00:00
|
|
|
|
2018-02-18 08:24:51 +00:00
|
|
|
|
2021-10-06 08:48:11 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2020-02-25 18:18:15 +00:00
|
|
|
"""Set up August from a config entry."""
|
2023-10-25 04:08:41 +00:00
|
|
|
session = async_create_august_clientsession(hass)
|
2024-06-09 10:30:41 +00:00
|
|
|
august_gateway = AugustGateway(Path(hass.config.config_dir), session)
|
2020-03-11 00:10:00 +00:00
|
|
|
try:
|
2024-06-19 14:21:04 +00:00
|
|
|
await async_setup_august(hass, entry, august_gateway)
|
2021-04-10 05:41:29 +00:00
|
|
|
except (RequireValidation, InvalidAuth) as err:
|
|
|
|
raise ConfigEntryAuthFailed from err
|
2024-02-05 10:31:33 +00:00
|
|
|
except TimeoutError as err:
|
2022-01-31 04:50:49 +00:00
|
|
|
raise ConfigEntryNotReady("Timed out connecting to august api") from err
|
2022-02-07 23:45:40 +00:00
|
|
|
except (AugustApiAIOHTTPError, ClientResponseError, CannotConnect) as err:
|
2020-08-28 11:50:32 +00:00
|
|
|
raise ConfigEntryNotReady from err
|
2024-06-19 14:21:04 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
|
|
|
return True
|
2020-02-16 05:08:52 +00:00
|
|
|
|
2020-02-25 18:18:15 +00:00
|
|
|
|
2024-05-02 12:39:02 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: AugustConfigEntry) -> bool:
|
2020-02-25 18:18:15 +00:00
|
|
|
"""Unload a config entry."""
|
2024-05-02 12:39:02 +00:00
|
|
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
2020-02-25 18:18:15 +00:00
|
|
|
|
2018-02-18 08:24:51 +00:00
|
|
|
|
2022-01-04 23:32:11 +00:00
|
|
|
async def async_setup_august(
|
2024-06-10 19:50:11 +00:00
|
|
|
hass: HomeAssistant, entry: AugustConfigEntry, august_gateway: AugustGateway
|
2024-06-19 14:21:04 +00:00
|
|
|
) -> None:
|
2021-03-16 23:22:07 +00:00
|
|
|
"""Set up the August component."""
|
2024-06-10 19:50:11 +00:00
|
|
|
config = cast(YaleXSConfig, entry.data)
|
2024-06-09 10:30:41 +00:00
|
|
|
await august_gateway.async_setup(config)
|
2021-03-16 23:22:07 +00:00
|
|
|
await august_gateway.async_authenticate()
|
2022-02-11 09:15:50 +00:00
|
|
|
await august_gateway.async_refresh_access_token_if_needed()
|
2024-06-19 14:21:04 +00:00
|
|
|
data = entry.runtime_data = AugustData(hass, august_gateway)
|
2024-06-10 19:50:11 +00:00
|
|
|
entry.async_on_unload(
|
|
|
|
hass.bus.async_listen(EVENT_HOMEASSISTANT_STOP, data.async_stop)
|
|
|
|
)
|
|
|
|
entry.async_on_unload(data.async_stop)
|
2022-03-15 18:05:56 +00:00
|
|
|
await data.async_setup()
|
2021-03-16 23:22:07 +00:00
|
|
|
|
|
|
|
|
2022-06-10 07:53:42 +00:00
|
|
|
async def async_remove_config_entry_device(
|
2024-05-02 12:39:02 +00:00
|
|
|
hass: HomeAssistant, config_entry: AugustConfigEntry, device_entry: dr.DeviceEntry
|
2022-06-10 07:53:42 +00:00
|
|
|
) -> bool:
|
|
|
|
"""Remove august config entry from a device if its no longer present."""
|
|
|
|
return not any(
|
|
|
|
identifier
|
|
|
|
for identifier in device_entry.identifiers
|
2024-05-02 12:39:02 +00:00
|
|
|
if identifier[0] == DOMAIN
|
|
|
|
and config_entry.runtime_data.get_device(identifier[1])
|
2022-06-10 07:53:42 +00:00
|
|
|
)
|