Use ConfigFlow.has_matching_flow to deduplicate plugwise flows (#126896)

This commit is contained in:
Erik Montnemery 2024-09-27 14:32:36 +02:00 committed by GitHub
parent b3b5d9602a
commit 9f2ba6bc2c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 26 deletions

View File

@ -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:

View File

@ -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"