Skip to content

Commit

Permalink
hotfix parsing multiple periods
Browse files Browse the repository at this point in the history
  • Loading branch information
Roeland authored and Roeland committed Oct 4, 2024
1 parent eecb7e7 commit 8c6e24f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
30 changes: 15 additions & 15 deletions custom_components/entsoe/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
23 changes: 20 additions & 3 deletions custom_components/entsoe/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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())
Expand All @@ -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()}
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.6.0"
"version": "0.6.1"
}

0 comments on commit 8c6e24f

Please sign in to comment.