Skip to content

Commit

Permalink
Update and migrate mqtt to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
NickM-27 committed Feb 29, 2024
1 parent cb30450 commit 8fbeee4
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions frigate/comms/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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
Expand Down Expand Up @@ -96,9 +97,11 @@ def _set_initial_topics(self) -> None:
)
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,
)

Expand All @@ -117,25 +120,26 @@ def _on_connect(
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
Expand All @@ -144,15 +148,23 @@ def _on_connect(
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
logger.error("MQTT disconnected")

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",
Expand Down

0 comments on commit 8fbeee4

Please sign in to comment.