Drop a stale Netatmo webhook on teardown (#186827)

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Tobias Sauerwein
2026-10-19 02:26:37 +00:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 59fa8aabb7
commit 697807ce09
3 changed files with 53 additions and 2 deletions
@@ -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
+8 -2
View File
@@ -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)
+42
View File
@@ -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()