@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,14 +1,11 @@
1
- import https from 'https';
2
- import crypto from 'crypto';
3
1
  import { Context } from 'vm';
4
- import { IncomingMessage } from 'http';
2
+ import { request } from 'undici';
3
+ import { sleep } from '../utils';
5
4
  import { interval, Subject } from 'rxjs';
6
- import superStringify from 'super-stringify';
7
5
  import { SwitchBotPlatform } from '../platform';
8
6
  import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
9
7
  import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
10
- import { device, devicesConfig, serviceData, ad, deviceStatus, HostDomain, DevicePath } from '../settings';
11
- import { sleep } from '../utils';
8
+ import { device, devicesConfig, serviceData, ad, deviceStatus, Devices } from '../settings';
12
9
 
13
10
  /**
14
11
  * Platform Accessory
@@ -188,7 +185,7 @@ export class Humidifier {
188
185
  } catch (e: any) {
189
186
  this.apiError(e);
190
187
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with ${this.device.connectionType} Connection,`
191
- + ` Error Message: ${superStringify(e.message)}`);
188
+ + ` Error Message: ${JSON.stringify(e.message)}`);
192
189
  }
193
190
  this.humidifierUpdateInProgress = false;
194
191
  });
@@ -207,7 +204,7 @@ export class Humidifier {
207
204
  } else {
208
205
  await this.offlineOff();
209
206
  this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
210
- + ` ${this.device.connectionType}, parseStatus will not happen.`);
207
+ + ` ${this.device.connectionType}, parseStatus will not happen.`);
211
208
  }
212
209
  }
213
210
 
@@ -261,7 +258,7 @@ export class Humidifier {
261
258
  + ` TargetHumidifierDehumidifierState: ${this.TargetHumidifierDehumidifierState}`);
262
259
  this.debugLog(
263
260
  `${this.device.deviceType}: ${this.accessory.displayName}`
264
- + ` RelativeHumidityHumidifierThreshold: ${this.RelativeHumidityHumidifierThreshold}`,
261
+ + ` RelativeHumidityHumidifierThreshold: ${this.RelativeHumidityHumidifierThreshold}`,
265
262
  );
266
263
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
267
264
  + ` CurrentHumidifierDehumidifierState: ${this.CurrentHumidifierDehumidifierState}`);
@@ -297,7 +294,7 @@ export class Humidifier {
297
294
  } else {
298
295
  await this.offlineOff();
299
296
  this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
300
- + ` ${this.device.connectionType}, refreshStatus will not happen.`);
297
+ + ` ${this.device.connectionType}, refreshStatus will not happen.`);
301
298
  }
302
299
  }
303
300
 
@@ -323,15 +320,15 @@ export class Humidifier {
323
320
  switchbot.onadvertisement = async (ad: ad) => {
324
321
  this.address = ad.address;
325
322
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},`
326
- + ` BLE Address Found: ${this.address}`);
323
+ + ` BLE Address Found: ${this.address}`);
327
324
  this.serviceData = ad.serviceData;
328
325
  this.autoMode = ad.serviceData.autoMode;
329
326
  this.onState = ad.serviceData.onState;
330
327
  this.percentage = ad.serviceData.percentage;
331
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${superStringify(ad.serviceData)}`);
328
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
332
329
  this.debugLog(
333
330
  `${this.device.deviceType}: ${this.accessory.displayName} model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName},` +
334
- `autoMode: ${ad.serviceData.autoMode}, onState: ${ad.serviceData.onState}, percentage: ${ad.serviceData.percentage}`,
331
+ `autoMode: ${ad.serviceData.autoMode}, onState: ${ad.serviceData.onState}, percentage: ${ad.serviceData.percentage}`,
335
332
  );
336
333
 
337
334
  if (this.serviceData) {
@@ -353,7 +350,7 @@ export class Humidifier {
353
350
  .catch(async (e: any) => {
354
351
  this.apiError(e);
355
352
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}`
356
- + ` Connection, Error Message: ${superStringify(e.message)}`);
353
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
357
354
  await this.BLERefreshConnection(switchbot);
358
355
  });
359
356
  } else {
@@ -364,57 +361,29 @@ export class Humidifier {
364
361
  async openAPIRefreshStatus(): Promise<void> {
365
362
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
366
363
  try {
367
- const t = Date.now();
368
- const nonce = 'requestID';
369
- const data = this.platform.config.credentials?.token + t + nonce;
370
- const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
371
- const sign = signTerm.toString('base64');
372
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
373
- const options = {
374
- hostname: HostDomain,
375
- port: 443,
376
- path: `${DevicePath}/${this.device.deviceId}/status`,
377
- method: 'GET',
378
- headers: {
379
- Authorization: this.platform.config.credentials?.token,
380
- sign: sign,
381
- nonce: nonce,
382
- t: t,
383
- 'Content-Type': 'application/json',
384
- },
385
- };
386
- const req = https.request(options, (res) => {
387
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus statusCode: ${res.statusCode}`);
388
- let rawData = '';
389
- res.on('data', (d) => {
390
- rawData += d;
391
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
392
- });
393
- res.on('end', () => {
394
- try {
395
- this.deviceStatus = JSON.parse(rawData);
396
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus: ${superStringify(this.deviceStatus)}`);
397
- this.auto = this.deviceStatus.body.auto;
398
- this.power = this.deviceStatus.body.power;
399
- this.lackWater = this.deviceStatus.body.lackWater;
400
- this.humidity = this.deviceStatus.body.humidity;
401
- this.temperature = this.deviceStatus.body.temperature;
402
- this.nebulizationEfficiency = this.deviceStatus.body.nebulizationEfficiency;
403
- this.openAPIparseStatus();
404
- this.updateHomeKitCharacteristics();
405
- } catch (e: any) {
406
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
407
- }
408
- });
409
- });
410
- req.on('error', (e: any) => {
411
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
364
+ const { body, statusCode, headers } = await request(`${Devices}/${this.device.deviceId}/status`, {
365
+ headers: this.platform.generateHeaders(),
412
366
  });
413
- req.end();
367
+ const deviceStatus = await body.json();
368
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Devices: ${JSON.stringify(deviceStatus.body)}`);
369
+ this.statusCode(statusCode);
370
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Headers: ${JSON.stringify(headers)}`);
371
+ this.debugLog(
372
+ `${this.device.deviceType}: ${this.accessory.displayName
373
+ } refreshStatus: ${JSON.stringify(deviceStatus)}`,
374
+ );
375
+ this.auto = deviceStatus.body.auto;
376
+ this.power = deviceStatus.body.power;
377
+ this.lackWater = deviceStatus.body.lackWater;
378
+ this.humidity = deviceStatus.body.humidity;
379
+ this.temperature = deviceStatus.body.temperature;
380
+ this.nebulizationEfficiency = deviceStatus.body.nebulizationEfficiency;
381
+ this.openAPIparseStatus();
382
+ this.updateHomeKitCharacteristics();
414
383
  } catch (e: any) {
415
384
  this.apiError(e);
416
385
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
417
- + ` Connection, Error Message: ${superStringify(e.message)}`);
386
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
418
387
  }
419
388
  }
420
389
 
@@ -431,7 +400,7 @@ export class Humidifier {
431
400
  } else {
432
401
  await this.offlineOff();
433
402
  this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
434
- + ` ${this.device.connectionType}, pushChanges will not happen.`);
403
+ + ` ${this.device.connectionType}, pushChanges will not happen.`);
435
404
  }
436
405
  interval(5000)
437
406
  .pipe(take(1))
@@ -465,71 +434,44 @@ export class Humidifier {
465
434
  .catch(async (e: any) => {
466
435
  this.apiError(e);
467
436
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
468
- + ` Connection, Error Message: ${superStringify(e.message)}`);
437
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
469
438
  await this.BLEPushConnection();
470
439
  });
471
440
  }
472
441
 
473
442
  async openAPIpushChanges(): Promise<void> {
474
- try {
475
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
476
- if (this.TargetHumidifierDehumidifierState === this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER &&
477
- this.Active === this.platform.Characteristic.Active.ACTIVE) {
478
- // Make Push On request to the API
479
- const t = Date.now();
480
- const nonce = 'requestID';
481
- const data = this.platform.config.credentials?.token + t + nonce;
482
- const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret)
483
- .update(Buffer.from(data, 'utf-8'))
484
- .digest();
485
- const sign = signTerm.toString('base64');
486
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
487
- this.debugLog(`Pushing Manual: ${this.RelativeHumidityHumidifierThreshold}!`);
488
- const body = superStringify({
489
- 'command': 'setMode',
490
- 'parameter': `${this.RelativeHumidityHumidifierThreshold}`,
491
- 'commandType': 'command',
492
- });
493
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
494
- const options = {
495
- hostname: HostDomain,
496
- port: 443,
497
- path: `${DevicePath}/${this.device.deviceId}/commands`,
443
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
444
+ if (this.TargetHumidifierDehumidifierState === this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER &&
445
+ this.Active === this.platform.Characteristic.Active.ACTIVE) {
446
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Pushing Manual: ${this.RelativeHumidityHumidifierThreshold}!`);
447
+ const body = JSON.stringify({
448
+ 'command': 'setMode',
449
+ 'parameter': `${this.RelativeHumidityHumidifierThreshold}`,
450
+ 'commandType': 'command',
451
+ });
452
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
453
+ try {
454
+ const { body, statusCode, headers } = await request(`${Devices}/${this.device.deviceId}/commands`, {
498
455
  method: 'POST',
499
- headers: {
500
- 'Authorization': this.platform.config.credentials?.token,
501
- 'sign': sign,
502
- 'nonce': nonce,
503
- 't': t,
504
- 'Content-Type': 'application/json',
505
- 'Content-Length': body.length,
506
- },
507
- };
508
- const req = https.request(options, res => {
509
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges statusCode: ${res.statusCode}`);
510
- res.on('data', d => {
511
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
512
- });
513
- });
514
- req.on('error', (e: any) => {
515
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
516
- this.apiError(e);
456
+ headers: this.platform.generateHeaders(),
517
457
  });
518
- req.write(body);
519
- req.end();
520
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges: ${superStringify(req)}`);
521
- } else if (
522
- this.TargetHumidifierDehumidifierState === this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER_OR_DEHUMIDIFIER &&
523
- this.Active === this.platform.Characteristic.Active.ACTIVE
524
- ) {
525
- await this.pushAutoChanges();
526
- } else {
527
- await this.pushActiveChanges();
458
+ const deviceStatus = await body.json();
459
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Devices: ${JSON.stringify(deviceStatus.body)}`);
460
+ this.statusCode(statusCode);
461
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Headers: ${JSON.stringify(headers)}`);
462
+ } catch (e: any) {
463
+ this.apiError(e);
464
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
465
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`,
466
+ );
528
467
  }
529
- } catch (e: any) {
530
- this.apiError(e);
531
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
532
- + ` Connection, Error Message: ${superStringify(e.message)}`);
468
+ } else if (
469
+ this.TargetHumidifierDehumidifierState === this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER_OR_DEHUMIDIFIER &&
470
+ this.Active === this.platform.Characteristic.Active.ACTIVE
471
+ ) {
472
+ await this.pushAutoChanges();
473
+ } else {
474
+ await this.pushActiveChanges();
533
475
  }
534
476
  }
535
477
 
@@ -537,63 +479,34 @@ export class Humidifier {
537
479
  * Pushes the requested changes to the SwitchBot API
538
480
  */
