@switchbot/homebridge-switchbot 1.6.2-beta.1 → 1.6.2-beta.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/dist/devices/bots.d.ts +1 -0
- package/dist/devices/bots.d.ts.map +1 -1
- package/dist/devices/bots.js +127 -109
- 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 +64 -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 +94 -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 +89 -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 +67 -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 +59 -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 +1 -1
- package/src/devices/bots.ts +105 -88
- package/src/devices/contact.ts +64 -51
- package/src/devices/curtains.ts +93 -71
- package/src/devices/humidifiers.ts +88 -70
- package/src/devices/meters.ts +67 -58
- package/src/devices/motion.ts +59 -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,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
|
@@ -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,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`);
|
|
@@ -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,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 (
|