Skip to content

Commit 1ffa360

Browse files
author
brentru
committed
add special time topics, on_message parsing
1 parent 2d51477 commit 1ffa360

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

adafruit_io/adafruit_io.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ def _on_connect_mqtt(self, client, userdata, flags, rc):
107107
self._client._logger.debug('Client called on_connect.')
108108
if rc == 0:
109109
self._connected = True
110-
print('Connected to Adafruit IO!')
111110
else:
112111
raise AdafruitIO_MQTTError(rc)
113112
# Call the user-defined on_connect callback if defined
@@ -138,8 +137,7 @@ def _on_message_mqtt(self, client, topic, payload):
138137
# Parse the MQTT topic string
139138
topic_name = topic.split('/')
140139
if topic_name[1] == "groups":
141-
# Adafruit IO Group Feed Parsing
142-
# May have rx'd more than one feed - parse them.
140+
# Adafruit IO Group Feed(s)
143141
feeds = []
144142
messages = []
145143
payload = eval(payload)
@@ -150,9 +148,13 @@ def _on_message_mqtt(self, client, topic, payload):
150148
messages.append(payload)
151149
topic_name = feeds
152150
message = messages
151+
elif topic_name[0] == "time":
152+
# Adafruit IO Time Topic
153+
topic_name = topic_name[1]
154+
message = payload
153155
else:
156+
# Standard Adafruit IO Feed
154157
topic_name = topic_name[2]
155-
# parse the payload
156158
message = '' if payload is None else message
157159
else:
158160
raise ValueError('You must define an on_message method before calling this callback.')
@@ -191,7 +193,6 @@ def subscribe(self, feed_key=None, group_key=None, shared_user=None):
191193
192194
client.subscribe([('temperature'), ('humidity')])
193195
"""
194-
print('sub called!')
195196
if shared_user is not None and feed_key is not None:
196197
self._client.subscribe('{0}/feeds/{1}'.format(shared_user, feed_key))
197198
elif group_key is not None:
@@ -227,6 +228,19 @@ def subscribe_to_weather(self, integration_id, forecast_type):
227228
"""
228229
self._client.subscribe('{0}/integration/weather/{1}/{2}'.format(self._user, integration_id, forecast_type))
229230

231+
def subscribe_to_time(self, time_type):
232+
"""Adafruit IO provides some built-in MQTT topics for getting the current server time.
233+
:param str time_type: Current Adafruit IO server time. Can be `seconds`, `millis`, or `iso`.
234+
Information about these topics can be found on the Adafruit IO MQTT API Docs.:
235+
https://io.adafruit.com/api/docs/mqtt.html#time-topics
236+
"""
237+
if time_type == 'seconds' or time_type == 'millis':
238+
self._client.subscribe('time/'+time_type)
239+
elif time_type == 'iso':
240+
self._client.subscribe('time/ISO-8601')
241+
else:
242+
raise TypeError('Invalid time feed type specified')
243+
230244
def unsubscribe(self, feed_key=None, group_key=None, shared_user=None):
231245
"""Unsubscribes from an Adafruit IO feed or group.
232246
Can also subscribe to someone else's feed.

0 commit comments

Comments
 (0)