Subscribe Withings webhooks after Home Assistant has started (#182017)

This commit is contained in:
Franck Nijhof
2026-09-12 08:19:00 -04:00
committed by GitHub
parent 8523c14c14
commit 76ea390074
2 changed files with 40 additions and 14 deletions
+14 -12
View File
@@ -40,7 +40,7 @@ from homeassistant.const import (
EVENT_HOMEASSISTANT_STOP,
Platform,
)
from homeassistant.core import CALLBACK_TYPE, HomeAssistant
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.config_entry_oauth2_flow import (
@@ -48,6 +48,7 @@ from homeassistant.helpers.config_entry_oauth2_flow import (
async_get_config_entry_implementation,
)
from homeassistant.helpers.event import async_call_later
from homeassistant.helpers.start import async_at_started
from .const import DEFAULT_TITLE, DOMAIN, LOGGER
from .coordinator import (
@@ -164,23 +165,24 @@ async def async_setup_entry(hass: HomeAssistant, entry: WithingsConfigEntry) ->
async_call_later(hass, 30, webhook_manager.register_webhook)
)
if cloud.async_active_subscription(hass):
if cloud.async_is_connected(hass):
entry.async_on_unload(
async_call_later(
hass, WEBHOOK_REGISTER_DELAY, webhook_manager.register_webhook
)
)
entry.async_on_unload(
cloud.async_listen_connection_change(hass, manage_cloudhook)
)
else:
@callback
def _async_register_webhook_later(_: HomeAssistant) -> None:
"""Register the webhook once Home Assistant answers for it."""
entry.async_on_unload(
async_call_later(
hass, WEBHOOK_REGISTER_DELAY, webhook_manager.register_webhook
)
)
if cloud.async_active_subscription(hass):
if cloud.async_is_connected(hass):
entry.async_on_unload(async_at_started(hass, _async_register_webhook_later))
entry.async_on_unload(
cloud.async_listen_connection_change(hass, manage_cloudhook)
)
else:
entry.async_on_unload(async_at_started(hass, _async_register_webhook_later))
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True
+26 -2
View File
@@ -26,8 +26,8 @@ from homeassistant.components.cloud import CloudNotAvailable
from homeassistant.components.webhook import async_generate_url
from homeassistant.components.withings.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import CONF_WEBHOOK_ID
from homeassistant.core import HomeAssistant
from homeassistant.const import CONF_WEBHOOK_ID, EVENT_HOMEASSISTANT_STARTED
from homeassistant.core import CoreState, HomeAssistant
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.config_entry_oauth2_flow import (
ImplementationUnavailableError,
@@ -857,6 +857,30 @@ async def test_webhook_subscription_auth_failure(
)
async def test_webhook_subscription_waits_for_start(
hass: HomeAssistant,
withings: AsyncMock,
webhook_config_entry: MockConfigEntry,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test the webhook is only subscribed once Home Assistant has started."""
hass.set_state(CoreState.not_running)
await setup_integration(hass, webhook_config_entry)
await prepare_webhook_setup(hass, freezer)
# Withings validates the callback URL by calling it, which cannot work
# while our own HTTP server is still starting up
assert withings.subscribe_notification.call_count == 0
hass.set_state(CoreState.running)
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
await hass.async_block_till_done()
await prepare_webhook_setup(hass, freezer)
assert withings.subscribe_notification.call_count == 6
async def test_webhook_subscription_invalid_params(
hass: HomeAssistant,
withings: AsyncMock,