File: gas.py

package info (click to toggle)
python-energyzero 5.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,052 kB
  • sloc: python: 1,903; makefile: 3
file content (51 lines) | stat: -rw-r--r-- 1,402 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""Asynchronous example: gas prices via REST API."""

import asyncio
from datetime import UTC, datetime

from energyzero import (  # pyright: ignore[reportMissingImports]
    EnergyPrices,
    EnergyZero,
    PriceType,
)


def _price_to_string(price: float | None) -> str:
    if price is None:
        return "Unknown"
    return f"€{price:0.3f}"


def _print_summary(data: EnergyPrices) -> None:
    if len(data.prices) == 0:
        print("No gas data available")
        return

    min_price, max_price = data.extreme_prices
    print(f"Average price: {_price_to_string(data.average_price)}")
    print(f"Min price: {_price_to_string(min_price)}")
    print(f"Max price: {_price_to_string(max_price)}")

    current = data.current_price
    if current is not None:
        print(f"Current price: {_price_to_string(current)}")
        print(f"Percent of max: {data.pct_of_max_price}%")

    print(f"Days <= current price: {data.time_ranges_priced_equal_or_lower}")


async def main() -> None:
    """Fetch current gas price via REST API."""
    async with EnergyZero() as client:
        today = datetime.now(UTC).astimezone().date()
        prices = await client.get_gas_prices(
            start_date=today,
            price_type=PriceType.ALL_IN,
        )

    print(f"--- GAS {today.isoformat()} (REST) ---")
    _print_summary(prices)


if __name__ == "__main__":
    asyncio.run(main())