Specify end_time when importing Elvia data to deal with drift (#109361)

This commit is contained in:
Joakim Sørensen 2024-02-02 13:42:07 +01:00 committed by GitHub
parent e328d3ec5e
commit 3bab1d7cd5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -38,11 +38,18 @@ class ElviaImporter:
self.client = Elvia(meter_value_token=api_token).meter_value()
self.metering_point_id = metering_point_id
async def _fetch_hourly_data(self, since: datetime) -> list[MeterValueTimeSeries]:
async def _fetch_hourly_data(
self,
since: datetime,
until: datetime,
) -> list[MeterValueTimeSeries]:
"""Fetch hourly data."""
LOGGER.debug("Fetching hourly data since %s", since)
start_time = since.isoformat()
end_time = until.isoformat()
LOGGER.debug("Fetching hourly data %s - %s", start_time, end_time)
all_data = await self.client.get_meter_values(
start_time=since.isoformat(),
start_time=start_time,
end_time=end_time,
metering_point_ids=[self.metering_point_id],
)
return all_data["meteringpoints"][0]["metervalue"]["timeSeries"]
@ -62,8 +69,10 @@ class ElviaImporter:
if not last_stats:
# First time we insert 1 years of data (if available)
until = dt_util.utcnow()
hourly_data = await self._fetch_hourly_data(
since=dt_util.now() - timedelta(days=365)
since=until - timedelta(days=365),
until=until,
)
if hourly_data is None or len(hourly_data) == 0:
return
@ -71,7 +80,8 @@ class ElviaImporter:
_sum = 0.0
else:
hourly_data = await self._fetch_hourly_data(
since=dt_util.utc_from_timestamp(last_stats[statistic_id][0]["end"])
since=dt_util.utc_from_timestamp(last_stats[statistic_id][0]["end"]),
until=dt_util.utcnow(),
)
if (