Skip to content

Commit

Permalink
Update to paho-mqtt 2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
WillB97 committed Jun 30, 2024
1 parent b45d1a6 commit 28c2dff
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ dev = [
"types-pyserial",
"pytest",
"pytest-cov",
"types-paho-mqtt",
"paho-mqtt >=2,<3"
]
vision = ["opencv-python-headless >=4,<5"]
mqtt = ["paho-mqtt >=1.6,<2"]
mqtt = ["paho-mqtt >=2,<3"]
19 changes: 14 additions & 5 deletions sbot/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(
self,
client_name: str | None = None,
topic_prefix: str | None = None,
mqtt_version: int = mqtt.MQTTv5,
mqtt_version: mqtt.MQTTProtocolVersion = mqtt.MQTTProtocolVersion.MQTTv5,
use_tls: bool | str = False,
username: str = '',
password: str = '',
Expand All @@ -32,7 +32,11 @@ def __init__(
self._client_name = client_name
self._img_topic = 'img'

self._client = mqtt.Client(client_id=client_name, protocol=mqtt_version)
self._client = mqtt.Client(
callback_api_version=mqtt.CallbackAPIVersion.VERSION2,
client_id=client_name,
protocol=mqtt_version,
)
self._client.on_connect = self._on_connect

if use_tls:
Expand Down Expand Up @@ -150,12 +154,17 @@ def wrapped_publish(
retain=retain, abs_topic=abs_topic)

def _on_connect(
self, client: mqtt.Client, userdata: Any, flags: dict[str, int], rc: int,
self,
client: mqtt.Client,
userdata: Any,
connect_flags: mqtt.ConnectFlags,
reason_code: mqtt.ReasonCode,
properties: mqtt.Properties | None = None,
) -> None:
if rc != mqtt.CONNACK_ACCEPTED:
if reason_code.is_failure:
LOGGER.warning(
f"Failed to connect to MQTT broker. Return code: {mqtt.error_string(rc)}"
"Failed to connect to MQTT broker. "
f"Return code: {reason_code.getName()}" # type: ignore[no-untyped-call]
)
return

Expand Down

0 comments on commit 28c2dff

Please sign in to comment.