From 697807ce09ab2f6df5ddd9f29b54a48d3d9dccda Mon Sep 17 00:00:00 2001 From: Tobias Sauerwein Date: Mon, 19 Oct 2026 04:26:37 +0200 Subject: [PATCH] Drop a stale Netatmo webhook on teardown (#186827) Co-authored-by: Claude Opus 5.5 (1M context) --- .../components/netatmo/coordinator.py | 3 ++ homeassistant/components/netatmo/webhook.py | 10 ++++- tests/components/netatmo/test_init.py | 42 +++++++++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/netatmo/coordinator.py b/homeassistant/components/netatmo/coordinator.py index 5f0fe008f014..945c04122fe2 100644 --- a/homeassistant/components/netatmo/coordinator.py +++ b/homeassistant/components/netatmo/coordinator.py @@ -176,6 +176,9 @@ class NetatmoDataHandler: self._queue: deque = deque() self._webhook_delivering: bool = False self.webhook_registered: bool = False + # Netatmo can hold a registration made before this load, so the first + # teardown drops one even though this load never registered + self.webhook_dropped: bool = False if config_entry.data["auth_implementation"] == cloud.DOMAIN: self._interval_factor = CLOUD_FACTOR self._rate_limit = CLOUD_LIMIT diff --git a/homeassistant/components/netatmo/webhook.py b/homeassistant/components/netatmo/webhook.py index a64d107e67d7..5739cc5da8cf 100644 --- a/homeassistant/components/netatmo/webhook.py +++ b/homeassistant/components/netatmo/webhook.py @@ -153,13 +153,18 @@ async def async_cloudhook_generate_url( async def async_unregister_webhook( hass: HomeAssistant, entry: NetatmoConfigEntry ) -> None: - """Unregister the webhook from the Netatmo backend.""" + """Unregister the webhook from the Netatmo backend. + + Dropped at most once per teardown, and never gated on whether this load was + the one that registered: what Netatmo holds outlives a restart. + """ if CONF_WEBHOOK_ID not in entry.data: return - if not entry.runtime_data.webhook_registered: + if entry.runtime_data.webhook_dropped: return + entry.runtime_data.webhook_dropped = True entry.runtime_data.webhook_registered = False _LOGGER.debug("Unregister Netatmo webhook (%s)", entry.data[CONF_WEBHOOK_ID]) @@ -219,6 +224,7 @@ async def async_register_webhook( async_handle_webhook, ) entry.runtime_data.webhook_registered = True + entry.runtime_data.webhook_dropped = False try: await entry.runtime_data.auth.async_addwebhook(webhook_url) diff --git a/tests/components/netatmo/test_init.py b/tests/components/netatmo/test_init.py index b7c2db9c5225..61143ef0cd9e 100644 --- a/tests/components/netatmo/test_init.py +++ b/tests/components/netatmo/test_init.py @@ -1850,6 +1850,7 @@ async def test_a_shutdown_drops_the_webhook_once( await hass.async_block_till_done() assert mock_auth.return_value.async_dropwebhook.call_count == 1 + assert config_entry.runtime_data.webhook_registered is False async def test_a_failed_registration_is_still_dropped_at_shutdown( @@ -2428,3 +2429,44 @@ async def test_a_cloudhook_the_cloud_refuses_is_retried( mock_auth.return_value.async_addwebhook.assert_called_once_with( "https://hooks.nabu.casa/ABCD" ) + + +async def test_a_webhook_never_registered_is_still_dropped( + hass: HomeAssistant, config_entry: MockConfigEntry +) -> None: + """Test that teardown clears a registration an earlier run left behind.""" + hass.config.components.add("cloud") + with ( + patch( + "homeassistant.helpers.network.get_url", + return_value="http://example.nabu.casa", + ), + patch( + "homeassistant.components.netatmo.api.AsyncConfigEntryNetatmoAuth" + ) as mock_auth, + patch("homeassistant.components.netatmo.coordinator.PLATFORMS", []), + patch( + "homeassistant.components.netatmo.async_get_config_entry_implementation", + ), + patch( + "homeassistant.components.netatmo.webhook.webhook_generate_url", + return_value="http://example.com", + ), + ): + mock_auth.return_value.async_post_api_request.side_effect = partial( + fake_post_request, hass + ) + mock_auth.return_value.async_addwebhook.side_effect = AsyncMock() + mock_auth.return_value.async_dropwebhook.side_effect = AsyncMock() + + # An http URL is refused before anything is registered + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + mock_auth.return_value.async_addwebhook.assert_not_called() + + hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP) + await hass.async_block_till_done() + + # Netatmo keeps a registration across restarts, so one is dropped regardless + mock_auth.return_value.async_dropwebhook.assert_called_once()