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
71 changes: 38 additions & 33 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_HOURLY, RESOLUTION_MONTHLY, RESOLUTION_WEEKLY, RESOLUTION_DAILY, RESOLUTION_ANNUAL
Fixed Show fixed Hide fixed
from .gql_queries import (
HISTORIC_DATA,
HISTORIC_DATA_DATE,
Expand Down Expand Up @@ -519,6 +519,7 @@
n_data: int,
resolution: str = RESOLUTION_HOURLY,
production: bool = False,
cursor: str = "",
) -> list[dict[str, Any]]:
"""Get historic data.

Expand All @@ -529,21 +530,39 @@
: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 = []
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
for k in range(n_data // max_n_data + 1):
_n_data = min(max_n_data, n_data)
_n_data = min(_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 All @@ -568,27 +587,13 @@
# Calculate the number of days to the end of the month from the given date
n_data = (date_from.replace(day=1, month=date_from.month + 1) - date_from).days

cons_or_prod_str = "production" if production else "consumption"
query = HISTORIC_DATA_DATE.format(
self.home_id,
cons_or_prod_str,
resolution,
return await self.get_historic_data(
n_data,
resolution,
production,
date_from_base64,
"profit production productionUnit" if production else "cost consumption consumptionUnit",
)

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

async def get_historic_price_data(
self,
resolution: str = RESOLUTION_HOURLY,
Expand Down
Loading