From 92c8c4b1ae34e8dbcbe207749c8028feeba6651b Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Thu, 22 Feb 2024 11:19:27 +0100 Subject: [PATCH] Ignore cloudhook already removed in mobile app (#111122) --- .../components/mobile_app/__init__.py | 2 +- tests/components/mobile_app/test_init.py | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/mobile_app/__init__.py b/homeassistant/components/mobile_app/__init__.py index 831d2d5cdfb..19aba56f260 100644 --- a/homeassistant/components/mobile_app/__init__.py +++ b/homeassistant/components/mobile_app/__init__.py @@ -150,5 +150,5 @@ async def async_remove_entry(hass: HomeAssistant, entry: ConfigEntry) -> None: await store.async_save(savable_state(hass)) if CONF_CLOUDHOOK_URL in entry.data: - with suppress(cloud.CloudNotAvailable): + with suppress(cloud.CloudNotAvailable, ValueError): await cloud.async_delete_cloudhook(hass, entry.data[CONF_WEBHOOK_ID]) diff --git a/tests/components/mobile_app/test_init.py b/tests/components/mobile_app/test_init.py index 6a365e84fb0..5d25e9568cf 100644 --- a/tests/components/mobile_app/test_init.py +++ b/tests/components/mobile_app/test_init.py @@ -5,6 +5,7 @@ from unittest.mock import Mock, patch import pytest +from homeassistant.components.cloud import CloudNotAvailable from homeassistant.components.mobile_app.const import ( ATTR_DEVICE_NAME, CONF_CLOUDHOOK_URL, @@ -118,6 +119,32 @@ async def test_create_cloud_hook_on_setup( await _test_create_cloud_hook(hass, hass_admin_user, {}, True, additional_steps) +@pytest.mark.parametrize("exception", (CloudNotAvailable, ValueError)) +async def test_remove_cloudhook( + hass: HomeAssistant, + hass_admin_user: MockUser, + caplog: pytest.LogCaptureFixture, + exception: Exception, +) -> None: + """Test removing a cloud hook when config entry is removed.""" + + async def additional_steps( + config_entry: ConfigEntry, mock_create_cloudhook: Mock, cloud_hook: str + ) -> None: + webhook_id = config_entry.data[CONF_WEBHOOK_ID] + assert config_entry.data[CONF_CLOUDHOOK_URL] == cloud_hook + with patch( + "homeassistant.components.cloud.async_delete_cloudhook", + side_effect=exception, + ) as delete_cloudhook: + await hass.config_entries.async_remove(config_entry.entry_id) + await hass.async_block_till_done() + delete_cloudhook.assert_called_once_with(hass, webhook_id) + assert str(exception) not in caplog.text + + await _test_create_cloud_hook(hass, hass_admin_user, {}, True, additional_steps) + + async def test_create_cloud_hook_aleady_exists( hass: HomeAssistant, hass_admin_user: MockUser,