Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for August 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 AugustApiAIOHTTPError
11 from yalexs.manager.exceptions import CannotConnect, InvalidAuth, RequireValidation
12 from yalexs.manager.gateway import Config as YaleXSConfig
13 
14 from homeassistant.config_entries import ConfigEntry
15 from homeassistant.const import EVENT_HOMEASSISTANT_STOP
16 from homeassistant.core import HomeAssistant, callback
17 from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
18 from homeassistant.helpers import device_registry as dr, issue_registry as ir
19 
20 from .const import DOMAIN, PLATFORMS
21 from .data import AugustData
22 from .gateway import AugustGateway
23 from .util import async_create_august_clientsession
24 
25 type AugustConfigEntry = ConfigEntry[AugustData]
26 
27 
28 @callback
30  hass: HomeAssistant, entry: AugustConfigEntry
31 ) -> None:
32  """Create an issue for a brand migration."""
33  ir.async_create_issue(
34  hass,
35  DOMAIN,
36  "yale_brand_migration",
37  breaks_in_ha_version="2024.9",
38  learn_more_url="https://www.home-assistant.io/integrations/yale",
39  translation_key="yale_brand_migration",
40  is_fixable=False,
41  severity=ir.IssueSeverity.CRITICAL,
42  translation_placeholders={
43  "migrate_url": "https://my.home-assistant.io/redirect/config_flow_start?domain=yale"
44  },
45  )
46 
47 
48 async def async_setup_entry(hass: HomeAssistant, entry: AugustConfigEntry) -> bool:
49  """Set up August from a config entry."""
50  session = async_create_august_clientsession(hass)
51  august_gateway = AugustGateway(Path(hass.config.config_dir), session)
52  try:
53  await async_setup_august(hass, entry, august_gateway)
54  except (RequireValidation, InvalidAuth) as err:
55  raise ConfigEntryAuthFailed from err
56  except TimeoutError as err:
57  raise ConfigEntryNotReady("Timed out connecting to august api") from err
58  except (AugustApiAIOHTTPError, ClientResponseError, CannotConnect) as err:
59  raise ConfigEntryNotReady from err
60  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
61  return True
62 
63 
64 async def async_remove_entry(hass: HomeAssistant, entry: AugustConfigEntry) -> None:
65  """Remove an August config entry."""
66  ir.async_delete_issue(hass, DOMAIN, "yale_brand_migration")
67 
68 
69 async def async_unload_entry(hass: HomeAssistant, entry: AugustConfigEntry) -> bool:
70  """Unload a config entry."""
71  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
72 
73 
75  hass: HomeAssistant, entry: AugustConfigEntry, august_gateway: AugustGateway
76 ) -> None:
77  """Set up the August component."""
78  config = cast(YaleXSConfig, entry.data)
79  await august_gateway.async_setup(config)
80  if august_gateway.api.brand == Brand.YALE_HOME:
82  await august_gateway.async_authenticate()
83  await august_gateway.async_refresh_access_token_if_needed()
84  data = entry.runtime_data = AugustData(hass, august_gateway)
85  entry.async_on_unload(
86  hass.bus.async_listen(EVENT_HOMEASSISTANT_STOP, data.async_stop)
87  )
88  entry.async_on_unload(data.async_stop)
89  await data.async_setup()
90 
91 
93  hass: HomeAssistant, config_entry: AugustConfigEntry, device_entry: dr.DeviceEntry
94 ) -> bool:
95  """Remove august config entry from a device if its no longer present."""
96  return not any(
97  identifier
98  for identifier in device_entry.identifiers
99  if identifier[0] == DOMAIN
100  and config_entry.runtime_data.get_device(identifier[1])
101  )
aiohttp.ClientSession async_create_august_clientsession(HomeAssistant hass)
Definition: util.py:23
None async_setup_august(HomeAssistant hass, AugustConfigEntry entry, AugustGateway august_gateway)
Definition: __init__.py:76
bool async_remove_config_entry_device(HomeAssistant hass, AugustConfigEntry config_entry, dr.DeviceEntry device_entry)
Definition: __init__.py:94
bool async_setup_entry(HomeAssistant hass, AugustConfigEntry entry)
Definition: __init__.py:48
bool async_unload_entry(HomeAssistant hass, AugustConfigEntry entry)
Definition: __init__.py:69
None async_remove_entry(HomeAssistant hass, AugustConfigEntry entry)
Definition: __init__.py:64
None _async_create_yale_brand_migration_issue(HomeAssistant hass, AugustConfigEntry entry)
Definition: __init__.py:31