mirror of
https://github.com/blakeblackshear/frigate.git
synced 2024-11-21 19:07:46 +01:00
Update and migrate mqtt to v2 (#10160)
This commit is contained in:
parent
dcc0886305
commit
ea9316e770
@ -3,6 +3,7 @@ import threading
|
|||||||
from typing import Any, Callable
|
from typing import Any, Callable
|
||||||
|
|
||||||
import paho.mqtt.client as mqtt
|
import paho.mqtt.client as mqtt
|
||||||
|
from paho.mqtt.enums import CallbackAPIVersion
|
||||||
|
|
||||||
from frigate.comms.dispatcher import Communicator
|
from frigate.comms.dispatcher import Communicator
|
||||||
from frigate.config import FrigateConfig
|
from frigate.config import FrigateConfig
|
||||||
@ -96,9 +97,11 @@ class MqttClient(Communicator): # type: ignore[misc]
|
|||||||
)
|
)
|
||||||
self.publish(
|
self.publish(
|
||||||
f"{camera_name}/birdseye_mode/state",
|
f"{camera_name}/birdseye_mode/state",
|
||||||
|
(
|
||||||
camera.birdseye.mode.value.upper()
|
camera.birdseye.mode.value.upper()
|
||||||
if camera.birdseye.enabled
|
if camera.birdseye.enabled
|
||||||
else "OFF",
|
else "OFF"
|
||||||
|
),
|
||||||
retain=True,
|
retain=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -117,25 +120,26 @@ class MqttClient(Communicator): # type: ignore[misc]
|
|||||||
client: mqtt.Client,
|
client: mqtt.Client,
|
||||||
userdata: Any,
|
userdata: Any,
|
||||||
flags: Any,
|
flags: Any,
|
||||||
rc: mqtt.ReasonCodes,
|
reason_code: mqtt.ReasonCode,
|
||||||
|
properties: Any,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Mqtt connection callback."""
|
"""Mqtt connection callback."""
|
||||||
threading.current_thread().name = "mqtt"
|
threading.current_thread().name = "mqtt"
|
||||||
if rc != 0:
|
if reason_code != 0:
|
||||||
if rc == 3:
|
if reason_code == "Server Unavailable":
|
||||||
logger.error(
|
logger.error(
|
||||||
"Unable to connect to MQTT server: MQTT Server unavailable"
|
"Unable to connect to MQTT server: MQTT Server unavailable"
|
||||||
)
|
)
|
||||||
elif rc == 4:
|
elif reason_code == "Bad user name or password":
|
||||||
logger.error(
|
logger.error(
|
||||||
"Unable to connect to MQTT server: MQTT Bad username or password"
|
"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")
|
logger.error("Unable to connect to MQTT server: MQTT Not authorized")
|
||||||
else:
|
else:
|
||||||
logger.error(
|
logger.error(
|
||||||
"Unable to connect to MQTT server: Connection refused. Error code: "
|
"Unable to connect to MQTT server: Connection refused. Error code: "
|
||||||
+ str(rc)
|
+ reason_code.getName()
|
||||||
)
|
)
|
||||||
|
|
||||||
self.connected = True
|
self.connected = True
|
||||||
@ -144,7 +148,12 @@ class MqttClient(Communicator): # type: ignore[misc]
|
|||||||
self._set_initial_topics()
|
self._set_initial_topics()
|
||||||
|
|
||||||
def _on_disconnect(
|
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:
|
) -> None:
|
||||||
"""Mqtt disconnection callback."""
|
"""Mqtt disconnection callback."""
|
||||||
self.connected = False
|
self.connected = False
|
||||||
@ -152,7 +161,10 @@ class MqttClient(Communicator): # type: ignore[misc]
|
|||||||
|
|
||||||
def _start(self) -> None:
|
def _start(self) -> None:
|
||||||
"""Start mqtt client."""
|
"""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.on_connect = self._on_connect
|
||||||
self.client.will_set(
|
self.client.will_set(
|
||||||
self.mqtt_config.topic_prefix + "/available",
|
self.mqtt_config.topic_prefix + "/available",
|
||||||
|
Loading…
Reference in New Issue
Block a user