From 716b559e5d50a00f47b96f581f13bc969df88c8c Mon Sep 17 00:00:00 2001 From: tronikos Date: Sat, 3 May 2025 12:12:01 -0700 Subject: [PATCH] Skip the update right after the migration in Opower (#144088) * Wait for the migration to finish in Opower * Don't call async_block_till_done since this can timeout and seems to meant for tests * Don't call async_block_till_done since this can timeout and seems to meant for tests --- .../components/opower/coordinator.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/opower/coordinator.py b/homeassistant/components/opower/coordinator.py index adb32d914ee..dd0b2c87bb5 100644 --- a/homeassistant/components/opower/coordinator.py +++ b/homeassistant/components/opower/coordinator.py @@ -190,7 +190,7 @@ class OpowerCoordinator(DataUpdateCoordinator[dict[str, Forecast]]): return_sum = 0.0 last_stats_time = None else: - await self._async_maybe_migrate_statistics( + migrated = await self._async_maybe_migrate_statistics( account.utility_account_id, { cost_statistic_id: compensation_statistic_id, @@ -203,6 +203,13 @@ class OpowerCoordinator(DataUpdateCoordinator[dict[str, Forecast]]): return_statistic_id: return_metadata, }, ) + if migrated: + # Skip update to avoid working on old data since the migration is done + # asynchronously. Update the statistics in the next refresh in 12h. + _LOGGER.debug( + "Statistics migration completed. Skipping update for now" + ) + continue cost_reads = await self._async_get_cost_reads( account, self.api.utility.timezone(), @@ -326,7 +333,7 @@ class OpowerCoordinator(DataUpdateCoordinator[dict[str, Forecast]]): utility_account_id: str, migration_map: dict[str, str], metadata_map: dict[str, StatisticMetaData], - ) -> None: + ) -> bool: """Perform one-time statistics migration based on the provided map. Splits negative values from source IDs into target IDs. @@ -339,7 +346,7 @@ class OpowerCoordinator(DataUpdateCoordinator[dict[str, Forecast]]): """ if not migration_map: - return + return False need_migration_source_ids = set() for source_id, target_id in migration_map.items(): @@ -354,7 +361,7 @@ class OpowerCoordinator(DataUpdateCoordinator[dict[str, Forecast]]): if not last_target_stat: need_migration_source_ids.add(source_id) if not need_migration_source_ids: - return + return False _LOGGER.info("Starting one-time migration for: %s", need_migration_source_ids) @@ -416,7 +423,7 @@ class OpowerCoordinator(DataUpdateCoordinator[dict[str, Forecast]]): if not need_migration_source_ids: _LOGGER.debug("No migration needed") - return + return False for stat_id, stats in processed_stats.items(): _LOGGER.debug("Applying %d migrated stats for %s", len(stats), stat_id) @@ -442,6 +449,8 @@ class OpowerCoordinator(DataUpdateCoordinator[dict[str, Forecast]]): }, ) + return True + async def _async_get_cost_reads( self, account: Account, time_zone_str: str, start_time: float | None = None ) -> list[CostRead]: