@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.Humidifier = 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
@@ -132,7 +127,7 @@ class Humidifier {
132
127
  catch (e) {
133
128
  this.apiError(e);
134
129
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with ${this.device.connectionType} Connection,`
135
- + ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
130
+ + ` Error Message: ${JSON.stringify(e.message)}`);
136
131
  }
137
132
  this.humidifierUpdateInProgress = false;
138
133
  });
@@ -275,7 +270,7 @@ class Humidifier {
275
270
  this.autoMode = ad.serviceData.autoMode;
276
271
  this.onState = ad.serviceData.onState;
277
272
  this.percentage = ad.serviceData.percentage;
278
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${(0, super_stringify_1.default)(ad.serviceData)}`);
273
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
279
274
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName},` +
280
275
  `autoMode: ${ad.serviceData.autoMode}, onState: ${ad.serviceData.onState}, percentage: ${ad.serviceData.percentage}`);
281
276
  if (this.serviceData) {
@@ -298,7 +293,7 @@ class Humidifier {
298
293
  .catch(async (e) => {
299
294
  this.apiError(e);
300
295
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}`
301
- + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
296
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
302
297
  await this.BLERefreshConnection(switchbot);
303
298
  });
304
299
  }
@@ -309,59 +304,27 @@ class Humidifier {
309
304
  async openAPIRefreshStatus() {
310
305
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
311
306
  try {
312
- const t = Date.now();
313
- const nonce = 'requestID';
314
- const data = this.platform.config.credentials?.token + t + nonce;
315
- const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
316
- const sign = signTerm.toString('base64');
317
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
318
- const options = {
319
- hostname: settings_1.HostDomain,
320
- port: 443,
321
- path: `${settings_1.DevicePath}/${this.device.deviceId}/status`,
322
- method: 'GET',
323
- headers: {
324
- Authorization: this.platform.config.credentials?.token,
325
- sign: sign,
326
- nonce: nonce,
327
- t: t,
328
- 'Content-Type': 'application/json',
329
- },
330
- };
331
- const req = https_1.default.request(options, (res) => {
332
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus statusCode: ${res.statusCode}`);
333
- let rawData = '';
334
- res.on('data', (d) => {
335
- rawData += d;
336
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
337
- });
338
- res.on('end', () => {
339
- try {
340
- this.deviceStatus = JSON.parse(rawData);
341
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus: ${(0, super_stringify_1.default)(this.deviceStatus)}`);
342
- this.auto = this.deviceStatus.body.auto;
343
- this.power = this.deviceStatus.body.power;
344
- this.lackWater = this.deviceStatus.body.lackWater;
345
- this.humidity = this.deviceStatus.body.humidity;
346
- this.temperature = this.deviceStatus.body.temperature;
347
- this.nebulizationEfficiency = this.deviceStatus.body.nebulizationEfficiency;
348
- this.openAPIparseStatus();
349
- this.updateHomeKitCharacteristics();
350
- }
351
- catch (e) {
352
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
353
- }
354
- });
307
+ const { body, statusCode, headers } = await (0, undici_1.request)(`${settings_1.Devices}/${this.device.deviceId}/status`, {
308
+ headers: this.platform.generateHeaders(),
355
309
  });
356
- req.on('error', (e) => {
357
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
358
- });
359
- req.end();
310
+ const deviceStatus = await body.json();
311
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Devices: ${JSON.stringify(deviceStatus.body)}`);
312
+ this.statusCode(statusCode);
313
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Headers: ${JSON.stringify(headers)}`);
314
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(deviceStatus)}`);
315
+ this.auto = deviceStatus.body.auto;
316
+ this.power = deviceStatus.body.power;
317
+ this.lackWater = deviceStatus.body.lackWater;
318
+ this.humidity = deviceStatus.body.humidity;
319
+ this.temperature = deviceStatus.body.temperature;
320
+ this.nebulizationEfficiency = deviceStatus.body.nebulizationEfficiency;
321
+ this.openAPIparseStatus();
322
+ this.updateHomeKitCharacteristics();
360
323
  }
361
324
  catch (e) {
362
325
  this.apiError(e);
363
326
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
364
- + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
327
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
365
328
  }
366
329
  }
367
330
  /**
@@ -412,192 +375,111 @@ class Humidifier {
412
375
  .catch(async (e) => {
413
376
  this.apiError(e);
414
377
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
415
- + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
378
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
416
379
  await this.BLEPushConnection();
417
380
  });
418
381
  }
419
382
  async openAPIpushChanges() {
420
- try {
421
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
422
- if (this.TargetHumidifierDehumidifierState === this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER &&
423
- this.Active === this.platform.Characteristic.Active.ACTIVE) {
424
- // Make Push On request to the API
425
- const t = Date.now();
426
- const nonce = 'requestID';
427
- const data = this.platform.config.credentials?.token + t + nonce;
428
- const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret)
429
- .update(Buffer.from(data, 'utf-8'))
430
- .digest();
431
- const sign = signTerm.toString('base64');
432
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
433
- this.debugLog(`Pushing Manual: ${this.RelativeHumidityHumidifierThreshold}!`);
434
- const body = (0, super_stringify_1.default)({
435
- 'command': 'setMode',
436
- 'parameter': `${this.RelativeHumidityHumidifierThreshold}`,
437
- 'commandType': 'command',
438
- });
439
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
440
- const options = {
441
- hostname: settings_1.HostDomain,
442
- port: 443,
443
- path: `${settings_1.DevicePath}/${this.device.deviceId}/commands`,
383
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
384
+ if (this.TargetHumidifierDehumidifierState === this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER &&
385
+ this.Active === this.platform.Characteristic.Active.ACTIVE) {
386
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Pushing Manual: ${this.RelativeHumidityHumidifierThreshold}!`);
387
+ const body = JSON.stringify({
388
+ 'command': 'setMode',
389
+ 'parameter': `${this.RelativeHumidityHumidifierThreshold}`,
390
+ 'commandType': 'command',
391
+ });
392
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
393
+ try {
394
+ const { body, statusCode, headers } = await (0, undici_1.request)(`${settings_1.Devices}/${this.device.deviceId}/commands`, {
444
395
  method: 'POST',
445
- headers: {
446
- 'Authorization': this.platform.config.credentials?.token,
447
- 'sign': sign,
448
- 'nonce': nonce,
449
- 't': t,
450
- 'Content-Type': 'application/json',
451
- 'Content-Length': body.length,
452
- },
453
- };
454
- const req = https_1.default.request(options, res => {
455
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges statusCode: ${res.statusCode}`);
456
- res.on('data', d => {
457
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
458
- });
396
+ headers: this.platform.generateHeaders(),
459
397
  });
460
- req.on('error', (e) => {
461
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
462
- this.apiError(e);
463
- });
464
- req.write(body);
465
- req.end();
466
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges: ${(0, super_stringify_1.default)(req)}`);
467
- }
468
- else if (this.TargetHumidifierDehumidifierState === this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER_OR_DEHUMIDIFIER &&
469
- this.Active === this.platform.Characteristic.Active.ACTIVE) {
470
- await this.pushAutoChanges();
398
+ const deviceStatus = await body.json();
399
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Devices: ${JSON.stringify(deviceStatus.body)}`);
400
+ this.statusCode(statusCode);
401
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Headers: ${JSON.stringify(headers)}`);
471
402
  }
472
- else {
473
- await this.pushActiveChanges();
403
+ catch (e) {
404
+ this.apiError(e);
405
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
406
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
474
407
  }
475
408
  }
476
- catch (e) {
477
- this.apiError(e);
478
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
479
- + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
409
+ else if (this.TargetHumidifierDehumidifierState === this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER_OR_DEHUMIDIFIER &&
410
+ this.Active === this.platform.Characteristic.Active.ACTIVE) {
411
+ await this.pushAutoChanges();
412
+ }
413
+ else {
414
+ await this.pushActiveChanges();
480
415
  }
481
416
  }
482
417
  /**
483
418
  * Pushes the requested changes to the SwitchBot API
484
419
  */
485
420
  async pushAutoChanges() {
486
- try {
487
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushAutoChanges`);
488
- if (this.TargetHumidifierDehumidifierState === this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER_OR_DEHUMIDIFIER &&
489
- this.Active === this.platform.Characteristic.Active.ACTIVE) {
490
- // Make Push On request to the API
491
- const t = Date.now();
492
- const nonce = 'requestID';
493
- const data = this.platform.config.credentials?.token + t + nonce;
494
- const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret)
495
- .update(Buffer.from(data, 'utf-8'))
496
- .digest();
497
- const sign = signTerm.toString('base64');
498
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
499
- this.debugLog('Pushing Auto!');
500
- const body = (0, super_stringify_1.default)({
501
- 'command': 'setMode',
502
- 'parameter': 'auto',
503
- 'commandType': 'command',
504
- });
505
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
506
- const options = {
507
- hostname: settings_1.HostDomain,
508
- port: 443,
509
- path: `${settings_1.DevicePath}/${this.device.deviceId}/commands`,
421
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushAutoChanges`);
422
+ if (this.TargetHumidifierDehumidifierState === this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER_OR_DEHUMIDIFIER &&
423
+ this.Active === this.platform.Characteristic.Active.ACTIVE) {
424
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Pushing Auto`);
425
+ const body = JSON.stringify({
426
+ 'command': 'setMode',
427
+ 'parameter': 'auto',
428
+ 'commandType': 'command',
429
+ });
430
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
431
+ try {
432
+ const { body, statusCode, headers } = await (0, undici_1.request)(`${settings_1.Devices}/${this.device.deviceId}/commands`, {
510
433
  method: 'POST',
511
- headers: {
512
- 'Authorization': this.platform.config.credentials?.token,
513
- 'sign': sign,
514
- 'nonce': nonce,
515
- 't': t,
516
- 'Content-Type': 'application/json',
517
- 'Content-Length': body.length,
518
- },
519
- };
520
- const req = https_1.default.request(options, res => {
521
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
522
- this.statusCode({ res });
523
- res.on('data', d => {
524
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
525
- });
526
- });
527
- req.on('error', (e) => {
528
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
434
+ headers: this.platform.generateHeaders(),
529
435
  });
530
- req.write(body);
531
- req.end();
532
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushAutoChanges: ${(0, super_stringify_1.default)(req)}`);
436
+ const deviceStatus = await body.json();
437
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Devices: ${JSON.stringify(deviceStatus.body)}`);
438
+ this.statusCode(statusCode);
439
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Headers: ${JSON.stringify(headers)}`);
533
440
  }
