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

get_historic_data #315

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
50 changes: 34 additions & 16 deletions tibber/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from gql import gql

from .const import RESOLUTION_HOURLY
from .const import RESOLUTION_DAILY, RESOLUTION_HOURLY, RESOLUTION_MONTHLY, RESOLUTION_WEEKLY
from .gql_queries import (
HISTORIC_DATA,
HISTORIC_DATA_DATE,
Expand Down Expand Up @@ -529,21 +529,39 @@ async def get_historic_data(
: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, timeout=30)):
_LOGGER.error("Could not get the data.")
return []
data = data["viewer"]["home"][cons_or_prod_str]
if data is None:
return []
return data["nodes"]
res: list[dict[str, Any]] = []
if resolution == RESOLUTION_HOURLY:
max_n_data = 24 * 30

Choose a reason for hiding this comment

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

31 days is max (not 30)

Copy link
Owner Author

Choose a reason for hiding this comment

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

It was 30 when this was written :)

Danielhiversen marked this conversation as resolved.
Show resolved Hide resolved
elif resolution == RESOLUTION_DAILY:
max_n_data = 30
Danielhiversen marked this conversation as resolved.
Show resolved Hide resolved
elif resolution == RESOLUTION_WEEKLY:
max_n_data = 52
elif resolution == RESOLUTION_MONTHLY:
max_n_data = 12
else:
max_n_data = 1
cursor = ""
for _ in range(n_data // max_n_data + 1):
_n_data = min(max_n_data, n_data - len(res))
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"])
if len(res) >= n_data:
break
cursor = data["pageInfo"]["startCursor"]
return res

async def get_historic_data_date(
self,
Expand Down