Skip to content

Commit

Permalink
Attempt to fix IndexError in Opower (#124478)
Browse files Browse the repository at this point in the history
* Change the order of async_add_external_statistics in Opower

* Use consumption_statistic_id instead of cost_statistic_id
  • Loading branch information
tronikos committed Aug 30, 2024
1 parent cb742a6 commit 910fb09
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions homeassistant/components/opower/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ async def _insert_statistics(self) -> None:
)

last_stat = await get_instance(self.hass).async_add_executor_job(
get_last_statistics, self.hass, 1, cost_statistic_id, True, set()
get_last_statistics, self.hass, 1, consumption_statistic_id, True, set()
)
if not last_stat:
_LOGGER.debug("Updating statistic for the first time")
Expand All @@ -124,7 +124,7 @@ async def _insert_statistics(self) -> None:
cost_reads = await self._async_get_cost_reads(
account,
self.api.utility.timezone(),
last_stat[cost_statistic_id][0]["start"],
last_stat[consumption_statistic_id][0]["start"],
)
if not cost_reads:
_LOGGER.debug("No recent usage/cost data. Skipping update")
Expand All @@ -141,7 +141,7 @@ async def _insert_statistics(self) -> None:
)
cost_sum = cast(float, stats[cost_statistic_id][0]["sum"])
consumption_sum = cast(float, stats[consumption_statistic_id][0]["sum"])
last_stats_time = stats[cost_statistic_id][0]["start"]
last_stats_time = stats[consumption_statistic_id][0]["start"]

cost_statistics = []
consumption_statistics = []
Expand Down Expand Up @@ -187,7 +187,17 @@ async def _insert_statistics(self) -> None:
else UnitOfVolume.CENTUM_CUBIC_FEET,
)

_LOGGER.debug(
"Adding %s statistics for %s",
len(cost_statistics),
cost_statistic_id,
)
async_add_external_statistics(self.hass, cost_metadata, cost_statistics)
_LOGGER.debug(
"Adding %s statistics for %s",
len(consumption_statistics),
consumption_statistic_id,
)
async_add_external_statistics(
self.hass, consumption_metadata, consumption_statistics
)
Expand Down

0 comments on commit 910fb09

Please sign in to comment.