From 02cb5b55ed041a5e567ea4d74532b540194307c8 Mon Sep 17 00:00:00 2001 From: Nick Mowen Date: Tue, 22 Nov 2022 14:02:45 -0700 Subject: [PATCH] Don't start mqtt until dispatcher is subscribed --- frigate/communication/mqtt.py | 72 +++++++++++++++++------------------ 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/frigate/communication/mqtt.py b/frigate/communication/mqtt.py index ed2790598..c85a09ca3 100644 --- a/frigate/communication/mqtt.py +++ b/frigate/communication/mqtt.py @@ -18,46 +18,11 @@ class MqttClient(Communicator): self.config = config self.mqtt_config = config.mqtt self.connected: bool = False - self._start() def subscribe(self, receiver) -> None: """Wrapper for allowing dispatcher to subscribe.""" self._dispatcher = receiver - - # register callbacks - for name in self.config.cameras.keys(): - self.client.message_callback_add( - f"{self.mqtt_config.topic_prefix}/{name}/recordings/set", - self.on_mqtt_command, - ) - self.client.message_callback_add( - f"{self.mqtt_config.topic_prefix}/{name}/snapshots/set", - self.on_mqtt_command, - ) - self.client.message_callback_add( - f"{self.mqtt_config.topic_prefix}/{name}/detect/set", - self.on_mqtt_command, - ) - self.client.message_callback_add( - f"{self.mqtt_config.topic_prefix}/{name}/motion/set", - self.on_mqtt_command, - ) - self.client.message_callback_add( - f"{self.mqtt_config.topic_prefix}/{name}/improve_contrast/set", - self.on_mqtt_command, - ) - self.client.message_callback_add( - f"{self.mqtt_config.topic_prefix}/{name}/motion_threshold/set", - self.on_mqtt_command, - ) - self.client.message_callback_add( - f"{self.mqtt_config.topic_prefix}/{name}/motion_contour_area/set", - self.on_mqtt_command, - ) - - self.client.message_callback_add( - f"{self.mqtt_config.topic_prefix}/restart", self.on_mqtt_command - ) + self._start() def publish(self, topic: str, payload, retain: bool = False) -> None: """Wrapper for publishing when client is in valid state.""" @@ -166,6 +131,41 @@ class MqttClient(Communicator): retain=True, ) + # register callbacks + for name in self.config.cameras.keys(): + self.client.message_callback_add( + f"{self.mqtt_config.topic_prefix}/{name}/recordings/set", + self.on_mqtt_command, + ) + self.client.message_callback_add( + f"{self.mqtt_config.topic_prefix}/{name}/snapshots/set", + self.on_mqtt_command, + ) + self.client.message_callback_add( + f"{self.mqtt_config.topic_prefix}/{name}/detect/set", + self.on_mqtt_command, + ) + self.client.message_callback_add( + f"{self.mqtt_config.topic_prefix}/{name}/motion/set", + self.on_mqtt_command, + ) + self.client.message_callback_add( + f"{self.mqtt_config.topic_prefix}/{name}/improve_contrast/set", + self.on_mqtt_command, + ) + self.client.message_callback_add( + f"{self.mqtt_config.topic_prefix}/{name}/motion_threshold/set", + self.on_mqtt_command, + ) + self.client.message_callback_add( + f"{self.mqtt_config.topic_prefix}/{name}/motion_contour_area/set", + self.on_mqtt_command, + ) + + self.client.message_callback_add( + f"{self.mqtt_config.topic_prefix}/restart", self.on_mqtt_command + ) + if not self.mqtt_config.tls_ca_certs is None: if ( not self.mqtt_config.tls_client_cert is None