Fix regression in Opower that was introduced in 2024.10.0 (#128141)
* Avoid KeyError when statistics have gaps * fix break * Remove unnecessary check
This commit is contained in:
parent
d9f4f424fd
commit
46184188e4
1 changed files with 32 additions and 10 deletions
|
@ -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 = []
|
||||
|
|
Loading…
Add table
Reference in a new issue