Skip to content

Commit

Permalink
fix mapping prices to hours
Browse files Browse the repository at this point in the history
  • Loading branch information
Roeland authored and Roeland committed Sep 13, 2024
1 parent 15925b2 commit ba79c95
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
4 changes: 2 additions & 2 deletions custom_components/entsoe/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
14 changes: 10 additions & 4 deletions custom_components/entsoe/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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"]:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/entsoe/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit ba79c95

Please sign in to comment.