diff --git a/homeassistant/components/broadlink/config_flow.py b/homeassistant/components/broadlink/config_flow.py index 766c2c60940..689ff28523f 100644 --- a/homeassistant/components/broadlink/config_flow.py +++ b/homeassistant/components/broadlink/config_flow.py @@ -62,19 +62,23 @@ class BroadlinkFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): unique_id = discovery_info[MAC_ADDRESS].lower().replace(":", "") await self.async_set_unique_id(unique_id) self._abort_if_unique_id_configured(updates={CONF_HOST: host}) + try: hello = partial(blk.discover, discover_ip_address=host) device = (await self.hass.async_add_executor_job(hello))[0] + except IndexError: return self.async_abort(reason="cannot_connect") + except OSError as err: if err.errno == errno.ENETUNREACH: return self.async_abort(reason="cannot_connect") - return self.async_abort(reason="invalid_host") - except Exception as ex: # pylint: disable=broad-except - _LOGGER.error("Failed to connect to the device at %s", host, exc_info=ex) return self.async_abort(reason="unknown") + supported_types = set.union(*DOMAINS_AND_TYPES.values()) + if device.type not in supported_types: + return self.async_abort(reason="not_supported") + await self.async_set_device(device) return await self.async_step_auth() diff --git a/tests/components/broadlink/test_config_flow.py b/tests/components/broadlink/test_config_flow.py index 503db632cf5..f8f8c81d520 100644 --- a/tests/components/broadlink/test_config_flow.py +++ b/tests/components/broadlink/test_config_flow.py @@ -900,29 +900,10 @@ async def test_dhcp_unreachable(hass): assert result["reason"] == "cannot_connect" -async def test_dhcp_connect_einval(hass): - """Test DHCP discovery flow that fails to connect with EINVAL.""" - await setup.async_setup_component(hass, "persistent_notification", {}) - with patch(DEVICE_DISCOVERY, side_effect=OSError(errno.EINVAL, None)): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": "dhcp"}, - data={ - HOSTNAME: "broadlink", - IP_ADDRESS: "1.2.3.4", - MAC_ADDRESS: "34:ea:34:b4:3b:5a", - }, - ) - await hass.async_block_till_done() - - assert result["type"] == "abort" - assert result["reason"] == "invalid_host" - - async def test_dhcp_connect_unknown_error(hass): - """Test DHCP discovery flow that fails to connect with an unknown error.""" + """Test DHCP discovery flow that fails to connect with an OSError.""" await setup.async_setup_component(hass, "persistent_notification", {}) - with patch(DEVICE_DISCOVERY, side_effect=ValueError("Unknown failure")): + with patch(DEVICE_DISCOVERY, side_effect=OSError()): result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": "dhcp"}, @@ -938,6 +919,27 @@ async def test_dhcp_connect_unknown_error(hass): assert result["reason"] == "unknown" +async def test_dhcp_device_not_supported(hass): + """Test DHCP discovery flow that fails because the device is not supported.""" + await setup.async_setup_component(hass, "persistent_notification", {}) + device = get_device("Kitchen") + mock_api = device.get_mock_api() + + with patch(DEVICE_DISCOVERY, return_value=[mock_api]): + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": "dhcp"}, + data={ + HOSTNAME: "broadlink", + IP_ADDRESS: device.host, + MAC_ADDRESS: device_registry.format_mac(device.mac), + }, + ) + + assert result["type"] == "abort" + assert result["reason"] == "not_supported" + + async def test_dhcp_already_exists(hass): """Test DHCP discovery flow that fails to connect.""" await setup.async_setup_component(hass, "persistent_notification", {})