@switchbot/homebridge-switchbot 2.0.0-beta.19 → 2.0.0-beta.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -1
- package/README.md +1 -2
- package/config.schema.json +51 -32
- package/dist/device/bot.d.ts +32 -29
- package/dist/device/bot.d.ts.map +1 -1
- package/dist/device/bot.js +549 -575
- package/dist/device/bot.js.map +1 -1
- package/dist/device/colorbulb.d.ts +33 -15
- package/dist/device/colorbulb.d.ts.map +1 -1
- package/dist/device/colorbulb.js +426 -341
- package/dist/device/colorbulb.js.map +1 -1
- package/dist/device/contact.d.ts +16 -9
- package/dist/device/contact.d.ts.map +1 -1
- package/dist/device/contact.js +246 -208
- package/dist/device/contact.js.map +1 -1
- package/dist/device/curtain.d.ts +21 -18
- package/dist/device/curtain.d.ts.map +1 -1
- package/dist/device/curtain.js +449 -449
- package/dist/device/curtain.js.map +1 -1
- package/dist/device/humidifier.d.ts +22 -20
- package/dist/device/humidifier.d.ts.map +1 -1
- package/dist/device/humidifier.js +346 -324
- package/dist/device/humidifier.js.map +1 -1
- package/dist/device/lock.d.ts +22 -10
- package/dist/device/lock.d.ts.map +1 -1
- package/dist/device/lock.js +257 -178
- package/dist/device/lock.js.map +1 -1
- package/dist/device/meter.d.ts +21 -14
- package/dist/device/meter.d.ts.map +1 -1
- package/dist/device/meter.js +287 -239
- package/dist/device/meter.js.map +1 -1
- package/dist/device/meterplus.d.ts +23 -10
- package/dist/device/meterplus.d.ts.map +1 -1
- package/dist/device/meterplus.js +308 -190
- package/dist/device/meterplus.js.map +1 -1
- package/dist/device/motion.d.ts +17 -10
- package/dist/device/motion.d.ts.map +1 -1
- package/dist/device/motion.js +225 -193
- package/dist/device/motion.js.map +1 -1
- package/dist/device/plug.d.ts +24 -17
- package/dist/device/plug.d.ts.map +1 -1
- package/dist/device/plug.js +270 -267
- package/dist/device/plug.js.map +1 -1
- package/dist/device/striplight.d.ts +36 -14
- package/dist/device/striplight.d.ts.map +1 -1
- package/dist/device/striplight.js +447 -299
- package/dist/device/striplight.js.map +1 -1
- package/dist/irdevice/airconditioner.d.ts +7 -14
- package/dist/irdevice/airconditioner.d.ts.map +1 -1
- package/dist/irdevice/airconditioner.js +177 -169
- package/dist/irdevice/airconditioner.js.map +1 -1
- package/dist/irdevice/airpurifier.d.ts +3 -5
- package/dist/irdevice/airpurifier.d.ts.map +1 -1
- package/dist/irdevice/airpurifier.js +72 -57
- package/dist/irdevice/airpurifier.js.map +1 -1
- package/dist/irdevice/camera.d.ts +3 -4
- package/dist/irdevice/camera.d.ts.map +1 -1
- package/dist/irdevice/camera.js +47 -33
- package/dist/irdevice/camera.js.map +1 -1
- package/dist/irdevice/fan.d.ts +3 -4
- package/dist/irdevice/fan.d.ts.map +1 -1
- package/dist/irdevice/fan.js +66 -48
- package/dist/irdevice/fan.js.map +1 -1
- package/dist/irdevice/light.d.ts +3 -4
- package/dist/irdevice/light.d.ts.map +1 -1
- package/dist/irdevice/light.js +48 -47
- package/dist/irdevice/light.js.map +1 -1
- package/dist/irdevice/other.d.ts +3 -4
- package/dist/irdevice/other.d.ts.map +1 -1
- package/dist/irdevice/other.js +47 -33
- package/dist/irdevice/other.js.map +1 -1
- package/dist/irdevice/tv.d.ts +10 -11
- package/dist/irdevice/tv.d.ts.map +1 -1
- package/dist/irdevice/tv.js +64 -48
- package/dist/irdevice/tv.js.map +1 -1
- package/dist/irdevice/vacuumcleaner.d.ts +7 -12
- package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
- package/dist/irdevice/vacuumcleaner.js +52 -41
- package/dist/irdevice/vacuumcleaner.js.map +1 -1
- package/dist/irdevice/waterheater.d.ts +6 -11
- package/dist/irdevice/waterheater.d.ts.map +1 -1
- package/dist/irdevice/waterheater.js +52 -42
- package/dist/irdevice/waterheater.js.map +1 -1
- package/dist/platform.d.ts +0 -4
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +44 -66
- package/dist/platform.js.map +1 -1
- package/dist/settings.d.ts +3 -3
- package/dist/settings.d.ts.map +1 -1
- package/package.json +7 -6
- package/src/device/bot.ts +551 -589
- package/src/device/colorbulb.ts +441 -375
- package/src/device/contact.ts +249 -213
- package/src/device/curtain.ts +452 -461
- package/src/device/humidifier.ts +358 -365
- package/src/device/lock.ts +263 -179
- package/src/device/meter.ts +302 -253
- package/src/device/meterplus.ts +319 -194
- package/src/device/motion.ts +230 -199
- package/src/device/plug.ts +273 -274
- package/src/device/striplight.ts +460 -316
- package/src/irdevice/airconditioner.ts +181 -183
- package/src/irdevice/airpurifier.ts +70 -60
- package/src/irdevice/camera.ts +46 -36
- package/src/irdevice/fan.ts +64 -49
- package/src/irdevice/light.ts +47 -51
- package/src/irdevice/other.ts +46 -36
- package/src/irdevice/tv.ts +64 -51
- package/src/irdevice/vacuumcleaner.ts +52 -45
- package/src/irdevice/waterheater.ts +50 -45
- package/src/platform.ts +44 -68
- package/src/settings.ts +3 -3
package/dist/device/bot.js
CHANGED
|
@@ -20,30 +20,18 @@ class Bot {
|
|
|
20
20
|
this.platform = platform;
|
|
21
21
|
this.accessory = accessory;
|
|
22
22
|
this.device = device;
|
|
23
|
+
// Connection
|
|
24
|
+
this.BLE = (this.device.connectionType === 'BLE' || this.device.connectionType === 'Both');
|
|
25
|
+
this.OpenAPI = (this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'Both');
|
|
23
26
|
// default placeholders
|
|
24
27
|
this.logs(device);
|
|
25
|
-
this.openAPI();
|
|
26
28
|
this.scan(device);
|
|
27
29
|
this.refreshRate(device);
|
|
28
30
|
this.PressOrSwitch(device);
|
|
29
31
|
this.allowPushChanges(device);
|
|
30
32
|
this.config(device);
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
this.accessory.context.On = this.On;
|
|
34
|
-
}
|
|
35
|
-
else {
|
|
36
|
-
this.On = this.accessory.context.On;
|
|
37
|
-
}
|
|
38
|
-
if (device.bot?.doublePress) {
|
|
39
|
-
this.doublePress = device.bot?.doublePress;
|
|
40
|
-
this.accessory.context.doublePress = this.doublePress;
|
|
41
|
-
}
|
|
42
|
-
else {
|
|
43
|
-
this.doublePress = 1;
|
|
44
|
-
}
|
|
45
|
-
this.BatteryLevel = 100;
|
|
46
|
-
this.StatusLowBattery = 0;
|
|
33
|
+
this.context();
|
|
34
|
+
this.DoublePress(device);
|
|
47
35
|
// this is subject we use to track when we need to POST changes to the SwitchBot API
|
|
48
36
|
this.doBotUpdate = new rxjs_1.Subject();
|
|
49
37
|
this.botUpdateInProgress = false;
|
|
@@ -75,7 +63,7 @@ class Bot {
|
|
|
75
63
|
`${accessory.displayName} Switch`;
|
|
76
64
|
this.infoLog(`${this.device.deviceType}: ${accessory.displayName} Displaying as Switch`);
|
|
77
65
|
this.switchService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
|
|
78
|
-
this.switchService.getCharacteristic(this.platform.Characteristic.On).onSet(this.
|
|
66
|
+
this.switchService.getCharacteristic(this.platform.Characteristic.On).onSet(this.OnSet.bind(this));
|
|
79
67
|
}
|
|
80
68
|
else if (device.bot?.deviceType === 'garagedoor') {
|
|
81
69
|
this.removeFanService(accessory);
|
|
@@ -93,7 +81,7 @@ class Bot {
|
|
|
93
81
|
`${accessory.displayName} Garage Door Opener`;
|
|
94
82
|
this.infoLog(`${this.device.deviceType}: ${accessory.displayName} Displaying as Garage Door Opener`);
|
|
95
83
|
this.garageDoorService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
|
|
96
|
-
this.garageDoorService.getCharacteristic(this.platform.Characteristic.TargetDoorState).onSet(this.
|
|
84
|
+
this.garageDoorService.getCharacteristic(this.platform.Characteristic.TargetDoorState).onSet(this.OnSet.bind(this));
|
|
97
85
|
this.garageDoorService.setCharacteristic(this.platform.Characteristic.ObstructionDetected, false);
|
|
98
86
|
}
|
|
99
87
|
else if (device.bot?.deviceType === 'door') {
|
|
@@ -119,7 +107,7 @@ class Bot {
|
|
|
119
107
|
maxValue: 100,
|
|
120
108
|
minStep: 100,
|
|
121
109
|
})
|
|
122
|
-
.onSet(this.
|
|
110
|
+
.onSet(this.OnSet.bind(this));
|
|
123
111
|
this.doorService.setCharacteristic(this.platform.Characteristic.PositionState, this.platform.Characteristic.PositionState.STOPPED);
|
|
124
112
|
}
|
|
125
113
|
else if (device.bot?.deviceType === 'window') {
|
|
@@ -145,7 +133,7 @@ class Bot {
|
|
|
145
133
|
maxValue: 100,
|
|
146
134
|
minStep: 100,
|
|
147
135
|
})
|
|
148
|
-
.onSet(this.
|
|
136
|
+
.onSet(this.OnSet.bind(this));
|
|
149
137
|
this.windowService.setCharacteristic(this.platform.Characteristic.PositionState, this.platform.Characteristic.PositionState.STOPPED);
|
|
150
138
|
}
|
|
151
139
|
else if (device.bot?.deviceType === 'windowcovering') {
|
|
@@ -172,7 +160,7 @@ class Bot {
|
|
|
172
160
|
maxValue: 100,
|
|
173
161
|
minStep: 100,
|
|
174
162
|
})
|
|
175
|
-
.onSet(this.
|
|
163
|
+
.onSet(this.OnSet.bind(this));
|
|
176
164
|
this.windowCoveringService.setCharacteristic(this.platform.Characteristic.PositionState, this.platform.Characteristic.PositionState.STOPPED);
|
|
177
165
|
}
|
|
178
166
|
else if (device.bot?.deviceType === 'lock') {
|
|
@@ -190,7 +178,7 @@ class Bot {
|
|
|
190
178
|
`${accessory.displayName} Lock`;
|
|
191
179
|
this.infoLog(`${this.device.deviceType}: ${accessory.displayName} Displaying as Lock`);
|
|
192
180
|
this.lockService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
|
|
193
|
-
this.lockService.getCharacteristic(this.platform.Characteristic.LockTargetState).onSet(this.
|
|
181
|
+
this.lockService.getCharacteristic(this.platform.Characteristic.LockTargetState).onSet(this.OnSet.bind(this));
|
|
194
182
|
}
|
|
195
183
|
else if (device.bot?.deviceType === 'faucet') {
|
|
196
184
|
this.removeFanService(accessory);
|
|
@@ -207,7 +195,7 @@ class Bot {
|
|
|
207
195
|
`${accessory.displayName} Faucet`;
|
|
208
196
|
this.infoLog(`${this.device.deviceType}: ${accessory.displayName} Displaying as Faucet`);
|
|
209
197
|
this.faucetService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
|
|
210
|
-
this.faucetService.getCharacteristic(this.platform.Characteristic.Active).onSet(this.
|
|
198
|
+
this.faucetService.getCharacteristic(this.platform.Characteristic.Active).onSet(this.OnSet.bind(this));
|
|
211
199
|
}
|
|
212
200
|
else if (device.bot?.deviceType === 'fan') {
|
|
213
201
|
this.removeLockService(accessory);
|
|
@@ -224,7 +212,7 @@ class Bot {
|
|
|
224
212
|
`${accessory.displayName} Fan`;
|
|
225
213
|
this.infoLog(`${this.device.deviceType}: ${accessory.displayName} Displaying as Fan`);
|
|
226
214
|
this.fanService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
|
|
227
|
-
this.fanService.getCharacteristic(this.platform.Characteristic.On).onSet(this.
|
|
215
|
+
this.fanService.getCharacteristic(this.platform.Characteristic.On).onSet(this.OnSet.bind(this));
|
|
228
216
|
}
|
|
229
217
|
else if (device.bot?.deviceType === 'stateful') {
|
|
230
218
|
this.removeFanService(accessory);
|
|
@@ -245,7 +233,7 @@ class Bot {
|
|
|
245
233
|
this.statefulProgrammableSwitchService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
|
|
246
234
|
this.statefulProgrammableSwitchService
|
|
247
235
|
.getCharacteristic(this.platform.Characteristic.ProgrammableSwitchOutputState)
|
|
248
|
-
.onSet(this.
|
|
236
|
+
.onSet(this.OnSet.bind(this));
|
|
249
237
|
}
|
|
250
238
|
else {
|
|
251
239
|
this.removeFanService(accessory);
|
|
@@ -262,15 +250,15 @@ class Bot {
|
|
|
262
250
|
`${accessory.displayName} Outlet`;
|
|
263
251
|
this.infoLog(`${this.device.deviceType}: ${accessory.displayName} Displaying as Outlet`);
|
|
264
252
|
this.outletService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
|
|
265
|
-
this.outletService.getCharacteristic(this.platform.Characteristic.On).onSet(this.
|
|
253
|
+
this.outletService.getCharacteristic(this.platform.Characteristic.On).onSet(this.OnSet.bind(this));
|
|
266
254
|
}
|
|
267
255
|
// Battery Service
|
|
268
|
-
if (!
|
|
256
|
+
if (!this.BLE) {
|
|
269
257
|
this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Battery Service`);
|
|
270
258
|
this.batteryService = this.accessory.getService(this.platform.Service.Battery);
|
|
271
259
|
accessory.removeService(this.batteryService);
|
|
272
260
|
}
|
|
273
|
-
else if (
|
|
261
|
+
else if (this.BLE && !this.batteryService) {
|
|
274
262
|
this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Battery Service`);
|
|
275
263
|
(this.batteryService = this.accessory.getService(this.platform.Service.Battery) || this.accessory.addService(this.platform.Service.Battery)),
|
|
276
264
|
`${accessory.displayName} Battery`;
|
|
@@ -288,28 +276,23 @@ class Bot {
|
|
|
288
276
|
await this.refreshStatus();
|
|
289
277
|
});
|
|
290
278
|
// Watch for Bot change events
|
|
291
|
-
// We put in a debounce of
|
|
279
|
+
// We put in a debounce of 1000ms so we don't make duplicate calls
|
|
292
280
|
this.doBotUpdate
|
|
293
281
|
.pipe((0, operators_1.tap)(() => {
|
|
294
282
|
this.botUpdateInProgress = true;
|
|
295
|
-
}), (0, operators_1.debounceTime)(
|
|
283
|
+
}), (0, operators_1.debounceTime)(this.platform.config.options.pushRate * 1000))
|
|
296
284
|
.subscribe(async () => {
|
|
297
285
|
try {
|
|
298
|
-
(0, rxjs_1.interval)(
|
|
286
|
+
(0, rxjs_1.interval)(15000)
|
|
299
287
|
.pipe((0, operators_1.take)(this.doublePress))
|
|
300
288
|
.subscribe(async () => {
|
|
301
289
|
await this.pushChanges();
|
|
302
290
|
});
|
|
303
291
|
}
|
|
304
292
|
catch (e) {
|
|
305
|
-
this.
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
+ ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
|
|
309
|
-
}
|
|
310
|
-
if (this.platform.debugMode) {
|
|
311
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges,` + ` Error: ${(0, super_stringify_1.default)(e)}`);
|
|
312
|
-
}
|
|
293
|
+
this.apiError(e);
|
|
294
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with ${this.device.connectionType} Connection,`
|
|
295
|
+
+ ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
|
|
313
296
|
}
|
|
314
297
|
this.botUpdateInProgress = false;
|
|
315
298
|
});
|
|
@@ -318,15 +301,20 @@ class Bot {
|
|
|
318
301
|
* Parse the device status from the SwitchBot api
|
|
319
302
|
*/
|
|
320
303
|
async parseStatus() {
|
|
321
|
-
if (this.
|
|
304
|
+
if (this.BLE && !this.SwitchToOpenAPI) {
|
|
305
|
+
await this.BLEparseStatus();
|
|
306
|
+
}
|
|
307
|
+
else if (this.OpenAPI && this.platform.config.credentials?.token) {
|
|
308
|
+
this.SwitchToOpenAPI = false;
|
|
322
309
|
await this.openAPIparseStatus();
|
|
323
310
|
}
|
|
324
311
|
else {
|
|
325
|
-
|
|
312
|
+
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
|
|
313
|
+
+ ` ${this.device.connectionType}, parseStatus will not happen.`);
|
|
326
314
|
}
|
|
327
315
|
}
|
|
328
316
|
async BLEparseStatus() {
|
|
329
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}
|
|
317
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
|
|
330
318
|
// BLEmode (true if Switch Mode) | (false if Press Mode)
|
|
331
319
|
if (this.mode) {
|
|
332
320
|
this.accessory.context.On = this.On;
|
|
@@ -353,37 +341,36 @@ class Bot {
|
|
|
353
341
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
|
|
354
342
|
}
|
|
355
343
|
async openAPIparseStatus() {
|
|
356
|
-
|
|
357
|
-
|
|
344
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
|
|
345
|
+
if (this.botMode === 'press') {
|
|
346
|
+
this.On = false;
|
|
347
|
+
this.accessory.context.On = this.On;
|
|
358
348
|
}
|
|
359
|
-
|
|
360
|
-
this.
|
|
361
|
-
if (this.
|
|
349
|
+
else {
|
|
350
|
+
this.accessory.context.On = this.On;
|
|
351
|
+
if (this.On === undefined) {
|
|
362
352
|
this.On = false;
|
|
363
|
-
this.accessory.context.On = this.On;
|
|
364
|
-
}
|
|
365
|
-
else {
|
|
366
|
-
this.accessory.context.On = this.On;
|
|
367
|
-
if (this.On === undefined) {
|
|
368
|
-
this.On = false;
|
|
369
|
-
}
|
|
370
353
|
}
|
|
371
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
|
|
372
354
|
}
|
|
355
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
|
|
373
356
|
}
|
|
374
357
|
/**
|
|
375
358
|
* Asks the SwitchBot API for the latest device information
|
|
376
359
|
*/
|
|
377
360
|
async refreshStatus() {
|
|
378
|
-
if (this.
|
|
361
|
+
if (this.BLE) {
|
|
379
362
|
await this.BLERefreshStatus();
|
|
380
363
|
}
|
|
381
|
-
else {
|
|
364
|
+
else if (this.OpenAPI && this.platform.config.credentials?.token) {
|
|
382
365
|
await this.openAPIRefreshStatus();
|
|
383
366
|
}
|
|
367
|
+
else {
|
|
368
|
+
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
|
|
369
|
+
+ ` ${this.device.connectionType}, refreshStatus will not happen.`);
|
|
370
|
+
}
|
|
384
371
|
}
|
|
385
372
|
async BLERefreshStatus() {
|
|
386
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}
|
|
373
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
|
|
387
374
|
const switchbot = await this.platform.connectBLE();
|
|
388
375
|
// Convert to BLE Address
|
|
389
376
|
this.device.bleMac = this.device
|
|
@@ -393,153 +380,114 @@ class Bot {
|
|
|
393
380
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
394
381
|
this.getCustomBLEAddress(switchbot);
|
|
395
382
|
// Start to monitor advertisement packets
|
|
396
|
-
if (switchbot
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
.then(() => {
|
|
405
|
-
// Set an event hander
|
|
406
|
-
switchbot.onadvertisement = (ad) => {
|
|
407
|
-
this.address = ad.address;
|
|
408
|
-
if (this.deviceLogging.includes('debug')) {
|
|
409
|
-
this.infoLog(this.address);
|
|
410
|
-
this.infoLog(this.device.bleMac);
|
|
411
|
-
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address Found: ${this.address}`);
|
|
412
|
-
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac}`);
|
|
413
|
-
}
|
|
414
|
-
this.serviceData = ad.serviceData;
|
|
415
|
-
this.mode = ad.serviceData.mode;
|
|
416
|
-
this.state = ad.serviceData.state;
|
|
417
|
-
this.battery = ad.serviceData.battery;
|
|
418
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${(0, super_stringify_1.default)(ad.serviceData)}`);
|
|
419
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}, model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName},` +
|
|
420
|
-
` mode: ${ad.serviceData.mode}, state: ${ad.serviceData.state}, battery: ${ad.serviceData.battery}`);
|
|
421
|
-
if (this.serviceData) {
|
|
422
|
-
this.connected = true;
|
|
423
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
|
|
424
|
-
}
|
|
425
|
-
else {
|
|
426
|
-
this.connected = false;
|
|
427
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
|
|
428
|
-
}
|
|
429
|
-
};
|
|
430
|
-
// Wait 2 seconds
|
|
431
|
-
return switchbot.wait(this.scanDuration * 1000);
|
|
432
|
-
})
|
|
433
|
-
.then(async () => {
|
|
434
|
-
// Stop to monitor
|
|
435
|
-
switchbot.stopScan();
|
|
436
|
-
if (this.connected) {
|
|
437
|
-
this.parseStatus();
|
|
438
|
-
this.updateHomeKitCharacteristics();
|
|
439
|
-
}
|
|
440
|
-
else {
|
|
441
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
|
|
442
|
-
if (this.platform.config.credentials?.token) {
|
|
443
|
-
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
444
|
-
this.SwitchToOpenAPI = true;
|
|
445
|
-
await this.openAPIRefreshStatus();
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
})
|
|
449
|
-
.catch(async (e) => {
|
|
450
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
|
|
451
|
-
if (this.deviceLogging.includes('debug')) {
|
|
452
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
|
|
453
|
-
+ ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
|
|
454
|
-
}
|
|
455
|
-
if (this.platform.config.credentials?.token) {
|
|
456
|
-
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
457
|
-
this.SwitchToOpenAPI = true;
|
|
458
|
-
await this.openAPIRefreshStatus();
|
|
459
|
-
}
|
|
460
|
-
this.apiError(e);
|
|
461
|
-
});
|
|
462
|
-
}
|
|
463
|
-
async getCustomBLEAddress(switchbot) {
|
|
464
|
-
if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
|
|
465
|
-
(async () => {
|
|
466
|
-
// Start to monitor advertisement packets
|
|
467
|
-
await switchbot.startScan({
|
|
468
|
-
model: 'H',
|
|
469
|
-
});
|
|
470
|
-
// Set an event handler
|
|
383
|
+
if (switchbot !== false) {
|
|
384
|
+
switchbot
|
|
385
|
+
.startScan({
|
|
386
|
+
model: 'H',
|
|
387
|
+
id: this.device.bleMac,
|
|
388
|
+
})
|
|
389
|
+
.then(() => {
|
|
390
|
+
// Set an event hander
|
|
471
391
|
switchbot.onadvertisement = (ad) => {
|
|
472
|
-
this.
|
|
392
|
+
this.address = ad.address;
|
|
393
|
+
if (this.deviceLogging.includes('debug')) {
|
|
394
|
+
this.infoLog(this.address);
|
|
395
|
+
this.infoLog(this.device.bleMac);
|
|
396
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address Found: ${this.address}`);
|
|
397
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac}`);
|
|
398
|
+
}
|
|
399
|
+
this.serviceData = ad.serviceData;
|
|
400
|
+
this.mode = ad.serviceData.mode;
|
|
401
|
+
this.state = ad.serviceData.state;
|
|
402
|
+
this.battery = ad.serviceData.battery;
|
|
403
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${(0, super_stringify_1.default)(ad.serviceData)}`);
|
|
404
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}, model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName},` +
|
|
405
|
+
` mode: ${ad.serviceData.mode}, state: ${ad.serviceData.state}, battery: ${ad.serviceData.battery}`);
|
|
406
|
+
if (this.serviceData) {
|
|
407
|
+
this.connected = true;
|
|
408
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
|
|
409
|
+
}
|
|
410
|
+
else {
|
|
411
|
+
this.connected = false;
|
|
412
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
|
|
413
|
+
}
|
|
473
414
|
};
|
|
474
|
-
|
|
415
|
+
// Wait 2 seconds
|
|
416
|
+
return switchbot.wait(this.scanDuration * 1000);
|
|
417
|
+
})
|
|
418
|
+
.then(async () => {
|
|
475
419
|
// Stop to monitor
|
|
476
420
|
switchbot.stopScan();
|
|
477
|
-
|
|
421
|
+
if (this.connected) {
|
|
422
|
+
this.parseStatus();
|
|
423
|
+
this.updateHomeKitCharacteristics();
|
|
424
|
+
}
|
|
425
|
+
else {
|
|
426
|
+
await this.BLEconnection(switchbot);
|
|
427
|
+
}
|
|
428
|
+
})
|
|
429
|
+
.catch(async (e) => {
|
|
430
|
+
this.apiError(e);
|
|
431
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}`
|
|
432
|
+
+ ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
|
|
433
|
+
await this.BLEconnection(switchbot);
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
else {
|
|
437
|
+
await this.BLEconnection(switchbot);
|
|
478
438
|
}
|
|
479
439
|
}
|
|
480
440
|
async openAPIRefreshStatus() {
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
this.debugLog(`d: ${d}`);
|
|
509
|
-
});
|
|
510
|
-
res.on('end', () => {
|
|
511
|
-
try {
|
|
512
|
-
this.deviceStatus = JSON.parse(rawData);
|
|
513
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus: ${(0, super_stringify_1.default)(this.deviceStatus)}`);
|
|
514
|
-
this.power = this.deviceStatus.body.power;
|
|
515
|
-
this.parseStatus();
|
|
516
|
-
this.updateHomeKitCharacteristics();
|
|
517
|
-
}
|
|
518
|
-
catch (e) {
|
|
519
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
|
|
520
|
-
}
|
|
521
|
-
});
|
|
441
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
|
|
442
|
+
try {
|
|
443
|
+
const t = Date.now();
|
|
444
|
+
const nonce = 'requestID';
|
|
445
|
+
const data = this.platform.config.credentials?.token + t + nonce;
|
|
446
|
+
const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
|
|
447
|
+
const sign = signTerm.toString('base64');
|
|
448
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
|
|
449
|
+
const options = {
|
|
450
|
+
hostname: settings_1.HostDomain,
|
|
451
|
+
port: 443,
|
|
452
|
+
path: `${settings_1.DevicePath}/${this.device.deviceId}/status`,
|
|
453
|
+
method: 'GET',
|
|
454
|
+
headers: {
|
|
455
|
+
Authorization: this.platform.config.credentials?.token,
|
|
456
|
+
sign: sign,
|
|
457
|
+
nonce: nonce,
|
|
458
|
+
t: t,
|
|
459
|
+
'Content-Type': 'application/json',
|
|
460
|
+
},
|
|
461
|
+
};
|
|
462
|
+
const req = https_1.default.request(options, (res) => {
|
|
463
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus statusCode: ${res.statusCode}`);
|
|
464
|
+
let rawData = '';
|
|
465
|
+
res.on('data', (d) => {
|
|
466
|
+
rawData += d;
|
|
467
|
+
this.debugLog(`d: ${d}`);
|
|
522
468
|
});
|
|
523
|
-
|
|
524
|
-
|
|
469
|
+
res.on('end', () => {
|
|
470
|
+
try {
|
|
471
|
+
this.deviceStatus = JSON.parse(rawData);
|
|
472
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus: ${(0, super_stringify_1.default)(this.deviceStatus)}`);
|
|
473
|
+
this.power = this.deviceStatus.body.power;
|
|
474
|
+
this.parseStatus();
|
|
475
|
+
this.updateHomeKitCharacteristics();
|
|
476
|
+
}
|
|
477
|
+
catch (e) {
|
|
478
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
|
|
479
|
+
}
|
|
525
480
|
});
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
|
|
532
|
-
+ ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
|
|
533
|
-
}
|
|
534
|
-
if (this.platform.debugMode) {
|
|
535
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
|
|
536
|
-
+ ` Error: ${(0, super_stringify_1.default)(e)}`);
|
|
537
|
-
}
|
|
538
|
-
this.apiError(e);
|
|
539
|
-
}
|
|
481
|
+
});
|
|
482
|
+
req.on('error', (e) => {
|
|
483
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
|
|
484
|
+
});
|
|
485
|
+
req.end();
|
|
540
486
|
}
|
|
541
|
-
|
|
542
|
-
this.
|
|
487
|
+
catch (e) {
|
|
488
|
+
this.apiError(e);
|
|
489
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
|
|
490
|
+
+ ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
|
|
543
491
|
}
|
|
544
492
|
}
|
|
545
493
|
/**
|
|
@@ -550,12 +498,16 @@ class Bot {
|
|
|
550
498
|
* Bot - "command" "press" "default" = trigger press
|
|
551
499
|
*/
|
|
552
500
|
async pushChanges() {
|
|
553
|
-
if (this.
|
|
554
|
-
await this.
|
|
501
|
+
if (this.BLE) {
|
|
502
|
+
await this.BLERefreshStatus();
|
|
555
503
|
}
|
|
556
|
-
else {
|
|
504
|
+
else if (this.OpenAPI) {
|
|
557
505
|
await this.openAPIpushChanges();
|
|
558
506
|
}
|
|
507
|
+
else {
|
|
508
|
+
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
|
|
509
|
+
+ ` ${this.device.connectionType}, pushChanges will not happen.`);
|
|
510
|
+
}
|
|
559
511
|
(0, rxjs_1.interval)(5000)
|
|
560
512
|
.pipe((0, operators_1.take)(1))
|
|
561
513
|
.subscribe(async () => {
|
|
@@ -563,9 +515,9 @@ class Bot {
|
|
|
563
515
|
});
|
|
564
516
|
}
|
|
565
517
|
async BLEpushChanges() {
|
|
566
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}
|
|
518
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`);
|
|
567
519
|
if (this.On !== this.accessory.context.On || this.allowPush) {
|
|
568
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}
|
|
520
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges On: ${this.On} OnCached: ${this.accessory.context.On}`);
|
|
569
521
|
const switchbot = await this.platform.connectBLE();
|
|
570
522
|
// Convert to BLE Address
|
|
571
523
|
this.device.bleMac = this.device
|
|
@@ -596,20 +548,13 @@ class Bot {
|
|
|
596
548
|
}, 500);
|
|
597
549
|
})
|
|
598
550
|
.catch(async (e) => {
|
|
599
|
-
this.
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
}
|
|
604
|
-
if (this.platform.debugMode) {
|
|
605
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
|
|
606
|
-
+ ` Error: ${(0, super_stringify_1.default)(e)}`);
|
|
607
|
-
}
|
|
608
|
-
if (this.platform.config.credentials?.token) {
|
|
551
|
+
this.apiError(e);
|
|
552
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
|
|
553
|
+
+ ` Connection & botMode: ${this.botMode}, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
|
|
554
|
+
if (this.platform.config.credentials?.token && this.device.connectionType !== 'Both') {
|
|
609
555
|
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
610
556
|
await this.openAPIpushChanges();
|
|
611
557
|
}
|
|
612
|
-
this.apiError(e);
|
|
613
558
|
});
|
|
614
559
|
}
|
|
615
560
|
else if (this.botMode === 'switch') {
|
|
@@ -625,128 +570,157 @@ class Bot {
|
|
|
625
570
|
this.accessory.context.On = this.On;
|
|
626
571
|
})
|
|
627
572
|
.catch(async (e) => {
|
|
628
|
-
this.
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
}
|
|
633
|
-
if (this.platform.debugMode) {
|
|
634
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
|
|
635
|
-
+ ` Error: ${(0, super_stringify_1.default)(e)}`);
|
|
636
|
-
}
|
|
637
|
-
if (this.platform.config.credentials?.token) {
|
|
573
|
+
this.apiError(e);
|
|
574
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
|
|
575
|
+
+ ` Connection & botMode: ${this.botMode}, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
|
|
576
|
+
if (this.platform.config.credentials?.token && this.device.connectionType !== 'Both') {
|
|
638
577
|
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
639
578
|
await this.openAPIpushChanges();
|
|
640
579
|
}
|
|
641
|
-
this.apiError(e);
|
|
642
580
|
});
|
|
643
581
|
}
|
|
644
582
|
else {
|
|
645
|
-
|
|
583
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Bot Mode: ${this.botMode}`);
|
|
646
584
|
}
|
|
647
585
|
}
|
|
648
|
-
|
|
586
|
+
else {
|
|
587
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges.` + `On: ${this.On}, `
|
|
588
|
+
+ `OnCached: ${this.accessory.context.On}`);
|
|
589
|
+
}
|
|
649
590
|
}
|
|
650
|
-
async
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
591
|
+
async openAPIpushChanges() {
|
|
592
|
+
try {
|
|
593
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
|
|
594
|
+
if (this.On !== this.accessory.context.On || this.allowPush) {
|
|
595
|
+
// Make Push On request to the API
|
|
596
|
+
const t = Date.now();
|
|
597
|
+
const nonce = 'requestID';
|
|
598
|
+
const data = this.platform.config.credentials?.token + t + nonce;
|
|
599
|
+
const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret)
|
|
600
|
+
.update(Buffer.from(data, 'utf-8'))
|
|
601
|
+
.digest();
|
|
602
|
+
const sign = signTerm.toString('base64');
|
|
603
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
|
|
604
|
+
let command = '';
|
|
605
|
+
if (this.botMode === 'switch' && this.On) {
|
|
606
|
+
command = 'turnOn';
|
|
607
|
+
this.On = true;
|
|
608
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Switch Mode, Turning ${this.On}`);
|
|
609
|
+
}
|
|
610
|
+
else if (this.botMode === 'switch' && !this.On) {
|
|
611
|
+
command = 'turnOff';
|
|
612
|
+
this.On = false;
|
|
613
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Switch Mode, Turning ${this.On}`);
|
|
614
|
+
}
|
|
615
|
+
else if (this.botMode === 'press') {
|
|
616
|
+
command = 'press';
|
|
617
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Press Mode`);
|
|
618
|
+
this.On = false;
|
|
656
619
|
}
|
|
657
620
|
else {
|
|
658
|
-
|
|
621
|
+
throw new Error(`${this.device.deviceType}: ${this.accessory.displayName} Device Paramters not set for this Bot.`);
|
|
659
622
|
}
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
}
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
this.On = false;
|
|
686
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Switch Mode, Turning ${this.On}`);
|
|
687
|
-
}
|
|
688
|
-
else if (this.botMode === 'press') {
|
|
689
|
-
command = 'press';
|
|
690
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Press Mode`);
|
|
691
|
-
this.On = false;
|
|
692
|
-
}
|
|
693
|
-
else {
|
|
694
|
-
throw new Error(`${this.device.deviceType}: ${this.accessory.displayName} Device Paramters not set for this Bot.`);
|
|
695
|
-
}
|
|
696
|
-
const body = (0, super_stringify_1.default)({
|
|
697
|
-
'command': `${command}`,
|
|
698
|
-
'parameter': 'default',
|
|
699
|
-
'commandType': 'command',
|
|
700
|
-
});
|
|
701
|
-
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
|
|
702
|
-
const options = {
|
|
703
|
-
hostname: settings_1.HostDomain,
|
|
704
|
-
port: 443,
|
|
705
|
-
path: `${settings_1.DevicePath}/${this.device.deviceId}/commands`,
|
|
706
|
-
method: 'POST',
|
|
707
|
-
headers: {
|
|
708
|
-
'Authorization': this.platform.config.credentials?.token,
|
|
709
|
-
'sign': sign,
|
|
710
|
-
'nonce': nonce,
|
|
711
|
-
't': t,
|
|
712
|
-
'Content-Type': 'application/json',
|
|
713
|
-
'Content-Length': body.length,
|
|
714
|
-
},
|
|
715
|
-
};
|
|
716
|
-
const req = https_1.default.request(options, res => {
|
|
717
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
|
|
718
|
-
this.statusCode({ res });
|
|
719
|
-
res.on('data', d => {
|
|
720
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
|
|
721
|
-
});
|
|
722
|
-
});
|
|
723
|
-
req.on('error', (e) => {
|
|
724
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
|
|
623
|
+
const body = (0, super_stringify_1.default)({
|
|
624
|
+
'command': `${command}`,
|
|
625
|
+
'parameter': 'default',
|
|
626
|
+
'commandType': 'command',
|
|
627
|
+
});
|
|
628
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
|
|
629
|
+
const options = {
|
|
630
|
+
hostname: settings_1.HostDomain,
|
|
631
|
+
port: 443,
|
|
632
|
+
path: `${settings_1.DevicePath}/${this.device.deviceId}/commands`,
|
|
633
|
+
method: 'POST',
|
|
634
|
+
headers: {
|
|
635
|
+
'Authorization': this.platform.config.credentials?.token,
|
|
636
|
+
'sign': sign,
|
|
637
|
+
'nonce': nonce,
|
|
638
|
+
't': t,
|
|
639
|
+
'Content-Type': 'application/json',
|
|
640
|
+
'Content-Length': body.length,
|
|
641
|
+
},
|
|
642
|
+
};
|
|
643
|
+
const req = https_1.default.request(options, res => {
|
|
644
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges statusCode: ${res.statusCode}`);
|
|
645
|
+
this.statusCode({ res });
|
|
646
|
+
res.on('data', d => {
|
|
647
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
|
|
725
648
|
});
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
this.
|
|
729
|
-
|
|
730
|
-
|
|
649
|
+
});
|
|
650
|
+
req.on('error', (e) => {
|
|
651
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
|
|
652
|
+
});
|
|
653
|
+
req.write(body);
|
|
654
|
+
req.end();
|
|
655
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges: ${(0, super_stringify_1.default)(req)}`);
|
|
731
656
|
}
|
|
732
|
-
|
|
733
|
-
this.
|
|
734
|
-
|
|
735
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
|
|
736
|
-
+ ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
|
|
737
|
-
}
|
|
738
|
-
if (this.platform.debugMode) {
|
|
739
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
|
|
740
|
-
+ ` Error: ${(0, super_stringify_1.default)(e)}`);
|
|
741
|
-
}
|
|
742
|
-
this.apiError(e);
|
|
657
|
+
else {
|
|
658
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.` + `On: ${this.On}, `
|
|
659
|
+
+ `OnCached: ${this.accessory.context.On}`);
|
|
743
660
|
}
|
|
744
661
|
}
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
662
|
+
catch (e) {
|
|
663
|
+
this.apiError(e);
|
|
664
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
|
|
665
|
+
+ ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
/**
|
|
669
|
+
* Handle requests to set the "On" characteristic
|
|
670
|
+
*/
|
|
671
|
+
async OnSet(value) {
|
|
672
|
+
if (this.device.bot?.deviceType === 'garagedoor') {
|
|
673
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set TargetDoorState: ${value}`);
|
|
674
|
+
if (value === this.platform.Characteristic.TargetDoorState.CLOSED) {
|
|
675
|
+
this.On = false;
|
|
676
|
+
}
|
|
677
|
+
else {
|
|
678
|
+
this.On = true;
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
else if (this.device.bot?.deviceType === 'door' ||
|
|
682
|
+
this.device.bot?.deviceType === 'window' ||
|
|
683
|
+
this.device.bot?.deviceType === 'windowcovering') {
|
|
684
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set TargetPosition: ${value}`);
|
|
685
|
+
if (value === 0) {
|
|
686
|
+
this.On = false;
|
|
687
|
+
}
|
|
688
|
+
else {
|
|
689
|
+
this.On = true;
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
else if (this.device.bot?.deviceType === 'lock') {
|
|
693
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set LockTargetState: ${value}`);
|
|
694
|
+
if (value === this.platform.Characteristic.LockTargetState.SECURED) {
|
|
695
|
+
this.On = false;
|
|
696
|
+
}
|
|
697
|
+
else {
|
|
698
|
+
this.On = true;
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
else if (this.device.bot?.deviceType === 'faucet') {
|
|
702
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Active: ${value}`);
|
|
703
|
+
if (value === this.platform.Characteristic.Active.INACTIVE) {
|
|
704
|
+
this.On = false;
|
|
705
|
+
}
|
|
706
|
+
else {
|
|
707
|
+
this.On = true;
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
else if (this.device.bot?.deviceType === 'stateful') {
|
|
711
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set ProgrammableSwitchOutputState: ${value}`);
|
|
712
|
+
if (value === 0) {
|
|
713
|
+
this.On = false;
|
|
714
|
+
}
|
|
715
|
+
else {
|
|
716
|
+
this.On = true;
|
|
748
717
|
}
|
|
749
718
|
}
|
|
719
|
+
else {
|
|
720
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set On: ${value}`);
|
|
721
|
+
this.On = value;
|
|
722
|
+
}
|
|
723
|
+
this.doBotUpdate.next();
|
|
750
724
|
}
|
|
751
725
|
/**
|
|
752
726
|
* Updates the status for each of the HomeKit Characteristics
|
|
@@ -923,7 +897,7 @@ class Bot {
|
|
|
923
897
|
}
|
|
924
898
|
}
|
|
925
899
|
this.accessory.context.On = this.On;
|
|
926
|
-
if (this.
|
|
900
|
+
if (this.BLE) {
|
|
927
901
|
if (this.BatteryLevel === undefined) {
|
|
928
902
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
|
|
929
903
|
}
|
|
@@ -942,52 +916,194 @@ class Bot {
|
|
|
942
916
|
}
|
|
943
917
|
}
|
|
944
918
|
}
|
|
945
|
-
async
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
this.
|
|
950
|
-
}
|
|
951
|
-
else if (this.device.bot?.deviceType === 'door') {
|
|
952
|
-
this.doorService?.updateCharacteristic(this.platform.Characteristic.TargetPosition, e);
|
|
953
|
-
this.doorService?.updateCharacteristic(this.platform.Characteristic.CurrentPosition, e);
|
|
954
|
-
this.doorService?.updateCharacteristic(this.platform.Characteristic.PositionState, e);
|
|
919
|
+
async removeOutletService(accessory) {
|
|
920
|
+
// If outletService still pressent, then remove first
|
|
921
|
+
this.outletService = this.accessory.getService(this.platform.Service.Outlet);
|
|
922
|
+
if (this.outletService) {
|
|
923
|
+
this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Outlet Service`);
|
|
955
924
|
}
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
925
|
+
accessory.removeService(this.outletService);
|
|
926
|
+
}
|
|
927
|
+
async removeGarageDoorService(accessory) {
|
|
928
|
+
// If garageDoorService still pressent, then remove first
|
|
929
|
+
this.garageDoorService = this.accessory.getService(this.platform.Service.GarageDoorOpener);
|
|
930
|
+
if (this.garageDoorService) {
|
|
931
|
+
this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Garage Door Service`);
|
|
960
932
|
}
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
933
|
+
accessory.removeService(this.garageDoorService);
|
|
934
|
+
}
|
|
935
|
+
async removeDoorService(accessory) {
|
|
936
|
+
// If doorService still pressent, then remove first
|
|
937
|
+
this.doorService = this.accessory.getService(this.platform.Service.Door);
|
|
938
|
+
if (this.doorService) {
|
|
939
|
+
this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Door Service`);
|
|
965
940
|
}
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
941
|
+
accessory.removeService(this.doorService);
|
|
942
|
+
}
|
|
943
|
+
async removeLockService(accessory) {
|
|
944
|
+
// If lockService still pressent, then remove first
|
|
945
|
+
this.lockService = this.accessory.getService(this.platform.Service.LockMechanism);
|
|
946
|
+
if (this.lockService) {
|
|
947
|
+
this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Lock Service`);
|
|
969
948
|
}
|
|
970
|
-
|
|
971
|
-
|
|
949
|
+
accessory.removeService(this.lockService);
|
|
950
|
+
}
|
|
951
|
+
async removeFaucetService(accessory) {
|
|
952
|
+
// If faucetService still pressent, then remove first
|
|
953
|
+
this.faucetService = this.accessory.getService(this.platform.Service.Faucet);
|
|
954
|
+
if (this.faucetService) {
|
|
955
|
+
this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Faucet Service`);
|
|
972
956
|
}
|
|
973
|
-
|
|
974
|
-
|
|
957
|
+
accessory.removeService(this.faucetService);
|
|
958
|
+
}
|
|
959
|
+
async removeFanService(accessory) {
|
|
960
|
+
// If fanService still pressent, then remove first
|
|
961
|
+
this.fanService = this.accessory.getService(this.platform.Service.Fan);
|
|
962
|
+
if (this.fanService) {
|
|
963
|
+
this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Fan Service`);
|
|
975
964
|
}
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
965
|
+
accessory.removeService(this.fanService);
|
|
966
|
+
}
|
|
967
|
+
async removeWindowService(accessory) {
|
|
968
|
+
// If windowService still pressent, then remove first
|
|
969
|
+
this.windowService = this.accessory.getService(this.platform.Service.Window);
|
|
970
|
+
if (this.windowService) {
|
|
971
|
+
this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Window Service`);
|
|
979
972
|
}
|
|
980
|
-
|
|
981
|
-
|
|
973
|
+
accessory.removeService(this.windowService);
|
|
974
|
+
}
|
|
975
|
+
async removeWindowCoveringService(accessory) {
|
|
976
|
+
// If windowCoveringService still pressent, then remove first
|
|
977
|
+
this.windowCoveringService = this.accessory.getService(this.platform.Service.WindowCovering);
|
|
978
|
+
if (this.windowCoveringService) {
|
|
979
|
+
this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Window Covering Service`);
|
|
980
|
+
}
|
|
981
|
+
accessory.removeService(this.windowCoveringService);
|
|
982
|
+
}
|
|
983
|
+
async removeStatefulProgrammableSwitchService(accessory) {
|
|
984
|
+
// If statefulProgrammableSwitchService still pressent, then remove first
|
|
985
|
+
this.statefulProgrammableSwitchService = this.accessory.getService(this.platform.Service.StatefulProgrammableSwitch);
|
|
986
|
+
if (this.statefulProgrammableSwitchService) {
|
|
987
|
+
this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Stateful Programmable Switch Service`);
|
|
988
|
+
}
|
|
989
|
+
accessory.removeService(this.statefulProgrammableSwitchService);
|
|
990
|
+
}
|
|
991
|
+
async removeSwitchService(accessory) {
|
|
992
|
+
// If switchService still pressent, then remove first
|
|
993
|
+
this.switchService = this.accessory.getService(this.platform.Service.Switch);
|
|
994
|
+
if (this.switchService) {
|
|
995
|
+
this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Switch Service`);
|
|
996
|
+
}
|
|
997
|
+
accessory.removeService(this.switchService);
|
|
998
|
+
}
|
|
999
|
+
DoublePress(device) {
|
|
1000
|
+
if (device.bot?.doublePress) {
|
|
1001
|
+
this.doublePress = device.bot?.doublePress;
|
|
1002
|
+
this.accessory.context.doublePress = this.doublePress;
|
|
982
1003
|
}
|
|
983
1004
|
else {
|
|
984
|
-
this.
|
|
1005
|
+
this.doublePress = 1;
|
|
985
1006
|
}
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
1007
|
+
}
|
|
1008
|
+
async getCustomBLEAddress(switchbot) {
|
|
1009
|
+
if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
|
|
1010
|
+
(async () => {
|
|
1011
|
+
// Start to monitor advertisement packets
|
|
1012
|
+
await switchbot.startScan({
|
|
1013
|
+
model: 'H',
|
|
1014
|
+
});
|
|
1015
|
+
// Set an event handler
|
|
1016
|
+
switchbot.onadvertisement = (ad) => {
|
|
1017
|
+
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${(0, super_stringify_1.default)(ad, null, ' ')}`);
|
|
1018
|
+
};
|
|
1019
|
+
await switchbot.wait(10000);
|
|
1020
|
+
// Stop to monitor
|
|
1021
|
+
switchbot.stopScan();
|
|
1022
|
+
})();
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
async BLEconnection(switchbot) {
|
|
1026
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
|
|
1027
|
+
if (this.platform.config.credentials?.token && this.device.connectionType !== 'Both') {
|
|
1028
|
+
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
1029
|
+
this.SwitchToOpenAPI = true;
|
|
1030
|
+
await this.openAPIRefreshStatus();
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
async turnOnOff(device_list) {
|
|
1034
|
+
return await this.retry({
|
|
1035
|
+
max: await this.maxRetry(),
|
|
1036
|
+
fn: () => {
|
|
1037
|
+
if (this.On) {
|
|
1038
|
+
return device_list[0].turnOn({ id: this.device.bleMac });
|
|
1039
|
+
}
|
|
1040
|
+
else {
|
|
1041
|
+
return device_list[0].turnOff({ id: this.device.bleMac });
|
|
1042
|
+
}
|
|
1043
|
+
},
|
|
1044
|
+
});
|
|
1045
|
+
}
|
|
1046
|
+
async retry({ max, fn }) {
|
|
1047
|
+
return fn().catch(async (err) => {
|
|
1048
|
+
if (max === 0) {
|
|
1049
|
+
throw err;
|
|
1050
|
+
}
|
|
1051
|
+
this.infoLog(err);
|
|
1052
|
+
this.infoLog('Retrying');
|
|
1053
|
+
await this.switchbot.wait(1000);
|
|
1054
|
+
return this.retry({ max: max - 1, fn });
|
|
1055
|
+
});
|
|
1056
|
+
}
|
|
1057
|
+
async maxRetry() {
|
|
1058
|
+
let maxRetry;
|
|
1059
|
+
if (this.device.bot?.maxRetry) {
|
|
1060
|
+
maxRetry = this.device.bot?.maxRetry;
|
|
1061
|
+
}
|
|
1062
|
+
else {
|
|
1063
|
+
maxRetry = 5;
|
|
1064
|
+
}
|
|
1065
|
+
return maxRetry;
|
|
1066
|
+
}
|
|
1067
|
+
async PressOrSwitch(device) {
|
|
1068
|
+
if (!device.bot?.mode) {
|
|
1069
|
+
this.botMode = 'switch';
|
|
1070
|
+
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} does not have bot mode set in the Plugin's SwitchBot Device Settings,`);
|
|
1071
|
+
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} is defaulting to "${this.botMode}" mode, you may experience issues.`);
|
|
1072
|
+
}
|
|
1073
|
+
else if (device.bot?.mode === 'switch') {
|
|
1074
|
+
this.botMode = 'switch';
|
|
1075
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Bot Mode: ${this.botMode}`);
|
|
1076
|
+
}
|
|
1077
|
+
else if (device.bot?.mode === 'press') {
|
|
1078
|
+
this.botMode = 'press';
|
|
1079
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Bot Mode: ${this.botMode}`);
|
|
1080
|
+
}
|
|
1081
|
+
else {
|
|
1082
|
+
throw new Error(`${this.device.deviceType}: ${this.accessory.displayName} Bot Mode: ${this.botMode}`);
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
async allowPushChanges(device) {
|
|
1086
|
+
if (device.bot?.allowPush) {
|
|
1087
|
+
this.allowPush = true;
|
|
1088
|
+
}
|
|
1089
|
+
else {
|
|
1090
|
+
this.allowPush = false;
|
|
1091
|
+
}
|
|
1092
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Allowing Push Changes: ${this.allowPush}`);
|
|
1093
|
+
}
|
|
1094
|
+
async scan(device) {
|
|
1095
|
+
if (device.scanDuration) {
|
|
1096
|
+
this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
|
|
1097
|
+
if (this.BLE) {
|
|
1098
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
|
|
1099
|
+
}
|
|
1100
|
+
}
|
|
1101
|
+
else {
|
|
1102
|
+
this.scanDuration = this.accessory.context.scanDuration = 1;
|
|
1103
|
+
if (this.BLE) {
|
|
1104
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
|
|
1105
|
+
}
|
|
989
1106
|
}
|
|
990
|
-
//throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
|
|
991
1107
|
}
|
|
992
1108
|
async statusCode({ res }) {
|
|
993
1109
|
switch (res.statusCode) {
|
|
@@ -1028,96 +1144,111 @@ class Bot {
|
|
|
1028
1144
|
else {
|
|
1029
1145
|
this.outletService?.getCharacteristic(this.platform.Characteristic.On).updateValue(this.On);
|
|
1030
1146
|
}
|
|
1147
|
+
await this.updateHomeKitCharacteristics();
|
|
1031
1148
|
}
|
|
1032
1149
|
}
|
|
1033
|
-
|
|
1034
|
-
* Handle requests to set the "On" characteristic
|
|
1035
|
-
*/
|
|
1036
|
-
async handleOnSet(value) {
|
|
1150
|
+
async apiError(e) {
|
|
1037
1151
|
if (this.device.bot?.deviceType === 'garagedoor') {
|
|
1038
|
-
this.
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
}
|
|
1042
|
-
else {
|
|
1043
|
-
this.On = true;
|
|
1044
|
-
}
|
|
1152
|
+
this.garageDoorService?.updateCharacteristic(this.platform.Characteristic.TargetDoorState, e);
|
|
1153
|
+
this.garageDoorService?.updateCharacteristic(this.platform.Characteristic.CurrentDoorState, e);
|
|
1154
|
+
this.garageDoorService?.updateCharacteristic(this.platform.Characteristic.ObstructionDetected, e);
|
|
1045
1155
|
}
|
|
1046
|
-
else if (this.device.bot?.deviceType === 'door'
|
|
1047
|
-
this.
|
|
1048
|
-
this.
|
|
1049
|
-
this.
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1156
|
+
else if (this.device.bot?.deviceType === 'door') {
|
|
1157
|
+
this.doorService?.updateCharacteristic(this.platform.Characteristic.TargetPosition, e);
|
|
1158
|
+
this.doorService?.updateCharacteristic(this.platform.Characteristic.CurrentPosition, e);
|
|
1159
|
+
this.doorService?.updateCharacteristic(this.platform.Characteristic.PositionState, e);
|
|
1160
|
+
}
|
|
1161
|
+
else if (this.device.bot?.deviceType === 'window') {
|
|
1162
|
+
this.windowService?.updateCharacteristic(this.platform.Characteristic.TargetPosition, e);
|
|
1163
|
+
this.windowService?.updateCharacteristic(this.platform.Characteristic.CurrentPosition, e);
|
|
1164
|
+
this.windowService?.updateCharacteristic(this.platform.Characteristic.PositionState, e);
|
|
1165
|
+
}
|
|
1166
|
+
else if (this.device.bot?.deviceType === 'windowcovering') {
|
|
1167
|
+
this.windowCoveringService?.updateCharacteristic(this.platform.Characteristic.TargetPosition, e);
|
|
1168
|
+
this.windowCoveringService?.updateCharacteristic(this.platform.Characteristic.CurrentPosition, e);
|
|
1169
|
+
this.windowCoveringService?.updateCharacteristic(this.platform.Characteristic.PositionState, e);
|
|
1056
1170
|
}
|
|
1057
1171
|
else if (this.device.bot?.deviceType === 'lock') {
|
|
1058
|
-
this.
|
|
1059
|
-
|
|
1060
|
-
this.On = false;
|
|
1061
|
-
}
|
|
1062
|
-
else {
|
|
1063
|
-
this.On = true;
|
|
1064
|
-
}
|
|
1172
|
+
this.doorService?.updateCharacteristic(this.platform.Characteristic.LockTargetState, e);
|
|
1173
|
+
this.doorService?.updateCharacteristic(this.platform.Characteristic.LockCurrentState, e);
|
|
1065
1174
|
}
|
|
1066
1175
|
else if (this.device.bot?.deviceType === 'faucet') {
|
|
1067
|
-
this.
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
else {
|
|
1072
|
-
this.On = true;
|
|
1073
|
-
}
|
|
1176
|
+
this.faucetService?.updateCharacteristic(this.platform.Characteristic.Active, e);
|
|
1177
|
+
}
|
|
1178
|
+
else if (this.device.bot?.deviceType === 'fan') {
|
|
1179
|
+
this.fanService?.updateCharacteristic(this.platform.Characteristic.On, e);
|
|
1074
1180
|
}
|
|
1075
1181
|
else if (this.device.bot?.deviceType === 'stateful') {
|
|
1076
|
-
this.
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
this.On = true;
|
|
1082
|
-
}
|
|
1182
|
+
this.statefulProgrammableSwitchService?.updateCharacteristic(this.platform.Characteristic.ProgrammableSwitchEvent, e);
|
|
1183
|
+
this.statefulProgrammableSwitchService?.updateCharacteristic(this.platform.Characteristic.ProgrammableSwitchOutputState, e);
|
|
1184
|
+
}
|
|
1185
|
+
else if (this.device.bot?.deviceType === 'switch') {
|
|
1186
|
+
this.switchService?.updateCharacteristic(this.platform.Characteristic.On, e);
|
|
1083
1187
|
}
|
|
1084
1188
|
else {
|
|
1085
|
-
this.
|
|
1086
|
-
|
|
1189
|
+
this.outletService?.updateCharacteristic(this.platform.Characteristic.On, e);
|
|
1190
|
+
}
|
|
1191
|
+
if (this.BLE) {
|
|
1192
|
+
this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
|
|
1193
|
+
this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
|
|
1087
1194
|
}
|
|
1088
|
-
this.doBotUpdate.next();
|
|
1089
1195
|
}
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1196
|
+
FirmwareRevision(accessory, device) {
|
|
1197
|
+
let FirmwareRevision;
|
|
1198
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
|
|
1199
|
+
+ ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
|
|
1200
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
|
|
1201
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
|
|
1202
|
+
if (accessory.context.FirmwareRevision) {
|
|
1203
|
+
FirmwareRevision = accessory.context.FirmwareRevision;
|
|
1204
|
+
}
|
|
1205
|
+
else if (device.firmware) {
|
|
1206
|
+
FirmwareRevision = device.firmware;
|
|
1207
|
+
}
|
|
1208
|
+
else {
|
|
1209
|
+
FirmwareRevision = this.platform.version;
|
|
1210
|
+
}
|
|
1211
|
+
return FirmwareRevision;
|
|
1100
1212
|
}
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1213
|
+
context() {
|
|
1214
|
+
if (this.On === undefined) {
|
|
1215
|
+
this.On = false;
|
|
1216
|
+
this.accessory.context.On = this.On;
|
|
1105
1217
|
}
|
|
1106
1218
|
else {
|
|
1107
|
-
|
|
1219
|
+
this.On = this.accessory.context.On;
|
|
1220
|
+
}
|
|
1221
|
+
if (this.BatteryLevel === undefined) {
|
|
1222
|
+
this.BatteryLevel = 100;
|
|
1223
|
+
}
|
|
1224
|
+
else {
|
|
1225
|
+
this.BatteryLevel = this.accessory.context.BatteryLevel;
|
|
1226
|
+
}
|
|
1227
|
+
if (this.StatusLowBattery === undefined) {
|
|
1228
|
+
this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
|
|
1229
|
+
this.accessory.context.StatusLowBattery = this.StatusLowBattery;
|
|
1230
|
+
}
|
|
1231
|
+
else {
|
|
1232
|
+
this.StatusLowBattery = this.accessory.context.StatusLowBattery;
|
|
1233
|
+
}
|
|
1234
|
+
}
|
|
1235
|
+
async refreshRate(device) {
|
|
1236
|
+
if (device.refreshRate) {
|
|
1237
|
+
this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
|
|
1238
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
|
|
1239
|
+
}
|
|
1240
|
+
else if (this.platform.config.options.refreshRate) {
|
|
1241
|
+
this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options.refreshRate;
|
|
1242
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
|
|
1108
1243
|
}
|
|
1109
|
-
return maxRetry;
|
|
1110
1244
|
}
|
|
1111
1245
|
async config(device) {
|
|
1112
1246
|
let config = {};
|
|
1113
1247
|
if (device.bot) {
|
|
1114
1248
|
config = device.bot;
|
|
1115
1249
|
}
|
|
1116
|
-
if (device.
|
|
1117
|
-
config['
|
|
1118
|
-
}
|
|
1119
|
-
if (device.openAPI !== undefined) {
|
|
1120
|
-
config['openAPI'] = device.openAPI;
|
|
1250
|
+
if (device.connectionType !== undefined) {
|
|
1251
|
+
config['connectionType'] = device.connectionType;
|
|
1121
1252
|
}
|
|
1122
1253
|
if (device.logging !== undefined) {
|
|
1123
1254
|
config['logging'] = device.logging;
|
|
@@ -1135,39 +1266,6 @@ class Bot {
|
|
|
1135
1266
|
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${(0, super_stringify_1.default)(config)}`);
|
|
1136
1267
|
}
|
|
1137
1268
|
}
|
|
1138
|
-
async refreshRate(device) {
|
|
1139
|
-
if (device.refreshRate) {
|
|
1140
|
-
this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
|
|
1141
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
|
|
1142
|
-
}
|
|
1143
|
-
else if (this.platform.config.options.refreshRate) {
|
|
1144
|
-
this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options.refreshRate;
|
|
1145
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
|
|
1146
|
-
}
|
|
1147
|
-
}
|
|
1148
|
-
async openAPI() {
|
|
1149
|
-
if (!this.device.openAPI) {
|
|
1150
|
-
this.OpenAPI = true;
|
|
1151
|
-
}
|
|
1152
|
-
else {
|
|
1153
|
-
this.OpenAPI = this.device.openAPI;
|
|
1154
|
-
}
|
|
1155
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI: ${this.OpenAPI}`);
|
|
1156
|
-
}
|
|
1157
|
-
async scan(device) {
|
|
1158
|
-
if (device.scanDuration) {
|
|
1159
|
-
this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
|
|
1160
|
-
if (device.ble) {
|
|
1161
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
|
|
1162
|
-
}
|
|
1163
|
-
}
|
|
1164
|
-
else {
|
|
1165
|
-
this.scanDuration = this.accessory.context.scanDuration = 1;
|
|
1166
|
-
if (this.device.ble) {
|
|
1167
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
|
|
1168
|
-
}
|
|
1169
|
-
}
|
|
1170
|
-
}
|
|
1171
1269
|
async logs(device) {
|
|
1172
1270
|
if (this.platform.debugMode) {
|
|
1173
1271
|
this.deviceLogging = this.accessory.context.logging = 'debugMode';
|
|
@@ -1186,130 +1284,6 @@ class Bot {
|
|
|
1186
1284
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
|
|
1187
1285
|
}
|
|
1188
1286
|
}
|
|
1189
|
-
FirmwareRevision(accessory, device) {
|
|
1190
|
-
let FirmwareRevision;
|
|
1191
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
|
|
1192
|
-
+ ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
|
|
1193
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
|
|
1194
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
|
|
1195
|
-
if (accessory.context.FirmwareRevision) {
|
|
1196
|
-
FirmwareRevision = accessory.context.FirmwareRevision;
|
|
1197
|
-
}
|
|
1198
|
-
else if (device.firmware) {
|
|
1199
|
-
FirmwareRevision = device.firmware;
|
|
1200
|
-
}
|
|
1201
|
-
else {
|
|
1202
|
-
FirmwareRevision = this.platform.version;
|
|
1203
|
-
}
|
|
1204
|
-
return FirmwareRevision;
|
|
1205
|
-
}
|
|
1206
|
-
async PressOrSwitch(device) {
|
|
1207
|
-
if (!device.bot?.mode) {
|
|
1208
|
-
this.botMode = 'switch';
|
|
1209
|
-
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} does not have bot mode set in the Plugin's SwitchBot Device Settings,`);
|
|
1210
|
-
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} is defaulting to "${this.botMode}" mode, you may experience issues.`);
|
|
1211
|
-
}
|
|
1212
|
-
else if (device.bot?.mode === 'switch') {
|
|
1213
|
-
this.botMode = 'switch';
|
|
1214
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Bot Mode: ${this.botMode}`);
|
|
1215
|
-
}
|
|
1216
|
-
else if (device.bot?.mode === 'press') {
|
|
1217
|
-
this.botMode = 'press';
|
|
1218
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Bot Mode: ${this.botMode}`);
|
|
1219
|
-
}
|
|
1220
|
-
else {
|
|
1221
|
-
throw new Error(`${this.device.deviceType}: ${this.accessory.displayName} Bot Mode: ${this.botMode}`);
|
|
1222
|
-
}
|
|
1223
|
-
}
|
|
1224
|
-
async allowPushChanges(device) {
|
|
1225
|
-
if (device.bot?.allowPush) {
|
|
1226
|
-
this.allowPush = true;
|
|
1227
|
-
}
|
|
1228
|
-
else {
|
|
1229
|
-
this.allowPush = false;
|
|
1230
|
-
}
|
|
1231
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Allowing Push Changes: ${this.allowPush}`);
|
|
1232
|
-
}
|
|
1233
|
-
async removeOutletService(accessory) {
|
|
1234
|
-
// If outletService still pressent, then remove first
|
|
1235
|
-
this.outletService = this.accessory.getService(this.platform.Service.Outlet);
|
|
1236
|
-
if (this.outletService) {
|
|
1237
|
-
this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Outlet Service`);
|
|
1238
|
-
}
|
|
1239
|
-
accessory.removeService(this.outletService);
|
|
1240
|
-
}
|
|
1241
|
-
async removeGarageDoorService(accessory) {
|
|
1242
|
-
// If garageDoorService still pressent, then remove first
|
|
1243
|
-
this.garageDoorService = this.accessory.getService(this.platform.Service.GarageDoorOpener);
|
|
1244
|
-
if (this.garageDoorService) {
|
|
1245
|
-
this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Garage Door Service`);
|
|
1246
|
-
}
|
|
1247
|
-
accessory.removeService(this.garageDoorService);
|
|
1248
|
-
}
|
|
1249
|
-
async removeDoorService(accessory) {
|
|
1250
|
-
// If doorService still pressent, then remove first
|
|
1251
|
-
this.doorService = this.accessory.getService(this.platform.Service.Door);
|
|
1252
|
-
if (this.doorService) {
|
|
1253
|
-
this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Door Service`);
|
|
1254
|
-
}
|
|
1255
|
-
accessory.removeService(this.doorService);
|
|
1256
|
-
}
|
|
1257
|
-
async removeLockService(accessory) {
|
|
1258
|
-
// If lockService still pressent, then remove first
|
|
1259
|
-
this.lockService = this.accessory.getService(this.platform.Service.LockMechanism);
|
|
1260
|
-
if (this.lockService) {
|
|
1261
|
-
this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Lock Service`);
|
|
1262
|
-
}
|
|
1263
|
-
accessory.removeService(this.lockService);
|
|
1264
|
-
}
|
|
1265
|
-
async removeFaucetService(accessory) {
|
|
1266
|
-
// If faucetService still pressent, then remove first
|
|
1267
|
-
this.faucetService = this.accessory.getService(this.platform.Service.Faucet);
|
|
1268
|
-
if (this.faucetService) {
|
|
1269
|
-
this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Faucet Service`);
|
|
1270
|
-
}
|
|
1271
|
-
accessory.removeService(this.faucetService);
|
|
1272
|
-
}
|
|
1273
|
-
async removeFanService(accessory) {
|
|
1274
|
-
// If fanService still pressent, then remove first
|
|
1275
|
-
this.fanService = this.accessory.getService(this.platform.Service.Fan);
|
|
1276
|
-
if (this.fanService) {
|
|
1277
|
-
this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Fan Service`);
|
|
1278
|
-
}
|
|
1279
|
-
accessory.removeService(this.fanService);
|
|
1280
|
-
}
|
|
1281
|
-
async removeWindowService(accessory) {
|
|
1282
|
-
// If windowService still pressent, then remove first
|
|
1283
|
-
this.windowService = this.accessory.getService(this.platform.Service.Window);
|
|
1284
|
-
if (this.windowService) {
|
|
1285
|
-
this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Window Service`);
|
|
1286
|
-
}
|
|
1287
|
-
accessory.removeService(this.windowService);
|
|
1288
|
-
}
|
|
1289
|
-
async removeWindowCoveringService(accessory) {
|
|
1290
|
-
// If windowCoveringService still pressent, then remove first
|
|
1291
|
-
this.windowCoveringService = this.accessory.getService(this.platform.Service.WindowCovering);
|
|
1292
|
-
if (this.windowCoveringService) {
|
|
1293
|
-
this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Window Covering Service`);
|
|
1294
|
-
}
|
|
1295
|
-
accessory.removeService(this.windowCoveringService);
|
|
1296
|
-
}
|
|
1297
|
-
async removeStatefulProgrammableSwitchService(accessory) {
|
|
1298
|
-
// If statefulProgrammableSwitchService still pressent, then remove first
|
|
1299
|
-
this.statefulProgrammableSwitchService = this.accessory.getService(this.platform.Service.StatefulProgrammableSwitch);
|
|
1300
|
-
if (this.statefulProgrammableSwitchService) {
|
|
1301
|
-
this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Stateful Programmable Switch Service`);
|
|
1302
|
-
}
|
|
1303
|
-
accessory.removeService(this.statefulProgrammableSwitchService);
|
|
1304
|
-
}
|
|
1305
|
-
async removeSwitchService(accessory) {
|
|
1306
|
-
// If switchService still pressent, then remove first
|
|
1307
|
-
this.switchService = this.accessory.getService(this.platform.Service.Switch);
|
|
1308
|
-
if (this.switchService) {
|
|
1309
|
-
this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Switch Service`);
|
|
1310
|
-
}
|
|
1311
|
-
accessory.removeService(this.switchService);
|
|
1312
|
-
}
|
|
1313
1287
|
/**
|
|
1314
1288
|
* Logging for Device
|
|
1315
1289
|
*/
|