@switchbot/homebridge-switchbot 1.6.1 → 1.6.2
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 +10 -0
- package/dist/devices/bots.d.ts +1 -0
- package/dist/devices/bots.d.ts.map +1 -1
- package/dist/devices/bots.js +130 -114
- package/dist/devices/bots.js.map +1 -1
- package/dist/devices/contact.d.ts +1 -0
- package/dist/devices/contact.d.ts.map +1 -1
- package/dist/devices/contact.js +65 -55
- package/dist/devices/contact.js.map +1 -1
- package/dist/devices/curtains.d.ts +2 -1
- package/dist/devices/curtains.d.ts.map +1 -1
- package/dist/devices/curtains.js +95 -75
- package/dist/devices/curtains.js.map +1 -1
- package/dist/devices/humidifiers.d.ts +2 -1
- package/dist/devices/humidifiers.d.ts.map +1 -1
- package/dist/devices/humidifiers.js +90 -74
- package/dist/devices/humidifiers.js.map +1 -1
- package/dist/devices/meters.d.ts +1 -0
- package/dist/devices/meters.d.ts.map +1 -1
- package/dist/devices/meters.js +68 -62
- package/dist/devices/meters.js.map +1 -1
- package/dist/devices/motion.d.ts +1 -0
- package/dist/devices/motion.d.ts.map +1 -1
- package/dist/devices/motion.js +60 -54
- package/dist/devices/motion.js.map +1 -1
- package/dist/devices/plugs.js +2 -2
- package/dist/devices/plugs.js.map +1 -1
- package/dist/platform.d.ts +2 -0
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +12 -0
- package/dist/platform.js.map +1 -1
- package/package.json +4 -4
- package/src/devices/bots.ts +109 -92
- package/src/devices/contact.ts +65 -52
- package/src/devices/curtains.ts +94 -72
- package/src/devices/humidifiers.ts +89 -71
- package/src/devices/meters.ts +69 -59
- package/src/devices/motion.ts +60 -51
- package/src/devices/plugs.ts +2 -2
- package/src/platform.ts +13 -0
package/src/devices/contact.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import Switchbot from 'node-switchbot';
|
|
2
1
|
import { interval, Subject } from 'rxjs';
|
|
3
2
|
import { skipWhile } from 'rxjs/operators';
|
|
4
3
|
import { SwitchBotPlatform } from '../platform';
|
|
@@ -163,12 +162,12 @@ export class Contact {
|
|
|
163
162
|
if (this.device.scanDuration) {
|
|
164
163
|
this.scanDuration = this.accessory.context.scanDuration = this.device.scanDuration;
|
|
165
164
|
if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
|
|
166
|
-
this.warnLog(`
|
|
165
|
+
this.warnLog(`Contact Sensor: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
|
|
167
166
|
}
|
|
168
167
|
} else {
|
|
169
168
|
this.scanDuration = this.accessory.context.scanDuration = 1;
|
|
170
169
|
if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
|
|
171
|
-
this.warnLog(`
|
|
170
|
+
this.warnLog(`Contact Sensor: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
|
|
172
171
|
}
|
|
173
172
|
}
|
|
174
173
|
}
|
|
@@ -275,9 +274,10 @@ export class Contact {
|
|
|
275
274
|
}
|
|
276
275
|
|
|
277
276
|
private connectBLE() {
|
|
278
|
-
|
|
277
|
+
// Convert to BLE Address
|
|
279
278
|
this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
|
|
280
279
|
this.debugLog(`Contact Sensor: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
280
|
+
const switchbot = this.platform.connectBLE();
|
|
281
281
|
return switchbot;
|
|
282
282
|
}
|
|
283
283
|
|
|
@@ -286,60 +286,73 @@ export class Contact {
|
|
|
286
286
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
287
287
|
const switchbot = this.connectBLE();
|
|
288
288
|
// Start to monitor advertisement packets
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
289
|
+
this.debugLog(`Contact Sensor: ${this.accessory.displayName} platform.Switchbot: ${JSON.stringify(switchbot)}`);
|
|
290
|
+
if (switchbot) {
|
|
291
|
+
switchbot.startScan({
|
|
292
|
+
model: 'd',
|
|
293
|
+
id: this.device.bleMac,
|
|
294
|
+
}).then(() => {
|
|
295
|
+
// Set an event hander
|
|
296
|
+
switchbot.onadvertisement = (ad: any) => {
|
|
297
|
+
this.serviceData = ad.serviceData;
|
|
298
|
+
this.movement = ad.serviceData.movement;
|
|
299
|
+
this.doorState = ad.serviceData.doorState;
|
|
300
|
+
this.lightLevel = ad.serviceData.lightLevel;
|
|
301
|
+
this.battery = ad.serviceData.battery;
|
|
302
|
+
this.debugLog(`Contact Sensor: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
303
|
+
this.debugLog(`Contact Sensor: ${this.accessory.displayName} movement: ${ad.serviceData.movement}, doorState: `
|
|
304
|
+
+ `${ad.serviceData.doorState}, lightLevel: ${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}`);
|
|
305
|
+
|
|
306
|
+
if (this.serviceData) {
|
|
307
|
+
this.connected = true;
|
|
308
|
+
this.debugLog(`Contact Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
|
|
309
|
+
} else {
|
|
310
|
+
this.connected = false;
|
|
311
|
+
this.debugLog(`Contact Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
// Wait 2 seconds
|
|
315
|
+
return switchbot.wait(this.scanDuration * 1000);
|
|
316
|
+
}).then(async () => {
|
|
317
|
+
// Stop to monitor
|
|
318
|
+
switchbot.stopScan();
|
|
319
|
+
if (this.connected) {
|
|
320
|
+
this.parseStatus();
|
|
321
|
+
this.updateHomeKitCharacteristics();
|
|
307
322
|
} else {
|
|
308
|
-
this.
|
|
309
|
-
|
|
323
|
+
this.errorLog(`Contact Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
|
|
324
|
+
if (this.platform.config.credentials?.openToken) {
|
|
325
|
+
this.warnLog(`Contact Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
326
|
+
await this.openAPIRefreshStatus();
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}).catch(async (e: any) => {
|
|
330
|
+
this.errorLog(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
|
|
331
|
+
if (this.deviceLogging === 'debug') {
|
|
332
|
+
this.errorLog(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
|
|
333
|
+
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
334
|
+
}
|
|
335
|
+
if (this.platform.debugMode) {
|
|
336
|
+
this.errorLog(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
|
|
337
|
+
+ ` Error: ${JSON.stringify(e)}`);
|
|
310
338
|
}
|
|
311
|
-
};
|
|
312
|
-
// Wait 2 seconds
|
|
313
|
-
return switchbot.wait(this.scanDuration * 1000);
|
|
314
|
-
}).then(async () => {
|
|
315
|
-
// Stop to monitor
|
|
316
|
-
switchbot.stopScan();
|
|
317
|
-
if (this.connected) {
|
|
318
|
-
this.parseStatus();
|
|
319
|
-
this.updateHomeKitCharacteristics();
|
|
320
|
-
} else {
|
|
321
|
-
this.errorLog(`Contact Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
|
|
322
339
|
if (this.platform.config.credentials?.openToken) {
|
|
323
340
|
this.warnLog(`Contact Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
324
341
|
await this.openAPIRefreshStatus();
|
|
325
342
|
}
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
await this.openAPIRefreshStatus();
|
|
340
|
-
}
|
|
341
|
-
this.apiError(e);
|
|
342
|
-
});
|
|
343
|
+
this.apiError(e);
|
|
344
|
+
});
|
|
345
|
+
} else {
|
|
346
|
+
await this.BLEconnection();
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
private async BLEconnection() {
|
|
351
|
+
this.errorLog(`Contact Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
|
|
352
|
+
if (this.platform.config.credentials?.openToken) {
|
|
353
|
+
this.warnLog(`Contact Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
354
|
+
await this.openAPIRefreshStatus();
|
|
355
|
+
}
|
|
343
356
|
}
|
|
344
357
|
|
|
345
358
|
private async openAPIRefreshStatus() {
|
package/src/devices/curtains.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
|
|
2
2
|
import { AxiosResponse } from 'axios';
|
|
3
|
-
import Switchbot from 'node-switchbot';
|
|
4
3
|
import { interval, Subject } from 'rxjs';
|
|
5
4
|
import { SwitchBotPlatform } from '../platform';
|
|
6
5
|
import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
|
|
@@ -225,12 +224,12 @@ export class Curtain {
|
|
|
225
224
|
if (this.device.scanDuration) {
|
|
226
225
|
this.scanDuration = this.accessory.context.scanDuration = this.device.scanDuration;
|
|
227
226
|
if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
|
|
228
|
-
this.warnLog(`
|
|
227
|
+
this.warnLog(`Curtain: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
|
|
229
228
|
}
|
|
230
229
|
} else {
|
|
231
230
|
this.scanDuration = this.accessory.context.scanDuration = 1;
|
|
232
231
|
if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
|
|
233
|
-
this.warnLog(`
|
|
232
|
+
this.warnLog(`Curtain: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
|
|
234
233
|
}
|
|
235
234
|
}
|
|
236
235
|
}
|
|
@@ -255,9 +254,10 @@ export class Curtain {
|
|
|
255
254
|
}
|
|
256
255
|
|
|
257
256
|
private connectBLE() {
|
|
258
|
-
|
|
257
|
+
// Convert to BLE Address
|
|
259
258
|
this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
|
|
260
259
|
this.debugLog(`Curtain: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
260
|
+
const switchbot = this.platform.connectBLE();
|
|
261
261
|
return switchbot;
|
|
262
262
|
}
|
|
263
263
|
|
|
@@ -454,60 +454,73 @@ export class Curtain {
|
|
|
454
454
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
455
455
|
const switchbot = this.connectBLE();
|
|
456
456
|
// Start to monitor advertisement packets
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
457
|
+
this.debugLog(`Curtain: ${this.accessory.displayName} platform.Switchbot: ${JSON.stringify(switchbot)}`);
|
|
458
|
+
if (switchbot) {
|
|
459
|
+
switchbot.startScan({
|
|
460
|
+
model: 'c',
|
|
461
|
+
id: this.device.bleMac,
|
|
462
|
+
}).then(() => {
|
|
463
|
+
// Set an event hander
|
|
464
|
+
switchbot.onadvertisement = (ad: any) => {
|
|
465
|
+
this.serviceData = ad.serviceData;
|
|
466
|
+
this.calibration = ad.serviceData.calibration;
|
|
467
|
+
this.battery = ad.serviceData.battery;
|
|
468
|
+
this.position = ad.serviceData.position;
|
|
469
|
+
this.lightLevel = ad.serviceData.lightLevel;
|
|
470
|
+
this.debugLog(`Curtain: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
471
|
+
this.debugLog(`Curtain: ${this.accessory.displayName} calibration: ${ad.serviceData.calibration}, `
|
|
472
|
+
+ `position: ${ad.serviceData.position}, lightLevel: ${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}`);
|
|
473
|
+
|
|
474
|
+
if (this.serviceData) {
|
|
475
|
+
this.connected = true;
|
|
476
|
+
this.debugLog(`Curtain: ${this.accessory.displayName} connected: ${this.connected}`);
|
|
477
|
+
} else {
|
|
478
|
+
this.connected = false;
|
|
479
|
+
this.debugLog(`Curtain: ${this.accessory.displayName} connected: ${this.connected}`);
|
|
480
|
+
}
|
|
481
|
+
};
|
|
482
|
+
// Wait 2 seconds
|
|
483
|
+
return switchbot.wait(this.scanDuration * 1000);
|
|
484
|
+
}).then(async () => {
|
|
485
|
+
// Stop to monitor
|
|
486
|
+
switchbot.stopScan();
|
|
487
|
+
if (this.connected) {
|
|
488
|
+
this.parseStatus();
|
|
489
|
+
this.updateHomeKitCharacteristics();
|
|
475
490
|
} else {
|
|
476
|
-
this.
|
|
477
|
-
|
|
491
|
+
this.errorLog(`Curtain: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
|
|
492
|
+
if (this.platform.config.credentials?.openToken) {
|
|
493
|
+
this.warnLog(`Curtain: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
494
|
+
await this.openAPIRefreshStatus();
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}).catch(async (e: any) => {
|
|
498
|
+
this.errorLog(`Curtain: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
|
|
499
|
+
if (this.deviceLogging === 'debug') {
|
|
500
|
+
this.errorLog(`Curtain: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
|
|
501
|
+
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
502
|
+
}
|
|
503
|
+
if (this.platform.debugMode) {
|
|
504
|
+
this.errorLog(`Curtain: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
|
|
505
|
+
+ ` Error: ${JSON.stringify(e)}`);
|
|
478
506
|
}
|
|
479
|
-
};
|
|
480
|
-
// Wait 2 seconds
|
|
481
|
-
return switchbot.wait(this.scanDuration * 1000);
|
|
482
|
-
}).then(async () => {
|
|
483
|
-
// Stop to monitor
|
|
484
|
-
switchbot.stopScan();
|
|
485
|
-
if (this.connected) {
|
|
486
|
-
this.parseStatus();
|
|
487
|
-
this.updateHomeKitCharacteristics();
|
|
488
|
-
} else {
|
|
489
|
-
this.errorLog(`Curtain: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
|
|
490
507
|
if (this.platform.config.credentials?.openToken) {
|
|
491
508
|
this.warnLog(`Curtain: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
492
509
|
await this.openAPIRefreshStatus();
|
|
493
510
|
}
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
await this.openAPIRefreshStatus();
|
|
508
|
-
}
|
|
509
|
-
this.apiError(e);
|
|
510
|
-
});
|
|
511
|
+
this.apiError(e);
|
|
512
|
+
});
|
|
513
|
+
} else {
|
|
514
|
+
await this.BLEconnection();
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
private async BLEconnection() {
|
|
519
|
+
this.errorLog(`Curtain: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
|
|
520
|
+
if (this.platform.config.credentials?.openToken) {
|
|
521
|
+
this.warnLog(`Curtain: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
522
|
+
await this.openAPIRefreshStatus();
|
|
523
|
+
}
|
|
511
524
|
}
|
|
512
525
|
|
|
513
526
|
private async openAPIRefreshStatus() {
|
|
@@ -537,7 +550,7 @@ export class Curtain {
|
|
|
537
550
|
if (this.device.ble) {
|
|
538
551
|
await this.BLEpushChanges();
|
|
539
552
|
} else {
|
|
540
|
-
await this.
|
|
553
|
+
await this.openAPIpushChanges();
|
|
541
554
|
}
|
|
542
555
|
interval(5000)
|
|
543
556
|
.pipe(skipWhile(() => this.curtainUpdateInProgress))
|
|
@@ -551,34 +564,43 @@ export class Curtain {
|
|
|
551
564
|
if (this.TargetPosition !== this.CurrentPosition) {
|
|
552
565
|
this.debugLog(`Curtain: ${this.accessory.displayName} BLE pushChanges`);
|
|
553
566
|
const switchbot = this.connectBLE();
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
this.errorLog(`Curtain: ${this.accessory.displayName} failed pushChanges with BLE Connection
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
567
|
+
this.debugLog(`Curtain: ${this.accessory.displayName} platform.Switchbot: ${JSON.stringify(switchbot)}`);
|
|
568
|
+
if (switchbot) {
|
|
569
|
+
switchbot.discover({ model: 'c', quick: true, id: this.device.bleMac }).then((device_list) => {
|
|
570
|
+
this.infoLog(`${this.accessory.displayName} Target Position: ${this.TargetPosition}`);
|
|
571
|
+
return device_list[0].runToPos(100 - Number(this.TargetPosition));
|
|
572
|
+
}).then(() => {
|
|
573
|
+
this.debugLog(`Curtain: ${this.accessory.displayName} Done.`);
|
|
574
|
+
}).catch(async (e: any) => {
|
|
575
|
+
this.errorLog(`Curtain: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
|
|
576
|
+
if (this.deviceLogging === 'debug') {
|
|
577
|
+
this.errorLog(`Curtain: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
|
|
578
|
+
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
579
|
+
}
|
|
580
|
+
if (this.platform.debugMode) {
|
|
581
|
+
this.errorLog(`Curtain: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
|
|
582
|
+
+ ` Error: ${JSON.stringify(e)}`);
|
|
583
|
+
}
|
|
584
|
+
if (this.platform.config.credentials?.openToken) {
|
|
585
|
+
this.warnLog(`Curtain: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
586
|
+
await this.openAPIpushChanges();
|
|
587
|
+
}
|
|
588
|
+
this.apiError(e);
|
|
589
|
+
});
|
|
590
|
+
} else {
|
|
591
|
+
this.errorLog(`Curtain: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
|
|
569
592
|
if (this.platform.config.credentials?.openToken) {
|
|
570
593
|
this.warnLog(`Curtain: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
571
|
-
await this.
|
|
594
|
+
await this.openAPIpushChanges();
|
|
572
595
|
}
|
|
573
|
-
|
|
574
|
-
});
|
|
596
|
+
}
|
|
575
597
|
} else {
|
|
576
598
|
this.debugLog(`Curtain: ${this.accessory.displayName} No BLE Changes, CurrentPosition & TargetPosition Are the Same.`
|
|
577
599
|
+ ` CurrentPosition: ${this.CurrentPosition}, TargetPosition ${this.TargetPosition}`);
|
|
578
600
|
}
|
|
579
601
|
}
|
|
580
602
|
|
|
581
|
-
private async
|
|
603
|
+
private async openAPIpushChanges() {
|
|
582
604
|
if (this.platform.config.credentials?.openToken) {
|
|
583
605
|
try {
|
|
584
606
|
this.debugLog(`Curtain: ${this.accessory.displayName} OpenAPI pushChanges`);
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { AxiosResponse } from 'axios';
|
|
2
|
-
import Switchbot from 'node-switchbot';
|
|
3
2
|
import { interval, Subject } from 'rxjs';
|
|
4
3
|
import { SwitchBotPlatform } from '../platform';
|
|
5
4
|
import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
|
|
@@ -210,12 +209,12 @@ export class Humidifier {
|
|
|
210
209
|
if (this.device.scanDuration) {
|
|
211
210
|
this.scanDuration = this.accessory.context.scanDuration = this.device.scanDuration;
|
|
212
211
|
if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
|
|
213
|
-
this.warnLog(`
|
|
212
|
+
this.warnLog(`Humidifier: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
|
|
214
213
|
}
|
|
215
214
|
} else {
|
|
216
215
|
this.scanDuration = this.accessory.context.scanDuration = 1;
|
|
217
216
|
if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
|
|
218
|
-
this.warnLog(`
|
|
217
|
+
this.warnLog(`Humidifier: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
|
|
219
218
|
}
|
|
220
219
|
}
|
|
221
220
|
}
|
|
@@ -341,9 +340,10 @@ export class Humidifier {
|
|
|
341
340
|
}
|
|
342
341
|
|
|
343
342
|
private connectBLE() {
|
|
344
|
-
|
|
343
|
+
// Convert to BLE Address
|
|
345
344
|
this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
|
|
346
345
|
this.debugLog(`Humidifier: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
346
|
+
const switchbot = this.platform.connectBLE();
|
|
347
347
|
return switchbot;
|
|
348
348
|
}
|
|
349
349
|
|
|
@@ -352,59 +352,68 @@ export class Humidifier {
|
|
|
352
352
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
353
353
|
const switchbot = this.connectBLE();
|
|
354
354
|
// Start to monitor advertisement packets
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
355
|
+
this.debugLog(`Humidifier: ${this.accessory.displayName} platform.Switchbot: ${JSON.stringify(switchbot)}`);
|
|
356
|
+
if (switchbot) {
|
|
357
|
+
switchbot.startScan({
|
|
358
|
+
model: 'e',
|
|
359
|
+
id: this.device.bleMac,
|
|
360
|
+
}).then(() => {
|
|
361
|
+
// Set an event hander
|
|
362
|
+
switchbot.onadvertisement = (ad: ad) => {
|
|
363
|
+
this.serviceData = ad.serviceData;
|
|
364
|
+
this.autoMode = ad.serviceData.autoMode;
|
|
365
|
+
this.onState = ad.serviceData.onState;
|
|
366
|
+
this.percentage = ad.serviceData.percentage;
|
|
367
|
+
this.debugLog(`Humidifier: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
368
|
+
this.debugLog(`Humidifier: ${this.accessory.displayName} model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName},`
|
|
369
|
+
+ `autoMode: ${ad.serviceData.autoMode}, onState: ${ad.serviceData.onState}, percentage: ${ad.serviceData.percentage}`);
|
|
370
|
+
|
|
371
|
+
if (this.serviceData) {
|
|
372
|
+
this.connected = true;
|
|
373
|
+
this.debugLog(`Humidifier: ${this.accessory.displayName} connected: ${this.connected}`);
|
|
374
|
+
} else {
|
|
375
|
+
this.connected = false;
|
|
376
|
+
this.debugLog(`Humidifier: ${this.accessory.displayName} connected: ${this.connected}`);
|
|
377
|
+
}
|
|
378
|
+
};
|
|
379
|
+
// Wait 2 seconds
|
|
380
|
+
return switchbot.wait(this.scanDuration * 1000);
|
|
381
|
+
}).then(async () => {
|
|
382
|
+
// Stop to monitor
|
|
383
|
+
switchbot.stopScan();
|
|
384
|
+
if (this.connected) {
|
|
385
|
+
this.parseStatus();
|
|
386
|
+
this.updateHomeKitCharacteristics();
|
|
372
387
|
} else {
|
|
373
|
-
this.
|
|
374
|
-
|
|
388
|
+
await this.BLEconnection();
|
|
389
|
+
}
|
|
390
|
+
}).catch(async (e: any) => {
|
|
391
|
+
this.errorLog(`Humidifier: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
|
|
392
|
+
if (this.deviceLogging === 'debug') {
|
|
393
|
+
this.errorLog(`Humidifier: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
|
|
394
|
+
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
395
|
+
}
|
|
396
|
+
if (this.platform.debugMode) {
|
|
397
|
+
this.errorLog(`Humidifier: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
|
|
398
|
+
+ ` Error: ${JSON.stringify(e)}`);
|
|
375
399
|
}
|
|
376
|
-
};
|
|
377
|
-
// Wait 2 seconds
|
|
378
|
-
return switchbot.wait(this.scanDuration * 1000);
|
|
379
|
-
}).then(async () => {
|
|
380
|
-
// Stop to monitor
|
|
381
|
-
switchbot.stopScan();
|
|
382
|
-
if (this.connected) {
|
|
383
|
-
this.parseStatus();
|
|
384
|
-
this.updateHomeKitCharacteristics();
|
|
385
|
-
} else {
|
|
386
|
-
this.errorLog(`Humidifier: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
|
|
387
400
|
if (this.platform.config.credentials?.openToken) {
|
|
388
401
|
this.warnLog(`Humidifier: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
389
402
|
await this.openAPIRefreshStatus();
|
|
390
403
|
}
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
await this.openAPIRefreshStatus();
|
|
405
|
-
}
|
|
406
|
-
this.apiError(e);
|
|
407
|
-
});
|
|
404
|
+
this.apiError(e);
|
|
405
|
+
});
|
|
406
|
+
} else {
|
|
407
|
+
await this.BLEconnection();
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
private async BLEconnection() {
|
|
412
|
+
this.errorLog(`Humidifier: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
|
|
413
|
+
if (this.platform.config.credentials?.openToken) {
|
|
414
|
+
this.warnLog(`Humidifier: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
415
|
+
await this.openAPIRefreshStatus();
|
|
416
|
+
}
|
|
408
417
|
}
|
|
409
418
|
|
|
410
419
|
private async openAPIRefreshStatus() {
|
|
@@ -441,7 +450,7 @@ export class Humidifier {
|
|
|
441
450
|
//if (this.device.ble) {
|
|
442
451
|
// await this.BLEpushChanges();
|
|
443
452
|
//} else {
|
|
444
|
-
await this.
|
|
453
|
+
await this.openAPIpushChanges();
|
|
445
454
|
//}
|
|
446
455
|
interval(5000)
|
|
447
456
|
.pipe(skipWhile(() => this.humidifierUpdateInProgress))
|
|
@@ -454,30 +463,39 @@ export class Humidifier {
|
|
|
454
463
|
private async BLEpushChanges() {
|
|
455
464
|
this.debugLog(`Humidifier: ${this.accessory.displayName} BLE pushChanges`);
|
|
456
465
|
const switchbot = this.connectBLE();
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushChanges with BLE Connection
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
466
|
+
this.debugLog(`Humidifier: ${this.accessory.displayName} platform.Switchbot: ${JSON.stringify(switchbot)}`);
|
|
467
|
+
if (switchbot) {
|
|
468
|
+
switchbot.discover({ model: 'e', quick: true, id: this.device.bleMac }).then((device_list) => {
|
|
469
|
+
this.infoLog(`${this.accessory.displayName} Target Position: ${this.Active}`);
|
|
470
|
+
return device_list[0].percentage(this.RelativeHumidityHumidifierThreshold);
|
|
471
|
+
}).then(() => {
|
|
472
|
+
this.debugLog(`Humidifier: ${this.accessory.displayName} Done.`);
|
|
473
|
+
}).catch(async (e: any) => {
|
|
474
|
+
this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
|
|
475
|
+
if (this.deviceLogging === 'debug') {
|
|
476
|
+
this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
|
|
477
|
+
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
478
|
+
}
|
|
479
|
+
if (this.platform.debugMode) {
|
|
480
|
+
this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
|
|
481
|
+
+ ` Error: ${JSON.stringify(e)}`);
|
|
482
|
+
}
|
|
483
|
+
if (this.platform.config.credentials?.openToken) {
|
|
484
|
+
this.warnLog(`Humidifier: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
485
|
+
await this.openAPIpushChanges();
|
|
486
|
+
}
|
|
487
|
+
this.apiError(e);
|
|
488
|
+
});
|
|
489
|
+
} else {
|
|
490
|
+
this.errorLog(`Humidifier: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
|
|
472
491
|
if (this.platform.config.credentials?.openToken) {
|
|
473
492
|
this.warnLog(`Humidifier: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
474
|
-
await this.
|
|
493
|
+
await this.openAPIpushChanges();
|
|
475
494
|
}
|
|
476
|
-
|
|
477
|
-
});
|
|
495
|
+
}
|
|
478
496
|
}
|
|
479
497
|
|
|
480
|
-
private async
|
|
498
|
+
private async openAPIpushChanges() {
|
|
481
499
|
if (this.platform.config.credentials?.openToken) {
|
|
482
500
|
this.debugLog(`Humidifier: ${this.accessory.displayName} OpenAPI pushChanges`);
|
|
483
501
|
if (
|