Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Price issue #317

Merged
merged 2 commits into from
Nov 4, 2024
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
50 changes: 21 additions & 29 deletions tibber/gql_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,35 +101,6 @@
}
}
"""
PRICE_INFO = """
{
viewer {
home(id: "%s") {
currentSubscription {
priceInfo {
current {
energy
tax
total
startsAt
level
}
today {
total
startsAt
level
}
tomorrow {
total
startsAt
level
}
}
}
}
}
}
"""
PUSH_NOTIFICATION = """
mutation{{
sendPushNotification(input: {{
Expand Down Expand Up @@ -295,3 +266,24 @@
}

"""
PRICE_INFO = """
{
viewer {
home(id: "%s") {
currentSubscription {
priceRating {
hourly {
currency
entries {
time
total
energy
level
}
}
}
}
}
}
}
"""
35 changes: 7 additions & 28 deletions tibber/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ async def update_info_and_price_info(self) -> None:
"""Update home info and all price info asynchronously."""
if data := await self._tibber_control.execute(UPDATE_INFO_PRICE % self._home_id):
self.info = data
self._process_price_info(self.info)
await self.update_price_info()

async def update_current_price_info(self) -> None:
"""Update just the current price info asynchronously."""
Expand All @@ -228,38 +228,17 @@ async def update_price_info(self) -> None:
"""Update the current price info, todays price info
and tomorrows price info asynchronously.
"""
if price_info := await self._tibber_control.execute(PRICE_INFO % self.home_id):
self._process_price_info(price_info)

def _process_price_info(self, price_info: dict[str, dict[str, Any]]) -> None:
"""Processes price information retrieved from a GraphQL query.
The information from the provided dictionary is extracted, then the
properties of this TibberHome object is updated with this data.

:param price_info: Price info to retrieve data from.
"""
price_info = await self._tibber_control.execute(PRICE_INFO % self.home_id)
if not price_info:
_LOGGER.error("Could not find price info.")
return
self._price_info = {}
self._level_info = {}
for key in ["current", "today", "tomorrow"]:
try:
price_info_k = price_info["viewer"]["home"]["currentSubscription"]["priceInfo"][key]
except (KeyError, TypeError):
_LOGGER.error("Could not find price info for %s.", key)
continue
if key == "current":
self._current_price_info = price_info_k
continue
for data in price_info_k:
self._price_info[data.get("startsAt")] = data.get("total")
self._level_info[data.get("startsAt")] = data.get("level")
if (
not self.last_data_timestamp
or dt.datetime.fromisoformat(data.get("startsAt")) > self.last_data_timestamp
):
self.last_data_timestamp = dt.datetime.fromisoformat(data.get("startsAt"))
data = price_info["viewer"]["home"]["currentSubscription"]["priceRating"]["hourly"]["entries"]
for row in data:
self._price_info[row.get("time")] = row.get("total")
self._level_info[row.get("time")] = row.get("level")
self.last_data_timestamp = dt.datetime.fromisoformat(data[-1]["time"])

@property
def current_price_total(self) -> float | None:
Expand Down