From 4712c5438f400fb6a761acf1ffdc94d6c276d12d Mon Sep 17 00:00:00 2001 From: Duco Sebel <74970928+DCSBL@users.noreply.github.com> Date: Fri, 21 Aug 2026 21:21:08 +0200 Subject: [PATCH] Fix HomeWizard setup flow giving error already_in_progress when trying to manually setup already discovered device (#177458) --- .../components/homewizard/config_flow.py | 8 ++-- .../components/homewizard/strings.json | 1 + .../components/homewizard/test_config_flow.py | 43 +++++++++++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/homewizard/config_flow.py b/homeassistant/components/homewizard/config_flow.py index 3d397e26014a..627073d47b04 100644 --- a/homeassistant/components/homewizard/config_flow.py +++ b/homeassistant/components/homewizard/config_flow.py @@ -19,7 +19,7 @@ from homewizard_energy.models import Device import voluptuous as vol from homeassistant.components import onboarding -from homeassistant.config_entries import ConfigFlow, ConfigFlowResult +from homeassistant.config_entries import SOURCE_USER, ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_IP_ADDRESS, CONF_TOKEN from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import AbortFlow @@ -60,7 +60,8 @@ class HomeWizardConfigFlow(ConfigFlow, domain=DOMAIN): return await self.async_step_authorize() else: await self.async_set_unique_id( - f"{device_info.product_type}_{device_info.serial}" + f"{device_info.product_type}_{device_info.serial}", + raise_on_progress=False, ) self._abort_if_unique_id_configured(updates=user_input) return self.async_create_entry( @@ -110,7 +111,8 @@ class HomeWizardConfigFlow(ConfigFlow, domain=DOMAIN): } await self.async_set_unique_id( - f"{device_info.product_type}_{device_info.serial}" + f"{device_info.product_type}_{device_info.serial}", + raise_on_progress=self.source != SOURCE_USER, ) self._abort_if_unique_id_configured(updates=data) return self.async_create_entry( diff --git a/homeassistant/components/homewizard/strings.json b/homeassistant/components/homewizard/strings.json index 475bff8640fc..bbc6521a5f34 100644 --- a/homeassistant/components/homewizard/strings.json +++ b/homeassistant/components/homewizard/strings.json @@ -2,6 +2,7 @@ "config": { "abort": { "already_configured": "[%key:common::config_flow::abort::already_configured_device%]", + "already_in_progress": "[%key:common::config_flow::abort::already_in_progress%]", "device_not_supported": "This device is not supported", "invalid_discovery_parameters": "Invalid discovery parameters", "reauth_enable_api_successful": "Enabling API was successful", diff --git a/tests/components/homewizard/test_config_flow.py b/tests/components/homewizard/test_config_flow.py index 09f10b8e9edb..e02a53901e23 100644 --- a/tests/components/homewizard/test_config_flow.py +++ b/tests/components/homewizard/test_config_flow.py @@ -379,6 +379,49 @@ async def test_discovery_flow_updates_new_ip( assert mock_config_entry.data[CONF_IP_ADDRESS] == "1.0.0.127" +@pytest.mark.usefixtures("mock_homewizardenergy", "mock_setup_entry") +async def test_manual_flow_ignores_pending_discovery_for_same_device( + hass: HomeAssistant, +) -> None: + """Test the user flow is not blocked by a stale discovery flow for the same device.""" + discovery_result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": config_entries.SOURCE_ZEROCONF}, + data=ZeroconfServiceInfo( + ip_address=ip_address("1.0.0.127"), + ip_addresses=[ip_address("1.0.0.127")], + port=80, + hostname="p1meter-ddeeff.local.", + type="", + name="", + properties={ + "api_enabled": "1", + "path": "/api/v1", + "product_name": "P1 Meter", + "product_type": "HWE-P1", + "serial": "5c2fafabcdef", + }, + ), + ) + + assert discovery_result["type"] is FlowResultType.FORM + assert discovery_result["step_id"] == "discovery_confirm" + assert len(hass.config_entries.flow.async_progress()) == 1 + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER} + ) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], {CONF_IP_ADDRESS: "2.2.2.2"} + ) + + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["data"][CONF_IP_ADDRESS] == "2.2.2.2" + + # The stale discovery flow is cleaned up once the manual flow succeeds + assert len(hass.config_entries.flow.async_progress()) == 0 + + @pytest.mark.usefixtures("mock_setup_entry") @pytest.mark.parametrize( ("exception", "reason"),