diff --git a/homeassistant/components/plaato/config_flow.py b/homeassistant/components/plaato/config_flow.py index 10bd96aa72c..dc9533c4be2 100644 --- a/homeassistant/components/plaato/config_flow.py +++ b/homeassistant/components/plaato/config_flow.py @@ -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 diff --git a/homeassistant/components/plaato/strings.json b/homeassistant/components/plaato/strings.json index 85bc39a8d83..9a4366da668 100644 --- a/homeassistant/components/plaato/strings.json +++ b/homeassistant/components/plaato/strings.json @@ -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%]" diff --git a/tests/components/plaato/test_config_flow.py b/tests/components/plaato/test_config_flow.py index 887b2b2bb01..ba244138469 100644 --- a/tests/components/plaato/test_config_flow.py +++ b/tests/components/plaato/test_config_flow.py @@ -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."""