-
-
Notifications
You must be signed in to change notification settings - Fork 127
/
switch-single.js
307 lines (282 loc) · 11.3 KB
/
switch-single.js
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
import platformConsts from '../utils/constants.js';
import { hasProperty } from '../utils/functions.js';
export default class {
constructor(platform, accessory) {
// Set up variables from the platform
this.eveChar = platform.eveChar;
this.hapChar = platform.api.hap.Characteristic;
this.hapErr = platform.api.hap.HapStatusError;
this.hapServ = platform.api.hap.Service;
this.lang = platform.lang;
this.log = platform.log;
this.platform = platform;
// Set up variables from the accessory
this.name = accessory.displayName;
this.accessory = accessory;
// Initially set the online flag as true (to be then updated as false if necessary)
this.isOnline = true;
/*
UIID 1/6/14/24/27/112/1009/1256/7004: single switch, no power readings
UIID 5: single switch, with wattage readings
UIID 32: single switch, with wattage, voltage and amp readings
UIID 77/78/81/107/112/138/160: single switch but firmware uses multiple channels (only need CH0)
UIID 182/190: single switch but multiple channels with wattage, voltage and amp readings
*/
// Set up custom variables for this device type
const deviceConf = platform.deviceConf[accessory.context.eweDeviceId] || {};
this.inUsePowerThreshold = deviceConf.inUsePowerThreshold || platformConsts.defaultValues.inUsePowerThreshold;
// Set the correct logging variables for this accessory
switch (deviceConf.overrideLogging) {
case 'standard':
this.enableLogging = true;
this.enableDebugLogging = false;
break;
case 'debug':
this.enableLogging = true;
this.enableDebugLogging = true;
break;
case 'disable':
this.enableLogging = false;
this.enableDebugLogging = false;
break;
default:
this.enableLogging = !platform.config.disableDeviceLogging;
this.enableDebugLogging = platform.config.debug;
break;
}
// If the accessory has a outlet service then remove it
if (this.accessory.getService(this.hapServ.Outlet)) {
this.accessory.removeService(this.accessory.getService(this.hapServ.Outlet));
}
// Add the switch service if it doesn't already exist
this.service = this.accessory.getService(this.hapServ.Switch)
|| this.accessory.addService(this.hapServ.Switch);
// Some models provide a power reading in different decimals
this.divisor = 1;
switch (this.accessory.context.eweUIID) {
case 5:
case 182:
case 190:
// Add Eve power characteristics
this.powerReadings = true;
if (!this.service.testCharacteristic(this.hapChar.OutletInUse)) {
this.service.addCharacteristic(this.hapChar.OutletInUse);
}
if (!this.service.testCharacteristic(this.eveChar.CurrentConsumption)) {
this.service.addCharacteristic(this.eveChar.CurrentConsumption);
}
// Remove unused Eve characteristics
if (this.service.testCharacteristic(this.eveChar.ElectricCurrent)) {
this.service.removeCharacteristic(
this.service.getCharacteristic(this.eveChar.ElectricCurrent),
);
}
if (this.service.testCharacteristic(this.eveChar.Voltage)) {
this.service.removeCharacteristic(this.service.getCharacteristic(this.eveChar.Voltage));
}
if (this.accessory.context.eweUIID === 190) {
this.divisor = 100;
}
break;
case 32:
// Add Eve power characteristics
this.powerReadings = true;
if (!this.service.testCharacteristic(this.hapChar.OutletInUse)) {
this.service.addCharacteristic(this.hapChar.OutletInUse);
}
if (!this.service.testCharacteristic(this.eveChar.CurrentConsumption)) {
this.service.addCharacteristic(this.eveChar.CurrentConsumption);
}
if (!this.service.testCharacteristic(this.eveChar.ElectricCurrent)) {
this.service.addCharacteristic(this.eveChar.ElectricCurrent);
}
if (!this.service.testCharacteristic(this.eveChar.Voltage)) {
this.service.addCharacteristic(this.eveChar.Voltage);
}
break;
default:
// Remove unused Eve characteristics
if (this.service.testCharacteristic(this.eveChar.CurrentConsumption)) {
this.service.removeCharacteristic(
this.service.getCharacteristic(this.eveChar.CurrentConsumption),
);
}
if (this.service.testCharacteristic(this.eveChar.ElectricCurrent)) {
this.service.removeCharacteristic(
this.service.getCharacteristic(this.eveChar.ElectricCurrent),
);
}
if (this.service.testCharacteristic(this.eveChar.Voltage)) {
this.service.removeCharacteristic(this.service.getCharacteristic(this.eveChar.Voltage));
}
break;
}
// Set a flag for devices with hardware that use multi-channel format
if (
platformConsts.devices.switchSCM.includes(this.accessory.context.eweUIID)
|| platformConsts.devices.switchSCMPower.includes(this.accessory.context.eweUIID)
) {
this.isSCM = true;
}
// Add the set handler to the switch on/off characteristic
this.service
.getCharacteristic(this.hapChar.On)
.onSet(async (value) => this.internalStateUpdate(value));
// Add the get handlers only if the user hasn't disabled the disableNoResponse setting
if (!platform.config.disableNoResponse) {
this.service.getCharacteristic(this.hapChar.On).onGet(() => {
if (!this.isOnline) {
throw new this.hapErr(-70402);
}
return this.service.getCharacteristic(this.hapChar.On).value;
});
}
// Pass the accessory to Fakegato to set up with Eve
this.accessory.eveService = new platform.eveService('switch', this.accessory, {
log: () => {},
});
// Set up extra features for outlets that provide power readings
if (this.powerReadings) {
// Set up an interval to get eWeLink to send power updates
setTimeout(() => {
this.internalUIUpdate();
this.intervalPoll = setInterval(() => this.internalUIUpdate(), 120000);
}, 5000);
// Stop the intervals on Homebridge shutdown
platform.api.on('shutdown', () => clearInterval(this.intervalPoll));
}
// Output the customised options to the log
const normalLogging = this.enableLogging ? 'standard' : 'disable';
const opts = JSON.stringify({
inUsePowerThreshold: this.inUsePowerThreshold,
logging: this.enableDebugLogging ? 'debug' : normalLogging,
showAs: 'default',
});
this.log('[%s] %s %s.', this.name, this.lang.devInitOpts, opts);
}
async internalStateUpdate(value) {
try {
const newValue = value ? 'on' : 'off';
if (newValue === this.cacheState) {
return;
}
const params = {};
if (this.isSCM) {
params.switches = [{ switch: newValue, outlet: 0 }];
} else {
params.switch = newValue;
}
await this.platform.sendDeviceUpdate(this.accessory, params);
this.cacheState = newValue;
this.accessory.eveService.addEntry({ status: value ? 1 : 0 });
if (this.enableLogging) {
this.log('[%s] %s [%s].', this.name, this.lang.curState, this.cacheState);
}
} catch (err) {
this.platform.deviceUpdateError(this.accessory, err, true);
setTimeout(() => {
this.service.updateCharacteristic(this.hapChar.On, this.cacheState === 'on');
}, 2000);
throw new this.hapErr(-70402);
}
}
async internalUIUpdate() {
try {
// Skip polling if device isn't online
if (!this.isOnline) {
return;
}
// Send the params to request the updates
await this.platform.sendDeviceUpdate(this.accessory, { uiActive: 120 });
} catch (err) {
// Suppress errors here
}
}
async externalUpdate(params) {
try {
if (!this.isSCM && params.switch && params.switch !== this.cacheState) {
this.cacheState = params.switch;
this.service.updateCharacteristic(this.hapChar.On, this.cacheState === 'on');
this.accessory.eveService.addEntry({ status: this.cacheState === 'on' ? 1 : 0 });
if (params.updateSource && this.enableLogging) {
this.log('[%s] %s [%s].', this.name, this.lang.curState, this.cacheState);
}
}
if (this.isSCM && params.switches && params.switches[0].switch !== this.cacheState) {
this.cacheState = params.switches[0].switch;
this.service.updateCharacteristic(this.hapChar.On, this.cacheState === 'on');
this.accessory.eveService.addEntry({ status: this.cacheState === 'on' ? 1 : 0 });
if (params.updateSource && this.enableLogging) {
this.log('[%s] %s [%s].', this.name, this.lang.curState, this.cacheState);
}
}
if (!this.powerReadings) {
return;
}
let logger = false;
let power;
let voltage;
let current;
if (hasProperty(params, 'power')) {
power = Math.round((parseFloat(params.power) * 100) / this.divisor) / 100;
this.service.updateCharacteristic(this.eveChar.CurrentConsumption, power);
this.service.updateCharacteristic(
this.hapChar.OutletInUse,
this.cacheState === 'on' && parseFloat(params.power) > this.inUsePowerThreshold,
);
logger = true;
}
if (hasProperty(params, 'voltage')) {
voltage = Math.round((parseFloat(params.voltage) * 100) / this.divisor) / 100;
this.service.updateCharacteristic(this.eveChar.Voltage, voltage);
logger = true;
}
if (hasProperty(params, 'current')) {
current = Math.round((parseFloat(params.current) * 100) / this.divisor) / 100;
this.service.updateCharacteristic(this.eveChar.ElectricCurrent, current);
logger = true;
}
if (params.updateSource && logger && this.enableLogging) {
this.log(
'[%s] %s%s%s.',
this.name,
power !== undefined ? `${this.lang.curPower} [${power}W]` : '',
voltage !== undefined ? ` ${this.lang.curVolt} [${voltage}V]` : '',
current !== undefined ? ` ${this.lang.curCurr} [${current}A]` : '',
);
}
} catch (err) {
this.platform.deviceUpdateError(this.accessory, err, false);
}
}
markStatus(isOnline) {
this.isOnline = isOnline;
}
async currentState() {
const toReturn = {};
toReturn.services = ['switch'];
toReturn.switch = {
state: this.service.getCharacteristic(this.hapChar.On).value ? 'on' : 'off',
};
if (this.powerReadings) {
try {
toReturn.services.push('power');
toReturn.power = {
state: await this.platform.sendDeviceUpdate(this.accessory, { hundredDaysKwh: 'get' }),
voltage: this.service.testCharacteristic(this.eveChar.Voltage)
? this.service.getCharacteristic(this.eveChar.Voltage).value
: undefined,
power: this.service.testCharacteristic(this.eveChar.CurrentConsumption)
? this.service.getCharacteristic(this.eveChar.CurrentConsumption).value
: undefined,
current: this.service.testCharacteristic(this.eveChar.ElectricCurrent)
? this.service.getCharacteristic(this.eveChar.ElectricCurrent).value
: undefined,
};
} catch (err) {
// Suppress errors here
}
}
return toReturn;
}
}