Defer zha auto configure probe until after clicking configure (#55239)

This commit is contained in:
J. Nick Koston 2021-08-26 08:59:41 -05:00 committed by GitHub
parent a89057ece5
commit d4fa625a7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 31 deletions

View File

@ -36,7 +36,6 @@ class ZhaFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Initialize flow instance.""" """Initialize flow instance."""
self._device_path = None self._device_path = None
self._radio_type = None self._radio_type = None
self._auto_detected_data = None
self._title = None self._title = None
async def async_step_user(self, user_input=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": if flow["handler"] == "deconz":
return self.async_abort(reason="not_zha_device") 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._device_path = dev_path
self._title = usb.human_readable_device_name( self._title = usb.human_readable_device_name(
dev_path, dev_path,
@ -149,9 +139,15 @@ class ZhaFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
async def async_step_confirm(self, user_input=None): async def async_step_confirm(self, user_input=None):
"""Confirm a discovery.""" """Confirm a discovery."""
if user_input is not None: 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( return self.async_create_entry(
title=self._title, title=self._title,
data=self._auto_detected_data, data=auto_detected_data,
) )
return self.async_show_form( return self.async_show_form(

View File

@ -30,7 +30,8 @@
}, },
"abort": { "abort": {
"single_instance_allowed": "[%key:common::config_flow::abort::single_instance_allowed%]", "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": { "config_panel": {

View File

@ -164,27 +164,17 @@ async def test_discovery_via_usb_no_radio(detect_mock, hass):
"zha", context={"source": SOURCE_USB}, data=discovery_info "zha", context={"source": SOURCE_USB}, data=discovery_info
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert result["type"] == RESULT_TYPE_ABORT assert result["type"] == RESULT_TYPE_FORM
assert result["reason"] == "not_zha_device" 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) assert result2["type"] == RESULT_TYPE_ABORT
async def test_discovery_via_usb_rejects_nortek_zwave(detect_mock, hass): assert result2["reason"] == "usb_probe_failed"
"""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"
@patch("zigpy_znp.zigbee.application.ControllerApplication.probe", return_value=True) @patch("zigpy_znp.zigbee.application.ControllerApplication.probe", return_value=True)