From db93d96e09232c7b601ec0890b9c3a4a2172764c Mon Sep 17 00:00:00 2001 From: Roeland Date: Sat, 5 Oct 2024 11:20:06 +0200 Subject: [PATCH] make sure the filtered hourprices get updated every hour. --- custom_components/entsoe/coordinator.py | 7 +++++-- custom_components/entsoe/sensor.py | 8 ++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/custom_components/entsoe/coordinator.py b/custom_components/entsoe/coordinator.py index 128f16b..c055843 100644 --- a/custom_components/entsoe/coordinator.py +++ b/custom_components/entsoe/coordinator.py @@ -178,12 +178,15 @@ async def get_energy_prices(self, start_date, end_date): } return self.parse_hourprices(await self.fetch_prices(start_date, end_date)) - def today_data_available(self): + def update_data(self): now = dt.now() if self.today.date() != now.date(): self.logger.debug(f"new day detected: update today and filtered hourprices") self.today = now.replace(hour=0, minute=0, second=0, microsecond=0) - self.filtered_hourprices = self._filter_calculated_hourprices(self.data) + + self.filtered_hourprices = self._filter_calculated_hourprices(self.data) + + def today_data_available(self): return len(self.get_data_today()) > MIN_HOURS def _filter_calculated_hourprices(self, data): diff --git a/custom_components/entsoe/sensor.py b/custom_components/entsoe/sensor.py index 33b3025..15d2107 100644 --- a/custom_components/entsoe/sensor.py +++ b/custom_components/entsoe/sensor.py @@ -46,7 +46,9 @@ class EntsoeEntityDescription(SensorEntityDescription): value_fn: Callable[[dict], StateType] = None -def sensor_descriptions(currency: str, energy_scale: str) -> tuple[EntsoeEntityDescription, ...]: +def sensor_descriptions( + currency: str, energy_scale: str +) -> tuple[EntsoeEntityDescription, ...]: """Construct EntsoeEntityDescription.""" return ( EntsoeEntityDescription( @@ -132,7 +134,7 @@ async def async_setup_entry( entity = {} for description in sensor_descriptions( currency=config_entry.options.get(CONF_CURRENCY, DEFAULT_CURRENCY), - energy_scale=config_entry.options.get(CONF_ENERGY_SCALE, DEFAULT_ENERGY_SCALE) + energy_scale=config_entry.options.get(CONF_ENERGY_SCALE, DEFAULT_ENERGY_SCALE), ): entity = description entities.append( @@ -213,6 +215,8 @@ async def async_update(self) -> None: utcnow().replace(minute=0, second=0) + timedelta(hours=1), ) + self.coordinator.update_data() + if ( self.coordinator.data is not None and self.coordinator.today_data_available()