Skip to content

Commit 328d003

Browse files
authored
Merge pull request #82 from brentru/add-random-service
Add random data service
2 parents 2170233 + b4ef4fe commit 328d003

File tree

4 files changed

+54
-4
lines changed

4 files changed

+54
-4
lines changed

Adafruit_IO/_version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "2.0.19"
1+
__version__ = "2.1"

Adafruit_IO/client.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,21 @@ def receive_weather(self, weather_id=None):
167167
:param int id: optional ID for retrieving a specified weather record.
168168
"""
169169
if weather_id:
170-
weatherpath = "integrations/weather/{0}".format(weather_id)
170+
weather_path = "integrations/weather/{0}".format(weather_id)
171171
else:
172-
weatherpath = "integrations/weather"
173-
return self._get(weatherpath)
172+
weather_path = "integrations/weather"
173+
return self._get(weather_path)
174+
175+
def receive_random(self, id=None):
176+
"""Access to Adafruit IO's Random Data
177+
service.
178+
:param int id: optional ID for retrieving a specified randomizer.
179+
"""
180+
if id:
181+
random_path = "integrations/words/{0}".format(id)
182+
else:
183+
random_path = "integrations/words"
184+
return self._get(random_path)
174185

175186
def receive(self, feed):
176187
"""Retrieve the most recent value for the specified feed. Returns a Data

Adafruit_IO/mqtt_client.py

+11
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,17 @@ def subscribe_group(self, group_id):
205205
"""
206206
self._client.subscribe('{0}/groups/{1}'.format(self._username, group_id))
207207

208+
def subscribe_randomizer(self, randomizer_id):
209+
"""Subscribe to changes on a specified random data stream from
210+
Adafruit IO's random data service.
211+
212+
MQTT random word subscriptions will publish data once per minute to
213+
every client that is subscribed to the same topic.
214+
215+
:param int randomizer_id: ID of the random word record you want data for.
216+
"""
217+
self._client.subscribe('{0}/integration/words/{1}'.format(self._username, randomizer_id))
218+
208219
def subscribe_weather(self, weather_id, forecast_type):
209220
"""Subscribe to Adafruit IO Weather
210221
:param int weather_id: weather record you want data for

examples/api/random_data.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
'random_data.py'
3+
================================================
4+
Example for accessing the Adafruit IO Random
5+
Data Service.
6+
7+
Author(s): Brent Rubell for Adafruit Industries
8+
"""
9+
# Import JSON for forecast parsing
10+
import json
11+
# Import Adafruit IO REST client.
12+
from Adafruit_IO import Client, Feed, RequestError
13+
14+
# Set to your Adafruit IO key.
15+
ADAFRUIT_IO_USERNAME = 'brubell'
16+
ADAFRUIT_IO_KEY = '6ec4b31bd2c54a09be911e0c1909b7ab'
17+
18+
# Create an instance of the REST client.
19+
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
20+
21+
generator_id = 1461
22+
23+
# Get the specified randomizer record with its current value and related details.
24+
random_data = aio.receive_random(generator_id)
25+
# Parse the API response
26+
data = json.dumps(random_data)
27+
data = json.loads(data)
28+
print('Random Data: {0}'.format(data['value']))

0 commit comments

Comments
 (0)