@switchbot/homebridge-switchbot 2.6.0-beta.4 → 2.6.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 (143) hide show
  1. package/.vscode/tasks.json +13 -0
  2. package/CHANGELOG.md +4 -2
  3. package/README.md +18 -9
  4. package/dist/device/blindtilt.d.ts +1 -5
  5. package/dist/device/blindtilt.d.ts.map +1 -1
  6. package/dist/device/blindtilt.js +80 -140
  7. package/dist/device/blindtilt.js.map +1 -1
  8. package/dist/device/bot.d.ts +1 -5
  9. package/dist/device/bot.d.ts.map +1 -1
  10. package/dist/device/bot.js +79 -138
  11. package/dist/device/bot.js.map +1 -1
  12. package/dist/device/ceilinglight.d.ts +2 -6
  13. package/dist/device/ceilinglight.d.ts.map +1 -1
  14. package/dist/device/ceilinglight.js +157 -304
  15. package/dist/device/ceilinglight.js.map +1 -1
  16. package/dist/device/colorbulb.d.ts +2 -6
  17. package/dist/device/colorbulb.d.ts.map +1 -1
  18. package/dist/device/colorbulb.js +164 -311
  19. package/dist/device/colorbulb.js.map +1 -1
  20. package/dist/device/contact.d.ts +1 -5
  21. package/dist/device/contact.d.ts.map +1 -1
  22. package/dist/device/contact.js +36 -72
  23. package/dist/device/contact.js.map +1 -1
  24. package/dist/device/curtain.d.ts +1 -5
  25. package/dist/device/curtain.d.ts.map +1 -1
  26. package/dist/device/curtain.js +78 -138
  27. package/dist/device/curtain.js.map +1 -1
  28. package/dist/device/hub.d.ts +2 -6
  29. package/dist/device/hub.d.ts.map +1 -1
  30. package/dist/device/hub.js +30 -63
  31. package/dist/device/hub.js.map +1 -1
  32. package/dist/device/humidifier.d.ts +1 -5
  33. package/dist/device/humidifier.d.ts.map +1 -1
  34. package/dist/device/humidifier.js +115 -228
  35. package/dist/device/humidifier.js.map +1 -1
  36. package/dist/device/iosensor.d.ts +1 -5
  37. package/dist/device/iosensor.d.ts.map +1 -1
  38. package/dist/device/iosensor.js +38 -74
  39. package/dist/device/iosensor.js.map +1 -1
  40. package/dist/device/{striplight.d.ts → lightstrip.d.ts} +2 -10
  41. package/dist/device/lightstrip.d.ts.map +1 -0
  42. package/dist/device/{striplight.js → lightstrip.js} +138 -194
  43. package/dist/device/lightstrip.js.map +1 -0
  44. package/dist/device/lock.d.ts +1 -5
  45. package/dist/device/lock.d.ts.map +1 -1
  46. package/dist/device/lock.js +66 -126
  47. package/dist/device/lock.js.map +1 -1
  48. package/dist/device/meter.d.ts +1 -5
  49. package/dist/device/meter.d.ts.map +1 -1
  50. package/dist/device/meter.js +38 -74
  51. package/dist/device/meter.js.map +1 -1
  52. package/dist/device/meterplus.d.ts +1 -5
  53. package/dist/device/meterplus.d.ts.map +1 -1
  54. package/dist/device/meterplus.js +39 -75
  55. package/dist/device/meterplus.js.map +1 -1
  56. package/dist/device/motion.d.ts +1 -5
  57. package/dist/device/motion.d.ts.map +1 -1
  58. package/dist/device/motion.js +35 -72
  59. package/dist/device/motion.js.map +1 -1
  60. package/dist/device/plug.d.ts +1 -5
  61. package/dist/device/plug.d.ts.map +1 -1
  62. package/dist/device/plug.js +65 -126
  63. package/dist/device/plug.js.map +1 -1
  64. package/dist/device/robotvacuumcleaner.d.ts +1 -5
  65. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  66. package/dist/device/robotvacuumcleaner.js +79 -151
  67. package/dist/device/robotvacuumcleaner.js.map +1 -1
  68. package/dist/irdevice/airconditioner.d.ts +3 -7
  69. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  70. package/dist/irdevice/airconditioner.js +34 -62
  71. package/dist/irdevice/airconditioner.js.map +1 -1
  72. package/dist/irdevice/airpurifier.d.ts +3 -7
  73. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  74. package/dist/irdevice/airpurifier.js +31 -59
  75. package/dist/irdevice/airpurifier.js.map +1 -1
  76. package/dist/irdevice/camera.d.ts +1 -5
  77. package/dist/irdevice/camera.d.ts.map +1 -1
  78. package/dist/irdevice/camera.js +30 -57
  79. package/dist/irdevice/camera.js.map +1 -1
  80. package/dist/irdevice/fan.d.ts +2 -6
  81. package/dist/irdevice/fan.d.ts.map +1 -1
  82. package/dist/irdevice/fan.js +39 -66
  83. package/dist/irdevice/fan.js.map +1 -1
  84. package/dist/irdevice/light.d.ts +1 -5
  85. package/dist/irdevice/light.d.ts.map +1 -1
  86. package/dist/irdevice/light.js +32 -59
  87. package/dist/irdevice/light.js.map +1 -1
  88. package/dist/irdevice/other.d.ts +1 -5
  89. package/dist/irdevice/other.d.ts.map +1 -1
  90. package/dist/irdevice/other.js +30 -57
  91. package/dist/irdevice/other.js.map +1 -1
  92. package/dist/irdevice/tv.d.ts +3 -7
  93. package/dist/irdevice/tv.d.ts.map +1 -1
  94. package/dist/irdevice/tv.js +46 -76
  95. package/dist/irdevice/tv.js.map +1 -1
  96. package/dist/irdevice/vacuumcleaner.d.ts +2 -6
  97. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  98. package/dist/irdevice/vacuumcleaner.js +30 -58
  99. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  100. package/dist/irdevice/waterheater.d.ts +7 -23
  101. package/dist/irdevice/waterheater.d.ts.map +1 -1
  102. package/dist/irdevice/waterheater.js +41 -74
  103. package/dist/irdevice/waterheater.js.map +1 -1
  104. package/dist/platform.d.ts +14 -5
  105. package/dist/platform.d.ts.map +1 -1
  106. package/dist/platform.js +156 -149
  107. package/dist/platform.js.map +1 -1
  108. package/dist/settings.d.ts +4 -0
  109. package/dist/settings.d.ts.map +1 -1
  110. package/dist/settings.js +5 -1
  111. package/dist/settings.js.map +1 -1
  112. package/nodemon.json +1 -1
  113. package/package.json +10 -10
  114. package/src/device/blindtilt.ts +122 -181
  115. package/src/device/bot.ts +96 -149
  116. package/src/device/ceilinglight.ts +172 -315
  117. package/src/device/colorbulb.ts +178 -324
  118. package/src/device/contact.ts +48 -78
  119. package/src/device/curtain.ts +93 -148
  120. package/src/device/hub.ts +34 -63
  121. package/src/device/humidifier.ts +135 -243
  122. package/src/device/iosensor.ts +49 -79
  123. package/src/device/{striplight.ts → lightstrip.ts} +148 -200
  124. package/src/device/lock.ts +78 -134
  125. package/src/device/meter.ts +48 -78
  126. package/src/device/meterplus.ts +50 -80
  127. package/src/device/motion.ts +46 -77
  128. package/src/device/plug.ts +76 -134
  129. package/src/device/robotvacuumcleaner.ts +91 -159
  130. package/src/irdevice/airconditioner.ts +56 -81
  131. package/src/irdevice/airpurifier.ts +41 -67
  132. package/src/irdevice/camera.ts +35 -60
  133. package/src/irdevice/fan.ts +46 -71
  134. package/src/irdevice/light.ts +38 -63
  135. package/src/irdevice/other.ts +35 -60
  136. package/src/irdevice/tv.ts +52 -80
  137. package/src/irdevice/vacuumcleaner.ts +36 -62
  138. package/src/irdevice/waterheater.ts +46 -76
  139. package/src/platform.ts +144 -158
  140. package/src/settings.ts +5 -0
  141. package/.github/npm-version-script.js +0 -81
  142. package/dist/device/striplight.d.ts.map +0 -1
  143. package/dist/device/striplight.js.map +0 -1
