Delete mobile_app cloudhook if not logged into the cloud (#123234)
parent
7cb94e6392
commit
260642345d
|
@ -124,12 +124,18 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
):
|
):
|
||||||
await async_create_cloud_hook(hass, webhook_id, entry)
|
await async_create_cloud_hook(hass, webhook_id, entry)
|
||||||
|
|
||||||
if (
|
if cloud.async_is_logged_in(hass):
|
||||||
CONF_CLOUDHOOK_URL not in entry.data
|
if (
|
||||||
and cloud.async_active_subscription(hass)
|
CONF_CLOUDHOOK_URL not in entry.data
|
||||||
and cloud.async_is_connected(hass)
|
and cloud.async_active_subscription(hass)
|
||||||
):
|
and cloud.async_is_connected(hass)
|
||||||
await async_create_cloud_hook(hass, webhook_id, entry)
|
):
|
||||||
|
await async_create_cloud_hook(hass, webhook_id, entry)
|
||||||
|
elif CONF_CLOUDHOOK_URL in entry.data:
|
||||||
|
# If we have a cloudhook but no longer logged in 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))
|
entry.async_on_unload(cloud.async_listen_connection_change(hass, manage_cloudhook))
|
||||||
|
|
||||||
|
|
|
@ -89,6 +89,7 @@ async def _test_create_cloud_hook(
|
||||||
"homeassistant.components.cloud.async_active_subscription",
|
"homeassistant.components.cloud.async_active_subscription",
|
||||||
return_value=async_active_subscription_return_value,
|
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_is_connected", return_value=True),
|
||||||
patch(
|
patch(
|
||||||
"homeassistant.components.cloud.async_get_or_create_cloudhook",
|
"homeassistant.components.cloud.async_get_or_create_cloudhook",
|
||||||
|
@ -187,3 +188,41 @@ async def test_create_cloud_hook_after_connection(
|
||||||
)
|
)
|
||||||
|
|
||||||
await _test_create_cloud_hook(hass, hass_admin_user, {}, False, 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
|
||||||
|
|
Loading…
Reference in New Issue