Specify end_time when importing Elvia data to deal with drift (#109361)
This commit is contained in:
parent
e328d3ec5e
commit
3bab1d7cd5
1 changed files with 15 additions and 5 deletions
|
@ -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 (
|
||||
|
|
Loading…
Add table
Reference in a new issue