Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """DataUpdateCoordinator for the Sensibo integration."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 from typing import TYPE_CHECKING
7 
8 from pysensibo import SensiboClient
9 from pysensibo.exceptions import AuthenticationError, SensiboError
10 from pysensibo.model import SensiboData
11 
12 from homeassistant.const import CONF_API_KEY
13 from homeassistant.core import HomeAssistant
14 from homeassistant.exceptions import ConfigEntryAuthFailed
15 from homeassistant.helpers.aiohttp_client import async_get_clientsession
16 from homeassistant.helpers.debounce import Debouncer
17 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
18 
19 from .const import DEFAULT_SCAN_INTERVAL, DOMAIN, LOGGER, TIMEOUT
20 
21 if TYPE_CHECKING:
22  from . import SensiboConfigEntry
23 
24 REQUEST_REFRESH_DELAY = 0.35
25 
26 
28  """A Sensibo Data Update Coordinator."""
29 
30  config_entry: SensiboConfigEntry
31 
32  def __init__(self, hass: HomeAssistant) -> None:
33  """Initialize the Sensibo coordinator."""
34  super().__init__(
35  hass,
36  LOGGER,
37  name=DOMAIN,
38  update_interval=timedelta(seconds=DEFAULT_SCAN_INTERVAL),
39  # We don't want an immediate refresh since the device
40  # takes a moment to reflect the state change
41  request_refresh_debouncer=Debouncer(
42  hass, LOGGER, cooldown=REQUEST_REFRESH_DELAY, immediate=False
43  ),
44  )
45  self.clientclient = SensiboClient(
46  self.config_entryconfig_entry.data[CONF_API_KEY],
47  session=async_get_clientsession(hass),
48  timeout=TIMEOUT,
49  )
50 
51  async def _async_update_data(self) -> SensiboData:
52  """Fetch data from Sensibo."""
53  try:
54  data = await self.clientclient.async_get_devices_data()
55  except AuthenticationError as error:
56  raise ConfigEntryAuthFailed from error
57  except SensiboError as error:
58  raise UpdateFailed from error
59 
60  if not data.raw:
61  raise UpdateFailed("No devices found")
62  return data
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)