diff --git a/custom_components/entsoe/api_client.py b/custom_components/entsoe/api_client.py index e5caaa8..54da6bf 100644 --- a/custom_components/entsoe/api_client.py +++ b/custom_components/entsoe/api_client.py @@ -76,25 +76,25 @@ def query_day_ahead_prices( # Extract TimeSeries data for timeseries in root.findall(".//TimeSeries"): - period = timeseries.find(".//Period") - resolution = period.find(".//resolution").text + for period in timeseries.findall(".//Period"): + resolution = period.find(".//resolution").text - if resolution != "PT60M": - continue + if resolution != "PT60M": + continue - start_time = period.find(".//timeInterval/start").text + start_time = period.find(".//timeInterval/start").text - date = ( - datetime.strptime(start_time, "%Y-%m-%dT%H:%MZ") - .replace(tzinfo=pytz.UTC) - .astimezone() - ) + date = ( + datetime.strptime(start_time, "%Y-%m-%dT%H:%MZ") + .replace(tzinfo=pytz.UTC) + .astimezone() + ) - for point in period.findall(".//Point"): - position = point.find(".//position").text - price = point.find(".//price.amount").text - hour = int(position) - 1 - series[date + timedelta(hours=hour)] = float(price) + for point in period.findall(".//Point"): + position = point.find(".//position").text + price = point.find(".//price.amount").text + hour = int(position) - 1 + 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 28cdd1a..128f16b 100644 --- a/custom_components/entsoe/coordinator.py +++ b/custom_components/entsoe/coordinator.py @@ -12,7 +12,7 @@ from requests.exceptions import HTTPError from .api_client import EntsoeClient -from .const import AREA_INFO, ENERGY_SCALES, CALCULATION_MODE, DEFAULT_MODIFYER +from .const import AREA_INFO, CALCULATION_MODE, DEFAULT_MODIFYER, ENERGY_SCALES # depending on timezone les than 24 hours could be returned. MIN_HOURS = 20 @@ -118,6 +118,7 @@ async def _async_update_data(self) -> dict: self.logger.debug( f"received pricing data from entso-e for {len(data)} hours" ) + self.data = parsed_data self.filtered_hourprices = self._filter_calculated_hourprices(parsed_data) return parsed_data @@ -195,8 +196,14 @@ def _filter_calculated_hourprices(self, data): elif self.calculation_mode == CALCULATION_MODE["sliding"]: now = dt.now().replace(minute=0, second=0, microsecond=0) return {hour: price for hour, price in data.items() if hour >= now} + elif self.calculation_mode == CALCULATION_MODE["publish"] and len(data) > 48: + return {hour: price for hour, price in data.items() if hour >= self.today} elif self.calculation_mode == CALCULATION_MODE["publish"]: - return dict(list(data.items())[-48:]) + return { + hour: price + for hour, price in data.items() + if hour >= self.today - timedelta(days=1) + } def get_prices_today(self): return self.get_timestamped_prices(self.get_data_today()) @@ -205,7 +212,17 @@ def get_prices_tomorrow(self): return self.get_timestamped_prices(self.get_data_tomorrow()) def get_prices(self): - return self.get_timestamped_prices(dict(list(self.data.items())[-48:])) + if len(self.data) > 48: + return self.get_timestamped_prices( + {hour: price for hour, price in self.data.items() if hour >= self.today} + ) + return self.get_timestamped_prices( + { + hour: price + for hour, price in self.data.items() + if hour >= self.today - timedelta(days=1) + } + ) def get_data(self, date): return {k: v for k, v in self.data.items() if k.date() == date.date()} diff --git a/custom_components/entsoe/manifest.json b/custom_components/entsoe/manifest.json index 42811df..2e81419 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.6.0" + "version": "0.6.1" }