diff --git a/homeassistant/components/ista_ecotrend/__init__.py b/homeassistant/components/ista_ecotrend/__init__.py index 7650f0d5f18..4b698139260 100644 --- a/homeassistant/components/ista_ecotrend/__init__.py +++ b/homeassistant/components/ista_ecotrend/__init__.py @@ -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) diff --git a/tests/components/ista_ecotrend/test_statistics.py b/tests/components/ista_ecotrend/test_statistics.py index aa4f71037c4..b5f419437c5 100644 --- a/tests/components/ista_ecotrend/test_statistics.py +++ b/tests/components/ista_ecotrend/test_statistics.py @@ -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"}, + )