Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """Sense Coordinators."""
2 
3 from datetime import timedelta
4 import logging
5 
6 from sense_energy import (
7  ASyncSenseable,
8  SenseAuthenticationException,
9  SenseMFARequiredException,
10 )
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 (
17  ACTIVE_UPDATE_RATE,
18  SENSE_CONNECT_EXCEPTIONS,
19  SENSE_TIMEOUT_EXCEPTIONS,
20  SENSE_WEBSOCKET_EXCEPTIONS,
21  TREND_UPDATE_RATE,
22 )
23 
24 _LOGGER = logging.getLogger(__name__)
25 
26 
28  """Sense Trend Coordinator."""
29 
30  def __init__(
31  self, hass: HomeAssistant, gateway: ASyncSenseable, name: str, update: int
32  ) -> None:
33  """Initialize."""
34  super().__init__(
35  hass,
36  logger=_LOGGER,
37  name=f"Sense {name} {gateway.sense_monitor_id}",
38  update_interval=timedelta(seconds=update),
39  )
40  self._gateway_gateway = gateway
41  self.last_update_successlast_update_successlast_update_success = False
42 
43 
45  """Sense Trend Coordinator."""
46 
47  def __init__(self, hass: HomeAssistant, gateway: ASyncSenseable) -> None:
48  """Initialize."""
49  super().__init__(hass, gateway, "Trends", TREND_UPDATE_RATE)
50 
51  async def _async_update_data(self) -> None:
52  """Update the trend data."""
53  try:
54  await self._gateway_gateway.update_trend_data()
55  except (SenseAuthenticationException, SenseMFARequiredException) as err:
56  _LOGGER.warning("Sense authentication expired")
57  raise ConfigEntryAuthFailed(err) from err
58  except SENSE_CONNECT_EXCEPTIONS as err:
59  raise UpdateFailed(err) from err
60 
61 
63  """Sense Realtime Coordinator."""
64 
65  def __init__(self, hass: HomeAssistant, gateway: ASyncSenseable) -> None:
66  """Initialize."""
67  super().__init__(hass, gateway, "Realtime", ACTIVE_UPDATE_RATE)
68 
69  async def _async_update_data(self) -> None:
70  """Retrieve latest state."""
71  try:
72  await self._gateway_gateway.update_realtime()
73  except SENSE_TIMEOUT_EXCEPTIONS as ex:
74  _LOGGER.error("Timeout retrieving data: %s", ex)
75  except SENSE_WEBSOCKET_EXCEPTIONS as ex:
76  _LOGGER.error("Failed to update data: %s", ex)
None __init__(self, HomeAssistant hass, ASyncSenseable gateway, str name, int update)
Definition: coordinator.py:32
None __init__(self, HomeAssistant hass, ASyncSenseable gateway)
Definition: coordinator.py:65
None __init__(self, HomeAssistant hass, ASyncSenseable gateway)
Definition: coordinator.py:47