534
- else {
535
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushAutoChanges.` +
536
- `TargetHumidifierDehumidifierState: ${this.TargetHumidifierDehumidifierState}, Active: ${this.Active}`);
441
+ catch (e) {
442
+ this.apiError(e);
443
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushAutoChanges with ${this.device.connectionType}`
444
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
537
445
  }
538
446
  }
539
- catch (e) {
540
- this.apiError(e);
541
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushAutoChanges with ${this.device.connectionType}`
542
- + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
447
+ else {
448
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushAutoChanges.` +
449
+ `TargetHumidifierDehumidifierState: ${this.TargetHumidifierDehumidifierState}, Active: ${this.Active}`);
543
450
  }
544
451
  }
545
452
  /**
546
453
  * Pushes the requested changes to the SwitchBot API
547
454
  */
548
455
  async pushActiveChanges() {
549
- try {
550
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushActiveChanges`);
551
- if (this.Active === this.platform.Characteristic.Active.INACTIVE) {
552
- // Make Push On request to the API
553
- const t = Date.now();
554
- const nonce = 'requestID';
555
- const data = this.platform.config.credentials?.token + t + nonce;
556
- const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret)
557
- .update(Buffer.from(data, 'utf-8'))
558
- .digest();
559
- const sign = signTerm.toString('base64');
560
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
561
- this.debugLog('Pushing Off!');
562
- const body = (0, super_stringify_1.default)({
563
- 'command': 'turnOff',
564
- 'parameter': 'default',
565
- 'commandType': 'command',
566
- });
567
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
568
- const options = {
569
- hostname: settings_1.HostDomain,
570
- port: 443,
571
- path: `${settings_1.DevicePath}/${this.device.deviceId}/commands`,
456
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushActiveChanges`);
457
+ if (this.Active === this.platform.Characteristic.Active.INACTIVE) {
458
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Pushing Off`);
459
+ const body = JSON.stringify({
460
+ 'command': 'turnOff',
461
+ 'parameter': 'default',
462
+ 'commandType': 'command',
463
+ });
464
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
465
+ try {
466
+ const { body, statusCode, headers } = await (0, undici_1.request)(`${settings_1.Devices}/${this.device.deviceId}/commands`, {
572
467
  method: 'POST',
573
- headers: {
574
- 'Authorization': this.platform.config.credentials?.token,
575
- 'sign': sign,
576
- 'nonce': nonce,
577
- 't': t,
578
- 'Content-Type': 'application/json',
579
- 'Content-Length': body.length,
580
- },
581
- };
582
- const req = https_1.default.request(options, res => {
583
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushActiveChanges statusCode: ${res.statusCode}`);
584
- this.statusCode({ res });
585
- res.on('data', d => {
586
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
587
- });
468
+ headers: this.platform.generateHeaders(),
588
469
  });
