-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatus.py
53 lines (47 loc) · 1.66 KB
/
status.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
''' program to email when a battery is low (below THRESHOLD in config file) '''
import smtplib
import json
import requests
import config
import utils
# Load the utils
gu = utils.GeniusUtility(config.HG_SIG)
# List to hold results
DEVICE_LIST = []
BAD_LIST = []
# Find the devices with batteries
devices = filter(
lambda device: 'batteryLevel' in device['state'], gu.getjson('/devices'))
for value in devices:
room = value['assignedZones'][0]['name']
devicetype = value['type']
node_id = value['id']
if 'batteryLevel' in value['state']:
text = node_id + ": " + devicetype + " in " + room
level = value['state']['batteryLevel']
text = text + " Battery level: " + str(level)
DEVICE_LIST.append(text)
if level < config.THRESHOLD:
BAD_LIST.append(text)
# Print sorted list
DEVICE_LIST = sorted(DEVICE_LIST, key=lambda x: int(x.split(':')[0]))
for result in DEVICE_LIST:
print(result)
# Email bad list
if BAD_LIST:
BAD_LIST = sorted(BAD_LIST, key=lambda x: int(x.split(':')[0]))
print("Sending message")
# Build message to send
MSG = "\r\n".join(["From: " + config.FROM_ADDRESS, "To: " + config.TO_ADDRESS,
"Subject: Heat Genius batteries are low", "", ""])
MSG += "\r\n".join(BAD_LIST)
try:
SERVER = smtplib.SMTP('smtp.gmail.com:587')
SERVER.ehlo()
SERVER.starttls()
SERVER.login(config.USER_NAME, config.PASSWORD)
SERVER.sendmail(config.FROM_ADDRESS, config.TO_ADDRESS, MSG)
SERVER.quit()
print("Message send")
except IOError:
print("Something went wrong")