539
481
  async pushAutoChanges(): Promise<void> {
540
- try {
541
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushAutoChanges`);
542
- if (
543
- this.TargetHumidifierDehumidifierState === this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER_OR_DEHUMIDIFIER &&
544
- this.Active === this.platform.Characteristic.Active.ACTIVE
545
- ) {
546
- // Make Push On request to the API
547
- const t = Date.now();
548
- const nonce = 'requestID';
549
- const data = this.platform.config.credentials?.token + t + nonce;
550
- const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret)
551
- .update(Buffer.from(data, 'utf-8'))
552
- .digest();
553
- const sign = signTerm.toString('base64');
554
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
555
- this.debugLog('Pushing Auto!');
556
- const body = superStringify({
557
- 'command': 'setMode',
558
- 'parameter': 'auto',
559
- 'commandType': 'command',
560
- });
561
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
562
- const options = {
563
- hostname: HostDomain,
564
- port: 443,
565
- path: `${DevicePath}/${this.device.deviceId}/commands`,
482
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushAutoChanges`);
483
+ if (this.TargetHumidifierDehumidifierState === this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER_OR_DEHUMIDIFIER &&
484
+ this.Active === this.platform.Characteristic.Active.ACTIVE) {
485
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Pushing Auto`);
486
+ const body = JSON.stringify({
487
+ 'command': 'setMode',
488
+ 'parameter': 'auto',
489
+ 'commandType': 'command',
490
+ });
491
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
492
+ try {
493
+ const { body, statusCode, headers } = await request(`${Devices}/${this.device.deviceId}/commands`, {
566
494
  method: 'POST',
567
- headers: {
568
- 'Authorization': this.platform.config.credentials?.token,
569
- 'sign': sign,
570
- 'nonce': nonce,
571
- 't': t,
572
- 'Content-Type': 'application/json',
573
- 'Content-Length': body.length,
574
- },
575
- };
576
- const req = https.request(options, res => {
577
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
578
- this.statusCode({ res });
579
- res.on('data', d => {
580
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
581
- });
495
+ headers: this.platform.generateHeaders(),
582
496
  });
583
- req.on('error', (e: any) => {
584
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
585
- });
586
- req.write(body);
587
- req.end();
588
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushAutoChanges: ${superStringify(req)}`);
589
- } else {
590
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushAutoChanges.` +
591
- `TargetHumidifierDehumidifierState: ${this.TargetHumidifierDehumidifierState}, Active: ${this.Active}`);
497
+ const deviceStatus = await body.json();
498
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Devices: ${JSON.stringify(deviceStatus.body)}`);
499
+ this.statusCode(statusCode);
500
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Headers: ${JSON.stringify(headers)}`);
501
+ } catch (e: any) {
502
+ this.apiError(e);
503
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushAutoChanges with ${this.device.connectionType}`
504
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`,
505
+ );
592
506
  }
593
- } catch (e: any) {
594
- this.apiError(e);
595
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushAutoChanges with ${this.device.connectionType}`
596
- + ` Connection, Error Message: ${superStringify(e.message)}`);
507
+ } else {
508
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushAutoChanges.` +
509
+ `TargetHumidifierDehumidifierState: ${this.TargetHumidifierDehumidifierState}, Active: ${this.Active}`);
597
510
  }
598
511
  }
599
512
 
@@ -601,58 +514,32 @@ export class Humidifier {
601
514
  * Pushes the requested changes to the SwitchBot API
602
515
  */
603
516
  async pushActiveChanges(): Promise<void> {
604
- try {
605
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushActiveChanges`);
606
- if (this.Active === this.platform.Characteristic.Active.INACTIVE) {
607
- // Make Push On request to the API
608
- const t = Date.now();
609
- const nonce = 'requestID';
610
- const data = this.platform.config.credentials?.token + t + nonce;
611
- const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret)
612
- .update(Buffer.from(data, 'utf-8'))
613
- .digest();
614
- const sign = signTerm.toString('base64');
615
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
616
- this.debugLog('Pushing Off!');
617
- const body = superStringify({
618
- 'command': 'turnOff',
619
- 'parameter': 'default',
620
- 'commandType': 'command',
621
- });
622
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
623
- const options = {
624
- hostname: HostDomain,
625
- port: 443,
626
- path: `${DevicePath}/${this.device.deviceId}/commands`,
517
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushActiveChanges`);
518
+ if (this.Active === this.platform.Characteristic.Active.INACTIVE) {
519
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Pushing Off`);
520
+ const body = JSON.stringify({
521
+ 'command': 'turnOff',
522
+ 'parameter': 'default',
523
+ 'commandType': 'command',
524
+ });
525
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
526
+ try {
527
+ const { body, statusCode, headers } = await request(`${Devices}/${this.device.deviceId}/commands`, {
627
528
  method: 'POST',
628
- headers: {
629
- 'Authorization': this.platform.config.credentials?.token,
630
- 'sign': sign,
631
- 'nonce': nonce,
632
- 't': t,
633
- 'Content-Type': 'application/json',
634
- 'Content-Length': body.length,
635
- },
636
- };
637
- const req = https.request(options, res => {
638
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushActiveChanges statusCode: ${res.statusCode}`);
639
- this.statusCode({ res });
640
- res.on('data', d => {
641
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
642
- });
643
- });
644
- req.on('error', (e: any) => {
645
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
529
+ headers: this.platform.generateHeaders(),
646
530
  });
647
- req.write(body);
648
- req.end();
649
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushActiveChanges: ${superStringify(req)}`);
531
+ const deviceStatus = await body.json();
532
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Devices: ${JSON.stringify(deviceStatus.body)}`);
533
+ this.statusCode(statusCode);
534
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Headers: ${JSON.stringify(headers)}`);
535
+ } catch (e: any) {
536
+ this.apiError(e);
537
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushActiveChanges with ${this.device.connectionType}`
538
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`,
539
+ );
650
540
  }
