-
Notifications
You must be signed in to change notification settings - Fork 0
/
hs110_daily_gather.py
75 lines (62 loc) · 2.44 KB
/
hs110_daily_gather.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
#!/usr/bin/env python3
import sqlite3
from pyHS100 import SmartPlug
import datetime
from jinja2 import Environment, FileSystemLoader, select_autoescape
env = Environment(
loader=FileSystemLoader(''),
autoescape=select_autoescape(['html', 'xml'])
)
db_conn = sqlite3.connect('daily_power_usage.db')
db_conn.execute("""CREATE TABLE IF NOT EXISTS power_usage
(
mac_address TEXT,
device_label TEXT,
measurement_date DATE,
measurement_wh INTEGER,
PRIMARY KEY (mac_address, measurement_date)
)""")
plug_ips = ["192.168.XXX.XXX", "192.168.XXX.XXX", "192.168.XXX.XXX"]
plug_info = {}
plug_connections = {}
def log_daily_data(plug_conn, year, month):
daily = plug_conn.get_emeter_daily(year, month)
for day in daily.keys():
date_for_entry = datetime.date(year, month, day)
formatted_date = date_for_entry.strftime('%Y-%m-%d')
day_power_wh = int(daily[day] * 1000)
db_conn.execute(
"INSERT OR REPLACE INTO power_usage (mac_address, device_label,measurement_date,measurement_wh) "
"VALUES (?,?,?,?)", (plug_info[plug_ip]['mac'], plug_info[plug_ip]['alias'],
formatted_date, day_power_wh))
for plug_ip in plug_ips:
plug = SmartPlug(plug_ip)
sys_info = plug.get_sysinfo()
plug_info[plug_ip] = {
'mac': sys_info['mac'],
'alias': sys_info['alias']
}
plug_connections[plug_ip] = plug
# try previous month
month_ago = datetime.datetime.now() - datetime.timedelta(days=30)
log_daily_data(plug, month_ago.year, month_ago.month)
# current month
now = datetime.datetime.now()
log_daily_data(plug, now.year, now.month)
db_conn.commit()
cur = db_conn.execute("""SELECT device_label,measurement_date,SUM(measurement_wh)
FROM power_usage
GROUP BY device_label,measurement_date ORDER BY date(measurement_date) DESC""")
results = cur.fetchall()
summary = {}
for row in results:
label, date, wh = row
if not (date in summary):
summary[date] = {'total_wh': 0, 'device_details': []}
summary[date]['total_wh'] += wh
summary[date]['device_details'].append({'device': label, 'wh': wh})
template = env.get_template('daily_template.html')
rendered = template.render(summary=summary)
with open('daily_rendered.html', 'w') as f:
f.write(rendered)
db_conn.close()