Skip to content

Commit

Permalink
Updated Paho API version to V2
Browse files Browse the repository at this point in the history
Updated affected callbacks according to paho migration guide.
https://eclipse.dev/paho/files/paho.mqtt.python/html/migrations.html
  • Loading branch information
hyperspacex2 authored and amotl committed Jan 7, 2025
1 parent efb06af commit 63bb785
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ in progress
===========
- paho-mqtt: Ignore deprecation warnings about Callback API v1
- mosquitto: Don't always pull OCI image
- Updated Paho API version to V2. Thank you, @hyperspacex2.

2024-07-29 0.4.2
================
Expand Down
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 @@ -68,10 +67,16 @@ def stop(self):
def on_socket_open(self, client, userdata, sock):
logger.debug("[PYTEST] Opened socket to MQTT broker")

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

def on_subscribe(self, client, userdata, mid, granted_qos, properties=None):
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, reason_codes, properties):
logger.debug("[PYTEST] Subscribed to MQTT topic(s)")

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

0 comments on commit 63bb785

Please sign in to comment.