Skip to content

Commit

Permalink
Get historic data (#231)
Browse files Browse the repository at this point in the history
* Improve get_historic_data

* Update tibber_home.py

* startCursor

* black

Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net>

Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net>
  • Loading branch information
Danielhiversen committed Dec 2, 2022
1 parent 30c80cf commit 7337daf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
6 changes: 5 additions & 1 deletion tibber/gql_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
{{
viewer {{
home(id: "{0}") {{
{1}(resolution: {2}, last: {3}) {{
{1}(resolution: {2}, last: {3}, before: "{5}") {{
pageInfo {{
hasPreviousPage
startCursor
}}
nodes {{
from
unitPrice
Expand Down
51 changes: 35 additions & 16 deletions tibber/tibber_home.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,22 +461,41 @@ async def get_historic_data(
DAILY, WEEKLY, MONTHLY or ANNUAL
:param production: True to get production data instead of consumption
"""
cons_or_prod_str = "production" if production else "consumption"
query = HISTORIC_DATA.format(
self.home_id,
cons_or_prod_str,
resolution,
n_data,
"profit" if production else "totalCost cost",
)

if not (data := await self._tibber_control.execute(query)):
_LOGGER.error("Could not get the data.")
return []
data = data["viewer"]["home"][cons_or_prod_str]
if data is None:
return []
return data["nodes"]
cursor = ""
res = []
max_n_data = 7000
while n_data > 0:
_n_data = min(max_n_data, n_data)
cons_or_prod_str = "production" if production else "consumption"
query = HISTORIC_DATA.format(
self.home_id,
cons_or_prod_str,
resolution,
_n_data,
"profit" if production else "totalCost cost",
cursor,
)
if not (data := await self._tibber_control.execute(query)):
_LOGGER.error("Could not get the data.")
continue
data = data["viewer"]["home"][cons_or_prod_str]
if data is None:
continue
res.extend(data["nodes"])
n_data -= len(data["nodes"])
if (
not data["pageInfo"]["hasPreviousPage"]
or not data["pageInfo"]["startCursor"]
):
if n_data > 0:
max_n_data = max_n_data // 10
if max_n_data < 1:
_LOGGER.warning("Found less data than requested")
break
continue
break
cursor = data["pageInfo"]["startCursor"]
return res

async def get_historic_price_data(
self,
Expand Down

0 comments on commit 7337daf

Please sign in to comment.