Skip to content

Commit cd4b56d

Browse files
committed
fix: off by one in _localize_hourly_data
1 parent 75cfbc5 commit cd4b56d

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

custom_components/omie/sensor.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def update() -> None:
7070
tomorrow_data = self._sources.tomorrow.data
7171
yesterday_data = self._sources.yesterday.data
7272

73-
if today_data is None:
73+
if None in [today_data, yesterday_data]:
7474
# not all necessary data available yet
7575
self._attr_native_value = None
7676
self._attr_extra_state_attributes = None
@@ -81,6 +81,10 @@ def update() -> None:
8181
cet_yesterday_hourly_data = _localize_hourly_data(yesterday_data, self._series)
8282
cet_hourly_data = cet_yesterday_hourly_data | cet_today_hourly_data | cet_tomorrow_hourly_data
8383

84+
_LOGGER.debug(f'*** {self._key}')
85+
_LOGGER.debug(f'today_data: {today_data}')
86+
_LOGGER.debug(f'cet_today_hourly_data: {cet_today_hourly_data}')
87+
8488
local_tz = pytz.timezone(self.hass.config.time_zone)
8589
now = utcnow().astimezone(local_tz)
8690
today = now.date()
@@ -90,6 +94,10 @@ def update() -> None:
9094
local_tomorrow_hourly_data = {h: cet_hourly_data.get(h.astimezone(CET)) for h in _day_hours(tomorrow, local_tz)}
9195
local_start_of_hour = local_tz.normalize(now.replace(minute=0, second=0, microsecond=0))
9296

97+
# _LOGGER.debug(f'_day_hours({today}, {local_tz}): {_day_hours(today, local_tz)}')
98+
# _LOGGER.debug(f'local_start_of_hour: {local_start_of_hour}')
99+
# _LOGGER.debug(f'local_today_hourly_data: {local_today_hourly_data}')
100+
93101
self._attr_native_value = local_today_hourly_data.get(local_start_of_hour)
94102
self._attr_extra_state_attributes = {
95103
'OMIE_today_average': _day_average(cet_today_hourly_data),
@@ -110,7 +118,7 @@ def update() -> None:
110118

111119
sensors = [
112120
PriceEntity(sources=coordinators.spot, key="spot_price_pt", series="pt_spot_price", tz=_TZ_LISBON),
113-
PriceEntity(sources=coordinators.spot, key="spot_price_es", series="es_spot_price", tz=_TZ_MADRID),
121+
# PriceEntity(sources=coordinators.spot, key="spot_price_es", series="es_spot_price", tz=_TZ_MADRID),
114122
]
115123

116124
async_add_entities(sensors, update_before_add=True)
@@ -136,21 +144,21 @@ def _localize_hourly_data(results: OMIEResults[SpotData], series_name: str) -> d
136144
return {
137145
hour_start: hour_average
138146
for hour in range(hours_in_day)
139-
if (quarter_hour := hour * 4)
140-
if (hour_start := CET.normalize(midnight + timedelta(hours=hour)))
141-
if (hour_average := statistics.mean(quarter_hourly_data[quarter_hour:quarter_hour + 4]))
147+
for quarter_hour in [hour * 4]
148+
for hour_start in [CET.normalize(midnight + timedelta(hours=hour))]
149+
for hour_average in [statistics.mean(quarter_hourly_data[quarter_hour:quarter_hour + 4])]
142150
}
143151

144152

145153
def _day_hours(day: date, tz: StaticTzInfo) -> list[datetime]:
146-
"""Returns a list of every quarter-hour in the given date, normalized to the given time zone."""
154+
"""Returns a list of every hour in the given date, normalized to the given time zone."""
147155
zero = tz.localize(datetime(day.year, day.month, day.day))
148156
hours = [tz.normalize(zero + timedelta(hours=h)) for h in range(25)]
149157
return [h for h in hours if h.date() == day] # 25th hour only occurs once a year
150158

151159

152160
def _day_average(hours_in_day: dict[datetime, float]) -> float | None:
153-
"""Returns the arithmetic mean of the day's quarter-hourly prices if possible."""
161+
"""Returns the arithmetic mean of the day's prices if possible."""
154162
values = [] if hours_in_day is None else list(filter(lambda elem: elem is not None, hours_in_day.values()))
155163
if len(values) == 0:
156164
return None

0 commit comments

Comments
 (0)