@switchbot/homebridge-switchbot 2.2.2-beta.2 → 2.2.2-beta.4

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.
@@ -689,7 +689,7 @@ export class ColorBulb {
689
689
  'parameter': 'default',
690
690
  'commandType': 'command',
691
691
  });
692
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
692
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
693
693
  const options = {
694
694
  hostname: HostDomain,
695
695
  port: 443,
@@ -766,7 +766,7 @@ export class ColorBulb {
766
766
  'parameter': `${red}:${green}:${blue}`,
767
767
  'commandType': 'command',
768
768
  });
769
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
769
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
770
770
  const options = {
771
771
  hostname: HostDomain,
772
772
  port: 443,
@@ -825,7 +825,7 @@ export class ColorBulb {
825
825
  'parameter': `${kelvin}`,
826
826
  'commandType': 'command',
827
827
  });
828
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
828
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
829
829
  const options = {
830
830
  hostname: HostDomain,
831
831
  port: 443,
@@ -882,7 +882,7 @@ export class ColorBulb {
882
882
  'parameter': `${this.Brightness}`,
883
883
  'commandType': 'command',
884
884
  });
885
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
885
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
886
886
  const options = {
887
887
  hostname: HostDomain,
888
888
  port: 443,
@@ -617,7 +617,7 @@ export class Curtain {
617
617
  'parameter': `0,${adjustedMode},${adjustedTargetPosition}`,
618
618
  'commandType': 'command',
619
619
  });
620
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
620
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
621
621
  const options = {
622
622
  hostname: HostDomain,
623
623
  port: 443,
@@ -484,7 +484,7 @@ export class Humidifier {
484
484
  'parameter': `${this.RelativeHumidityHumidifierThreshold}`,
485
485
  'commandType': 'command',
486
486
  });
487
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
487
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
488
488
  const options = {
489
489
  hostname: HostDomain,
490
490
  port: 443,
@@ -552,7 +552,7 @@ export class Humidifier {
552
552
  'parameter': 'auto',
553
553
  'commandType': 'command',
554
554
  });
555
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
555
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
556
556
  const options = {
557
557
  hostname: HostDomain,
558
558
  port: 443,
@@ -613,7 +613,7 @@ export class Humidifier {
613
613
  'parameter': 'default',
614
614
  'commandType': 'command',
615
615
  });
616
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
616
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
617
617
  const options = {
618
618
  hostname: HostDomain,
619
619
  port: 443,
@@ -408,7 +408,7 @@ export class Lock {
408
408
  'parameter': 'default',
409
409
  'commandType': 'command',
410
410
  });
411
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
411
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
412
412
  const options = {
413
413
  hostname: HostDomain,
414
414
  port: 443,
@@ -392,7 +392,7 @@ export class Plug {
392
392
  'parameter': 'default',
393
393
  'commandType': 'command',
394
394
  });
395
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
395
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
396
396
  const options = {
397
397
  hostname: HostDomain,
398
398
  port: 443,
@@ -54,6 +54,7 @@ export class StripLight {
54
54
 
55
55
  // Others
56
56
  cacheKelvin!: number;
57
+ change!: string;
57
58
 
58
59
  // Updates
59
60
  stripLightUpdateInProgress!: boolean;
@@ -474,68 +475,117 @@ export class StripLight {
474
475
  + ` Connection, Error Message: ${superStringify(e.message)}`);
475
476
  await this.BLEPushConnection();
476
477
  });
