fix mqtt switch handling

This commit is contained in:
Blake Blackshear 2021-01-19 07:41:17 -06:00
parent 7f663328dc
commit 1e84f08018

View File

@ -15,7 +15,6 @@ def create_mqtt_client(config: FrigateConfig, camera_metrics):
logger.debug(f"on_clips_toggle: {message.topic} {payload}") logger.debug(f"on_clips_toggle: {message.topic} {payload}")
camera_name = message.topic.split('/')[-3] camera_name = message.topic.split('/')[-3]
command = message.topic.split('/')[-1]
clips_settings = config.cameras[camera_name].clips clips_settings = config.cameras[camera_name].clips
@ -30,16 +29,14 @@ def create_mqtt_client(config: FrigateConfig, camera_metrics):
else: else:
logger.warning(f"Received unsupported value at {message.topic}: {payload}") logger.warning(f"Received unsupported value at {message.topic}: {payload}")
if command == "set": state_topic = f"{message.topic[:-4]}/state"
state_topic = f"{message.topic[:-4]}/state" client.publish(state_topic, payload, retain=True)
client.publish(state_topic, payload, retain=True)
def on_snapshots_command(client, userdata, message): def on_snapshots_command(client, userdata, message):
payload = message.payload.decode() payload = message.payload.decode()
logger.debug(f"on_snapshots_toggle: {message.topic} {payload}") logger.debug(f"on_snapshots_toggle: {message.topic} {payload}")
camera_name = message.topic.split('/')[-3] camera_name = message.topic.split('/')[-3]
command = message.topic.split('/')[-1]
snapshots_settings = config.cameras[camera_name].snapshots snapshots_settings = config.cameras[camera_name].snapshots
@ -54,16 +51,14 @@ def create_mqtt_client(config: FrigateConfig, camera_metrics):
else: else:
logger.warning(f"Received unsupported value at {message.topic}: {payload}") logger.warning(f"Received unsupported value at {message.topic}: {payload}")
if command == "set": state_topic = f"{message.topic[:-4]}/state"
state_topic = f"{message.topic[:-4]}/state" client.publish(state_topic, payload, retain=True)
client.publish(state_topic, payload, retain=True)
def on_detect_command(client, userdata, message): def on_detect_command(client, userdata, message):
payload = message.payload.decode() payload = message.payload.decode()
logger.debug(f"on_detect_toggle: {message.topic} {payload}") logger.debug(f"on_detect_toggle: {message.topic} {payload}")
camera_name = message.topic.split('/')[-3] camera_name = message.topic.split('/')[-3]
command = message.topic.split('/')[-1]
detect_settings = config.cameras[camera_name].detect detect_settings = config.cameras[camera_name].detect
@ -80,9 +75,8 @@ def create_mqtt_client(config: FrigateConfig, camera_metrics):
else: else:
logger.warning(f"Received unsupported value at {message.topic}: {payload}") logger.warning(f"Received unsupported value at {message.topic}: {payload}")
if command == "set": state_topic = f"{message.topic[:-4]}/state"
state_topic = f"{message.topic[:-4]}/state" client.publish(state_topic, payload, retain=True)
client.publish(state_topic, payload, retain=True)
def on_connect(client, userdata, flags, rc): def on_connect(client, userdata, flags, rc):
threading.current_thread().name = "mqtt" threading.current_thread().name = "mqtt"
@ -105,9 +99,9 @@ def create_mqtt_client(config: FrigateConfig, camera_metrics):
# register callbacks # register callbacks
for name in config.cameras.keys(): 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}/clips/set", 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}/snapshots/set", 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}/detect/set", on_detect_command)
if not mqtt_config.user is None: if not mqtt_config.user is None:
client.username_pw_set(mqtt_config.user, password=mqtt_config.password) 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}/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}/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.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}/+/clips/set")
client.subscribe(f"{mqtt_config.topic_prefix}/+/snapshots/#") client.subscribe(f"{mqtt_config.topic_prefix}/+/snapshots/set")
client.subscribe(f"{mqtt_config.topic_prefix}/+/detect/#") client.subscribe(f"{mqtt_config.topic_prefix}/+/detect/set")
return client return client