@switchbot/homebridge-switchbot 2.6.0-beta.3 → 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 (139) hide show
  1. package/.github/workflows/dependabot.yml +2 -0
  2. package/.vscode/tasks.json +13 -0
  3. package/dist/device/blindtilt.d.ts +1 -5
  4. package/dist/device/blindtilt.d.ts.map +1 -1
  5. package/dist/device/blindtilt.js +80 -140
  6. package/dist/device/blindtilt.js.map +1 -1
  7. package/dist/device/bot.d.ts +1 -5
  8. package/dist/device/bot.d.ts.map +1 -1
  9. package/dist/device/bot.js +79 -138
  10. package/dist/device/bot.js.map +1 -1
  11. package/dist/device/ceilinglight.d.ts +2 -6
  12. package/dist/device/ceilinglight.d.ts.map +1 -1
  13. package/dist/device/ceilinglight.js +157 -304
  14. package/dist/device/ceilinglight.js.map +1 -1
  15. package/dist/device/colorbulb.d.ts +2 -6
  16. package/dist/device/colorbulb.d.ts.map +1 -1
  17. package/dist/device/colorbulb.js +164 -311
  18. package/dist/device/colorbulb.js.map +1 -1
  19. package/dist/device/contact.d.ts +1 -5
  20. package/dist/device/contact.d.ts.map +1 -1
  21. package/dist/device/contact.js +36 -72
  22. package/dist/device/contact.js.map +1 -1
  23. package/dist/device/curtain.d.ts +1 -5
  24. package/dist/device/curtain.d.ts.map +1 -1
  25. package/dist/device/curtain.js +78 -138
  26. package/dist/device/curtain.js.map +1 -1
  27. package/dist/device/hub.d.ts +2 -6
  28. package/dist/device/hub.d.ts.map +1 -1
  29. package/dist/device/hub.js +30 -63
  30. package/dist/device/hub.js.map +1 -1
  31. package/dist/device/humidifier.d.ts +1 -5
  32. package/dist/device/humidifier.d.ts.map +1 -1
  33. package/dist/device/humidifier.js +115 -228
  34. package/dist/device/humidifier.js.map +1 -1
  35. package/dist/device/iosensor.d.ts +1 -5
  36. package/dist/device/iosensor.d.ts.map +1 -1
  37. package/dist/device/iosensor.js +38 -74
  38. package/dist/device/iosensor.js.map +1 -1
  39. package/dist/device/lock.d.ts +1 -5
  40. package/dist/device/lock.d.ts.map +1 -1
  41. package/dist/device/lock.js +66 -126
  42. package/dist/device/lock.js.map +1 -1
  43. package/dist/device/meter.d.ts +1 -5
  44. package/dist/device/meter.d.ts.map +1 -1
  45. package/dist/device/meter.js +38 -74
  46. package/dist/device/meter.js.map +1 -1
  47. package/dist/device/meterplus.d.ts +1 -5
  48. package/dist/device/meterplus.d.ts.map +1 -1
  49. package/dist/device/meterplus.js +39 -75
  50. package/dist/device/meterplus.js.map +1 -1
  51. package/dist/device/motion.d.ts +1 -5
  52. package/dist/device/motion.d.ts.map +1 -1
  53. package/dist/device/motion.js +35 -72
  54. package/dist/device/motion.js.map +1 -1
  55. package/dist/device/plug.d.ts +1 -5
  56. package/dist/device/plug.d.ts.map +1 -1
  57. package/dist/device/plug.js +65 -126
  58. package/dist/device/plug.js.map +1 -1
  59. package/dist/device/robotvacuumcleaner.d.ts +1 -5
  60. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  61. package/dist/device/robotvacuumcleaner.js +79 -151
  62. package/dist/device/robotvacuumcleaner.js.map +1 -1
  63. package/dist/device/striplight.d.ts +1 -9
  64. package/dist/device/striplight.d.ts.map +1 -1
  65. package/dist/device/striplight.js +137 -193
  66. package/dist/device/striplight.js.map +1 -1
  67. package/dist/irdevice/airconditioner.d.ts +3 -7
  68. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  69. package/dist/irdevice/airconditioner.js +34 -62
  70. package/dist/irdevice/airconditioner.js.map +1 -1
  71. package/dist/irdevice/airpurifier.d.ts +3 -7
  72. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  73. package/dist/irdevice/airpurifier.js +31 -59
  74. package/dist/irdevice/airpurifier.js.map +1 -1
  75. package/dist/irdevice/camera.d.ts +1 -5
  76. package/dist/irdevice/camera.d.ts.map +1 -1
  77. package/dist/irdevice/camera.js +30 -57
  78. package/dist/irdevice/camera.js.map +1 -1
  79. package/dist/irdevice/fan.d.ts +2 -6
  80. package/dist/irdevice/fan.d.ts.map +1 -1
  81. package/dist/irdevice/fan.js +39 -66
  82. package/dist/irdevice/fan.js.map +1 -1
  83. package/dist/irdevice/light.d.ts +1 -5
  84. package/dist/irdevice/light.d.ts.map +1 -1
  85. package/dist/irdevice/light.js +32 -59
  86. package/dist/irdevice/light.js.map +1 -1
  87. package/dist/irdevice/other.d.ts +1 -5
  88. package/dist/irdevice/other.d.ts.map +1 -1
  89. package/dist/irdevice/other.js +30 -57
  90. package/dist/irdevice/other.js.map +1 -1
  91. package/dist/irdevice/tv.d.ts +3 -7
  92. package/dist/irdevice/tv.d.ts.map +1 -1
  93. package/dist/irdevice/tv.js +46 -76
  94. package/dist/irdevice/tv.js.map +1 -1
  95. package/dist/irdevice/vacuumcleaner.d.ts +2 -6
  96. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  97. package/dist/irdevice/vacuumcleaner.js +30 -58
  98. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  99. package/dist/irdevice/waterheater.d.ts +7 -23
  100. package/dist/irdevice/waterheater.d.ts.map +1 -1
  101. package/dist/irdevice/waterheater.js +41 -74
  102. package/dist/irdevice/waterheater.js.map +1 -1
  103. package/dist/platform.d.ts +14 -5
  104. package/dist/platform.d.ts.map +1 -1
  105. package/dist/platform.js +153 -146
  106. package/dist/platform.js.map +1 -1
  107. package/dist/settings.d.ts +4 -0
  108. package/dist/settings.d.ts.map +1 -1
  109. package/dist/settings.js +5 -1
  110. package/dist/settings.js.map +1 -1
  111. package/nodemon.json +1 -1
  112. package/package.json +10 -10
  113. package/src/device/blindtilt.ts +122 -181
  114. package/src/device/bot.ts +96 -149
  115. package/src/device/ceilinglight.ts +172 -315
  116. package/src/device/colorbulb.ts +178 -324
  117. package/src/device/contact.ts +48 -78
  118. package/src/device/curtain.ts +93 -148
  119. package/src/device/hub.ts +34 -63
  120. package/src/device/humidifier.ts +135 -243
  121. package/src/device/iosensor.ts +49 -79
  122. package/src/device/lock.ts +78 -134
  123. package/src/device/meter.ts +48 -78
  124. package/src/device/meterplus.ts +50 -80
  125. package/src/device/motion.ts +46 -77
  126. package/src/device/plug.ts +76 -134
  127. package/src/device/robotvacuumcleaner.ts +91 -159
  128. package/src/device/striplight.ts +148 -200
  129. package/src/irdevice/airconditioner.ts +56 -81
  130. package/src/irdevice/airpurifier.ts +41 -67
  131. package/src/irdevice/camera.ts +35 -60
  132. package/src/irdevice/fan.ts +46 -71
  133. package/src/irdevice/light.ts +38 -63
  134. package/src/irdevice/other.ts +35 -60
  135. package/src/irdevice/tv.ts +52 -80
  136. package/src/irdevice/vacuumcleaner.ts +36 -62
  137. package/src/irdevice/waterheater.ts +46 -76
  138. package/src/platform.ts +143 -157
  139. 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.CeilingLight = 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 CeilingLight {
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.ceilingLightUpdateInProgress = false;
152
147
  });
