@switchbot/homebridge-switchbot 1.15.1-beta.8 → 2.0.0-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.
Files changed (104) hide show
  1. package/.github/Old Releaser.zip +0 -0
  2. package/.github/npm-version-script.js +81 -0
  3. package/.github/workflows/beta-release.yml +31 -0
  4. package/.github/workflows/build.yml +18 -0
  5. package/.github/workflows/codeql-analysis.yml +5 -63
  6. package/.github/workflows/label.yml +2 -2
  7. package/.github/workflows/release-drafter.yml +14 -0
  8. package/.github/workflows/release.yml +24 -0
  9. package/.github/workflows/stale.yml +6 -22
  10. package/README.md +10 -3
  11. package/dist/device/bot.d.ts.map +1 -1
  12. package/dist/device/bot.js +33 -29
  13. package/dist/device/bot.js.map +1 -1
  14. package/dist/device/colorbulb.d.ts +1 -0
  15. package/dist/device/colorbulb.d.ts.map +1 -1
  16. package/dist/device/colorbulb.js +129 -97
  17. package/dist/device/colorbulb.js.map +1 -1
  18. package/dist/device/contact.js +1 -1
  19. package/dist/device/contact.js.map +1 -1
  20. package/dist/device/curtain.d.ts.map +1 -1
  21. package/dist/device/curtain.js +34 -26
  22. package/dist/device/curtain.js.map +1 -1
  23. package/dist/device/humidifier.d.ts.map +1 -1
  24. package/dist/device/humidifier.js +88 -92
  25. package/dist/device/humidifier.js.map +1 -1
  26. package/dist/device/lock.d.ts.map +1 -1
  27. package/dist/device/lock.js +29 -25
  28. package/dist/device/lock.js.map +1 -1
  29. package/dist/device/meter.js +1 -1
  30. package/dist/device/meter.js.map +1 -1
  31. package/dist/device/meterplus.js +1 -1
  32. package/dist/device/meterplus.js.map +1 -1
  33. package/dist/device/motion.js +1 -1
  34. package/dist/device/motion.js.map +1 -1
  35. package/dist/device/plug.d.ts.map +1 -1
  36. package/dist/device/plug.js +31 -27
  37. package/dist/device/plug.js.map +1 -1
  38. package/dist/device/striplight.d.ts.map +1 -1
  39. package/dist/device/striplight.js +81 -72
  40. package/dist/device/striplight.js.map +1 -1
  41. package/dist/irdevice/airconditioner.d.ts +2 -2
  42. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  43. package/dist/irdevice/airconditioner.js +35 -32
  44. package/dist/irdevice/airconditioner.js.map +1 -1
  45. package/dist/irdevice/airpurifier.d.ts +2 -2
  46. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  47. package/dist/irdevice/airpurifier.js +35 -32
  48. package/dist/irdevice/airpurifier.js.map +1 -1
  49. package/dist/irdevice/camera.d.ts +2 -2
  50. package/dist/irdevice/camera.d.ts.map +1 -1
  51. package/dist/irdevice/camera.js +28 -26
  52. package/dist/irdevice/camera.js.map +1 -1
  53. package/dist/irdevice/fan.d.ts +2 -2
  54. package/dist/irdevice/fan.d.ts.map +1 -1
  55. package/dist/irdevice/fan.js +47 -45
  56. package/dist/irdevice/fan.js.map +1 -1
  57. package/dist/irdevice/light.d.ts +2 -2
  58. package/dist/irdevice/light.d.ts.map +1 -1
  59. package/dist/irdevice/light.js +40 -38
  60. package/dist/irdevice/light.js.map +1 -1
  61. package/dist/irdevice/other.d.ts +2 -2
  62. package/dist/irdevice/other.d.ts.map +1 -1
  63. package/dist/irdevice/other.js +28 -26
  64. package/dist/irdevice/other.js.map +1 -1
  65. package/dist/irdevice/tv.d.ts +2 -2
  66. package/dist/irdevice/tv.d.ts.map +1 -1
  67. package/dist/irdevice/tv.js +82 -80
  68. package/dist/irdevice/tv.js.map +1 -1
  69. package/dist/irdevice/vacuumcleaner.d.ts +2 -2
  70. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  71. package/dist/irdevice/vacuumcleaner.js +24 -22
  72. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  73. package/dist/irdevice/waterheater.d.ts +2 -2
  74. package/dist/irdevice/waterheater.d.ts.map +1 -1
  75. package/dist/irdevice/waterheater.js +28 -26
  76. package/dist/irdevice/waterheater.js.map +1 -1
  77. package/dist/settings.d.ts +0 -5
  78. package/dist/settings.d.ts.map +1 -1
  79. package/dist/settings.js.map +1 -1
  80. package/package.json +79 -79
  81. package/src/device/bot.ts +34 -39
  82. package/src/device/colorbulb.ts +130 -119
  83. package/src/device/contact.ts +1 -1
  84. package/src/device/curtain.ts +35 -35
  85. package/src/device/humidifier.ts +89 -118
  86. package/src/device/lock.ts +29 -34
  87. package/src/device/meter.ts +1 -1
  88. package/src/device/meterplus.ts +1 -1
  89. package/src/device/motion.ts +1 -1
  90. package/src/device/plug.ts +32 -36
  91. package/src/device/striplight.ts +82 -98
  92. package/src/irdevice/airconditioner.ts +36 -41
  93. package/src/irdevice/airpurifier.ts +36 -43
  94. package/src/irdevice/camera.ts +29 -34
  95. package/src/irdevice/fan.ts +48 -53
  96. package/src/irdevice/light.ts +41 -46
  97. package/src/irdevice/other.ts +29 -34
  98. package/src/irdevice/tv.ts +83 -88
  99. package/src/irdevice/vacuumcleaner.ts +25 -30
  100. package/src/irdevice/waterheater.ts +29 -34
  101. package/src/settings.ts +0 -19
  102. package/.github/workflows/nodejs-beta.yml +0 -42
  103. package/.github/workflows/nodejs.yml +0 -47
  104. package/.github/workflows/prerelease-beta.js +0 -34
