From 02634fb588172130e0fc83d24d6890c0c2fac024 Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 15 Jan 2019 14:21:40 -0500 Subject: [PATCH 1/4] add method for receiving random data --- Adafruit_IO/client.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) 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 From 32661c2b056f0abb43b7564715cc86081e8bad59 Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 15 Jan 2019 14:33:51 -0500 Subject: [PATCH 2/4] adding example of getting random data by id --- examples/api/random_data.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 examples/api/random_data.py diff --git a/examples/api/random_data.py b/examples/api/random_data.py new file mode 100644 index 0000000..96a190e --- /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 = 'USER' +ADAFRUIT_IO_KEY = 'KEY' + +# 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 From d1b1ac8bc335a10211142909f1e842af60c8f4c3 Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 15 Jan 2019 15:20:30 -0500 Subject: [PATCH 3/4] add subscribe_randomizer and mqtt example --- Adafruit_IO/mqtt_client.py | 11 +++++++++++ examples/api/random_data.py | 4 ++-- 2 files changed, 13 insertions(+), 2 deletions(-) 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 index 96a190e..a1da2b3 100644 --- a/examples/api/random_data.py +++ b/examples/api/random_data.py @@ -12,8 +12,8 @@ from Adafruit_IO import Client, Feed, RequestError # Set to your Adafruit IO key. -ADAFRUIT_IO_USERNAME = 'USER' -ADAFRUIT_IO_KEY = 'KEY' +ADAFRUIT_IO_USERNAME = 'brubell' +ADAFRUIT_IO_KEY = '6ec4b31bd2c54a09be911e0c1909b7ab' # Create an instance of the REST client. aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY) From b4ef4feb176617adc8b3962956b1b067e03cc360 Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 15 Jan 2019 15:27:18 -0500 Subject: [PATCH 4/4] update version to 2.1 --- Adafruit_IO/_version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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