From aa92de42dabb0774bb2d46fa1cede48289809bba Mon Sep 17 00:00:00 2001 From: Julian Pawlowski Date: Fri, 18 Apr 2025 22:10:52 +0000 Subject: [PATCH] add update schedule --- custom_components/tibber_prices/__init__.py | 3 ++- custom_components/tibber_prices/coordinator.py | 11 +++++++++++ custom_components/tibber_prices/switch.py | 2 -- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/custom_components/tibber_prices/__init__.py b/custom_components/tibber_prices/__init__.py index d9f7807..88da7b1 100644 --- a/custom_components/tibber_prices/__init__.py +++ b/custom_components/tibber_prices/__init__.py @@ -43,7 +43,7 @@ async def async_setup_entry( entry=entry, logger=LOGGER, name=DOMAIN, - update_interval=timedelta(hours=1), + update_interval=timedelta(minutes=5), ) entry.runtime_data = TibberPricesData( client=TibberPricesApiClient( @@ -68,6 +68,7 @@ async def async_unload_entry( entry: TibberPricesConfigEntry, ) -> bool: """Handle unload of an entry.""" + await entry.runtime_data.coordinator.shutdown() return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) diff --git a/custom_components/tibber_prices/coordinator.py b/custom_components/tibber_prices/coordinator.py index 27561a8..f69787b 100644 --- a/custom_components/tibber_prices/coordinator.py +++ b/custom_components/tibber_prices/coordinator.py @@ -61,6 +61,17 @@ class TibberPricesDataUpdateCoordinator(DataUpdateCoordinator): self._last_price_update: datetime | None = None self._last_rating_update: datetime | None = None self._scheduled_price_update: asyncio.Task | None = None + self._remove_update_listener = None + + # Schedule additional updates at the start of every hour + self._remove_update_listener = hass.helpers.event.async_track_time_change( + self.async_request_refresh, minute=0, second=0 + ) + + async def shutdown(self) -> None: + """Clean up coordinator on shutdown.""" + if self._remove_update_listener: + self._remove_update_listener() async def _async_initialize(self) -> None: """Load stored data.""" diff --git a/custom_components/tibber_prices/switch.py b/custom_components/tibber_prices/switch.py index 09300fa..bf98906 100644 --- a/custom_components/tibber_prices/switch.py +++ b/custom_components/tibber_prices/switch.py @@ -58,10 +58,8 @@ class TibberPricesSwitch(TibberPricesEntity, SwitchEntity): async def async_turn_on(self, **_: Any) -> None: """Turn on the switch.""" - await self.coordinator.config_entry.runtime_data.client.async_set_title("bar") await self.coordinator.async_request_refresh() async def async_turn_off(self, **_: Any) -> None: """Turn off the switch.""" - await self.coordinator.config_entry.runtime_data.client.async_set_title("foo") await self.coordinator.async_request_refresh()