diff --git a/paradox/interfaces/mqtt/basic.py b/paradox/interfaces/mqtt/basic.py index c733b96..a976fb8 100644 --- a/paradox/interfaces/mqtt/basic.py +++ b/paradox/interfaces/mqtt/basic.py @@ -117,7 +117,7 @@ def __init__(self, alarm): ps.subscribe(self._handle_panel_change, "changes") ps.subscribe(self._handle_panel_event, "events") - def on_connect(self, mqttc, userdata, flags, result): + def on_connect(self, mqttc, userdata, connect_flags, reason_code, properties=None): self.subscribe_callback( get_control_topic_prefix("output") + "/#", self._mqtt_handle_output_control, diff --git a/paradox/interfaces/mqtt/core.py b/paradox/interfaces/mqtt/core.py index 8df30ed..e511912 100644 --- a/paradox/interfaces/mqtt/core.py +++ b/paradox/interfaces/mqtt/core.py @@ -11,6 +11,7 @@ from paho.mqtt.client import ( LOGGING_LEVEL, MQTT_ERR_SUCCESS, + CallbackAPIVersion, Client, MQTTv5, MQTTv31, @@ -58,7 +59,8 @@ def get_instance(cls) -> "MQTTConnection": def __init__(self): self.client = Client( - "pai" + os.urandom(8).hex(), + CallbackAPIVersion.VERSION2, + client_id="pai" + os.urandom(8).hex(), protocol=protocol_map.get(str(cfg.MQTT_PROTOCOL), MQTTv311), transport=cfg.MQTT_TRANSPORT, ) @@ -197,30 +199,27 @@ def _report_pai_status(self, status): retain=True, ) - def _on_connect_cb(self, client, userdata, flags, result, properties=None): + def _on_connect_cb(self, client, userdata, connect_flags, reason_code, properties=None): # called on Thread-6 - if result == MQTT_ERR_SUCCESS: + if not reason_code.is_failure: logger.info("MQTT Broker Connected") self.state = ConnectionState.CONNECTED self._report_pai_status(self._last_pai_status) - self._call_registars("on_connect", client, userdata, flags, result) + self._call_registars("on_connect", client, userdata, connect_flags, reason_code, properties) else: logger.error( - f"Failed to connect to MQTT: {connack_string(result)} ({result})" + f"Failed to connect to MQTT: {connack_string(reason_code)} ({reason_code})" ) - def _on_disconnect_cb(self, client, userdata, *args, **kwargs): + def _on_disconnect_cb(self, client, userdata, disconnect_flags, reason_code, properties=None): # called on Thread-6 - # Handle different MQTT version signatures by using the first argument as rc - rc = args[0] if args else MQTT_ERR_SUCCESS - - if rc == MQTT_ERR_SUCCESS: + if not reason_code.is_failure: logger.info("MQTT Broker Disconnected") else: - logger.error(f"MQTT Broker unexpectedly disconnected. Code: {rc}") + logger.error(f"MQTT Broker unexpectedly disconnected. Code: {reason_code}") self.state = ConnectionState.NEW - self._call_registars("on_disconnect", self.client, userdata, rc) + self._call_registars("on_disconnect", self.client, userdata, disconnect_flags, reason_code, properties) def disconnect(self, reasoncode=None, properties=None): self.state = ConnectionState.DISCONNECTING @@ -305,10 +304,10 @@ def subscribe_callback(self, sub, callback: typing.Callable): self.mqtt.message_callback_add(sub, callback) self.mqtt.subscribe(sub) - def on_disconnect(self, client, userdata, rc): + def on_disconnect(self, client, userdata, disconnect_flags, reason_code, properties=None): """Called from MQTT connection""" pass - def on_connect(self, client, userdata, flags, result): + def on_connect(self, client, userdata, connect_flags, reason_code, properties=None): """Called from MQTT connection""" pass diff --git a/paradox/interfaces/mqtt/homeassistant.py b/paradox/interfaces/mqtt/homeassistant.py index 4399a5c..b325c6f 100644 --- a/paradox/interfaces/mqtt/homeassistant.py +++ b/paradox/interfaces/mqtt/homeassistant.py @@ -55,7 +55,7 @@ def _ready_future_callback(x): ps.subscribe(self._handle_labels_loaded, "labels_loaded") ps.subscribe(first_status_update_future, "status_update") - def on_connect(self, client, userdata, flags, result): + def on_connect(self, client, userdata, connect_flags, reason_code, properties=None): # TODO: do not create connected_future, use some other if not self.connected_future.done(): self.connected_future.set_result(True) diff --git a/paradox/lib/help.py b/paradox/lib/help.py index b97e401..f309f07 100644 --- a/paradox/lib/help.py +++ b/paradox/lib/help.py @@ -46,7 +46,7 @@ "pytz": dict( mandatory=False, desc="Panel time sync", install_name="pytz>=2023.3.post1" ), - "mqtt": dict(mandatory=False, desc="MQTT", install_name="paho_mqtt>=1.5.0,<2"), + "mqtt": dict(mandatory=False, desc="MQTT", install_name="paho_mqtt>=2.1.0,<3"), "pre-commit": dict(mandatory=False, desc="Development", install_name="pre-commit"), "flake8": dict(mandatory=False, desc="Code checker", install_name="flake8"), "tox": dict(mandatory=False, desc="virtual env / testing", install_name="tox"), diff --git a/requirements.txt b/requirements.txt index 4becf3a..65635d1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,7 @@ argparse>=1.4.0 construct~=2.10.70 flake8 -paho_mqtt>=1.5.0,<2 +paho_mqtt>=2.1.0,<3 pre-commit pushbullet.py>=0.11.0 diff --git a/setup.cfg b/setup.cfg index 2595c5f..b4448f0 100644 --- a/setup.cfg +++ b/setup.cfg @@ -33,7 +33,7 @@ install_requires = argparse>=1.4.0 python-slugify>=4.0.1 pytz>=2021.3 - paho_mqtt>=1.5.0,<2 + paho_mqtt>=2.1.0,<3 requests>=2.20.0 pyserial-asyncio>=0.4