Skip to content

Commit

Permalink
Price issue (#317)
Browse files Browse the repository at this point in the history
* Price issue

* Prices
  • Loading branch information
Danielhiversen authored Nov 4, 2024
1 parent 6694047 commit 3ae59e6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 57 deletions.
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 {

This comment has been minimized.

Copy link
@TylonHH

TylonHH Nov 14, 2024

@Danielhiversen
shouldnt this be priceInfo

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"]

This comment has been minimized.

Copy link
@TylonHH

TylonHH Nov 14, 2024

her also priceInfo instead of priceRating

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

2 comments on commit 3ae59e6

@TylonHH
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check inline comments

@Danielhiversen
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just replacing priceRating with priceInfo will not work

Please sign in to comment.