From 927ddb17b6038eee087a3a8a5ce2510b6b6600a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Hjelseth=20H=C3=B8yer?= Date: Mon, 4 Nov 2024 20:20:14 +0100 Subject: [PATCH 1/2] Price issue --- tibber/gql_queries.py | 50 ++++++++++++++++++------------------------- 1 file changed, 21 insertions(+), 29 deletions(-) diff --git a/tibber/gql_queries.py b/tibber/gql_queries.py index a4be9d8..b322089 100644 --- a/tibber/gql_queries.py +++ b/tibber/gql_queries.py @@ -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: {{ @@ -295,3 +266,24 @@ } """ +PRICE_INFO = """ +{ + viewer { + home(id: "%s") { + currentSubscription { + priceRating { + hourly { + currency + entries { + time + total + energy + level + } + } + } + } + } + } +} +""" From 4d452b07cc941f9a046ed00e1e180eea0e35c9be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Hjelseth=20H=C3=B8yer?= Date: Mon, 4 Nov 2024 20:24:38 +0100 Subject: [PATCH 2/2] Prices --- tibber/home.py | 35 +++++++---------------------------- 1 file changed, 7 insertions(+), 28 deletions(-) diff --git a/tibber/home.py b/tibber/home.py index e5cf64f..5d369be 100644 --- a/tibber/home.py +++ b/tibber/home.py @@ -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.""" @@ -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: