@@ -59,15 +59,15 @@ class IO_MQTT:
59
59
60
60
# pylint: disable=protected-access
61
61
def __init__ (self , mqtt_client ):
62
- # MiniMQTT Object
62
+ # Check for MiniMQTT client
63
63
mqtt_client_type = str (type (mqtt_client ))
64
64
if "MQTT" in mqtt_client_type :
65
65
self ._client = mqtt_client
66
66
else :
67
67
raise TypeError (
68
68
"This class requires a MiniMQTT client object, please create one."
69
69
)
70
- # Adafruit IO Auth. requires a username
70
+ # MiniMQTT's username kwarg is optional, IO requires a username
71
71
try :
72
72
self ._user = self ._client ._user
73
73
except :
@@ -85,11 +85,18 @@ def __init__(self, mqtt_client):
85
85
self ._client .on_disconnect = self ._on_disconnect_mqtt
86
86
self ._client .on_message = self ._on_message_mqtt
87
87
self ._logger = False
88
+ # Write to the MiniMQTT logger, if avaliable.
88
89
if self ._client ._logger is not None :
89
90
self ._logger = True
90
91
self ._client .set_logger_level ("DEBUG" )
91
92
self ._connected = False
92
93
94
+ def __enter__ (self ):
95
+ return self
96
+
97
+ def __exit__ (self , exception_type , exception_value , traceback ):
98
+ self .disconnect ()
99
+
93
100
def connect (self ):
94
101
"""Connects to the Adafruit IO MQTT Broker.
95
102
Must be called before any other API methods are called.
@@ -100,7 +107,7 @@ def connect(self):
100
107
raise AdafruitIO_MQTTError ("Unable to connect to Adafruit IO." )
101
108
102
109
def disconnect (self ):
103
- """Disconnects from Adafruit IO.
110
+ """Disconnects from Adafruit IO MQTT Broker .
104
111
"""
105
112
if self ._connected :
106
113
self ._client .disconnect ()
@@ -112,7 +119,7 @@ def is_connected(self):
112
119
113
120
# pylint: disable=not-callable, unused-argument
114
121
def _on_connect_mqtt (self , client , userdata , flags , return_code ):
115
- """Runs when the on_connect callback is run from code .
122
+ """Runs when the client calls on_connect .
116
123
"""
117
124
if self ._logger :
118
125
self ._client ._logger .debug ("Client called on_connect." )
@@ -126,8 +133,7 @@ def _on_connect_mqtt(self, client, userdata, flags, return_code):
126
133
127
134
# pylint: disable=not-callable, unused-argument
128
135
def _on_disconnect_mqtt (self , client , userdata , return_code ):
129
- """Runs when the on_disconnect callback is run from
130
- code.
136
+ """Runs when the client calls on_disconnect.
131
137
"""
132
138
if self ._logger :
133
139
self ._client ._logger .debug ("Client called on_disconnect" )
@@ -138,8 +144,8 @@ def _on_disconnect_mqtt(self, client, userdata, return_code):
138
144
139
145
# pylint: disable=not-callable
140
146
def _on_message_mqtt (self , client , topic , payload ):
141
- """Runs when the on_message callback is run from code.
142
- Parses incoming data from special Adafruit IO feeds.
147
+ """Runs when the client calls on_message. Parses and returns
148
+ incoming data from Adafruit IO feeds.
143
149
:param MQTT client: A MQTT Client Instance.
144
150
:param str topic: MQTT topic response from Adafruit IO.
145
151
:param str payload: MQTT payload data response from Adafruit IO.
@@ -197,7 +203,7 @@ def loop_blocking(self):
197
203
198
204
# Subscriptions
199
205
def subscribe (self , feed_key = None , group_key = None , shared_user = None ):
200
- """Subscribes to an Adafruit IO feed or group.
206
+ """Subscribes to your Adafruit IO feed or group.
201
207
Can also subscribe to someone else's feed.
202
208
:param str feed_key: Adafruit IO Feed key.
203
209
:param str group_key: Adafruit IO Group key.
@@ -226,13 +232,13 @@ def subscribe(self, feed_key=None, group_key=None, shared_user=None):
226
232
raise AdafruitIO_MQTTError ("Must provide a feed_key or group_key." )
227
233
228
234
def subscribe_to_throttling (self ):
229
- """Subscribes to your personal Adafruit IO /throttle feed .
235
+ """Subscribes to your personal Adafruit IO /throttle topic .
230
236
https://io.adafruit.com/api/docs/mqtt.html#mqtt-api-rate-limiting
231
237
"""
232
238
self ._client .subscribe ("%s/throttle" % self ._user )
233
239
234
240
def subscribe_to_errors (self ):
235
- """Subscribes to your personal Adafruit IO /errors feed .
241
+ """Subscribes to your personal Adafruit IO /errors topic .
236
242
Notifies you of errors relating to publish/subscribe calls.
237
243
"""
238
244
self ._client .subscribe ("%s/errors" % self ._user )
@@ -277,19 +283,25 @@ def unsubscribe(self, feed_key=None, group_key=None, shared_user=None):
277
283
:param str group_key: Adafruit IO Group key.
278
284
:param str shared_user: Owner of the Adafruit IO feed, required for shared feeds.
279
285
280
- Example of unsubscribing from an Adafruit IO Feed named 'temperature' :
286
+ Example of unsubscribing from a Feed:
281
287
282
288
.. code-block:: python
283
289
284
290
client.unsubscribe('temperature')
285
291
286
- Example of unsubscribing to two Adafruit IO feeds: `temperature`
292
+ Example of unsubscribing from two feeds: `temperature`
287
293
and `humidity`
288
294
289
295
.. code-block:: python
290
296
291
297
client.unsubscribe([('temperature'), ('humidity')])
292
298
299
+ Example of unsubscribing from a shared feed.
300
+
301
+ .. code-block:: python
302
+
303
+ client.unsubscribe('temperature', shared_user='adabot')
304
+
293
305
"""
294
306
if shared_user is not None and feed_key is not None :
295
307
self ._client .unsubscribe ("{0}/feeds/{1}" .format (shared_user , feed_key ))
@@ -302,9 +314,10 @@ def unsubscribe(self, feed_key=None, group_key=None, shared_user=None):
302
314
303
315
# Publishing
304
316
def publish_multiple (self , feeds_and_data , timeout = 3 , is_group = False ):
305
- """Publishes multiple data points to multiple feeds or groups.
317
+ """Publishes multiple data points to multiple feeds or groups with a variable
318
+ timeout.
306
319
:param str feeds_and_data: List of tuples containing topic strings and data values.
307
- :param int timeout: Delay between publishing data points to Adafruit IO.
320
+ :param int timeout: Delay between publishing data points to Adafruit IO, in seconds .
308
321
:param bool is_group: Set to True if you're publishing to a group.
309
322
310
323
Example of publishing multiple data points on different feeds to Adafruit IO:
@@ -343,22 +356,22 @@ def publish(self, feed_key, data, metadata=None, shared_user=None, is_group=Fals
343
356
344
357
client.publish('temperature', 30)
345
358
346
- Example of publishing a floating point value to Adafruit IO on feed 'temperature'.
359
+ Example of publishing a floating point value to feed 'temperature'.
347
360
..code-block:: python
348
361
349
362
client.publish('temperature', 3.14)
350
363
351
- Example of publishing a string to Adafruit IO on feed 'temperature'.
364
+ Example of publishing a string to feed 'temperature'.
352
365
..code-block:: python
353
366
354
367
client.publish('temperature, 'thirty degrees')
355
368
356
- Example of publishing an integer to Adafruit IO on group 'weatherstation'.
369
+ Example of publishing an integer to group 'weatherstation'.
357
370
..code-block:: python
358
371
359
372
client.publish('weatherstation', 12, is_group=True)
360
373
361
- Example of publishing to a shared Adafruit IO feed.
374
+ Example of publishing to a shared feed.
362
375
..code-block:: python
363
376
364
377
client.publish('temperature', shared_user='myfriend')
@@ -391,6 +404,11 @@ def get(self, feed_key):
391
404
value on feed_key.
392
405
https://io.adafruit.com/api/docs/mqtt.html#retained-values
393
406
:param str feed_key: Adafruit IO Feed key.
407
+
408
+ Example of obtaining a recently published value on a feed:
409
+ ..code-block:: python
410
+
411
+ io.get('temperature')
394
412
"""
395
413
self ._client .publish ("{0}/feeds{1}/get" .format (self ._user , feed_key ), "\0 " )
396
414
0 commit comments