* refoss * refoss * refoss * refoss * refoss modify * ip * 8.22 * format * format * format * bugfix * test * test * test * test * test * test * 9.1 * refosss * refoss * refoss * refoss * refoss * refoss * refoss * refoss * test * requirements_test_all.txt * codeowners * refoss * Review feedback repair * strings * refoss * refoss * refoss * 1.1.1 * 1.1.2 * refoss * refoss * refoss.1.1.7 * refoss-gree * 1.1.7 * test * refoss * test refoss * test refoss * refoss-test * refoss * refoss * test * test * refoss * CODEOWNERS * fix * Update homeassistant/components/refoss/__init__.py --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
39 lines
1.3 KiB
Python
39 lines
1.3 KiB
Python
"""Helper and coordinator for refoss."""
|
|
from __future__ import annotations
|
|
|
|
from datetime import timedelta
|
|
|
|
from refoss_ha.controller.device import BaseDevice
|
|
from refoss_ha.exceptions import DeviceTimeoutError
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
|
|
|
from .const import _LOGGER, DOMAIN, MAX_ERRORS
|
|
|
|
|
|
class RefossDataUpdateCoordinator(DataUpdateCoordinator[None]):
|
|
"""Manages polling for state changes from the device."""
|
|
|
|
def __init__(self, hass: HomeAssistant, device: BaseDevice) -> None:
|
|
"""Initialize the data update coordinator."""
|
|
super().__init__(
|
|
hass,
|
|
_LOGGER,
|
|
name=f"{DOMAIN}-{device.device_info.dev_name}",
|
|
update_interval=timedelta(seconds=15),
|
|
)
|
|
self.device = device
|
|
self._error_count = 0
|
|
|
|
async def _async_update_data(self) -> None:
|
|
"""Update the state of the device."""
|
|
try:
|
|
await self.device.async_handle_update()
|
|
self.last_update_success = True
|
|
self._error_count = 0
|
|
except DeviceTimeoutError:
|
|
self._error_count += 1
|
|
|
|
if self._error_count >= MAX_ERRORS:
|
|
self.last_update_success = False
|