Skip to content

Add random service #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Adafruit_IO/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.0.19"
__version__ = "2.1"
17 changes: 14 additions & 3 deletions Adafruit_IO/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,21 @@ def receive_weather(self, weather_id=None):
:param int id: optional ID for retrieving a specified weather record.
"""
if weather_id:
weatherpath = "integrations/weather/{0}".format(weather_id)
weather_path = "integrations/weather/{0}".format(weather_id)
else:
weatherpath = "integrations/weather"
return self._get(weatherpath)
weather_path = "integrations/weather"
return self._get(weather_path)

def receive_random(self, id=None):
"""Access to Adafruit IO's Random Data
service.
:param int id: optional ID for retrieving a specified randomizer.
"""
if id:
random_path = "integrations/words/{0}".format(id)
else:
random_path = "integrations/words"
return self._get(random_path)

def receive(self, feed):
"""Retrieve the most recent value for the specified feed. Returns a Data
Expand Down
11 changes: 11 additions & 0 deletions Adafruit_IO/mqtt_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,17 @@ def subscribe_group(self, group_id):
"""
self._client.subscribe('{0}/groups/{1}'.format(self._username, group_id))

def subscribe_randomizer(self, randomizer_id):
"""Subscribe to changes on a specified random data stream from
Adafruit IO's random data service.

MQTT random word subscriptions will publish data once per minute to
every client that is subscribed to the same topic.

:param int randomizer_id: ID of the random word record you want data for.
"""
self._client.subscribe('{0}/integration/words/{1}'.format(self._username, randomizer_id))

def subscribe_weather(self, weather_id, forecast_type):
"""Subscribe to Adafruit IO Weather
:param int weather_id: weather record you want data for
Expand Down
28 changes: 28 additions & 0 deletions examples/api/random_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
'random_data.py'
================================================
Example for accessing the Adafruit IO Random
Data Service.

Author(s): Brent Rubell for Adafruit Industries
"""
# Import JSON for forecast parsing
import json
# Import Adafruit IO REST client.
from Adafruit_IO import Client, Feed, RequestError

# Set to your Adafruit IO key.
ADAFRUIT_IO_USERNAME = 'brubell'
ADAFRUIT_IO_KEY = '6ec4b31bd2c54a09be911e0c1909b7ab'

# Create an instance of the REST client.
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

generator_id = 1461

# Get the specified randomizer record with its current value and related details.
random_data = aio.receive_random(generator_id)
# Parse the API response
data = json.dumps(random_data)
data = json.loads(data)
print('Random Data: {0}'.format(data['value']))