@@ -6,7 +6,7 @@ import { interval, Subject } from 'rxjs';
6
6
  import { SwitchBotPlatform } from '../platform';
7
7
  import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
8
8
  import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
9
- import { device, devicesConfig, serviceData, ad, payload, deviceStatus, HostDomain, DevicePath } from '../settings';
9
+ import { device, devicesConfig, serviceData, ad, deviceStatus, HostDomain, DevicePath } from '../settings';
10
10
 
11
11
  /**
12
12
  * Platform Accessory
@@ -369,7 +369,7 @@ export class Humidifier {
369
369
  });
370
370
  // Set an event handler
371
371
  switchbot.onadvertisement = (ad: any) => {
372
- this.warnLog(JSON.stringify(ad, null, ' '));
372
+ this.warnLog(`Humidifier: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
373
373
  };
374
374
  await switchbot.wait(10000);
375
375
  // Stop to monitor
@@ -514,66 +514,49 @@ export class Humidifier {
514
514
  this.TargetHumidifierDehumidifierState === this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER &&
515
515
  this.Active === this.platform.Characteristic.Active.ACTIVE
516
516
  ) {
517
- try {
518
- this.debugLog(`Pushing Manual: ${this.RelativeHumidityHumidifierThreshold}!`);
519
- const payload = {
520
- commandType: 'command',
521
- command: 'setMode',
522
- parameter: `${this.RelativeHumidityHumidifierThreshold}`,
523
- } as payload;
524
-
525
- this.infoLog(
526
- `Humidifier: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},` +
527
- ` parameter: ${payload.parameter}, commandType: ${payload.commandType}`,
528
- );
529
-
530
- // Make the API request
531
- const t = Date.now();
532
- const nonce = 'requestID';
533
- const data = this.platform.config.credentials?.token + t + nonce;
534
- const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
535
- const sign = signTerm.toString('base64');
536
- this.debugLog(`Humidifier: ${this.accessory.displayName} sign: ${sign}`);
537
- const options = {
538
- hostname: 'api.switch-bot.com',
539
- port: 443,
540
- path: `/v1.1/devices/${this.device.deviceId}/commands`,
541
- method: 'POST',
542
- headers: {
543
- Authorization: this.platform.config.credentials?.token,
544
- sign: sign,
545
- nonce: nonce,
546
- t: t,
547
- 'Content-Type': 'application/json',
548
- },
549
- };
550
-
551
- const req = https.request(options, (res) => {
552
- this.debugLog(`Humidifier: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
553
- this.statusCode({ res });
554
- res.on('data', (d) => {
555
- this.debugLog(`Humidifier: ${this.accessory.displayName} d: ${d}`);
556
- });
557
- });
558
-
559
- req.on('error', (error) => {
560
- this.errorLog(`Humidifier: ${this.accessory.displayName} error: ${error}`);
517
+ // Make Push On request to the API
518
+ const t = Date.now();
519
+ const nonce = 'requestID';
520
+ const data = this.platform.config.credentials?.token + t + nonce;
521
+ const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret)
522
+ .update(Buffer.from(data, 'utf-8'))
523
+ .digest();
524
+ const sign = signTerm.toString('base64');
525
+ this.debugLog(`Humidifier: ${this.accessory.displayName} sign: ${sign}`);
526
+ this.debugLog(`Pushing Manual: ${this.RelativeHumidityHumidifierThreshold}!`);
527
+ const body = JSON.stringify({
528
+ 'command': 'setMode',
529
+ 'parameter': `${this.RelativeHumidityHumidifierThreshold}`,
530
+ 'commandType': 'command',
531
+ });
532
+ this.infoLog(`Humidifier: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
533
+ const options = {
534
+ hostname: HostDomain,
535
+ port: 443,
536
+ path: `${DevicePath}/${this.device.deviceId}/commands`,
537
+ method: 'POST',
538
+ headers: {
539
+ 'Authorization': this.platform.config.credentials?.token,
540
+ 'sign': sign,
541
+ 'nonce': nonce,
542
+ 't': t,
543
+ 'Content-Type': 'application/json',
544
+ 'Content-Length': body.length,
545
+ },
546
+ };
547
+ const req = https.request(options, res => {
548
+ this.debugLog(`Humidifier: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
549
+ res.on('data', d => {
550
+ this.debugLog(`Humidifier: ${this.accessory.displayName} d: ${d}`);
561
551
  });
562
-
563
- req.write(payload);
564
- req.end();
565
-
566
- this.debugLog(`Humidifier: ${this.accessory.displayName} pushchanges: ${JSON.stringify(req)}`);
567
- } catch (e: any) {
568
- this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
569
- if (this.deviceLogging.includes('debug')) {
570
- this.errorLog(
571
- `Humidifier: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,` +
572
- ` Error Message: ${JSON.stringify(e.message)}`,
573
- );
574
- }
552
+ });
553
+ req.on('error', (e: any) => {
554
+ this.errorLog(`Humidifier: ${this.accessory.displayName} error message: ${e.message}`);
575
555
  this.apiError(e);
576
- }
556
+ });
557
+ req.write(body);
558
+ req.end();
559
+ this.debugLog(`Humidifier: ${this.accessory.displayName} openAPIpushChanges: ${JSON.stringify(req)}`);
577
560
  } else if (
578
561
  this.TargetHumidifierDehumidifierState === this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER_OR_DEHUMIDIFIER &&
579
562
  this.Active === this.platform.Characteristic.Active.ACTIVE
@@ -594,54 +577,48 @@ export class Humidifier {
594
577
  this.TargetHumidifierDehumidifierState === this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER_OR_DEHUMIDIFIER &&
595
578
  this.Active === this.platform.Characteristic.Active.ACTIVE
596
579
  ) {
597
- this.debugLog('Pushing Auto!');
598
- const payload = {
599
- commandType: 'command',
600
- command: 'setMode',
601
- parameter: 'auto',
602
- } as payload;
603
-
604
- this.infoLog(
605
- `Humidifier: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},` +
606
- ` parameter: ${payload.parameter}, commandType: ${payload.commandType}`,
607
- );
608
-
609
- // Make the API request
580
+ // Make Push On request to the API
610
581
  const t = Date.now();
611
582
  const nonce = 'requestID';
612
583
  const data = this.platform.config.credentials?.token + t + nonce;
613
- const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
584
+ const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret)
585
+ .update(Buffer.from(data, 'utf-8'))
586
+ .digest();
614
587
  const sign = signTerm.toString('base64');
615
588
  this.debugLog(`Humidifier: ${this.accessory.displayName} sign: ${sign}`);
589
+ this.debugLog('Pushing Auto!');
590
+ const body = JSON.stringify({
591
+ 'command': 'setMode',
592
+ 'parameter': 'auto',
593
+ 'commandType': 'command',
594
+ });
595
+ this.infoLog(`Humidifier: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
616
596
  const options = {
617
- hostname: 'api.switch-bot.com',
597
+ hostname: HostDomain,
618
598
  port: 443,
619
- path: `/v1.1/devices/${this.device.deviceId}/commands`,
599
+ path: `${DevicePath}/${this.device.deviceId}/commands`,
620
600
  method: 'POST',
621
601
  headers: {
622
- Authorization: this.platform.config.credentials?.token,
623
- sign: sign,
624
- nonce: nonce,
625
- t: t,
602
+ 'Authorization': this.platform.config.credentials?.token,
603
+ 'sign': sign,
604
+ 'nonce': nonce,
605
+ 't': t,
626
606
  'Content-Type': 'application/json',
607
+ 'Content-Length': body.length,
627
608
  },
628
609
  };
629
-
630
- const req = https.request(options, (res) => {
610
+ const req = https.request(options, res => {
631
611
  this.debugLog(`Humidifier: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
632
612
  this.statusCode({ res });
633
- res.on('data', (d) => {
613
+ res.on('data', d => {
634
614
  this.debugLog(`Humidifier: ${this.accessory.displayName} d: ${d}`);
635
615
  });
636
616
  });
637
-
638
- req.on('error', (error) => {
639
- this.errorLog(`Humidifier: ${this.accessory.displayName} error: ${error}`);
617
+ req.on('error', (e: any) => {
618
+ this.errorLog(`Humidifier: ${this.accessory.displayName} error message: ${e.message}`);
640
619
  });
641
-
642
- req.write(payload);
620
+ req.write(body);
643
621
  req.end();
644
-
645
622
  this.debugLog(`Humidifier: ${this.accessory.displayName} pushAutoChanges: ${JSON.stringify(req)}`);
646
623
  }
647
624
  } catch (e: any) {
@@ -662,54 +639,48 @@ export class Humidifier {
662
639
  async pushActiveChanges(): Promise<void> {
663
640
  try {
664
641
  if (this.Active === this.platform.Characteristic.Active.INACTIVE) {
665
- this.debugLog('Pushing Off!');
666
- const payload = {
667
- commandType: 'command',
668
- command: 'turnOff',
669
- parameter: 'default',
670
- } as payload;
671
-
672
- this.infoLog(
673
- `Humidifier: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},` +
674
- ` parameter: ${payload.parameter}, commandType: ${payload.commandType}`,
675
- );
676
-
677
- // Make the API request
642
+ // Make Push On request to the API
678
643
  const t = Date.now();
679
644
  const nonce = 'requestID';
680
645
  const data = this.platform.config.credentials?.token + t + nonce;
681
- const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
646
+ const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret)
647
+ .update(Buffer.from(data, 'utf-8'))
648
+ .digest();
682
649
  const sign = signTerm.toString('base64');
683
650
  this.debugLog(`Humidifier: ${this.accessory.displayName} sign: ${sign}`);
651
+ this.debugLog('Pushing Off!');
652
+ const body = JSON.stringify({
653
+ 'command': 'turnOff',
654
+ 'parameter': 'default',
655
+ 'commandType': 'command',
656
+ });
657
+ this.infoLog(`Humidifier: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
684
658
  const options = {
685
- hostname: 'api.switch-bot.com',
659
+ hostname: HostDomain,
686
660
  port: 443,
687
- path: `/v1.1/devices/${this.device.deviceId}/commands`,
661
+ path: `${DevicePath}/${this.device.deviceId}/commands`,
688
662
  method: 'POST',
689
663
  headers: {
690
- Authorization: this.platform.config.credentials?.token,
691
- sign: sign,
692
- nonce: nonce,
693
- t: t,
664
+ 'Authorization': this.platform.config.credentials?.token,
665
+ 'sign': sign,
666
+ 'nonce': nonce,
667
+ 't': t,
694
668
  'Content-Type': 'application/json',
669
+ 'Content-Length': body.length,
695
670
  },
696
671
  };
697
-
698
- const req = https.request(options, (res) => {
672
+ const req = https.request(options, res => {
699
673
  this.debugLog(`Humidifier: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
700
674
  this.statusCode({ res });
701
- res.on('data', (d) => {
675
+ res.on('data', d => {
702
676
  this.debugLog(`Humidifier: ${this.accessory.displayName} d: ${d}`);
703
677
  });
704
678
  });
705
-
706
- req.on('error', (error) => {
707
- this.errorLog(`Humidifier: ${this.accessory.displayName} error: ${error}`);
679
+ req.on('error', (e: any) => {
680
+ this.errorLog(`Humidifier: ${this.accessory.displayName} error message: ${e.message}`);
708
681
  });
709
-
710
- req.write(payload);
682
+ req.write(body);
711
683
  req.end();
712
-
713
684
  this.debugLog(`Humidifier: ${this.accessory.displayName} pushActiveChanges: ${JSON.stringify(req)}`);
714
685
  }
715
686
  } catch (e: any) {
@@ -6,7 +6,7 @@ import { interval, Subject } from 'rxjs';
6
6
  import { SwitchBotPlatform } from '../platform';
7
7
  import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
8
8
  import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
9
- import { device, devicesConfig, payload, deviceStatus, HostDomain, DevicePath } from '../settings';
9
+ import { device, devicesConfig, deviceStatus, HostDomain, DevicePath } from '../settings';
10
10
 
11
11
  export class Lock {
12
12
  // Services
@@ -214,58 +214,53 @@ export class Lock {
214
214
  * Lock - "command" "unlock" "default" = set to ???? state - LockCurrentState
215
215
  */