478
+ // Push Brightness Update
479
+ if (this.On) {
480
+ await this.BLEpushBrightnessChanges();
481
+ }
482
+ // Push Hue & Saturation Update
483
+ if (this.On) {
484
+ await this.BLEpushRGBChanges();
485
+ }
477
486
  } else {
478
487
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges.` + `On: ${this.On}, `
479
488
  +`OnCached: ${this.accessory.context.On}`);
480
489
  }
481
490
  }
482
491
 
492
+ async BLEpushBrightnessChanges(): Promise<void> {
493
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushBrightnessChanges`);
494
+ if (this.Brightness !== this.accessory.context.Brightness) {
495
+ const switchbot = await this.platform.connectBLE();
496
+ // Convert to BLE Address
497
+ this.device.bleMac = this.device
498
+ .deviceId!.match(/.{1,2}/g)!
499
+ .join(':')
500
+ .toLowerCase();
501
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
502
+ switchbot
503
+ .discover({
504
+ model: 'u',
505
+ id: this.device.bleMac,
506
+ })
507
+ .then((device_list: any) => {
508
+ this.infoLog(`${this.accessory.displayName} Target Brightness: ${this.Brightness}`);
509
+ return device_list[0].setBrightness(this.Brightness);
510
+ })
511
+ .then(() => {
512
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
513
+ this.On = false;
514
+ })
515
+ .catch(async (e: any) => {
516
+ this.apiError(e);
517
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushBrightnessChanges with ${this.device.connectionType}`
518
+ + ` Connection, Error Message: ${superStringify(e.message)}`);
519
+ await this.BLEPushConnection();
520
+ });
521
+ } else {
522
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushBrightnessChanges.` + `Brightness: ${this.Brightness}, `
523
+ +`BrightnessCached: ${this.accessory.context.Brightness}`);
524
+ }
525
+ }
526
+
527
+ async BLEpushRGBChanges(): Promise<void> {
528
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushRGBChanges`);
529
+ if (this.Hue !== this.accessory.context.Hue || this.Saturation !== this.accessory.context.Saturation) {
530
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${superStringify(this.Hue)}`);
531
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${superStringify(this.Saturation)}`);
532
+
533
+ const [red, green, blue] = hs2rgb(Number(this.Hue), Number(this.Saturation));
534
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} rgb: ${superStringify([red, green, blue])}`);
535
+
536
+ const switchbot = await this.platform.connectBLE();
537
+ // Convert to BLE Address
538
+ this.device.bleMac = this.device
539
+ .deviceId!.match(/.{1,2}/g)!
540
+ .join(':')
541
+ .toLowerCase();
542
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
543
+ switchbot
544
+ .discover({
545
+ model: 'u',
546
+ id: this.device.bleMac,
547
+ })
548
+ .then((device_list: any) => {
549
+ this.infoLog(`${this.accessory.displayName} Target RGB: ${this.Brightness, red, green, blue}`);
550
+ return device_list[0].setRGB(this.Brightness, red, green, blue);
551
+ })
552
+ .then(() => {
553
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
554
+ this.On = false;
555
+ })
556
+ .catch(async (e: any) => {
557
+ this.apiError(e);
558
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushRGBChanges with ${this.device.connectionType}`
559
+ + ` Connection, Error Message: ${superStringify(e.message)}`);
560
+ await this.BLEPushConnection();
561
+ });
562
+ } else {
563
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushRGBChanges. Hue: ${this.Hue}, `
564
+ + `HueCached: ${this.accessory.context.Hue}, Saturation: ${this.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`);
565
+ }
566
+ }
567
+
483
568
  async openAPIpushChanges() {
569
+ const change = 'openAPIpushChanges';
484
570
  try {
485
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
571
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${change}`);
486
572
  if (this.On !== this.accessory.context.On) {
487
573
  // Make Push On request to the API
488
- const t = Date.now();
489
- const nonce = 'requestID';
490
- const data = this.platform.config.credentials?.token + t + nonce;
491
- const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret)
492
- .update(Buffer.from(data, 'utf-8'))
493
- .digest();
494
- const sign = signTerm.toString('base64');
495
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
496
-
497
- let command = '';
498
- if (this.On) {
574
+ const command = this.On ? 'turnOn' : 'turnOff';
575
+ /*if (this.On) {
499
576
  command = 'turnOn';
500
577
  } else {
501
578
  command = 'turnOff';
502
- }
579
+ }*/
503
580
  const body = superStringify({
504
581
  'command': `${command}`,
505
582
  'parameter': 'default',
506
583
  'commandType': 'command',
507
584
  });
508
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
509
- const options = {
510
- hostname: HostDomain,
511
- port: 443,
512
- path: `${DevicePath}/${this.device.deviceId}/commands`,
513
- method: 'POST',
514
- headers: {
515
- 'Authorization': this.platform.config.credentials?.token,
516
- 'sign': sign,
517
- 'nonce': nonce,
518
- 't': t,
519
- 'Content-Type': 'application/json',
520
- 'Content-Length': body.length,
521
- },
522
- };
523
- const req = https.request(options, res => {
524
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges statusCode: ${res.statusCode}`);
525
- this.statusCode({ res });
526
- res.on('data', d => {
527
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
528
- });
529
- });
530
- req.on('error', (e: any) => {
531
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
532
- });
533
- req.write(body);
534
- req.end();
535
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges: ${superStringify(req)}`);
585
+ await this.request({ body, change });
536
586
  } else {
537
587
  this.debugLog(
538
- `${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.` + `On: ${this.On}, `
588
+ `${this.device.deviceType}: ${this.accessory.displayName} No ${change}.` + `On: ${this.On}, `
539
589
  +`OnCached: ${this.accessory.context.On}`,
540
590
  );
541
591
  }
@@ -549,15 +599,16 @@ export class StripLight {
549
599
  }
550
600
  } catch (e: any) {
551
601
  this.apiError(e);
552
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
602
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed ${change} with ${this.device.connectionType}`
553
603
  + ` Connection, Error Message: ${superStringify(e.message)}`,
554
604
  );
