diff --git a/Adafruit_IO/_version.py b/Adafruit_IO/_version.py index 00b64fe..259fe5f 100644 --- a/Adafruit_IO/_version.py +++ b/Adafruit_IO/_version.py @@ -1 +1 @@ -__version__ = "2.0.19" \ No newline at end of file +__version__ = "2.1" \ No newline at end of file diff --git a/Adafruit_IO/client.py b/Adafruit_IO/client.py index bd1ceb2..c4d5fb2 100644 --- a/Adafruit_IO/client.py +++ b/Adafruit_IO/client.py @@ -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 diff --git a/Adafruit_IO/mqtt_client.py b/Adafruit_IO/mqtt_client.py index 046669a..22cda92 100644 --- a/Adafruit_IO/mqtt_client.py +++ b/Adafruit_IO/mqtt_client.py @@ -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 diff --git a/examples/api/random_data.py b/examples/api/random_data.py new file mode 100644 index 0000000..a1da2b3 --- /dev/null +++ b/examples/api/random_data.py @@ -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'])) \ No newline at end of file