Fix timing issue in Withings (#105203)

This commit is contained in:
Joost Lekkerkerker 2023-12-13 16:48:46 +01:00 committed by GitHub
parent e475829ce6
commit 816a37f9fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -192,52 +192,67 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = withings_data hass.data.setdefault(DOMAIN, {})[entry.entry_id] = withings_data
register_lock = asyncio.Lock()
webhooks_registered = False
async def unregister_webhook( async def unregister_webhook(
_: Any, _: Any,
) -> None: ) -> None:
LOGGER.debug("Unregister Withings webhook (%s)", entry.data[CONF_WEBHOOK_ID]) nonlocal webhooks_registered
webhook_unregister(hass, entry.data[CONF_WEBHOOK_ID]) async with register_lock:
await async_unsubscribe_webhooks(client) LOGGER.debug(
for coordinator in withings_data.coordinators: "Unregister Withings webhook (%s)", entry.data[CONF_WEBHOOK_ID]
coordinator.webhook_subscription_listener(False) )
webhook_unregister(hass, entry.data[CONF_WEBHOOK_ID])
await async_unsubscribe_webhooks(client)
for coordinator in withings_data.coordinators:
coordinator.webhook_subscription_listener(False)
webhooks_registered = False
async def register_webhook( async def register_webhook(
_: Any, _: Any,
) -> None: ) -> None:
if cloud.async_active_subscription(hass): nonlocal webhooks_registered
webhook_url = await _async_cloudhook_generate_url(hass, entry) async with register_lock:
else: if webhooks_registered:
webhook_url = webhook_generate_url(hass, entry.data[CONF_WEBHOOK_ID]) return
url = URL(webhook_url) if cloud.async_active_subscription(hass):
if url.scheme != "https" or url.port != 443: webhook_url = await _async_cloudhook_generate_url(hass, entry)
LOGGER.warning( else:
"Webhook not registered - " webhook_url = webhook_generate_url(hass, entry.data[CONF_WEBHOOK_ID])
"https and port 443 is required to register the webhook" url = URL(webhook_url)
if url.scheme != "https" or url.port != 443:
LOGGER.warning(
"Webhook not registered - "
"https and port 443 is required to register the webhook"
)
return
webhook_name = "Withings"
if entry.title != DEFAULT_TITLE:
webhook_name = f"{DEFAULT_TITLE} {entry.title}"
webhook_register(
hass,
DOMAIN,
webhook_name,
entry.data[CONF_WEBHOOK_ID],
get_webhook_handler(withings_data),
allowed_methods=[METH_POST],
) )
return LOGGER.debug("Registered Withings webhook at hass: %s", webhook_url)
webhook_name = "Withings" await async_subscribe_webhooks(client, webhook_url)
if entry.title != DEFAULT_TITLE: for coordinator in withings_data.coordinators:
webhook_name = f"{DEFAULT_TITLE} {entry.title}" coordinator.webhook_subscription_listener(True)
LOGGER.debug("Registered Withings webhook at Withings: %s", webhook_url)
webhook_register( entry.async_on_unload(
hass, hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, unregister_webhook)
DOMAIN, )
webhook_name, webhooks_registered = True
entry.data[CONF_WEBHOOK_ID],
get_webhook_handler(withings_data),
allowed_methods=[METH_POST],
)
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)
entry.async_on_unload(
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, unregister_webhook)
)
async def manage_cloudhook(state: cloud.CloudConnectionState) -> None: async def manage_cloudhook(state: cloud.CloudConnectionState) -> None:
LOGGER.debug("Cloudconnection state changed to %s", state)
if state is cloud.CloudConnectionState.CLOUD_CONNECTED: if state is cloud.CloudConnectionState.CLOUD_CONNECTED:
await register_webhook(None) await register_webhook(None)