Skip to content

Commit 99f3d0a

Browse files
author
brentru
committed
fix exit method, dc on exit
1 parent a2772c9 commit 99f3d0a

File tree

1 file changed

+37
-19
lines changed

1 file changed

+37
-19
lines changed

adafruit_io/adafruit_io.py

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ class IO_MQTT:
5959

6060
# pylint: disable=protected-access
6161
def __init__(self, mqtt_client):
62-
# MiniMQTT Object
62+
# Check for MiniMQTT client
6363
mqtt_client_type = str(type(mqtt_client))
6464
if "MQTT" in mqtt_client_type:
6565
self._client = mqtt_client
6666
else:
6767
raise TypeError(
6868
"This class requires a MiniMQTT client object, please create one."
6969
)
70-
# Adafruit IO Auth. requires a username
70+
# MiniMQTT's username kwarg is optional, IO requires a username
7171
try:
7272
self._user = self._client._user
7373
except:
@@ -85,11 +85,18 @@ def __init__(self, mqtt_client):
8585
self._client.on_disconnect = self._on_disconnect_mqtt
8686
self._client.on_message = self._on_message_mqtt
8787
self._logger = False
88+
# Write to the MiniMQTT logger, if avaliable.
8889
if self._client._logger is not None:
8990
self._logger = True
9091
self._client.set_logger_level("DEBUG")
9192
self._connected = False
9293

94+
def __enter__(self):
95+
return self
96+
97+
def __exit__(self, exception_type, exception_value, traceback):
98+
self.disconnect()
99+
93100
def connect(self):
94101
"""Connects to the Adafruit IO MQTT Broker.
95102
Must be called before any other API methods are called.
@@ -100,7 +107,7 @@ def connect(self):
100107
raise AdafruitIO_MQTTError("Unable to connect to Adafruit IO.")
101108

102109
def disconnect(self):
103-
"""Disconnects from Adafruit IO.
110+
"""Disconnects from Adafruit IO MQTT Broker.
104111
"""
105112
if self._connected:
106113
self._client.disconnect()
@@ -112,7 +119,7 @@ def is_connected(self):
112119

113120
# pylint: disable=not-callable, unused-argument
114121
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.
116123
"""
117124
if self._logger:
118125
self._client._logger.debug("Client called on_connect.")
@@ -126,8 +133,7 @@ def _on_connect_mqtt(self, client, userdata, flags, return_code):
126133

127134
# pylint: disable=not-callable, unused-argument
128135
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.
131137
"""
132138
if self._logger:
133139
self._client._logger.debug("Client called on_disconnect")
@@ -138,8 +144,8 @@ def _on_disconnect_mqtt(self, client, userdata, return_code):
138144

139145
# pylint: disable=not-callable
140146
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.
143149
:param MQTT client: A MQTT Client Instance.
144150
:param str topic: MQTT topic response from Adafruit IO.
145151
:param str payload: MQTT payload data response from Adafruit IO.
@@ -197,7 +203,7 @@ def loop_blocking(self):
197203

198204
# Subscriptions
199205
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.
201207
Can also subscribe to someone else's feed.
202208
:param str feed_key: Adafruit IO Feed key.
203209
:param str group_key: Adafruit IO Group key.
@@ -226,13 +232,13 @@ def subscribe(self, feed_key=None, group_key=None, shared_user=None):
226232
raise AdafruitIO_MQTTError("Must provide a feed_key or group_key.")
227233

228234
def subscribe_to_throttling(self):
229-
"""Subscribes to your personal Adafruit IO /throttle feed.
235+
"""Subscribes to your personal Adafruit IO /throttle topic.
230236
https://io.adafruit.com/api/docs/mqtt.html#mqtt-api-rate-limiting
231237
"""
232238
self._client.subscribe("%s/throttle" % self._user)
233239

234240
def subscribe_to_errors(self):
235-
"""Subscribes to your personal Adafruit IO /errors feed.
241+
"""Subscribes to your personal Adafruit IO /errors topic.
236242
Notifies you of errors relating to publish/subscribe calls.
237243
"""
238244
self._client.subscribe("%s/errors" % self._user)
@@ -277,19 +283,25 @@ def unsubscribe(self, feed_key=None, group_key=None, shared_user=None):
277283
:param str group_key: Adafruit IO Group key.
278284
:param str shared_user: Owner of the Adafruit IO feed, required for shared feeds.
279285
280-
Example of unsubscribing from an Adafruit IO Feed named 'temperature':
286+
Example of unsubscribing from a Feed:
281287
282288
.. code-block:: python
283289
284290
client.unsubscribe('temperature')
285291
286-
Example of unsubscribing to two Adafruit IO feeds: `temperature`
292+
Example of unsubscribing from two feeds: `temperature`
287293
and `humidity`
288294
289295
.. code-block:: python
290296
291297
client.unsubscribe([('temperature'), ('humidity')])
292298
299+
Example of unsubscribing from a shared feed.
300+
301+
.. code-block:: python
302+
303+
client.unsubscribe('temperature', shared_user='adabot')
304+
293305
"""
294306
if shared_user is not None and feed_key is not None:
295307
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):
302314

303315
# Publishing
304316
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.
306319
: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.
308321
:param bool is_group: Set to True if you're publishing to a group.
309322
310323
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
343356
344357
client.publish('temperature', 30)
345358
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'.
347360
..code-block:: python
348361
349362
client.publish('temperature', 3.14)
350363
351-
Example of publishing a string to Adafruit IO on feed 'temperature'.
364+
Example of publishing a string to feed 'temperature'.
352365
..code-block:: python
353366
354367
client.publish('temperature, 'thirty degrees')
355368
356-
Example of publishing an integer to Adafruit IO on group 'weatherstation'.
369+
Example of publishing an integer to group 'weatherstation'.
357370
..code-block:: python
358371
359372
client.publish('weatherstation', 12, is_group=True)
360373
361-
Example of publishing to a shared Adafruit IO feed.
374+
Example of publishing to a shared feed.
362375
..code-block:: python
363376
364377
client.publish('temperature', shared_user='myfriend')
@@ -391,6 +404,11 @@ def get(self, feed_key):
391404
value on feed_key.
392405
https://io.adafruit.com/api/docs/mqtt.html#retained-values
393406
: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')
394412
"""
395413
self._client.publish("{0}/feeds{1}/get".format(self._user, feed_key), "\0")
396414

0 commit comments

Comments
 (0)