-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotification.py
More file actions
59 lines (44 loc) · 1.93 KB
/
Copy pathnotification.py
File metadata and controls
59 lines (44 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import smtplib
import datetime
import logging
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
logger = logging.getLogger(__name__)
def send_email_notification(recipients, subject, body):
if not recipients:
return
try:
msg = MIMEMultipart()
msg["From"] = "eplumber@localhost"
msg["To"] = ", ".join(recipients)
msg["Subject"] = subject
msg.attach(MIMEText(body, "plain"))
server = smtplib.SMTP("localhost", 25)
server.sendmail("eplumber@localhost", recipients, msg.as_string())
server.quit()
logger.info(f"Email notification sent: {subject}")
except Exception as e:
logger.error(f"Failed to send email notification '{subject}': {e}")
def send_action_notification(recipients, action_name, rule_context=None):
subject = f"Eplumber Action: {action_name}"
body = f"Action '{action_name}' has been executed.\nTime: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n"
if rule_context:
body += f"Rule: {rule_context.name}\n\nTest Results:\n"
for test in rule_context.tests:
current_value = test.sensor.mean
if isinstance(current_value, float):
current_value = round(current_value, 2)
passes = (
test.operator(current_value, test.value)
if current_value is not None
else False
)
status = "✅ PASS" if passes else "❌ FAIL"
body += (
f" {status} {test.sensor.name}: {current_value} {test.op} {test.value}\n"
)
send_email_notification(recipients, subject, body)
def send_startup_notification(recipients):
subject = "Eplumber Started"
body = f"Eplumber IoT automation system has started successfully.\nTime: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
send_email_notification(recipients, subject, body)