From bbaa0c16cc0547b485396d793229a6daf6d97b80 Mon Sep 17 00:00:00 2001 From: Arie Catsman <120491684+catsmanac@users.noreply.github.com> Date: Wed, 24 Apr 2024 16:33:14 +0200 Subject: [PATCH] Cancel timer on enphase_envoy config entry unload (#111406) * lingeringtimer * Add async_cleanup to enphase_envoy_coordinator and call from unload_entry --- homeassistant/components/enphase_envoy/__init__.py | 2 ++ homeassistant/components/enphase_envoy/coordinator.py | 6 ++++++ tests/components/enphase_envoy/conftest.py | 2 +- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/enphase_envoy/__init__.py b/homeassistant/components/enphase_envoy/__init__.py index 2407f807eb7..2cdba43453e 100644 --- a/homeassistant/components/enphase_envoy/__init__.py +++ b/homeassistant/components/enphase_envoy/__init__.py @@ -46,6 +46,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Unload a config entry.""" + coordinator = hass.data[DOMAIN][entry.entry_id] + await coordinator.async_cleanup() unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) if unload_ok: hass.data[DOMAIN].pop(entry.entry_id) diff --git a/homeassistant/components/enphase_envoy/coordinator.py b/homeassistant/components/enphase_envoy/coordinator.py index a508d5127d6..c0852fca807 100644 --- a/homeassistant/components/enphase_envoy/coordinator.py +++ b/homeassistant/components/enphase_envoy/coordinator.py @@ -159,3 +159,9 @@ class EnphaseUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]): return envoy_data.raw raise RuntimeError("Unreachable code in _async_update_data") # pragma: no cover + + async def async_cleanup(self) -> None: + """Cleanup coordinator.""" + if self._cancel_token_refresh: + self._cancel_token_refresh() + self._cancel_token_refresh = None diff --git a/tests/components/enphase_envoy/conftest.py b/tests/components/enphase_envoy/conftest.py index 4d50f026c55..965af3b40fc 100644 --- a/tests/components/enphase_envoy/conftest.py +++ b/tests/components/enphase_envoy/conftest.py @@ -343,7 +343,7 @@ def mock_envoy_fixture( @pytest.fixture(name="setup_enphase_envoy") -async def setup_enphase_envoy_fixture(hass, config, mock_envoy): +async def setup_enphase_envoy_fixture(hass: HomeAssistant, config, mock_envoy): """Define a fixture to set up Enphase Envoy.""" with ( patch(