From bfa919d078a5ac81c17763d8dc4c2980669d6367 Mon Sep 17 00:00:00 2001 From: Petar Petrov Date: Tue, 27 May 2025 13:53:30 +0300 Subject: [PATCH] Remove confirm screen after Z-Wave usb discovery (#145682) * Remove confirm screen after Z-Wave usb discovery * Simplify async_step_usb --- .../components/zwave_js/config_flow.py | 30 +----- .../components/zwave_js/strings.json | 3 - tests/components/zwave_js/test_config_flow.py | 91 +++---------------- 3 files changed, 14 insertions(+), 110 deletions(-) diff --git a/homeassistant/components/zwave_js/config_flow.py b/homeassistant/components/zwave_js/config_flow.py index 3e899da0538..e2941b52522 100644 --- a/homeassistant/components/zwave_js/config_flow.py +++ b/homeassistant/components/zwave_js/config_flow.py @@ -170,8 +170,6 @@ class ZWaveJSConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - _title: str - def __init__(self) -> None: """Set up flow instance.""" self.s0_legacy_key: str | None = None @@ -446,7 +444,7 @@ class ZWaveJSConfigFlow(ConfigFlow, domain=DOMAIN): # at least for a short time. return self.async_abort(reason="already_in_progress") if current_config_entries := self._async_current_entries(include_ignore=False): - config_entry = next( + self._reconfigure_config_entry = next( ( entry for entry in current_config_entries @@ -454,7 +452,7 @@ class ZWaveJSConfigFlow(ConfigFlow, domain=DOMAIN): ), None, ) - if not config_entry: + if not self._reconfigure_config_entry: return self.async_abort(reason="addon_required") vid = discovery_info.vid @@ -503,31 +501,9 @@ class ZWaveJSConfigFlow(ConfigFlow, domain=DOMAIN): ) title = human_name.split(" - ")[0].strip() self.context["title_placeholders"] = {CONF_NAME: title} - self._title = title - return await self.async_step_usb_confirm() - - async def async_step_usb_confirm( - self, user_input: dict[str, Any] | None = None - ) -> ConfigFlowResult: - """Handle USB Discovery confirmation.""" - if user_input is None: - return self.async_show_form( - step_id="usb_confirm", - description_placeholders={CONF_NAME: self._title}, - ) self._usb_discovery = True - if current_config_entries := self._async_current_entries(include_ignore=False): - self._reconfigure_config_entry = next( - ( - entry - for entry in current_config_entries - if entry.data.get(CONF_USE_ADDON) - ), - None, - ) - if not self._reconfigure_config_entry: - return self.async_abort(reason="addon_required") + if current_config_entries: return await self.async_step_intent_migrate() return await self.async_step_installation_type() diff --git a/homeassistant/components/zwave_js/strings.json b/homeassistant/components/zwave_js/strings.json index fbe43af1f6f..ee6efcb2fb6 100644 --- a/homeassistant/components/zwave_js/strings.json +++ b/homeassistant/components/zwave_js/strings.json @@ -98,9 +98,6 @@ "start_addon": { "title": "The Z-Wave add-on is starting." }, - "usb_confirm": { - "description": "Do you want to set up {name} with the Z-Wave add-on?" - }, "zeroconf_confirm": { "description": "Do you want to add the Z-Wave Server with home ID {home_id} found at {url} to Home Assistant?", "title": "Discovered Z-Wave Server" diff --git a/tests/components/zwave_js/test_config_flow.py b/tests/components/zwave_js/test_config_flow.py index bae8ae55034..c9929759a49 100644 --- a/tests/components/zwave_js/test_config_flow.py +++ b/tests/components/zwave_js/test_config_flow.py @@ -585,8 +585,8 @@ async def test_abort_hassio_discovery_with_existing_flow(hass: HomeAssistant) -> context={"source": config_entries.SOURCE_USB}, data=USB_DISCOVERY_INFO, ) - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "usb_confirm" + assert result["type"] is FlowResultType.MENU + assert result["step_id"] == "installation_type" result2 = await hass.config_entries.flow.async_init( DOMAIN, @@ -664,13 +664,8 @@ async def test_usb_discovery( context={"source": config_entries.SOURCE_USB}, data=usb_discovery_info, ) - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "usb_confirm" - assert result["description_placeholders"] == {"name": discovery_name} + assert mock_usb_serial_by_id.call_count == 1 - - result = await hass.config_entries.flow.async_configure(result["flow_id"], {}) - assert result["type"] is FlowResultType.MENU assert result["step_id"] == "installation_type" assert result["menu_options"] == ["intent_recommended", "intent_custom"] @@ -771,12 +766,8 @@ async def test_usb_discovery_addon_not_running( context={"source": config_entries.SOURCE_USB}, data=USB_DISCOVERY_INFO, ) - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "usb_confirm" + assert mock_usb_serial_by_id.call_count == 2 - - result = await hass.config_entries.flow.async_configure(result["flow_id"], {}) - assert result["type"] is FlowResultType.MENU assert result["step_id"] == "installation_type" @@ -932,12 +923,8 @@ async def test_usb_discovery_migration( context={"source": config_entries.SOURCE_USB}, data=USB_DISCOVERY_INFO, ) - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "usb_confirm" + assert mock_usb_serial_by_id.call_count == 2 - - result = await hass.config_entries.flow.async_configure(result["flow_id"], {}) - assert result["type"] is FlowResultType.FORM assert result["step_id"] == "intent_migrate" @@ -1063,12 +1050,8 @@ async def test_usb_discovery_migration_restore_driver_ready_timeout( context={"source": config_entries.SOURCE_USB}, data=USB_DISCOVERY_INFO, ) - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "usb_confirm" + assert mock_usb_serial_by_id.call_count == 2 - - result = await hass.config_entries.flow.async_configure(result["flow_id"], {}) - assert result["type"] is FlowResultType.FORM assert result["step_id"] == "intent_migrate" @@ -1366,16 +1349,16 @@ async def test_usb_discovery_with_existing_usb_flow(hass: HomeAssistant) -> None data=first_usb_info, ) - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "usb_confirm" + assert result["type"] is FlowResultType.MENU + assert result["step_id"] == "installation_type" result2 = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USB}, data=USB_DISCOVERY_INFO, ) - assert result2["type"] is FlowResultType.FORM - assert result2["step_id"] == "usb_confirm" + assert result2["type"] is FlowResultType.MENU + assert result2["step_id"] == "installation_type" usb_flows_in_progress = hass.config_entries.flow.async_progress_by_handler( DOMAIN, match_context={"source": config_entries.SOURCE_USB} @@ -1409,53 +1392,6 @@ async def test_abort_usb_discovery_addon_required(hass: HomeAssistant) -> None: assert result["reason"] == "addon_required" -@pytest.mark.usefixtures( - "supervisor", - "addon_running", -) -async def test_abort_usb_discovery_confirm_addon_required( - hass: HomeAssistant, - addon_options: dict[str, Any], - mock_usb_serial_by_id: MagicMock, -) -> None: - """Test usb discovery confirm aborted when existing entry not using add-on.""" - addon_options["device"] = "/dev/another_device" - entry = MockConfigEntry( - domain=DOMAIN, - data={ - "url": "ws://localhost:3000", - "usb_path": "/dev/another_device", - "use_addon": True, - }, - title=TITLE, - unique_id="1234", - ) - entry.add_to_hass(hass) - - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_USB}, - data=USB_DISCOVERY_INFO, - ) - - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "usb_confirm" - assert mock_usb_serial_by_id.call_count == 2 - - hass.config_entries.async_update_entry( - entry, - data={ - **entry.data, - "use_addon": False, - }, - ) - - result = await hass.config_entries.flow.async_configure(result["flow_id"], {}) - - assert result["type"] is FlowResultType.ABORT - assert result["reason"] == "addon_required" - - async def test_usb_discovery_requires_supervisor(hass: HomeAssistant) -> None: """Test usb discovery flow is aborted when there is no supervisor.""" result = await hass.config_entries.flow.async_init( @@ -4635,13 +4571,8 @@ async def test_recommended_usb_discovery( context={"source": config_entries.SOURCE_USB}, data=usb_discovery_info, ) - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "usb_confirm" - assert result["description_placeholders"] == {"name": discovery_name} + assert mock_usb_serial_by_id.call_count == 1 - - result = await hass.config_entries.flow.async_configure(result["flow_id"], {}) - assert result["type"] is FlowResultType.MENU assert result["step_id"] == "installation_type" assert result["menu_options"] == ["intent_recommended", "intent_custom"]