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