From 060bf3ee8a510ba2b8afb13102b89e2779420d9b Mon Sep 17 00:00:00 2001 From: Robert Goldmann <deadlocker@gmx.de> Date: Fri, 12 Feb 2021 00:01:06 +0100 Subject: [PATCH] #45 - SensorLineChartTile: option to send notification via pushbullet if a sensor has an outdated value --- config/pageSettings-example.json | 4 +++- src/logic/Helpers.py | 22 +++++++++++++++++++ .../service/services/StorageLeafService.py | 6 +++++ src/logic/tile/tiles/SensorLineChartTile.py | 20 ++++++++++++++++- 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/config/pageSettings-example.json b/config/pageSettings-example.json index d957d27..3957142 100644 --- a/config/pageSettings-example.json +++ b/config/pageSettings-example.json @@ -22,7 +22,9 @@ "showAxes": false, "outdatedValueWarning": { "enable": false, - "limitInSeconds": -1 + "limitInSeconds": -1, + "enableNotificationViaPushbullet": false, + "pushbulletToken": null } }, "x": 0, diff --git a/src/logic/Helpers.py b/src/logic/Helpers.py index 14ea772..f5a1010 100644 --- a/src/logic/Helpers.py +++ b/src/logic/Helpers.py @@ -1,6 +1,8 @@ +import json from datetime import datetime import pytz +import requests from logic import Constants @@ -61,3 +63,23 @@ def is_dayTime(sunrise: datetime, sunset: datetime, currentTimestamp: datetime) def timestamp_to_timezone(timestamp: int, timeZone: datetime.tzinfo) -> datetime: timestamp = datetime.fromtimestamp(timestamp, pytz.timezone('UTC')) return timestamp.astimezone(timeZone) + + +PUSHBULLET_PUSH_URL = 'https://api.pushbullet.com/v2/pushes' + + +def send_notification_via_pushbullet(token: str, title: str, body: str): + data = { + 'type': 'note', + 'title': title, + 'body': body + } + + headers = { + 'Authorization': f'Bearer {token}', + 'Content-Type': 'application/json' + } + + response = requests.post(PUSHBULLET_PUSH_URL, data=json.dumps(data), headers=headers) + if response.status_code != 200: + raise Exception(f'Error sending notification via pushbullet (status code: "{response.status_code}")') diff --git a/src/logic/service/services/StorageLeafService.py b/src/logic/service/services/StorageLeafService.py index 75c9082..feb537f 100644 --- a/src/logic/service/services/StorageLeafService.py +++ b/src/logic/service/services/StorageLeafService.py @@ -54,11 +54,17 @@ class StorageLeafService(MultiCacheKeyService): if sensorInfoResponse.status_code != 200: raise Exception(f'Invalid status code: {sensorInfoResponse.status_code}') + urlDeviceInfo = Helpers.join_url_parts(settings['url'], 'device', str(sensorInfoResponse.json()['device_id'])) + deviceInfoResponse = requests.get(urlDeviceInfo) + if deviceInfoResponse.status_code != 200: + raise Exception(f'Invalid status code: {deviceInfoResponse.status_code}') + sensorValueResponse = requests.get(urlSensorValue) if sensorValueResponse.status_code != 200: raise Exception(f'Invalid status code: {sensorValueResponse.status_code}') return { + 'deviceInfo': deviceInfoResponse.json(), 'sensorInfo': sensorInfoResponse.json(), 'sensorValue': sensorValueResponse.json() } diff --git a/src/logic/tile/tiles/SensorLineChartTile.py b/src/logic/tile/tiles/SensorLineChartTile.py index cef3315..bc6c538 100644 --- a/src/logic/tile/tiles/SensorLineChartTile.py +++ b/src/logic/tile/tiles/SensorLineChartTile.py @@ -34,7 +34,9 @@ class SensorLineChartTile(Tile): "showAxes": True, "outdatedValueWarning": { "enable": False, - "limitInSeconds": 300 + "limitInSeconds": 300, + "enableNotificationViaPushbullet": False, + "pushbulletToken": None } } @@ -86,6 +88,7 @@ class SensorLineChartTile(Tile): 'x': x, 'y': y, 'sensorInfo': sensorData['sensorInfo'], + 'deviceInfo': sensorData['deviceInfo'], 'min': minValue, 'max': maxValue, 'ghostTraceX': ghostTraceX, @@ -181,6 +184,8 @@ class SensorLineChartTile(Tile): warningSettings = self._settings['outdatedValueWarning'] timeSinceLastValue = self.__get_time_since_last_value(warningSettings, data) + if warningSettings['enableNotificationViaPushbullet']: + self.__send_notification(warningSettings, data['sensorInfo'], data['deviceInfo'], timeSinceLastValue) return Tile.render_template(os.path.dirname(__file__), __class__.__name__, x=data['x'], @@ -217,6 +222,19 @@ class SensorLineChartTile(Tile): return timeAgo + def __send_notification(self, warningSettings: Dict, sensorInfo: Dict, deviceInfo: Dict, timeSinceLastValue: str): + token = warningSettings['pushbulletToken'] + + sensorName = sensorInfo['name'] + sensorType = sensorInfo['type'] + deviceName = deviceInfo['name'] + + title = 'DashboardLeaf - Outdated Value Warning' + description = f'Last value for sensor "{sensorName}" received {timeSinceLastValue} ' \ + f'(type: {sensorType}, device: {deviceName})' + + Helpers.send_notification_via_pushbullet(token, title, description) + def __format_date(self, dateTime: str): parsedDateTime = datetime.strptime(dateTime, self.DATE_FORMAT) return datetime.strftime(parsedDateTime, self.DATE_FORMAT_CHART) -- GitLab