From 157b2307029231c0709f777dc2b71d2b0181dcc5 Mon Sep 17 00:00:00 2001 From: Blake Blackshear Date: Wed, 23 Dec 2020 22:16:36 -0600 Subject: [PATCH] turn off snapshots via mqtt --- frigate/mqtt.py | 43 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/frigate/mqtt.py b/frigate/mqtt.py index 5d79c334e..9d44304b0 100644 --- a/frigate/mqtt.py +++ b/frigate/mqtt.py @@ -17,10 +17,40 @@ def create_mqtt_client(config: FrigateConfig): camera_name = message.topic.split('/')[-3] command = message.topic.split('/')[-1] + clips_settings = config.cameras[camera_name].clips + if payload == 'ON': - config.cameras[camera_name].clips._enabled = True + if not clips_settings.enabled: + logger.info(f"Turning on clips for {camera_name} via mqtt") + clips_settings._enabled = True elif payload == 'OFF': - config.cameras[camera_name].clips._enabled = False + if clips_settings.enabled: + logger.info(f"Turning off clips for {camera_name} via mqtt") + clips_settings._enabled = False + 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) + + 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 + + if payload == 'ON': + if not snapshots_settings.enabled: + logger.info(f"Turning on snapshots for {camera_name} via mqtt") + snapshots_settings._enabled = True + elif payload == 'OFF': + if snapshots_settings.enabled: + logger.info(f"Turning off snapshots for {camera_name} via mqtt") + snapshots_settings._enabled = False else: logger.warning(f"Received unsupported value at {message.topic}: {payload}") @@ -49,8 +79,8 @@ def create_mqtt_client(config: FrigateConfig): # register callbacks for name in config.cameras.keys(): - clips_topic = f"{mqtt_config.topic_prefix}/{name}/clips/#" - client.message_callback_add(clips_topic, on_clips_command) + 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) if not mqtt_config.user is None: client.username_pw_set(mqtt_config.user, password=mqtt_config.password) @@ -61,8 +91,7 @@ def create_mqtt_client(config: FrigateConfig): raise client.loop_start() - - clips_topic = f"{mqtt_config.topic_prefix}/+/clips/#" - client.subscribe(clips_topic) + client.subscribe(f"{mqtt_config.topic_prefix}/+/clips/#") + client.subscribe(f"{mqtt_config.topic_prefix}/+/snapshots/#") return client