@@ -145,15 +145,27 @@ def _on_disconnect_mqtt(self, client, userdata, rc):
145
145
def _on_message_mqtt (self , client , topic , message ):
146
146
"""Runs when the on_message callback is run from code.
147
147
Performs parsing based on username/feed/feed-key.
148
+ :param MQTT client: A MQTT Client Instance.
149
+ :param str topic: MQTT topic response from Adafruit IO.
150
+ :param str message: MQTT message data response from Adafruit IO.
148
151
"""
149
152
if self ._logger :
150
153
self ._client ._logger .debug ('Client called on_message.' )
151
154
if self .on_message is not None :
152
- # parse the feed/group name
155
+ # Parse the MQTT topic string
153
156
topic_name = topic .split ('/' )
154
- topic_name = topic_name [2 ]
155
- # parse the message
156
- message = '' if message is None else message
157
+ if topic_name [1 ] == "groups" :
158
+ print (message )
159
+ message = eval (message )
160
+ for feed in message ['feeds' ]:
161
+ topic_name = feed # TODO: change this to topic_name
162
+ message = message ['feeds' ][topic ]
163
+ print (topic , message )
164
+ topic_name = topic
165
+ else :
166
+ topic_name = topic_name [2 ]
167
+ # parse the message
168
+ message = '' if message is None else message
157
169
else :
158
170
raise ValueError ('You must define an on_message method before calling this callback.' )
159
171
self .on_message (self , topic_name , message )
@@ -169,7 +181,7 @@ def loop_blocking(self):
169
181
from Adafruit IO. Code below this call will not run.
170
182
"""
171
183
self ._client .loop_forever ()
172
-
184
+
173
185
# Subscriptions
174
186
def subscribe (self , feed_key = None , group_key = None , shared_user = None ):
175
187
"""Subscribes to an Adafruit IO feed or group.
@@ -194,26 +206,27 @@ def subscribe(self, feed_key=None, group_key=None, shared_user=None):
194
206
if shared_user is not None and feed_key is not None :
195
207
self ._client .subscribe ('{0}/feeds/{1}' .format (shared_user , feed_key ))
196
208
elif group_key is not None :
197
- self ._client .subscribe ('{0}/groups/{1}' .format (self ._user , feed_key ))
209
+ print ('subscribing to group...' )
210
+ self ._client .subscribe ('{0}/groups/{1}' .format (self ._user , group_key ))
198
211
elif feed_key is not None :
199
212
self ._client .subscribe ('{0}/feeds/{1}' .format (self ._user , feed_key ))
200
213
else :
201
214
raise AdafruitIO_MQTTError ('Must provide a feed_key or group_key.' )
202
215
203
- def subscribe_throttling (self ):
204
- """Subscribes to the username/ throttle feed to
205
- notify you if your Adafruit IO rate limit has been
206
- exceeded.
216
+ def subscribe_to_throttling (self ):
217
+ """Subscribes to the throttle feed to notify you
218
+ if your Adafruit IO rate limit has been exceeded.
219
+ https://io.adafruit.com/api/docs/mqtt.html#mqtt-api-rate-limiting
207
220
"""
208
221
self ._client .subscribe ('%s/throttle' % self ._user )
209
222
210
- def subscribe_randomizer (self , randomizer_id ):
223
+ def subscribe_to_randomizer (self , randomizer_id ):
211
224
"""Subscribes to a random data stream created by the Adafruit IO Words service.
212
225
:param int randomizer_id: Random word record you want data for.
213
226
"""
214
227
self ._client .subscribe ('{0}/integration/words/{1}' .format (self ._user , randomizer_id ))
215
228
216
- def subscribe_weather (self , integration_id , forecast_type ):
229
+ def subscribe_to_weather (self , integration_id , forecast_type ):
217
230
"""Subscribes to a weather forecast using the Adafruit IO PLUS weather
218
231
service. This feature is only avaliable to Adafruit IO PLUS subscribers.
219
232
:param int integration_id: Weather record you want data for.
@@ -240,6 +253,7 @@ def unsubscribe(self, feed_key=None, group_key=None, shared_user=None):
240
253
.. code-block:: python
241
254
242
255
client.unsubscribe([('temperature'), ('humidity')])
256
+
243
257
"""
244
258
if shared_user is not None and feed_key is not None :
245
259
self ._client .unsubscribe ('{0}/feeds/{1}' .format (shared_user , feed_key ))
@@ -320,6 +334,14 @@ def publish(self, feed_key, data, shared_user=None, is_group=False):
320
334
else :
321
335
self ._client .publish ('{0}/feeds/{1}' .format (self ._user , feed_key ), data )
322
336
337
+ def get (self , feed_key ):
338
+ """Calling this method will make Adafruit IO publish the most recent
339
+ value on feed_key.
340
+ https://io.adafruit.com/api/docs/mqtt.html#retained-values
341
+ :param str feed_key: Adafruit IO Feed key.
342
+ """
343
+ self ._client .publish ('{0}/feeds{1}/get' .format (self ._user , feed_key ), '\0 ' )
344
+
323
345
class RESTClient ():
324
346
"""
325
347
Client for interacting with the Adafruit IO HTTP API.
0 commit comments