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
|
continue
|
||||||
start = cost_reads[0].start_time
|
start = cost_reads[0].start_time
|
||||||
_LOGGER.debug("Getting statistics at: %s", start)
|
_LOGGER.debug("Getting statistics at: %s", start)
|
||||||
stats = await get_instance(self.hass).async_add_executor_job(
|
# In the common case there should be a previous statistic at start time
|
||||||
statistics_during_period,
|
# so we only need to fetch one statistic. If there isn't any, fetch all.
|
||||||
self.hass,
|
for end in (start + timedelta(seconds=1), None):
|
||||||
start,
|
stats = await get_instance(self.hass).async_add_executor_job(
|
||||||
start + timedelta(seconds=1),
|
statistics_during_period,
|
||||||
{cost_statistic_id, consumption_statistic_id},
|
self.hass,
|
||||||
"hour",
|
start,
|
||||||
None,
|
end,
|
||||||
{"sum"},
|
{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"])
|
cost_sum = cast(float, stats[cost_statistic_id][0]["sum"])
|
||||||
consumption_sum = cast(float, stats[consumption_statistic_id][0]["sum"])
|
consumption_sum = cast(float, stats[consumption_statistic_id][0]["sum"])
|
||||||
last_stats_time = stats[consumption_statistic_id][0]["start"]
|
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()
|
assert last_stats_time == start.timestamp()
|
||||||
|
|
||||||
cost_statistics = []
|
cost_statistics = []
|
||||||
|
|
Loading…
Add table
Reference in a new issue