Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix timing issue in Withings #105203

Merged
merged 5 commits into from Dec 13, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion homeassistant/components/withings/__init__.py
Expand Up @@ -168,6 +168,8 @@

refresh_lock = asyncio.Lock()

is_registering_webhooks = False

async def _refresh_token() -> str:
async with refresh_lock:
await oauth_session.async_ensure_token_valid()
Expand Down Expand Up @@ -204,6 +206,10 @@
async def register_webhook(
_: Any,
) -> None:
nonlocal is_registering_webhooks
if is_registering_webhooks:
return

Check warning on line 211 in homeassistant/components/withings/__init__.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/withings/__init__.py#L211

Added line #L211 was not covered by tests
is_registering_webhooks = True
if cloud.async_active_subscription(hass):
webhook_url = await _async_cloudhook_generate_url(hass, entry)
else:
Expand All @@ -228,16 +234,19 @@
get_webhook_handler(withings_data),
allowed_methods=[METH_POST],
)
LOGGER.debug("Registered Withings webhook at hass: %s", webhook_url)

await async_subscribe_webhooks(client, webhook_url)
for coordinator in withings_data.coordinators:
coordinator.webhook_subscription_listener(True)
LOGGER.debug("Register Withings webhook: %s", webhook_url)
LOGGER.debug("Registered Withings webhook at Withings: %s", webhook_url)
entry.async_on_unload(
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, unregister_webhook)
)
is_registering_webhooks = False

async def manage_cloudhook(state: cloud.CloudConnectionState) -> None:
LOGGER.debug("Cloudconnection state changed to %s", state)
if state is cloud.CloudConnectionState.CLOUD_CONNECTED:
await register_webhook(None)

Expand Down