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: if user_input is None:
return self.async_show_form(step_id="user") return self.async_show_form(step_id="user")
try:
webhook_id, webhook_url, cloudhook = await self._get_webhook_id() 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) secret = secrets.token_hex(16)

View File

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

View File

@ -149,20 +149,48 @@ async def test_unload(hass):
async def test_with_cloud_sub(hass): async def test_with_cloud_sub(hass):
"""Test creating a config flow while subscribed.""" """Test creating a config flow while subscribed."""
hass.config.components.add("cloud") assert await async_setup_component(hass, "cloud", {})
with patch( with patch(
"homeassistant.components.cloud.async_active_subscription", return_value=True "homeassistant.components.cloud.async_active_subscription", return_value=True
), patch( ), patch(
"homeassistant.components.cloud.async_create_cloudhook", "homeassistant.components.cloud.async_is_logged_in", return_value=True
return_value="https://hooks.nabu.casa/ABCD", ), 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( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}, data={} DOMAIN, context={"source": config_entries.SOURCE_USER}, data={}
) )
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
entry = result["result"] entry = result["result"]
assert entry.data["cloudhook"] assert entry.data["cloudhook"]
assert ( assert (
result["description_placeholders"]["webhook_url"] result["description_placeholders"]["webhook_url"]
== "https://hooks.nabu.casa/ABCD" == "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"