Skip to content

Commit baf5531

Browse files
committed
feat: add fetch_price_info() method on subscription
1 parent 886963a commit baf5531

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

tibber/types/subscription.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
"""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
55

66
from tibber.networking.query_builder import QueryBuilder
77
from tibber.types.legal_entity import LegalEntity
@@ -50,6 +50,31 @@ def price_info(self) -> PriceInfo:
5050
"""Price information related to the subscription"""
5151
return PriceInfo(self.cache.get("priceInfo"), self.tibber_client)
5252

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+
5378
def fetch_price_info_range(
5479
self,
5580
resolution: str,

0 commit comments

Comments
 (0)