From ea9316e770b7bd3d8b21c6f69eccb08f8e177f8b Mon Sep 17 00:00:00 2001 From: Nicolas Mowen Date: Thu, 29 Feb 2024 17:59:47 -0700 Subject: [PATCH] Update and migrate mqtt to v2 (#10160) --- frigate/comms/mqtt.py | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/frigate/comms/mqtt.py b/frigate/comms/mqtt.py index 6182c89e3..b83484d41 100644 --- a/frigate/comms/mqtt.py +++ b/frigate/comms/mqtt.py @@ -3,6 +3,7 @@ import threading from typing import Any, Callable import paho.mqtt.client as mqtt +from paho.mqtt.enums import CallbackAPIVersion from frigate.comms.dispatcher import Communicator from frigate.config import FrigateConfig @@ -96,9 +97,11 @@ class MqttClient(Communicator): # type: ignore[misc] ) self.publish( f"{camera_name}/birdseye_mode/state", - camera.birdseye.mode.value.upper() - if camera.birdseye.enabled - else "OFF", + ( + camera.birdseye.mode.value.upper() + if camera.birdseye.enabled + else "OFF" + ), retain=True, ) @@ -117,25 +120,26 @@ class MqttClient(Communicator): # type: ignore[misc] client: mqtt.Client, userdata: Any, flags: Any, - rc: mqtt.ReasonCodes, + reason_code: mqtt.ReasonCode, + properties: Any, ) -> None: """Mqtt connection callback.""" threading.current_thread().name = "mqtt" - if rc != 0: - if rc == 3: + if reason_code != 0: + if reason_code == "Server Unavailable": logger.error( "Unable to connect to MQTT server: MQTT Server unavailable" ) - elif rc == 4: + elif reason_code == "Bad user name or password": logger.error( "Unable to connect to MQTT server: MQTT Bad username or password" ) - elif rc == 5: + elif reason_code == "Not authorized": logger.error("Unable to connect to MQTT server: MQTT Not authorized") else: logger.error( "Unable to connect to MQTT server: Connection refused. Error code: " - + str(rc) + + reason_code.getName() ) self.connected = True @@ -144,7 +148,12 @@ class MqttClient(Communicator): # type: ignore[misc] self._set_initial_topics() def _on_disconnect( - self, client: mqtt.Client, userdata: Any, flags: Any, rc: mqtt + self, + client: mqtt.Client, + userdata: Any, + flags: Any, + reason_code: mqtt.ReasonCode, + properties: Any, ) -> None: """Mqtt disconnection callback.""" self.connected = False @@ -152,7 +161,10 @@ class MqttClient(Communicator): # type: ignore[misc] def _start(self) -> None: """Start mqtt client.""" - self.client = mqtt.Client(client_id=self.mqtt_config.client_id) + self.client = mqtt.Client( + callback_api_version=CallbackAPIVersion.VERSION2, + client_id=self.mqtt_config.client_id, + ) self.client.on_connect = self._on_connect self.client.will_set( self.mqtt_config.topic_prefix + "/available",