39
39
https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT
40
40
"""
41
41
import time
42
- from adafruit_io .adafruit_io_errors import AdafruitIO_RequestError , AdafruitIO_ThrottleError , AdafruitIO_MQTTError
42
+ from adafruit_io .adafruit_io_errors import AdafruitIO_RequestError , AdafruitIO_ThrottleError
43
+ from adafruit_io .adafruit_io_errors import AdafruitIO_MQTTError
43
44
44
45
__version__ = "0.0.0-auto.0"
45
46
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Adafruit_IO.git"
@@ -53,9 +54,9 @@ class IO_MQTT():
53
54
Client for interacting with the Adafruit IO MQTT API. The client establishes
54
55
a secure connection to Adafruit IO by default.
55
56
:param MiniMQTT mqtt_client: MiniMQTT Client object.
56
- :param bool secure: Enables a secure SSL/TLS connection with Adafruit IO.
57
57
"""
58
- def __init__ (self , mqtt_client , secure = True ):
58
+ # pylint: disable=protected-access
59
+ def __init__ (self , mqtt_client ):
59
60
# MiniMQTT Object
60
61
mqtt_client_type = str (type (mqtt_client ))
61
62
if 'MQTT' in mqtt_client_type :
@@ -85,9 +86,8 @@ def connect(self):
85
86
"""
86
87
try :
87
88
self ._client .connect ()
88
- except error as err :
89
- AdafruitIO_MQTTError (err )
90
- return
89
+ except error as e :
90
+ AdafruitIO_MQTTError (e )
91
91
92
92
def disconnect (self ):
93
93
"""Disconnects from Adafruit IO.
@@ -100,20 +100,22 @@ def is_connected(self):
100
100
"""Returns if connected to Adafruit IO MQTT Broker."""
101
101
return self ._client .is_connected
102
102
103
- def _on_connect_mqtt (self , client , userdata , flags , rc ):
103
+ # pylint: disable=not-callable
104
+ def _on_connect_mqtt (self , client , userdata , flags , return_code ):
104
105
"""Runs when the on_connect callback is run from code.
105
106
"""
106
107
if self ._logger :
107
108
self ._client ._logger .debug ('Client called on_connect.' )
108
- if rc == 0 :
109
+ if return_code == 0 :
109
110
self ._connected = True
110
111
else :
111
- raise AdafruitIO_MQTTError (rc )
112
+ raise AdafruitIO_MQTTError (return_code )
112
113
# Call the user-defined on_connect callback if defined
113
114
if self .on_connect is not None :
114
115
self .on_connect (self )
115
116
116
- def _on_disconnect_mqtt (self , client , userdata , rc ):
117
+ # pylint: disable=not-callable
118
+ def _on_disconnect_mqtt (self , client , userdata , return_code ):
117
119
"""Runs when the on_disconnect callback is run from
118
120
code.
119
121
"""
@@ -123,7 +125,8 @@ def _on_disconnect_mqtt(self, client, userdata, rc):
123
125
# Call the user-defined on_disconnect callblack if defined
124
126
if self .on_disconnect is not None :
125
127
self .on_disconnect (self )
126
-
128
+
129
+ # pylint: disable=not-callable
127
130
def _on_message_mqtt (self , client , topic , payload ):
128
131
"""Runs when the on_message callback is run from code.
129
132
Parses incoming data from special Adafruit IO feeds.
@@ -140,6 +143,7 @@ def _on_message_mqtt(self, client, topic, payload):
140
143
# Adafruit IO Group Feed(s)
141
144
feeds = []
142
145
messages = []
146
+ # TODO: Remove eval here...
143
147
payload = eval (payload )
144
148
for feed in payload ['feeds' ]:
145
149
feeds .append (feed )
@@ -159,13 +163,13 @@ def _on_message_mqtt(self, client, topic, payload):
159
163
else :
160
164
raise ValueError ('You must define an on_message method before calling this callback.' )
161
165
self .on_message (self , topic_name , message )
162
-
166
+
163
167
def loop (self ):
164
168
"""Manually process messages from Adafruit IO.
165
169
Use this method to check incoming subscription messages.
166
170
"""
167
171
self ._client .loop ()
168
-
172
+
169
173
def loop_blocking (self ):
170
174
"""Starts a blocking loop and to processes messages
171
175
from Adafruit IO. Code below this call will not run.
@@ -179,7 +183,7 @@ def subscribe(self, feed_key=None, group_key=None, shared_user=None):
179
183
:param str feed_key: Adafruit IO Feed key.
180
184
:param str group_key: Adafruit IO Group key.
181
185
:param str shared_user: Owner of the Adafruit IO feed, required for shared feeds.
182
-
186
+
183
187
Example of subscribing to an Adafruit IO Feed named 'temperature':
184
188
185
189
.. code-block:: python
@@ -220,21 +224,22 @@ def subscribe_to_randomizer(self, randomizer_id):
220
224
"""
221
225
self ._client .subscribe ('{0}/integration/words/{1}' .format (self ._user , randomizer_id ))
222
226
223
- def subscribe_to_weather (self , integration_id , forecast_type ):
227
+ def subscribe_to_weather (self , weather_record , forecast ):
224
228
"""Subscribes to a weather forecast using the Adafruit IO PLUS weather
225
229
service. This feature is only avaliable to Adafruit IO PLUS subscribers.
226
- :param int integration_id : Weather record you want data for.
227
- :param str forecast_type : Forecast data you'd like to recieve.
230
+ :param int weather_record : Weather record you want data for.
231
+ :param str forecast : Forecast data you'd like to recieve.
228
232
"""
229
- self ._client .subscribe ('{0}/integration/weather/{1}/{2}' .format (self ._user , integration_id , forecast_type ))
233
+ self ._client .subscribe ('{0}/integration/weather/{1}/{2}' .format (self ._user ,
234
+ weather_record , forecast ))
230
235
231
236
def subscribe_to_time (self , time_type ):
232
237
"""Adafruit IO provides some built-in MQTT topics for getting the current server time.
233
238
:param str time_type: Current Adafruit IO server time. Can be `seconds`, `millis`, or `iso`.
234
239
Information about these topics can be found on the Adafruit IO MQTT API Docs.:
235
240
https://io.adafruit.com/api/docs/mqtt.html#time-topics
236
241
"""
237
- if time_type == 'seconds' or time_type == 'millis' :
242
+ if 'seconds' or 'millis' in time_type :
238
243
self ._client .subscribe ('time/' + time_type )
239
244
elif time_type == 'iso' :
240
245
self ._client .subscribe ('time/ISO-8601' )
@@ -247,7 +252,7 @@ def unsubscribe(self, feed_key=None, group_key=None, shared_user=None):
247
252
:param str feed_key: Adafruit IO Feed key.
248
253
:param str group_key: Adafruit IO Group key.
249
254
:param str shared_user: Owner of the Adafruit IO feed, required for shared feeds.
250
-
255
+
251
256
Example of unsubscribing from an Adafruit IO Feed named 'temperature':
252
257
253
258
.. code-block:: python
@@ -280,7 +285,7 @@ def publish_multiple(self, feeds_and_data, timeout=3, is_group=False):
280
285
281
286
Example of publishing multiple data points on different feeds to Adafruit IO:
282
287
..code-block:: python
283
-
288
+
284
289
client.publish_multiple([('humidity', 24.5), ('temperature', 54)])
285
290
286
291
"""
@@ -311,12 +316,12 @@ def publish(self, feed_key, data, shared_user=None, is_group=False):
311
316
..code-block:: python
312
317
313
318
client.publish('temperature', 30)
314
-
319
+
315
320
Example of publishing a floating point value to Adafruit IO on feed 'temperature'.
316
321
..code-block:: python
317
322
318
323
client.publish('temperature', 3.14)
319
-
324
+
320
325
Example of publishing a string to Adafruit IO on feed 'temperature'.
321
326
..code-block:: python
322
327
@@ -336,7 +341,7 @@ def publish(self, feed_key, data, shared_user=None, is_group=False):
336
341
if is_group :
337
342
self ._client .publish ('{0}/groups/{1}' .format (self ._user , feed_key ), data )
338
343
return
339
- elif shared_user is not None :
344
+ if shared_user is not None :
340
345
self ._client .publish ('{0}/feeds/{1}' .format (shared_user , feed_key ), data )
341
346
else :
342
347
self ._client .publish ('{0}/feeds/{1}' .format (self ._user , feed_key ), data )
@@ -571,5 +576,6 @@ def receive_time(self):
571
576
"""
572
577
path = self ._compose_path ('integrations/time/struct.json' )
573
578
time = self ._get (path )
574
- return time .struct_time ((time ['year' ], time ['mon' ], time ['mday' ], time ['hour' ],
575
- time ['min' ], time ['sec' ], time ['wday' ], time ['yday' ], time ['isdst' ]))
579
+ return time .struct_time ((time ['year' ], time ['mon' ], time ['mday' ],
580
+ time ['hour' ], time ['min' ], time ['sec' ],
581
+ time ['wday' ], time ['yday' ], time ['isdst' ]))
0 commit comments