-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvuegraf.py
289 lines (239 loc) · 10.5 KB
/
vuegraf.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/env python3
import datetime
import json
import signal
import sys
import time
import traceback
from threading import Event
# InfluxDB v1
import influxdb
# InfluxDB v2
import influxdb_client
from pyemvue import PyEmVue
from pyemvue.enums import Scale, Unit
# flush=True helps when running in a container without a tty attached
# (alternatively, "python -u" or PYTHONUNBUFFERED will help here)
def log(level, msg):
now = datetime.datetime.utcnow()
print('{} | {} | {}'.format(now, level.ljust(5), msg), flush=True)
def info(msg):
log("INFO", msg)
def error(msg):
log("ERROR", msg)
def handleExit(signum, frame):
global running
error('Caught exit signal')
running = False
pauseEvent.set()
def getConfigValue(key, defaultValue):
if key in config:
return config[key]
return defaultValue
def populateDevices(account):
deviceIdMap = {}
account['deviceIdMap'] = deviceIdMap
channelIdMap = {}
account['channelIdMap'] = channelIdMap
devices = account['vue'].get_devices()
for device in devices:
device = account['vue'].populate_device_properties(device)
deviceIdMap[device.device_gid] = device
for chan in device.channels:
key = "{}-{}".format(device.device_gid, chan.channel_num)
if chan.name is None and chan.channel_num == '1,2,3':
chan.name = device.device_name
channelIdMap[key] = chan
info("Discovered new channel: {} ({})".format(chan.name, chan.channel_num))
def lookupDeviceName(account, device_gid):
if device_gid not in account['deviceIdMap']:
populateDevices(account)
deviceName = "{}".format(device_gid)
if device_gid in account['deviceIdMap']:
deviceName = account['deviceIdMap'][device_gid].device_name
return deviceName
def lookupChannelName(account, chan):
if chan.device_gid not in account['deviceIdMap']:
populateDevices(account)
deviceName = lookupDeviceName(account, chan.device_gid)
name = "{}-{}".format(deviceName, chan.channel_num)
try:
num = int(chan.channel_num)
if 'devices' in account:
for device in account['devices']:
if 'name' in device and device['name'] == deviceName:
if 'channels' in device and len(device['channels']) >= num:
name = device['channels'][num - 1]
break
except:
if chan.channel_num == '1,2,3':
name = deviceName
return name
def createDataPoint(account, chanName, watts, timestamp, detailed):
dataPoint = None
if influxVersion == 2:
dataPoint = influxdb_client.Point("energy_usage") \
.tag("account_name", account['name']) \
.tag("device_name", chanName) \
.tag("detailed", detailed) \
.field("usage", watts) \
.time(time=timestamp)
else:
dataPoint = {
"measurement": "energy_usage",
"tags": {
"account_name": account['name'],
"device_name": chanName,
"detailed": detailed,
},
"fields": {
"usage": watts,
},
"time": timestamp
}
return dataPoint
def extractDataPoints(device, usageDataPoints, historyStartTime=None, historyEndTime=None):
excludedDetailChannelNumbers = ['Balance', 'TotalUsage']
minutesInAnHour = 60
secondsInAMinute = 60
wattsInAKw = 1000
for chanNum, chan in device.channels.items():
if chan.nested_devices:
for gid, nestedDevice in chan.nested_devices.items():
extractDataPoints(nestedDevice, usageDataPoints, historyStartTime, historyEndTime)
chanName = lookupChannelName(account, chan)
kwhUsage = chan.usage
if kwhUsage is not None:
watts = float(minutesInAnHour * wattsInAKw) * kwhUsage
timestamp = stopTime
usageDataPoints.append(createDataPoint(account, chanName, watts, timestamp, False))
if chanNum in excludedDetailChannelNumbers:
continue
if detailedEnabled:
usage, usage_start_time = account['vue'].get_chart_usage(chan, detailedStartTime, stopTime, scale=Scale.SECOND.value, unit=Unit.KWH.value)
index = 0
for kwhUsage in usage:
if kwhUsage is None:
continue
timestamp = detailedStartTime + datetime.timedelta(seconds=index)
watts = float(secondsInAMinute * minutesInAnHour * wattsInAKw) * kwhUsage
usageDataPoints.append(createDataPoint(account, chanName, watts, timestamp, True))
index += 1
# fetches historical minute data
if historyStartTime is not None and historyEndTime is not None:
usage, usage_start_time = account['vue'].get_chart_usage(chan, historyStartTime, historyEndTime, scale=Scale.MINUTE.value, unit=Unit.KWH.value)
index = 0
for kwhUsage in usage:
if kwhUsage is None:
continue
timestamp = historyStartTime + datetime.timedelta(minutes=index)
watts = float(minutesInAnHour * wattsInAKw) * kwhUsage
usageDataPoints.append(createDataPoint(account, chanName, watts, timestamp, False))
index += 1
startupTime = datetime.datetime.utcnow()
try:
if len(sys.argv) != 2:
print('Usage: python {} <config-file>'.format(sys.argv[0]))
sys.exit(1)
configFilename = sys.argv[1]
config = {}
with open(configFilename) as configFile:
config = json.load(configFile)
influxVersion = 1
if 'version' in config['influxDb']:
influxVersion = config['influxDb']['version']
bucket = ''
write_api = None
query_api = None
sslVerify = True
if 'ssl_verify' in config['influxDb']:
sslVerify = config['influxDb']['ssl_verify']
if influxVersion == 2:
info('Using InfluxDB version 2')
bucket = config['influxDb']['bucket']
org = config['influxDb']['org']
token = config['influxDb']['token']
url= config['influxDb']['url']
influx2 = influxdb_client.InfluxDBClient(
url=url,
token=token,
org=org,
verify_ssl=sslVerify
)
write_api = influx2.write_api(write_options=influxdb_client.client.write_api.SYNCHRONOUS)
query_api = influx2.query_api()
if config['influxDb']['reset']:
info('Resetting database')
delete_api = influx2.delete_api()
start = "1970-01-01T00:00:00Z"
stop = startupTime.isoformat(timespec='seconds')
delete_api.delete(start, stop, '_measurement="energy_usage"', bucket=bucket, org=org)
else:
info('Using InfluxDB version 1')
sslEnable = False
if 'ssl_enable' in config['influxDb']:
sslEnable = config['influxDb']['ssl_enable']
# Only authenticate to ingress if 'user' entry was provided in config
if 'user' in config['influxDb']:
influx = influxdb.InfluxDBClient(host=config['influxDb']['host'], port=config['influxDb']['port'], username=config['influxDb']['user'], password=config['influxDb']['pass'], database=config['influxDb']['database'], ssl=sslEnable, verify_ssl=sslVerify)
else:
influx = influxdb.InfluxDBClient(host=config['influxDb']['host'], port=config['influxDb']['port'], database=config['influxDb']['database'], ssl=sslEnable, verify_ssl=sslVerify)
influx.create_database(config['influxDb']['database'])
if config['influxDb']['reset']:
info('Resetting database')
influx.delete_series(measurement='energy_usage')
historyDays = min(config['influxDb'].get('historyDays', 0), 7)
history = historyDays > 0
running = True
signal.signal(signal.SIGINT, handleExit)
signal.signal(signal.SIGHUP, handleExit)
pauseEvent = Event()
intervalSecs=getConfigValue("updateIntervalSecs", 60)
detailedIntervalSecs=getConfigValue("detailedIntervalSecs", 3600)
lagSecs=getConfigValue("lagSecs", 5)
detailedStartTime = startupTime
while running:
now = datetime.datetime.utcnow()
stopTime = now - datetime.timedelta(seconds=lagSecs)
detailedEnabled = (stopTime - detailedStartTime).total_seconds() >= detailedIntervalSecs
for account in config["accounts"]:
if 'vue' not in account:
account['vue'] = PyEmVue()
account['vue'].login(username=account['email'], password=account['password'])
info('Login completed')
populateDevices(account)
try:
deviceGids = list(account['deviceIdMap'].keys())
usages = account['vue'].get_device_list_usage(deviceGids, stopTime, scale=Scale.MINUTE.value, unit=Unit.KWH.value)
if usages is not None:
usageDataPoints = []
for gid, device in usages.items():
extractDataPoints(device, usageDataPoints)
if history:
for day in range(historyDays):
info('Loading historical data: {} day ago'.format(day+1))
historyStartTime = stopTime - datetime.timedelta(seconds=3600*24*(day+1))
historyEndTime = stopTime - datetime.timedelta(seconds=3600*24*day)
for gid, device in usages.items():
extractDataPoints(device, usageDataPoints, historyStartTime, historyEndTime)
if not running:
break
pauseEvent.wait(30)
history = False
if not running:
break
info('Submitting datapoints to database; account="{}"; points={}'.format(account['name'], len(usageDataPoints)))
if influxVersion == 2:
write_api.write(bucket=bucket, record=usageDataPoints)
else:
influx.write_points(usageDataPoints)
except:
error('Failed to record new usage data: {}'.format(sys.exc_info()))
traceback.print_exc()
if detailedEnabled:
detailedStartTime = stopTime + datetime.timedelta(seconds=1)
pauseEvent.wait(intervalSecs)
info('Finished')
except:
error('Fatal error: {}'.format(sys.exc_info()))
traceback.print_exc()