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

#45 - SensorLineChartTile: option to send notification via pushbullet if a...

#45 - SensorLineChartTile: option to send notification via pushbullet if a sensor has an outdated value
parent 8c6513f5
No related branches found
No related tags found
No related merge requests found
......@@ -22,7 +22,9 @@
"showAxes": false,
"outdatedValueWarning": {
"enable": false,
"limitInSeconds": -1
"limitInSeconds": -1,
"enableNotificationViaPushbullet": false,
"pushbulletToken": null
}
},
"x": 0,
......
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}")')
......@@ -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()
}
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment