mirror of
https://github.com/home-assistant/core.git
synced 2025-07-26 06:37:52 +00:00
Specify end_time when importing Elvia data to deal with drift (#109361)
This commit is contained in:
parent
e328d3ec5e
commit
3bab1d7cd5
@ -38,11 +38,18 @@ class ElviaImporter:
|
|||||||
self.client = Elvia(meter_value_token=api_token).meter_value()
|
self.client = Elvia(meter_value_token=api_token).meter_value()
|
||||||
self.metering_point_id = metering_point_id
|
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."""
|
"""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(
|
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],
|
metering_point_ids=[self.metering_point_id],
|
||||||
)
|
)
|
||||||
return all_data["meteringpoints"][0]["metervalue"]["timeSeries"]
|
return all_data["meteringpoints"][0]["metervalue"]["timeSeries"]
|
||||||
@ -62,8 +69,10 @@ class ElviaImporter:
|
|||||||
|
|
||||||
if not last_stats:
|
if not last_stats:
|
||||||
# First time we insert 1 years of data (if available)
|
# First time we insert 1 years of data (if available)
|
||||||
|
until = dt_util.utcnow()
|
||||||
hourly_data = await self._fetch_hourly_data(
|
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:
|
if hourly_data is None or len(hourly_data) == 0:
|
||||||
return
|
return
|
||||||
@ -71,7 +80,8 @@ class ElviaImporter:
|
|||||||
_sum = 0.0
|
_sum = 0.0
|
||||||
else:
|
else:
|
||||||
hourly_data = await self._fetch_hourly_data(
|
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 (
|
if (
|
||||||
|
Loading…
x
Reference in New Issue
Block a user