-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.py
225 lines (192 loc) · 8.01 KB
/
plugin.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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
####################
# Copyright (c) 2016, Perceptive Automation, LLC. All rights reserved.
# http://www.indigodomo.com
import indigo
import os
import sys
from datetime import datetime
import json, requests
import sense_energy
from sense_energy.sense_exceptions import *
# Note the "indigo" module is automatically imported and made available inside
# our global name space by the host process.
################################################################################
class Plugin(indigo.PluginBase):
########################################
def __init__(self, pluginId, pluginDisplayName, pluginVersion, pluginPrefs):
super(Plugin, self).__init__(pluginId, pluginDisplayName, pluginVersion, pluginPrefs)
self.debug = pluginPrefs.get("showDebugInfo", False)
self.rateLimit = pluginPrefs.get("rateLimit", 30)
self.doSolar = bool(pluginPrefs.get("solarEnabled", False))
self.folderID = pluginPrefs.get("folderID", None)
self.devIDs = list()
self.sidFromDev = dict()
self.devFromSid = dict()
self.rt = dict()
self.dontStart = True
self.csvPath = "{}/Preferences/Plugins/{}".format(indigo.server.getInstallFolderPath(), self.pluginId)
self.csvActive = "{}/Preferences/Plugins/{}/activeLog.csv".format(indigo.server.getInstallFolderPath(), self.pluginId)
self.csvDaily = "{}/Preferences/Plugins/{}/dailyLog.csv".format(indigo.server.getInstallFolderPath(), self.pluginId)
if not os.path.exists(self.csvPath):
os.mkdir(self.csvPath)
csv_file = open(self.csvActive, 'w+')
csv_file.write("Timestamp,power\n")
csv_file.close()
def validatePrefsConfigUi(self, valuesDict):
fid = int(valuesDict["folderID"])
if (fid in indigo.devices.folders):
return True
else:
errorDict = indigo.Dict()
errorDict["folderID"] = "This field should contain a folder ID"
errorDict["showAlertText"] = "Folder not found with ID: %s \n\nEnsure you have used the ID, not the name of the folder.\n\nRight-click the folder you want to use and use 'Copy ID' to obtain the correct ID." % fid
return (False, valuesDict, errorDict)
def closedPrefsConfigUi(self, valuesDict, userCancelled):
# Since the dialog closed we want to set the debug flag - if you don't directly use
# a plugin's properties (and for debugLog we don't) you'll want to translate it to
# the appropriate stuff here.
if not userCancelled:
self.debug = valuesDict.get("showDebugInfo", False)
if self.debug:
indigo.server.log("Debug logging enabled")
else:
indigo.server.log("Debug logging disabled")
self.rateLimit = int(valuesDict.get("rateLimit", 30))
self.debugLog(self.sense.authenticate(str(valuesDict['username']), str(valuesDict['password']), self.rateLimit))
self.doSolar = bool(valuesDict.get("solarEnabled", False))
self.folderID = valuesDict.get("folderID", "")
try:
dev = indigo.device.create(indigo.kProtocol.Plugin,"Active Total","Active Total",deviceTypeId="sensedevice",folder=int(self.folderID))
dev.updateStateOnServer(key='id', value='core')
dev.updateStateOnServer(key='power', value="0", uiValue="0 w")
dev.stateListOrDisplayStateIdChanged()
except ValueError as e:
pass
########################################
def startup(self):
self.debugLog(u"startup called")
#self.debugLog(u"Creating senseable")
self.sense = sense_energy.Senseable()
self.debugLog(u"Authenticating...")
self.debugLog(self.sense.authenticate(str(self.pluginPrefs['username']), str(self.pluginPrefs['password']), self.rateLimit))
#for dev in indigo.devices.iter("self"):
#indigo.device.delete(dev)
def shutdown(self):
self.debugLog(u"shutdown called")
def deviceStartComm(self, dev):
#self.debugLog("deviceStartComm called")
dev.stateListOrDisplayStateIdChanged()
#self.debugLog(dev)
if (dev.deviceTypeId == "sensedevice"):
devID = dev.id
sID = dev.states['id']
dName = dev.name
self.devIDs.append(sID)
self.sidFromDev[int(devID)] = sID
self.devFromSid[sID] = devID
#self.debugLog("Added device {} ({})".format(sID,dName))
def deviceStopComm(self, dev):
#self.debugLog("deviceStopComm called")
if (dev.deviceTypeId == "sensedevice"):
devID = dev.id
sID = dev.states['id']
dName = dev.name
try:
self.devIDs.remove(sID)
except:
pass
self.sidFromDev.pop(int(devID),None)
self.devFromSid.pop(sID,None)
#self.debugLog("Removed device {} ({})".format(sID,dName))
def getDevices(self):
self.debugLog("IDs: %s" % self.devIDs)
#self.debugLog(u"Getting realtime()")
try:
self.sense.update_realtime()
for i in self.sense._realtime['devices']:
rtid = i['id']
self.rt[rtid] = int(i['w'])
except SenseAPITimeoutException as e:
self.errorLog(e)
self.sense.update_trend_data()
active = self.sense.active_power
daily = self.sense.daily_usage
self.debugLog("Active: {}w".format(active))
self.debugLog("Daily: {}kw".format(daily))
indigo.devices[self.devFromSid['core']].updateStateOnServer(key='power', value=str(int(active)), uiValue=str("{} w".format(int(active))))
indigo.devices[self.devFromSid['core']].updateStateImageOnServer(indigo.kStateImageSel.PowerOn)
lastUpdateTS = self.sense.getRealtimeCall()
lastUpdate = datetime.fromtimestamp(lastUpdateTS).strftime("%Y-%m-%d %H:%M:%S.%f")
self.debugLog("CSV Output: {},{}".format(lastUpdate,int(active)))
csv_file = open(self.csvActive, 'a+')
csv_file.write('{0},{1}\n'.format(lastUpdate, int(active)))
csv_file.close()
if (self.doSolar):
self.debugLog("Active Solar {}w:".format(self.sense.active_solar_power))
self.debugLog("Daily Solar: {}kw".format(self.sense.daily_production))
for d in self.sense.get_discovered_device_data():
sID = d['id']
if ((not self.doSolar) and (sID == "solar")):
#self.debugLog("Solar disabled: skipping")
continue
dName = d['name']
dRevoked = False
if ('tags' in d) and ('Revoked' in d['tags']):
if (d['tags']['Revoked'] == 'true'):
dRevoked = True
if ('tags' in d) and ('UserDeleted' in d['tags']):
if (d['tags']['UserDeleted'] == 'true'):
dRevoked = True
if ('tags' in d) and ('MergedDevices' in d['tags']):
mergedDevices = d['tags']['MergedDevices'].split(',')
for md in mergedDevices:
if (md in self.devIDs):
self.debugLog(u"Deleting merged device: %s" % indigo.devices[self.devFromSid[md]].name)
indigo.device.delete(self.devFromSid[md])
if (dRevoked):
if (sID in self.devIDs):
#indigo.device.delete(self.devFromSid[sID])
indigo.device.enable(self.devFromSid[sID], value=False)
else:
if (sID in self.devIDs):
dev = indigo.devices[self.devFromSid[sID]]
if (dev.name != dName):
dev.name = dName
dev.replaceOnServer()
if (str(sID) in self.rt.keys()):
dev.updateStateOnServer(key='power', value=str(self.rt[sID]), uiValue=str("{} w".format(self.rt[sID])))
dev.updateStateImageOnServer(indigo.kStateImageSel.PowerOn)
else:
dev.updateStateOnServer(key='power', value="0", uiValue="0 w")
dev.updateStateImageOnServer(indigo.kStateImageSel.PowerOff)
#dev.stateListOrDisplayStateIdChanged()
else:
self.debugLog("CREATING: {} ({})".format(dName,sID))
#self.debugLog(d)
try:
dev = indigo.device.create(indigo.kProtocol.Plugin,dName,dName,deviceTypeId="sensedevice",folder=int(self.folderID))
dev.updateStateOnServer(key='id', value=str(sID))
dev.updateStateOnServer(key='power', value="0", uiValue="0 w")
dev.stateListOrDisplayStateIdChanged()
except ValueError as e:
if (str(e) == "NameNotUniqueError"):
self.debugLog("Duplicate device found - please ensure Sense devices are all uniquely named")
else:
self.errorLog(e)
#dev.stateListOrDisplayStateIdChanged()
self.rt = None
self.rt = dict()
#self.debugLog("")
def runConcurrentThread(self):
try:
while True:
if (self.dontStart):
self.sleep(10) #Wait for initialisation to finish
self.dontStart = False
self.getDevices()
#self.debugLog(self.sense.getRealtimeCall())
self.sleep(int(self.rateLimit))
except self.StopThread:
pass