Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """Coordinator for lookin devices."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Awaitable, Callable
6 from datetime import timedelta
7 import logging
8 import time
9 
10 from homeassistant.core import HomeAssistant, callback
11 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
12 
13 from .const import NEVER_TIME, POLLING_FALLBACK_SECONDS
14 
15 _LOGGER = logging.getLogger(__name__)
16 
17 
19  """Keep track of when the last push update was."""
20 
21  def __init__(self, name: str) -> None:
22  """Init the push coordininator."""
23  self.last_updatelast_update = NEVER_TIME
24  self.namename = name
25 
26  def update(self) -> None:
27  """Remember the last push time."""
28  self.last_updatelast_update = time.monotonic()
29 
30  def active(self, interval: timedelta) -> bool:
31  """Check if the last push update was recently."""
32  time_since_last_update = time.monotonic() - self.last_updatelast_update
33  is_active = time_since_last_update < POLLING_FALLBACK_SECONDS
34  _LOGGER.debug(
35  "%s: push updates active: %s (time_since_last_update=%s)",
36  self.namename,
37  is_active,
38  time_since_last_update,
39  )
40  return is_active
41 
42 
43 class LookinDataUpdateCoordinator[_DataT](DataUpdateCoordinator[_DataT]):
44  """DataUpdateCoordinator to gather data for a specific lookin devices."""
45 
46  def __init__(
47  self,
48  hass: HomeAssistant,
49  push_coordinator: LookinPushCoordinator,
50  name: str,
51  update_interval: timedelta | None = None,
52  update_method: Callable[[], Awaitable[_DataT]] | None = None,
53  ) -> None:
54  """Initialize DataUpdateCoordinator to gather data for specific device."""
55  self.push_coordinatorpush_coordinator = push_coordinator
56  super().__init__(
57  hass,
58  _LOGGER,
59  name=name,
60  update_interval=update_interval,
61  update_method=update_method,
62  always_update=False,
63  )
64 
65  @callback
66  def async_set_updated_data(self, data: _DataT) -> None:
67  """Manually update data, notify listeners and reset refresh interval, and remember."""
68  self.push_coordinatorpush_coordinator.update()
69  super().async_set_updated_data(data)
70 
71  async def _async_update_data(self) -> _DataT:
72  """Fetch data only if we have not received a push inside the interval."""
73  interval = self.update_intervalupdate_intervalupdate_intervalupdate_interval
74  if (
75  interval is not None
76  and self.last_update_successlast_update_success
77  and self.datadata
78  and self.push_coordinatorpush_coordinator.active(interval)
79  ):
80  data = self.datadata
81  else:
82  data = await super()._async_update_data()
83  return data
None __init__(self, HomeAssistant hass, LookinPushCoordinator push_coordinator, str name, timedelta|None update_interval=None, Callable[[], Awaitable[_DataT]]|None update_method=None)
Definition: coordinator.py:53
IssData update(pyiss.ISS iss)
Definition: __init__.py:33