22
33from __future__ import annotations
44
5- from typing import Any
5+ from typing import Any , Self
66
77from plugwise import Smile
88from plugwise .exceptions import (
4646 ANNA_WITH_ADAM ,
4747 CONF_HOMEKIT_EMULATION , # pw-beta option
4848 CONF_REFRESH_INTERVAL , # pw-beta option
49- CONTEXT ,
5049 DEFAULT_PORT ,
5150 DEFAULT_SCAN_INTERVAL , # pw-beta option
5251 DEFAULT_USERNAME ,
5352 DOMAIN ,
54- FLOW_ID ,
5553 FLOW_SMILE ,
5654 FLOW_STRETCH ,
5755 INIT ,
58- PRODUCT ,
5956 SMILE ,
6057 SMILE_OPEN_THERM ,
6158 SMILE_THERMO ,
@@ -130,6 +127,7 @@ class PlugwiseConfigFlow(ConfigFlow, domain=DOMAIN):
130127 MINOR_VERSION = 1
131128
132129 discovery_info : ZeroconfServiceInfo | None = None
130+ product : str = "Unknown Smile"
133131 _username : str = DEFAULT_USERNAME
134132
135133 async def async_step_zeroconf (
@@ -138,8 +136,8 @@ async def async_step_zeroconf(
138136 """Prepare configuration for a discovered Plugwise Smile."""
139137 self .discovery_info = discovery_info
140138 _properties = discovery_info .properties
141- _product = _properties .get (PRODUCT , "Unknown Smile" )
142139 _version = _properties .get (VERSION , "n/a" )
140+ self .product = _product = _properties .get ("product" , "Unknown Smile" )
143141 unique_id = discovery_info .hostname .split ("." )[0 ].split ("-" )[0 ]
144142 if DEFAULT_USERNAME not in unique_id :
145143 self ._username = STRETCH_USERNAME
@@ -173,36 +171,32 @@ async def async_step_zeroconf(
173171 # If we have discovered an Adam or Anna, both might be on the network.
174172 # In that case, we need to cancel the Anna flow, as the Adam should
175173 # be added.
176- for flow in self ._async_in_progress ():
177- # This is an Anna, and there is already an Adam flow in progress
178- if (
179- _product == SMILE_THERMO
180- and CONTEXT in flow
181- and flow [CONTEXT ].get (PRODUCT ) == SMILE_OPEN_THERM
182- ):
183- return self .async_abort (reason = ANNA_WITH_ADAM )
184-
185- # This is an Adam, and there is already an Anna flow in progress
186- if (
187- _product == SMILE_OPEN_THERM
188- and CONTEXT in flow
189- and flow [CONTEXT ].get (PRODUCT ) == SMILE_THERMO
190- and FLOW_ID in flow
191- ):
192- self .hass .config_entries .flow .async_abort (flow [FLOW_ID ])
174+ if self .hass .config_entries .flow .async_has_matching_flow (self ):
175+ return self .async_abort (reason = "anna_with_adam" )
193176
194177 _name = f"{ ZEROCONF_MAP .get (_product , _product )} v{ _version } "
195178 self .context .update (
196179 {
197180 TITLE_PLACEHOLDERS : {CONF_NAME : _name },
198181 ATTR_CONFIGURATION_URL : (
199182 f"http://{ discovery_info .host } :{ discovery_info .port } "
200- ),
201- PRODUCT : _product ,
183+ )
202184 }
203185 )
204186 return await self .async_step_user ()
205187
188+ def is_matching (self , other_flow : Self ) -> bool :
189+ """Return True if other_flow is matching this flow."""
190+ # This is an Anna, and there is already an Adam flow in progress
191+ if self .product == SMILE_THERMO and other_flow .product == SMILE_OPEN_THERM :
192+ return True
193+
194+ # This is an Adam, and there is already an Anna flow in progress
195+ if self .product == SMILE_OPEN_THERM and other_flow .product == SMILE_THERMO :
196+ self .hass .config_entries .flow .async_abort (other_flow .flow_id )
197+
198+ return False
199+
206200 async def async_step_user (
207201 self , user_input : dict [str , Any ] | None = None
208202 ) -> ConfigFlowResult :
0 commit comments