Skip to content

Commit

Permalink
Generate prices list for gas with example (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
klaasnicolaas authored Dec 7, 2023
1 parent d8870ef commit 6b878f9
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 20 deletions.
25 changes: 25 additions & 0 deletions examples/prices_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""Asynchronous Python client for the EnergyZero API."""

import asyncio
from datetime import date

from energyzero import EnergyZero, VatOption


async def main() -> None:
"""Show example on fetching the timestamp lists from EnergyZero."""
async with EnergyZero(vat=VatOption.INCLUDE) as client:
today = date(2023, 12, 5)
energy = await client.energy_prices(start_date=today, end_date=today)
gas = await client.gas_prices(start_date=today, end_date=today)

print("--- ENERGY ---")
print(energy.timestamp_prices)
print()

print("--- GAS ---")
print(gas.timestamp_prices)


if __name__ == "__main__":
asyncio.run(main())
49 changes: 29 additions & 20 deletions src/energyzero/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ def _get_pricetime(
return func(prices, key=prices.get) # type: ignore[call-arg]


def _generate_timestamp_list(
prices: dict[datetime, float],
) -> list[dict[str, float | datetime]]:
"""Return a list of timestamps.
Args:
----
prices: A dictionary with the hourprices.
Returns:
-------
A list of timestamps.
"""
return [
{"timestamp": timestamp, "price": price} for timestamp, price in prices.items()
]


@dataclass
class Electricity:
"""Object representing electricity data."""
Expand Down Expand Up @@ -110,7 +128,7 @@ def timestamp_prices(self) -> list[dict[str, float | datetime]]:
-------
list of prices with timestamp
"""
return self.generate_timestamp_list(self.prices)
return _generate_timestamp_list(self.prices)

@property
def hours_priced_equal_or_lower(self) -> int:
Expand All @@ -132,25 +150,6 @@ def utcnow(self) -> datetime:
"""
return datetime.now(timezone.utc)

def generate_timestamp_list(
self,
prices: dict[datetime, float],
) -> list[dict[str, float | datetime]]:
"""Return a list of timestamps.
Args:
----
prices: A dictionary with the hourprices.
Returns:
-------
A list of timestamps.
"""
return [
{"timestamp": timestamp, "price": price}
for timestamp, price in prices.items()
]

def price_at_time(self, moment: datetime) -> float | None:
"""Return the price at a specific time.
Expand Down Expand Up @@ -220,6 +219,16 @@ def extreme_prices(self) -> tuple[float, float]:
"""
return min(self.prices.values()), max(self.prices.values())

@property
def timestamp_prices(self) -> list[dict[str, float | datetime]]:
"""Return a list of prices with timestamp.
Returns
-------
list of prices with timestamp
"""
return _generate_timestamp_list(self.prices)

def utcnow(self) -> datetime:
"""Return the current timestamp in the UTC timezone.
Expand Down
1 change: 1 addition & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ async def test_gas_model(aresponses: ResponsesMockServer) -> None:
# The next hour price
next_hour = datetime(2022, 12, 7, 15, 0, tzinfo=timezone.utc)
assert gas.price_at_time(next_hour) == 1.47
assert isinstance(gas.timestamp_prices, list)


@pytest.mark.freeze_time("2022-12-07 04:00:00+01:00")
Expand Down

0 comments on commit 6b878f9

Please sign in to comment.