@switchbot/homebridge-switchbot 3.5.0 → 3.5.1-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/.github/npm-version-script.cjs +81 -0
- package/config.schema.json +4 -4
- package/dist/device/blindtilt.d.ts.map +1 -1
- package/dist/device/blindtilt.js +32 -63
- package/dist/device/blindtilt.js.map +1 -1
- package/dist/device/bot.d.ts.map +1 -1
- package/dist/device/bot.js +31 -63
- package/dist/device/bot.js.map +1 -1
- package/dist/device/ceilinglight.d.ts.map +1 -1
- package/dist/device/ceilinglight.js +51 -111
- package/dist/device/ceilinglight.js.map +1 -1
- package/dist/device/colorbulb.d.ts +1 -1
- package/dist/device/colorbulb.d.ts.map +1 -1
- package/dist/device/colorbulb.js +38 -113
- package/dist/device/colorbulb.js.map +1 -1
- package/dist/device/contact.d.ts.map +1 -1
- package/dist/device/contact.js +22 -49
- package/dist/device/contact.js.map +1 -1
- package/dist/device/curtain.d.ts.map +1 -1
- package/dist/device/curtain.js +53 -61
- package/dist/device/curtain.js.map +1 -1
- package/dist/device/device.d.ts +17 -1
- package/dist/device/device.d.ts.map +1 -1
- package/dist/device/device.js +112 -5
- package/dist/device/device.js.map +1 -1
- package/dist/device/fan.d.ts.map +1 -1
- package/dist/device/fan.js +42 -95
- package/dist/device/fan.js.map +1 -1
- package/dist/device/hub.d.ts +7 -1
- package/dist/device/hub.d.ts.map +1 -1
- package/dist/device/hub.js +52 -14
- package/dist/device/hub.js.map +1 -1
- package/dist/device/humidifier.d.ts.map +1 -1
- package/dist/device/humidifier.js +52 -99
- package/dist/device/humidifier.js.map +1 -1
- package/dist/device/iosensor.d.ts.map +1 -1
- package/dist/device/iosensor.js +25 -43
- package/dist/device/iosensor.js.map +1 -1
- package/dist/device/lightstrip.d.ts +1 -1
- package/dist/device/lightstrip.d.ts.map +1 -1
- package/dist/device/lightstrip.js +53 -115
- package/dist/device/lightstrip.js.map +1 -1
- package/dist/device/lock.d.ts +1 -1
- package/dist/device/lock.d.ts.map +1 -1
- package/dist/device/lock.js +33 -61
- package/dist/device/lock.js.map +1 -1
- package/dist/device/meter.d.ts.map +1 -1
- package/dist/device/meter.js +25 -43
- package/dist/device/meter.js.map +1 -1
- package/dist/device/meterplus.d.ts.map +1 -1
- package/dist/device/meterplus.js +24 -42
- package/dist/device/meterplus.js.map +1 -1
- package/dist/device/motion.d.ts.map +1 -1
- package/dist/device/motion.js +22 -40
- package/dist/device/motion.js.map +1 -1
- package/dist/device/plug.d.ts.map +1 -1
- package/dist/device/plug.js +29 -61
- package/dist/device/plug.js.map +1 -1
- package/dist/device/robotvacuumcleaner.d.ts +1 -1
- package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
- package/dist/device/robotvacuumcleaner.js +36 -77
- package/dist/device/robotvacuumcleaner.js.map +1 -1
- package/dist/device/waterdetector.d.ts.map +1 -1
- package/dist/device/waterdetector.js +21 -40
- package/dist/device/waterdetector.js.map +1 -1
- package/dist/irdevice/airconditioner.d.ts.map +1 -1
- package/dist/irdevice/airconditioner.js +8 -25
- package/dist/irdevice/airconditioner.js.map +1 -1
- package/dist/irdevice/airpurifier.d.ts.map +1 -1
- package/dist/irdevice/airpurifier.js +6 -17
- package/dist/irdevice/airpurifier.js.map +1 -1
- package/dist/irdevice/camera.d.ts.map +1 -1
- package/dist/irdevice/camera.js +6 -17
- package/dist/irdevice/camera.js.map +1 -1
- package/dist/irdevice/fan.d.ts.map +1 -1
- package/dist/irdevice/fan.js +6 -17
- package/dist/irdevice/fan.js.map +1 -1
- package/dist/irdevice/irdevice.d.ts +6 -0
- package/dist/irdevice/irdevice.d.ts.map +1 -1
- package/dist/irdevice/irdevice.js +24 -0
- package/dist/irdevice/irdevice.js.map +1 -1
- package/dist/irdevice/light.d.ts.map +1 -1
- package/dist/irdevice/light.js +6 -17
- package/dist/irdevice/light.js.map +1 -1
- package/dist/irdevice/other.d.ts.map +1 -1
- package/dist/irdevice/other.js +6 -17
- package/dist/irdevice/other.js.map +1 -1
- package/dist/irdevice/tv.d.ts.map +1 -1
- package/dist/irdevice/tv.js +6 -17
- package/dist/irdevice/tv.js.map +1 -1
- package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
- package/dist/irdevice/vacuumcleaner.js +6 -17
- package/dist/irdevice/vacuumcleaner.js.map +1 -1
- package/dist/irdevice/waterheater.d.ts.map +1 -1
- package/dist/irdevice/waterheater.js +6 -17
- package/dist/irdevice/waterheater.js.map +1 -1
- package/dist/settings.d.ts +2 -1
- package/dist/settings.d.ts.map +1 -1
- package/dist/utils.d.ts +19 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +20 -1
- package/dist/utils.js.map +1 -1
- package/package.json +8 -8
- package/src/device/blindtilt.ts +30 -63
- package/src/device/bot.ts +29 -63
- package/src/device/ceilinglight.ts +48 -111
- package/src/device/colorbulb.ts +37 -114
- package/src/device/contact.ts +20 -49
- package/src/device/curtain.ts +52 -62
- package/src/device/device.ts +124 -7
- package/src/device/fan.ts +40 -97
- package/src/device/hub.ts +53 -16
- package/src/device/humidifier.ts +49 -99
- package/src/device/iosensor.ts +23 -43
- package/src/device/lightstrip.ts +51 -116
- package/src/device/lock.ts +32 -62
- package/src/device/meter.ts +23 -43
- package/src/device/meterplus.ts +22 -42
- package/src/device/motion.ts +20 -40
- package/src/device/plug.ts +27 -61
- package/src/device/robotvacuumcleaner.ts +33 -78
- package/src/device/waterdetector.ts +20 -40
- package/src/irdevice/airconditioner.ts +8 -23
- package/src/irdevice/airpurifier.ts +6 -17
- package/src/irdevice/camera.ts +6 -17
- package/src/irdevice/fan.ts +6 -17
- package/src/irdevice/irdevice.ts +24 -0
- package/src/irdevice/light.ts +6 -17
- package/src/irdevice/other.ts +6 -17
- package/src/irdevice/tv.ts +6 -17
- package/src/irdevice/vacuumcleaner.ts +6 -17
- package/src/irdevice/waterheater.ts +6 -17
- package/src/settings.ts +2 -1
- package/src/utils.ts +20 -1
|
@@ -2,10 +2,8 @@
|
|
|
2
2
|
*
|
|
3
3
|
* ceilinglight.ts: @switchbot/homebridge-switchbot.
|
|
4
4
|
*/
|
|
5
|
-
import { request } from 'undici';
|
|
6
5
|
import { deviceBase } from './device.js';
|
|
7
6
|
import { interval, Subject } from 'rxjs';
|
|
8
|
-
import { Devices } from '../settings.js';
|
|
9
7
|
import { hs2rgb, rgb2hs, m2hs } from '../utils.js';
|
|
10
8
|
import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
|
|
11
9
|
|
|
@@ -282,58 +280,38 @@ export class CeilingLight extends deviceBase {
|
|
|
282
280
|
|
|
283
281
|
async BLERefreshStatus(): Promise<void> {
|
|
284
282
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
|
|
285
|
-
const switchbot = await this.
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
.
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
292
|
-
this.getCustomBLEAddress(switchbot);
|
|
293
|
-
// Start to monitor advertisement packets
|
|
294
|
-
(async () => {
|
|
283
|
+
const switchbot = await this.switchbotBLE();
|
|
284
|
+
|
|
285
|
+
if (switchbot === undefined) {
|
|
286
|
+
await this.BLERefreshConnection(switchbot);
|
|
287
|
+
} else {
|
|
288
|
+
(async () => {
|
|
295
289
|
// Start to monitor advertisement packets
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.model}`);
|
|
302
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
290
|
+
const serviceData: serviceData = await this.monitorAdvertisementPackets(switchbot);
|
|
291
|
+
// Update HomeKit
|
|
292
|
+
if (serviceData.model !== '' && serviceData.modelName !== '') {
|
|
293
|
+
await this.BLEparseStatus(serviceData);
|
|
294
|
+
await this.updateHomeKitCharacteristics();
|
|
303
295
|
} else {
|
|
304
|
-
this.
|
|
296
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed to get serviceData, serviceData: ${serviceData}`);
|
|
297
|
+
await this.BLERefreshConnection(switchbot);
|
|
305
298
|
}
|
|
306
|
-
};
|
|
307
|
-
// Wait 10 seconds
|
|
308
|
-
await switchbot.wait(this.scanDuration * 1000);
|
|
309
|
-
// Stop to monitor
|
|
310
|
-
await switchbot.stopScan();
|
|
311
|
-
// Update HomeKit
|
|
312
|
-
await this.BLEparseStatus(switchbot.onadvertisement.serviceData);
|
|
313
|
-
await this.updateHomeKitCharacteristics();
|
|
314
|
-
})();
|
|
315
|
-
if (switchbot === undefined) {
|
|
316
|
-
await this.BLERefreshConnection(switchbot);
|
|
299
|
+
})();
|
|
317
300
|
}
|
|
318
301
|
}
|
|
319
302
|
|
|
320
303
|
async openAPIRefreshStatus(): Promise<void> {
|
|
321
304
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
|
|
322
305
|
try {
|
|
323
|
-
const { body, statusCode } = await this.
|
|
324
|
-
`${Devices}/${this.device.deviceId}/status`, { headers: this.platform.generateHeaders() });
|
|
325
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
|
|
306
|
+
const { body, statusCode } = await this.deviceRefreshStatus();
|
|
326
307
|
const deviceStatus: any = await body.json();
|
|
327
|
-
this.
|
|
328
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
|
|
308
|
+
await this.refreshStatusCodes(statusCode, deviceStatus);;
|
|
329
309
|
if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
|
|
330
|
-
this.
|
|
331
|
-
|
|
332
|
-
this.
|
|
333
|
-
this.updateHomeKitCharacteristics();
|
|
310
|
+
await this.successfulRefreshStatus(statusCode, deviceStatus);
|
|
311
|
+
await this.openAPIparseStatus(deviceStatus);
|
|
312
|
+
await this.updateHomeKitCharacteristics();
|
|
334
313
|
} else {
|
|
335
|
-
this.
|
|
336
|
-
this.statusCode(deviceStatus.statusCode);
|
|
314
|
+
await this.statusCodes(statusCode, deviceStatus);
|
|
337
315
|
}
|
|
338
316
|
} catch (e: any) {
|
|
339
317
|
this.apiError(e);
|
|
@@ -403,8 +381,8 @@ export class CeilingLight extends deviceBase {
|
|
|
403
381
|
async pushChanges(): Promise<void> {
|
|
404
382
|
if (!this.device.enableCloudService && this.OpenAPI) {
|
|
405
383
|
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} pushChanges enableCloudService: ${this.device.enableCloudService}`);
|
|
406
|
-
|
|
407
|
-
|
|
384
|
+
} else if (this.BLE) {
|
|
385
|
+
await this.BLEpushChanges();
|
|
408
386
|
} else if (this.OpenAPI && this.platform.config.credentials?.token) {
|
|
409
387
|
await this.openAPIpushChanges();
|
|
410
388
|
} else {
|
|
@@ -428,16 +406,9 @@ export class CeilingLight extends deviceBase {
|
|
|
428
406
|
+ ` BLEpushChanges On: ${this.LightBulb.On} OnCached: ${this.accessory.context.On}`);
|
|
429
407
|
const switchbot = await this.platform.connectBLE();
|
|
430
408
|
// Convert to BLE Address
|
|
431
|
-
|
|
432
|
-
.deviceId!.match(/.{1,2}/g)!
|
|
433
|
-
.join(':')
|
|
434
|
-
.toLowerCase();
|
|
435
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
409
|
+
await this.convertBLEAddress();
|
|
436
410
|
switchbot
|
|
437
|
-
.discover({
|
|
438
|
-
model: 'u',
|
|
439
|
-
id: this.device.bleMac,
|
|
440
|
-
})
|
|
411
|
+
.discover({ model: this.device.bleModel, id: this.device.bleMac })
|
|
441
412
|
.then(async (device_list: any) => {
|
|
442
413
|
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
|
|
443
414
|
return await this.retryBLE({
|
|
@@ -486,24 +457,15 @@ export class CeilingLight extends deviceBase {
|
|
|
486
457
|
});
|
|
487
458
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
|
|
488
459
|
try {
|
|
489
|
-
const { body, statusCode } = await
|
|
490
|
-
body: bodyChange,
|
|
491
|
-
method: 'POST',
|
|
492
|
-
headers: this.platform.generateHeaders(),
|
|
493
|
-
});
|
|
494
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
|
|
460
|
+
const { body, statusCode } = await this.pushChangeRequest(bodyChange);
|
|
495
461
|
const deviceStatus: any = await body.json();
|
|
496
|
-
this.
|
|
497
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
|
|
498
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
|
|
462
|
+
await this.pushStatusCodes(statusCode, deviceStatus);
|
|
499
463
|
if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
|
|
500
|
-
this.
|
|
501
|
-
|
|
502
|
-
this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
503
|
-
+ `request to SwitchBot API, body: ${JSON.stringify(JSON.parse(bodyChange))} sent successfully`);
|
|
464
|
+
await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
|
|
465
|
+
await this.updateHomeKitCharacteristics();
|
|
504
466
|
} else {
|
|
505
|
-
this.statusCode(statusCode);
|
|
506
|
-
this.statusCode(deviceStatus.statusCode);
|
|
467
|
+
await this.statusCode(statusCode);
|
|
468
|
+
await this.statusCode(deviceStatus.statusCode);
|
|
507
469
|
}
|
|
508
470
|
} catch (e: any) {
|
|
509
471
|
this.apiError(e);
|
|
@@ -543,24 +505,15 @@ export class CeilingLight extends deviceBase {
|
|
|
543
505
|
});
|
|
544
506
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
|
|
545
507
|
try {
|
|
546
|
-
const { body, statusCode } = await
|
|
547
|
-
body: bodyChange,
|
|
548
|
-
method: 'POST',
|
|
549
|
-
headers: this.platform.generateHeaders(),
|
|
550
|
-
});
|
|
551
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
|
|
508
|
+
const { body, statusCode } = await this.pushChangeRequest(bodyChange);
|
|
552
509
|
const deviceStatus: any = await body.json();
|
|
553
|
-
this.
|
|
554
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
|
|
555
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
|
|
510
|
+
await this.pushStatusCodes(statusCode, deviceStatus);
|
|
556
511
|
if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
|
|
557
|
-
this.
|
|
558
|
-
|
|
559
|
-
this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
560
|
-
+ `request to SwitchBot API, body: ${JSON.stringify(JSON.parse(bodyChange))} sent successfully`);
|
|
512
|
+
await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
|
|
513
|
+
await this.updateHomeKitCharacteristics();
|
|
561
514
|
} else {
|
|
562
|
-
this.statusCode(statusCode);
|
|
563
|
-
this.statusCode(deviceStatus.statusCode);
|
|
515
|
+
await this.statusCode(statusCode);
|
|
516
|
+
await this.statusCode(deviceStatus.statusCode);
|
|
564
517
|
}
|
|
565
518
|
} catch (e: any) {
|
|
566
519
|
this.apiError(e);
|
|
@@ -585,22 +538,15 @@ export class CeilingLight extends deviceBase {
|
|
|
585
538
|
});
|
|
586
539
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
|
|
587
540
|
try {
|
|
588
|
-
const { body, statusCode } = await
|
|
589
|
-
body: bodyChange,
|
|
590
|
-
method: 'POST',
|
|
591
|
-
headers: this.platform.generateHeaders(),
|
|
592
|
-
});
|
|
593
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
|
|
541
|
+
const { body, statusCode } = await this.pushChangeRequest(bodyChange);
|
|
594
542
|
const deviceStatus: any = await body.json();
|
|
595
|
-
this.
|
|
596
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
|
|
597
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
|
|
543
|
+
await this.pushStatusCodes(statusCode, deviceStatus);
|
|
598
544
|
if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
|
|
599
|
-
this.
|
|
600
|
-
|
|
545
|
+
await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
|
|
546
|
+
await this.updateHomeKitCharacteristics();
|
|
601
547
|
} else {
|
|
602
|
-
this.statusCode(statusCode);
|
|
603
|
-
this.statusCode(deviceStatus.statusCode);
|
|
548
|
+
await this.statusCode(statusCode);
|
|
549
|
+
await this.statusCode(deviceStatus.statusCode);
|
|
604
550
|
}
|
|
605
551
|
} catch (e: any) {
|
|
606
552
|
this.apiError(e);
|
|
@@ -623,24 +569,15 @@ export class CeilingLight extends deviceBase {
|
|
|
623
569
|
});
|
|
624
570
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
|
|
625
571
|
try {
|
|
626
|
-
const { body, statusCode } = await
|
|
627
|
-
body: bodyChange,
|
|
628
|
-
method: 'POST',
|
|
629
|
-
headers: this.platform.generateHeaders(),
|
|
630
|
-
});
|
|
631
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
|
|
572
|
+
const { body, statusCode } = await this.pushChangeRequest(bodyChange);
|
|
632
573
|
const deviceStatus: any = await body.json();
|
|
633
|
-
this.
|
|
634
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
|
|
635
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
|
|
574
|
+
await this.pushStatusCodes(statusCode, deviceStatus);
|
|
636
575
|
if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
|
|
637
|
-
this.
|
|
638
|
-
|
|
639
|
-
this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
640
|
-
+ `request to SwitchBot API, body: ${JSON.stringify(JSON.parse(bodyChange))} sent successfully`);
|
|
576
|
+
await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
|
|
577
|
+
await this.updateHomeKitCharacteristics();
|
|
641
578
|
} else {
|
|
642
|
-
this.statusCode(statusCode);
|
|
643
|
-
this.statusCode(deviceStatus.statusCode);
|
|
579
|
+
await this.statusCode(statusCode);
|
|
580
|
+
await this.statusCode(deviceStatus.statusCode);
|
|
644
581
|
}
|
|
645
582
|
} catch (e: any) {
|
|
646
583
|
this.apiError(e);
|
package/src/device/colorbulb.ts
CHANGED
|
@@ -2,15 +2,13 @@
|
|
|
2
2
|
*
|
|
3
3
|
* blindtilt.ts: @switchbot/homebridge-switchbot.
|
|
4
4
|
*/
|
|
5
|
-
import { request } from 'undici';
|
|
6
5
|
import { deviceBase } from './device.js';
|
|
7
6
|
import { interval, Subject } from 'rxjs';
|
|
8
|
-
import { Devices } from '../settings.js';
|
|
9
7
|
import { hs2rgb, rgb2hs, m2hs } from '../utils.js';
|
|
10
8
|
import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
|
|
11
9
|
|
|
12
10
|
import type { SwitchBotPlatform } from '../platform.js';
|
|
13
|
-
import type { device, devicesConfig,
|
|
11
|
+
import type { device, devicesConfig, serviceData, deviceStatus } from '../settings.js';
|
|
14
12
|
import type { Service, PlatformAccessory, CharacteristicValue, ControllerConstructor, Controller, ControllerServiceMap } from 'homebridge';
|
|
15
13
|
|
|
16
14
|
/**
|
|
@@ -308,58 +306,39 @@ export class ColorBulb extends deviceBase {
|
|
|
308
306
|
|
|
309
307
|
async BLERefreshStatus(): Promise<void> {
|
|
310
308
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
|
|
311
|
-
const switchbot = await this.
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
.
|
|
315
|
-
|
|
316
|
-
.toLowerCase();
|
|
317
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
318
|
-
this.getCustomBLEAddress(switchbot);
|
|
309
|
+
const switchbot = await this.switchbotBLE();
|
|
310
|
+
|
|
311
|
+
if (switchbot === undefined) {
|
|
312
|
+
await this.BLERefreshConnection(switchbot);
|
|
313
|
+
} else {
|
|
319
314
|
// Start to monitor advertisement packets
|
|
320
|
-
|
|
315
|
+
(async () => {
|
|
321
316
|
// Start to monitor advertisement packets
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.model}`);
|
|
328
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
317
|
+
const serviceData: serviceData = await this.monitorAdvertisementPackets(switchbot);
|
|
318
|
+
// Update HomeKit
|
|
319
|
+
if (serviceData.model !== '' && serviceData.modelName !== '') {
|
|
320
|
+
await this.BLEparseStatus(serviceData);
|
|
321
|
+
await this.updateHomeKitCharacteristics();
|
|
329
322
|
} else {
|
|
330
|
-
this.
|
|
323
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed to get serviceData, serviceData: ${serviceData}`);
|
|
324
|
+
await this.BLERefreshConnection(switchbot);
|
|
331
325
|
}
|
|
332
|
-
};
|
|
333
|
-
// Wait 10 seconds
|
|
334
|
-
await switchbot.wait(this.scanDuration * 1000);
|
|
335
|
-
// Stop to monitor
|
|
336
|
-
await switchbot.stopScan();
|
|
337
|
-
// Update HomeKit
|
|
338
|
-
await this.BLEparseStatus(switchbot.onadvertisement.serviceData);
|
|
339
|
-
await this.updateHomeKitCharacteristics();
|
|
340
|
-
})();
|
|
341
|
-
if (switchbot === undefined) {
|
|
342
|
-
await this.BLERefreshConnection(switchbot);
|
|
326
|
+
})();
|
|
343
327
|
}
|
|
344
328
|
}
|
|
345
329
|
|
|
346
330
|
async openAPIRefreshStatus(): Promise<void> {
|
|
347
331
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
|
|
348
332
|
try {
|
|
349
|
-
const { body, statusCode } = await this.
|
|
350
|
-
`${Devices}/${this.device.deviceId}/status`, { headers: this.platform.generateHeaders() });
|
|
351
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
|
|
333
|
+
const { body, statusCode } = await this.deviceRefreshStatus();
|
|
352
334
|
const deviceStatus: any = await body.json();
|
|
353
|
-
this.
|
|
354
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
|
|
335
|
+
await this.refreshStatusCodes(statusCode, deviceStatus);;
|
|
355
336
|
if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
|
|
356
|
-
this.
|
|
357
|
-
|
|
358
|
-
this.
|
|
359
|
-
this.updateHomeKitCharacteristics();
|
|
337
|
+
await this.successfulRefreshStatus(statusCode, deviceStatus);
|
|
338
|
+
await this.openAPIparseStatus(deviceStatus);
|
|
339
|
+
await this.updateHomeKitCharacteristics();
|
|
360
340
|
} else {
|
|
361
|
-
this.
|
|
362
|
-
this.statusCode(deviceStatus.statusCode);
|
|
341
|
+
await this.statusCodes(statusCode, deviceStatus);
|
|
363
342
|
}
|
|
364
343
|
} catch (e: any) {
|
|
365
344
|
this.apiError(e);
|
|
@@ -480,16 +459,9 @@ export class ColorBulb extends deviceBase {
|
|
|
480
459
|
+ ` On: ${this.LightBulb.On}, OnCached: ${this.accessory.context.On}`);
|
|
481
460
|
const switchbot = await this.platform.connectBLE();
|
|
482
461
|
// Convert to BLE Address
|
|
483
|
-
|
|
484
|
-
.deviceId!.match(/.{1,2}/g)!
|
|
485
|
-
.join(':')
|
|
486
|
-
.toLowerCase();
|
|
487
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
462
|
+
await this.convertBLEAddress();
|
|
488
463
|
switchbot
|
|
489
|
-
.discover({
|
|
490
|
-
model: 'u',
|
|
491
|
-
id: this.device.bleMac,
|
|
492
|
-
})
|
|
464
|
+
.discover({ model: this.device.bleModel, id: this.device.bleMac })
|
|
493
465
|
.then(async (device_list: any) => {
|
|
494
466
|
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
|
|
495
467
|
return await this.retryBLE({
|
|
@@ -538,16 +510,9 @@ export class ColorBulb extends deviceBase {
|
|
|
538
510
|
if (this.LightBulb.Brightness !== this.accessory.context.Brightness) {
|
|
539
511
|
const switchbot = await this.platform.connectBLE();
|
|
540
512
|
// Convert to BLE Address
|
|
541
|
-
|
|
542
|
-
.deviceId!.match(/.{1,2}/g)!
|
|
543
|
-
.join(':')
|
|
544
|
-
.toLowerCase();
|
|
545
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
513
|
+
await this.convertBLEAddress();
|
|
546
514
|
switchbot
|
|
547
|
-
.discover({
|
|
548
|
-
model: 'u',
|
|
549
|
-
id: this.device.bleMac,
|
|
550
|
-
})
|
|
515
|
+
.discover({ model: this.device.bleModel, id: this.device.bleMac })
|
|
551
516
|
.then(async (device_list: any) => {
|
|
552
517
|
this.infoLog(`${this.accessory.displayName} Target Brightness: ${this.LightBulb.Brightness}`);
|
|
553
518
|
return await device_list[0].setBrightness(this.LightBulb.Brightness);
|
|
@@ -573,16 +538,9 @@ export class ColorBulb extends deviceBase {
|
|
|
573
538
|
if (this.LightBulb.ColorTemperature !== this.accessory.context.ColorTemperature) {
|
|
574
539
|
const switchbot = await this.platform.connectBLE();
|
|
575
540
|
// Convert to BLE Address
|
|
576
|
-
|
|
577
|
-
.deviceId!.match(/.{1,2}/g)!
|
|
578
|
-
.join(':')
|
|
579
|
-
.toLowerCase();
|
|
580
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
541
|
+
await this.convertBLEAddress();
|
|
581
542
|
switchbot
|
|
582
|
-
.discover({
|
|
583
|
-
model: 'u',
|
|
584
|
-
id: this.device.bleMac,
|
|
585
|
-
})
|
|
543
|
+
.discover({ model: this.device.bleModel, id: this.device.bleMac })
|
|
586
544
|
.then(async (device_list: any) => {
|
|
587
545
|
this.infoLog(`${this.accessory.displayName} Target ColorTemperature: ${this.LightBulb.ColorTemperature}`);
|
|
588
546
|
return await device_list[0].setColorTemperature(this.LightBulb.ColorTemperature);
|
|
@@ -614,16 +572,9 @@ export class ColorBulb extends deviceBase {
|
|
|
614
572
|
|
|
615
573
|
const switchbot = await this.platform.connectBLE();
|
|
616
574
|
// Convert to BLE Address
|
|
617
|
-
|
|
618
|
-
.deviceId!.match(/.{1,2}/g)!
|
|
619
|
-
.join(':')
|
|
620
|
-
.toLowerCase();
|
|
621
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
575
|
+
await this.convertBLEAddress();
|
|
622
576
|
switchbot
|
|
623
|
-
.discover({
|
|
624
|
-
model: 'u',
|
|
625
|
-
id: this.device.bleMac,
|
|
626
|
-
})
|
|
577
|
+
.discover({ model: this.device.bleModel, id: this.device.bleMac })
|
|
627
578
|
.then(async (device_list: any) => {
|
|
628
579
|
this.infoLog(`${this.accessory.displayName} Target RGB: ${(this.LightBulb.Brightness, red, green, blue)}`);
|
|
629
580
|
return await device_list[0].setRGB(this.LightBulb.Brightness, red, green, blue);
|
|
@@ -660,16 +611,9 @@ export class ColorBulb extends deviceBase {
|
|
|
660
611
|
});
|
|
661
612
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
|
|
662
613
|
try {
|
|
663
|
-
const { body, statusCode } = await
|
|
664
|
-
body: bodyChange,
|
|
665
|
-
method: 'POST',
|
|
666
|
-
headers: this.platform.generateHeaders(),
|
|
667
|
-
});
|
|
668
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
|
|
614
|
+
const { body, statusCode } = await this.pushChangeRequest(bodyChange);
|
|
669
615
|
const deviceStatus: any = await body.json();
|
|
670
|
-
this.
|
|
671
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
|
|
672
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
|
|
616
|
+
await this.pushStatusCodes(statusCode, deviceStatus);
|
|
673
617
|
if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
|
|
674
618
|
this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
675
619
|
+ `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
|
|
@@ -716,16 +660,9 @@ export class ColorBulb extends deviceBase {
|
|
|
716
660
|
});
|
|
717
661
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
|
|
718
662
|
try {
|
|
719
|
-
const { body, statusCode } = await
|
|
720
|
-
body: bodyChange,
|
|
721
|
-
method: 'POST',
|
|
722
|
-
headers: this.platform.generateHeaders(),
|
|
723
|
-
});
|
|
724
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
|
|
663
|
+
const { body, statusCode } = await this.pushChangeRequest(bodyChange);
|
|
725
664
|
const deviceStatus: any = await body.json();
|
|
726
|
-
this.
|
|
727
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
|
|
728
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
|
|
665
|
+
await this.pushStatusCodes(statusCode, deviceStatus);
|
|
729
666
|
if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
|
|
730
667
|
this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
731
668
|
+ `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
|
|
@@ -758,16 +695,9 @@ export class ColorBulb extends deviceBase {
|
|
|
758
695
|
});
|
|
759
696
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
|
|
760
697
|
try {
|
|
761
|
-
const { body, statusCode } = await
|
|
762
|
-
body: bodyChange,
|
|
763
|
-
method: 'POST',
|
|
764
|
-
headers: this.platform.generateHeaders(),
|
|
765
|
-
});
|
|
766
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
|
|
698
|
+
const { body, statusCode } = await this.pushChangeRequest(bodyChange);
|
|
767
699
|
const deviceStatus: any = await body.json();
|
|
768
|
-
this.
|
|
769
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
|
|
770
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
|
|
700
|
+
await this.pushStatusCodes(statusCode, deviceStatus);
|
|
771
701
|
if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
|
|
772
702
|
this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
773
703
|
+ `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
|
|
@@ -798,16 +728,9 @@ export class ColorBulb extends deviceBase {
|
|
|
798
728
|
});
|
|
799
729
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
|
|
800
730
|
try {
|
|
801
|
-
const { body, statusCode } = await
|
|
802
|
-
body: bodyChange,
|
|
803
|
-
method: 'POST',
|
|
804
|
-
headers: this.platform.generateHeaders(),
|
|
805
|
-
});
|
|
806
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
|
|
731
|
+
const { body, statusCode } = await this.pushChangeRequest(bodyChange);
|
|
807
732
|
const deviceStatus: any = await body.json();
|
|
808
|
-
this.
|
|
809
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
|
|
810
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
|
|
733
|
+
await this.pushStatusCodes(statusCode, deviceStatus);
|
|
811
734
|
if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
|
|
812
735
|
this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
813
736
|
+ `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
|
package/src/device/contact.ts
CHANGED
|
@@ -4,7 +4,6 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import { deviceBase } from './device.js';
|
|
6
6
|
import { interval, Subject } from 'rxjs';
|
|
7
|
-
import { Devices } from '../settings.js';
|
|
8
7
|
import { skipWhile } from 'rxjs/operators';
|
|
9
8
|
|
|
10
9
|
import type { SwitchBotPlatform } from '../platform.js';
|
|
@@ -219,9 +218,6 @@ export class Contact extends deviceBase {
|
|
|
219
218
|
}
|
|
220
219
|
}
|
|
221
220
|
// Battery
|
|
222
|
-
if (serviceData.battery === undefined) {
|
|
223
|
-
serviceData.battery === 100;
|
|
224
|
-
}
|
|
225
221
|
this.Battery.BatteryLevel = Number(serviceData.battery);
|
|
226
222
|
if (this.Battery.BatteryLevel < 10) {
|
|
227
223
|
this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
|
|
@@ -265,9 +261,6 @@ export class Contact extends deviceBase {
|
|
|
265
261
|
+ ` CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`);
|
|
266
262
|
}
|
|
267
263
|
// Battery
|
|
268
|
-
if (deviceStatus.body.battery === undefined) {
|
|
269
|
-
deviceStatus.body.battery === 100;
|
|
270
|
-
}
|
|
271
264
|
this.Battery.BatteryLevel = Number(deviceStatus.body.battery);
|
|
272
265
|
if (this.Battery.BatteryLevel < 10) {
|
|
273
266
|
this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
|
|
@@ -312,61 +305,39 @@ export class Contact extends deviceBase {
|
|
|
312
305
|
|
|
313
306
|
async BLERefreshStatus(): Promise<void> {
|
|
314
307
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
|
|
315
|
-
const switchbot = await this.
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
.
|
|
319
|
-
|
|
320
|
-
.toLowerCase();
|
|
321
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
322
|
-
this.getCustomBLEAddress(switchbot);
|
|
308
|
+
const switchbot = await this.switchbotBLE();
|
|
309
|
+
|
|
310
|
+
if (switchbot === undefined) {
|
|
311
|
+
await this.BLERefreshConnection(switchbot);
|
|
312
|
+
} else {
|
|
323
313
|
// Start to monitor advertisement packets
|
|
324
|
-
|
|
314
|
+
(async () => {
|
|
325
315
|
// Start to monitor advertisement packets
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.model}`);
|
|
332
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
316
|
+
const serviceData: serviceData = await this.monitorAdvertisementPackets(switchbot);
|
|
317
|
+
// Update HomeKit
|
|
318
|
+
if (serviceData.model !== '' && serviceData.modelName !== '') {
|
|
319
|
+
await this.BLEparseStatus(serviceData);
|
|
320
|
+
await this.updateHomeKitCharacteristics();
|
|
333
321
|
} else {
|
|
334
|
-
this.
|
|
322
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed to get serviceData, serviceData: ${serviceData}`);
|
|
323
|
+
await this.BLERefreshConnection(switchbot);
|
|
335
324
|
}
|
|
336
|
-
};
|
|
337
|
-
// Wait 10 seconds
|
|
338
|
-
await switchbot.wait(this.scanDuration * 1000);
|
|
339
|
-
// Stop to monitor
|
|
340
|
-
await switchbot.stopScan();
|
|
341
|
-
// Update HomeKit
|
|
342
|
-
await this.BLEparseStatus(switchbot.onadvertisement.serviceData);
|
|
343
|
-
await this.updateHomeKitCharacteristics();
|
|
344
|
-
})();
|
|
345
|
-
if (switchbot === undefined) {
|
|
346
|
-
await this.BLERefreshConnection(switchbot);
|
|
325
|
+
})();
|
|
347
326
|
}
|
|
348
327
|
}
|
|
349
328
|
|
|
350
329
|
async openAPIRefreshStatus(): Promise<void> {
|
|
351
330
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
|
|
352
331
|
try {
|
|
353
|
-
const { body, statusCode } = await this.
|
|
354
|
-
`${Devices}/${this.device.deviceId}/status`, { headers: this.platform.generateHeaders() });
|
|
355
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
|
|
332
|
+
const { body, statusCode } = await this.deviceRefreshStatus();
|
|
356
333
|
const deviceStatus: any = await body.json();
|
|
357
|
-
this.
|
|
358
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
|
|
334
|
+
await this.refreshStatusCodes(statusCode, deviceStatus);;
|
|
359
335
|
if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
|
|
360
|
-
this.
|
|
361
|
-
|
|
362
|
-
this.
|
|
363
|
-
this.updateHomeKitCharacteristics();
|
|
336
|
+
await this.successfulRefreshStatus(statusCode, deviceStatus);
|
|
337
|
+
await this.openAPIparseStatus(deviceStatus);
|
|
338
|
+
await this.updateHomeKitCharacteristics();
|
|
364
339
|
} else {
|
|
365
|
-
this.
|
|
366
|
-
this.statusCode(deviceStatus.statusCode);
|
|
367
|
-
if (deviceStatus.statusCode === 161 || deviceStatus.statusCode === 171) {
|
|
368
|
-
this.offlineOff();
|
|
369
|
-
}
|
|
340
|
+
await this.statusCodes(statusCode, deviceStatus);
|
|
370
341
|
}
|
|
371
342
|
} catch (e: any) {
|
|
372
343
|
this.apiError(e);
|