Skip to content

Commit

Permalink
Prices
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielhiversen authored Nov 4, 2024
1 parent 927ddb1 commit 4d452b0
Showing 1 changed file with 7 additions and 28 deletions.
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

0 comments on commit 4d452b0

Please sign in to comment.