@switchbot/homebridge-switchbot 1.6.4-beta.1 → 1.6.4-beta.5
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 +3 -1
- package/config.schema.json +6 -0
- package/dist/devices/bots.d.ts +3 -2
- package/dist/devices/bots.d.ts.map +1 -1
- package/dist/devices/bots.js +229 -149
- package/dist/devices/bots.js.map +1 -1
- package/dist/devices/contact.d.ts +2 -2
- package/dist/devices/contact.d.ts.map +1 -1
- package/dist/devices/contact.js +26 -9
- package/dist/devices/contact.js.map +1 -1
- package/dist/devices/curtains.d.ts +2 -2
- package/dist/devices/curtains.d.ts.map +1 -1
- package/dist/devices/curtains.js +27 -10
- package/dist/devices/curtains.js.map +1 -1
- package/dist/devices/humidifiers.d.ts +2 -2
- package/dist/devices/humidifiers.d.ts.map +1 -1
- package/dist/devices/humidifiers.js +28 -11
- package/dist/devices/humidifiers.js.map +1 -1
- package/dist/devices/meters.d.ts.map +1 -1
- package/dist/devices/meters.js +27 -10
- package/dist/devices/meters.js.map +1 -1
- package/dist/devices/motion.d.ts +2 -2
- package/dist/devices/motion.d.ts.map +1 -1
- package/dist/devices/motion.js +27 -10
- package/dist/devices/motion.js.map +1 -1
- package/dist/platform.d.ts +0 -2
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +0 -12
- package/dist/platform.js.map +1 -1
- package/package.json +2 -2
- package/src/devices/bots.ts +208 -129
- package/src/devices/contact.ts +25 -9
- package/src/devices/curtains.ts +26 -10
- package/src/devices/humidifiers.ts +27 -11
- package/src/devices/meters.ts +26 -11
- package/src/devices/motion.ts +26 -10
- package/src/platform.ts +0 -13
package/src/devices/bots.ts
CHANGED
|
@@ -15,6 +15,7 @@ export class Bot {
|
|
|
15
15
|
// Services
|
|
16
16
|
private switchService?: Service;
|
|
17
17
|
private doorService?: Service;
|
|
18
|
+
private lockService?: Service;
|
|
18
19
|
private garageDoorService?: Service;
|
|
19
20
|
private outletService?: Service;
|
|
20
21
|
private batteryService?: Service;
|
|
@@ -97,6 +98,7 @@ export class Bot {
|
|
|
97
98
|
// get the LightBulb service if it exists, otherwise create a new LightBulb service
|
|
98
99
|
// you can create multiple services for each accessory
|
|
99
100
|
if (device.bot?.deviceType === 'switch') {
|
|
101
|
+
this.removeLockService(accessory);
|
|
100
102
|
this.removeDoorService(accessory);
|
|
101
103
|
this.removeOutletService(accessory);
|
|
102
104
|
this.removeGarageDoorService(accessory);
|
|
@@ -110,6 +112,7 @@ export class Bot {
|
|
|
110
112
|
this.switchService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
|
|
111
113
|
this.switchService.getCharacteristic(this.platform.Characteristic.On).onSet(this.handleOnSet.bind(this));
|
|
112
114
|
} else if (device.bot?.deviceType === 'garagedoor') {
|
|
115
|
+
this.removeLockService(accessory);
|
|
113
116
|
this.removeDoorService(accessory);
|
|
114
117
|
this.removeOutletService(accessory);
|
|
115
118
|
this.removeSwitchService(accessory);
|
|
@@ -124,6 +127,7 @@ export class Bot {
|
|
|
124
127
|
this.garageDoorService.getCharacteristic(this.platform.Characteristic.TargetDoorState).onSet(this.handleOnSet.bind(this));
|
|
125
128
|
this.garageDoorService.setCharacteristic(this.platform.Characteristic.ObstructionDetected, false);
|
|
126
129
|
} else if (device.bot?.deviceType === 'door') {
|
|
130
|
+
this.removeLockService(accessory);
|
|
127
131
|
this.removeOutletService(accessory);
|
|
128
132
|
this.removeSwitchService(accessory);
|
|
129
133
|
this.removeGarageDoorService(accessory);
|
|
@@ -132,7 +136,7 @@ export class Bot {
|
|
|
132
136
|
(this.doorService =
|
|
133
137
|
accessory.getService(this.platform.Service.Door) ||
|
|
134
138
|
accessory.addService(this.platform.Service.Door)), `${accessory.displayName} Door`;
|
|
135
|
-
this.infoLog(`Bot: ${accessory.displayName} Displaying as
|
|
139
|
+
this.infoLog(`Bot: ${accessory.displayName} Displaying as Door`);
|
|
136
140
|
|
|
137
141
|
this.doorService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
|
|
138
142
|
this.doorService.getCharacteristic(this.platform.Characteristic.TargetPosition)
|
|
@@ -143,7 +147,22 @@ export class Bot {
|
|
|
143
147
|
minStep: 100,
|
|
144
148
|
}).onSet(this.handleOnSet.bind(this));
|
|
145
149
|
this.doorService.setCharacteristic(this.platform.Characteristic.PositionState, this.platform.Characteristic.PositionState.STOPPED);
|
|
150
|
+
} else if (device.bot?.deviceType === 'lock') {
|
|
151
|
+
this.removeDoorService(accessory);
|
|
152
|
+
this.removeOutletService(accessory);
|
|
153
|
+
this.removeSwitchService(accessory);
|
|
154
|
+
this.removeGarageDoorService(accessory);
|
|
155
|
+
|
|
156
|
+
// Add switchService
|
|
157
|
+
(this.lockService =
|
|
158
|
+
accessory.getService(this.platform.Service.LockMechanism) ||
|
|
159
|
+
accessory.addService(this.platform.Service.LockMechanism)), `${accessory.displayName} Lock`;
|
|
160
|
+
this.infoLog(`Bot: ${accessory.displayName} Displaying as Lock`);
|
|
161
|
+
|
|
162
|
+
this.lockService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
|
|
163
|
+
this.lockService.getCharacteristic(this.platform.Characteristic.LockTargetState).onSet(this.handleOnSet.bind(this));
|
|
146
164
|
} else {
|
|
165
|
+
this.removeLockService(accessory);
|
|
147
166
|
this.removeDoorService(accessory);
|
|
148
167
|
this.removeSwitchService(accessory);
|
|
149
168
|
this.removeGarageDoorService(accessory);
|
|
@@ -209,37 +228,46 @@ export class Bot {
|
|
|
209
228
|
|
|
210
229
|
private removeOutletService(accessory: PlatformAccessory) {
|
|
211
230
|
// If outletService still pressent, then remove first
|
|
231
|
+
this.outletService = this.accessory.getService(this.platform.Service.Outlet);
|
|
212
232
|
if (this.outletService) {
|
|
213
|
-
this.
|
|
233
|
+
this.warnLog(`Bot: ${accessory.displayName} Removing Leftover Outlet Service`);
|
|
214
234
|
}
|
|
215
|
-
this.outletService = this.accessory.getService(this.platform.Service.Outlet);
|
|
216
235
|
accessory.removeService(this.outletService!);
|
|
217
236
|
}
|
|
218
237
|
|
|
219
238
|
private removeGarageDoorService(accessory: PlatformAccessory) {
|
|
220
239
|
// If garageDoorService still pressent, then remove first
|
|
240
|
+
this.garageDoorService = this.accessory.getService(this.platform.Service.GarageDoorOpener);
|
|
221
241
|
if (this.garageDoorService) {
|
|
222
|
-
this.
|
|
242
|
+
this.warnLog(`Bot: ${accessory.displayName} Removing Leftover Garage Door Service`);
|
|
223
243
|
}
|
|
224
|
-
this.garageDoorService = this.accessory.getService(this.platform.Service.GarageDoorOpener);
|
|
225
244
|
accessory.removeService(this.garageDoorService!);
|
|
226
245
|
}
|
|
227
246
|
|
|
228
247
|
private removeDoorService(accessory: PlatformAccessory) {
|
|
229
248
|
// If doorService still pressent, then remove first
|
|
249
|
+
this.doorService = this.accessory.getService(this.platform.Service.Door);
|
|
230
250
|
if (this.doorService) {
|
|
231
|
-
this.
|
|
251
|
+
this.warnLog(`Bot: ${accessory.displayName} Removing Leftover Door Service`);
|
|
232
252
|
}
|
|
233
|
-
this.doorService = this.accessory.getService(this.platform.Service.Door);
|
|
234
253
|
accessory.removeService(this.doorService!);
|
|
235
254
|
}
|
|
236
255
|
|
|
256
|
+
private removeLockService(accessory: PlatformAccessory) {
|
|
257
|
+
// If lockService still pressent, then remove first
|
|
258
|
+
this.lockService = this.accessory.getService(this.platform.Service.LockMechanism);
|
|
259
|
+
if (this.lockService) {
|
|
260
|
+
this.warnLog(`Bot: ${accessory.displayName} Removing Leftover Lock Service`);
|
|
261
|
+
}
|
|
262
|
+
accessory.removeService(this.lockService!);
|
|
263
|
+
}
|
|
264
|
+
|
|
237
265
|
private removeSwitchService(accessory: PlatformAccessory) {
|
|
238
266
|
// If switchService still pressent, then remove first
|
|
267
|
+
this.switchService = this.accessory.getService(this.platform.Service.Switch);
|
|
239
268
|
if (this.switchService) {
|
|
240
|
-
this.
|
|
269
|
+
this.warnLog(`Bot: ${accessory.displayName} Removing Leftover Switch Service`);
|
|
241
270
|
}
|
|
242
|
-
this.switchService = this.accessory.getService(this.platform.Service.Switch);
|
|
243
271
|
accessory.removeService(this.switchService!);
|
|
244
272
|
}
|
|
245
273
|
|
|
@@ -379,11 +407,27 @@ export class Bot {
|
|
|
379
407
|
}
|
|
380
408
|
}
|
|
381
409
|
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
410
|
+
public async connectBLE() {
|
|
411
|
+
let switchbot: any;
|
|
412
|
+
try {
|
|
413
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
414
|
+
const Switchbot = require('node-switchbot');
|
|
415
|
+
switchbot = new Switchbot();
|
|
416
|
+
// Convert to BLE Address
|
|
417
|
+
this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
|
|
418
|
+
this.debugLog(`Bot: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
419
|
+
} catch (e: any) {
|
|
420
|
+
switchbot = false;
|
|
421
|
+
this.errorLog(`Bot: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot}`);
|
|
422
|
+
if (this.deviceLogging === 'debug') {
|
|
423
|
+
this.errorLog(`Bot: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
|
|
424
|
+
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
425
|
+
}
|
|
426
|
+
if (this.platform.debugMode) {
|
|
427
|
+
this.errorLog(`Bot: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
|
|
428
|
+
+ ` Error: ${JSON.stringify(e)}`);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
387
431
|
return switchbot;
|
|
388
432
|
}
|
|
389
433
|
|
|
@@ -391,69 +435,64 @@ export class Bot {
|
|
|
391
435
|
this.debugLog(`Bot: ${this.accessory.displayName} BLE refreshStatus`);
|
|
392
436
|
|
|
393
437
|
// Convert to BLE Address
|
|
394
|
-
const switchbot = this.connectBLE();
|
|
438
|
+
const switchbot = await this.connectBLE();
|
|
395
439
|
// Start to monitor advertisement packets
|
|
396
|
-
if (switchbot
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
this.debugLog(`Bot: ${this.accessory.displayName} connected: ${this.connected}`);
|
|
417
|
-
}
|
|
418
|
-
};
|
|
419
|
-
// Wait 2 seconds
|
|
420
|
-
return switchbot.wait(this.scanDuration * 1000);
|
|
421
|
-
}).then(async () => {
|
|
422
|
-
// Stop to monitor
|
|
423
|
-
switchbot.stopScan();
|
|
424
|
-
if (this.connected) {
|
|
425
|
-
this.parseStatus();
|
|
426
|
-
this.updateHomeKitCharacteristics();
|
|
440
|
+
if (switchbot === false) {
|
|
441
|
+
this.errorLog(`Bot: ${this.accessory.displayName} wasn't able to establish BLE Connection: ${switchbot}`);
|
|
442
|
+
}
|
|
443
|
+
switchbot.startScan({
|
|
444
|
+
model: 'H',
|
|
445
|
+
id: this.device.bleMac,
|
|
446
|
+
}).then(() => {
|
|
447
|
+
// Set an event hander
|
|
448
|
+
switchbot.onadvertisement = (ad: ad) => {
|
|
449
|
+
this.serviceData = ad.serviceData;
|
|
450
|
+
this.mode = ad.serviceData.mode;
|
|
451
|
+
this.state = ad.serviceData.state;
|
|
452
|
+
this.battery = ad.serviceData.battery;
|
|
453
|
+
this.debugLog(`Bot: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
454
|
+
this.debugLog(`Bot: ${this.accessory.displayName}, model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName},`
|
|
455
|
+
+ ` mode: ${ad.serviceData.mode}, state: ${ad.serviceData.state}, battery: ${ad.serviceData.battery}`);
|
|
456
|
+
|
|
457
|
+
if (this.serviceData) {
|
|
458
|
+
this.connected = true;
|
|
459
|
+
this.debugLog(`Bot: ${this.accessory.displayName} connected: ${this.connected}`);
|
|
427
460
|
} else {
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
}).catch(async (e: any) => {
|
|
431
|
-
this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
|
|
432
|
-
if (this.deviceLogging === 'debug') {
|
|
433
|
-
this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
|
|
434
|
-
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
435
|
-
}
|
|
436
|
-
if (this.platform.debugMode) {
|
|
437
|
-
this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
|
|
438
|
-
+ ` Error: ${JSON.stringify(e)}`);
|
|
461
|
+
this.connected = false;
|
|
462
|
+
this.debugLog(`Bot: ${this.accessory.displayName} connected: ${this.connected}`);
|
|
439
463
|
}
|
|
464
|
+
};
|
|
465
|
+
// Wait 2 seconds
|
|
466
|
+
return switchbot.wait(this.scanDuration * 1000);
|
|
467
|
+
}).then(async () => {
|
|
468
|
+
// Stop to monitor
|
|
469
|
+
switchbot.stopScan();
|
|
470
|
+
if (this.connected) {
|
|
471
|
+
this.parseStatus();
|
|
472
|
+
this.updateHomeKitCharacteristics();
|
|
473
|
+
} else {
|
|
474
|
+
this.errorLog(`Bot: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
|
|
440
475
|
if (this.platform.config.credentials?.openToken) {
|
|
441
476
|
this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
442
477
|
await this.openAPIRefreshStatus();
|
|
443
478
|
}
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
479
|
+
}
|
|
480
|
+
}).catch(async (e: any) => {
|
|
481
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
|
|
482
|
+
if (this.deviceLogging === 'debug') {
|
|
483
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
|
|
484
|
+
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
485
|
+
}
|
|
486
|
+
if (this.platform.debugMode) {
|
|
487
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
|
|
488
|
+
+ ` Error: ${JSON.stringify(e)}`);
|
|
489
|
+
}
|
|
490
|
+
if (this.platform.config.credentials?.openToken) {
|
|
491
|
+
this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
492
|
+
await this.openAPIRefreshStatus();
|
|
493
|
+
}
|
|
494
|
+
this.apiError(e);
|
|
495
|
+
});
|
|
457
496
|
}
|
|
458
497
|
|
|
459
498
|
private async openAPIRefreshStatus() {
|
|
@@ -501,54 +540,30 @@ export class Bot {
|
|
|
501
540
|
}
|
|
502
541
|
|
|
503
542
|
private async BLEpushChanges() {
|
|
543
|
+
this.debugLog(`Bot: ${this.accessory.displayName} BLE pushChanges On: ${this.On} OnCached: ${this.OnCached}`);
|
|
504
544
|
if (this.On !== this.OnCached) {
|
|
505
545
|
this.debugLog(`Bot: ${this.accessory.displayName} BLE pushChanges`);
|
|
506
|
-
const switchbot = this.connectBLE();
|
|
507
|
-
if (switchbot !== false) {
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
this.infoLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
|
|
513
|
-
return device_list[0].press({ id: this.device.bleMac });
|
|
514
|
-
}).then(() => {
|
|
515
|
-
this.debugLog(`Bot: ${this.accessory.displayName} Done.`);
|
|
516
|
-
this.On = false;
|
|
517
|
-
this.OnCached = this.On;
|
|
518
|
-
this.accessory.context.On = this.OnCached;
|
|
519
|
-
setTimeout(() => {
|
|
520
|
-
if (this.device.bot?.deviceType === 'switch') {
|
|
521
|
-
this.switchService?.getCharacteristic(this.platform.Characteristic.On).updateValue(this.On);
|
|
522
|
-
} else {
|
|
523
|
-
this.outletService?.getCharacteristic(this.platform.Characteristic.On).updateValue(this.On);
|
|
524
|
-
}
|
|
525
|
-
this.debugLog(`Bot: ${this.accessory.displayName} On: ${this.On}, Switch Timeout`);
|
|
526
|
-
}, 500);
|
|
527
|
-
}).catch(async (e: any) => {
|
|
528
|
-
this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
|
|
529
|
-
if (this.deviceLogging === 'debug') {
|
|
530
|
-
this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
|
|
531
|
-
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
532
|
-
}
|
|
533
|
-
if (this.platform.debugMode) {
|
|
534
|
-
this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
|
|
535
|
-
+ ` Error: ${JSON.stringify(e)}`);
|
|
536
|
-
}
|
|
537
|
-
if (this.platform.config.credentials?.openToken) {
|
|
538
|
-
this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
539
|
-
await this.openAPIpushChanges();
|
|
540
|
-
}
|
|
541
|
-
this.apiError(e);
|
|
542
|
-
});
|
|
543
|
-
} else if (this.botMode === 'switch') {
|
|
544
|
-
this.debugLog(`Bot: ${this.accessory.displayName} Press Mode: ${this.botMode}`);
|
|
545
|
-
switchbot.discover({ model: 'H', quick: true, id: this.device.bleMac }).then((device_list: any) => {
|
|
546
|
+
const switchbot = await this.connectBLE();
|
|
547
|
+
//if (switchbot !== false) {
|
|
548
|
+
if (this.botMode === 'press') {
|
|
549
|
+
this.debugLog(`Bot: ${this.accessory.displayName} Bot Mode: ${this.botMode}`);
|
|
550
|
+
switchbot.discover({ model: 'H', quick: true, id: this.device.bleMac })
|
|
551
|
+
.then((device_list: { press: (arg0: { id: string | undefined; }) => any; }[]) => {
|
|
546
552
|
this.infoLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
|
|
547
|
-
return this.
|
|
553
|
+
return device_list[0].press({ id: this.device.bleMac });
|
|
548
554
|
}).then(() => {
|
|
549
555
|
this.debugLog(`Bot: ${this.accessory.displayName} Done.`);
|
|
556
|
+
this.On = false;
|
|
550
557
|
this.OnCached = this.On;
|
|
551
558
|
this.accessory.context.On = this.OnCached;
|
|
559
|
+
setTimeout(() => {
|
|
560
|
+
if (this.device.bot?.deviceType === 'switch') {
|
|
561
|
+
this.switchService?.getCharacteristic(this.platform.Characteristic.On).updateValue(this.On);
|
|
562
|
+
} else {
|
|
563
|
+
this.outletService?.getCharacteristic(this.platform.Characteristic.On).updateValue(this.On);
|
|
564
|
+
}
|
|
565
|
+
this.debugLog(`Bot: ${this.accessory.displayName} On: ${this.On}, Switch Timeout`);
|
|
566
|
+
}, 500);
|
|
552
567
|
}).catch(async (e: any) => {
|
|
553
568
|
this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
|
|
554
569
|
if (this.deviceLogging === 'debug') {
|
|
@@ -565,16 +580,41 @@ export class Bot {
|
|
|
565
580
|
}
|
|
566
581
|
this.apiError(e);
|
|
567
582
|
});
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
}
|
|
583
|
+
} else if (this.botMode === 'switch') {
|
|
584
|
+
this.debugLog(`Bot: ${this.accessory.displayName} Press Mode: ${this.botMode}`);
|
|
585
|
+
switchbot.discover({ model: 'H', quick: true, id: this.device.bleMac }).then((device_list: any) => {
|
|
586
|
+
this.infoLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
|
|
587
|
+
return this.turnOnOff(device_list);
|
|
588
|
+
}).then(() => {
|
|
589
|
+
this.debugLog(`Bot: ${this.accessory.displayName} Done.`);
|
|
590
|
+
this.OnCached = this.On;
|
|
591
|
+
this.accessory.context.On = this.OnCached;
|
|
592
|
+
}).catch(async (e: any) => {
|
|
593
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
|
|
594
|
+
if (this.deviceLogging === 'debug') {
|
|
595
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
|
|
596
|
+
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
597
|
+
}
|
|
598
|
+
if (this.platform.debugMode) {
|
|
599
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
|
|
600
|
+
+ ` Error: ${JSON.stringify(e)}`);
|
|
601
|
+
}
|
|
602
|
+
if (this.platform.config.credentials?.openToken) {
|
|
603
|
+
this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
604
|
+
await this.openAPIpushChanges();
|
|
605
|
+
}
|
|
606
|
+
this.apiError(e);
|
|
607
|
+
});
|
|
571
608
|
} else {
|
|
572
|
-
|
|
573
|
-
if (this.platform.config.credentials?.openToken) {
|
|
574
|
-
this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
575
|
-
await this.openAPIpushChanges();
|
|
576
|
-
}
|
|
609
|
+
throw new Error(`Bot: ${this.accessory.displayName} Bot Mode: ${this.botMode}`);
|
|
577
610
|
}
|
|
611
|
+
/* } else {
|
|
612
|
+
this.errorLog(`Bot: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
|
|
613
|
+
if (this.platform.config.credentials?.openToken) {
|
|
614
|
+
this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
615
|
+
await this.openAPIpushChanges();
|
|
616
|
+
}
|
|
617
|
+
}*/
|
|
578
618
|
}
|
|
579
619
|
this.OnCached = this.On;
|
|
580
620
|
this.accessory.context.On = this.OnCached;
|
|
@@ -661,22 +701,45 @@ export class Bot {
|
|
|
661
701
|
this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic TargetDoorState: Open, CurrentDoorState: Open`);
|
|
662
702
|
}
|
|
663
703
|
}
|
|
664
|
-
this.debugLog(`Bot: ${this.accessory.displayName}
|
|
704
|
+
this.debugLog(`Bot: ${this.accessory.displayName} Garage Door On: ${this.On}`);
|
|
665
705
|
} else if (this.device.bot?.deviceType === 'door') {
|
|
666
706
|
if (this.On === undefined) {
|
|
667
707
|
this.debugLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
|
|
668
708
|
} else {
|
|
669
709
|
if (this.On) {
|
|
670
|
-
this.
|
|
671
|
-
this.
|
|
710
|
+
this.doorService?.updateCharacteristic(this.platform.Characteristic.TargetPosition, 100);
|
|
711
|
+
this.doorService?.updateCharacteristic(this.platform.Characteristic.CurrentPosition, 100);
|
|
712
|
+
this.doorService?.updateCharacteristic(this.platform.Characteristic.PositionState,
|
|
713
|
+
this.platform.Characteristic.PositionState.STOPPED);
|
|
672
714
|
this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic TargetPosition: 100, CurrentPosition: 100`);
|
|
673
715
|
} else {
|
|
674
|
-
this.
|
|
675
|
-
this.
|
|
716
|
+
this.doorService?.updateCharacteristic(this.platform.Characteristic.TargetPosition, 0);
|
|
717
|
+
this.doorService?.updateCharacteristic(this.platform.Characteristic.CurrentPosition, 0);
|
|
718
|
+
this.doorService?.updateCharacteristic(this.platform.Characteristic.PositionState,
|
|
719
|
+
this.platform.Characteristic.PositionState.STOPPED);
|
|
676
720
|
this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic TargetPosition: 0, CurrentPosition: 0`);
|
|
677
721
|
}
|
|
678
722
|
}
|
|
679
|
-
this.debugLog(`Bot: ${this.accessory.displayName}
|
|
723
|
+
this.debugLog(`Bot: ${this.accessory.displayName} Door On: ${this.On}`);
|
|
724
|
+
} else if (this.device.bot?.deviceType === 'lock') {
|
|
725
|
+
if (this.On === undefined) {
|
|
726
|
+
this.debugLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
|
|
727
|
+
} else {
|
|
728
|
+
if (this.On) {
|
|
729
|
+
this.lockService?.updateCharacteristic(this.platform.Characteristic.LockTargetState,
|
|
730
|
+
this.platform.Characteristic.LockTargetState.UNSECURED);
|
|
731
|
+
this.lockService?.updateCharacteristic(this.platform.Characteristic.LockCurrentState,
|
|
732
|
+
this.platform.Characteristic.LockCurrentState.UNSECURED);
|
|
733
|
+
this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic LockTargetState: UNSECURED, LockCurrentState: UNSECURED`);
|
|
734
|
+
} else {
|
|
735
|
+
this.lockService?.updateCharacteristic(this.platform.Characteristic.LockTargetState,
|
|
736
|
+
this.platform.Characteristic.LockTargetState.SECURED);
|
|
737
|
+
this.lockService?.updateCharacteristic(this.platform.Characteristic.LockCurrentState,
|
|
738
|
+
this.platform.Characteristic.LockCurrentState.SECURED);
|
|
739
|
+
this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic LockTargetState: SECURED, LockCurrentState: SECURED`);
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
this.debugLog(`Bot: ${this.accessory.displayName} Lock On: ${this.On}`);
|
|
680
743
|
} else if (this.device.bot?.deviceType === 'switch') {
|
|
681
744
|
if (this.On === undefined) {
|
|
682
745
|
this.debugLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
|
|
@@ -692,8 +755,6 @@ export class Bot {
|
|
|
692
755
|
this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
|
|
693
756
|
}
|
|
694
757
|
}
|
|
695
|
-
|
|
696
|
-
|
|
697
758
|
if (this.device.ble) {
|
|
698
759
|
if (this.BatteryLevel === undefined) {
|
|
699
760
|
this.debugLog(`Bot: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
|
|
@@ -711,7 +772,18 @@ export class Bot {
|
|
|
711
772
|
}
|
|
712
773
|
|
|
713
774
|
public apiError(e: any) {
|
|
714
|
-
if (this.device.bot?.deviceType === '
|
|
775
|
+
if (this.device.bot?.deviceType === 'garagedoor') {
|
|
776
|
+
this.garageDoorService?.updateCharacteristic(this.platform.Characteristic.TargetDoorState, e);
|
|
777
|
+
this.garageDoorService?.updateCharacteristic(this.platform.Characteristic.CurrentDoorState, e);
|
|
778
|
+
this.garageDoorService?.updateCharacteristic(this.platform.Characteristic.ObstructionDetected, e);
|
|
779
|
+
} else if (this.device.bot?.deviceType === 'door') {
|
|
780
|
+
this.doorService?.updateCharacteristic(this.platform.Characteristic.TargetPosition, e);
|
|
781
|
+
this.doorService?.updateCharacteristic(this.platform.Characteristic.CurrentPosition, e);
|
|
782
|
+
this.doorService?.updateCharacteristic(this.platform.Characteristic.PositionState, e);
|
|
783
|
+
} else if (this.device.bot?.deviceType === 'lock') {
|
|
784
|
+
this.doorService?.updateCharacteristic(this.platform.Characteristic.LockTargetState, e);
|
|
785
|
+
this.doorService?.updateCharacteristic(this.platform.Characteristic.LockCurrentState, e);
|
|
786
|
+
} else if (this.device.bot?.deviceType === 'switch') {
|
|
715
787
|
this.switchService?.updateCharacteristic(this.platform.Characteristic.On, e);
|
|
716
788
|
} else {
|
|
717
789
|
this.outletService?.updateCharacteristic(this.platform.Characteristic.On, e);
|
|
@@ -778,7 +850,14 @@ export class Bot {
|
|
|
778
850
|
}
|
|
779
851
|
} else if (this.device.bot?.deviceType === 'door') {
|
|
780
852
|
this.debugLog(`Bot: ${this.accessory.displayName} TargetPosition: ${value}`);
|
|
781
|
-
if (value ===
|
|
853
|
+
if (value === 0) {
|
|
854
|
+
this.On = false;
|
|
855
|
+
} else {
|
|
856
|
+
this.On = true;
|
|
857
|
+
}
|
|
858
|
+
} else if (this.device.bot?.deviceType === 'lock') {
|
|
859
|
+
this.debugLog(`Bot: ${this.accessory.displayName} LockTargetState: ${value}`);
|
|
860
|
+
if (value === this.platform.Characteristic.LockTargetState.SECURED) {
|
|
782
861
|
this.On = false;
|
|
783
862
|
} else {
|
|
784
863
|
this.On = true;
|
package/src/devices/contact.ts
CHANGED
|
@@ -273,18 +273,34 @@ export class Contact {
|
|
|
273
273
|
}
|
|
274
274
|
}
|
|
275
275
|
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
276
|
+
public async connectBLE() {
|
|
277
|
+
let switchbot: any;
|
|
278
|
+
try {
|
|
279
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
280
|
+
const Switchbot = require('node-switchbot');
|
|
281
|
+
switchbot = new Switchbot();
|
|
282
|
+
// Convert to BLE Address
|
|
283
|
+
this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
|
|
284
|
+
this.debugLog(`Contact Sensor: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
285
|
+
} catch (e: any) {
|
|
286
|
+
switchbot = false;
|
|
287
|
+
this.errorLog(`Contact Sensor: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot}`);
|
|
288
|
+
if (this.deviceLogging === 'debug') {
|
|
289
|
+
this.errorLog(`Contact Sensor: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
|
|
290
|
+
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
291
|
+
}
|
|
292
|
+
if (this.platform.debugMode) {
|
|
293
|
+
this.errorLog(`Contact Sensor: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
|
|
294
|
+
+ ` Error: ${JSON.stringify(e)}`);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
281
297
|
return switchbot;
|
|
282
298
|
}
|
|
283
299
|
|
|
284
300
|
private async BLERefreshStatus() {
|
|
285
301
|
this.debugLog(`Contact Sensor: ${this.accessory.displayName} BLE refreshStatus`);
|
|
286
302
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
287
|
-
const switchbot = this.connectBLE();
|
|
303
|
+
const switchbot = await this.connectBLE();
|
|
288
304
|
// Start to monitor advertisement packets
|
|
289
305
|
if (switchbot !== false) {
|
|
290
306
|
switchbot.startScan({
|
|
@@ -342,12 +358,12 @@ export class Contact {
|
|
|
342
358
|
this.apiError(e);
|
|
343
359
|
});
|
|
344
360
|
} else {
|
|
345
|
-
await this.BLEconnection();
|
|
361
|
+
await this.BLEconnection(switchbot);
|
|
346
362
|
}
|
|
347
363
|
}
|
|
348
364
|
|
|
349
|
-
|
|
350
|
-
this.errorLog(`Contact Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
|
|
365
|
+
public async BLEconnection(switchbot: any) {
|
|
366
|
+
this.errorLog(`Contact Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
|
|
351
367
|
if (this.platform.config.credentials?.openToken) {
|
|
352
368
|
this.warnLog(`Contact Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
353
369
|
await this.openAPIRefreshStatus();
|
package/src/devices/curtains.ts
CHANGED
|
@@ -253,11 +253,27 @@ export class Curtain {
|
|
|
253
253
|
}
|
|
254
254
|
}
|
|
255
255
|
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
256
|
+
public async connectBLE() {
|
|
257
|
+
let switchbot: any;
|
|
258
|
+
try {
|
|
259
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
260
|
+
const Switchbot = require('node-switchbot');
|
|
261
|
+
switchbot = new Switchbot();
|
|
262
|
+
// Convert to BLE Address
|
|
263
|
+
this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
|
|
264
|
+
this.debugLog(`Curtain: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
265
|
+
} catch (e: any) {
|
|
266
|
+
switchbot = false;
|
|
267
|
+
this.errorLog(`Curtain: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot}`);
|
|
268
|
+
if (this.deviceLogging === 'debug') {
|
|
269
|
+
this.errorLog(`Curtain: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
|
|
270
|
+
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
271
|
+
}
|
|
272
|
+
if (this.platform.debugMode) {
|
|
273
|
+
this.errorLog(`Curtain: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
|
|
274
|
+
+ ` Error: ${JSON.stringify(e)}`);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
261
277
|
return switchbot;
|
|
262
278
|
}
|
|
263
279
|
|
|
@@ -452,7 +468,7 @@ export class Curtain {
|
|
|
452
468
|
private async BLERefreshStatus() {
|
|
453
469
|
this.debugLog(`Curtain: ${this.accessory.displayName} BLE refreshStatus`);
|
|
454
470
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
455
|
-
const switchbot = this.connectBLE();
|
|
471
|
+
const switchbot = await this.connectBLE();
|
|
456
472
|
// Start to monitor advertisement packets
|
|
457
473
|
if (switchbot !== false) {
|
|
458
474
|
switchbot.startScan({
|
|
@@ -510,12 +526,12 @@ export class Curtain {
|
|
|
510
526
|
this.apiError(e);
|
|
511
527
|
});
|
|
512
528
|
} else {
|
|
513
|
-
await this.BLEconnection();
|
|
529
|
+
await this.BLEconnection(switchbot);
|
|
514
530
|
}
|
|
515
531
|
}
|
|
516
532
|
|
|
517
|
-
|
|
518
|
-
this.errorLog(`Curtain: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
|
|
533
|
+
public async BLEconnection(switchbot: any) {
|
|
534
|
+
this.errorLog(`Curtain: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
|
|
519
535
|
if (this.platform.config.credentials?.openToken) {
|
|
520
536
|
this.warnLog(`Curtain: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
521
537
|
await this.openAPIRefreshStatus();
|
|
@@ -562,7 +578,7 @@ export class Curtain {
|
|
|
562
578
|
private async BLEpushChanges() {
|
|
563
579
|
if (this.TargetPosition !== this.CurrentPosition) {
|
|
564
580
|
this.debugLog(`Curtain: ${this.accessory.displayName} BLE pushChanges`);
|
|
565
|
-
const switchbot = this.connectBLE();
|
|
581
|
+
const switchbot = await this.connectBLE();
|
|
566
582
|
if (switchbot !== false) {
|
|
567
583
|
switchbot.discover({ model: 'c', quick: true, id: this.device.bleMac }).then((device_list) => {
|
|
568
584
|
this.infoLog(`${this.accessory.displayName} Target Position: ${this.TargetPosition}`);
|