From d4fa625a7f0d42d5a38fc8c4888104d8c91733d0 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 26 Aug 2021 08:59:41 -0500 Subject: [PATCH] Defer zha auto configure probe until after clicking configure (#55239) --- homeassistant/components/zha/config_flow.py | 18 ++++++------- homeassistant/components/zha/strings.json | 3 ++- tests/components/zha/test_config_flow.py | 28 +++++++-------------- 3 files changed, 18 insertions(+), 31 deletions(-) diff --git a/homeassistant/components/zha/config_flow.py b/homeassistant/components/zha/config_flow.py index 61898328d2e..772362b3850 100644 --- a/homeassistant/components/zha/config_flow.py +++ b/homeassistant/components/zha/config_flow.py @@ -36,7 +36,6 @@ class ZhaFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): """Initialize flow instance.""" self._device_path = None self._radio_type = None - self._auto_detected_data = None self._title = None async def async_step_user(self, user_input=None): @@ -124,15 +123,6 @@ class ZhaFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): if flow["handler"] == "deconz": return self.async_abort(reason="not_zha_device") - # The Nortek sticks are a special case since they - # have a Z-Wave and a Zigbee radio. We need to reject - # the Z-Wave radio. - if vid == "10C4" and pid == "8A2A" and "ZigBee" not in description: - return self.async_abort(reason="not_zha_device") - - self._auto_detected_data = await detect_radios(dev_path) - if self._auto_detected_data is None: - return self.async_abort(reason="not_zha_device") self._device_path = dev_path self._title = usb.human_readable_device_name( dev_path, @@ -149,9 +139,15 @@ class ZhaFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): async def async_step_confirm(self, user_input=None): """Confirm a discovery.""" if user_input is not None: + auto_detected_data = await detect_radios(self._device_path) + if auto_detected_data is None: + # This probably will not happen how they have + # have very specific usb matching, but there could + # be a problem with the device + return self.async_abort(reason="usb_probe_failed") return self.async_create_entry( title=self._title, - data=self._auto_detected_data, + data=auto_detected_data, ) return self.async_show_form( diff --git a/homeassistant/components/zha/strings.json b/homeassistant/components/zha/strings.json index 4b5b429522f..5953df52e92 100644 --- a/homeassistant/components/zha/strings.json +++ b/homeassistant/components/zha/strings.json @@ -30,7 +30,8 @@ }, "abort": { "single_instance_allowed": "[%key:common::config_flow::abort::single_instance_allowed%]", - "not_zha_device": "This device is not a zha device" + "not_zha_device": "This device is not a zha device", + "usb_probe_failed": "Failed to probe the usb device" } }, "config_panel": { diff --git a/tests/components/zha/test_config_flow.py b/tests/components/zha/test_config_flow.py index 9f7e3baeaf1..281a0683eb8 100644 --- a/tests/components/zha/test_config_flow.py +++ b/tests/components/zha/test_config_flow.py @@ -164,27 +164,17 @@ async def test_discovery_via_usb_no_radio(detect_mock, hass): "zha", context={"source": SOURCE_USB}, data=discovery_info ) await hass.async_block_till_done() - assert result["type"] == RESULT_TYPE_ABORT - assert result["reason"] == "not_zha_device" + assert result["type"] == RESULT_TYPE_FORM + assert result["step_id"] == "confirm" + with patch("homeassistant.components.zha.async_setup_entry"): + result2 = await hass.config_entries.flow.async_configure( + result["flow_id"], user_input={} + ) + await hass.async_block_till_done() -@patch("zigpy_znp.zigbee.application.ControllerApplication.probe", return_value=True) -async def test_discovery_via_usb_rejects_nortek_zwave(detect_mock, hass): - """Test usb flow -- reject the nortek zwave radio.""" - discovery_info = { - "device": "/dev/null", - "vid": "10C4", - "pid": "8A2A", - "serial_number": "612020FD", - "description": "HubZ Smart Home Controller - HubZ Z-Wave Com Port", - "manufacturer": "Silicon Labs", - } - result = await hass.config_entries.flow.async_init( - "zha", context={"source": SOURCE_USB}, data=discovery_info - ) - await hass.async_block_till_done() - assert result["type"] == RESULT_TYPE_ABORT - assert result["reason"] == "not_zha_device" + assert result2["type"] == RESULT_TYPE_ABORT + assert result2["reason"] == "usb_probe_failed" @patch("zigpy_znp.zigbee.application.ControllerApplication.probe", return_value=True)