From aa03b63884e3230801419d86fa43970ddaa08d4c Mon Sep 17 00:00:00 2001 From: Thomas Schamm Date: Wed, 6 Oct 2021 10:09:02 +0200 Subject: [PATCH] Skip link local addresses in bosch_shc discovery step (#57074) --- .../components/bosch_shc/config_flow.py | 18 ++++++++++---- .../components/bosch_shc/test_config_flow.py | 24 ++++++++++++++++++- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/bosch_shc/config_flow.py b/homeassistant/components/bosch_shc/config_flow.py index 4415a0ff6ef..416dc6cf304 100644 --- a/homeassistant/components/bosch_shc/config_flow.py +++ b/homeassistant/components/bosch_shc/config_flow.py @@ -187,16 +187,26 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): return self.async_abort(reason="not_bosch_shc") try: - self.info = info = await self._get_info(discovery_info["host"]) + hosts = ( + discovery_info["host"] + if isinstance(discovery_info["host"], list) + else [discovery_info["host"]] + ) + for host in hosts: + if host.startswith("169."): # skip link local address + continue + self.info = await self._get_info(host) + self.host = host + if self.host is None: + return self.async_abort(reason="cannot_connect") except SHCConnectionError: return self.async_abort(reason="cannot_connect") local_name = discovery_info["hostname"][:-1] node_name = local_name[: -len(".local")] - await self.async_set_unique_id(info["unique_id"]) - self._abort_if_unique_id_configured({CONF_HOST: discovery_info["host"]}) - self.host = discovery_info["host"] + await self.async_set_unique_id(self.info["unique_id"]) + self._abort_if_unique_id_configured({CONF_HOST: self.host}) self.context["title_placeholders"] = {"name": node_name} return await self.async_step_confirm_discovery() diff --git a/tests/components/bosch_shc/test_config_flow.py b/tests/components/bosch_shc/test_config_flow.py index 6d8ef9bd32e..543d0438738 100644 --- a/tests/components/bosch_shc/test_config_flow.py +++ b/tests/components/bosch_shc/test_config_flow.py @@ -20,7 +20,7 @@ MOCK_SETTINGS = { "device": {"mac": "test-mac", "hostname": "test-host"}, } DISCOVERY_INFO = { - "host": "1.1.1.1", + "host": ["169.1.1.1", "1.1.1.1"], "port": 0, "hostname": "shc012345.local.", "type": "_http._tcp.local.", @@ -526,6 +526,28 @@ async def test_zeroconf_cannot_connect(hass, mock_zeroconf): assert result["reason"] == "cannot_connect" +async def test_zeroconf_link_local(hass, mock_zeroconf): + """Test we get the form.""" + DISCOVERY_INFO_LINK_LOCAL = { + "host": ["169.1.1.1"], + "port": 0, + "hostname": "shc012345.local.", + "type": "_http._tcp.local.", + "name": "Bosch SHC [test-mac]._http._tcp.local.", + } + + with patch( + "boschshcpy.session.SHCSession.mdns_info", side_effect=SHCConnectionError + ): + result = await hass.config_entries.flow.async_init( + DOMAIN, + data=DISCOVERY_INFO_LINK_LOCAL, + context={"source": config_entries.SOURCE_ZEROCONF}, + ) + assert result["type"] == "abort" + assert result["reason"] == "cannot_connect" + + async def test_zeroconf_not_bosch_shc(hass, mock_zeroconf): """Test we filter out non-bosch_shc devices.""" result = await hass.config_entries.flow.async_init(