Clear statistics on entry removal in ista EcoTrend integration (#143433)

This commit is contained in:
Manu 2025-04-22 13:57:28 +02:00 committed by GitHub
parent c654936a91
commit ccd1a08aca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 65 additions and 0 deletions

View File

@ -6,6 +6,7 @@ import logging
from pyecotrend_ista import PyEcotrendIsta
from homeassistant.components.recorder import get_instance
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, Platform
from homeassistant.core import HomeAssistant
@ -37,3 +38,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: IstaConfigEntry) -> bool
async def async_unload_entry(hass: HomeAssistant, entry: IstaConfigEntry) -> bool:
"""Unload a config entry."""
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
async def async_remove_entry(hass: HomeAssistant, entry: IstaConfigEntry) -> None:
"""Handle removal of an entry."""
statistic_ids = [f"{DOMAIN}:{name}" for name in entry.options.values()]
get_instance(hass).async_clear_statistics(statistic_ids)

View File

@ -84,3 +84,61 @@ async def test_statistics_import(
assert stats[statistic_id] == snapshot(name=f"{statistic_id}_3months")
assert len(stats[statistic_id]) == 3
@pytest.mark.usefixtures("recorder_mock", "mock_ista")
async def test_remove(
hass: HomeAssistant,
ista_config_entry: MockConfigEntry,
) -> None:
"""Test remove config entry and clear statistics."""
ista_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(ista_config_entry.entry_id)
await hass.async_block_till_done()
assert ista_config_entry.state is ConfigEntryState.LOADED
await async_wait_recording_done(hass)
assert await hass.async_add_executor_job(
statistics_during_period,
hass,
datetime.datetime.fromtimestamp(0, tz=datetime.UTC),
None,
{"ista_ecotrend:bahnhofsstr_1a_heating"},
"month",
None,
{"state", "sum"},
)
assert await hass.config_entries.async_unload(ista_config_entry.entry_id)
await hass.async_block_till_done()
assert ista_config_entry.state is ConfigEntryState.NOT_LOADED
await async_wait_recording_done(hass)
assert await hass.async_add_executor_job(
statistics_during_period,
hass,
datetime.datetime.fromtimestamp(0, tz=datetime.UTC),
None,
{"ista_ecotrend:bahnhofsstr_1a_heating"},
"month",
None,
{"state", "sum"},
)
assert await hass.config_entries.async_remove(ista_config_entry.entry_id)
await hass.async_block_till_done()
await async_wait_recording_done(hass)
assert not await hass.async_add_executor_job(
statistics_during_period,
hass,
datetime.datetime.fromtimestamp(0, tz=datetime.UTC),
None,
{"ista_ecotrend:bahnhofsstr_1a_heating"},
"month",
None,
{"state", "sum"},
)