From 484c7cc755fb69a960b5ece4a37bbf6b9ba43602 Mon Sep 17 00:00:00 2001 From: Robert Resch Date: Tue, 6 Aug 2024 10:59:03 +0000 Subject: [PATCH] Delete mobile_app cloudhook if not logged into the cloud --- .../components/mobile_app/__init__.py | 18 ++++++--- tests/components/mobile_app/test_init.py | 39 +++++++++++++++++++ 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/mobile_app/__init__.py b/homeassistant/components/mobile_app/__init__.py index a8577cc596d0b2..72916b7e1adf4e 100644 --- a/homeassistant/components/mobile_app/__init__.py +++ b/homeassistant/components/mobile_app/__init__.py @@ -124,12 +124,18 @@ async def manage_cloudhook(state: cloud.CloudConnectionState) -> None: ): await async_create_cloud_hook(hass, webhook_id, entry) - if ( - CONF_CLOUDHOOK_URL not in entry.data - and cloud.async_active_subscription(hass) - and cloud.async_is_connected(hass) - ): - await async_create_cloud_hook(hass, webhook_id, entry) + if cloud.async_is_logged_in(hass): + if ( + CONF_CLOUDHOOK_URL not in entry.data + and cloud.async_active_subscription(hass) + and cloud.async_is_connected(hass) + ): + await async_create_cloud_hook(hass, webhook_id, entry) + elif CONF_CLOUDHOOK_URL in entry.data: + # If we have a cloudhook but no longer connected to the cloud, remove it from the entry + data = dict(entry.data) + data.pop(CONF_CLOUDHOOK_URL) + hass.config_entries.async_update_entry(entry, data=data) entry.async_on_unload(cloud.async_listen_connection_change(hass, manage_cloudhook)) diff --git a/tests/components/mobile_app/test_init.py b/tests/components/mobile_app/test_init.py index 15380a0d8d7323..e1c7ed27cf99b8 100644 --- a/tests/components/mobile_app/test_init.py +++ b/tests/components/mobile_app/test_init.py @@ -89,6 +89,7 @@ async def _test_create_cloud_hook( "homeassistant.components.cloud.async_active_subscription", return_value=async_active_subscription_return_value, ), + patch("homeassistant.components.cloud.async_is_logged_in", return_value=True), patch("homeassistant.components.cloud.async_is_connected", return_value=True), patch( "homeassistant.components.cloud.async_get_or_create_cloudhook", @@ -187,3 +188,41 @@ async def additional_steps( ) await _test_create_cloud_hook(hass, hass_admin_user, {}, False, additional_steps) + + +@pytest.mark.parametrize( + ("cloud_logged_in", "should_cloudhook_exist"), + [(True, True), (False, False)], +) +async def test_delete_cloud_hook( + hass: HomeAssistant, + hass_admin_user: MockUser, + cloud_logged_in: bool, + should_cloudhook_exist: bool, +) -> None: + """Test deleting the cloud hook only when logged out of the cloud.""" + + config_entry = MockConfigEntry( + data={ + **REGISTER_CLEARTEXT, + CONF_WEBHOOK_ID: "test-webhook-id", + ATTR_DEVICE_NAME: "Test", + ATTR_DEVICE_ID: "Test", + CONF_USER_ID: hass_admin_user.id, + CONF_CLOUDHOOK_URL: "https://hook-url-already-exists", + }, + domain=DOMAIN, + title="Test", + ) + config_entry.add_to_hass(hass) + + with ( + patch( + "homeassistant.components.cloud.async_is_logged_in", + return_value=cloud_logged_in, + ), + ): + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + assert (CONF_CLOUDHOOK_URL in config_entry.data) == should_cloudhook_exist