From 1e84f08018fe8c1cf1f3afee155e219510e0e5b8 Mon Sep 17 00:00:00 2001 From: Blake Blackshear Date: Tue, 19 Jan 2021 07:41:17 -0600 Subject: [PATCH] fix mqtt switch handling --- frigate/mqtt.py | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/frigate/mqtt.py b/frigate/mqtt.py index 5f4a3a2e0..608c62f19 100644 --- a/frigate/mqtt.py +++ b/frigate/mqtt.py @@ -15,7 +15,6 @@ def create_mqtt_client(config: FrigateConfig, camera_metrics): logger.debug(f"on_clips_toggle: {message.topic} {payload}") camera_name = message.topic.split('/')[-3] - command = message.topic.split('/')[-1] clips_settings = config.cameras[camera_name].clips @@ -30,16 +29,14 @@ def create_mqtt_client(config: FrigateConfig, camera_metrics): else: logger.warning(f"Received unsupported value at {message.topic}: {payload}") - if command == "set": - state_topic = f"{message.topic[:-4]}/state" - client.publish(state_topic, payload, retain=True) + state_topic = f"{message.topic[:-4]}/state" + client.publish(state_topic, payload, retain=True) def on_snapshots_command(client, userdata, message): payload = message.payload.decode() logger.debug(f"on_snapshots_toggle: {message.topic} {payload}") camera_name = message.topic.split('/')[-3] - command = message.topic.split('/')[-1] snapshots_settings = config.cameras[camera_name].snapshots @@ -54,16 +51,14 @@ def create_mqtt_client(config: FrigateConfig, camera_metrics): else: logger.warning(f"Received unsupported value at {message.topic}: {payload}") - if command == "set": - state_topic = f"{message.topic[:-4]}/state" - client.publish(state_topic, payload, retain=True) + state_topic = f"{message.topic[:-4]}/state" + client.publish(state_topic, payload, retain=True) def on_detect_command(client, userdata, message): payload = message.payload.decode() logger.debug(f"on_detect_toggle: {message.topic} {payload}") camera_name = message.topic.split('/')[-3] - command = message.topic.split('/')[-1] detect_settings = config.cameras[camera_name].detect @@ -80,9 +75,8 @@ def create_mqtt_client(config: FrigateConfig, camera_metrics): else: logger.warning(f"Received unsupported value at {message.topic}: {payload}") - if command == "set": - state_topic = f"{message.topic[:-4]}/state" - client.publish(state_topic, payload, retain=True) + state_topic = f"{message.topic[:-4]}/state" + client.publish(state_topic, payload, retain=True) def on_connect(client, userdata, flags, rc): threading.current_thread().name = "mqtt" @@ -105,9 +99,9 @@ def create_mqtt_client(config: FrigateConfig, camera_metrics): # register callbacks for name in config.cameras.keys(): - client.message_callback_add(f"{mqtt_config.topic_prefix}/{name}/clips/#", on_clips_command) - client.message_callback_add(f"{mqtt_config.topic_prefix}/{name}/snapshots/#", on_snapshots_command) - client.message_callback_add(f"{mqtt_config.topic_prefix}/{name}/detect/#", on_detect_command) + client.message_callback_add(f"{mqtt_config.topic_prefix}/{name}/clips/set", on_clips_command) + client.message_callback_add(f"{mqtt_config.topic_prefix}/{name}/snapshots/set", on_snapshots_command) + client.message_callback_add(f"{mqtt_config.topic_prefix}/{name}/detect/set", on_detect_command) if not mqtt_config.user is None: client.username_pw_set(mqtt_config.user, password=mqtt_config.password) @@ -123,9 +117,9 @@ def create_mqtt_client(config: FrigateConfig, camera_metrics): client.publish(f"{mqtt_config.topic_prefix}/{name}/clips/state", 'ON' if config.cameras[name].clips.enabled else 'OFF', retain=True) client.publish(f"{mqtt_config.topic_prefix}/{name}/snapshots/state", 'ON' if config.cameras[name].clips.enabled else 'OFF', retain=True) client.publish(f"{mqtt_config.topic_prefix}/{name}/detect/state", 'ON' if config.cameras[name].clips.enabled else 'OFF', retain=True) - - client.subscribe(f"{mqtt_config.topic_prefix}/+/clips/#") - client.subscribe(f"{mqtt_config.topic_prefix}/+/snapshots/#") - client.subscribe(f"{mqtt_config.topic_prefix}/+/detect/#") + + client.subscribe(f"{mqtt_config.topic_prefix}/+/clips/set") + client.subscribe(f"{mqtt_config.topic_prefix}/+/snapshots/set") + client.subscribe(f"{mqtt_config.topic_prefix}/+/detect/set") return client