Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate paho to api v2 #28

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions pytest_mqtt/capmqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import logging
import threading
import typing as t
import warnings

import paho.mqtt.client as mqtt
import pytest
Expand All @@ -34,11 +33,11 @@ def __init__(self, on_message_callback: t.Optional[t.Callable] = None, host: str
if not hasattr(mqtt, "CallbackAPIVersion"):
# paho-mqtt 1.x
self.client = mqtt.Client()
self.use_legacy_api = True
else:
# paho-mqtt 2.x
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
self.client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1)
self.client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
self.use_legacy_api = False
self.on_message_callback = on_message_callback
self.host = host
self.port = int(port)
Expand All @@ -47,8 +46,8 @@ def __init__(self, on_message_callback: t.Optional[t.Callable] = None, host: str
def setup(self):
client = self.client
client.on_socket_open = self.on_socket_open
client.on_connect = self.on_connect
client.on_subscribe = self.on_subscribe
client.on_connect = self.on_connect_v1 if self.use_legacy_api else self.on_connect
client.on_subscribe = self.on_subscribe_v1 if self.use_legacy_api else self.on_subscribe
client.on_message = self.on_message
if self.on_message_callback:
client.on_message = self.on_message_callback
Expand All @@ -67,11 +66,17 @@ def stop(self):

def on_socket_open(self, client, userdata, sock):
logger.debug("[PYTEST] Opened socket to MQTT broker")

def on_connect_v1(self, client, userdata, flags, rc): # legacy API version 1
logger.debug("[PYTEST] Connected to MQTT broker")

def on_connect(self, client, userdata, flags, rc):
def on_connect(self, client, userdata, flags, reason_code, properties):
logger.debug("[PYTEST] Connected to MQTT broker")

def on_subscribe_v1(self, client, userdata, mid, granted_qos, properties=None): # legacy API version 1
logger.debug("[PYTEST] Subscribed to MQTT topic(s)")

def on_subscribe(self, client, userdata, mid, granted_qos, properties=None):
def on_subscribe(self, client, userdata, mid, reason_codes, properties):
logger.debug("[PYTEST] Subscribed to MQTT topic(s)")

def on_message(self, client, userdata, msg):
Expand Down
Loading