From ba79c95fbab844f83802d67b5534d04b5d634d65 Mon Sep 17 00:00:00 2001 From: Roeland Date: Fri, 13 Sep 2024 22:25:01 +0200 Subject: [PATCH] fix mapping prices to hours --- custom_components/entsoe/api_client.py | 4 ++-- custom_components/entsoe/coordinator.py | 14 ++++++++++---- custom_components/entsoe/manifest.json | 2 +- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/custom_components/entsoe/api_client.py b/custom_components/entsoe/api_client.py index 7c29b8d..c861c41 100644 --- a/custom_components/entsoe/api_client.py +++ b/custom_components/entsoe/api_client.py @@ -3,7 +3,7 @@ import enum import logging import xml.etree.ElementTree as ET -from datetime import datetime +from datetime import datetime, timedelta from typing import Dict, Union import pytz @@ -82,7 +82,7 @@ def query_day_ahead_prices( position = point.find("ns:position", ns).text price = point.find("ns:price.amount", ns).text hour = int(position) - 1 - series[date.replace(hour=hour)] = float(price) + series[date + timedelta(hours=hour)] = float(price) return series except Exception as exc: diff --git a/custom_components/entsoe/coordinator.py b/custom_components/entsoe/coordinator.py index dbb8cca..51d665b 100644 --- a/custom_components/entsoe/coordinator.py +++ b/custom_components/entsoe/coordinator.py @@ -14,6 +14,9 @@ from .api_client import EntsoeClient from .const import AREA_INFO, CALCULATION_MODE, DEFAULT_MODIFYER +# depending on timezone les than 24 hours could be returned. +MIN_HOURS = 20 + class EntsoeCoordinator(DataUpdateCoordinator): """Get the latest data and update the states.""" @@ -119,9 +122,9 @@ async def _async_update_data(self) -> dict: def check_update_needed(self, now): if self.data is None: return True - if len(self.get_data_today()) != 24: + if len(self.get_data_today()) < MIN_HOURS: return True - if len(self.get_data_tomorrow()) != 24 and now.hour > 11: + if len(self.get_data_tomorrow()) < MIN_HOURS and now.hour > 11: return True return False @@ -160,7 +163,10 @@ def api_update(self, start_date, end_date, api_key): async def get_energy_prices(self, start_date, end_date): # check if we have the data already - if len(self.get_data(start_date)) == 24 and len(self.get_data(end_date)) == 24: + if ( + len(self.get_data(start_date)) > MIN_HOURS + and len(self.get_data(end_date)) > MIN_HOURS + ): self.logger.debug(f"return prices from coordinator cache.") return { k: v @@ -175,7 +181,7 @@ def today_data_available(self): 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) - return len(self.get_data_today()) == 24 + return len(self.get_data_today()) > MIN_HOURS def _filter_calculated_hourprices(self, data): if self.calculation_mode == CALCULATION_MODE["rotation"]: diff --git a/custom_components/entsoe/manifest.json b/custom_components/entsoe/manifest.json index c269f8b..1cf143a 100644 --- a/custom_components/entsoe/manifest.json +++ b/custom_components/entsoe/manifest.json @@ -7,5 +7,5 @@ "iot_class": "cloud_polling", "issue_tracker": "https://github.com/JaccoR/hass-entso-e/issues", "requirements": ["requests"], - "version": "0.5.0-beta1" + "version": "0.5.0-beta3" }