@@ -164,20 +164,20 @@ def test_connection(self) -> Optional[int]:
164164 """
165165 try :
166166 self .__connect ()
167- self .disconnect ()
167+ self .disconnect ()
168168 except Exception as e :
169169 if isinstance (e , ConnectionRefusedError ):
170170 self .__connection_error = 3 # cannot connect
171171 else :
172172 self .__connection_error = 6 # unknown
173-
173+
174174 return self .__connection_error
175175
176176 def subscribe_listener (self , topic : str , unique_id : str , fnc : Callable [[Any ], Any ]) -> None :
177177 """Append new item into the datachange listener."""
178178 #if topic not in self.__listeners:
179179 # self.__listeners[topic] = dict[str, Callable[[Any], Any]]()
180-
180+
181181 stripped_topic = "/" .join (topic .split ("/" )[2 :])
182182 self .__listeners [stripped_topic ][unique_id ] = fnc
183183
@@ -244,7 +244,7 @@ def __on_connect(
244244 else :
245245 self .__is_available = False
246246 self .__connection_error = reason_code
247-
247+
248248 _LOGGER .info (
249249 "Mqtt broker %s:%s %s" ,
250250 self .__host ,
@@ -399,7 +399,7 @@ def __on_discover(
399399 """Special callback function used only in discover_all function
400400 placed in on_message. It is the same as on_message callback func,
401401 but does different things
402-
402+
403403 Args:
404404 client (MqttClient): Mqtt broker instance
405405 msg (object): Topic with payload from broker
@@ -434,7 +434,7 @@ def __on_discover(
434434 self .__last_values [msg .topic ] = copy .copy (msg .topic )
435435 self .__is_subscribed_list [msg .topic ] = True
436436 _LOGGER .info ("Device of type %s found [gw].\n " , device_type )
437-
437+
438438 def __on_message (
439439 self ,
440440 client : mqtt .Client , # pylint: disable=unused-argument
@@ -463,15 +463,26 @@ def __on_message(
463463 )
464464 self .__messages [msg .topic ] = msg .payload
465465
466+ if device_type == "gw" and message_type == "connected" :
467+ mac = message_parts [2 ]
468+ for stripped_topic in self .__listeners :
469+ if stripped_topic .startswith (mac ):
470+ self .__notify_listeners (stripped_topic , True )
471+ return
472+
466473 stripped_topic = "/" .join (message_parts [2 :])
467474
468475 is_connected_message = message_type == "connected"
469476
470477 if len (self .__listeners ) > 0 and stripped_topic in self .__listeners :
471478 # This pass data change directely into the device.
472- if len (self .__listeners [stripped_topic ]) > 0 :
473- for unique_id in list (self .__listeners [stripped_topic ]): #prevents the dictionary increased in size during iteration exception
474- self .__listeners [stripped_topic ][unique_id ](is_connected_message )
479+ self .__notify_listeners (stripped_topic , is_connected_message )
480+
481+ def __notify_listeners (self , stripped_topic : str , is_connected_message : bool ) -> None :
482+ """Notify listeners for a specific topic."""
483+ if len (self .__listeners [stripped_topic ]) > 0 :
484+ for unique_id in list (self .__listeners [stripped_topic ]): #prevents the dictionary increased in size during iteration exception
485+ self .__listeners [stripped_topic ][unique_id ](is_connected_message )
475486
476487 def __on_subscribe (
477488 self ,
0 commit comments