Skip to content
Snippets Groups Projects
Commit 59b1def4 authored by Robert Goldmann's avatar Robert Goldmann
Browse files

add support to send notifications to a ntfy server

parent 749472b8
No related branches found
No related tags found
No related merge requests found
......@@ -2,6 +2,7 @@ import os
from datetime import datetime
from typing import Dict, List
from TheCodeLabs_BaseUtils.NtfyHelper import NtfyHelper
from flask import Blueprint
from babel.dates import format_date
......@@ -27,11 +28,18 @@ class GarbageContainerScheduleTile(Tile):
EXAMPLE_SETTINGS = {
"path": "path/to/my/calendar.ics",
"garbageType": "Papier" or "Gelbe Tonne" or "Bioabfall" or "Restabfall",
"notificationViaPushbullet": {
"enable": False,
"notification": {
"daysBeforeEvent": 1,
"hour": 10,
"pushbulletToken": None
"enableNotificationViaPushbullet": False,
"pushbulletToken": None,
"enableNotificationViaNtfy": False,
"ntfySettings": {
"username": "",
"password": "",
"baseUrl": "",
"topicName": ""
}
}
}
......@@ -83,8 +91,8 @@ class GarbageContainerScheduleTile(Tile):
return None
def _send_notification(self, remainingDays: int, nextEventDate: str):
notificationSettings = self._settings['notificationViaPushbullet']
if not notificationSettings['enable']:
notificationSettings = self._settings['notification']
if not notificationSettings['enableNotificationViaPushbullet'] and not notificationSettings['enableNotificationViaNtfy']:
self._lastNotificationDate = None
return
......@@ -104,8 +112,17 @@ class GarbageContainerScheduleTile(Tile):
title = 'DashboardLeaf - Garbage Schedule Notification'
description = f'"{self._settings["garbageType"]}" will be collected in {remainingDays} days ({nextEventDate})'
if notificationSettings['enableNotificationViaPushbullet']:
Helpers.send_notification_via_pushbullet(notificationSettings['pushbulletToken'], title, description)
if notificationSettings['enableNotificationViaNtfy']:
NtfyHelper.send_message(userName=notificationSettings['ntfySettings']['username'],
password=notificationSettings['ntfySettings']['password'],
baseUrl=notificationSettings['ntfySettings']['baseUrl'],
topicName=notificationSettings['ntfySettings']['topicName'],
message=f'{title}\n\n{description}',
tags=['wastebasket'])
def _is_already_notified(self, now: datetime) -> bool:
if self._lastNotificationDate is None:
return False
......
......@@ -5,6 +5,7 @@ import uuid
from datetime import datetime, timedelta
from typing import Dict, Tuple, List
from TheCodeLabs_BaseUtils.NtfyHelper import NtfyHelper
from timeago import format
from TheCodeLabs_BaseUtils.MultiCacheKeyService import MultiCacheKeyService
from flask import Blueprint
......@@ -36,7 +37,14 @@ class SensorLineChartTile(Tile):
"enable": False,
"limitInSeconds": 300,
"enableNotificationViaPushbullet": False,
"pushbulletToken": None
"pushbulletToken": None,
"enableNotificationViaNtfy": False,
"ntfySettings": {
"username": "",
"password": "",
"baseUrl": "",
"topicName": ""
}
}
}
......@@ -222,7 +230,7 @@ class SensorLineChartTile(Tile):
return timeAgo
def _send_notification(self, warningSettings: Dict, sensorInfo: Dict, deviceInfo: Dict, timeSinceLastValue: str):
if not warningSettings['enableNotificationViaPushbullet']:
if not warningSettings['enableNotificationViaPushbullet'] and not warningSettings['enableNotificationViaNtfy']:
return
if not timeSinceLastValue:
......@@ -232,7 +240,6 @@ class SensorLineChartTile(Tile):
if self._notificationSent:
return
token = warningSettings['pushbulletToken']
sensorName = sensorInfo['name']
sensorType = sensorInfo['type']
......@@ -242,7 +249,19 @@ class SensorLineChartTile(Tile):
description = f'Last value for sensor "{sensorName}" received {timeSinceLastValue} ' \
f'(type: {sensorType}, device: {deviceName})'
if warningSettings['enableNotificationViaPushbullet']:
token = warningSettings['pushbulletToken']
Helpers.send_notification_via_pushbullet(token, title, description)
if warningSettings['enableNotificationViaNtfy']:
NtfyHelper.send_message(userName=warningSettings['ntfySettings']['username'],
password=warningSettings['ntfySettings']['password'],
baseUrl=warningSettings['ntfySettings']['baseUrl'],
topicName=warningSettings['ntfySettings']['topicName'],
message=f'{title}\n\n{description}',
tags=['warning'])
self._notificationSent = True
def __format_date(self, dateTime: str):
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment