Use ConfigFlow.has_matching_flow to deduplicate flux_led flows (#126888)

This commit is contained in:
Erik Montnemery 2024-09-27 19:22:00 +02:00 committed by GitHub
parent 4edc3872ce
commit 4599d1650b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 6 deletions

View File

@ -3,7 +3,7 @@
from __future__ import annotations from __future__ import annotations
import contextlib import contextlib
from typing import Any, cast from typing import Any, Self, cast
from flux_led.const import ( from flux_led.const import (
ATTR_ID, ATTR_ID,
@ -61,6 +61,8 @@ class FluxLedConfigFlow(ConfigFlow, domain=DOMAIN):
VERSION = 1 VERSION = 1
host: str | None = None
def __init__(self) -> None: def __init__(self) -> None:
"""Initialize the config flow.""" """Initialize the config flow."""
self._discovered_devices: dict[str, FluxLEDDiscovery] = {} self._discovered_devices: dict[str, FluxLEDDiscovery] = {}
@ -149,9 +151,8 @@ class FluxLedConfigFlow(ConfigFlow, domain=DOMAIN):
assert device is not None assert device is not None
await self._async_set_discovered_mac(device, self._allow_update_mac) await self._async_set_discovered_mac(device, self._allow_update_mac)
host = device[ATTR_IPADDR] host = device[ATTR_IPADDR]
self.context[CONF_HOST] = host self.host = host
for progress in self._async_in_progress(): if self.hass.config_entries.flow.async_has_matching_flow(self):
if progress.get("context", {}).get(CONF_HOST) == host:
return self.async_abort(reason="already_in_progress") return self.async_abort(reason="already_in_progress")
if not device[ATTR_MODEL_DESCRIPTION]: if not device[ATTR_MODEL_DESCRIPTION]:
mac_address = device[ATTR_ID] mac_address = device[ATTR_ID]
@ -173,6 +174,10 @@ class FluxLedConfigFlow(ConfigFlow, domain=DOMAIN):
await self._async_set_discovered_mac(device, True) await self._async_set_discovered_mac(device, True)
return await self.async_step_discovery_confirm() 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( async def async_step_discovery_confirm(
self, user_input: dict[str, Any] | None = None self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult: ) -> ConfigFlowResult:

View File

@ -8,6 +8,7 @@ import pytest
from homeassistant import config_entries from homeassistant import config_entries
from homeassistant.components import dhcp from homeassistant.components import dhcp
from homeassistant.components.flux_led.config_flow import FluxLedConfigFlow
from homeassistant.components.flux_led.const import ( from homeassistant.components.flux_led.const import (
CONF_CUSTOM_EFFECT_COLORS, CONF_CUSTOM_EFFECT_COLORS,
CONF_CUSTOM_EFFECT_SPEED_PCT, 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["type"] is FlowResultType.ABORT
assert result2["reason"] == "already_in_progress" 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( result3 = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
context={"source": config_entries.SOURCE_DHCP}, 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() 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["type"] is FlowResultType.ABORT
assert result3["reason"] == "already_in_progress" assert result3["reason"] == "already_in_progress"