From e5d0b2a6995ced6e1a049ba0f68ca785e8d3c45b Mon Sep 17 00:00:00 2001 From: Guido Schmitz Date: Wed, 4 May 2022 12:13:29 +0000 Subject: [PATCH 1/3] Handle empty zeroconf properties in devolo_home_network --- .../devolo_home_network/config_flow.py | 8 +++++++- tests/components/devolo_home_network/const.py | 13 ++++++++++++- .../devolo_home_network/test_config_flow.py | 18 +++++++++++++++++- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/devolo_home_network/config_flow.py b/homeassistant/components/devolo_home_network/config_flow.py index c96126f43e2af3..3ba870d79d286f 100644 --- a/homeassistant/components/devolo_home_network/config_flow.py +++ b/homeassistant/components/devolo_home_network/config_flow.py @@ -78,6 +78,12 @@ async def async_step_zeroconf( self, discovery_info: zeroconf.ZeroconfServiceInfo ) -> FlowResult: """Handle zeroconf discovery.""" + if ( + "MT" not in discovery_info.properties + or "SN" not in discovery_info.properties + ): + return self.async_abort(reason="incomplete_query") + if discovery_info.properties["MT"] in ["2600", "2601"]: return self.async_abort(reason="home_control") @@ -86,7 +92,7 @@ async def async_step_zeroconf( self.context[CONF_HOST] = discovery_info.host self.context["title_placeholders"] = { - PRODUCT: discovery_info.properties["Product"], + PRODUCT: discovery_info.properties.get("Product", ""), CONF_NAME: discovery_info.hostname.split(".")[0], } diff --git a/tests/components/devolo_home_network/const.py b/tests/components/devolo_home_network/const.py index 0e48833a78b1af..cfacc3cd1fb71e 100644 --- a/tests/components/devolo_home_network/const.py +++ b/tests/components/devolo_home_network/const.py @@ -43,10 +43,21 @@ hostname="mock_hostname", name="mock_name", port=None, - properties={"MT": "2600"}, + properties={"MT": "2600", "SN": "1234567890"}, type="mock_type", ) +DISCOVERY_INFO_INCOMPLETE_QUERY = zeroconf.ZeroconfServiceInfo( + host="mock_host", + addresses=["mock_host"], + hostname="mock_hostname", + name="mock_name", + port=None, + properties={}, + type="mock_type", +) + + NEIGHBOR_ACCESS_POINTS = { "neighbor_aps": [ { diff --git a/tests/components/devolo_home_network/test_config_flow.py b/tests/components/devolo_home_network/test_config_flow.py index 7cd1ba5222c63f..6d3ee79c9c2d60 100644 --- a/tests/components/devolo_home_network/test_config_flow.py +++ b/tests/components/devolo_home_network/test_config_flow.py @@ -22,7 +22,12 @@ RESULT_TYPE_FORM, ) -from .const import DISCOVERY_INFO, DISCOVERY_INFO_WRONG_DEVICE, IP +from .const import ( + DISCOVERY_INFO, + DISCOVERY_INFO_INCOMPLETE_QUERY, + DISCOVERY_INFO_WRONG_DEVICE, + IP, +) async def test_form(hass: HomeAssistant, info: dict[str, Any]): @@ -118,6 +123,17 @@ async def test_zeroconf(hass: HomeAssistant): } +async def test_abort_zeroconf_incomplete_query(hass: HomeAssistant): + """Test we abort zeroconf on incomplete queries.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": config_entries.SOURCE_ZEROCONF}, + data=DISCOVERY_INFO_INCOMPLETE_QUERY, + ) + assert result["type"] == RESULT_TYPE_ABORT + assert result["reason"] == "incomplete_query" + + async def test_abort_zeroconf_wrong_device(hass: HomeAssistant): """Test we abort zeroconf for wrong devices.""" result = await hass.config_entries.flow.async_init( From 18f6ee35ef6dfd5128318b0f631bcad8ea77f2b5 Mon Sep 17 00:00:00 2001 From: Guido Schmitz Date: Wed, 4 May 2022 13:33:49 +0000 Subject: [PATCH 2/3] Change approach --- .../devolo_home_network/config_flow.py | 8 +------- .../devolo_home_network/manifest.json | 4 +++- homeassistant/generated/zeroconf.py | 5 ++++- tests/components/devolo_home_network/const.py | 10 ---------- .../devolo_home_network/test_config_flow.py | 18 +----------------- 5 files changed, 9 insertions(+), 36 deletions(-) diff --git a/homeassistant/components/devolo_home_network/config_flow.py b/homeassistant/components/devolo_home_network/config_flow.py index 3ba870d79d286f..c96126f43e2af3 100644 --- a/homeassistant/components/devolo_home_network/config_flow.py +++ b/homeassistant/components/devolo_home_network/config_flow.py @@ -78,12 +78,6 @@ async def async_step_zeroconf( self, discovery_info: zeroconf.ZeroconfServiceInfo ) -> FlowResult: """Handle zeroconf discovery.""" - if ( - "MT" not in discovery_info.properties - or "SN" not in discovery_info.properties - ): - return self.async_abort(reason="incomplete_query") - if discovery_info.properties["MT"] in ["2600", "2601"]: return self.async_abort(reason="home_control") @@ -92,7 +86,7 @@ async def async_step_zeroconf( self.context[CONF_HOST] = discovery_info.host self.context["title_placeholders"] = { - PRODUCT: discovery_info.properties.get("Product", ""), + PRODUCT: discovery_info.properties["Product"], CONF_NAME: discovery_info.hostname.split(".")[0], } diff --git a/homeassistant/components/devolo_home_network/manifest.json b/homeassistant/components/devolo_home_network/manifest.json index a514606a322250..445a383ea146a4 100644 --- a/homeassistant/components/devolo_home_network/manifest.json +++ b/homeassistant/components/devolo_home_network/manifest.json @@ -4,7 +4,9 @@ "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/devolo_home_network", "requirements": ["devolo-plc-api==0.7.1"], - "zeroconf": ["_dvl-deviceapi._tcp.local."], + "zeroconf": [ + { "type": "_dvl-deviceapi._tcp.local.", "properties": { "MT": "*" } } + ], "codeowners": ["@2Fake", "@Shutgun"], "quality_scale": "platinum", "iot_class": "local_polling", diff --git a/homeassistant/generated/zeroconf.py b/homeassistant/generated/zeroconf.py index 34e24e51fc99a3..b93d7249211f47 100644 --- a/homeassistant/generated/zeroconf.py +++ b/homeassistant/generated/zeroconf.py @@ -103,7 +103,10 @@ "domain": "devolo_home_control" }, { - "domain": "devolo_home_network" + "domain": "devolo_home_network", + "properties": { + "MT": "*" + } } ], "_easylink._tcp.local.": [ diff --git a/tests/components/devolo_home_network/const.py b/tests/components/devolo_home_network/const.py index cfacc3cd1fb71e..ae6be3275f41e0 100644 --- a/tests/components/devolo_home_network/const.py +++ b/tests/components/devolo_home_network/const.py @@ -47,16 +47,6 @@ type="mock_type", ) -DISCOVERY_INFO_INCOMPLETE_QUERY = zeroconf.ZeroconfServiceInfo( - host="mock_host", - addresses=["mock_host"], - hostname="mock_hostname", - name="mock_name", - port=None, - properties={}, - type="mock_type", -) - NEIGHBOR_ACCESS_POINTS = { "neighbor_aps": [ diff --git a/tests/components/devolo_home_network/test_config_flow.py b/tests/components/devolo_home_network/test_config_flow.py index 6d3ee79c9c2d60..7cd1ba5222c63f 100644 --- a/tests/components/devolo_home_network/test_config_flow.py +++ b/tests/components/devolo_home_network/test_config_flow.py @@ -22,12 +22,7 @@ RESULT_TYPE_FORM, ) -from .const import ( - DISCOVERY_INFO, - DISCOVERY_INFO_INCOMPLETE_QUERY, - DISCOVERY_INFO_WRONG_DEVICE, - IP, -) +from .const import DISCOVERY_INFO, DISCOVERY_INFO_WRONG_DEVICE, IP async def test_form(hass: HomeAssistant, info: dict[str, Any]): @@ -123,17 +118,6 @@ async def test_zeroconf(hass: HomeAssistant): } -async def test_abort_zeroconf_incomplete_query(hass: HomeAssistant): - """Test we abort zeroconf on incomplete queries.""" - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_ZEROCONF}, - data=DISCOVERY_INFO_INCOMPLETE_QUERY, - ) - assert result["type"] == RESULT_TYPE_ABORT - assert result["reason"] == "incomplete_query" - - async def test_abort_zeroconf_wrong_device(hass: HomeAssistant): """Test we abort zeroconf for wrong devices.""" result = await hass.config_entries.flow.async_init( From 6e6fb4f9fdef6cb31ff312d761b6dbfc98f0264b Mon Sep 17 00:00:00 2001 From: Guido Schmitz Date: Wed, 4 May 2022 13:36:31 +0000 Subject: [PATCH 3/3] Restore test data --- tests/components/devolo_home_network/const.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/components/devolo_home_network/const.py b/tests/components/devolo_home_network/const.py index ae6be3275f41e0..0e48833a78b1af 100644 --- a/tests/components/devolo_home_network/const.py +++ b/tests/components/devolo_home_network/const.py @@ -43,11 +43,10 @@ hostname="mock_hostname", name="mock_name", port=None, - properties={"MT": "2600", "SN": "1234567890"}, + properties={"MT": "2600"}, type="mock_type", ) - NEIGHBOR_ACCESS_POINTS = { "neighbor_aps": [ {