Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """The data update coordinator for the A. O. Smith integration."""
2 
3 import logging
4 
5 from py_aosmith import (
6  AOSmithAPIClient,
7  AOSmithInvalidCredentialsException,
8  AOSmithUnknownException,
9 )
10 from py_aosmith.models import Device as AOSmithDevice
11 
12 from homeassistant.core import HomeAssistant
13 from homeassistant.exceptions import ConfigEntryAuthFailed
14 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
15 
16 from .const import DOMAIN, ENERGY_USAGE_INTERVAL, FAST_INTERVAL, REGULAR_INTERVAL
17 
18 _LOGGER = logging.getLogger(__name__)
19 
20 
21 class AOSmithStatusCoordinator(DataUpdateCoordinator[dict[str, AOSmithDevice]]):
22  """Coordinator for device status, updating with a frequent interval."""
23 
24  def __init__(self, hass: HomeAssistant, client: AOSmithAPIClient) -> None:
25  """Initialize the coordinator."""
26  super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=REGULAR_INTERVAL)
27  self.clientclient = client
28 
29  async def _async_update_data(self) -> dict[str, AOSmithDevice]:
30  """Fetch latest data from the device status endpoint."""
31  try:
32  devices = await self.clientclient.get_devices()
33  except AOSmithInvalidCredentialsException as err:
34  raise ConfigEntryAuthFailed from err
35  except AOSmithUnknownException as err:
36  raise UpdateFailed(f"Error communicating with API: {err}") from err
37 
38  mode_pending = any(device.status.mode_change_pending for device in devices)
39  setpoint_pending = any(
40  device.status.temperature_setpoint_pending for device in devices
41  )
42 
43  if mode_pending or setpoint_pending:
45  else:
46  self.update_intervalupdate_intervalupdate_intervalupdate_intervalupdate_interval = REGULAR_INTERVAL
47 
48  return {device.junction_id: device for device in devices}
49 
50 
52  """Coordinator for energy usage data, updating with a slower interval."""
53 
54  def __init__(
55  self,
56  hass: HomeAssistant,
57  client: AOSmithAPIClient,
58  junction_ids: list[str],
59  ) -> None:
60  """Initialize the coordinator."""
61  super().__init__(
62  hass, _LOGGER, name=DOMAIN, update_interval=ENERGY_USAGE_INTERVAL
63  )
64  self.clientclient = client
65  self.junction_idsjunction_ids = junction_ids
66 
67  async def _async_update_data(self) -> dict[str, float]:
68  """Fetch latest data from the energy usage endpoint."""
69  energy_usage_by_junction_id: dict[str, float] = {}
70 
71  for junction_id in self.junction_idsjunction_ids:
72  try:
73  energy_usage = await self.clientclient.get_energy_use_data(junction_id)
74  except AOSmithInvalidCredentialsException as err:
75  raise ConfigEntryAuthFailed from err
76  except AOSmithUnknownException as err:
77  raise UpdateFailed(f"Error communicating with API: {err}") from err
78 
79  energy_usage_by_junction_id[junction_id] = energy_usage.lifetime_kwh
80 
81  return energy_usage_by_junction_id
None __init__(self, HomeAssistant hass, AOSmithAPIClient client, list[str] junction_ids)
Definition: coordinator.py:59
None __init__(self, HomeAssistant hass, AOSmithAPIClient client)
Definition: coordinator.py:24