Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """Helper and coordinator for refoss."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 
7 from refoss_ha.controller.device import BaseDevice
8 from refoss_ha.exceptions import DeviceTimeoutError
9 
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
12 
13 from .const import _LOGGER, DOMAIN, MAX_ERRORS
14 
15 
17  """Manages polling for state changes from the device."""
18 
19  def __init__(self, hass: HomeAssistant, device: BaseDevice) -> None:
20  """Initialize the data update coordinator."""
21  super().__init__(
22  hass,
23  _LOGGER,
24  name=f"{DOMAIN}-{device.device_info.dev_name}",
25  update_interval=timedelta(seconds=15),
26  )
27  self.devicedevice = device
28  self._error_count_error_count = 0
29 
30  async def _async_update_data(self) -> None:
31  """Update the state of the device."""
32  try:
33  await self.devicedevice.async_handle_update()
34  self.last_update_successlast_update_successlast_update_success = True
35  self._error_count_error_count = 0
36  except DeviceTimeoutError:
37  self._error_count_error_count += 1
38 
39  if self._error_count_error_count >= MAX_ERRORS:
40  self.last_update_successlast_update_successlast_update_success = False
None __init__(self, HomeAssistant hass, BaseDevice device)
Definition: coordinator.py:19