Skip to content

Commit 07bf6c4

Browse files
committed
added shared config
1 parent f039272 commit 07bf6c4

18 files changed

+2598
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
from openhab.jsr223 import scope
2+
from configuration import userConfigs
3+
4+
5+
class NotificationHelper:
6+
PRIORITY_INFO = 1
7+
PRIORITY_NOTICE = 2
8+
PRIORITY_WARN = 3
9+
PRIORITY_ERROR = 4
10+
PRIORITY_ALERT = 5
11+
12+
# *** Notifications ***
13+
@staticmethod
14+
def _sendNotification(notification_config, notification_type, mapped_sound, mapped_priority, action, header, message, url=None, retry = 1 ):
15+
success = False
16+
if url == None:
17+
if notification_type == "telegram":
18+
success = action.sendTelegram("*" + header + "*: " + message)
19+
elif notification_type == "pushover":
20+
success = action.sendMessage(message, header, mapped_sound, None, None, None, None, mapped_priority, notification_config[2], None )
21+
else:
22+
log.error("Unknown notification type {}".format(notification_type))
23+
success = None
24+
else:
25+
if notification_type == "telegram":
26+
success = action.sendTelegramPhoto(url,"*" + header + "*: " + message)
27+
elif notification_type == "pushover":
28+
#sendMessage(String message, @Nullable String title, @Nullable String sound, @Nullable String url, @Nullable String urlTitle, @Nullable String attachment, @Nullable String contentType, @Nullable Integer priority, @Nullable String device, @Nullable Duration ttl)
29+
success = action.sendMessage(message, header, mapped_sound, None, None, url, None, mapped_priority, notification_config[2], None )
30+
else:
31+
log.error("Unknown notification type {}".format(notification_type))
32+
success = None
33+
34+
if not success and success is not None and retry < 5:
35+
waiting_time = retry * 5
36+
log.info("Failed to send message '{}: {}'. Retry in {} seconds.".format(header, message, waiting_time))
37+
time.sleep(waiting_time)
38+
success = NotificationHelper._sendNotification(notification_config, notification_type, mapped_sound, mapped_priority, action, header, message, url, retry + 1)
39+
40+
return bool(success)
41+
42+
@staticmethod
43+
def sendNotification(priority, header, message, url=None, recipients = None):
44+
chars = {
45+
"_": "\\_"
46+
}
47+
for k,v in chars.items():
48+
message = message.replace(k,v)
49+
50+
if recipients is None:
51+
recipients = []
52+
for userName in userConfigs:
53+
if not userConfigs[userName]["notification_config"]:
54+
continue
55+
recipients.append(userName)
56+
57+
for recipient in recipients:
58+
notification_config = userConfigs[recipient]["notification_config"]
59+
notification_type = notification_config[0]
60+
notification_thing = notification_config[1]
61+
62+
action = scope.get("actions").get(notification_type, notification_thing)
63+
64+
if action is None:
65+
log.warn("No Action found, Type: '{}', Thing: '{}'".format(notification_type, notification_thing))
66+
continue
67+
68+
if notification_type == "pushover":
69+
# mapping to pushover priorities
70+
if priority == NotificationHelper.PRIORITY_INFO:
71+
mapped_priority = 0
72+
mapped_sound = "magic"
73+
elif priority == NotificationHelper.PRIORITY_NOTICE:
74+
mapped_priority = 0
75+
mapped_sound = "gamelan"
76+
elif priority == NotificationHelper.PRIORITY_WARN or priority == NotificationHelper.PRIORITY_ERROR:
77+
mapped_priority = 1
78+
mapped_sound = "intermission"
79+
else:
80+
mapped_priority = 2
81+
mapped_sound = "echo"
82+
else:
83+
mapped_priority = 0
84+
mapped_sound = None
85+
86+
success = NotificationHelper._sendNotification(notification_config, notification_type, mapped_sound, mapped_priority, action, header, message, url)
87+
if not success:
88+
caller = getframeinfo(stack()[1][0])
89+
log.error("Failed to send message '{}: {}' from {}:{}".format(header, message, caller.filename, caller.lineno))
90+
91+
@staticmethod
92+
def sendNotificationToAllAdmins(priority, header, message, url=None):
93+
recipients = []
94+
for userName in userConfigs:
95+
if not userConfigs[userName]["notification_config"] or not userConfigs[userName]["is_admin"]:
96+
continue
97+
recipients.append(userName)
98+
NotificationHelper.sendNotification(priority, header, message, url, recipients)

0 commit comments

Comments
 (0)