diff --git a/custom_components/hilo/__init__.py b/custom_components/hilo/__init__.py index 03c29fbc..6a1225ff 100644 --- a/custom_components/hilo/__init__.py +++ b/custom_components/hilo/__init__.py @@ -35,7 +35,6 @@ ) from homeassistant.helpers.aiohttp_client import async_create_clientsession from homeassistant.helpers.dispatcher import async_dispatcher_send -from homeassistant.helpers.update_coordinator import DataUpdateCoordinator from homeassistant.util import dt as dt_util, slugify from packaging.version import Version from pyhilo import API @@ -73,6 +72,7 @@ LOG, MIN_SCAN_INTERVAL, ) +from .coordinator import HiloCoordinator from .oauth2 import AuthCodeWithPKCEImplementation DISPATCHER_TOPIC_SIGNALR_EVENT = "pyhilo_signalr_event" @@ -434,7 +434,7 @@ def __init__(self, hass: HomeAssistant, entry: ConfigEntry, api: API) -> None: ) self.cost_sensors: dict = {} # This will get filled in by async_init: - self.coordinator: DataUpdateCoordinator | None = None + self.coordinator: HiloCoordinator | None = None self.unknown_tracker_device: HiloDevice | None = None self._events: dict = {} if self.track_unknown_sources: @@ -916,12 +916,8 @@ async def signalr_disconnect_listener(_: Event) -> None: EVENT_HOMEASSISTANT_STOP, signalr_disconnect_listener ) ) - self.coordinator = DataUpdateCoordinator( - self._hass, - LOG, - name="hilo", - update_interval=timedelta(seconds=scan_interval), - update_method=self.async_update, + self.coordinator = HiloCoordinator( + self._hass, self.entry, self, timedelta(seconds=scan_interval) ) async def start_signalr_loop(self, hub, id) -> None: diff --git a/custom_components/hilo/coordinator.py b/custom_components/hilo/coordinator.py new file mode 100644 index 00000000..97202865 --- /dev/null +++ b/custom_components/hilo/coordinator.py @@ -0,0 +1,44 @@ +"""DataUpdateCoordinator for the Hilo integration.""" + +from __future__ import annotations + +from datetime import timedelta +from typing import TYPE_CHECKING + +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant +from homeassistant.helpers.update_coordinator import DataUpdateCoordinator + +from .const import LOG + +if TYPE_CHECKING: + from . import Hilo + + +class HiloCoordinator(DataUpdateCoordinator[None]): + """Periodically recompute tariff and unknown power state. + + Device state is pushed into python-hilo by SignalR and GraphQL, so this + coordinator does not fetch it; entities only use it as a refresh tick. + """ + + def __init__( + self, + hass: HomeAssistant, + config_entry: ConfigEntry, + hilo: Hilo, + update_interval: timedelta, + ) -> None: + """Initialize the coordinator.""" + super().__init__( + hass, + LOG, + config_entry=config_entry, + name="hilo", + update_interval=update_interval, + ) + self._hilo = hilo + + async def _async_update_data(self) -> None: + """Run the periodic tariff and unknown power updates.""" + await self._hilo.async_update() diff --git a/custom_components/hilo/quality_scale.yaml b/custom_components/hilo/quality_scale.yaml index 342dbb50..bd1cb22b 100644 --- a/custom_components/hilo/quality_scale.yaml +++ b/custom_components/hilo/quality_scale.yaml @@ -8,8 +8,8 @@ rules: comment: Devices and challenges are push (SignalR); only notifications, weather and rewards poll (30 min-2 h). brands: done common-modules: - status: todo - comment: entity.py exists; coordinator.py does not. + status: done + comment: coordinator.py only drives the tariff tick; device data does not flow through it yet. config-flow-test-coverage: todo config-flow: done dependency-transparency: done