Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Schlage integration."""
2 
3 from __future__ import annotations
4 
5 from pycognito.exceptions import WarrantException
6 import pyschlage
7 
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
10 from homeassistant.core import HomeAssistant
11 from homeassistant.exceptions import ConfigEntryAuthFailed
12 
13 from .const import DOMAIN
14 from .coordinator import SchlageDataUpdateCoordinator
15 
16 PLATFORMS: list[Platform] = [
17  Platform.BINARY_SENSOR,
18  Platform.LOCK,
19  Platform.SELECT,
20  Platform.SENSOR,
21  Platform.SWITCH,
22 ]
23 
24 
25 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
26  """Set up Schlage from a config entry."""
27  username = entry.data[CONF_USERNAME]
28  password = entry.data[CONF_PASSWORD]
29  try:
30  auth = await hass.async_add_executor_job(pyschlage.Auth, username, password)
31  except WarrantException as ex:
32  raise ConfigEntryAuthFailed from ex
33 
34  coordinator = SchlageDataUpdateCoordinator(hass, username, pyschlage.Schlage(auth))
35  hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
36  await coordinator.async_config_entry_first_refresh()
37  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
38  return True
39 
40 
41 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
42  """Unload a config entry."""
43  if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
44  hass.data[DOMAIN].pop(entry.entry_id)
45 
46  return unload_ok
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:41
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:25