mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Use ConfigFlow.has_matching_flow to deduplicate yeelight flows (#127165)
This commit is contained in:
parent
e3e68dad36
commit
375d47ee3a
@ -3,7 +3,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any
|
||||
from typing import Any, Self
|
||||
from urllib.parse import urlparse
|
||||
|
||||
import voluptuous as vol
|
||||
@ -53,7 +53,7 @@ class YeelightConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
|
||||
VERSION = 1
|
||||
|
||||
_discovered_ip: str
|
||||
_discovered_ip: str = ""
|
||||
_discovered_model: str
|
||||
|
||||
@staticmethod
|
||||
@ -119,10 +119,8 @@ class YeelightConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
|
||||
async def _async_handle_discovery(self) -> ConfigFlowResult:
|
||||
"""Handle any discovery."""
|
||||
self.context[CONF_HOST] = self._discovered_ip
|
||||
for progress in self._async_in_progress():
|
||||
if progress.get("context", {}).get(CONF_HOST) == self._discovered_ip:
|
||||
return self.async_abort(reason="already_in_progress")
|
||||
if self.hass.config_entries.flow.async_has_matching_flow(self):
|
||||
return self.async_abort(reason="already_in_progress")
|
||||
self._async_abort_entries_match({CONF_HOST: self._discovered_ip})
|
||||
|
||||
try:
|
||||
@ -140,6 +138,10 @@ class YeelightConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
)
|
||||
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._discovered_ip == self._discovered_ip # noqa: SLF001
|
||||
|
||||
async def async_step_discovery_confirm(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> ConfigFlowResult:
|
||||
|
@ -7,7 +7,11 @@ import pytest
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components import dhcp, ssdp, zeroconf
|
||||
from homeassistant.components.yeelight.config_flow import MODEL_UNKNOWN, CannotConnect
|
||||
from homeassistant.components.yeelight.config_flow import (
|
||||
MODEL_UNKNOWN,
|
||||
CannotConnect,
|
||||
YeelightConfigFlow,
|
||||
)
|
||||
from homeassistant.components.yeelight.const import (
|
||||
CONF_DETECTED_MODEL,
|
||||
CONF_MODE_MUSIC,
|
||||
@ -503,10 +507,20 @@ async def test_discovered_by_homekit_and_dhcp(hass: HomeAssistant) -> None:
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] is None
|
||||
|
||||
real_is_matching = YeelightConfigFlow.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_discovery_interval(),
|
||||
patch(f"{MODULE_CONFIG_FLOW}.AsyncBulb", return_value=mocked_bulb),
|
||||
patch.object(
|
||||
YeelightConfigFlow, "is_matching", wraps=is_matching, autospec=True
|
||||
),
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
@ -518,6 +532,8 @@ async def test_discovered_by_homekit_and_dhcp(hass: HomeAssistant) -> None:
|
||||
await hass.async_block_till_done()
|
||||
assert result2["type"] is FlowResultType.ABORT
|
||||
assert result2["reason"] == "already_in_progress"
|
||||
# Ensure the is_matching method returned True
|
||||
assert return_values == [True]
|
||||
|
||||
with (
|
||||
_patch_discovery(),
|
||||
|
Loading…
x
Reference in New Issue
Block a user