@@ -209,14 +204,14 @@ class CeilingLight {
209
204
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.Brightness}`);
210
205
  // Color, Hue & Brightness
211
206
  if (this.color) {
212
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} color: ${(0, super_stringify_1.default)(this.color)}`);
207
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} color: ${JSON.stringify(this.color)}`);
213
208
  const [red, green, blue] = this.color.split(':');
214
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} red: ${(0, super_stringify_1.default)(red)}`);
215
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} green: ${(0, super_stringify_1.default)(green)}`);
216
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} blue: ${(0, super_stringify_1.default)(blue)}`);
209
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} red: ${JSON.stringify(red)}`);
210
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} green: ${JSON.stringify(green)}`);
211
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} blue: ${JSON.stringify(blue)}`);
217
212
  const [hue, saturation] = (0, settings_1.rgb2hs)(Number(red), Number(green), Number(blue));
218
213
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
219
- + ` hs: ${(0, super_stringify_1.default)((0, settings_1.rgb2hs)(Number(red), Number(green), Number(blue)))}`);
214
+ + ` hs: ${JSON.stringify((0, settings_1.rgb2hs)(Number(red), Number(green), Number(blue)))}`);
220
215
  // Hue
221
216
  this.Hue = hue;
222
217
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.Hue}`);
@@ -274,7 +269,7 @@ class CeilingLight {
274
269
  this.address = ad.address;
275
270
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},`
276
271
  + ` BLE Address Found: ${this.address}`);
277
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${(0, super_stringify_1.default)(ad.serviceData)}`);
272
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
278
273
  this.serviceData = ad.serviceData;
