From 3be7cbd0973c3ac69877bc25cb155477975bab2d Mon Sep 17 00:00:00 2001 From: Joostlek Date: Thu, 22 Feb 2024 08:46:11 +0100 Subject: [PATCH 1/3] Ignore cloudhook already removed in mobile app --- homeassistant/components/mobile_app/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/mobile_app/__init__.py b/homeassistant/components/mobile_app/__init__.py index 831d2d5cdfb02..19aba56f26066 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]) From 77fbbed209093f7121534308839ebfdf2bc3d906 Mon Sep 17 00:00:00 2001 From: Joostlek Date: Thu, 22 Feb 2024 09:57:40 +0100 Subject: [PATCH 2/3] Add test --- tests/components/mobile_app/test_init.py | 25 ++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/components/mobile_app/test_init.py b/tests/components/mobile_app/test_init.py index 6a365e84fb0f1..7637fa068eb2a 100644 --- a/tests/components/mobile_app/test_init.py +++ b/tests/components/mobile_app/test_init.py @@ -5,6 +5,7 @@ import pytest +from homeassistant.components.cloud import CloudNotAvailable from homeassistant.components.mobile_app.const import ( ATTR_DEVICE_NAME, CONF_CLOUDHOOK_URL, @@ -118,6 +119,30 @@ async def additional_steps( await _test_create_cloud_hook(hass, hass_admin_user, {}, True, additional_steps) +@pytest.mark.parametrize("exception", (None, CloudNotAvailable, ValueError)) +async def test_remove_cloudhook( + hass: HomeAssistant, + hass_admin_user: MockUser, + exception: Exception | None, +) -> 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) + + 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, From 67d22fa14c4bf159151b5dc23d33c06975a4fd9a Mon Sep 17 00:00:00 2001 From: Joostlek Date: Thu, 22 Feb 2024 10:22:40 +0100 Subject: [PATCH 3/3] Fix test --- tests/components/mobile_app/test_init.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/components/mobile_app/test_init.py b/tests/components/mobile_app/test_init.py index 7637fa068eb2a..5d25e9568cf13 100644 --- a/tests/components/mobile_app/test_init.py +++ b/tests/components/mobile_app/test_init.py @@ -119,11 +119,12 @@ async def additional_steps( await _test_create_cloud_hook(hass, hass_admin_user, {}, True, additional_steps) -@pytest.mark.parametrize("exception", (None, CloudNotAvailable, ValueError)) +@pytest.mark.parametrize("exception", (CloudNotAvailable, ValueError)) async def test_remove_cloudhook( hass: HomeAssistant, hass_admin_user: MockUser, - exception: Exception | None, + caplog: pytest.LogCaptureFixture, + exception: Exception, ) -> None: """Test removing a cloud hook when config entry is removed.""" @@ -139,6 +140,7 @@ async def additional_steps( 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)