Delete existing Withings cloudhook (#101527)

This commit is contained in:
Joost Lekkerkerker 2023-10-06 13:19:39 +02:00 committed by Franck Nijhof
parent 81f582eeb7
commit 76f78e249b
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
2 changed files with 9 additions and 4 deletions

View File

@ -5,6 +5,7 @@ For more details about this platform, please refer to the documentation at
from __future__ import annotations from __future__ import annotations
from collections.abc import Awaitable, Callable from collections.abc import Awaitable, Callable
import contextlib
from typing import Any from typing import Any
from aiohttp.hdrs import METH_HEAD, METH_POST from aiohttp.hdrs import METH_HEAD, METH_POST
@ -214,9 +215,12 @@ async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
async def async_cloudhook_generate_url(hass: HomeAssistant, entry: ConfigEntry) -> str: async def async_cloudhook_generate_url(hass: HomeAssistant, entry: ConfigEntry) -> str:
"""Generate the full URL for a webhook_id.""" """Generate the full URL for a webhook_id."""
if CONF_CLOUDHOOK_URL not in entry.data: if CONF_CLOUDHOOK_URL not in entry.data:
webhook_url = await cloud.async_create_cloudhook( webhook_id = entry.data[CONF_WEBHOOK_ID]
hass, entry.data[CONF_WEBHOOK_ID] # Some users already have their webhook as cloudhook.
) # We remove them to be sure we can create a new one.
with contextlib.suppress(ValueError):
await cloud.async_delete_cloudhook(hass, webhook_id)
webhook_url = await cloud.async_create_cloudhook(hass, webhook_id)
data = {**entry.data, CONF_CLOUDHOOK_URL: webhook_url} data = {**entry.data, CONF_CLOUDHOOK_URL: webhook_url}
hass.config_entries.async_update_entry(entry, data=data) hass.config_entries.async_update_entry(entry, data=data)
return webhook_url return webhook_url

View File

@ -421,6 +421,7 @@ async def test_setup_with_cloud(
assert hass.components.cloud.async_active_subscription() is True assert hass.components.cloud.async_active_subscription() is True
assert hass.components.cloud.async_is_connected() is True assert hass.components.cloud.async_is_connected() is True
fake_create_cloudhook.assert_called_once() fake_create_cloudhook.assert_called_once()
fake_delete_cloudhook.assert_called_once()
assert ( assert (
hass.config_entries.async_entries("withings")[0].data["cloudhook_url"] hass.config_entries.async_entries("withings")[0].data["cloudhook_url"]
@ -432,7 +433,7 @@ async def test_setup_with_cloud(
for config_entry in hass.config_entries.async_entries("withings"): for config_entry in hass.config_entries.async_entries("withings"):
await hass.config_entries.async_remove(config_entry.entry_id) await hass.config_entries.async_remove(config_entry.entry_id)
fake_delete_cloudhook.assert_called_once() fake_delete_cloudhook.call_count == 2
await hass.async_block_till_done() await hass.async_block_till_done()
assert not hass.config_entries.async_entries(DOMAIN) assert not hass.config_entries.async_entries(DOMAIN)