279
274
  //this.state = ad.serviceData.state;
280
275
  //this.delay = ad.serviceData.delay;
@@ -283,7 +278,7 @@ class CeilingLight {
283
278
  //this.wifiRssi = ad.serviceData.wifiRssi;
284
279
  //this.overload = ad.serviceData.overload;
285
280
  //this.currentPower = ad.serviceData.currentPower;
286
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${(0, super_stringify_1.default)(ad.serviceData)}`);
281
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
287
282
  /*this.debugLog(
288
283
  `${this.device.deviceType}: ${this.accessory.displayName} state: ${ad.serviceData.state}, ` +
289
284
  `delay: ${ad.serviceData.delay}, timer: ${ad.serviceData.timer}, syncUtcTime: ${ad.serviceData.syncUtcTime} ` +
@@ -309,7 +304,7 @@ class CeilingLight {
309
304
  .catch(async (e) => {
310
305
  this.apiError(e);
311
306
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}`
312
- + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
307
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
313
308
  await this.BLERefreshConnection(switchbot);
314
309
  });
315
310
  }
@@ -320,58 +315,25 @@ class CeilingLight {
320
315
  async openAPIRefreshStatus() {
321
316
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
322
317
  try {
323
- const t = Date.now();
324
- const nonce = 'requestID';
325
- const data = this.platform.config.credentials?.token + t + nonce;
326
- const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
327
- const sign = signTerm.toString('base64');
328
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
329
- const options = {
330
- hostname: settings_1.HostDomain,
331
- port: 443,
332
- path: `${settings_1.DevicePath}/${this.device.deviceId}/status`,
333
- method: 'GET',
334
- headers: {
335
- Authorization: this.platform.config.credentials?.token,
336
- sign: sign,
337
- nonce: nonce,
338
- t: t,
339
- 'Content-Type': 'application/json',
340
- },
341
- };
342
- const req = https_1.default.request(options, (res) => {
343
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus statusCode: ${res.statusCode}`);
344
- this.statusCode({ res });
345
- let rawData = '';
346
- res.on('data', (d) => {
347
- rawData += d;
348
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
349
- });
350
- res.on('end', () => {
351
- try {
352
- this.deviceStatus = JSON.parse(rawData);
353
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus: ${(0, super_stringify_1.default)(this.deviceStatus)}`);
354
- this.power = this.deviceStatus.body.power;
355
- this.color = this.deviceStatus.body.color;
356
- this.brightness = this.deviceStatus.body.brightness;
357
- this.colorTemperature = this.deviceStatus.body.colorTemperature;
358
- this.openAPIparseStatus();
359
- this.updateHomeKitCharacteristics();
360
- }
361
- catch (e) {
362
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
363
- }
364
- });
365
- });
366
- req.on('error', (e) => {
367
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
318
+ const { body, statusCode, headers } = await (0, undici_1.request)(`${settings_1.Devices}/${this.device.deviceId}/status`, {
319
+ headers: this.platform.generateHeaders(),
368
320
  });
369
- req.end();
321
+ const deviceStatus = await body.json();
322
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Devices: ${JSON.stringify(deviceStatus.body)}`);
323
+ this.statusCode(statusCode);
324
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Headers: ${JSON.stringify(headers)}`);
325
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(deviceStatus)}`);
326
+ this.power = deviceStatus.body.power;
327
+ this.color = deviceStatus.body.color;
328
+ this.brightness = deviceStatus.body.brightness;
329
+ this.colorTemperature = deviceStatus.body.colorTemperature;
330
+ this.openAPIparseStatus();
331
+ this.updateHomeKitCharacteristics();
370
332
  }
371
333
  catch (e) {
372
334
  this.apiError(e);
373
335
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
374
- + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
336
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
375
337
  }
376
338
  }
377
339
  /**
@@ -444,7 +406,7 @@ class CeilingLight {
444
406
  .catch(async (e) => {
445
407
  this.apiError(e);
446
408
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
447
- + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
409
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
448
410
  await this.BLEPushConnection();
449
411
  });
450
412
  }
@@ -454,258 +416,148 @@ class CeilingLight {
454
416
  }
455
417
  }
456
418
  async openAPIpushChanges() {
457
- try {
458
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
459
- if (this.On !== this.accessory.context.On) {
460
- // Make Push On request to the API
461
- const t = Date.now();
462
- const nonce = 'requestID';
463
- const data = this.platform.config.credentials?.token + t + nonce;
464
- const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret)
465
- .update(Buffer.from(data, 'utf-8'))
466
- .digest();
467
- const sign = signTerm.toString('base64');
468
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
469
- let command = '';
470
- if (this.On) {
471
- command = 'turnOn';
472
- }
473
- else {
474
- command = 'turnOff';
475
- }
476
- const body = (0, super_stringify_1.default)({
477
- 'command': `${command}`,
478
- 'parameter': 'default',
479
- 'commandType': 'command',
480
- });
481
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
482
- const options = {
483
- hostname: settings_1.HostDomain,
484
- port: 443,
485
- path: `${settings_1.DevicePath}/${this.device.deviceId}/commands`,
486
- method: 'POST',
487
- headers: {
488
- 'Authorization': this.platform.config.credentials?.token,
489
- 'sign': sign,
490
- 'nonce': nonce,
491
- 't': t,
492
- 'Content-Type': 'application/json',
493
- 'Content-Length': body.length,
494
- },
495
- };
496
- const req = https_1.default.request(options, res => {
497
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges statusCode: ${res.statusCode}`);
498
- this.statusCode({ res });
499
- res.on('data', d => {
500
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
501
- });
502
- });
503
- req.on('error', (e) => {
504
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
505
- });
506
- req.write(body);
507
- req.end();
508
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushChanges: ${(0, super_stringify_1.default)(req)}`);
419
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} OpenAPI pushChanges`);
420
+ if (this.On !== this.accessory.context.On) {
421
+ let command = '';
422
+ if (this.On) {
423
+ command = 'turnOn';
509
424
  }
510
425
  else {
511
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.` + `On: ${this.On}, `
512
- + `OnCached: ${this.accessory.context.On}`);
513
- }
514
- // Push Brightness Update
515
- if (this.On) {
516
- await this.pushBrightnessChanges();
426
+ command = 'turnOff';
517
427
  }
518
- // Push ColorTemperature Update
519
- if (this.On) {
520
- await this.pushColorTemperatureChanges();
428
+ const body = JSON.stringify({
429
+ 'command': `${command}`,
430
+ 'parameter': 'default',
431
+ 'commandType': 'command',
432
+ });
433
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
434
+ try {
435
+ const { body, statusCode, headers } = await (0, undici_1.request)(`${settings_1.Devices}/${this.device.deviceId}/commands`, {
436
+ method: 'POST',
437
+ headers: this.platform.generateHeaders(),
438
+ });
439
+ const deviceStatus = await body.json();
440
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Devices: ${JSON.stringify(deviceStatus.body)}`);
441
+ this.statusCode(statusCode);
442
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Headers: ${JSON.stringify(headers)}`);
521
443
  }
522
- // Push Hue & Saturation Update
523
- if (this.On) {
524
- await this.pushHueSaturationChanges();
444
+ catch (e) {
445
+ this.apiError(e);
446
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
447
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
525
448
  }
526
449
  }
527
- catch (e) {
528
- this.apiError(e);
529
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
530
- + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
450
+ else {
451
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.` + `On: ${this.On}, `
452
+ + `OnCached: ${this.accessory.context.On}`);
453
+ }
454
+ // Push Hue & Saturation Update
455
+ if (this.On) {
456
+ await this.pushHueSaturationChanges();
457
+ }
458
+ // Push ColorTemperature Update
459
+ if (this.On) {
460
+ await this.pushColorTemperatureChanges();
461
+ }
462
+ // Push Brightness Update
463
+ if (this.On) {
464
+ await this.pushBrightnessChanges();
531
465
  }
