mirror of
https://github.com/jokob-sk/NetAlertX.git
synced 2026-04-05 01:31:49 -07:00
@@ -731,6 +731,34 @@
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"function": "SEND_NOTIFICATIONS",
|
||||||
|
"type": {
|
||||||
|
"dataType": "boolean",
|
||||||
|
"elements": [
|
||||||
|
{
|
||||||
|
"elementType": "input",
|
||||||
|
"elementOptions": [{ "type": "checkbox" }],
|
||||||
|
"transformers": []
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"default_value": true,
|
||||||
|
"options": [],
|
||||||
|
"localized": ["name", "description"],
|
||||||
|
"name": [
|
||||||
|
{
|
||||||
|
"language_code": "en_us",
|
||||||
|
"string": "Send notifications"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": [
|
||||||
|
{
|
||||||
|
"language_code": "en_us",
|
||||||
|
"string": "Check to send notifications to the broker with json containing events, new_devices, down_devices and more."
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"function": "topic_root",
|
"function": "topic_root",
|
||||||
"type": {
|
"type": {
|
||||||
|
|||||||
@@ -26,7 +26,8 @@ from logger import mylog, Logger # noqa: E402 [flake8 lint suppression]
|
|||||||
from helper import get_setting_value, bytes_to_string, \
|
from helper import get_setting_value, bytes_to_string, \
|
||||||
sanitize_string, normalize_string # noqa: E402 [flake8 lint suppression]
|
sanitize_string, normalize_string # noqa: E402 [flake8 lint suppression]
|
||||||
from database import DB, get_device_stats # noqa: E402 [flake8 lint suppression]
|
from database import DB, get_device_stats # noqa: E402 [flake8 lint suppression]
|
||||||
|
from utils.datetime_utils import timeNowDB # noqa: E402 [flake8 lint suppression]
|
||||||
|
from models.notification_instance import NotificationInstance # noqa: E402 [flake8 lint suppression]
|
||||||
|
|
||||||
# Make sure the TIMEZONE for logging is correct
|
# Make sure the TIMEZONE for logging is correct
|
||||||
conf.tz = timezone(get_setting_value('TIMEZONE'))
|
conf.tz = timezone(get_setting_value('TIMEZONE'))
|
||||||
@@ -459,6 +460,10 @@ def mqtt_start(db):
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Notifications
|
||||||
|
if get_setting_value('MQTT_SEND_NOTIFICATIONS'):
|
||||||
|
publish_notifications(db, mqtt_client)
|
||||||
|
|
||||||
# Generate device-specific MQTT messages if enabled
|
# Generate device-specific MQTT messages if enabled
|
||||||
if get_setting_value('MQTT_SEND_DEVICES'):
|
if get_setting_value('MQTT_SEND_DEVICES'):
|
||||||
|
|
||||||
@@ -536,6 +541,57 @@ def mqtt_start(db):
|
|||||||
publish_mqtt(mqtt_client, sensorConfig.json_attr_topic, devJson)
|
publish_mqtt(mqtt_client, sensorConfig.json_attr_topic, devJson)
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------
|
||||||
|
# Publish webhook-style notifications via MQTT
|
||||||
|
# ---------------------------------------------------------------------
|
||||||
|
def publish_notifications(db, mqtt_client):
|
||||||
|
"""
|
||||||
|
Gather pending notifications and publish a single JSON payload via MQTT
|
||||||
|
that mirrors the webhook structure.
|
||||||
|
Only runs if MQTT_SEND_NOTIFICATIONS is enabled.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Ensure MQTT client is connected
|
||||||
|
if not mqtt_connected_to_broker:
|
||||||
|
mylog('minimal', [f"[{pluginName}] ⚠ ERROR: Not connected to broker, aborting notification publish."])
|
||||||
|
return False
|
||||||
|
|
||||||
|
notifications = NotificationInstance(db).getNew() or []
|
||||||
|
if not notifications:
|
||||||
|
mylog('debug', [f"[{pluginName}] No new notifications to publish via MQTT."])
|
||||||
|
return False
|
||||||
|
|
||||||
|
for notification in notifications:
|
||||||
|
# Use pre-built JSON payload if available
|
||||||
|
if notification.get("Payload"):
|
||||||
|
payload = notification["Payload"]
|
||||||
|
else:
|
||||||
|
# fallback generic payload (like webhook does)
|
||||||
|
payload = {
|
||||||
|
"username": "NetAlertX",
|
||||||
|
"text": "There are new notifications",
|
||||||
|
"attachments": [{
|
||||||
|
"title": "NetAlertX Notifications",
|
||||||
|
"title_link": get_setting_value('REPORT_DASHBOARD_URL'),
|
||||||
|
"text": notification.get("Text") or notification.get("HTML") or ""
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
|
||||||
|
# Optional: attach meta info
|
||||||
|
payload["_meta"] = {
|
||||||
|
"published_at": timeNowDB(),
|
||||||
|
"source": "NetAlertX",
|
||||||
|
"notification_GUID": notification.get("GUID")
|
||||||
|
}
|
||||||
|
|
||||||
|
# Publish to a single MQTT topic
|
||||||
|
topic = f"{topic_root}/notifications/all"
|
||||||
|
mylog('debug', [f"[{pluginName}] Publishing notification GUID {notification.get('GUID')} to MQTT topic {topic}"])
|
||||||
|
publish_mqtt(mqtt_client, topic, payload)
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
# =============================================================================
|
# =============================================================================
|
||||||
# Home Assistant UTILs
|
# Home Assistant UTILs
|
||||||
# =============================================================================
|
# =============================================================================
|
||||||
|
|||||||
Reference in New Issue
Block a user