From 51117a4ce0a7dd213b36b4006732344b270f5c1e Mon Sep 17 00:00:00 2001 From: tyeth Date: Wed, 14 Feb 2024 16:20:06 +0000 Subject: [PATCH 1/4] Specify CallbackAPIVersion.VERSION1 --- Adafruit_IO/mqtt_client.py | 2 +- examples/mqtt/mqtt_viewall.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Adafruit_IO/mqtt_client.py b/Adafruit_IO/mqtt_client.py index b6af726..4a16b20 100644 --- a/Adafruit_IO/mqtt_client.py +++ b/Adafruit_IO/mqtt_client.py @@ -60,7 +60,7 @@ def __init__(self, username, key, service_host='io.adafruit.com', secure=True): self.on_message = None self.on_subscribe = None # Initialize MQTT client. - self._client = mqtt.Client() + self._client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1) if secure: self._client.tls_set_context() self._secure = True diff --git a/examples/mqtt/mqtt_viewall.py b/examples/mqtt/mqtt_viewall.py index 64392be..36c2250 100644 --- a/examples/mqtt/mqtt_viewall.py +++ b/examples/mqtt/mqtt_viewall.py @@ -50,12 +50,12 @@ def on_connect(client, userdata, flags, rc): def on_disconnect(client, userdata, rc): print('Disconnected!') -def on_message(client, userdata, msg, retain): +def on_message(client, userdata, msg): print('Received on {0}: {1}'.format(msg.topic, msg.payload.decode('utf-8'))) # Create MQTT client and connect to Adafruit IO. -client = mqtt.Client() +client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1) client.username_pw_set(USERNAME, KEY) client.on_connect = on_connect client.on_disconnect = on_disconnect From 29bed98b60dc3bbc170f9e1c94c4d4eb657318d2 Mon Sep 17 00:00:00 2001 From: tyeth Date: Wed, 14 Feb 2024 16:26:10 +0000 Subject: [PATCH 2/4] Bump version to 2.7.2 --- 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 7a38ae0..7f90850 100644 --- a/Adafruit_IO/_version.py +++ b/Adafruit_IO/_version.py @@ -1 +1 @@ -__version__ = "2.7.1" +__version__ = "2.7.2" From a0cb9f613c5204f5f7e9762fbf2cfd55eb5eb3de Mon Sep 17 00:00:00 2001 From: tyeth Date: Wed, 14 Feb 2024 18:34:15 +0000 Subject: [PATCH 3/4] Update error codes + shared_feeds example --- Adafruit_IO/errors.py | 7 +++++-- examples/mqtt/mqtt_shared_feeds.py | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Adafruit_IO/errors.py b/Adafruit_IO/errors.py index ba6d88f..52e1590 100644 --- a/Adafruit_IO/errors.py +++ b/Adafruit_IO/errors.py @@ -20,8 +20,11 @@ # SOFTWARE. import json, requests +from paho.mqtt.client import error_string -# MQTT RC Error Types +# MQTT RC Error Types *** OBSOLETE *** +# See error_string() in client.py and enums.py from paho instead +# https://github.com/eclipse/paho.mqtt.python/blob/4eeb431f5ae72b42474cec42641fca1daa91c4b0/src/paho/mqtt/client.py#L291-L404 MQTT_ERRORS = [ 'Connection successful', 'Incorrect protocol version', 'Invalid Client ID', @@ -63,6 +66,6 @@ class MQTTError(Exception): """Handles connection attempt failed errors. """ def __init__(self, response): - error = MQTT_ERRORS[response] + error = error_string(response) super(MQTTError, self).__init__(error) pass \ No newline at end of file diff --git a/examples/mqtt/mqtt_shared_feeds.py b/examples/mqtt/mqtt_shared_feeds.py index 38b6667..23b2f16 100644 --- a/examples/mqtt/mqtt_shared_feeds.py +++ b/examples/mqtt/mqtt_shared_feeds.py @@ -70,5 +70,5 @@ def message(client, feed_id, payload): while True: value = random.randint(0, 100) print('Publishing {0} to {1}.'.format(value, IO_FEED)) - client.publish(IO_FEED, value, IO_FEED_USERNAME) + client.publish(IO_FEED, value, feed_user=IO_FEED_USERNAME) time.sleep(10) From 2737781f4c2cced380342dba94357694dc694d5a Mon Sep 17 00:00:00 2001 From: tyeth Date: Fri, 16 Feb 2024 12:40:37 +0000 Subject: [PATCH 4/4] PR Feedback: label client v1, clarify paho client in example/viewall --- Adafruit_IO/errors.py | 10 ---------- Adafruit_IO/mqtt_client.py | 2 +- examples/mqtt/mqtt_viewall.py | 2 +- 3 files changed, 2 insertions(+), 12 deletions(-) diff --git a/Adafruit_IO/errors.py b/Adafruit_IO/errors.py index 52e1590..52b3fd7 100644 --- a/Adafruit_IO/errors.py +++ b/Adafruit_IO/errors.py @@ -22,16 +22,6 @@ import json, requests from paho.mqtt.client import error_string -# MQTT RC Error Types *** OBSOLETE *** -# See error_string() in client.py and enums.py from paho instead -# https://github.com/eclipse/paho.mqtt.python/blob/4eeb431f5ae72b42474cec42641fca1daa91c4b0/src/paho/mqtt/client.py#L291-L404 -MQTT_ERRORS = [ 'Connection successful', - 'Incorrect protocol version', - 'Invalid Client ID', - 'Server unavailable ', - 'Bad username or password', - 'Not authorized' ] - class AdafruitIOError(Exception): """Base class for all Adafruit IO request failures.""" pass diff --git a/Adafruit_IO/mqtt_client.py b/Adafruit_IO/mqtt_client.py index 4a16b20..198b4d6 100644 --- a/Adafruit_IO/mqtt_client.py +++ b/Adafruit_IO/mqtt_client.py @@ -59,7 +59,7 @@ def __init__(self, username, key, service_host='io.adafruit.com', secure=True): self.on_disconnect = None self.on_message = None self.on_subscribe = None - # Initialize MQTT client. + # Initialize v1 MQTT client. self._client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1) if secure: self._client.tls_set_context() diff --git a/examples/mqtt/mqtt_viewall.py b/examples/mqtt/mqtt_viewall.py index 36c2250..31c3a9e 100644 --- a/examples/mqtt/mqtt_viewall.py +++ b/examples/mqtt/mqtt_viewall.py @@ -54,7 +54,7 @@ def on_message(client, userdata, msg): print('Received on {0}: {1}'.format(msg.topic, msg.payload.decode('utf-8'))) -# Create MQTT client and connect to Adafruit IO. +# Create Paho v1 MQTT client and connect to Adafruit IO. client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1) client.username_pw_set(USERNAME, KEY) client.on_connect = on_connect