651
- } catch (e: any) {
652
- this.apiError(e);
653
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushActiveChanges with ${this.device.connectionType}`
654
- + ` Connection, Error Message: ${superStringify(e.message)}`,
655
- );
541
+ } else {
542
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushActiveChanges. Active: ${this.Active}`);
656
543
  }
657
544
  }
658
545
 
@@ -711,7 +598,7 @@ export class Humidifier {
711
598
  } else {
712
599
  this.humidifierService.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.CurrentRelativeHumidity);
713
600
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
714
- + ` updateCharacteristic CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
601
+ + ` updateCharacteristic CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
715
602
  this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidity;
716
603
  }
717
604
  if (this.OpenAPI) {
@@ -725,22 +612,22 @@ export class Humidifier {
725
612
  }
726
613
  if (this.CurrentHumidifierDehumidifierState === undefined) {
727
614
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
728
- + ` CurrentHumidifierDehumidifierState: ${this.CurrentHumidifierDehumidifierState}`);
615
+ + ` CurrentHumidifierDehumidifierState: ${this.CurrentHumidifierDehumidifierState}`);
729
616
  } else {
730
617
  this.humidifierService.updateCharacteristic(this.platform.Characteristic.CurrentHumidifierDehumidifierState,
731
618
  this.CurrentHumidifierDehumidifierState);
732
619
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}` +
733
- ` updateCharacteristic CurrentHumidifierDehumidifierState: ${this.CurrentHumidifierDehumidifierState}`);
620
+ ` updateCharacteristic CurrentHumidifierDehumidifierState: ${this.CurrentHumidifierDehumidifierState}`);
734
621
  this.accessory.context.CurrentHumidifierDehumidifierState = this.CurrentHumidifierDehumidifierState;
735
622
  }
736
623
  if (this.TargetHumidifierDehumidifierState === undefined) {
737
624
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
738
- +` TargetHumidifierDehumidifierState: ${this.TargetHumidifierDehumidifierState}`);
625
+ + ` TargetHumidifierDehumidifierState: ${this.TargetHumidifierDehumidifierState}`);
739
626
  } else {
740
627
  this.humidifierService.updateCharacteristic(this.platform.Characteristic.TargetHumidifierDehumidifierState,
741
628
  this.TargetHumidifierDehumidifierState);
742
629
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}` +
743
- ` updateCharacteristic TargetHumidifierDehumidifierState: ${this.TargetHumidifierDehumidifierState}`);
630
+ ` updateCharacteristic TargetHumidifierDehumidifierState: ${this.TargetHumidifierDehumidifierState}`);
744
631
  this.accessory.context.TargetHumidifierDehumidifierState = this.TargetHumidifierDehumidifierState;
745
632
  }
746
633
  if (this.Active === undefined) {
@@ -757,7 +644,7 @@ export class Humidifier {
757
644
  this.humidifierService.updateCharacteristic(this.platform.Characteristic.RelativeHumidityHumidifierThreshold,
758
645
  this.RelativeHumidityHumidifierThreshold);
759
646
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}` +
760
- ` updateCharacteristic RelativeHumidityHumidifierThreshold: ${this.RelativeHumidityHumidifierThreshold}`);
647
+ ` updateCharacteristic RelativeHumidityHumidifierThreshold: ${this.RelativeHumidityHumidifierThreshold}`);
761
648
  this.accessory.context.RelativeHumidityHumidifierThreshold = this.RelativeHumidityHumidifierThreshold;
762
649
  }
763
650
  if (!this.device.humidifier?.hide_temperature && !this.BLE) {
@@ -790,7 +677,7 @@ export class Humidifier {
790
677
  });
791
678
  // Set an event handler
792
679
  switchbot.onadvertisement = (ad: any) => {
793
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${superStringify(ad, null, ' ')}`);
680
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
794
681
  };
795
682
  await sleep(10000);
796
683
  // Stop to monitor
@@ -837,36 +724,41 @@ export class Humidifier {
837
724
  }
838
725
  }