@@ -1,16 +1,11 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.ColorBulb = void 0;
7
- const https_1 = __importDefault(require("https"));
8
- const crypto_1 = __importDefault(require("crypto"));
9
- const super_stringify_1 = __importDefault(require("super-stringify"));
4
+ const undici_1 = require("undici");
5
+ const utils_1 = require("../utils");
10
6
  const rxjs_1 = require("rxjs");
11
7
  const operators_1 = require("rxjs/operators");
12
8
  const settings_1 = require("../settings");
13
- const utils_1 = require("../utils");
14
9
  /**
15
10
  * Platform Accessory
16
11
  * An instance of this class is created for each accessory your platform registers
@@ -146,7 +141,7 @@ class ColorBulb {
146
141
  catch (e) {
147
142
  this.apiError(e);
148
143
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with ${this.device.connectionType} Connection,`
149
- + ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
144
+ + ` Error Message: ${JSON.stringify(e.message)}`);
150
145
  }
151
146
  this.colorBulbUpdateInProgress = false;
152
147
  });
@@ -190,7 +185,7 @@ class ColorBulb {
190
185
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} blue: ${this.blue}`);
191
186
  const [hue, saturation] = (0, settings_1.rgb2hs)(Number(this.red), Number(this.green), Number(this.blue));
192
187
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
193
- + ` hs: ${(0, super_stringify_1.default)((0, settings_1.rgb2hs)(Number(this.red), Number(this.green), Number(this.blue)))}`);
188
+ + ` hs: ${JSON.stringify((0, settings_1.rgb2hs)(Number(this.red), Number(this.green), Number(this.blue)))}`);
194
189
  // Hue
195
190
  this.Hue = hue;
196
191
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.Hue}`);
@@ -220,14 +215,14 @@ class ColorBulb {
220
215
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.Brightness}`);
221
216
  // Color, Hue & Brightness
222
217
  if (this.color) {
223
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} color: ${(0, super_stringify_1.default)(this.color)}`);
218
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} color: ${JSON.stringify(this.color)}`);
224
219
  const [red, green, blue] = this.color.split(':');
225
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} red: ${(0, super_stringify_1.default)(red)}`);
226
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} green: ${(0, super_stringify_1.default)(green)}`);
227
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} blue: ${(0, super_stringify_1.default)(blue)}`);
220
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} red: ${JSON.stringify(red)}`);
221
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} green: ${JSON.stringify(green)}`);
222
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} blue: ${JSON.stringify(blue)}`);
228
223
  const [hue, saturation] = (0, settings_1.rgb2hs)(Number(red), Number(green), Number(blue));
229
224
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
230
- + ` hs: ${(0, super_stringify_1.default)((0, settings_1.rgb2hs)(Number(red), Number(green), Number(blue)))}`);
225
+ + ` hs: ${JSON.stringify((0, settings_1.rgb2hs)(Number(red), Number(green), Number(blue)))}`);
231
226
  // Hue
232
227
  this.Hue = hue;
233
228
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.Hue}`);
@@ -286,7 +281,7 @@ class ColorBulb {
286
281
  this.address = ad.address;
287
282
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},`
288
283
  + ` BLE Address Found: ${this.address}`);
289
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${(0, super_stringify_1.default)(ad.serviceData)}`);
284
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
290
285
  this.serviceData = ad.serviceData;
291
286
  this.powerState = ad.serviceData.power;
292
287
  this.state = ad.serviceData.state;
@@ -300,7 +295,7 @@ class ColorBulb {
300
295
  this.color_mode = ad.serviceData.color_mode;
301
296
  this.speed = ad.serviceData.speed;
302
297
  this.loop_index = ad.serviceData.loop_index;
303
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${(0, super_stringify_1.default)(ad.serviceData)}`);
298
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
304
299
  /*this.debugLog(
305
300
  `${this.device.deviceType}: ${this.accessory.displayName} state: ${ad.serviceData.state}, ` +
306
301
  `delay: ${ad.serviceData.delay}, timer: ${ad.serviceData.timer}, syncUtcTime: ${ad.serviceData.syncUtcTime} ` +
@@ -326,7 +321,7 @@ class ColorBulb {
326
321
  .catch(async (e) => {
327
322
  this.apiError(e);
328
323
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}`
329
- + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
324
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
330
325
  await this.BLERefreshConnection(switchbot);
331
326
  });
332
327
  }
@@ -337,58 +332,25 @@ class ColorBulb {
337
332
  async openAPIRefreshStatus() {
338
333
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
339
334
  try {
340
- const t = Date.now();
341
- const nonce = 'requestID';
342
- const data = this.platform.config.credentials?.token + t + nonce;
343
- const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
344
- const sign = signTerm.toString('base64');
345
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
346
- const options = {
347
- hostname: settings_1.HostDomain,
348
- port: 443,
349
- path: `${settings_1.DevicePath}/${this.device.deviceId}/status`,
350
- method: 'GET',
351
- headers: {
352
- Authorization: this.platform.config.credentials?.token,
353
- sign: sign,
354
- nonce: nonce,
355
- t: t,
356
- 'Content-Type': 'application/json',
357
- },
358
- };
359
- const req = https_1.default.request(options, (res) => {
360
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus statusCode: ${res.statusCode}`);
361
- this.statusCode({ res });
362
- let rawData = '';
363
- res.on('data', (d) => {
364
- rawData += d;
365
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
366
- });
367
- res.on('end', () => {
368
- try {
369
- this.deviceStatus = JSON.parse(rawData);
370
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus: ${(0, super_stringify_1.default)(this.deviceStatus)}`);
371
- this.power = this.deviceStatus.body.power;
372
- this.color = this.deviceStatus.body.color;
373
- this.brightness = this.deviceStatus.body.brightness;
374
- this.colorTemperature = this.deviceStatus.body.colorTemperature;
375
- this.openAPIparseStatus();
376
- this.updateHomeKitCharacteristics();
377
- }
378
- catch (e) {
379
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
380
- }
381
- });
335
+ const { body, statusCode, headers } = await (0, undici_1.request)(`${settings_1.Devices}/${this.device.deviceId}/status`, {
336
+ headers: this.platform.generateHeaders(),
382
337
  });
383
- req.on('error', (e) => {
384
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
385
- });
386
- req.end();
338
+ const deviceStatus = await body.json();
339
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Devices: ${JSON.stringify(deviceStatus.body)}`);
340
+ this.statusCode(statusCode);
341
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Headers: ${JSON.stringify(headers)}`);
342
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(deviceStatus)}`);
343
+ this.power = deviceStatus.body.power;
344
+ this.color = deviceStatus.body.color;
345
+ this.brightness = deviceStatus.body.brightness;
346
+ this.colorTemperature = deviceStatus.body.colorTemperature;
347
+ this.openAPIparseStatus();
348
+ this.updateHomeKitCharacteristics();
387
349
  }
388
350
  catch (e) {
389
351
  this.apiError(e);
390
352
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
391
- + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
353
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
392
354
  }
393
355
  }
394
356
  /**
@@ -461,7 +423,7 @@ class ColorBulb {
461
423
  .catch(async (e) => {
462
424
  this.apiError(e);
463
425
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
464
- + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
426
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
465
427
  await this.BLEPushConnection();
466
428
  });
467
429
  // Push Brightness Update
@@ -508,7 +470,7 @@ class ColorBulb {
508
470
  .catch(async (e) => {
509
471
  this.apiError(e);
510
472
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushBrightnessChanges with ${this.device.connectionType}`
511
- + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
473
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
512
474
  await this.BLEPushConnection();
513
475
  });
514
476
  }
@@ -543,7 +505,7 @@ class ColorBulb {
543
505
  .catch(async (e) => {
544
506
  this.apiError(e);
545
507
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushColorTemperatureChanges with `
546
- + `${this.device.connectionType} Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
508
+ + `${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
547
509
  await this.BLEPushConnection();
548
510
  });
549
511
  }
@@ -555,10 +517,10 @@ class ColorBulb {
555
517
  async BLEpushRGBChanges() {
556
518
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushRGBChanges`);
557
519
  if (this.Hue !== this.accessory.context.Hue || this.Saturation !== this.accessory.context.Saturation) {
558
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${(0, super_stringify_1.default)(this.Hue)}`);
559
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${(0, super_stringify_1.default)(this.Saturation)}`);
520
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${JSON.stringify(this.Hue)}`);
521
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${JSON.stringify(this.Saturation)}`);
560
522
  const [red, green, blue] = (0, settings_1.hs2rgb)(Number(this.Hue), Number(this.Saturation));
561
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} rgb: ${(0, super_stringify_1.default)([red, green, blue])}`);
523
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} rgb: ${JSON.stringify([red, green, blue])}`);
562
524
  const switchbot = await this.platform.connectBLE();
563
525
  // Convert to BLE Address
564
526
  this.device.bleMac = this.device
@@ -582,7 +544,7 @@ class ColorBulb {
582
544
  .catch(async (e) => {
583
545
  this.apiError(e);
584
546
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushRGBChanges with ${this.device.connectionType}`
585
- + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
547
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
586
548
  await this.BLEPushConnection();
587
549
  });
588
550
  }
@@ -592,258 +554,148 @@ class ColorBulb {
592
554
  }
593
555
  }
594
556
  async openAPIpushChanges() {
595
- try {
596
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
597
- if (this.On !== this.accessory.context.On) {
598
- // Make Push On request to the API
599
- const t = Date.now();
600
- const nonce = 'requestID';
601
- const data = this.platform.config.credentials?.token + t + nonce;
602
- const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret)
603
- .update(Buffer.from(data, 'utf-8'))
604
- .digest();
605
- const sign = signTerm.toString('base64');
606
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
607
- let command = '';
608
- if (this.On) {
609
- command = 'turnOn';
610
- }
611
- else {
612
- command = 'turnOff';
613
- }
614
- const body = (0, super_stringify_1.default)({
615
- 'command': `${command}`,
616
- 'parameter': 'default',
617
- 'commandType': 'command',
618
- });
619
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
620
- const options = {
621
- hostname: settings_1.HostDomain,
622
- port: 443,
623
- path: `${settings_1.DevicePath}/${this.device.deviceId}/commands`,
624
- method: 'POST',
625
- headers: {
626
- 'Authorization': this.platform.config.credentials?.token,
627
- 'sign': sign,
628
- 'nonce': nonce,
629
- 't': t,
630
- 'Content-Type': 'application/json',
631
- 'Content-Length': body.length,
632
- },
633
- };
634
- const req = https_1.default.request(options, res => {
635
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges statusCode: ${res.statusCode}`);
636
- this.statusCode({ res });
637
- res.on('data', d => {
638
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
639
- });
640
- });
641
- req.on('error', (e) => {
642
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
643
- });
644
- req.write(body);
645
- req.end();
646
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushChanges: ${(0, super_stringify_1.default)(req)}`);
557
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
558
+ if (this.On !== this.accessory.context.On) {
559
+ let command = '';
560
+ if (this.On) {
561
+ command = 'turnOn';
647
562
  }
648
563
  else {
649
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.` + `On: ${this.On}, `
650
- + `OnCached: ${this.accessory.context.On}`);
564
+ command = 'turnOff';
651
565
  }
652
- // Push Brightness Update
653
- if (this.On) {
654
- await this.pushBrightnessChanges();
655
- }
656
- // Push ColorTemperature Update
657
- if (this.On) {
658
- await this.pushColorTemperatureChanges();
566
+ const body = JSON.stringify({
567
+ 'command': `${command}`,
568
+ 'parameter': 'default',
569
+ 'commandType': 'command',
570
+ });
571
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
572
+ try {
573
+ const { body, statusCode, headers } = await (0, undici_1.request)(`${settings_1.Devices}/${this.device.deviceId}/commands`, {
574
+ method: 'POST',
575
+ headers: this.platform.generateHeaders(),
576
+ });
577
+ const deviceStatus = await body.json();
578
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Devices: ${JSON.stringify(deviceStatus.body)}`);
579
+ this.statusCode(statusCode);
580
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Headers: ${JSON.stringify(headers)}`);
659
581
  }
660
- // Push Hue & Saturation Update
661
- if (this.On) {
662
- await this.pushHueSaturationChanges();
582
+ catch (e) {
583
+ this.apiError(e);
584
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
585
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
663
586
  }
664
587
  }
665
- catch (e) {
666
- this.apiError(e);
667
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
668
- + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
588
+ else {
589
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.` + `On: ${this.On}, `
590
+ + `OnCached: ${this.accessory.context.On}`);
591
+ }
592
+ // Push Hue & Saturation Update
593
+ if (this.On) {
594
+ await this.pushHueSaturationChanges();
595
+ }
596
+ // Push ColorTemperature Update
597
+ if (this.On) {
598
+ await this.pushColorTemperatureChanges();
599
+ }
600
+ // Push Brightness Update
601
+ if (this.On) {
602
+ await this.pushBrightnessChanges();
669
603
  }
670
604
  }
671
605
  async pushHueSaturationChanges() {
672
- try {
673
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushHueSaturationChanges`);
674
- if (this.Hue !== this.accessory.context.Hue || this.Saturation !== this.accessory.context.Saturation) {
675
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${(0, super_stringify_1.default)(this.Hue)}`);
676
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${(0, super_stringify_1.default)(this.Saturation)}`);
677
- const [red, green, blue] = (0, settings_1.hs2rgb)(Number(this.Hue), Number(this.Saturation));
678
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} rgb: ${(0, super_stringify_1.default)([red, green, blue])}`);
679
- // Make Push Hue/Saturation request to the API
680
- const t = Date.now();
681
- const nonce = 'requestID';
682
- const data = this.platform.config.credentials?.token + t + nonce;
683
- const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret)
684
- .update(Buffer.from(data, 'utf-8'))
685
- .digest();
686
- const sign = signTerm.toString('base64');
687
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
688
- const body = (0, super_stringify_1.default)({
689
- 'command': 'setColor',
690
- 'parameter': `${red}:${green}:${blue}`,
691
- 'commandType': 'command',
692
- });
693
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
694
- const options = {
695
- hostname: settings_1.HostDomain,
696
- port: 443,
697
- path: `${settings_1.DevicePath}/${this.device.deviceId}/commands`,
606
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushHueSaturationChanges`);
607
+ if (this.Hue !== this.accessory.context.Hue || this.Saturation !== this.accessory.context.Saturation) {
608
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${JSON.stringify(this.Hue)}`);
609
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${JSON.stringify(this.Saturation)}`);
610
+ const [red, green, blue] = (0, settings_1.hs2rgb)(Number(this.Hue), Number(this.Saturation));
611
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} rgb: ${JSON.stringify([red, green, blue])}`);
612
+ const body = JSON.stringify({
613
+ 'command': 'setColor',
614
+ 'parameter': `${red}:${green}:${blue}`,
615
+ 'commandType': 'command',
616
+ });
617
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
618
+ try {
619
+ const { body, statusCode, headers } = await (0, undici_1.request)(`${settings_1.Devices}/${this.device.deviceId}/commands`, {
698
620
  method: 'POST',
699
- headers: {
700
- 'Authorization': this.platform.config.credentials?.token,
701
- 'sign': sign,
702
- 'nonce': nonce,
703
- 't': t,
704
- 'Content-Type': 'application/json',
705
- 'Content-Length': body.length,
706
- },
707
- };
708
- const req = https_1.default.request(options, res => {
709
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushHueSaturationChanges statusCode: ${res.statusCode}`);
710
- res.on('data', d => {
711
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
712
- });
713
- });
714
- req.on('error', (e) => {
715
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
621
+ headers: this.platform.generateHeaders(),
716
622
  });
717
- req.write(body);
718
- req.end();
719
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushHueSaturationChanges: ${(0, super_stringify_1.default)(req)}`);
623
+ const deviceStatus = await body.json();
624
+ this.debugLog(`Devices: ${JSON.stringify(deviceStatus.body)}`);
625
+ this.statusCode(statusCode);
626
+ this.debugLog(`Headers: ${JSON.stringify(headers)}`);
720
627
  }
721
- else {
722
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushHueSaturationChanges. Hue: ${this.Hue}, `
723
- + `HueCached: ${this.accessory.context.Hue}, Saturation: ${this.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`);
628
+ catch (e) {
629
+ this.apiError(e);
630
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushHueSaturationChanges with ${this.device.connectionType}`
631
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
724
632
  }
725
633
  }
726
- catch (e) {
727
- this.apiError(e);
728
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushHueSaturationChanges with ${this.device.connectionType}`
729
- + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
634
+ else {
635
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushHueSaturationChanges. Hue: ${this.Hue}, `
636
+ + `HueCached: ${this.accessory.context.Hue}, Saturation: ${this.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`);
730
637
  }
731
638
  }
732
639
  async pushColorTemperatureChanges() {
733
- try {
734
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushColorTemperatureChanges`);
735
- if (this.ColorTemperature !== this.accessory.context.ColorTemperature) {
736
- const kelvin = Math.round(1000000 / Number(this.ColorTemperature));
737
- this.cacheKelvin = kelvin;
738
- // Make Push On request to the API
739
- const t = Date.now();
740
- const nonce = 'requestID';
741
- const data = this.platform.config.credentials?.token + t + nonce;
742
- const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret)
743
- .update(Buffer.from(data, 'utf-8'))
744
- .digest();
745
- const sign = signTerm.toString('base64');
746
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
747
- const body = (0, super_stringify_1.default)({
748
- 'command': 'setColorTemperature',
749
- 'parameter': `${kelvin}`,
750
- 'commandType': 'command',
751
- });
752
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
753
- const options = {
754
- hostname: settings_1.HostDomain,
755
- port: 443,
756
- path: `${settings_1.DevicePath}/${this.device.deviceId}/commands`,
640
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushColorTemperatureChanges`);
641
+ if (this.ColorTemperature !== this.accessory.context.ColorTemperature) {
642
+ const kelvin = Math.round(1000000 / Number(this.ColorTemperature));
643
+ this.cacheKelvin = kelvin;
644
+ const body = JSON.stringify({
645
+ 'command': 'setColorTemperature',
646
+ 'parameter': `${kelvin}`,
647
+ 'commandType': 'command',
648
+ });
649
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
650
+ try {
651
+ const { body, statusCode, headers } = await (0, undici_1.request)(`${settings_1.Devices}/${this.device.deviceId}/commands`, {
757
652
  method: 'POST',
758
- headers: {
759
- 'Authorization': this.platform.config.credentials?.token,
760
- 'sign': sign,
761
- 'nonce': nonce,
762
- 't': t,
763
- 'Content-Type': 'application/json',
764
- 'Content-Length': body.length,
765
- },
766
- };
767
- const req = https_1.default.request(options, res => {
768
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
769
- res.on('data', d => {
770
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
771
- });
772
- });
773
- req.on('error', (e) => {
774
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
653
+ headers: this.platform.generateHeaders(),
775
654
  });
776
- req.write(body);
777
- req.end();
778
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushColorTemperatureChanges: ${(0, super_stringify_1.default)(req)}`);
779
- this.accessory.context.ColorTemperature = this.ColorTemperature;
655
+ const deviceStatus = await body.json();
656
+ this.debugLog(`Devices: ${JSON.stringify(deviceStatus.body)}`);
657
+ this.statusCode(statusCode);
658
+ this.debugLog(`Headers: ${JSON.stringify(headers)}`);
780
659
  }
781
- else {
782
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushColorTemperatureChanges.` +
783
- `ColorTemperature: ${this.ColorTemperature}, ColorTemperatureCached: ${this.accessory.context.ColorTemperature}`);
660
+ catch (e) {
661
+ this.apiError(e);
662
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushColorTemperatureChanges with ${this.device.connectionType}`
663
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
784
664
  }
785
665
  }
786
- catch (e) {
787
- this.apiError(e);
788
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushColorTemperatureChanges with ${this.device.connectionType}`
789
- + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
666
+ else {
667
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushColorTemperatureChanges.` +
668
+ `ColorTemperature: ${this.ColorTemperature}, ColorTemperatureCached: ${this.accessory.context.ColorTemperature}`);
790
669
  }
791
670
  }
792
671
  async pushBrightnessChanges() {
793
- try {
794
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushBrightnessChanges`);
795
- if (this.Brightness !== this.accessory.context.Brightness) {
796
- // Make Push On request to the API
797
- const t = Date.now();
798
- const nonce = 'requestID';
799
- const data = this.platform.config.credentials?.token + t + nonce;
800
- const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret)
801
- .update(Buffer.from(data, 'utf-8'))
802
- .digest();
803
- const sign = signTerm.toString('base64');
804
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
805
- const body = (0, super_stringify_1.default)({
806
- 'command': 'setBrightness',
807
- 'parameter': `${this.Brightness}`,
808
- 'commandType': 'command',
809
- });
810
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
811
- const options = {
812
- hostname: settings_1.HostDomain,
813
- port: 443,
814
- path: `${settings_1.DevicePath}/${this.device.deviceId}/commands`,
672
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushBrightnessChanges`);
673
+ if (this.Brightness !== this.accessory.context.Brightness) {
674
+ const body = JSON.stringify({
675
+ 'command': 'setBrightness',
676
+ 'parameter': `${this.Brightness}`,
677
+ 'commandType': 'command',
678
+ });
679
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
680
+ try {
681
+ const { body, statusCode, headers } = await (0, undici_1.request)(`${settings_1.Devices}/${this.device.deviceId}/commands`, {
815
682
  method: 'POST',
816
- headers: {
817
- 'Authorization': this.platform.config.credentials?.token,
818
- 'sign': sign,
819
- 'nonce': nonce,
820
- 't': t,
821
- 'Content-Type': 'application/json',
822
- 'Content-Length': body.length,
823
- },
824
- };
825
- const req = https_1.default.request(options, res => {
826
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushBrightnessChanges statusCode: ${res.statusCode}`);
827
- res.on('data', d => {
828
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
829
- });
830
- });
831
- req.on('error', (e) => {
832
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
683
+ headers: this.platform.generateHeaders(),
833
684
  });
834
- req.write(body);
835
- req.end();
836
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushBrightnessChanges: ${(0, super_stringify_1.default)(req)}`);
685
+ const deviceStatus = await body.json();
686
+ this.debugLog(`Devices: ${JSON.stringify(deviceStatus.body)}`);
687
+ this.statusCode(statusCode);
688
+ this.debugLog(`Headers: ${JSON.stringify(headers)}`);
837
689
  }
838
- else {
839
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushBrightnessChanges.` + `Brightness: ${this.Brightness}, `
840
- + `BrightnessCached: ${this.accessory.context.Brightness}`);
690
+ catch (e) {
691
+ this.apiError(e);
692
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushBrightnessChanges with ${this.device.connectionType}`
693
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
841
694
  }
842
695
  }
843
- catch (e) {
844
- this.apiError(e);
845
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushBrightnessChanges with ${this.device.connectionType}`
846
- + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
696
+ else {
697
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushBrightnessChanges.` + `Brightness: ${this.Brightness}, `
698
+ + `BrightnessCached: ${this.accessory.context.Brightness}`);
847
699
  }
848
700
  }
849
701
  /**
@@ -1008,7 +860,7 @@ class ColorBulb {
1008
860
  });
1009
861
  // Set an event handler
1010
862
  switchbot.onadvertisement = (ad) => {
1011
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${(0, super_stringify_1.default)(ad, null, ' ')}`);
863
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
1012
864
  };
1013
865
  await (0, utils_1.sleep)(10000);
1014
866
  // Stop to monitor
@@ -1071,38 +923,39 @@ class ColorBulb {
1071
923
  }
1072
924
  }
1073
925
  }
1074
- async statusCode({ res }) {
1075
- switch (res.statusCode) {
926
+ async statusCode(statusCode) {
927
+ switch (statusCode) {
1076
928
  case 151:
1077
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this device type.`);
929
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this deviceType, statusCode: ${statusCode}`);
1078
930
  break;
1079
931
  case 152:
1080
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found.`);
932
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found, statusCode: ${statusCode}`);
1081
933
  break;
1082
934
  case 160:
1083
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported.`);
935
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported, statusCode: ${statusCode}`);
1084
936
  break;
1085
937
  case 161:
1086
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline.`);
1087
- await this.offlineOff();
938
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline, statusCode: ${statusCode}`);
939
+ this.offlineOff();
1088
940
  break;
1089
941
  case 171:
1090
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} is offline. Hub: ${this.device.hubDeviceId}`);
1091
- await this.offlineOff();
942
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. `
943
+ + `Hub: ${this.device.hubDeviceId}`);
944
+ this.offlineOff();
1092
945
  break;
1093
946
  case 190:
1094
947
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
1095
- ` Or command: ${(0, super_stringify_1.default)(res)} format is invalid`);
948
+ ` Or command format is invalid, statusCode: ${statusCode}`);
1096
949
  break;
1097
950
  case 100:
1098
- if (this.platform.debugMode) {
1099
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent.`);
1100
- }
951
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent, statusCode: ${statusCode}`);
952
+ break;
953
+ case 200:
954
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Request successful, statusCode: ${statusCode}`);
1101
955
  break;
1102
956
  default:
1103
- if (this.platform.debugMode) {
1104
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode.`);
1105
- }
957
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: `
958
+ + `${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`);
1106
959
  }
1107
960
  }
1108
961
  async offlineOff() {
@@ -1206,7 +1059,7 @@ class ColorBulb {
1206
1059
  config['maxRetry'] = device.maxRetry;
1207
1060
  }
1208
1061
  if (Object.entries(config).length !== 0) {
1209
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${(0, super_stringify_1.default)(config)}`);
1062
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
1210
1063
  }
1211
1064
  }
1212
1065
  async logs(device) {