Clear statistics when you unload the Opower integration (#135908)

* Clear statistics when you remove the Opower integration

* fix
This commit is contained in:
tronikos 2025-02-07 19:41:09 -08:00 committed by GitHub
parent 7883106e7c
commit aa19207ea4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 16 additions and 7 deletions

View File

@ -2,18 +2,14 @@
from __future__ import annotations
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from .coordinator import OpowerCoordinator
from .coordinator import OpowerConfigEntry, OpowerCoordinator
PLATFORMS: list[Platform] = [Platform.SENSOR]
type OpowerConfigEntry = ConfigEntry[OpowerCoordinator]
async def async_setup_entry(hass: HomeAssistant, entry: OpowerConfigEntry) -> bool:
"""Set up Opower from a config entry."""

View File

@ -23,6 +23,7 @@ from homeassistant.components.recorder.statistics import (
get_last_statistics,
statistics_during_period,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, UnitOfEnergy, UnitOfVolume
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryAuthFailed
@ -34,10 +35,14 @@ from .const import CONF_TOTP_SECRET, CONF_UTILITY, DOMAIN
_LOGGER = logging.getLogger(__name__)
type OpowerConfigEntry = ConfigEntry[OpowerCoordinator]
class OpowerCoordinator(DataUpdateCoordinator[dict[str, Forecast]]):
"""Handle fetching Opower data, updating sensors and inserting statistics."""
config_entry: OpowerConfigEntry
def __init__(
self,
hass: HomeAssistant,
@ -59,6 +64,7 @@ class OpowerCoordinator(DataUpdateCoordinator[dict[str, Forecast]]):
entry_data[CONF_PASSWORD],
entry_data.get(CONF_TOTP_SECRET),
)
self._statistic_ids: set[str] = set()
@callback
def _dummy_listener() -> None:
@ -70,6 +76,12 @@ class OpowerCoordinator(DataUpdateCoordinator[dict[str, Forecast]]):
# _async_update_data not periodically getting called which is needed for _insert_statistics.
self.async_add_listener(_dummy_listener)
self.config_entry.async_on_unload(self._clear_statistics)
def _clear_statistics(self) -> None:
"""Clear statistics."""
get_instance(self.hass).async_clear_statistics(list(self._statistic_ids))
async def _async_update_data(
self,
) -> dict[str, Forecast]:
@ -115,6 +127,8 @@ class OpowerCoordinator(DataUpdateCoordinator[dict[str, Forecast]]):
)
cost_statistic_id = f"{DOMAIN}:{id_prefix}_energy_cost"
consumption_statistic_id = f"{DOMAIN}:{id_prefix}_energy_consumption"
self._statistic_ids.add(cost_statistic_id)
self._statistic_ids.add(consumption_statistic_id)
_LOGGER.debug(
"Updating Statistics for %s and %s",
cost_statistic_id,

View File

@ -20,9 +20,8 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import OpowerConfigEntry
from .const import DOMAIN
from .coordinator import OpowerCoordinator
from .coordinator import OpowerConfigEntry, OpowerCoordinator
@dataclass(frozen=True, kw_only=True)