589
- req.on('error', (e) => {
590
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
591
- });
592
- req.write(body);
593
- req.end();
594
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushActiveChanges: ${(0, super_stringify_1.default)(req)}`);
470
+ const deviceStatus = await body.json();
471
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Devices: ${JSON.stringify(deviceStatus.body)}`);
472
+ this.statusCode(statusCode);
473
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Headers: ${JSON.stringify(headers)}`);
474
+ }
475
+ catch (e) {
476
+ this.apiError(e);
477
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushActiveChanges with ${this.device.connectionType}`
478
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
595
479
  }
596
480
  }
597
- catch (e) {
598
- this.apiError(e);
599
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushActiveChanges with ${this.device.connectionType}`
600
- + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
481
+ else {
482
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushActiveChanges. Active: ${this.Active}`);
601
483
  }
602
484
  }
603
485
  /**
@@ -734,7 +616,7 @@ class Humidifier {
734
616
  });
735
617
  // Set an event handler
736
618
  switchbot.onadvertisement = (ad) => {
737
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${(0, super_stringify_1.default)(ad, null, ' ')}`);
619
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
738
620
  };
739
621
  await (0, utils_1.sleep)(10000);
740
622
  // Stop to monitor
@@ -778,34 +660,39 @@ class Humidifier {
778
660
  }
779
661
  }
780
662
  }
781
- async statusCode({ res }) {
782
- switch (res.statusCode) {
663
+ async statusCode(statusCode) {
664
+ switch (statusCode) {
783
665
  case 151:
784
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this device type.`);
666
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this deviceType, statusCode: ${statusCode}`);
785
667
  break;
786
668
  case 152:
787
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found.`);
669
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found, statusCode: ${statusCode}`);
788
670
  break;
789
671
  case 160:
790
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported.`);
672
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported, statusCode: ${statusCode}`);
791
673
  break;
792
674
  case 161:
793
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline.`);
794
- await this.offlineOff();
675
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline, statusCode: ${statusCode}`);
676
+ this.offlineOff();
795
677
  break;
796
678
  case 171:
797
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
798
- await this.offlineOff();
679
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. `
680
+ + `Hub: ${this.device.hubDeviceId}`);
681
+ this.offlineOff();
799
682
  break;
800
683
  case 190:
801
684
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
802
- ` Or command: ${(0, super_stringify_1.default)(res)} format is invalid`);
685
+ ` Or command format is invalid, statusCode: ${statusCode}`);
803
686
  break;
804
687
  case 100:
805
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent.`);
688
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent, statusCode: ${statusCode}`);
689
+ break;
690
+ case 200:
691
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Request successful, statusCode: ${statusCode}`);
806
692
  break;
807
693
  default:
808
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode.`);
694
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: `
695
+ + `${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`);
809
696
  }
810
697
  }
811
698
  async offlineOff() {
@@ -932,7 +819,7 @@ class Humidifier {
932
819
  config['offline'] = device.offline;
933
820
  }
934
821
  if (Object.entries(config).length !== 0) {
935
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${(0, super_stringify_1.default)(config)}`);
822
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
936
823
  }
937
824
  }
938
825
  async logs(device) {