Skip to content

Commit 8fd72c2

Browse files
committed
Update to use new MQTT path with username.
1 parent 0dbd70c commit 8fd72c2

File tree

5 files changed

+26
-9
lines changed

5 files changed

+26
-9
lines changed

Adafruit_IO/mqtt_client.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,15 @@ class MQTTClient(object):
3434
using the MQTT protocol.
3535
"""
3636

37-
def __init__(self, key, service_host='io.adafruit.com', service_port=1883):
37+
def __init__(self, username, key, service_host='io.adafruit.com', service_port=1883):
3838
"""Create instance of MQTT client.
3939
4040
Required parameters:
41+
- username: The Adafruit.IO username for your account (found on the
42+
accounts site https://accounts.adafruit.com/).
4143
- key: The Adafruit.IO access key for your account.
4244
"""
45+
self._username = username
4346
self._service_host = service_host
4447
self._service_port = service_port
4548
# Initialize event callbacks to be None so they don't fire.
@@ -48,7 +51,7 @@ def __init__(self, key, service_host='io.adafruit.com', service_port=1883):
4851
self.on_message = None
4952
# Initialize MQTT client.
5053
self._client = mqtt.Client()
51-
self._client.username_pw_set(key)
54+
self._client.username_pw_set(username, key)
5255
self._client.on_connect = self._mqtt_connect
5356
self._client.on_disconnect = self._mqtt_disconnect
5457
self._client.on_message = self._mqtt_message
@@ -146,7 +149,7 @@ def subscribe(self, feed_id):
146149
"""Subscribe to changes on the specified feed. When the feed is updated
147150
the on_message function will be called with the feed_id and new value.
148151
"""
149-
self._client.subscribe('api/feeds/{0}/data/receive.json'.format(feed_id))
152+
self._client.subscribe('{0}/feeds/{1}'.format(self._username, feed_id))
150153

151154
def publish(self, feed_id, value):
152155
"""Publish a value to a specified feed.
@@ -155,5 +158,5 @@ def publish(self, feed_id, value):
155158
- feed_id: The id of the feed to update.
156159
- value: The new value to publish to the feed.
157160
"""
158-
self._client.publish('api/feeds/{0}/data/send.json'.format(feed_id),
161+
self._client.publish('{0}/feeds/{1}'.format(self._username, feed_id),
159162
payload=value)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
setup(
66
name='adafruit-io',
7-
version='0.9.1',
7+
version='1.0.0',
88
author='Justin Cooper',
99
author_email='[email protected]',
1010
packages=['Adafruit_IO'],

tests/README.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ set this envirionment variable before running the tests, for example to run all
1010
the tests with a key execute in this directory:
1111
ADAFRUIT_IO_KEY=my_io_key_value python -m unittest discover
1212

13+
In addition for the MQTT tests you must set the following environment variable
14+
to the username for your AIO account (found on https://accounts.adafruit.com):
15+
ADAFRUIT_IO_USERNAME=your_username
16+
1317
To add your own tests you are strongly encouraged to build off the test base
1418
class provided in base.py. This class provides a place for common functions
1519
that don't need to be duplicated across all the tests. See the existing test

tests/base.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,13 @@ def get_test_key(self):
3333
raise RuntimeError("ADAFRUIT_IO_KEY environment variable must be " \
3434
"set with valid Adafruit IO key to run this test!")
3535
return key
36+
37+
def get_test_username(self):
38+
"""Return the AIO username specified in the ADAFRUIT_IO_USERNAME
39+
environment variable, or raise an exception if it doesn't exist.
40+
"""
41+
username = os.environ.get('ADAFRUIT_IO_USERNAME', None)
42+
if username is None:
43+
raise RuntimeError("ADAFRUIT_IO_USERNAME environment variable must be " \
44+
"set with valid Adafruit IO username to run this test!")
45+
return username

tests/test_mqtt_client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ def wait_until_connected(self, client, connect_value=True,
4545

4646
def test_create_client(self):
4747
# Create MQTT test client.
48-
client = MQTTClient(self.get_test_key())
48+
client = MQTTClient(self.get_test_username(), self.get_test_key())
4949
# Verify not connected by default.
5050
self.assertFalse(client.is_connected())
5151

5252
def test_connect(self):
5353
# Create MQTT test client.
54-
client = MQTTClient(self.get_test_key())
54+
client = MQTTClient(self.get_test_username(), self.get_test_key())
5555
# Verify on_connect handler is called and expected client is provided.
5656
def on_connect(mqtt_client):
5757
self.assertEqual(mqtt_client, client)
@@ -64,7 +64,7 @@ def on_connect(mqtt_client):
6464

6565
def test_disconnect(self):
6666
# Create MQTT test client.
67-
client = MQTTClient(self.get_test_key())
67+
client = MQTTClient(self.get_test_username(), self.get_test_key())
6868
# Verify on_connect handler is called and expected client is provided.
6969
def on_disconnect(mqtt_client):
7070
self.assertEqual(mqtt_client, client)
@@ -80,7 +80,7 @@ def on_disconnect(mqtt_client):
8080

8181
def test_subscribe_and_publish(self):
8282
# Create MQTT test client.
83-
client = MQTTClient(self.get_test_key())
83+
client = MQTTClient(self.get_test_username(), self.get_test_key())
8484
# Save all on_message handler responses.
8585
messages = []
8686
def on_message(mqtt_client, feed, payload):

0 commit comments

Comments
 (0)