From 14996d65826310b4cc65e7ec960eb9035de1d847 Mon Sep 17 00:00:00 2001 From: jokob-sk Date: Wed, 2 Oct 2024 16:10:19 +1000 Subject: [PATCH] MQTT_topic_root --- front/plugins/_publisher_mqtt/config.json | 26 ++++++++++++++++++++++- front/plugins/_publisher_mqtt/mqtt.py | 17 ++++++++------- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/front/plugins/_publisher_mqtt/config.json b/front/plugins/_publisher_mqtt/config.json index 754bc2c2..d1bf768e 100755 --- a/front/plugins/_publisher_mqtt/config.json +++ b/front/plugins/_publisher_mqtt/config.json @@ -656,7 +656,7 @@ "description": [ { "language_code": "en_us", - "string": "The root path of the stats overview sensor. Inserted into the system-sensors/sensor/{DEVICE_ID}/state topic." + "string": "The root path of the stats overview sensor. Inserted into the {MQTT_topic_root}/sensor/{MQTT_DEVICE_ID}/state topic." } ] }, @@ -712,6 +712,30 @@ } ] }, + { + "function": "topic_root", + "type": { + "dataType": "string", + "elements": [ + { "elementType": "input", "elementOptions": [], "transformers": [] } + ] + }, + "default_value": "system-sensors", + "options": [], + "localized": ["name", "description"], + "name": [ + { + "language_code": "en_us", + "string": "MQTT topic root" + } + ], + "description": [ + { + "language_code": "en_us", + "string": "The topic root of the devices sensors. Inserted into the {MQTT_topic_root}/sensor/{DEVICE_ID}/state topic." + } + ] + }, { "function": "DEVICES_SQL", "type": { diff --git a/front/plugins/_publisher_mqtt/mqtt.py b/front/plugins/_publisher_mqtt/mqtt.py index d1149a20..fe7b5a9f 100755 --- a/front/plugins/_publisher_mqtt/mqtt.py +++ b/front/plugins/_publisher_mqtt/mqtt.py @@ -49,6 +49,7 @@ pluginName = 'MQTT' mqtt_sensors = [] mqtt_connected_to_broker = False mqtt_client = None # mqtt client +topic_root = get_setting_value('MQTT_topic_root') def main(): @@ -130,7 +131,7 @@ class sensor_config: # Handle 'binary_sensor' or 'sensor' types if self.sensorType in ['binary_sensor', 'sensor']: self.topic = f'homeassistant/{self.sensorType}/{self.deviceId}/{self.sensorName}/config' - self.state_topic = f'system-sensors/{self.sensorType}/{self.deviceId}/state' + self.state_topic = f'{topic_root}/{self.sensorType}/{self.deviceId}/state' self.unique_id = f'{self.deviceId}_sensor_{self.sensorName}' # Update the message dictionary, expanding it without overwriting @@ -151,8 +152,8 @@ class sensor_config: # Handle 'device_tracker' sensor type elif self.sensorType == 'device_tracker': self.topic = f'homeassistant/device_tracker/{self.deviceId}/config' - self.state_topic = f'system-sensors/device_tracker/{self.deviceId}/state' - self.json_attr_topic = f'system-sensors/device_tracker/{self.deviceId}/attributes' + self.state_topic = f'{topic_root}/device_tracker/{self.deviceId}/state' + self.json_attr_topic = f'{topic_root}/device_tracker/{self.deviceId}/attributes' self.unique_id = f'{self.deviceId}_{self.sensorType}_{self.sensorName}' # Construct the message dictionary for device_tracker @@ -414,7 +415,7 @@ def mqtt_start(db): row = get_device_stats(db) # Publish (wrap into {} and remove last ',' from above) - publish_mqtt(mqtt_client, f"system-sensors/sensor/{deviceId}/state", + publish_mqtt(mqtt_client, f"{topic_root}/sensor/{deviceId}/state", { "online": row[0], "down": row[1], @@ -441,10 +442,10 @@ def mqtt_start(db): for device in devices: - # debug statement START 🔻 - if 'Moto' not in device["dev_Name"]: - continue - # debug statement END 🔺 + # # debug statement START 🔻 + # if 'Moto' not in device["dev_Name"]: + # continue + # # debug statement END 🔺 # Create devices in Home Assistant - send config messages deviceId = 'mac_' + device["dev_MAC"].replace(" ", "").replace(":", "_").lower()