Use ConfigFlow.has_matching_flow to deduplicate fritzbox flows (#126891)

This commit is contained in:
Erik Montnemery 2024-09-27 11:44:59 +02:00 committed by GitHub
parent d7fe7f35ad
commit 8bdd909351
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -4,7 +4,7 @@ from __future__ import annotations
from collections.abc import Mapping from collections.abc import Mapping
import ipaddress import ipaddress
from typing import Any from typing import Any, Self
from urllib.parse import urlparse from urllib.parse import urlparse
from pyfritzhome import Fritzhome, LoginError from pyfritzhome import Fritzhome, LoginError
@ -122,7 +122,6 @@ class FritzboxConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a flow initialized by discovery.""" """Handle a flow initialized by discovery."""
host = urlparse(discovery_info.ssdp_location).hostname host = urlparse(discovery_info.ssdp_location).hostname
assert isinstance(host, str) assert isinstance(host, str)
self.context[CONF_HOST] = host
if ( if (
ipaddress.ip_address(host).version == 6 ipaddress.ip_address(host).version == 6
@ -136,9 +135,9 @@ class FritzboxConfigFlow(ConfigFlow, domain=DOMAIN):
await self.async_set_unique_id(uuid) await self.async_set_unique_id(uuid)
self._abort_if_unique_id_configured({CONF_HOST: host}) self._abort_if_unique_id_configured({CONF_HOST: host})
for progress in self._async_in_progress(): self._host = host
if progress.get("context", {}).get(CONF_HOST) == host: if self.hass.config_entries.flow.async_has_matching_flow(self):
return self.async_abort(reason="already_in_progress") return self.async_abort(reason="already_in_progress")
# update old and user-configured config entries # update old and user-configured config entries
for entry in self._async_current_entries(include_ignore=False): for entry in self._async_current_entries(include_ignore=False):
@ -147,12 +146,15 @@ class FritzboxConfigFlow(ConfigFlow, domain=DOMAIN):
self.hass.config_entries.async_update_entry(entry, unique_id=uuid) self.hass.config_entries.async_update_entry(entry, unique_id=uuid)
return self.async_abort(reason="already_configured") return self.async_abort(reason="already_configured")
self._host = host
self._name = str(discovery_info.upnp.get(ssdp.ATTR_UPNP_FRIENDLY_NAME) or host) self._name = str(discovery_info.upnp.get(ssdp.ATTR_UPNP_FRIENDLY_NAME) or host)
self.context["title_placeholders"] = {"name": self._name} self.context["title_placeholders"] = {"name": self._name}
return await self.async_step_confirm() return await self.async_step_confirm()
def is_matching(self, other_flow: Self) -> bool:
"""Return True if other_flow is matching this flow."""
return other_flow._host == self._host # noqa: SLF001
async def async_step_confirm( async def async_step_confirm(
self, user_input: dict[str, Any] | None = None self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult: ) -> ConfigFlowResult: