@switchbot/homebridge-switchbot 2.6.0-beta.4 → 2.6.0-beta.5

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 (138) hide show
  1. package/.vscode/tasks.json +13 -0
  2. package/dist/device/blindtilt.d.ts +1 -5
  3. package/dist/device/blindtilt.d.ts.map +1 -1
  4. package/dist/device/blindtilt.js +80 -140
  5. package/dist/device/blindtilt.js.map +1 -1
  6. package/dist/device/bot.d.ts +1 -5
  7. package/dist/device/bot.d.ts.map +1 -1
  8. package/dist/device/bot.js +79 -138
  9. package/dist/device/bot.js.map +1 -1
  10. package/dist/device/ceilinglight.d.ts +2 -6
  11. package/dist/device/ceilinglight.d.ts.map +1 -1
  12. package/dist/device/ceilinglight.js +157 -304
  13. package/dist/device/ceilinglight.js.map +1 -1
  14. package/dist/device/colorbulb.d.ts +2 -6
  15. package/dist/device/colorbulb.d.ts.map +1 -1
  16. package/dist/device/colorbulb.js +164 -311
  17. package/dist/device/colorbulb.js.map +1 -1
  18. package/dist/device/contact.d.ts +1 -5
  19. package/dist/device/contact.d.ts.map +1 -1
  20. package/dist/device/contact.js +36 -72
  21. package/dist/device/contact.js.map +1 -1
  22. package/dist/device/curtain.d.ts +1 -5
  23. package/dist/device/curtain.d.ts.map +1 -1
  24. package/dist/device/curtain.js +78 -138
  25. package/dist/device/curtain.js.map +1 -1
  26. package/dist/device/hub.d.ts +2 -6
  27. package/dist/device/hub.d.ts.map +1 -1
  28. package/dist/device/hub.js +30 -63
  29. package/dist/device/hub.js.map +1 -1
  30. package/dist/device/humidifier.d.ts +1 -5
  31. package/dist/device/humidifier.d.ts.map +1 -1
  32. package/dist/device/humidifier.js +115 -228
  33. package/dist/device/humidifier.js.map +1 -1
  34. package/dist/device/iosensor.d.ts +1 -5
  35. package/dist/device/iosensor.d.ts.map +1 -1
  36. package/dist/device/iosensor.js +38 -74
  37. package/dist/device/iosensor.js.map +1 -1
  38. package/dist/device/lock.d.ts +1 -5
  39. package/dist/device/lock.d.ts.map +1 -1
  40. package/dist/device/lock.js +66 -126
  41. package/dist/device/lock.js.map +1 -1
  42. package/dist/device/meter.d.ts +1 -5
  43. package/dist/device/meter.d.ts.map +1 -1
  44. package/dist/device/meter.js +38 -74
  45. package/dist/device/meter.js.map +1 -1
  46. package/dist/device/meterplus.d.ts +1 -5
  47. package/dist/device/meterplus.d.ts.map +1 -1
  48. package/dist/device/meterplus.js +39 -75
  49. package/dist/device/meterplus.js.map +1 -1
  50. package/dist/device/motion.d.ts +1 -5
  51. package/dist/device/motion.d.ts.map +1 -1
  52. package/dist/device/motion.js +35 -72
  53. package/dist/device/motion.js.map +1 -1
  54. package/dist/device/plug.d.ts +1 -5
  55. package/dist/device/plug.d.ts.map +1 -1
  56. package/dist/device/plug.js +65 -126
  57. package/dist/device/plug.js.map +1 -1
  58. package/dist/device/robotvacuumcleaner.d.ts +1 -5
  59. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  60. package/dist/device/robotvacuumcleaner.js +79 -151
  61. package/dist/device/robotvacuumcleaner.js.map +1 -1
  62. package/dist/device/striplight.d.ts +1 -9
  63. package/dist/device/striplight.d.ts.map +1 -1
  64. package/dist/device/striplight.js +137 -193
  65. package/dist/device/striplight.js.map +1 -1
  66. package/dist/irdevice/airconditioner.d.ts +3 -7
  67. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  68. package/dist/irdevice/airconditioner.js +34 -62
  69. package/dist/irdevice/airconditioner.js.map +1 -1
  70. package/dist/irdevice/airpurifier.d.ts +3 -7
  71. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  72. package/dist/irdevice/airpurifier.js +31 -59
  73. package/dist/irdevice/airpurifier.js.map +1 -1
  74. package/dist/irdevice/camera.d.ts +1 -5
  75. package/dist/irdevice/camera.d.ts.map +1 -1
  76. package/dist/irdevice/camera.js +30 -57
  77. package/dist/irdevice/camera.js.map +1 -1
  78. package/dist/irdevice/fan.d.ts +2 -6
  79. package/dist/irdevice/fan.d.ts.map +1 -1
  80. package/dist/irdevice/fan.js +39 -66
  81. package/dist/irdevice/fan.js.map +1 -1
  82. package/dist/irdevice/light.d.ts +1 -5
  83. package/dist/irdevice/light.d.ts.map +1 -1
  84. package/dist/irdevice/light.js +32 -59
  85. package/dist/irdevice/light.js.map +1 -1
  86. package/dist/irdevice/other.d.ts +1 -5
  87. package/dist/irdevice/other.d.ts.map +1 -1
  88. package/dist/irdevice/other.js +30 -57
  89. package/dist/irdevice/other.js.map +1 -1
  90. package/dist/irdevice/tv.d.ts +3 -7
  91. package/dist/irdevice/tv.d.ts.map +1 -1
  92. package/dist/irdevice/tv.js +46 -76
  93. package/dist/irdevice/tv.js.map +1 -1
  94. package/dist/irdevice/vacuumcleaner.d.ts +2 -6
  95. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  96. package/dist/irdevice/vacuumcleaner.js +30 -58
  97. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  98. package/dist/irdevice/waterheater.d.ts +7 -23
  99. package/dist/irdevice/waterheater.d.ts.map +1 -1
  100. package/dist/irdevice/waterheater.js +41 -74
  101. package/dist/irdevice/waterheater.js.map +1 -1
  102. package/dist/platform.d.ts +14 -5
  103. package/dist/platform.d.ts.map +1 -1
  104. package/dist/platform.js +153 -146
  105. package/dist/platform.js.map +1 -1
  106. package/dist/settings.d.ts +4 -0
  107. package/dist/settings.d.ts.map +1 -1
  108. package/dist/settings.js +5 -1
  109. package/dist/settings.js.map +1 -1
  110. package/nodemon.json +1 -1
  111. package/package.json +10 -10
  112. package/src/device/blindtilt.ts +122 -181
  113. package/src/device/bot.ts +96 -149
  114. package/src/device/ceilinglight.ts +172 -315
  115. package/src/device/colorbulb.ts +178 -324
  116. package/src/device/contact.ts +48 -78
  117. package/src/device/curtain.ts +93 -148
  118. package/src/device/hub.ts +34 -63
  119. package/src/device/humidifier.ts +135 -243
  120. package/src/device/iosensor.ts +49 -79
  121. package/src/device/lock.ts +78 -134
  122. package/src/device/meter.ts +48 -78
  123. package/src/device/meterplus.ts +50 -80
  124. package/src/device/motion.ts +46 -77
  125. package/src/device/plug.ts +76 -134
  126. package/src/device/robotvacuumcleaner.ts +91 -159
  127. package/src/device/striplight.ts +148 -200
  128. package/src/irdevice/airconditioner.ts +56 -81
  129. package/src/irdevice/airpurifier.ts +41 -67
  130. package/src/irdevice/camera.ts +35 -60
  131. package/src/irdevice/fan.ts +46 -71
  132. package/src/irdevice/light.ts +38 -63
  133. package/src/irdevice/other.ts +35 -60
  134. package/src/irdevice/tv.ts +52 -80
  135. package/src/irdevice/vacuumcleaner.ts +36 -62
  136. package/src/irdevice/waterheater.ts +46 -76
  137. package/src/platform.ts +143 -157
  138. package/src/settings.ts +5 -0
@@ -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.StripLight = void 0;
7
- const https_1 = __importDefault(require("https"));
8
- const crypto_1 = __importDefault(require("crypto"));
4
+ const undici_1 = require("undici");
5
+ const utils_1 = require("../utils");
9
6
  const rxjs_1 = require("rxjs");
10
- const super_stringify_1 = __importDefault(require("super-stringify"));
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
@@ -145,7 +140,7 @@ class StripLight {
145
140
  catch (e) {
146
141
  this.apiError(e);
147
142
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with ${this.device.connectionType} Connection,`
148
- + ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
143
+ + ` Error Message: ${JSON.stringify(e.message)}`);
149
144
  }
150
145
  this.stripLightUpdateInProgress = false;
151
146
  });
@@ -196,14 +191,14 @@ class StripLight {
196
191
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.Brightness}`);
197
192
  // Color, Hue & Brightness
198
193
  if (this.color) {
199
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} color: ${(0, super_stringify_1.default)(this.color)}`);
194
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} color: ${JSON.stringify(this.color)}`);
200
195
  const [red, green, blue] = this.color.split(':');
201
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} red: ${(0, super_stringify_1.default)(red)}`);
202
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} green: ${(0, super_stringify_1.default)(green)}`);
203
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} blue: ${(0, super_stringify_1.default)(blue)}`);
196
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} red: ${JSON.stringify(red)}`);
197
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} green: ${JSON.stringify(green)}`);
198
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} blue: ${JSON.stringify(blue)}`);
204
199
  const [hue, saturation] = (0, settings_1.rgb2hs)(Number(red), Number(green), Number(blue));
205
200
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
206
- + ` hs: ${(0, super_stringify_1.default)((0, settings_1.rgb2hs)(Number(red), Number(green), Number(blue)))}`);
201
+ + ` hs: ${JSON.stringify((0, settings_1.rgb2hs)(Number(red), Number(green), Number(blue)))}`);
207
202
  // Hue
208
203
  this.Hue = hue;
209
204
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.Hue}`);
@@ -254,7 +249,7 @@ class StripLight {
254
249
  this.address = ad.address;
255
250
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},`
256
251
  + ` BLE Address Found: ${this.address}`);
257
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${(0, super_stringify_1.default)(ad.serviceData)}`);
252
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
258
253
  this.serviceData = ad.serviceData;
259
254
  //this.state = ad.serviceData.state;
260
255
  //this.delay = ad.serviceData.delay;
@@ -263,7 +258,7 @@ class StripLight {
263
258
  //this.wifiRssi = ad.serviceData.wifiRssi;
264
259
  //this.overload = ad.serviceData.overload;
265
260
  //this.currentPower = ad.serviceData.currentPower;
266
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${(0, super_stringify_1.default)(ad.serviceData)}`);
261
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
267
262
  /*this.debugLog(
268
263
  `${this.device.deviceType}: ${this.accessory.displayName} state: ${ad.serviceData.state}, ` +
269
264
  `delay: ${ad.serviceData.delay}, timer: ${ad.serviceData.timer}, syncUtcTime: ${ad.serviceData.syncUtcTime} ` +
@@ -289,7 +284,7 @@ class StripLight {
289
284
  .catch(async (e) => {
290
285
  this.apiError(e);
291
286
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}`
292
- + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
287
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
293
288
  await this.BLERefreshConnection(switchbot);
294
289
  });
295
290
  }
@@ -300,57 +295,24 @@ class StripLight {
300
295
  async openAPIRefreshStatus() {
301
296
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
302
297
  try {
303
- const t = Date.now();
304
- const nonce = 'requestID';
305
- const data = this.platform.config.credentials?.token + t + nonce;
306
- const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
307
- const sign = signTerm.toString('base64');
308
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
309
- const options = {
310
- hostname: settings_1.HostDomain,
311
- port: 443,
312
- path: `${settings_1.DevicePath}/${this.device.deviceId}/status`,
313
- method: 'GET',
314
- headers: {
315
- Authorization: this.platform.config.credentials?.token,
316
- sign: sign,
317
- nonce: nonce,
318
- t: t,
319
- 'Content-Type': 'application/json',
320
- },
321
- };
322
- const req = https_1.default.request(options, (res) => {
323
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus statusCode: ${res.statusCode}`);
324
- this.statusCode({ res });
325
- let rawData = '';
326
- res.on('data', (d) => {
327
- rawData += d;
328
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
329
- });
330
- res.on('end', () => {
331
- try {
332
- this.deviceStatus = JSON.parse(rawData);
333
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus: ${(0, super_stringify_1.default)(this.deviceStatus)}`);
334
- this.power = this.deviceStatus.body.power;
335
- this.color = this.deviceStatus.body.color;
336
- this.brightness = this.deviceStatus.body.brightness;
337
- this.openAPIparseStatus();
338
- this.updateHomeKitCharacteristics();
339
- }
340
- catch (e) {
341
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
342
- }
343
- });
344
- });
345
- req.on('error', (e) => {
346
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
298
+ const { body, statusCode, headers } = await (0, undici_1.request)(`${settings_1.Devices}/${this.device.deviceId}/status`, {
299
+ headers: this.platform.generateHeaders(),
347
300
  });
348
- req.end();
301
+ const deviceStatus = await body.json();
302
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Devices: ${JSON.stringify(deviceStatus.body)}`);
303
+ this.statusCode(statusCode);
304
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Headers: ${JSON.stringify(headers)}`);
305
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(deviceStatus)}`);
306
+ this.power = deviceStatus.body.power;
307
+ this.color = deviceStatus.body.color;
308
+ this.brightness = deviceStatus.body.brightness;
309
+ this.openAPIparseStatus();
310
+ this.updateHomeKitCharacteristics();
349
311
  }
350
312
  catch (e) {
351
313
  this.apiError(e);
352
314
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
353
- + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
315
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
354
316
  }
355
317
  }
356
318
  /**
@@ -422,7 +384,7 @@ class StripLight {
422
384
  .catch(async (e) => {
423
385
  this.apiError(e);
424
386
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
425
- + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
387
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
426
388
  await this.BLEPushConnection();
427
389
  });
428
390
  // Push Brightness Update
@@ -465,7 +427,7 @@ class StripLight {
465
427
  .catch(async (e) => {
466
428
  this.apiError(e);
467
429
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushBrightnessChanges with ${this.device.connectionType}`
468
- + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
430
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
469
431
  await this.BLEPushConnection();
470
432
  });
471
433
  }
@@ -477,10 +439,10 @@ class StripLight {
477
439
  async BLEpushRGBChanges() {
478
440
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushRGBChanges`);
479
441
  if (this.Hue !== this.accessory.context.Hue || this.Saturation !== this.accessory.context.Saturation) {
480
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${(0, super_stringify_1.default)(this.Hue)}`);
481
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${(0, super_stringify_1.default)(this.Saturation)}`);
442
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${JSON.stringify(this.Hue)}`);
443
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${JSON.stringify(this.Saturation)}`);
482
444
  const [red, green, blue] = (0, settings_1.hs2rgb)(Number(this.Hue), Number(this.Saturation));
483
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} rgb: ${(0, super_stringify_1.default)([red, green, blue])}`);
445
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} rgb: ${JSON.stringify([red, green, blue])}`);
484
446
  const switchbot = await this.platform.connectBLE();
485
447
  // Convert to BLE Address
486
448
  this.device.bleMac = this.device
@@ -504,7 +466,7 @@ class StripLight {
504
466
  .catch(async (e) => {
505
467
  this.apiError(e);
506
468
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushRGBChanges with ${this.device.connectionType}`
507
- + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
469
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
508
470
  await this.BLEPushConnection();
509
471
  });
510
472
  }
@@ -514,133 +476,114 @@ class StripLight {
514
476
  }
515
477
  }
516
478
  async openAPIpushChanges() {
517
- const change = 'openAPIpushChanges';
518
- try {
519
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${change}`);
520
- if (this.On !== this.accessory.context.On) {
521
- // Make Push On request to the API
522
- const command = this.On ? 'turnOn' : 'turnOff';
523
- /*if (this.On) {
524
- command = 'turnOn';
525
- } else {
526
- command = 'turnOff';
527
- }*/
528
- const body = (0, super_stringify_1.default)({
529
- 'command': `${command}`,
530
- 'parameter': 'default',
531
- 'commandType': 'command',
479
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
480
+ if (this.On !== this.accessory.context.On) {
481
+ const command = this.On ? 'turnOn' : 'turnOff';
482
+ /*if (this.On) {
483
+ command = 'turnOn';
484
+ } else {
485
+ command = 'turnOff';
486
+ }*/
487
+ const body = JSON.stringify({
488
+ 'command': `${command}`,
489
+ 'parameter': 'default',
490
+ 'commandType': 'command',
491
+ });
492
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
493
+ try {
494
+ const { body, statusCode, headers } = await (0, undici_1.request)(`${settings_1.Devices}/${this.device.deviceId}/commands`, {
495
+ method: 'POST',
496
+ headers: this.platform.generateHeaders(),
532
497
  });
533
- await this.request({ body, change });
498
+ const deviceStatus = await body.json();
499
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Devices: ${JSON.stringify(deviceStatus.body)}`);
500
+ this.statusCode(statusCode);
501
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Headers: ${JSON.stringify(headers)}`);
534
502
  }
535
- else {
536
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No ${change}.` + `On: ${this.On}, `
537
- + `OnCached: ${this.accessory.context.On}`);
538
- }
539
- // Push Brightness Update
540
- if (this.On) {
541
- await this.pushBrightnessChanges();
542
- }
543
- // Push Hue & Saturation Update
544
- if (this.On) {
545
- await this.pushHueSaturationChanges();
503
+ catch (e) {
504
+ this.apiError(e);
505
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
506
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
546
507
  }
547
508
  }
548
- catch (e) {
549
- this.apiError(e);
550
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed ${change} with ${this.device.connectionType}`
551
- + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
509
+ else {
510
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.` + `On: ${this.On}, `
511
+ + `OnCached: ${this.accessory.context.On}`);
512
+ }
513
+ // Push Hue & Saturation Update
514
+ if (this.On) {
515
+ await this.pushHueSaturationChanges();
516
+ }
517
+ // Push Brightness Update
518
+ if (this.On) {
519
+ await this.pushBrightnessChanges();
552
520
  }
553
521
  }
554
522
  async pushHueSaturationChanges() {
555
- const change = 'pushHueSaturationChanges';
556
- try {
557
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${change}`);
558
- if (this.Hue !== this.accessory.context.Hue || this.Saturation !== this.accessory.context.Saturation) {
559
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${(0, super_stringify_1.default)(this.Hue)}`);
560
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${(0, super_stringify_1.default)(this.Saturation)}`);
561
- const [red, green, blue] = (0, settings_1.hs2rgb)(Number(this.Hue), Number(this.Saturation));
562
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} rgb: ${(0, super_stringify_1.default)([red, green, blue])}`);
563
- // Make Push On request to the API
564
- const body = (0, super_stringify_1.default)({
565
- 'command': 'setColor',
566
- 'parameter': `${red}:${green}:${blue}`,
567
- 'commandType': 'command',
523
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushHueSaturationChanges`);
524
+ if (this.Hue !== this.accessory.context.Hue || this.Saturation !== this.accessory.context.Saturation) {
525
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${JSON.stringify(this.Hue)}`);
526
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${JSON.stringify(this.Saturation)}`);
527
+ const [red, green, blue] = (0, settings_1.hs2rgb)(Number(this.Hue), Number(this.Saturation));
528
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} rgb: ${JSON.stringify([red, green, blue])}`);
529
+ // Make Push On request to the API
530
+ const body = JSON.stringify({
531
+ 'command': 'setColor',
532
+ 'parameter': `${red}:${green}:${blue}`,
533
+ 'commandType': 'command',
534
+ });
535
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
536
+ try {
537
+ const { body, statusCode, headers } = await (0, undici_1.request)(`${settings_1.Devices}/${this.device.deviceId}/commands`, {
538
+ method: 'POST',
539
+ headers: this.platform.generateHeaders(),
568
540
  });
569
- await this.request({ body, change });
541
+ const deviceStatus = await body.json();
542
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Devices: ${JSON.stringify(deviceStatus.body)}`);
543
+ this.statusCode(statusCode);
544
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Headers: ${JSON.stringify(headers)}`);
570
545
  }
571
- else {
572
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No ${change}. Hue: ${this.Hue}, `
573
- + `HueCached: ${this.accessory.context.Hue}, Saturation: ${this.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`);
546
+ catch (e) {
547
+ this.apiError(e);
548
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushHueSaturationChanges with ${this.device.connectionType}`
549
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
574
550
  }
575
551
  }
576
- catch (e) {
577
- this.apiError(e);
578
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed ${change} with ${this.device.connectionType}`
579
- + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
552
+ else {
553
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushHueSaturationChanges. Hue: ${this.Hue}, `
554
+ + `HueCached: ${this.accessory.context.Hue}, Saturation: ${this.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`);
580
555
  }
581
556
  }
582
557
  async pushBrightnessChanges() {
583
- const change = 'pushBrightnessChanges';
584
- try {
585
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${change}`);
586
- if (this.Brightness !== this.accessory.context.Brightness) {
587
- // Make Push On request to the API
588
- const body = (0, super_stringify_1.default)({
589
- 'command': 'setBrightness',
590
- 'parameter': `${this.Brightness}`,
591
- 'commandType': 'command',
558
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushBrightnessChanges`);
559
+ if (this.Brightness !== this.accessory.context.Brightness) {
560
+ const body = JSON.stringify({
561
+ 'command': 'setBrightness',
562
+ 'parameter': `${this.Brightness}`,
563
+ 'commandType': 'command',
564
+ });
565
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
566
+ try {
567
+ const { body, statusCode, headers } = await (0, undici_1.request)(`${settings_1.Devices}/${this.device.deviceId}/commands`, {
568
+ method: 'POST',
569
+ headers: this.platform.generateHeaders(),
592
570
  });
593
- await this.request({ body, change });
571
+ const deviceStatus = await body.json();
572
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Devices: ${JSON.stringify(deviceStatus.body)}`);
573
+ this.statusCode(statusCode);
574
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Headers: ${JSON.stringify(headers)}`);
594
575
  }
595
- else {
596
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No ${change},` + `Brightness: ${this.Brightness}, `
597
- + `BrightnessCached: ${this.accessory.context.Brightness}`);
576
+ catch (e) {
577
+ this.apiError(e);
578
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushBrightnessChanges with ${this.device.connectionType}`
579
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
598
580
  }
599
581
  }
600
- catch (e) {
601
- this.apiError(e);
602
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed ${change} with ${this.device.connectionType}`
603
- + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
582
+ else {
583
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushBrightnessChanges,` + `Brightness: ${this.Brightness}, `
584
+ + `BrightnessCached: ${this.accessory.context.Brightness}`);
604
585
  }
605
586
  }
606
- async request({ body, change }) {
607
- const t = Date.now();
608
- const nonce = 'requestID';
609
- const data = this.platform.config.credentials?.token + t + nonce;
610
- const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret)
611
- .update(Buffer.from(data, 'utf-8'))
612
- .digest();
613
- const sign = signTerm.toString('base64');
614
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
615
- const options = {
616
- hostname: settings_1.HostDomain,
617
- port: 443,
618
- path: `${settings_1.DevicePath}/${this.device.deviceId}/commands`,
619
- method: 'POST',
620
- headers: {
621
- 'Authorization': this.platform.config.credentials?.token,
622
- 'sign': sign,
623
- 'nonce': nonce,
624
- 't': t,
625
- 'Content-Type': 'application/json',
626
- 'Content-Length': body.length,
627
- },
628
- };
629
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
630
- const req = https_1.default.request(options, res => {
631
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${change} statusCode: ${res.statusCode}`);
632
- this.statusCode({ res });
633
- res.on('data', d => {
634
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
635
- });
636
- });
637
- req.on('error', (e) => {
638
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
639
- });
640
- req.write(body);
641
- req.end();
642
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${change} req: ${(0, super_stringify_1.default)(req)}`);
643
- }
644
587
  /**
645
588
  * Handle requests to set the value of the "On" characteristic
646
589
  */
@@ -793,7 +736,7 @@ class StripLight {
793
736
  });
794
737
  // Set an event handler
795
738
  switchbot.onadvertisement = (ad) => {
796
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${(0, super_stringify_1.default)(ad, null, ' ')}`);
739
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
797
740
  };
798
741
  await (0, utils_1.sleep)(10000);
799
742
  // Stop to monitor
@@ -866,38 +809,39 @@ class StripLight {
866
809
  }
867
810
  }
868
811
  }
869
- async statusCode({ res }) {
870
- switch (res.statusCode) {
812
+ async statusCode(statusCode) {
813
+ switch (statusCode) {
871
814
  case 151:
872
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this device type.`);
815
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this deviceType, statusCode: ${statusCode}`);
873
816
  break;
874
817
  case 152:
875
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found.`);
818
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found, statusCode: ${statusCode}`);
876
819
  break;
877
820
  case 160:
878
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported.`);
821
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported, statusCode: ${statusCode}`);
879
822
  break;
880
823
  case 161:
881
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline.`);
882
- await this.offlineOff();
824
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline, statusCode: ${statusCode}`);
825
+ this.offlineOff();
883
826
  break;
884
827
  case 171:
885
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} is offline. Hub: ${this.device.hubDeviceId}`);
886
- await this.offlineOff();
828
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. `
829
+ + `Hub: ${this.device.hubDeviceId}`);
830
+ this.offlineOff();
887
831
  break;
888
832
  case 190:
889
833
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
890
- ` Or command: ${(0, super_stringify_1.default)(res)} format is invalid`);
834
+ ` Or command format is invalid, statusCode: ${statusCode}`);
891
835
  break;
892
836
  case 100:
893
- if (this.platform.debugMode) {
894
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent.`);
895
- }
837
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent, statusCode: ${statusCode}`);
838
+ break;
839
+ case 200:
840
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Request successful, statusCode: ${statusCode}`);
896
841
  break;
897
842
  default:
898
- if (this.platform.debugMode) {
899
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode.`);
900
- }
843
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: `
844
+ + `${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`);
901
845
  }
902
846
  }
903
847
  async offlineOff() {
@@ -1000,7 +944,7 @@ class StripLight {
1000
944
  config['maxRetry'] = device.maxRetry;
1001
945
  }
1002
946
  if (Object.entries(config).length !== 0) {
1003
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${(0, super_stringify_1.default)(config)}`);
947
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
1004
948
  }
1005
949
  }
1006
950
  async logs(device) {