From 95d5dbcf68c0efe4a2c2c5814ce727844e589b6a Mon Sep 17 00:00:00 2001 From: jokob-sk Date: Tue, 9 Jul 2024 23:09:42 +1000 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=8C=20MQTT=20-=20do=20not=20send=20UNK?= =?UTF-8?q?NOWN=20+=20expose=20DEVICES=5FSQL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dockerfiles/README.md | 2 +- front/plugins/_publisher_mqtt/config.json | 26 ++++++++++++++++++++++- front/plugins/_publisher_mqtt/mqtt.py | 7 +++--- server/helper.py | 8 +++++-- 4 files changed, 36 insertions(+), 7 deletions(-) diff --git a/dockerfiles/README.md b/dockerfiles/README.md index 8eee700d..d8465a66 100755 --- a/dockerfiles/README.md +++ b/dockerfiles/README.md @@ -85,7 +85,7 @@ These are the most important settings to get at least some output in your Device Use the official installation guides at first and use community content as supplementary material. Open an issue if you'd like to add your link to the list 🙏 -- ▶ [Home Lab Network Monitoring - Scotti-BYTE Enterprise Consulting Services](https://www.youtube.com/watch?v=0DryhzrQSJA) +- ▶ [Home Lab Network Monitoring - Scotti-BYTE Enterprise Consulting Services](https://www.youtube.com/watch?v=0DryhzrQSJA) (July 2024) - 📄 [How to Install NetAlertX on Your Synology NAS - Marius hosting](https://mariushosting.com/how-to-install-pi-alert-on-your-synology-nas/) (Updated frequently) - 📄 [Using the PiAlert Network Security Scanner on a Raspberry Pi - PiMyLifeUp](https://pimylifeup.com/raspberry-pi-pialert/) - ▶ [How to Setup Pi.Alert on Your Synology NAS - Digital Aloha](https://www.youtube.com/watch?v=M4YhpuRFaUg) diff --git a/front/plugins/_publisher_mqtt/config.json b/front/plugins/_publisher_mqtt/config.json index 43ff1e70..4d6932a0 100755 --- a/front/plugins/_publisher_mqtt/config.json +++ b/front/plugins/_publisher_mqtt/config.json @@ -25,7 +25,7 @@ "description": [ { "language_code": "en_us", - "string": "A plugin to publish a notification via the Apprise gateway." + "string": "A plugin to publish a notification via MQTT to Home Assistant." } ], "params": [ @@ -671,6 +671,30 @@ } ] }, + { + "function": "DEVICES_SQL", + "type": { + "dataType": "string", + "elements": [ + { "elementType": "input", "elementOptions": [], "transformers": [] } + ] + }, + "default_value": "select rowid, * from Devices where dev_Name not in ({s-quote}null{s-quote}, {s-quote}(name not found){s-quote}, {s-quote}(unknown){s-quote})", + "options": [], + "localized": ["name", "description"], + "name": [ + { + "language_code": "en_us", + "string": "Device SQL" + } + ], + "description": [ + { + "language_code": "en_us", + "string": "The SQL condition used to select which Devices are sent via MQTT. Please note that you need to use {s-quote} for single quotes replacement and double-quotes are not supported." + } + ] + }, { "function": "QOS", "type": { diff --git a/front/plugins/_publisher_mqtt/mqtt.py b/front/plugins/_publisher_mqtt/mqtt.py index 8f54efe3..b562af36 100755 --- a/front/plugins/_publisher_mqtt/mqtt.py +++ b/front/plugins/_publisher_mqtt/mqtt.py @@ -13,6 +13,7 @@ import paho.mqtt.client as mqtt # from paho.mqtt import client as mqtt_client # from paho.mqtt import CallbackAPIVersion as mqtt_CallbackAPIVersion import hashlib +import sqlite3 # Register NetAlertX directories @@ -27,7 +28,7 @@ from plugin_helper import Plugin_Objects from logger import mylog, append_line_to_file from helper import timeNowTZ, get_setting_value, bytes_to_string, sanitize_string from notification import Notification_obj -from database import DB, get_all_devices, get_device_stats +from database import DB, get_device_stats CUR_PATH = str(pathlib.Path(__file__).parent.resolve()) @@ -340,10 +341,10 @@ def mqtt_start(db): # Generate device-specific MQTT messages if enabled if get_setting_value('MQTT_SEND_DEVICES') == True: - # Specific devices + # Specific devices processing # Get all devices - devices = get_all_devices(db) + devices = db.read(get_setting_value('MQTT_DEVICES_SQL').replace('{s-quote}',"'")) sec_delay = len(devices) * int(get_setting_value('MQTT_DELAY_SEC'))*5 diff --git a/server/helper.py b/server/helper.py index 9a7f4040..e15c2e41 100755 --- a/server/helper.py +++ b/server/helper.py @@ -325,7 +325,6 @@ def setting_value_to_python_type(set_type, set_value): # "type": {"dataType":"array", "elements": [{"elementType" : "select", "elementOptions" : [{"multiple":"true"}] ,"transformers": []}]} - setTypJSN = json.loads(str(set_type).replace('"','\"').replace("'",'"')) # Handle different types of settings based on set_type dictionary @@ -356,15 +355,20 @@ def setting_value_to_python_type(set_type, set_value): if dataType == 'string' and elementType in ['input', 'select']: value = str(set_value) - elif dataType == 'integer' and elementType == 'input': + elif dataType == 'integer' and (elementType == 'input' or elementType == 'select'): # handle storing/retrieving boolean values as 1/0 + if set_value.lower() not in ['true', 'false'] and isinstance(set_value, str): value = int(set_value) + elif isinstance(set_value, bool): value = 1 if set_value else 0 + elif isinstance(set_value, str): + value = 1 if set_value.lower() == 'true' else 0 else: + value = int(set_value) elif dataType == 'boolean' and elementType == 'input':