Skip to content

Commit 4e0bc07

Browse files
committed
Handle partial price data better
If today or tomorrow has only partial price data, don't return any averages or highest/lowest price, because it's not possible to return something that makes sense if we don't have prices for the whole day.
1 parent 24c33cb commit 4e0bc07

4 files changed

Lines changed: 742 additions & 3 deletions

File tree

spothinta_api/models.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class Electricity:
5959
"""Object representing electricity data."""
6060

6161
prices: dict[datetime, float]
62+
resolution: timedelta
6263
time_zone: ZoneInfo
6364

6465
@property
@@ -257,12 +258,19 @@ def prices_today(self) -> dict[datetime, float]:
257258
258259
"""
259260
today = self.now_in_timezone().astimezone().date()
260-
return {
261+
prices = {
261262
timestamp: price
262263
for timestamp, price in self.prices.items()
263264
if timestamp.date() == today
264265
}
265266

267+
if (self.resolution == timedelta(minutes=15) and len(prices) == 96) or (
268+
self.resolution == timedelta(minutes=60) and len(prices) == 24
269+
):
270+
return prices
271+
272+
return {}
273+
266274
def prices_tomorrow(self) -> dict[datetime, float]:
267275
"""Return the prices for tomorrow.
268276
@@ -272,12 +280,19 @@ def prices_tomorrow(self) -> dict[datetime, float]:
272280
273281
"""
274282
tomorrow = (self.now_in_timezone() + timedelta(days=1)).astimezone().date()
275-
return {
283+
prices = {
276284
timestamp: price
277285
for timestamp, price in self.prices.items()
278286
if timestamp.date() == tomorrow
279287
}
280288

289+
if (self.resolution == timedelta(minutes=15) and len(prices) == 96) or (
290+
self.resolution == timedelta(minutes=60) and len(prices) == 24
291+
):
292+
return prices
293+
294+
return {}
295+
281296
def now_in_timezone(self) -> datetime:
282297
"""Return the current timestamp in the current timezone.
283298
@@ -329,13 +344,15 @@ def price_at_time(self, moment: datetime) -> float | None:
329344
def from_dict(
330345
cls: type[Electricity],
331346
data: list[dict[str, Any]],
347+
resolution: timedelta,
332348
time_zone: ZoneInfo,
333349
) -> Electricity:
334350
"""Create an Electricity object from a dictionary.
335351
336352
Args:
337353
----
338354
data: A dictionary with the data from the API.
355+
resolution: The price resolution.
339356
time_zone: The timezone to use for determining "today" and "tomorrow".
340357
341358
Returns:
@@ -350,5 +367,6 @@ def from_dict(
350367
]
351368
return cls(
352369
prices=prices,
370+
resolution=resolution,
353371
time_zone=time_zone,
354372
)

spothinta_api/spothinta.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ async def energy_prices(
161161
raise SpotHintaNoDataError(msg)
162162

163163
time_zone = await async_get_time_zone(REGION_TO_TIMEZONE[region])
164-
return Electricity.from_dict(data, time_zone=time_zone)
164+
return Electricity.from_dict(data, resolution=resolution, time_zone=time_zone)
165165

166166
async def close(self) -> None:
167167
"""Close open client session."""

0 commit comments

Comments
 (0)