From 6f9876d5e0e2b5136d2ce4f1e9d91b65fed8fbe0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Sat, 3 Feb 2024 08:16:28 +0100 Subject: [PATCH] Extend the history of Elvia history to 3 years (#109490) Extend the history of Elvia data to 3 years --- homeassistant/components/elvia/config_flow.py | 4 ++- homeassistant/components/elvia/importer.py | 36 +++++++++++++------ 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/elvia/config_flow.py b/homeassistant/components/elvia/config_flow.py index e65c93b09a6..fb50842e39b 100644 --- a/homeassistant/components/elvia/config_flow.py +++ b/homeassistant/components/elvia/config_flow.py @@ -35,8 +35,10 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): self._api_token = api_token = user_input[CONF_API_TOKEN] client = Elvia(meter_value_token=api_token).meter_value() try: + end_time = dt_util.utcnow() results = await client.get_meter_values( - start_time=(dt_util.now() - timedelta(hours=1)).isoformat() + start_time=(end_time - timedelta(hours=1)).isoformat(), + end_time=end_time.isoformat(), ) except ElviaError.AuthError as exception: diff --git a/homeassistant/components/elvia/importer.py b/homeassistant/components/elvia/importer.py index 69e3d64d09d..097db51cab8 100644 --- a/homeassistant/components/elvia/importer.py +++ b/homeassistant/components/elvia/importer.py @@ -4,7 +4,7 @@ from __future__ import annotations from datetime import datetime, timedelta from typing import TYPE_CHECKING, cast -from elvia import Elvia +from elvia import Elvia, error as ElviaError from homeassistant.components.recorder.models import StatisticData, StatisticMetaData from homeassistant.components.recorder.statistics import ( @@ -68,21 +68,37 @@ class ElviaImporter: ) if not last_stats: - # First time we insert 1 years of data (if available) + # First time we insert 3 years of data (if available) + hourly_data: list[MeterValueTimeSeries] = [] until = dt_util.utcnow() - hourly_data = await self._fetch_hourly_data( - since=until - timedelta(days=365), - until=until, - ) + for year in (3, 2, 1): + try: + year_hours = await self._fetch_hourly_data( + since=until - timedelta(days=365 * year), + until=until - timedelta(days=365 * (year - 1)), + ) + except ElviaError.ElviaException: + # This will raise if the contract have no data for the + # year, we can safely ignore this + continue + hourly_data.extend(year_hours) + if hourly_data is None or len(hourly_data) == 0: + LOGGER.error("No data available for the metering point") return last_stats_time = None _sum = 0.0 else: - hourly_data = await self._fetch_hourly_data( - since=dt_util.utc_from_timestamp(last_stats[statistic_id][0]["end"]), - until=dt_util.utcnow(), - ) + try: + hourly_data = await self._fetch_hourly_data( + since=dt_util.utc_from_timestamp( + last_stats[statistic_id][0]["end"] + ), + until=dt_util.utcnow(), + ) + except ElviaError.ElviaException as err: + LOGGER.error("Error fetching data: %s", err) + return if ( hourly_data is None