|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | """A class representing the Subscription type from the GraphQL Tibber API.""" |
4 | | -from typing import TYPE_CHECKING, Optional |
| 4 | +from typing import TYPE_CHECKING, Optional, Literal |
5 | 5 |
|
6 | 6 | from tibber.networking.query_builder import QueryBuilder |
7 | 7 | from tibber.types.legal_entity import LegalEntity |
@@ -50,6 +50,31 @@ def price_info(self) -> PriceInfo: |
50 | 50 | """Price information related to the subscription""" |
51 | 51 | return PriceInfo(self.cache.get("priceInfo"), self.tibber_client) |
52 | 52 |
|
| 53 | + def fetch_price_info(self, |
| 54 | + resolution: Literal["HOURLY", "QUARTER_HOURLY"], |
| 55 | + home_id: Optional[str] = None |
| 56 | + ) -> PriceInfo: |
| 57 | + price_info_query_dict = QueryBuilder.price_info_query(resolution) |
| 58 | + |
| 59 | + price_info_query = QueryBuilder.create_query( |
| 60 | + "viewer", "homes", "currentSubscription", price_info_query_dict |
| 61 | + ) |
| 62 | + |
| 63 | + full_data = self.tibber_client.execute_query( |
| 64 | + self.tibber_client.token, price_info_query |
| 65 | + ) |
| 66 | + |
| 67 | + home = full_data["viewer"]["homes"][0] |
| 68 | + if home_id: |
| 69 | + home_of_id = [ |
| 70 | + home for home in full_data["viewer"]["homes"] if home["id"] == home_id |
| 71 | + ][0] |
| 72 | + |
| 73 | + if home_of_id: |
| 74 | + home = home_of_id |
| 75 | + |
| 76 | + return PriceInfo(home["currentSubscription"]["priceInfo"], self.tibber_client) |
| 77 | + |
53 | 78 | def fetch_price_info_range( |
54 | 79 | self, |
55 | 80 | resolution: str, |
|
0 commit comments