Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Oncue integration."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 import logging
7 
8 from aiooncue import LoginFailedException, Oncue, OncueDevice
9 
10 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
11 from homeassistant.core import HomeAssistant
12 from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
13 from homeassistant.helpers.aiohttp_client import async_get_clientsession
14 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
15 
16 from .const import CONNECTION_EXCEPTIONS, DOMAIN # noqa: F401
17 from .types import OncueConfigEntry
18 
19 PLATFORMS: list[str] = [Platform.BINARY_SENSOR, Platform.SENSOR]
20 
21 _LOGGER = logging.getLogger(__name__)
22 
23 
24 async def async_setup_entry(hass: HomeAssistant, entry: OncueConfigEntry) -> bool:
25  """Set up Oncue from a config entry."""
26  data = entry.data
27  websession = async_get_clientsession(hass)
28  client = Oncue(data[CONF_USERNAME], data[CONF_PASSWORD], websession)
29  try:
30  await client.async_login()
31  except CONNECTION_EXCEPTIONS as ex:
32  raise ConfigEntryNotReady from ex
33  except LoginFailedException as ex:
34  raise ConfigEntryAuthFailed from ex
35 
36  async def _async_update() -> dict[str, OncueDevice]:
37  """Fetch data from Oncue."""
38  try:
39  return await client.async_fetch_all()
40  except LoginFailedException as ex:
41  raise ConfigEntryAuthFailed from ex
42 
43  coordinator = DataUpdateCoordinator[dict[str, OncueDevice]](
44  hass,
45  _LOGGER,
46  config_entry=entry,
47  name=f"Oncue {entry.data[CONF_USERNAME]}",
48  update_interval=timedelta(minutes=10),
49  update_method=_async_update,
50  always_update=False,
51  )
52  await coordinator.async_config_entry_first_refresh()
53 
54  entry.runtime_data = coordinator
55  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
56  return True
57 
58 
59 async def async_unload_entry(hass: HomeAssistant, entry: OncueConfigEntry) -> bool:
60  """Unload a config entry."""
61  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_unload_entry(HomeAssistant hass, OncueConfigEntry entry)
Definition: __init__.py:59
bool async_setup_entry(HomeAssistant hass, OncueConfigEntry entry)
Definition: __init__.py:24
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)