555
605
  }
556
606
  }
557
607
 
558
608
  async pushHueSaturationChanges(): Promise<void> {
609
+ const change = 'pushHueSaturationChanges';
559
610
  try {
560
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushHueSaturationChanges`);
611
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${change}`);
561
612
  if (this.Hue !== this.accessory.context.Hue || this.Saturation !== this.accessory.context.Saturation) {
562
613
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${superStringify(this.Hue)}`);
563
614
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${superStringify(this.Saturation)}`);
@@ -565,115 +616,85 @@ export class StripLight {
565
616
  const [red, green, blue] = hs2rgb(Number(this.Hue), Number(this.Saturation));
566
617
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} rgb: ${superStringify([red, green, blue])}`);
567
618
  // Make Push On request to the API
568
- const t = Date.now();
569
- const nonce = 'requestID';
570
- const data = this.platform.config.credentials?.token + t + nonce;
571
- const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret)
572
- .update(Buffer.from(data, 'utf-8'))
573
- .digest();
574
- const sign = signTerm.toString('base64');
575
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
576
619
  const body = superStringify({
577
620
  'command': 'setColor',
578
621
  'parameter': `${red}:${green}:${blue}`,
579
622
  'commandType': 'command',
580
623
  });
581
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
582
- const options = {
583
- hostname: HostDomain,
584
- port: 443,
585
- path: `${DevicePath}/${this.device.deviceId}/commands`,
586
- method: 'POST',
587
- headers: {
588
- 'Authorization': this.platform.config.credentials?.token,
589
- 'sign': sign,
590
- 'nonce': nonce,
591
- 't': t,
592
- 'Content-Type': 'application/json',
593
- 'Content-Length': body.length,
594
- },
595
- };
596
- const req = https.request(options, res => {
597
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushHueSaturationChanges statusCode: ${res.statusCode}`);
598
- this.statusCode({ res });
599
- res.on('data', d => {
600
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
601
- });
602
- });
603
- req.on('error', (e: any) => {
604
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
605
- });
606
- req.write(body);
607
- req.end();
608
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushHueSaturationChanges: ${superStringify(req)}`);
624
+ await this.request({ body, change });
609
625
  } else {
610
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushHueSaturationChanges. Hue: ${this.Hue}, `
626
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No ${change}. Hue: ${this.Hue}, `
611
627
  + `HueCached: ${this.accessory.context.Hue}, Saturation: ${this.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`);
612
628
  }
613
629
  } catch (e: any) {
614
630
  this.apiError(e);
615
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushHueSaturationChanges with ${this.device.connectionType}`
631
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed ${change} with ${this.device.connectionType}`
616
632
  + ` Connection, Error Message: ${superStringify(e.message)}`);
617
633
  }
618
634
  }
619
635
 
620
636
  async pushBrightnessChanges(): Promise<void> {
637
+ const change = 'pushBrightnessChanges';
621
638
  try {
622
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushBrightnessChanges`);
639
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${change}`);
623
640
  if (this.Brightness !== this.accessory.context.Brightness) {
624
641
  // Make Push On request to the API
625
- const t = Date.now();
626
- const nonce = 'requestID';
627
- const data = this.platform.config.credentials?.token + t + nonce;
628
- const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret)
629
- .update(Buffer.from(data, 'utf-8'))
630
- .digest();
631
- const sign = signTerm.toString('base64');
632
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
633
642
  const body = superStringify({
634
643
  'command': 'setBrightness',
635
644
  'parameter': `${this.Brightness}`,
636
645
  'commandType': 'command',
637
646
  });
638
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
639
- const options = {
640
- hostname: HostDomain,
641
- port: 443,
642
- path: `${DevicePath}/${this.device.deviceId}/commands`,
643
- method: 'POST',
644
- headers: {
645
- 'Authorization': this.platform.config.credentials?.token,
646
- 'sign': sign,
647
- 'nonce': nonce,
648
- 't': t,
649
- 'Content-Type': 'application/json',
650
- 'Content-Length': body.length,
651
- },
652
- };
653
- const req = https.request(options, res => {
654
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushBrightnessChanges statusCode: ${res.statusCode}`);
655
- this.statusCode({ res });
656
- res.on('data', d => {
657
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
658
- });
659
- });
660
- req.on('error', (e: any) => {
661
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
662
- });
663
- req.write(body);
664
- req.end();
665
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushBrightnessChanges: ${superStringify(req)}`);
647
+ await this.request({ body, change });
666
648
  } else {
667
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushBrightnessChanges.` + `Brightness: ${this.Brightness}, `
649
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No ${change},` + `Brightness: ${this.Brightness}, `
668
650
  +`BrightnessCached: ${this.accessory.context.Brightness}`);
669
651
  }
670
652
  } catch (e: any) {
671
653
  this.apiError(e);
672
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushBrightnessChanges with ${this.device.connectionType}`
654
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed ${change} with ${this.device.connectionType}`
673
655
  + ` Connection, Error Message: ${superStringify(e.message)}`);
674
656
  }
675
657
  }
676
658
 
659
+ async request({ body, change }: { body: any; change: string; }): Promise<void> {
660
+ const t = Date.now();
661
+ const nonce = 'requestID';
662
+ const data = this.platform.config.credentials?.token + t + nonce;
663
+ const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret)
664
+ .update(Buffer.from(data, 'utf-8'))
665
+ .digest();
666
+ const sign = signTerm.toString('base64');
667
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
668
+ const options = {
669
+ hostname: HostDomain,
670
+ port: 443,
671
+ path: `${DevicePath}/${this.device.deviceId}/commands`,
672
+ method: 'POST',
673
+ headers: {
674
+ 'Authorization': this.platform.config.credentials?.token,
675
+ 'sign': sign,
676
+ 'nonce': nonce,
677
+ 't': t,
678
+ 'Content-Type': 'application/json',
679
+ 'Content-Length': body.length,
680
+ },
681
+ };
682
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
683
+ const req = https.request(options, res => {
684
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${change} statusCode: ${res.statusCode}`);
685
+ this.statusCode({ res });
686
+ res.on('data', d => {
687
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
688
+ });
689
+ });
690
+ req.on('error', (e: any) => {
691
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
692
+ });
693
+ req.write(body);
694
+ req.end();
695
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${change} req: ${superStringify(req)}`);
696
+ }
697
+
677
698
  /**
678
699
  * Handle requests to set the value of the "On" characteristic
679
700
  */