Skip to content

Commit 7a9ebb8

Browse files
committed
Add support for paho-mqtt 2.x, retaining compatibility for 1.x
1 parent c5e15c4 commit 7a9ebb8

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

.github/workflows/tests.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@ jobs:
2323
matrix:
2424
os: ["ubuntu-latest"]
2525
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]
26+
paho-mqtt-version: ["1.*", "2.*"]
2627
fail-fast: false
2728

2829
env:
2930
OS: ${{ matrix.os }}
3031
PYTHON: ${{ matrix.python-version }}
3132

32-
name: Python ${{ matrix.python-version }} on OS ${{ matrix.os }}
33+
name:
34+
Python ${{ matrix.python-version }},
35+
paho-mqtt ${{ matrix.paho-mqtt-version }}
36+
on OS ${{ matrix.os }}
3337
steps:
3438

3539
- name: Acquire sources
@@ -53,6 +57,9 @@ jobs:
5357
# Install package in editable mode.
5458
pip install --editable=.[test,develop]
5559
60+
# Explicitly install designated version of paho-mqtt.
61+
pip install --upgrade 'paho-mqtt==${{ matrix.paho-mqtt-version }}'
62+
5663
- name: Check code style
5764
if: matrix.python-version != '3.6' && matrix.python-version != '3.7'
5865
run: |

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ in progress
88
- Accept command line options ``--mqtt-host`` and ``--mqtt-port``,
99
in order to connect to an MQTT broker on a different endpoint
1010
than ``localhost:1883``. Thanks, @zedfmario.
11+
- Add support for paho-mqtt 2.x, retaining compatibility for 1.x
1112

1213

1314
2023-08-03 0.3.1

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ classifiers = [
5656
dependencies = [
5757
"dataclasses; python_version<'3.7'",
5858
"importlib-metadata; python_version<'3.8'",
59-
"paho-mqtt<2",
59+
"paho-mqtt<3",
6060
"pytest-docker-fixtures<2",
6161
]
6262

pytest_mqtt/capmqtt.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@
2929
class MqttClientAdapter(threading.Thread):
3030
def __init__(self, on_message_callback: t.Optional[t.Callable] = None, host: str = "localhost", port: int = 1883):
3131
super().__init__()
32-
self.client: mqtt.Client = mqtt.Client()
32+
self.client: mqtt.Client
33+
if not hasattr(mqtt, "CallbackAPIVersion"):
34+
# paho-mqtt 1.x
35+
self.client = mqtt.Client()
36+
else:
37+
# paho-mqtt 2.x
38+
self.client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1)
3339
self.on_message_callback = on_message_callback
3440
self.host = host
3541
self.port = int(port)

0 commit comments

Comments
 (0)