From b645b151f987dda47df390870c35cae8f8b010ea Mon Sep 17 00:00:00 2001 From: Maciej Bieniek Date: Wed, 24 Feb 2021 14:13:32 +0100 Subject: [PATCH] Catch AuthRequired exception in confirm discovery step for Shelly config flow (#46135) --- .../components/shelly/config_flow.py | 2 ++ tests/components/shelly/test_config_flow.py | 30 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/homeassistant/components/shelly/config_flow.py b/homeassistant/components/shelly/config_flow.py index cd74b83a62a..dfb078ee9c7 100644 --- a/homeassistant/components/shelly/config_flow.py +++ b/homeassistant/components/shelly/config_flow.py @@ -192,6 +192,8 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): device_info = await validate_input(self.hass, self.host, {}) except HTTP_CONNECT_ERRORS: errors["base"] = "cannot_connect" + except aioshelly.AuthRequired: + return await self.async_step_credentials() except Exception: # pylint: disable=broad-except _LOGGER.exception("Unexpected exception") errors["base"] = "unknown" diff --git a/tests/components/shelly/test_config_flow.py b/tests/components/shelly/test_config_flow.py index 450bf8efb24..9dfbc19255b 100644 --- a/tests/components/shelly/test_config_flow.py +++ b/tests/components/shelly/test_config_flow.py @@ -512,6 +512,36 @@ async def test_zeroconf_confirm_error(hass, error): assert result2["errors"] == {"base": base_error} +async def test_zeroconf_confirm_auth_error(hass): + """Test we get credentials form after an auth error when confirming discovery.""" + await setup.async_setup_component(hass, "persistent_notification", {}) + + with patch( + "aioshelly.get_info", + return_value={"mac": "test-mac", "type": "SHSW-1", "auth": False}, + ): + result = await hass.config_entries.flow.async_init( + DOMAIN, + data=DISCOVERY_INFO, + context={"source": config_entries.SOURCE_ZEROCONF}, + ) + assert result["type"] == data_entry_flow.RESULT_TYPE_FORM + assert result["errors"] == {} + + with patch( + "aioshelly.Device.create", + new=AsyncMock(side_effect=aioshelly.AuthRequired), + ): + result2 = await hass.config_entries.flow.async_configure( + result["flow_id"], + {}, + ) + + assert result2["type"] == data_entry_flow.RESULT_TYPE_FORM + assert result2["step_id"] == "credentials" + assert result2["errors"] == {} + + async def test_zeroconf_already_configured(hass): """Test we get the form.""" await setup.async_setup_component(hass, "persistent_notification", {})