Skip to content

Commit

Permalink
Add paho.mqtt API v2.0+ support. Retains V1.x compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
thess committed Apr 7, 2024
1 parent cb14875 commit 5dc577d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion callattendant/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# and screened callers through to the home phone.
#
default_config = {
"VERSION": '1.7.3',
"VERSION": '1.7.4',

"DEBUG": False,
"TESTING": False,
Expand Down
11 changes: 10 additions & 1 deletion callattendant/hardware/mqttindicators.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def __init__(self, host, port=1883, topic_prefix='callattendant', username=None,
if mqtt_client is None:
# No need to re-init
mqtt_client = self
# Check for callback API version (new in v2)
self.has_callback_version = hasattr(mqtt, 'CallbackAPIVersion')

self.server = host
self.port = port
Expand All @@ -52,7 +54,14 @@ def publish(self, topic, message):
"""
Publish a message to a topic.
"""
client = mqtt.Client()
# Changes to the Paho MQTT client API in version 2.0
# Even if we don't use callbacks, we need to specify the version
if (self.has_callback_version):
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1)
else:
# Version 1.x compatibility
client = mqtt.Client()

if self.username is not None:
client.username_pw_set(self.username, self.password)
client.connect(self.server, self.port)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

setuptools.setup(
name="callattendant", # Add username when uploading to TestPyPI
version="1.7.3", # Ensure this is in-sync with VERSION in config.py
version="1.7.4", # Ensure this is in-sync with VERSION in config.py
author="Ted Hess",
author_email="[email protected]",
description="An automated call attendant and call blocker using a USR5637 or CX930xx modem",
Expand Down

0 comments on commit 5dc577d

Please sign in to comment.