From 44f0ba0924306a60e7685218d3f49c4766000a31 Mon Sep 17 00:00:00 2001 From: Jokob-sk Date: Sat, 6 Jan 2024 12:09:59 +1100 Subject: [PATCH] =?UTF-8?q?MQTT=20settings=20to=20select=20what=20is=20sen?= =?UTF-8?q?d=20#364=F0=9F=93=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- front/plugins/_publisher_mqtt/config.json | 36 ++++++- front/plugins/_publisher_mqtt/mqtt.py | 112 +++++++++++----------- pialert/initialise.py | 1 + 3 files changed, 94 insertions(+), 55 deletions(-) diff --git a/front/plugins/_publisher_mqtt/config.json b/front/plugins/_publisher_mqtt/config.json index 1f33a5d6..c965fc13 100755 --- a/front/plugins/_publisher_mqtt/config.json +++ b/front/plugins/_publisher_mqtt/config.json @@ -267,7 +267,7 @@ "description": [ { "language_code": "en_us", - "string" : "Enable sending notifications via MQTT to your Home Assistance instance. Usually, on_notification is recommended." + "string" : "Enable sending notifications via MQTT to your Home Assistance instance. Usually, on_notification is recommended. See the PiAlert Home Assistant guide for details." }, { "language_code": "es_es", @@ -493,6 +493,40 @@ "language_code": "es_es", "string" : "Un pequeño truco: retrase la adición a la cola en caso de que el proceso se reinicie y los procesos de publicación anteriores se anulen (se necesitan ~2s para actualizar la configuración de un sensor en el intermediario). Probado con 2-3 segundos de retraso. Este retraso solo se aplica cuando se crean dispositivos (durante el primer bucle de notificación). No afecta los escaneos o notificaciones posteriores." }] + }, + { + "function": "SEND_STATS", + "type": "boolean", + "default_value":true, + "options": [], + "localized": ["name", "description"], + "name" : [{ + "language_code":"en_us", + "string" : "Send stats" + } + ], + "description": [{ + "language_code":"en_us", + "string" : "Check to send overal device stats, such as number of Online and Offline devices." + } + ] + }, + { + "function": "SEND_DEVICES", + "type": "boolean", + "default_value":true, + "options": [], + "localized": ["name", "description"], + "name" : [{ + "language_code":"en_us", + "string" : "Send devices" + } + ], + "description": [{ + "language_code":"en_us", + "string" : "Check to send individual devices to the broker with details, such as is_new, is_present, or mac_address of the devices." + } + ] } ] } diff --git a/front/plugins/_publisher_mqtt/mqtt.py b/front/plugins/_publisher_mqtt/mqtt.py index 5bc801bc..09734721 100755 --- a/front/plugins/_publisher_mqtt/mqtt.py +++ b/front/plugins/_publisher_mqtt/mqtt.py @@ -257,76 +257,80 @@ def mqtt_start(db): # General stats # Create a generic device for overal stats - create_generic_device(client) + if get_setting_value('MQTT_SEND_STATS') == True: + # Create a new device representing overall PiAlert stats + create_generic_device(client) - # Get the data - row = get_device_stats(db) + # Get the data + row = get_device_stats(db) - columns = ["online","down","all","archived","new","unknown"] + columns = ["online","down","all","archived","new","unknown"] - payload = "" + payload = "" - # Update the values - for column in columns: - payload += '"'+column+'": ' + str(row[column]) +',' + # Update the values + for column in columns: + payload += '"'+column+'": ' + str(row[column]) +',' - # Publish (warap into {} and remove last ',' from above) - publish_mqtt(client, "system-sensors/sensor/pialert/state", - '{ \ - '+ payload[:-1] +'\ - }' - ) + # Publish (wrap into {} and remove last ',' from above) + publish_mqtt(client, "system-sensors/sensor/pialert/state", + '{ \ + '+ payload[:-1] +'\ + }' + ) + # Generate device-specific MQTT messages if enabled + if get_setting_value('MQTT_SEND_DEVICES') == True: - # Specific devices + # Specific devices - # Get all devices - devices = get_all_devices(db) + # Get all devices + devices = get_all_devices(db) - sec_delay = len(devices) * int(get_setting_value('MQTT_DELAY_SEC'))*5 + sec_delay = len(devices) * int(get_setting_value('MQTT_DELAY_SEC'))*5 - mylog('minimal', [f"[{pluginName}] Estimated delay: ", (sec_delay), 's ', '(', round(sec_delay/60,1) , 'min)' ]) + mylog('minimal', [f"[{pluginName}] Estimated delay: ", (sec_delay), 's ', '(', round(sec_delay/60,1) , 'min)' ]) - - for device in devices: + + for device in devices: - - # Create devices in Home Assistant - send config messages - deviceId = 'mac_' + device["dev_MAC"].replace(" ", "").replace(":", "_").lower() - deviceNameDisplay = re.sub('[^a-zA-Z0-9-_\s]', '', device["dev_Name"]) + + # Create devices in Home Assistant - send config messages + deviceId = 'mac_' + device["dev_MAC"].replace(" ", "").replace(":", "_").lower() + deviceNameDisplay = re.sub('[^a-zA-Z0-9-_\s]', '', device["dev_Name"]) - create_sensor(client, deviceId, deviceNameDisplay, 'sensor', 'last_ip', 'ip-network', device["dev_MAC"]) - create_sensor(client, deviceId, deviceNameDisplay, 'binary_sensor', 'is_present', 'wifi', device["dev_MAC"]) - create_sensor(client, deviceId, deviceNameDisplay, 'sensor', 'mac_address', 'folder-key-network', device["dev_MAC"]) - create_sensor(client, deviceId, deviceNameDisplay, 'sensor', 'is_new', 'bell-alert-outline', device["dev_MAC"]) - create_sensor(client, deviceId, deviceNameDisplay, 'sensor', 'vendor', 'cog', device["dev_MAC"]) - - # update device sensors in home assistant + create_sensor(client, deviceId, deviceNameDisplay, 'sensor', 'last_ip', 'ip-network', device["dev_MAC"]) + create_sensor(client, deviceId, deviceNameDisplay, 'binary_sensor', 'is_present', 'wifi', device["dev_MAC"]) + create_sensor(client, deviceId, deviceNameDisplay, 'sensor', 'mac_address', 'folder-key-network', device["dev_MAC"]) + create_sensor(client, deviceId, deviceNameDisplay, 'sensor', 'is_new', 'bell-alert-outline', device["dev_MAC"]) + create_sensor(client, deviceId, deviceNameDisplay, 'sensor', 'vendor', 'cog', device["dev_MAC"]) + + # update device sensors in home assistant - publish_mqtt(client, 'system-sensors/sensor/'+deviceId+'/state', - '{ \ - "last_ip": "' + device["dev_LastIP"] +'", \ - "is_new": "' + str(device["dev_NewDevice"]) +'", \ - "vendor": "' + sanitize_string(device["dev_Vendor"]) +'", \ - "mac_address": "' + str(device["dev_MAC"]) +'" \ - }' - ) + publish_mqtt(client, 'system-sensors/sensor/'+deviceId+'/state', + '{ \ + "last_ip": "' + device["dev_LastIP"] +'", \ + "is_new": "' + str(device["dev_NewDevice"]) +'", \ + "vendor": "' + sanitize_string(device["dev_Vendor"]) +'", \ + "mac_address": "' + str(device["dev_MAC"]) +'" \ + }' + ) - publish_mqtt(client, 'system-sensors/binary_sensor/'+deviceId+'/state', - '{ \ - "is_present": "' + to_binary_sensor(str(device["dev_PresentLastScan"])) +'"\ - }' - ) + publish_mqtt(client, 'system-sensors/binary_sensor/'+deviceId+'/state', + '{ \ + "is_present": "' + to_binary_sensor(str(device["dev_PresentLastScan"])) +'"\ + }' + ) - # delete device / topic - # homeassistant/sensor/mac_44_ef_bf_c4_b1_af/is_present/config - # client.publish( - # topic="homeassistant/sensor/"+deviceId+"/is_present/config", - # payload="", - # qos=1, - # retain=True, - # ) - # time.sleep(10) + # delete device / topic + # homeassistant/sensor/mac_44_ef_bf_c4_b1_af/is_present/config + # client.publish( + # topic="homeassistant/sensor/"+deviceId+"/is_present/config", + # payload="", + # qos=1, + # retain=True, + # ) + # time.sleep(10) #=============================================================================== diff --git a/pialert/initialise.py b/pialert/initialise.py index 9bde256d..e0bde510 100755 --- a/pialert/initialise.py +++ b/pialert/initialise.py @@ -248,6 +248,7 @@ def read_config_file(filename): #------------------------------------------------------------------------------- # DEPERECATED soonest after 3/3/2024 +# 🤔Idea/TODO: Check and compare versions/timestamps amd only perform a replacement if config/version older than... replacements = { r'\bREPORT_TO\b': 'SMTP_REPORT_TO', r'\bREPORT_FROM\b': 'SMTP_REPORT_FROM',