diff --git a/homeassistant/components/flux_led/config_flow.py b/homeassistant/components/flux_led/config_flow.py index 469c67deb22..63e8655f57c 100644 --- a/homeassistant/components/flux_led/config_flow.py +++ b/homeassistant/components/flux_led/config_flow.py @@ -3,7 +3,7 @@ from __future__ import annotations import contextlib -from typing import Any, cast +from typing import Any, Self, cast from flux_led.const import ( ATTR_ID, @@ -61,6 +61,8 @@ class FluxLedConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 + host: str | None = None + def __init__(self) -> None: """Initialize the config flow.""" self._discovered_devices: dict[str, FluxLEDDiscovery] = {} @@ -149,10 +151,9 @@ class FluxLedConfigFlow(ConfigFlow, domain=DOMAIN): assert device is not None await self._async_set_discovered_mac(device, self._allow_update_mac) host = device[ATTR_IPADDR] - self.context[CONF_HOST] = host - for progress in self._async_in_progress(): - if progress.get("context", {}).get(CONF_HOST) == host: - return self.async_abort(reason="already_in_progress") + self.host = host + if self.hass.config_entries.flow.async_has_matching_flow(self): + return self.async_abort(reason="already_in_progress") if not device[ATTR_MODEL_DESCRIPTION]: mac_address = device[ATTR_ID] assert mac_address is not None @@ -173,6 +174,10 @@ class FluxLedConfigFlow(ConfigFlow, domain=DOMAIN): await self._async_set_discovered_mac(device, True) return await self.async_step_discovery_confirm() + def is_matching(self, other_flow: Self) -> bool: + """Return True if other_flow is matching this flow.""" + return other_flow.host == self.host + async def async_step_discovery_confirm( self, user_input: dict[str, Any] | None = None ) -> ConfigFlowResult: diff --git a/tests/components/flux_led/test_config_flow.py b/tests/components/flux_led/test_config_flow.py index d95bc99f097..4332cb69f02 100644 --- a/tests/components/flux_led/test_config_flow.py +++ b/tests/components/flux_led/test_config_flow.py @@ -8,6 +8,7 @@ import pytest from homeassistant import config_entries from homeassistant.components import dhcp +from homeassistant.components.flux_led.config_flow import FluxLedConfigFlow from homeassistant.components.flux_led.const import ( CONF_CUSTOM_EFFECT_COLORS, CONF_CUSTOM_EFFECT_SPEED_PCT, @@ -406,7 +407,20 @@ async def test_discovered_by_discovery_and_dhcp(hass: HomeAssistant) -> None: assert result2["type"] is FlowResultType.ABORT assert result2["reason"] == "already_in_progress" - with _patch_discovery(), _patch_wifibulb(): + real_is_matching = FluxLedConfigFlow.is_matching + return_values = [] + + def is_matching(self, other_flow) -> bool: + return_values.append(real_is_matching(self, other_flow)) + return return_values[-1] + + with ( + _patch_discovery(), + _patch_wifibulb(), + patch.object( + FluxLedConfigFlow, "is_matching", wraps=is_matching, autospec=True + ), + ): result3 = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_DHCP}, @@ -417,6 +431,10 @@ async def test_discovered_by_discovery_and_dhcp(hass: HomeAssistant) -> None: ), ) await hass.async_block_till_done() + + # Ensure the is_matching method returned True + assert return_values == [True] + assert result3["type"] is FlowResultType.ABORT assert result3["reason"] == "already_in_progress"