diff --git a/homeassistant/components/plugwise/config_flow.py b/homeassistant/components/plugwise/config_flow.py index 1e0f34007c9..846b063e1e8 100644 --- a/homeassistant/components/plugwise/config_flow.py +++ b/homeassistant/components/plugwise/config_flow.py @@ -2,7 +2,7 @@ from __future__ import annotations -from typing import Any +from typing import Any, Self from plugwise import Smile from plugwise.exceptions import ( @@ -85,6 +85,7 @@ class PlugwiseConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 discovery_info: ZeroconfServiceInfo | None = None + product: str | None = None _username: str = DEFAULT_USERNAME async def async_step_zeroconf( @@ -118,7 +119,7 @@ class PlugwiseConfigFlow(ConfigFlow, domain=DOMAIN): if DEFAULT_USERNAME not in unique_id: self._username = STRETCH_USERNAME - _product = _properties.get("product", None) + self.product = _product = _properties.get("product", None) _version = _properties.get("version", "n/a") _name = f"{ZEROCONF_MAP.get(_product, _product)} v{_version}" @@ -130,23 +131,8 @@ class PlugwiseConfigFlow(ConfigFlow, domain=DOMAIN): # If we have discovered an Adam or Anna, both might be on the network. # In that case, we need to cancel the Anna flow, as the Adam should # be added. - for flow in self._async_in_progress(): - # This is an Anna, and there is already an Adam flow in progress - if ( - _product == "smile_thermo" - and "context" in flow - and flow["context"].get("product") == "smile_open_therm" - ): - return self.async_abort(reason="anna_with_adam") - - # This is an Adam, and there is already an Anna flow in progress - if ( - _product == "smile_open_therm" - and "context" in flow - and flow["context"].get("product") == "smile_thermo" - and "flow_id" in flow - ): - self.hass.config_entries.flow.async_abort(flow["flow_id"]) + if self.hass.config_entries.flow.async_has_matching_flow(self): + return self.async_abort(reason="anna_with_adam") self.context.update( { @@ -159,11 +145,22 @@ class PlugwiseConfigFlow(ConfigFlow, domain=DOMAIN): "configuration_url": ( f"http://{discovery_info.host}:{discovery_info.port}" ), - "product": _product, } ) return await self.async_step_user() + def is_matching(self, other_flow: Self) -> bool: + """Return True if other_flow is matching this flow.""" + # This is an Anna, and there is already an Adam flow in progress + if self.product == "smile_thermo" and other_flow.product == "smile_open_therm": + return True + + # This is an Adam, and there is already an Anna flow in progress + if self.product == "smile_open_therm" and other_flow.product == "smile_thermo": + self.hass.config_entries.flow.async_abort(other_flow.flow_id) + + return False + async def async_step_user( self, user_input: dict[str, Any] | None = None ) -> ConfigFlowResult: diff --git a/tests/components/plugwise/test_config_flow.py b/tests/components/plugwise/test_config_flow.py index 44a5b5409ed..e0f9d6bb38c 100644 --- a/tests/components/plugwise/test_config_flow.py +++ b/tests/components/plugwise/test_config_flow.py @@ -340,9 +340,9 @@ async def test_zeroconf_abort_anna_with_adam(hass: HomeAssistant) -> None: assert result.get("type") is FlowResultType.FORM assert result.get("step_id") == "user" - flows_in_progress = hass.config_entries.flow.async_progress() + flows_in_progress = hass.config_entries.flow._handler_progress_index[DOMAIN] assert len(flows_in_progress) == 1 - assert flows_in_progress[0]["context"]["product"] == "smile_thermo" + assert list(flows_in_progress)[0].product == "smile_thermo" # Discover Adam, Anna should be aborted and no longer present result2 = await hass.config_entries.flow.async_init( @@ -354,9 +354,9 @@ async def test_zeroconf_abort_anna_with_adam(hass: HomeAssistant) -> None: assert result2.get("type") is FlowResultType.FORM assert result2.get("step_id") == "user" - flows_in_progress = hass.config_entries.flow.async_progress() + flows_in_progress = hass.config_entries.flow._handler_progress_index[DOMAIN] assert len(flows_in_progress) == 1 - assert flows_in_progress[0]["context"]["product"] == "smile_open_therm" + assert list(flows_in_progress)[0].product == "smile_open_therm" # Discover Anna again, Anna should be aborted directly result3 = await hass.config_entries.flow.async_init( @@ -368,6 +368,6 @@ async def test_zeroconf_abort_anna_with_adam(hass: HomeAssistant) -> None: assert result3.get("reason") == "anna_with_adam" # Adam should still be there - flows_in_progress = hass.config_entries.flow.async_progress() + flows_in_progress = hass.config_entries.flow._handler_progress_index[DOMAIN] assert len(flows_in_progress) == 1 - assert flows_in_progress[0]["context"]["product"] == "smile_open_therm" + assert list(flows_in_progress)[0].product == "smile_open_therm"