From 46184188e406790d9c930968553a52e94c97d738 Mon Sep 17 00:00:00 2001 From: tronikos Date: Fri, 11 Oct 2024 03:10:07 -0700 Subject: [PATCH] Fix regression in Opower that was introduced in 2024.10.0 (#128141) * Avoid KeyError when statistics have gaps * fix break * Remove unnecessary check --- .../components/opower/coordinator.py | 42 ++++++++++++++----- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/opower/coordinator.py b/homeassistant/components/opower/coordinator.py index cd2e28ed638..3b4cd07590c 100644 --- a/homeassistant/components/opower/coordinator.py +++ b/homeassistant/components/opower/coordinator.py @@ -130,19 +130,41 @@ class OpowerCoordinator(DataUpdateCoordinator[dict[str, Forecast]]): continue start = cost_reads[0].start_time _LOGGER.debug("Getting statistics at: %s", start) - stats = await get_instance(self.hass).async_add_executor_job( - statistics_during_period, - self.hass, - start, - start + timedelta(seconds=1), - {cost_statistic_id, consumption_statistic_id}, - "hour", - None, - {"sum"}, - ) + # In the common case there should be a previous statistic at start time + # so we only need to fetch one statistic. If there isn't any, fetch all. + for end in (start + timedelta(seconds=1), None): + stats = await get_instance(self.hass).async_add_executor_job( + statistics_during_period, + self.hass, + start, + end, + {cost_statistic_id, consumption_statistic_id}, + "hour", + None, + {"sum"}, + ) + if stats: + break + if end: + _LOGGER.debug( + "Not found. Trying to find the oldest statistic after %s", + start, + ) + # We are in this code path only if get_last_statistics found a stat + # so statistics_during_period should also have found at least one. + assert stats cost_sum = cast(float, stats[cost_statistic_id][0]["sum"]) consumption_sum = cast(float, stats[consumption_statistic_id][0]["sum"]) last_stats_time = stats[consumption_statistic_id][0]["start"] + if end is None: + # If there was no statistic at the start of the cost reads, + # ignore cost reads past the last_stats_time. + cost_reads = [ + cost_read + for cost_read in cost_reads + if cost_read.start_time.timestamp() >= last_stats_time + ] + start = cost_reads[0].start_time assert last_stats_time == start.timestamp() cost_statistics = []