-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.json
334 lines (270 loc) · 10.7 KB
/
index.json
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
const axios = require('axios');
const setupCache = require('axios-cache-adapter').setupCache;
var Service, Characteristic;
const DEF_MIN_LUX = 0,
DEF_MAX_LUX = 25;
const PLUGIN_NAME = 'homebridge-solaredge';
const ACCESSORY_NAME_GRID = 'SolarEdge GRID';
const ACCESSORY_NAME_PV = 'SolarEdge PV';
const ACCESSORY_NAME_HOUSE = 'SolarEdge HOUSE';
const ACCESSORY_NAME_BATTERYINV = 'SolarEdge BATTERYINV';
const ACCESSORY_NAME_BATTERY = 'SolarEdge BATTERY';
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory(PLUGIN_NAME, ACCESSORY_NAME_GRID, SolarEdgeGRID);
homebridge.registerAccessory(PLUGIN_NAME, ACCESSORY_NAME_PV, SolarEdgePV);
homebridge.registerAccessory(PLUGIN_NAME, ACCESSORY_NAME_HOUSE, SolarEdgeHOUSE);
homebridge.registerAccessory(PLUGIN_NAME, ACCESSORY_NAME_BATTERYINV, SolarEdgeBATTERYINV);
homebridge.registerAccessory(PLUGIN_NAME, ACCESSORY_NAME_BATTERY, SolarEdgeBATTERY);
}
/**
* Setup Cache For Axios to prevent additional requests
*/
const cache = setupCache({
maxAge: 1 * 1000 //in ms
})
const api = axios.create({
adapter: cache.adapter
})
/**
* Main API request with site overview data
*
* @param {siteID} the SolarEdge Site ID to be queried
* @param {apiKey} the SolarEdge monitoring API Key for access to the Site
*/
const getSiteData = async(siteID, apiKey) => {
try {
return await api.get('https://monitoringapi.solaredge.com/site/'+siteID+'/currentPowerFlow?api_key='+apiKey)
} catch (error) {
console.error(error)
}
}
/**
* Gets and returns the accessory's value in the correct format.
*
* @param {siteID} the SolarEdge Site ID to be queried
* @param {apiKey} the SolarEdge monitoring API Key for access to the Site
* @param (log) access to the homebridge logfile
* @return {bool} the value for the accessory
*/
const getAccessoryValue = async (siteID, apiKey, log) => {
// To Do: Need to handle if no connection
const SiteData = await getSiteData(siteID, apiKey)
if(SiteData) {
log.info('Data from API', SiteData.data.siteCurrentPowerFlow.GRID.currentPower);
if (SiteData.data.siteCurrentPowerFlow.GRID.currentPower == null) {
return 0
} else {
// Return positive value
return Math.abs(SiteData.data.siteCurrentPowerFlow.GRID.currentPower)
}
} else {
// No response SiteData return 0
return 0
}
}
class SolarEdgeGRID {
constructor(log, config) {
this.log = log
this.config = config
this.service = new Service.LightSensor(this.config.name)
this.name = config["name"];
this.manufacturer = config["manufacturer"] || "GRID Provider";
this.model = config["model"] || "GRID Meter";
this.serial = config["serial"] || "GRID Contract";
this.site_id = config["site_id"];
this.api_key = config["api_key"];
this.minLux = config["min_lux"] || DEF_MIN_LUX;
this.maxLux = config["max_lux"] || DEF_MAX_LUX;
}
getServices () {
const informationService = new Service.AccessoryInformation()
.setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.SerialNumber, this.serial)
this.service.getCharacteristic(Characteristic.CurrentAmbientLightLevel)
.on('get', this.getOnCharacteristicHandler.bind(this))
return [informationService, this.service]
}
async getOnCharacteristicHandler (callback) {
this.log(`calling getOnCharacteristicHandler`, await getAccessoryValue(this.site_id, this.api_key, this.log))
callback(null, await getAccessoryValue(this.site_id, this.api_key, this.log))
}
}
const getAccessoryValue1 = async (siteID, apiKey, log) => {
// To Do: Need to handle if no connection
const SiteData = await getSiteData(siteID, apiKey)
if(SiteData) {
log.info('Data from API', SiteData.data.siteCurrentPowerFlow.PV.currentPower);
if (SiteData.data.siteCurrentPowerFlow.PV.currentPower == null) {
return 0
} else {
// Return positive value
return Math.abs(SiteData.data.siteCurrentPowerFlow.PV.currentPower)
}
} else {
// No response SiteData return 0
return 0
}
}
class SolarEdgePV {
constructor(log, config) {
this.log = log
this.config = config
this.service = new Service.LightSensor(this.config.name)
this.name = config["name"];
this.manufacturer = config["manufacturer"] || "SolarEdge";
this.model = config["model"] || "Inverter";
this.serial = config["serial"] || "solaredge-inverter-1";
this.site_id = config["site_id"];
this.api_key = config["api_key"];
this.minLux = config["min_lux"] || DEF_MIN_LUX;
this.maxLux = config["max_lux"] || DEF_MAX_LUX;
}
getServices () {
const informationService = new Service.AccessoryInformation()
.setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.SerialNumber, this.serial)
this.service.getCharacteristic(Characteristic.CurrentAmbientLightLevel)
.on('get', this.getOnCharacteristicHandler.bind(this))
return [informationService, this.service]
}
async getOnCharacteristicHandler (callback) {
this.log(`calling getOnCharacteristicHandler`, await getAccessoryValue1(this.site_id, this.api_key, this.log))
callback(null, await getAccessoryValue1(this.site_id, this.api_key, this.log))
}
}
const getAccessoryValue2 = async (siteID, apiKey, log) => {
// To Do: Need to handle if no connection
const SiteData = await getSiteData(siteID, apiKey)
if(SiteData) {
log.info('Data from API', SiteData.data.siteCurrentPowerFlow.LOAD.currentPower);
if (SiteData.data.siteCurrentPowerFlow.LOAD.currentPower == null) {
return 0
} else {
// Return positive value
return Math.abs(SiteData.data.siteCurrentPowerFlow.LOAD.currentPower)
}
} else {
// No response SiteData return 0
return 0
}
}
class SolarEdgeHOUSE {
constructor(log, config) {
this.log = log
this.config = config
this.service = new Service.LightSensor(this.config.name)
this.name = config["name"];
this.manufacturer = config["manufacturer"] || "House";
this.model = config["model"] || "House type";
this.serial = config["serial"] || "House Address";
this.site_id = config["site_id"];
this.api_key = config["api_key"];
this.minLux = config["min_lux"] || DEF_MIN_LUX;
this.maxLux = config["max_lux"] || DEF_MAX_LUX;
}
getServices () {
const informationService = new Service.AccessoryInformation()
.setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.SerialNumber, this.serial)
this.service.getCharacteristic(Characteristic.CurrentAmbientLightLevel)
.on('get', this.getOnCharacteristicHandler.bind(this))
return [informationService, this.service]
}
async getOnCharacteristicHandler (callback) {
this.log(`calling getOnCharacteristicHandler`, await getAccessoryValue2(this.site_id, this.api_key, this.log))
callback(null, await getAccessoryValue2(this.site_id, this.api_key, this.log))
}
}
const getAccessoryValue3 = async (siteID, apiKey, log) => {
// To Do: Need to handle if no connection
const SiteData = await getSiteData(siteID, apiKey)
if(SiteData) {
log.info('Data from API', SiteData.data.siteCurrentPowerFlow.STORAGE.currentPower);
if (SiteData.data.siteCurrentPowerFlow.STORAGE.currentPower == null) {
return 0
} else {
// Return positive value
return Math.abs(SiteData.data.siteCurrentPowerFlow.STORAGE.currentPower)
}
} else {
// No response SiteData return 0
return 0
}
}
class SolarEdgeBATTERYINV {
constructor(log, config) {
this.log = log
this.config = config
this.service = new Service.LightSensor(this.config.name)
this.name = config["name"];
this.manufacturer = config["manufacturer"] || "SolarEdge";
this.model = config["model"] || "Inverter";
this.serial = config["serial"] || "solaredge-inverter-1";
this.site_id = config["site_id"];
this.api_key = config["api_key"];
this.minLux = config["min_lux"] || DEF_MIN_LUX;
this.maxLux = config["max_lux"] || DEF_MAX_LUX;
}
getServices () {
const informationService = new Service.AccessoryInformation()
.setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.SerialNumber, this.serial)
this.service.getCharacteristic(Characteristic.CurrentAmbientLightLevel)
.on('get', this.getOnCharacteristicHandler.bind(this))
return [informationService, this.service]
}
async getOnCharacteristicHandler (callback) {
this.log(`calling getOnCharacteristicHandler`, await getAccessoryValue3(this.site_id, this.api_key, this.log))
callback(null, await getAccessoryValue3(this.site_id, this.api_key, this.log))
}
}
const getAccessoryValue4 = async (siteID, apiKey, log) => {
// To Do: Need to handle if no connection
const SiteData = await getSiteData(siteID, apiKey)
if(SiteData) {
log.info('Data from API', SiteData.data.siteCurrentPowerFlow.STORAGE.chargeLevel);
if (SiteData.data.siteCurrentPowerFlow.STORAGE.chargeLevel == null) {
return 0
} else {
// Return positive value
return Math.abs(SiteData.data.siteCurrentPowerFlow.STORAGE.chargeLevel)
}
} else {
// No response SiteData return 0
return 0
}
}
class SolarEdgeBATTERY {
constructor(log, config) {
this.log = log
this.config = config
this.service = new Service.HumiditySensor(this.config.name)
this.name = config["name"];
this.manufacturer = config["manufacturer"] || "LGCHEM";
this.model = config["model"] || "LGCHEM";
this.serial = config["serial"] || "solaredge-LGCHEM";
this.site_id = config["site_id"];
this.api_key = config["api_key"];
this.minLux = config["min_lux"] || DEF_MIN_LUX;
this.maxLux = config["max_lux"] || DEF_MAX_LUX;
}
getServices () {
const informationService = new Service.AccessoryInformation()
.setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.SerialNumber, this.serial)
this.service.getCharacteristic(Characteristic.CurrentRelativeHumidity)
.on('get', this.getOnCharacteristicHandler.bind(this))
return [informationService, this.service]
}
async getOnCharacteristicHandler (callback) {
this.log(`calling getOnCharacteristicHandler`, await getAccessoryValue4(this.site_id, this.api_key, this.log))
callback(null, await getAccessoryValue4(this.site_id, this.api_key, this.log))
}
}