@@ -168,7 +168,10 @@ class MQTT:
168
168
in seconds.
169
169
:param int connect_retries: How many times to try to connect to the broker before giving up
170
170
on connect or reconnect. Exponential backoff will be used for the retries.
171
- :param class user_data: arbitrary data to pass as a second argument to the callbacks.
171
+ :param class user_data: arbitrary data to pass as a second argument to most of the callbacks.
172
+ This works with all callbacks but the "on_message" and those added via add_topic_callback();
173
+ for those, to get access to the user_data use the 'user_data' member of the MQTT object
174
+ passed as 1st argument.
172
175
:param bool use_imprecise_time: on boards without time.monotonic_ns() one has to set
173
176
this to True in order to operate correctly over more than 24 days or so
174
177
@@ -222,7 +225,7 @@ def __init__(
222
225
self ._recv_timeout = recv_timeout
223
226
224
227
self .keep_alive = keep_alive
225
- self ._user_data = user_data
228
+ self .user_data = user_data
226
229
self ._is_connected = False
227
230
self ._msg_size_lim = MQTT_MSG_SZ_LIM
228
231
self ._pid = 0
@@ -440,6 +443,11 @@ def add_topic_callback(self, mqtt_topic: str, callback_method) -> None:
440
443
441
444
:param str mqtt_topic: MQTT topic identifier.
442
445
:param function callback_method: The callback method.
446
+
447
+ Expected method signature is ``on_message(client, topic, message)``
448
+ To get access to the user_data, use the client argument.
449
+
450
+ If a callback is called for the topic, then any "on_message" callback will not be called.
443
451
"""
444
452
if mqtt_topic is None or callback_method is None :
445
453
raise ValueError ("MQTT topic and callback method must both be defined." )
@@ -464,6 +472,7 @@ def on_message(self):
464
472
"""Called when a new message has been received on a subscribed topic.
465
473
466
474
Expected method signature is ``on_message(client, topic, message)``
475
+ To get access to the user_data, use the client argument.
467
476
"""
468
477
return self ._on_message
469
478
@@ -665,7 +674,7 @@ def _connect(
665
674
self ._is_connected = True
666
675
result = rc [0 ] & 1
667
676
if self .on_connect is not None :
668
- self .on_connect (self , self ._user_data , result , rc [2 ])
677
+ self .on_connect (self , self .user_data , result , rc [2 ])
669
678
670
679
return result
671
680
@@ -688,7 +697,7 @@ def disconnect(self) -> None:
688
697
self ._is_connected = False
689
698
self ._subscribed_topics = []
690
699
if self .on_disconnect is not None :
691
- self .on_disconnect (self , self ._user_data , 0 )
700
+ self .on_disconnect (self , self .user_data , 0 )
692
701
693
702
def ping (self ) -> list [int ]:
694
703
"""Pings the MQTT Broker to confirm if the broker is alive or if
@@ -784,7 +793,7 @@ def publish(
784
793
self ._sock .send (pub_hdr_var )
785
794
self ._sock .send (msg )
786
795
if qos == 0 and self .on_publish is not None :
787
- self .on_publish (self , self ._user_data , topic , self ._pid )
796
+ self .on_publish (self , self .user_data , topic , self ._pid )
788
797
if qos == 1 :
789
798
stamp = self .get_monotonic_time ()
790
799
while True :
@@ -796,7 +805,7 @@ def publish(
796
805
rcv_pid = rcv_pid_buf [0 ] << 0x08 | rcv_pid_buf [1 ]
797
806
if self ._pid == rcv_pid :
798
807
if self .on_publish is not None :
799
- self .on_publish (self , self ._user_data , topic , rcv_pid )
808
+ self .on_publish (self , self .user_data , topic , rcv_pid )
800
809
return
801
810
802
811
if op is None :
@@ -876,7 +885,7 @@ def subscribe(self, topic: str, qos: int = 0) -> None:
876
885
877
886
for t , q in topics :
878
887
if self .on_subscribe is not None :
879
- self .on_subscribe (self , self ._user_data , t , q )
888
+ self .on_subscribe (self , self .user_data , t , q )
880
889
self ._subscribed_topics .append (t )
881
890
return
882
891
@@ -934,7 +943,7 @@ def unsubscribe(self, topic: str) -> None:
934
943
assert rc [1 ] == packet_id_bytes [0 ] and rc [2 ] == packet_id_bytes [1 ]
935
944
for t in topics :
936
945
if self .on_unsubscribe is not None :
937
- self .on_unsubscribe (self , self ._user_data , t , self ._pid )
946
+ self .on_unsubscribe (self , self .user_data , t , self ._pid )
938
947
self ._subscribed_topics .remove (t )
939
948
return
940
949
@@ -1013,7 +1022,7 @@ def loop(self, timeout: float = 0) -> Optional[list[int]]:
1013
1022
:param float timeout: return after this timeout, in seconds.
1014
1023
1015
1024
"""
1016
-
1025
+ self . _connected ()
1017
1026
self .logger .debug (f"waiting for messages for { timeout } seconds" )
1018
1027
if self ._timestamp == 0 :
1019
1028
self ._timestamp = self .get_monotonic_time ()
0 commit comments