Abort plaato flows when not connected to home assistant cloud (#64969)

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

View File

@ -85,7 +85,10 @@ class PlaatoConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
use_webhook = self._init_info[CONF_USE_WEBHOOK]
if use_webhook and user_input is None:
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")
self._init_info[CONF_WEBHOOK_ID] = webhook_id
self._init_info[CONF_CLOUDHOOK] = cloudhook

View File

@ -28,6 +28,7 @@
"no_api_method": "You need to add an auth token or select webhook"
},
"abort": {
"cloud_not_connected": "[%key:common::config_flow::abort::cloud_not_connected%]",
"single_instance_allowed": "[%key:common::config_flow::abort::single_instance_allowed%]",
"webhook_not_internet_accessible": "[%key:common::config_flow::abort::webhook_not_internet_accessible%]",
"already_configured": "[%key:common::config_flow::abort::already_configured_account%]"

View File

@ -12,7 +12,12 @@ from homeassistant.components.plaato.const import (
DOMAIN,
)
from homeassistant.const import CONF_SCAN_INTERVAL, CONF_TOKEN, CONF_WEBHOOK_ID
from homeassistant.data_entry_flow import RESULT_TYPE_CREATE_ENTRY, RESULT_TYPE_FORM
from homeassistant.data_entry_flow import (
RESULT_TYPE_ABORT,
RESULT_TYPE_CREATE_ENTRY,
RESULT_TYPE_FORM,
)
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry
@ -95,12 +100,16 @@ async def test_show_config_form_validate_webhook(hass, webhook_id):
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "api_method"
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_configure(
result["flow_id"],
@ -114,6 +123,50 @@ async def test_show_config_form_validate_webhook(hass, webhook_id):
assert result["step_id"] == "webhook"
async def test_show_config_form_validate_webhook_not_connected(hass, webhook_id):
"""Test validating webhook when not connected aborts."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "user"
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={
CONF_DEVICE_TYPE: PlaatoDeviceType.Airlock,
CONF_DEVICE_NAME: "device_name",
},
)
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "api_method"
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_configure(
result["flow_id"],
user_input={
CONF_TOKEN: "",
CONF_USE_WEBHOOK: True,
},
)
assert result["type"] == RESULT_TYPE_ABORT
assert result["reason"] == "cloud_not_connected"
async def test_show_config_form_validate_token(hass):
"""Test show configuration form."""