Abort owntracks config flow when not connected to home assistant cloud (#64968)

This commit is contained in:
Erik Montnemery 2022-01-26 20:07:34 +01:00 committed by GitHub
parent 664be84121
commit 67838518ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 4 deletions

View file

@ -25,7 +25,10 @@ class OwnTracksFlow(config_entries.ConfigFlow, domain=DOMAIN):
if user_input is None:
return self.async_show_form(step_id="user")
webhook_id, webhook_url, cloudhook = await self._get_webhook_id()
try:
webhook_id, webhook_url, cloudhook = await self._get_webhook_id()
except cloud.CloudNotConnected:
return self.async_abort(reason="cloud_not_connected")
secret = secrets.token_hex(16)

View file

@ -7,6 +7,7 @@
}
},
"abort": {
"cloud_not_connected": "[%key:common::config_flow::abort::cloud_not_connected%]",
"single_instance_allowed": "[%key:common::config_flow::abort::single_instance_allowed%]"
},
"create_entry": {

View file

@ -149,20 +149,48 @@ async def test_unload(hass):
async def test_with_cloud_sub(hass):
"""Test creating a config flow while subscribed."""
hass.config.components.add("cloud")
assert await async_setup_component(hass, "cloud", {})
with patch(
"homeassistant.components.cloud.async_active_subscription", return_value=True
), patch(
"homeassistant.components.cloud.async_create_cloudhook",
return_value="https://hooks.nabu.casa/ABCD",
"homeassistant.components.cloud.async_is_logged_in", return_value=True
), patch(
"homeassistant.components.cloud.async_is_connected", return_value=True
), patch(
"hass_nabucasa.cloudhooks.Cloudhooks.async_create",
return_value={"cloudhook_url": "https://hooks.nabu.casa/ABCD"},
):
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}, data={}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
entry = result["result"]
assert entry.data["cloudhook"]
assert (
result["description_placeholders"]["webhook_url"]
== "https://hooks.nabu.casa/ABCD"
)
async def test_with_cloud_sub_not_connected(hass):
"""Test creating a config flow while subscribed."""
assert await async_setup_component(hass, "cloud", {})
with patch(
"homeassistant.components.cloud.async_active_subscription", return_value=True
), patch(
"homeassistant.components.cloud.async_is_logged_in", return_value=True
), patch(
"homeassistant.components.cloud.async_is_connected", return_value=False
), patch(
"hass_nabucasa.cloudhooks.Cloudhooks.async_create",
return_value={"cloudhook_url": "https://hooks.nabu.casa/ABCD"},
):
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}, data={}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "cloud_not_connected"