Skip to content

Commit

Permalink
Use ConfigFlow.has_matching_flow to deduplicate yeelight flows (#127165)
Browse files Browse the repository at this point in the history
  • Loading branch information
emontnemery authored Oct 2, 2024
1 parent e3e68da commit 375d47e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
14 changes: 8 additions & 6 deletions homeassistant/components/yeelight/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -53,7 +53,7 @@ class YeelightConfigFlow(ConfigFlow, domain=DOMAIN):

VERSION = 1

_discovered_ip: str
_discovered_ip: str = ""
_discovered_model: str

@staticmethod
Expand Down Expand Up @@ -119,10 +119,8 @@ async def _async_handle_discovery_with_unique_id(self) -> ConfigFlowResult:

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:
Expand All @@ -140,6 +138,10 @@ async def _async_handle_discovery(self) -> ConfigFlowResult:
)
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:
Expand Down
18 changes: 17 additions & 1 deletion tests/components/yeelight/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

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,
Expand Down Expand Up @@ -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,
Expand All @@ -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(),
Expand Down

0 comments on commit 375d47e

Please sign in to comment.