-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathups_monitor.py
105 lines (87 loc) · 3.16 KB
/
ups_monitor.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import smtplib
import requests
import time
from nut2 import PyNUTClient
from subprocess import call
CHECK_INTERVAL = 60 # Seconds - how often to check UPS status
UPS_SHUTDOWN_PER_THRES = 8 # percentage on when to shutdown hub
UPS_SHUTDOWN_MIN_THRES = 8 # minutes remaining on when to shutdown hub
def shutdown_hubitat():
ip = '10.0.1.20' # Hubitat IP Address
login_data = { # Hubitat Login Credentials
'username': 'admin',
'password': 'mypassword'
}
with requests.Session() as s:
try:
s.get('http://%s/login' % (ip), timeout=5)
s.post('http://%s/login' % (ip), data=login_data, timeout=5)
s.post('http://%s/hub/shutdown' % (ip), timeout=5)
print("Shutdown Command Sent")
except:
print("Shutdown Failed")
def send_email(subject):
# GMAIL Login
user = '[email protected]'
pwd = 'mypassword'
recipient = '[email protected]'
body = subject
FROM = user
TO = recipient if isinstance(recipient, list) else [recipient]
SUBJECT = subject
TEXT = body
# Prepare actual message
message = """From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
try:
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(user, pwd)
server.sendmail(FROM, TO, message)
server.close()
print ('successfully sent the mail')
except Exception as e:
print ("failed to send mail")
print(e)
opt_track = {
'on_battery_email_sent': False,
'shutdown_email_sent': False,
'script_error': False
}
print("Running Script")
while True:
status = {}
try:
with PyNUTClient() as s:
status = s.list_vars("ups")
except:
pass
if 'ups.status' not in status or 'battery.charge' not in status or 'battery.runtime' not in status:
print("Can't Reach UPS")
print(time.ctime())
print()
if opt_track['script_error'] is False:
opt_track['script_error'] = True
send_email('UPS script error')
time.sleep(60)
continue
if opt_track['on_battery_email_sent'] is False and "OL" not in status['ups.status']:
print("Power Outage")
opt_track['on_battery_email_sent'] = True
send_email('Home on UPS Power')
elif opt_track['on_battery_email_sent'] is True and "OL" in status['ups.status']:
print("Power Restored")
opt_track['on_battery_email_sent'] = False
opt_track['shutdown_email_sent'] = False
send_email('Home Power Restored')
if opt_track['shutdown_email_sent'] is False and (int(status['battery.charge']) <= UPS_SHUTDOWN_PER_THRES or int(status['battery.runtime']) <= UPS_SHUTDOWN_MIN_THRES*60) and "OL" not in status[
'ups.status']:
print("Shutting Down")
opt_track['on_battery_email_sent'] = True
opt_track['shutdown_email_sent'] = True
send_email('Home - Shutting Down Pi & Hubitat')
shutdown_hubitat()
time.sleep(3)
call("sudo shutdown -h now", shell=True) # Shutdown Raspberry Pi
time.sleep(CHECK_INTERVAL)