839
726
 
840
- async statusCode({ res }: { res: IncomingMessage }): Promise<void> {
841
- switch (res.statusCode) {
727
+ async statusCode(statusCode: number): Promise<void> {
728
+ switch (statusCode) {
842
729
  case 151:
843
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this device type.`);
730
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this deviceType, statusCode: ${statusCode}`);
844
731
  break;
845
732
  case 152:
846
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found.`);
733
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found, statusCode: ${statusCode}`);
847
734
  break;
848
735
  case 160:
849
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported.`);
736
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported, statusCode: ${statusCode}`);
850
737
  break;
851
738
  case 161:
852
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline.`);
853
- await this.offlineOff();
739
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline, statusCode: ${statusCode}`);
740
+ this.offlineOff();
854
741
  break;
855
742
  case 171:
856
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
857
- await this.offlineOff();
743
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. `
744
+ + `Hub: ${this.device.hubDeviceId}`);
745
+ this.offlineOff();
858
746
  break;
859
747
  case 190:
860
748
  this.errorLog(
861
749
  `${this.device.deviceType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
862
- ` Or command: ${superStringify(res)} format is invalid`,
750
+ ` Or command format is invalid, statusCode: ${statusCode}`,
863
751
  );
864
752
  break;
865
753
  case 100:
866
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent.`);
754
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent, statusCode: ${statusCode}`);
755
+ break;
756
+ case 200:
757
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Request successful, statusCode: ${statusCode}`);
867
758
  break;
868
759
  default:
869
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode.`);
760
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: `
761
+ + `${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`);
870
762
  }
871
763
  }
872
764
 
@@ -898,7 +790,7 @@ export class Humidifier {
898
790
  FirmwareRevision(accessory: PlatformAccessory<Context>, device: device & devicesConfig): CharacteristicValue {
899
791
  let FirmwareRevision: string;
900
792
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
901
- + ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
793
+ + ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
902
794
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
903
795
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
904
796
  if (accessory.context.FirmwareRevision) {
@@ -987,7 +879,7 @@ export class Humidifier {
987
879
  config['offline'] = device.offline;
988
880
  }
989
881
  if (Object.entries(config).length !== 0) {
990
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${superStringify(config)}`);
882
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
991
883
  }
992
884
  }
993
885