Skip to content

Commit

Permalink
Delete mobile_app cloudhook if not logged into the cloud
Browse files Browse the repository at this point in the history
  • Loading branch information
edenhaus authored Aug 6, 2024
1 parent bc38085 commit 484c7cc
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
18 changes: 12 additions & 6 deletions homeassistant/components/mobile_app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
39 changes: 39 additions & 0 deletions tests/components/mobile_app/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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

0 comments on commit 484c7cc

Please sign in to comment.