From 0fdd456cee9fb373ef780986f40933378658afaa Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 29 Nov 2021 21:06:32 +0000 Subject: [PATCH 1/2] Use dataclass properties in bosch_shc discovery --- homeassistant/components/bosch_shc/config_flow.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/bosch_shc/config_flow.py b/homeassistant/components/bosch_shc/config_flow.py index c642df2a619f6f..c9f30a2fb4d8f9 100644 --- a/homeassistant/components/bosch_shc/config_flow.py +++ b/homeassistant/components/bosch_shc/config_flow.py @@ -191,9 +191,9 @@ async def async_step_zeroconf( try: hosts = ( - discovery_info[zeroconf.ATTR_HOST] - if isinstance(discovery_info[zeroconf.ATTR_HOST], list) - else [discovery_info[zeroconf.ATTR_HOST]] + 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 @@ -205,7 +205,7 @@ async def async_step_zeroconf( except SHCConnectionError: return self.async_abort(reason="cannot_connect") - local_name = discovery_info[zeroconf.ATTR_HOSTNAME][:-1] + local_name = discovery_info.hostname[:-1] node_name = local_name[: -len(".local")] await self.async_set_unique_id(self.info["unique_id"]) From f941f78fe61ba1b856f83e5db77f2081a099eb93 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 29 Nov 2021 21:50:37 +0000 Subject: [PATCH 2/2] host cannot be a list --- .../components/bosch_shc/config_flow.py | 14 ++--------- .../components/bosch_shc/test_config_flow.py | 25 +------------------ 2 files changed, 3 insertions(+), 36 deletions(-) diff --git a/homeassistant/components/bosch_shc/config_flow.py b/homeassistant/components/bosch_shc/config_flow.py index c9f30a2fb4d8f9..6ad1a374a5ab71 100644 --- a/homeassistant/components/bosch_shc/config_flow.py +++ b/homeassistant/components/bosch_shc/config_flow.py @@ -190,20 +190,10 @@ async def async_step_zeroconf( return self.async_abort(reason="not_bosch_shc") try: - 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.info is None or self.host is None: - return self.async_abort(reason="cannot_connect") + self.info = await self._get_info(discovery_info.host) except SHCConnectionError: return self.async_abort(reason="cannot_connect") + self.host = discovery_info.host local_name = discovery_info.hostname[:-1] node_name = local_name[: -len(".local")] diff --git a/tests/components/bosch_shc/test_config_flow.py b/tests/components/bosch_shc/test_config_flow.py index b10b76b1042218..065035bedbe777 100644 --- a/tests/components/bosch_shc/test_config_flow.py +++ b/tests/components/bosch_shc/test_config_flow.py @@ -21,7 +21,7 @@ "device": {"mac": "test-mac", "hostname": "test-host"}, } DISCOVERY_INFO = zeroconf.ZeroconfServiceInfo( - host=["169.1.1.1", "1.1.1.1"], + host="1.1.1.1", hostname="shc012345.local.", name="Bosch SHC [test-mac]._http._tcp.local.", port=0, @@ -527,29 +527,6 @@ 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 = zeroconf.ZeroconfServiceInfo( - host=["169.1.1.1"], - hostname="shc012345.local.", - name="Bosch SHC [test-mac]._http._tcp.local.", - port=0, - properties={}, - type="_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(