@@ -30,7 +30,7 @@ class DeviceInfo(TypedDict, total=False):
3030
3131
3232class MqttManager (Thread ):
33- things : dict [ Optional [DeviceInfo ], list [ Thing ]]
33+ things : list [ tuple [ Optional [DeviceInfo ], Thing ]]
3434 client : mqtt .Client
3535 node_id : str
3636 base_topic : str
@@ -100,20 +100,20 @@ def __init__(self, host='localhost', port=1883, username=None,
100100 else :
101101 self .name = self .node_id
102102
103- self .things = defaultdict ( list )
103+ self .things = list ( )
104104 self .unique_identifier = unique_identifier or self .get_mac ()
105105 self .device_info = self ._gen_device_info ()
106106
107107 logger .debug ("Initialization parameters: node_id=%s, base_topic=%s, discovery_prefix=%s, name=%s" ,
108108 self .node_id , self .base_topic , self .discovery_prefix , self .name )
109109
110110 def add_thing (self , thing : Thing , origin : Optional [DeviceInfo ] = None ):
111- self .things [ origin ] .append (thing )
111+ self .things .append (( origin , thing ) )
112112 thing .set_manager (self )
113113
114114 def add_things (self , things : list [Thing ], origin : Optional [DeviceInfo ] = None ):
115- self .things [origin ].extend (things )
116115 for thing in things :
116+ self .things .append ((origin , thing ))
117117 thing .set_manager (self )
118118
119119 def run (self ):
@@ -182,7 +182,7 @@ def on_connect(self, _, userdata, flags, rc):
182182
183183 logger .debug ("Device information for this manager: %s" , self .device_info )
184184
185- for origin , things in self .things . items () :
185+ for origin , thing in self .things :
186186 if origin is None :
187187 common_config = {
188188 "~" : self .base_topic ,
@@ -197,28 +197,27 @@ def on_connect(self, _, userdata, flags, rc):
197197 "via" : self .device_info ["identifiers" ][0 ]
198198 }
199199
200- for thing in things :
201- logger .info ("Publishing discovery message for: %r" , thing )
202-
203- # New dictionary with sensible defaults
204- config = common_config .copy ()
205- config ["unique_id" ] = f"{ self .unique_identifier } _{ thing .short_id } "
200+ logger .info ("Publishing discovery message for: %r" , thing )
201+
202+ # New dictionary with sensible defaults
203+ config = common_config .copy ()
204+ config ["unique_id" ] = f"{ self .unique_identifier } _{ thing .short_id } "
206205
207- # Then call get_config, and allow the implementation to override
208- # the previously set defaults (at their own risk)
209- config .update (thing .get_config ())
206+ # Then call get_config, and allow the implementation to override
207+ # the previously set defaults (at their own risk)
208+ config .update (thing .get_config ())
210209
211- config_topic = "%s/%s/%s/%s/config" % (
212- self .discovery_prefix ,
213- thing .component ,
214- self .node_id ,
215- thing .short_id
216- )
210+ config_topic = "%s/%s/%s/%s/config" % (
211+ self .discovery_prefix ,
212+ thing .component ,
213+ self .node_id ,
214+ thing .short_id
215+ )
217216
218- logger .debug ("Sending the following config dict to %s:\n %s" , config_topic , config )
217+ logger .debug ("Sending the following config dict to %s:\n %s" , config_topic , config )
219218
220- self .client .publish (config_topic , json .dumps (config ), retain = True )
221- thing .set_callbacks ()
219+ self .client .publish (config_topic , json .dumps (config ), retain = True )
220+ thing .set_callbacks ()
222221
223222 # Set up availability topic
224223 ###########################
0 commit comments