532
466
  }
533
467
  async pushHueSaturationChanges() {
534
- try {
535
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushHueSaturationChanges`);
536
- if (this.Hue !== this.accessory.context.Hue || this.Saturation !== this.accessory.context.Saturation) {
537
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${(0, super_stringify_1.default)(this.Hue)}`);
538
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${(0, super_stringify_1.default)(this.Saturation)}`);
539
- const [red, green, blue] = (0, settings_1.hs2rgb)(Number(this.Hue), Number(this.Saturation));
540
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} rgb: ${(0, super_stringify_1.default)([red, green, blue])}`);
541
- // Make Push Hue/Saturation request to the API
542
- const t = Date.now();
543
- const nonce = 'requestID';
544
- const data = this.platform.config.credentials?.token + t + nonce;
545
- const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret)
546
- .update(Buffer.from(data, 'utf-8'))
547
- .digest();
548
- const sign = signTerm.toString('base64');
549
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
550
- const body = (0, super_stringify_1.default)({
551
- 'command': 'setColor',
552
- 'parameter': `${red}:${green}:${blue}`,
553
- 'commandType': 'command',
554
- });
555
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
556
- const options = {
557
- hostname: settings_1.HostDomain,
558
- port: 443,
559
- path: `${settings_1.DevicePath}/${this.device.deviceId}/commands`,
468
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushHueSaturationChanges`);
469
+ if (this.Hue !== this.accessory.context.Hue || this.Saturation !== this.accessory.context.Saturation) {
470
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${JSON.stringify(this.Hue)}`);
471
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${JSON.stringify(this.Saturation)}`);
472
+ const [red, green, blue] = (0, settings_1.hs2rgb)(Number(this.Hue), Number(this.Saturation));
473
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} rgb: ${JSON.stringify([red, green, blue])}`);
474
+ const body = JSON.stringify({
475
+ 'command': 'setColor',
476
+ 'parameter': `${red}:${green}:${blue}`,
477
+ 'commandType': 'command',
478
+ });
479
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
480
+ try {
481
+ const { body, statusCode, headers } = await (0, undici_1.request)(`${settings_1.Devices}/${this.device.deviceId}/commands`, {
560
482
  method: 'POST',
561
- headers: {
562
- 'Authorization': this.platform.config.credentials?.token,
563
- 'sign': sign,
564
- 'nonce': nonce,
565
- 't': t,
566
- 'Content-Type': 'application/json',
567
- 'Content-Length': body.length,
568
- },
569
- };
570
- const req = https_1.default.request(options, res => {
571
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushHueSaturationChanges statusCode: ${res.statusCode}`);
572
- res.on('data', d => {
573
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
574
- });
575
- });
576
- req.on('error', (e) => {
577
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
483
+ headers: this.platform.generateHeaders(),
578
484
  });
579
- req.write(body);
580
- req.end();
581
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushHueSaturationChanges: ${(0, super_stringify_1.default)(req)}`);
485
+ const deviceStatus = await body.json();
486
+ this.debugLog(`Devices: ${JSON.stringify(deviceStatus.body)}`);
487
+ this.statusCode(statusCode);
488
+ this.debugLog(`Headers: ${JSON.stringify(headers)}`);
582
489
  }
583
- else {
584
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushHueSaturationChanges. Hue: ${this.Hue}, `
585
- + `HueCached: ${this.accessory.context.Hue}, Saturation: ${this.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`);
490
+ catch (e) {
491
+ this.apiError(e);
492
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushHueSaturationChanges with ${this.device.connectionType}`
493
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
586
494
  }
587
495
  }
588
- catch (e) {
589
- this.apiError(e);
590
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushHueSaturationChanges with ${this.device.connectionType}`
591
- + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
496
+ else {
497
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushHueSaturationChanges. Hue: ${this.Hue}, `
498
+ + `HueCached: ${this.accessory.context.Hue}, Saturation: ${this.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`);
592
499
  }
593
500
  }
594
501
  async pushColorTemperatureChanges() {
595
- try {
596
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushColorTemperatureChanges`);
597
- if (this.ColorTemperature !== this.accessory.context.ColorTemperature) {
598
- const kelvin = Math.round(1000000 / Number(this.ColorTemperature));
599
- this.cacheKelvin = kelvin;
600
- // Make Push On request to the API
601
- const t = Date.now();
602
- const nonce = 'requestID';
603
- const data = this.platform.config.credentials?.token + t + nonce;
604
- const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret)
605
- .update(Buffer.from(data, 'utf-8'))
606
- .digest();
607
- const sign = signTerm.toString('base64');
608
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
609
- const body = (0, super_stringify_1.default)({
610
- 'command': 'setColorTemperature',
611
- 'parameter': `${kelvin}`,
612
- 'commandType': 'command',
613
- });
614
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
615
- const options = {
616
- hostname: settings_1.HostDomain,
617
- port: 443,
618
- path: `${settings_1.DevicePath}/${this.device.deviceId}/commands`,
502
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushColorTemperatureChanges`);
503
+ if (this.ColorTemperature !== this.accessory.context.ColorTemperature) {
504
+ const kelvin = Math.round(1000000 / Number(this.ColorTemperature));
505
+ this.cacheKelvin = kelvin;
506
+ const body = JSON.stringify({
507
+ 'command': 'setColorTemperature',
508
+ 'parameter': `${kelvin}`,
509
+ 'commandType': 'command',
510
+ });
511
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
512
+ try {
513
+ const { body, statusCode, headers } = await (0, undici_1.request)(`${settings_1.Devices}/${this.device.deviceId}/commands`, {
619
514
  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
- const req = https_1.default.request(options, res => {
630
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
631
- res.on('data', d => {
632
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
633
- });
634
- });
635
- req.on('error', (e) => {
636
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
515
+ headers: this.platform.generateHeaders(),
637
516
  });
638
- req.write(body);
639
- req.end();
640
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushColorTemperatureChanges: ${(0, super_stringify_1.default)(req)}`);
641
- this.accessory.context.ColorTemperature = this.ColorTemperature;
517
+ const deviceStatus = await body.json();
518
+ this.debugLog(`Devices: ${JSON.stringify(deviceStatus.body)}`);
519
+ this.statusCode(statusCode);
520
+ this.debugLog(`Headers: ${JSON.stringify(headers)}`);
642
521
  }
643
- else {
644
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushColorTemperatureChanges.` +
645
- `ColorTemperature: ${this.ColorTemperature}, ColorTemperatureCached: ${this.accessory.context.ColorTemperature}`);
522
+ catch (e) {
523
+ this.apiError(e);
524
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushColorTemperatureChanges with ${this.device.connectionType}`
525
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
646
526
  }
647
527
  }
648
- catch (e) {
649
- this.apiError(e);
650
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushColorTemperatureChanges with ${this.device.connectionType}`
651
- + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
528
+ else {
529
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushColorTemperatureChanges.` +
530
+ `ColorTemperature: ${this.ColorTemperature}, ColorTemperatureCached: ${this.accessory.context.ColorTemperature}`);
652
531
  }
653
532
  }
654
533
  async pushBrightnessChanges() {
655
- try {
656
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushBrightnessChanges`);
657
- if (this.Brightness !== this.accessory.context.Brightness) {
658
- // Make Push On request to the API
659
- const t = Date.now();
660
- const nonce = 'requestID';
661
- const data = this.platform.config.credentials?.token + t + nonce;
662
- const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret)
663
- .update(Buffer.from(data, 'utf-8'))
664
- .digest();
665
- const sign = signTerm.toString('base64');
666
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
667
- const body = (0, super_stringify_1.default)({
668
- 'command': 'setBrightness',
669
- 'parameter': `${this.Brightness}`,
670
- 'commandType': 'command',
671
- });
672
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
673
- const options = {
674
- hostname: settings_1.HostDomain,
675
- port: 443,
676
- path: `${settings_1.DevicePath}/${this.device.deviceId}/commands`,
534
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushBrightnessChanges`);
535
+ if (this.Brightness !== this.accessory.context.Brightness) {
536
+ const body = JSON.stringify({
537
+ 'command': 'setBrightness',
538
+ 'parameter': `${this.Brightness}`,
539
+ 'commandType': 'command',
540
+ });
541
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
542
+ try {
543
+ const { body, statusCode, headers } = await (0, undici_1.request)(`${settings_1.Devices}/${this.device.deviceId}/commands`, {
677
544
  method: 'POST',
678
- headers: {
679
- 'Authorization': this.platform.config.credentials?.token,
680
- 'sign': sign,
681
- 'nonce': nonce,
682
- 't': t,
683
- 'Content-Type': 'application/json',
684
- 'Content-Length': body.length,
685
- },
686
- };
687
- const req = https_1.default.request(options, res => {
688
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushBrightnessChanges statusCode: ${res.statusCode}`);
689
- res.on('data', d => {
690
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
691
- });
692
- });
693
- req.on('error', (e) => {
694
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
545
+ headers: this.platform.generateHeaders(),
695
546
  });
696
- req.write(body);
697
- req.end();
698
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushBrightnessChanges: ${(0, super_stringify_1.default)(req)}`);
547
+ const deviceStatus = await body.json();
548
+ this.debugLog(`Devices: ${JSON.stringify(deviceStatus.body)}`);
549
+ this.statusCode(statusCode);
550
+ this.debugLog(`Headers: ${JSON.stringify(headers)}`);
699
551
  }
700
- else {
701
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushBrightnessChanges.` + `Brightness: ${this.Brightness}, `
702
- + `BrightnessCached: ${this.accessory.context.Brightness}`);
552
+ catch (e) {
553
+ this.apiError(e);
554
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushBrightnessChanges with ${this.device.connectionType}`
555
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
703
556
  }
704
557
  }
705
- catch (e) {
706
- this.apiError(e);
707
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushBrightnessChanges with ${this.device.connectionType}`
708
- + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
558
+ else {
559
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushBrightnessChanges.` + `Brightness: ${this.Brightness}, `
560
+ + `BrightnessCached: ${this.accessory.context.Brightness}`);
709
561
  }
710
562
  }
711
563
  /**
@@ -870,7 +722,7 @@ class CeilingLight {
870
722
  });
871
723
  // Set an event handler
872
724
  switchbot.onadvertisement = (ad) => {
873
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${(0, super_stringify_1.default)(ad, null, ' ')}`);
725
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
874
726
  };
875
727
  await (0, utils_1.sleep)(10000);
876
728
  // Stop to monitor
@@ -933,38 +785,39 @@ class CeilingLight {
933
785
  }
934
786
  }
935
787
  }
