From 716abaa9b1fc1b713b6d9fc8a1e8f50422ca1a54 Mon Sep 17 00:00:00 2001 From: Martin Hjelmare Date: Mon, 23 Aug 2021 13:50:08 +0200 Subject: [PATCH] Prefer discovered usb device over add-on config in zwave_js (#55056) --- .../components/zwave_js/config_flow.py | 2 +- tests/components/zwave_js/test_config_flow.py | 68 +++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/zwave_js/config_flow.py b/homeassistant/components/zwave_js/config_flow.py index 6b0fc7b692e..55266d02389 100644 --- a/homeassistant/components/zwave_js/config_flow.py +++ b/homeassistant/components/zwave_js/config_flow.py @@ -485,7 +485,7 @@ class ConfigFlow(BaseZwaveJSFlow, config_entries.ConfigFlow, domain=DOMAIN): return await self.async_step_start_addon() - usb_path = addon_config.get(CONF_ADDON_DEVICE) or self.usb_path or "" + usb_path = self.usb_path or addon_config.get(CONF_ADDON_DEVICE) or "" network_key = addon_config.get(CONF_ADDON_NETWORK_KEY, self.network_key or "") data_schema = vol.Schema( diff --git a/tests/components/zwave_js/test_config_flow.py b/tests/components/zwave_js/test_config_flow.py index 393de228d87..5e994a2ac7a 100644 --- a/tests/components/zwave_js/test_config_flow.py +++ b/tests/components/zwave_js/test_config_flow.py @@ -499,6 +499,74 @@ async def test_usb_discovery( assert len(mock_setup_entry.mock_calls) == 1 +async def test_usb_discovery_addon_not_running( + hass, + supervisor, + addon_installed, + addon_options, + set_addon_options, + start_addon, + get_addon_discovery_info, +): + """Test usb discovery when add-on is installed but not running.""" + addon_options["device"] = "/dev/incorrect_device" + + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": config_entries.SOURCE_USB}, + data=USB_DISCOVERY_INFO, + ) + assert result["type"] == "form" + assert result["step_id"] == "usb_confirm" + + result = await hass.config_entries.flow.async_configure(result["flow_id"], {}) + + assert result["type"] == "form" + assert result["step_id"] == "configure_addon" + + # Make sure the discovered usb device is preferred. + data_schema = result["data_schema"] + assert data_schema({}) == { + "usb_path": USB_DISCOVERY_INFO["device"], + "network_key": "", + } + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {"usb_path": USB_DISCOVERY_INFO["device"], "network_key": "abc123"}, + ) + + assert set_addon_options.call_args == call( + hass, + "core_zwave_js", + {"options": {"device": USB_DISCOVERY_INFO["device"], "network_key": "abc123"}}, + ) + + assert result["type"] == "progress" + assert result["step_id"] == "start_addon" + + with patch( + "homeassistant.components.zwave_js.async_setup", return_value=True + ) as mock_setup, patch( + "homeassistant.components.zwave_js.async_setup_entry", + return_value=True, + ) as mock_setup_entry: + await hass.async_block_till_done() + result = await hass.config_entries.flow.async_configure(result["flow_id"]) + await hass.async_block_till_done() + + assert start_addon.call_args == call(hass, "core_zwave_js") + + assert result["type"] == "create_entry" + assert result["title"] == TITLE + assert result["data"]["usb_path"] == USB_DISCOVERY_INFO["device"] + assert result["data"]["integration_created_addon"] is False + assert result["data"]["use_addon"] is True + assert result["data"]["network_key"] == "abc123" + assert len(mock_setup.mock_calls) == 1 + assert len(mock_setup_entry.mock_calls) == 1 + + async def test_discovery_addon_not_running( hass, supervisor, addon_installed, addon_options, set_addon_options, start_addon ):