Skip to content

Commit 2306cbf

Browse files
author
brentru
committed
add /get retain method, modify on_message_mqtt to correctly parse a group
1 parent b615e84 commit 2306cbf

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

adafruit_io/adafruit_io.py

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,27 @@ def _on_disconnect_mqtt(self, client, userdata, rc):
145145
def _on_message_mqtt(self, client, topic, message):
146146
"""Runs when the on_message callback is run from code.
147147
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.
148151
"""
149152
if self._logger:
150153
self._client._logger.debug('Client called on_message.')
151154
if self.on_message is not None:
152-
# parse the feed/group name
155+
# Parse the MQTT topic string
153156
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
157169
else:
158170
raise ValueError('You must define an on_message method before calling this callback.')
159171
self.on_message(self, topic_name, message)
@@ -169,7 +181,7 @@ def loop_blocking(self):
169181
from Adafruit IO. Code below this call will not run.
170182
"""
171183
self._client.loop_forever()
172-
184+
173185
# Subscriptions
174186
def subscribe(self, feed_key=None, group_key=None, shared_user=None):
175187
"""Subscribes to an Adafruit IO feed or group.
@@ -194,26 +206,27 @@ def subscribe(self, feed_key=None, group_key=None, shared_user=None):
194206
if shared_user is not None and feed_key is not None:
195207
self._client.subscribe('{0}/feeds/{1}'.format(shared_user, feed_key))
196208
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))
198211
elif feed_key is not None:
199212
self._client.subscribe('{0}/feeds/{1}'.format(self._user, feed_key))
200213
else:
201214
raise AdafruitIO_MQTTError('Must provide a feed_key or group_key.')
202215

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
207220
"""
208221
self._client.subscribe('%s/throttle'%self._user)
209222

210-
def subscribe_randomizer(self, randomizer_id):
223+
def subscribe_to_randomizer(self, randomizer_id):
211224
"""Subscribes to a random data stream created by the Adafruit IO Words service.
212225
:param int randomizer_id: Random word record you want data for.
213226
"""
214227
self._client.subscribe('{0}/integration/words/{1}'.format(self._user, randomizer_id))
215228

216-
def subscribe_weather(self, integration_id, forecast_type):
229+
def subscribe_to_weather(self, integration_id, forecast_type):
217230
"""Subscribes to a weather forecast using the Adafruit IO PLUS weather
218231
service. This feature is only avaliable to Adafruit IO PLUS subscribers.
219232
: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):
240253
.. code-block:: python
241254
242255
client.unsubscribe([('temperature'), ('humidity')])
256+
243257
"""
244258
if shared_user is not None and feed_key is not None:
245259
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):
320334
else:
321335
self._client.publish('{0}/feeds/{1}'.format(self._user, feed_key), data)
322336

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+
323345
class RESTClient():
324346
"""
325347
Client for interacting with the Adafruit IO HTTP API.

0 commit comments

Comments
 (0)