diff --git a/homeassistant/components/elvia/importer.py b/homeassistant/components/elvia/importer.py index 3fc79240254..69e3d64d09d 100644 --- a/homeassistant/components/elvia/importer.py +++ b/homeassistant/components/elvia/importer.py @@ -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 (