216
216
  async pushChanges(): Promise<void> {
217
- const payload = {
218
- commandType: 'command',
219
- parameter: 'default',
220
- } as payload;
221
-
222
- if (this.LockTargetState) {
223
- payload.command = 'lock';
224
- } else {
225
- payload.command = 'unlock';
226
- }
227
-
228
- this.infoLog(
229
- `Lock: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},` +
230
- ` parameter: ${payload.parameter}, commandType: ${payload.commandType}`,
231
- );
232
-
233
- // Make the API request
217
+ // Make Push On request to the API
234
218
  const t = Date.now();
235
219
  const nonce = 'requestID';
236
220
  const data = this.platform.config.credentials?.token + t + nonce;
237
- const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
221
+ const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret)
222
+ .update(Buffer.from(data, 'utf-8'))
223
+ .digest();
238
224
  const sign = signTerm.toString('base64');
239
225
  this.debugLog(`Lock: ${this.accessory.displayName} sign: ${sign}`);
226
+ let command = '';
227
+ if (this.LockTargetState) {
228
+ command = 'lock';
229
+ } else {
230
+ command = 'unlock';
231
+ }
232
+ const body = JSON.stringify({
233
+ 'command': `${command}`,
234
+ 'parameter': 'default',
235
+ 'commandType': 'command',
236
+ });
237
+ this.infoLog(`Lock: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
240
238
  const options = {
241
- hostname: 'api.switch-bot.com',
239
+ hostname: HostDomain,
242
240
  port: 443,
243
- path: `/v1.1/devices/${this.device.deviceId}/commands`,
241
+ path: `${DevicePath}/${this.device.deviceId}/commands`,
244
242
  method: 'POST',
245
243
  headers: {
246
- Authorization: this.platform.config.credentials?.token,
247
- sign: sign,
248
- nonce: nonce,
249
- t: t,
244
+ 'Authorization': this.platform.config.credentials?.token,
245
+ 'sign': sign,
246
+ 'nonce': nonce,
247
+ 't': t,
250
248
  'Content-Type': 'application/json',
249
+ 'Content-Length': body.length,
251
250
  },
252
251
  };
253
-
254
- const req = https.request(options, (res) => {
252
+ const req = https.request(options, res => {
255
253
  this.debugLog(`Lock: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
256
254
  this.statusCode({ res });
257
- res.on('data', (d) => {
255
+ res.on('data', d => {
258
256
  this.debugLog(`Lock: ${this.accessory.displayName} d: ${d}`);
259
257
  });
260
258
  });
261
-
262
- req.on('error', (error) => {
263
- this.errorLog(`Lock: ${this.accessory.displayName} error: ${error}`);
259
+ req.on('error', (e: any) => {
260
+ this.errorLog(`Lock: ${this.accessory.displayName} error message: ${e.message}`);
264
261
  });
265
-
266
- req.write(payload);
262
+ req.write(body);
267
263
  req.end();
268
-
269
264
  this.debugLog(`Lock: ${this.accessory.displayName} pushchanges: ${JSON.stringify(req)}`);
270
265
  this.accessory.context.On = this.LockTargetState;
271
266
  interval(5000)
@@ -387,7 +387,7 @@ export class Meter {
387
387
  });
388
388
  // Set an event handler
389
389
  switchbot.onadvertisement = (ad: any) => {
390
- this.warnLog(JSON.stringify(ad, null, ' '));
390
+ this.warnLog(`Meter: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
391
391
  };
392
392
  await switchbot.wait(10000);
393
393
  // Stop to monitor
@@ -324,7 +324,7 @@ export class MeterPlus {
324
324
  });
325
325
  // Set an event handler
326
326
  switchbot.onadvertisement = (ad: any) => {
327
- this.warnLog(JSON.stringify(ad, null, ' '));
327
+ this.warnLog(`Meter Plus: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
328
328
  };
329
329
  await switchbot.wait(10000);
330
330
  // Stop to monitor
@@ -306,7 +306,7 @@ export class Motion {
306
306
  });
307
307
  // Set an event handler
308
308
  switchbot.onadvertisement = (ad: any) => {
309
- this.warnLog(JSON.stringify(ad, null, ' '));
309
+ this.warnLog(`Motion Sensor: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
310
310
  };
311
311
  await switchbot.wait(10000);
312
312
  // Stop to monitor
@@ -6,7 +6,7 @@ import { interval, Subject } from 'rxjs';
6
6
  import { SwitchBotPlatform } from '../platform';
7
7
  import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
8
8
  import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
9
- import { device, devicesConfig, payload, deviceStatus, ad, serviceData, switchbot, HostDomain, DevicePath } from '../settings';
9
+ import { device, devicesConfig, deviceStatus, ad, serviceData, switchbot, HostDomain, DevicePath } from '../settings';
10
10
 
11
11
  export class Plug {
12
12
  // Services
@@ -263,7 +263,7 @@ export class Plug {
263
263
  });
264
264
  // Set an event handler
265
265
  switchbot.onadvertisement = (ad: any) => {
266
- this.warnLog(JSON.stringify(ad, null, ' '));
266
+ this.warnLog(`Plug: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
267
267
  };
268
268
  await switchbot.wait(10000);
269
269
  // Stop to monitor
@@ -415,60 +415,56 @@ export class Plug {
415
415
  async openAPIpushChanges() {
416
416
  if (this.platform.config.credentials?.token) {
417
417
  if (this.On !== this.OnCached) {
418
+ // Make Push On request to the API
418
419
  this.debugLog(`Plug: ${this.accessory.displayName} OpenAPI pushChanges`);
419
- const payload = {
420
- commandType: 'command',
421
- parameter: 'default',
422
- } as payload;
423
-
424
- if (this.On) {
425
- payload.command = 'turnOn';
426
- } else {
427
- payload.command = 'turnOff';
428
- }
429
-
430
- this.infoLog(
431
- `Plug: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},` +
432
- ` parameter: ${payload.parameter}, commandType: ${payload.commandType}`,
433
- );
434
-
435
- // Make the API request
436
420
  const t = Date.now();
437
421
  const nonce = 'requestID';
438
422
  const data = this.platform.config.credentials?.token + t + nonce;
439
- const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
423
+ const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret)
424
+ .update(Buffer.from(data, 'utf-8'))
425
+ .digest();
440
426
  const sign = signTerm.toString('base64');
441
427
  this.debugLog(`Plug: ${this.accessory.displayName} sign: ${sign}`);
428
+
429
+ let command = '';
430
+ if (this.On) {
431
+ command = 'turnOn';
432
+ } else {
433
+ command = 'turnOff';
434
+ }
435
+ const body = JSON.stringify({
436
+ 'command': `${command}`,
437
+ 'parameter': 'default',
438
+ 'commandType': 'command',
439
+ });
440
+ this.infoLog(`Plug: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
442
441
  const options = {
443
- hostname: 'api.switch-bot.com',
442
+ hostname: HostDomain,
444
443
  port: 443,
445
- path: `/v1.1/devices/${this.device.deviceId}/commands`,
444
+ path: `${DevicePath}/${this.device.deviceId}/commands`,
446
445
  method: 'POST',
447
446
  headers: {
448
- Authorization: this.platform.config.credentials?.token,
449
- sign: sign,
450
- nonce: nonce,
451
- t: t,
447
+ 'Authorization': this.platform.config.credentials?.token,
448
+ 'sign': sign,
449
+ 'nonce': nonce,
450
+ 't': t,
452
451
  'Content-Type': 'application/json',
452
+ 'Content-Length': body.length,
453
453
  },
454
454
  };
455
-
456
- const req = https.request(options, (res) => {
455
+ const req = https.request(options, res => {
457
456
  this.debugLog(`Plug: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
458
457
  this.statusCode({ res });
459
- res.on('data', (d) => {
458
+ res.on('data', d => {
460
459
  this.debugLog(`Plug: ${this.accessory.displayName} d: ${d}`);
461
460
  });
462
461
  });
463
-
464
- req.on('error', (error) => {
465
- this.errorLog(`Plug: ${this.accessory.displayName} error: ${error}`);
462
+ req.on('error', (e: any) => {
463
+ this.errorLog(`Plug: ${this.accessory.displayName} error message: ${e.message}`);
466
464
  });
467
-
468
- req.write(payload);
465
+ req.write(body);
469
466
  req.end();
470
-
471
- this.debugLog(`Plug: ${this.accessory.displayName} pushchanges: ${JSON.stringify(req)}`);
467
+ this.debugLog(`Plug: ${this.accessory.displayName} openAPIpushChanges: ${JSON.stringify(req)}`);
472
468
  this.OnCached = this.On;
473
469
  this.accessory.context.On = this.OnCached;
474
470
  }