Skip to content

Commit 1f1eb7b

Browse files
authored
Merge pull request #582 from ParadoxAlarmInterface/copilot/update-paho-mqtt-requirement
Upgrade paho-mqtt to v2.x and fix breaking API changes
2 parents e0157c7 + 3876b11 commit 1f1eb7b

File tree

6 files changed

+18
-19
lines changed

6 files changed

+18
-19
lines changed

paradox/interfaces/mqtt/basic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def __init__(self, alarm):
117117
ps.subscribe(self._handle_panel_change, "changes")
118118
ps.subscribe(self._handle_panel_event, "events")
119119

120-
def on_connect(self, mqttc, userdata, flags, result):
120+
def on_connect(self, mqttc, userdata, connect_flags, reason_code, properties=None):
121121
self.subscribe_callback(
122122
get_control_topic_prefix("output") + "/#",
123123
self._mqtt_handle_output_control,

paradox/interfaces/mqtt/core.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from paho.mqtt.client import (
1212
LOGGING_LEVEL,
1313
MQTT_ERR_SUCCESS,
14+
CallbackAPIVersion,
1415
Client,
1516
MQTTv5,
1617
MQTTv31,
@@ -58,7 +59,8 @@ def get_instance(cls) -> "MQTTConnection":
5859

5960
def __init__(self):
6061
self.client = Client(
61-
"pai" + os.urandom(8).hex(),
62+
CallbackAPIVersion.VERSION2,
63+
client_id="pai" + os.urandom(8).hex(),
6264
protocol=protocol_map.get(str(cfg.MQTT_PROTOCOL), MQTTv311),
6365
transport=cfg.MQTT_TRANSPORT,
6466
)
@@ -197,30 +199,27 @@ def _report_pai_status(self, status):
197199
retain=True,
198200
)
199201

200-
def _on_connect_cb(self, client, userdata, flags, result, properties=None):
202+
def _on_connect_cb(self, client, userdata, connect_flags, reason_code, properties=None):
201203
# called on Thread-6
202-
if result == MQTT_ERR_SUCCESS:
204+
if not reason_code.is_failure:
203205
logger.info("MQTT Broker Connected")
204206
self.state = ConnectionState.CONNECTED
205207
self._report_pai_status(self._last_pai_status)
206-
self._call_registars("on_connect", client, userdata, flags, result)
208+
self._call_registars("on_connect", client, userdata, connect_flags, reason_code, properties)
207209
else:
208210
logger.error(
209-
f"Failed to connect to MQTT: {connack_string(result)} ({result})"
211+
f"Failed to connect to MQTT: {connack_string(reason_code)} ({reason_code})"
210212
)
211213

212-
def _on_disconnect_cb(self, client, userdata, *args, **kwargs):
214+
def _on_disconnect_cb(self, client, userdata, disconnect_flags, reason_code, properties=None):
213215
# called on Thread-6
214-
# Handle different MQTT version signatures by using the first argument as rc
215-
rc = args[0] if args else MQTT_ERR_SUCCESS
216-
217-
if rc == MQTT_ERR_SUCCESS:
216+
if not reason_code.is_failure:
218217
logger.info("MQTT Broker Disconnected")
219218
else:
220-
logger.error(f"MQTT Broker unexpectedly disconnected. Code: {rc}")
219+
logger.error(f"MQTT Broker unexpectedly disconnected. Code: {reason_code}")
221220

222221
self.state = ConnectionState.NEW
223-
self._call_registars("on_disconnect", self.client, userdata, rc)
222+
self._call_registars("on_disconnect", self.client, userdata, disconnect_flags, reason_code, properties)
224223

225224
def disconnect(self, reasoncode=None, properties=None):
226225
self.state = ConnectionState.DISCONNECTING
@@ -305,10 +304,10 @@ def subscribe_callback(self, sub, callback: typing.Callable):
305304
self.mqtt.message_callback_add(sub, callback)
306305
self.mqtt.subscribe(sub)
307306

308-
def on_disconnect(self, client, userdata, rc):
307+
def on_disconnect(self, client, userdata, disconnect_flags, reason_code, properties=None):
309308
"""Called from MQTT connection"""
310309
pass
311310

312-
def on_connect(self, client, userdata, flags, result):
311+
def on_connect(self, client, userdata, connect_flags, reason_code, properties=None):
313312
"""Called from MQTT connection"""
314313
pass

paradox/interfaces/mqtt/homeassistant.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def _ready_future_callback(x):
5555
ps.subscribe(self._handle_labels_loaded, "labels_loaded")
5656
ps.subscribe(first_status_update_future, "status_update")
5757

58-
def on_connect(self, client, userdata, flags, result):
58+
def on_connect(self, client, userdata, connect_flags, reason_code, properties=None):
5959
# TODO: do not create connected_future, use some other
6060
if not self.connected_future.done():
6161
self.connected_future.set_result(True)

paradox/lib/help.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"pytz": dict(
4747
mandatory=False, desc="Panel time sync", install_name="pytz>=2023.3.post1"
4848
),
49-
"mqtt": dict(mandatory=False, desc="MQTT", install_name="paho_mqtt>=1.5.0,<2"),
49+
"mqtt": dict(mandatory=False, desc="MQTT", install_name="paho_mqtt>=2.1.0,<3"),
5050
"pre-commit": dict(mandatory=False, desc="Development", install_name="pre-commit"),
5151
"flake8": dict(mandatory=False, desc="Code checker", install_name="flake8"),
5252
"tox": dict(mandatory=False, desc="virtual env / testing", install_name="tox"),

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
argparse>=1.4.0
22
construct~=2.10.70
33
flake8
4-
paho_mqtt>=1.5.0,<2
4+
paho_mqtt>=2.1.0,<3
55

66
pre-commit
77
pushbullet.py>=0.11.0

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ install_requires =
3333
argparse>=1.4.0
3434
python-slugify>=4.0.1
3535
pytz>=2021.3
36-
paho_mqtt>=1.5.0,<2
36+
paho_mqtt>=2.1.0,<3
3737
requests>=2.20.0
3838
pyserial-asyncio>=0.4
3939

0 commit comments

Comments
 (0)