add update schedule

This commit is contained in:
Julian Pawlowski 2025-04-18 22:10:52 +00:00
parent f06b9ff0cf
commit aa92de42da
3 changed files with 13 additions and 3 deletions

View file

@ -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)

View file

@ -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."""

View file

@ -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()