Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Omnilogic integration."""
2 
3 import logging
4 
5 from omnilogic import LoginException, OmniLogic, OmniLogicException
6 
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
9 from homeassistant.core import HomeAssistant
10 from homeassistant.exceptions import ConfigEntryNotReady
11 from homeassistant.helpers import aiohttp_client
12 
13 from .const import (
14  CONF_SCAN_INTERVAL,
15  COORDINATOR,
16  DEFAULT_SCAN_INTERVAL,
17  DOMAIN,
18  OMNI_API,
19 )
20 from .coordinator import OmniLogicUpdateCoordinator
21 
22 _LOGGER = logging.getLogger(__name__)
23 
24 PLATFORMS = [Platform.SENSOR, Platform.SWITCH]
25 
26 
27 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
28  """Set up Omnilogic from a config entry."""
29 
30  conf = entry.data
31  username = conf[CONF_USERNAME]
32  password = conf[CONF_PASSWORD]
33 
34  polling_interval = conf.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
35 
36  session = aiohttp_client.async_get_clientsession(hass)
37 
38  api = OmniLogic(username, password, session)
39 
40  try:
41  await api.connect()
42  await api.get_telemetry_data()
43  except LoginException as error:
44  _LOGGER.error("Login Failed: %s", error)
45  return False
46  except OmniLogicException as error:
47  _LOGGER.debug("OmniLogic API error: %s", error)
48  raise ConfigEntryNotReady from error
49 
50  coordinator = OmniLogicUpdateCoordinator(
51  hass=hass,
52  api=api,
53  name="Omnilogic",
54  config_entry=entry,
55  polling_interval=polling_interval,
56  )
57  await coordinator.async_config_entry_first_refresh()
58 
59  hass.data.setdefault(DOMAIN, {})
60  hass.data[DOMAIN][entry.entry_id] = {
61  COORDINATOR: coordinator,
62  OMNI_API: api,
63  }
64 
65  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
66 
67  return True
68 
69 
70 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
71  """Unload a config entry."""
72  unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
73  if unload_ok:
74  hass.data[DOMAIN].pop(entry.entry_id)
75 
76  return unload_ok
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:70
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:27