Skip to content

Commit

Permalink
Add support for paho-mqtt 2.x, retaining compatibility for 1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Mar 29, 2024
1 parent c5e15c4 commit 7a9ebb8
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/tests.yml
Expand Up @@ -23,13 +23,17 @@ jobs:
matrix:
os: ["ubuntu-latest"]
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]
paho-mqtt-version: ["1.*", "2.*"]
fail-fast: false

env:
OS: ${{ matrix.os }}
PYTHON: ${{ matrix.python-version }}

name: Python ${{ matrix.python-version }} on OS ${{ matrix.os }}
name:
Python ${{ matrix.python-version }},
paho-mqtt ${{ matrix.paho-mqtt-version }}
on OS ${{ matrix.os }}
steps:

- name: Acquire sources
Expand All @@ -53,6 +57,9 @@ jobs:
# Install package in editable mode.
pip install --editable=.[test,develop]
# Explicitly install designated version of paho-mqtt.
pip install --upgrade 'paho-mqtt==${{ matrix.paho-mqtt-version }}'
- name: Check code style
if: matrix.python-version != '3.6' && matrix.python-version != '3.7'
run: |
Expand Down
1 change: 1 addition & 0 deletions CHANGES.rst
Expand Up @@ -8,6 +8,7 @@ in progress
- Accept command line options ``--mqtt-host`` and ``--mqtt-port``,
in order to connect to an MQTT broker on a different endpoint
than ``localhost:1883``. Thanks, @zedfmario.
- Add support for paho-mqtt 2.x, retaining compatibility for 1.x


2023-08-03 0.3.1
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Expand Up @@ -56,7 +56,7 @@ classifiers = [
dependencies = [
"dataclasses; python_version<'3.7'",
"importlib-metadata; python_version<'3.8'",
"paho-mqtt<2",
"paho-mqtt<3",
"pytest-docker-fixtures<2",
]

Expand Down
8 changes: 7 additions & 1 deletion pytest_mqtt/capmqtt.py
Expand Up @@ -29,7 +29,13 @@
class MqttClientAdapter(threading.Thread):
def __init__(self, on_message_callback: t.Optional[t.Callable] = None, host: str = "localhost", port: int = 1883):
super().__init__()
self.client: mqtt.Client = mqtt.Client()
self.client: mqtt.Client
if not hasattr(mqtt, "CallbackAPIVersion"):
# paho-mqtt 1.x
self.client = mqtt.Client()
else:
# paho-mqtt 2.x
self.client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1)
self.on_message_callback = on_message_callback
self.host = host
self.port = int(port)
Expand Down

0 comments on commit 7a9ebb8

Please sign in to comment.