Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The totalconnect component."""
2 
3 from total_connect_client.client import TotalConnectClient
4 from total_connect_client.exceptions import AuthenticationError
5 
6 from homeassistant.config_entries import ConfigEntry
7 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
8 from homeassistant.core import HomeAssistant
9 from homeassistant.exceptions import ConfigEntryAuthFailed
10 
11 from .const import AUTO_BYPASS, CONF_USERCODES, DOMAIN
12 from .coordinator import TotalConnectDataUpdateCoordinator
13 
14 PLATFORMS = [Platform.ALARM_CONTROL_PANEL, Platform.BINARY_SENSOR, Platform.BUTTON]
15 
16 
17 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
18  """Set up upon config entry in user interface."""
19  conf = entry.data
20  username = conf[CONF_USERNAME]
21  password = conf[CONF_PASSWORD]
22  bypass = entry.options.get(AUTO_BYPASS, False)
23 
24  if CONF_USERCODES not in conf:
25  # should only happen for those who used UI before we added usercodes
26  raise ConfigEntryAuthFailed("No usercodes in TotalConnect configuration")
27 
28  temp_codes = conf[CONF_USERCODES]
29  usercodes = {int(code): temp_codes[code] for code in temp_codes}
30 
31  try:
32  client = await hass.async_add_executor_job(
33  TotalConnectClient, username, password, usercodes, bypass
34  )
35  except AuthenticationError as exception:
37  "TotalConnect authentication failed during setup"
38  ) from exception
39 
40  coordinator = TotalConnectDataUpdateCoordinator(hass, client)
41  await coordinator.async_config_entry_first_refresh()
42 
43  hass.data.setdefault(DOMAIN, {})
44  hass.data[DOMAIN][entry.entry_id] = coordinator
45  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
46 
47  entry.async_on_unload(entry.add_update_listener(update_listener))
48 
49  return True
50 
51 
52 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
53  """Unload a config entry."""
54  unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
55  if unload_ok:
56  hass.data[DOMAIN].pop(entry.entry_id)
57 
58  return unload_ok
59 
60 
61 async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
62  """Update listener."""
63  bypass = entry.options.get(AUTO_BYPASS, False)
64  client = hass.data[DOMAIN][entry.entry_id].client
65  for location_id in client.locations:
66  client.locations[location_id].auto_bypass_low_battery = bypass
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:17
None update_listener(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:61
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:52