Skip to content

Commit 0a4c9d3

Browse files
RoelandRoeland
authored andcommitted
2 parents 2b6ebac + c9273af commit 0a4c9d3

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

custom_components/entsoe/coordinator.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def __init__(
4040
self.calculation_mode = calculation_mode
4141
self.vat = VAT
4242
self.today = None
43+
self.calculator_last_sync = None
4344
self.filtered_hourprices = []
4445

4546
# Check incase the sensor was setup using config flow.
@@ -61,6 +62,7 @@ def __init__(
6162
update_interval=timedelta(minutes=60),
6263
)
6364

65+
# calculate the price using the given template
6466
def calc_price(self, value, fake_dt=None, no_template=False) -> float:
6567
"""Calculate price based on the users settings."""
6668
# Used to inject the current hour.
@@ -93,6 +95,7 @@ def parse_hourprices(self, hourprices):
9395
hourprices[hour] = self.calc_price(value=price, fake_dt=hour)
9496
return hourprices
9597

98+
# Called by HA every refresh interval (60 minutes)
9699
async def _async_update_data(self) -> dict:
97100
"""Get the latest data from ENTSO-e"""
98101
self.logger.debug("ENTSO-e DataUpdateCoordinator data update")
@@ -178,17 +181,29 @@ async def get_energy_prices(self, start_date, end_date):
178181
}
179182
return self.parse_hourprices(await self.fetch_prices(start_date, end_date))
180183

181-
def update_data(self):
182-
now = dt.now()
183-
if self.today.date() != now.date():
184-
self.logger.debug(f"new day detected: update today and filtered hourprices")
185-
self.today = now.replace(hour=0, minute=0, second=0, microsecond=0)
186-
187-
self.filtered_hourprices = self._filter_calculated_hourprices(self.data)
188-
189184
def today_data_available(self):
190185
return len(self.get_data_today()) > MIN_HOURS
191186

187+
# this method is called by each sensor, each complete hour, and ensures the date and filtered hourprices are in line with the current time
188+
# we could still optimize as not every calculator mode needs hourly updates
189+
def sync_calculator(self):
190+
now = dt.now()
191+
if (
192+
self.calculator_last_sync is None
193+
or self.calculator_last_sync.hour != now.hour
194+
):
195+
self.logger.debug(
196+
f"The calculator needs to be synced with the current time"
197+
)
198+
if self.today.date() != now.date():
199+
self.logger.debug(
200+
f"new day detected: update today and filtered hourprices"
201+
)
202+
self.today = now.replace(hour=0, minute=0, second=0, microsecond=0)
203+
self.filtered_hourprices = self._filter_calculated_hourprices(self.data)
204+
205+
self.calculator_last_sync = now
206+
192207
def _filter_calculated_hourprices(self, data):
193208
# rotation = calculations made upon 24hrs today
194209
if self.calculation_mode == CALCULATION_MODE["rotation"]:
@@ -204,7 +219,7 @@ def _filter_calculated_hourprices(self, data):
204219
# publish >48 hrs of data = calculations made on all data of today and tomorrow (48 hrs)
205220
elif self.calculation_mode == CALCULATION_MODE["publish"] and len(data) > 48:
206221
return {hour: price for hour, price in data.items() if hour >= self.today}
207-
# publish <=48 hrs of data = calculations made on all data of yesterday and today (48 hrs)
222+
# publish <=48 hrs of data = calculations made on all data of yesterday and today (48 hrs)
208223
elif self.calculation_mode == CALCULATION_MODE["publish"]:
209224
return {
210225
hour: price

custom_components/entsoe/sensor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ async def async_update(self) -> None:
224224
utcnow().replace(minute=0, second=0) + timedelta(hours=1),
225225
)
226226

227-
self.coordinator.update_data()
227+
# ensure the calculated data is refreshed by the changing hour
228+
self.coordinator.sync_calculator()
228229

229230
if (
230231
self.coordinator.data is not None

0 commit comments

Comments
 (0)