Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/energy.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async def print_prices(client: SpotHinta, region: Region) -> None:
print(f"Current price: {energy_today.current_price}")
print(f"Next hour price: {energy_today.price_at_time(utc_next_hour)}")

lower_hours: int = energy_today.hours_priced_equal_or_lower
lower_hours: int = energy_today.intervals_priced_equal_or_lower
print(f"Lower hours: {lower_hours}")


Expand Down
12 changes: 6 additions & 6 deletions spothinta_api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def _timed_value(moment: datetime, prices: dict[datetime, float]) -> float | Non
"""
value = None
for timestamp, price in prices.items():
future_dt = timestamp + timedelta(hours=1)
future_dt = timestamp + timedelta(minutes=15)
if timestamp <= moment < future_dt:
value = round(price, 5)
return value
Expand Down Expand Up @@ -63,11 +63,11 @@ class Electricity:

@property
def current_price(self) -> float | None:
"""Return the price for the current hour.
"""Return the price for the current interval.

Returns
-------
The price for the current hour or None if no price is available.
The price for the current interval or None if no price is available.

"""
return self.price_at_time(self.now_in_timezone())
Expand Down Expand Up @@ -237,12 +237,12 @@ def timestamp_prices_today(self) -> list[dict[str, float | datetime]]:
return self.generate_timestamp_list(self.prices_today())

@property
def hours_priced_equal_or_lower(self) -> int:
"""Return the number of hours with the current price or better.
def intervals_priced_equal_or_lower(self) -> int:
"""Return the number of intervals with the current price or better.

Returns
-------
The number of hours with the current price or better.
The number of intervals with the current price or better.

"""
current = self.current_price or 0
Expand Down
40 changes: 20 additions & 20 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ async def test_model(aresponses: ResponsesMockServer) -> None:
assert energy.average_price_today == 0.08935
assert energy.average_price_tomorrow == 0.07284
assert energy.current_price == 0.062
assert energy.hours_priced_equal_or_lower == 1
# The price for another hour
another_hour = datetime(2023, 5, 6, 17, 0, tzinfo=timezone.utc)
assert energy.price_at_time(another_hour) == 0.1055
assert energy.intervals_priced_equal_or_lower == 1
# The price for another interval
another_interval = datetime(2023, 5, 6, 17, 0, tzinfo=timezone.utc)
assert energy.price_at_time(another_interval) == 0.1055
assert energy.lowest_price_time_today == datetime.strptime(
"2023-05-06 15:00:00+03:00",
"%Y-%m-%d %H:%M:%S%z",
Expand Down Expand Up @@ -92,10 +92,10 @@ async def test_model_15_minute_resolution(aresponses: ResponsesMockServer) -> No
assert energy.average_price_today == 0.00131
assert energy.average_price_tomorrow == 0.00239
assert energy.current_price == 0.00157
assert energy.hours_priced_equal_or_lower == 63
# The price for another hour
another_hour = datetime(2025, 10, 4, 17, 0, tzinfo=timezone.utc)
assert energy.price_at_time(another_hour) == 0.0033
assert energy.intervals_priced_equal_or_lower == 63
# The price for another interval
another_interval = datetime(2025, 10, 4, 17, 0, tzinfo=timezone.utc)
assert energy.price_at_time(another_interval) == 0.0033
assert energy.lowest_price_time_today == datetime.strptime(
"2025-10-04 5:00:00+03:00",
"%Y-%m-%d %H:%M:%S%z",
Expand Down Expand Up @@ -143,10 +143,10 @@ async def test_model_se1_15_minute_resolution(aresponses: ResponsesMockServer) -
assert energy.average_price_today == 0.00113
assert energy.average_price_tomorrow == 0.00249
assert energy.current_price == 0.00128
assert energy.hours_priced_equal_or_lower == 57
# The price for another hour
another_hour = datetime(2025, 10, 4, 18, 0, tzinfo=timezone.utc)
assert energy.price_at_time(another_hour) == 0.00242
assert energy.intervals_priced_equal_or_lower == 57
# The price for another interval
another_interval = datetime(2025, 10, 4, 18, 0, tzinfo=timezone.utc)
assert energy.price_at_time(another_interval) == 0.00242
assert energy.lowest_price_time_today == datetime.strptime(
"2025-10-04 03:00:00+00:00",
"%Y-%m-%d %H:%M:%S%z",
Expand Down Expand Up @@ -191,10 +191,10 @@ async def test_model_no_prices_for_tomorrow(aresponses: ResponsesMockServer) ->
assert energy.average_price_today == 0.08935
assert energy.average_price_tomorrow is None
assert energy.current_price == 0.062
assert energy.hours_priced_equal_or_lower == 1
# The price for another hour
another_hour = datetime(2023, 5, 6, 17, 0, tzinfo=timezone.utc)
assert energy.price_at_time(another_hour) == 0.1055
assert energy.intervals_priced_equal_or_lower == 1
# The price for another interval
another_interval = datetime(2023, 5, 6, 17, 0, tzinfo=timezone.utc)
assert energy.price_at_time(another_interval) == 0.1055
assert energy.lowest_price_time_today == datetime.strptime(
"2023-05-06 15:00:00+03:00",
"%Y-%m-%d %H:%M:%S%z",
Expand Down Expand Up @@ -273,10 +273,10 @@ async def test_only_data_for_tomorrow(aresponses: ResponsesMockServer) -> None:
assert energy.average_price_today is None
assert energy.highest_price_today is None
assert energy.lowest_price_today is None
assert energy.hours_priced_equal_or_lower == 0
# The price for another hour
another_hour = datetime(2023, 5, 6, 17, 0, tzinfo=timezone.utc)
assert energy.price_at_time(another_hour) is None
assert energy.intervals_priced_equal_or_lower == 0
# The price for another interval
another_interval = datetime(2023, 5, 6, 17, 0, tzinfo=timezone.utc)
assert energy.price_at_time(another_interval) is None
assert energy.lowest_price_time_today is None
assert energy.highest_price_time_today is None
assert isinstance(energy.timestamp_prices, list)
Expand Down