936
- async statusCode({ res }) {
937
- switch (res.statusCode) {
788
+ async statusCode(statusCode) {
789
+ switch (statusCode) {
938
790
  case 151:
939
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this device type.`);
791
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this deviceType, statusCode: ${statusCode}`);
940
792
  break;
941
793
  case 152:
942
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found.`);
794
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found, statusCode: ${statusCode}`);
943
795
  break;
944
796
  case 160:
945
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported.`);
797
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported, statusCode: ${statusCode}`);
946
798
  break;
947
799
  case 161:
948
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline.`);
949
- await this.offlineOff();
800
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline, statusCode: ${statusCode}`);
801
+ this.offlineOff();
950
802
  break;
951
803
  case 171:
952
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} is offline. Hub: ${this.device.hubDeviceId}`);
953
- await this.offlineOff();
804
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. `
805
+ + `Hub: ${this.device.hubDeviceId}`);
806
+ this.offlineOff();
954
807
  break;
955
808
  case 190:
956
809
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
957
- ` Or command: ${(0, super_stringify_1.default)(res)} format is invalid`);
810
+ ` Or command format is invalid, statusCode: ${statusCode}`);
958
811
  break;
959
812
  case 100:
960
- if (this.platform.debugMode) {
961
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent.`);
962
- }
813
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent, statusCode: ${statusCode}`);
814
+ break;
815
+ case 200:
816
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Request successful, statusCode: ${statusCode}`);
963
817
  break;
964
818
  default:
965
- if (this.platform.debugMode) {
966
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode.`);
967
- }
819
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: `
820
+ + `${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`);
968
821
  }
969
822
  }
970
823
  async offlineOff() {
@@ -1068,7 +921,7 @@ class CeilingLight {
1068
921
  config['maxRetry'] = device.maxRetry;
1069
922
  }
1070
923
  if (Object.entries(config).length !== 0) {
1071
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${(0, super_stringify_1.default)(config)}`);
924
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
1072
925
  }
1073
926
  }
1074
927
  async logs(device) {