diff --git a/homeassistant/components/shelly/config_flow.py b/homeassistant/components/shelly/config_flow.py index dfb078ee9c7..0ad95d67833 100644 --- a/homeassistant/components/shelly/config_flow.py +++ b/homeassistant/components/shelly/config_flow.py @@ -160,52 +160,34 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): self._abort_if_unique_id_configured({CONF_HOST: zeroconf_info["host"]}) self.host = zeroconf_info["host"] - if not info["auth"] and info.get("sleep_mode", False): - try: - self.device_info = await validate_input(self.hass, self.host, {}) - except HTTP_CONNECT_ERRORS: - return self.async_abort(reason="cannot_connect") - self.context["title_placeholders"] = { "name": zeroconf_info.get("name", "").split(".")[0] } + + if info["auth"]: + return await self.async_step_credentials() + + try: + self.device_info = await validate_input(self.hass, self.host, {}) + except HTTP_CONNECT_ERRORS: + return self.async_abort(reason="cannot_connect") + return await self.async_step_confirm_discovery() async def async_step_confirm_discovery(self, user_input=None): """Handle discovery confirm.""" errors = {} if user_input is not None: - if self.info["auth"]: - return await self.async_step_credentials() + return self.async_create_entry( + title=self.device_info["title"] or self.device_info["hostname"], + data={ + "host": self.host, + "sleep_period": self.device_info["sleep_period"], + "model": self.device_info["model"], + }, + ) - if self.device_info: - return self.async_create_entry( - title=self.device_info["title"] or self.device_info["hostname"], - data={ - "host": self.host, - "sleep_period": self.device_info["sleep_period"], - "model": self.device_info["model"], - }, - ) - - try: - 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" - else: - return self.async_create_entry( - title=device_info["title"] or device_info["hostname"], - data={ - "host": self.host, - "sleep_period": device_info["sleep_period"], - "model": device_info["model"], - }, - ) + self._set_confirm_only() return self.async_show_form( step_id="confirm_discovery", diff --git a/tests/components/shelly/test_config_flow.py b/tests/components/shelly/test_config_flow.py index 9dfbc19255b..463c9111a60 100644 --- a/tests/components/shelly/test_config_flow.py +++ b/tests/components/shelly/test_config_flow.py @@ -338,6 +338,13 @@ async def test_zeroconf(hass): with patch( "aioshelly.get_info", return_value={"mac": "test-mac", "type": "SHSW-1", "auth": False}, + ), patch( + "aioshelly.Device.create", + new=AsyncMock( + return_value=Mock( + settings=MOCK_SETTINGS, + ) + ), ): result = await hass.config_entries.flow.async_init( DOMAIN, @@ -352,14 +359,8 @@ async def test_zeroconf(hass): if flow["flow_id"] == result["flow_id"] ) assert context["title_placeholders"]["name"] == "shelly1pm-12345" + assert context["confirm_only"] is True with patch( - "aioshelly.Device.create", - new=AsyncMock( - return_value=Mock( - settings=MOCK_SETTINGS, - ) - ), - ), patch( "homeassistant.components.shelly.async_setup", return_value=True ) as mock_setup, patch( "homeassistant.components.shelly.async_setup_entry", @@ -479,69 +480,6 @@ async def test_zeroconf_sleeping_device_error(hass, error): assert result["reason"] == "cannot_connect" -@pytest.mark.parametrize( - "error", [(asyncio.TimeoutError, "cannot_connect"), (ValueError, "unknown")] -) -async def test_zeroconf_confirm_error(hass, error): - """Test we get the form.""" - exc, base_error = error - 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=exc), - ): - result2 = await hass.config_entries.flow.async_configure( - result["flow_id"], - {}, - ) - - assert result2["type"] == data_entry_flow.RESULT_TYPE_FORM - 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", {}) @@ -607,13 +545,6 @@ async def test_zeroconf_require_auth(hass): assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["errors"] == {} - result2 = await hass.config_entries.flow.async_configure( - result["flow_id"], - {}, - ) - assert result2["type"] == data_entry_flow.RESULT_TYPE_FORM - assert result2["errors"] == {} - with patch( "aioshelly.Device.create", new=AsyncMock( @@ -627,15 +558,15 @@ async def test_zeroconf_require_auth(hass): "homeassistant.components.shelly.async_setup_entry", return_value=True, ) as mock_setup_entry: - result3 = await hass.config_entries.flow.async_configure( - result2["flow_id"], + result2 = await hass.config_entries.flow.async_configure( + result["flow_id"], {"username": "test username", "password": "test password"}, ) await hass.async_block_till_done() - assert result3["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY - assert result3["title"] == "Test name" - assert result3["data"] == { + assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY + assert result2["title"] == "Test name" + assert result2["data"] == { "host": "1.1.1.1", "model": "SHSW-1", "sleep_period": 0,