@switchbot/homebridge-switchbot 2.8.0-beta.0 → 2.8.0-beta.10

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/.prettierrc +1 -1
  2. package/.vscode/settings.json +1 -1
  3. package/CHANGELOG.md +10 -0
  4. package/config.schema.json +8 -0
  5. package/dist/device/blindtilt.d.ts +1 -0
  6. package/dist/device/blindtilt.d.ts.map +1 -1
  7. package/dist/device/blindtilt.js +58 -65
  8. package/dist/device/blindtilt.js.map +1 -1
  9. package/dist/device/bot.d.ts +1 -0
  10. package/dist/device/bot.d.ts.map +1 -1
  11. package/dist/device/bot.js +52 -55
  12. package/dist/device/bot.js.map +1 -1
  13. package/dist/device/ceilinglight.d.ts +1 -0
  14. package/dist/device/ceilinglight.d.ts.map +1 -1
  15. package/dist/device/ceilinglight.js +68 -68
  16. package/dist/device/ceilinglight.js.map +1 -1
  17. package/dist/device/colorbulb.d.ts +1 -0
  18. package/dist/device/colorbulb.d.ts.map +1 -1
  19. package/dist/device/colorbulb.js +76 -75
  20. package/dist/device/colorbulb.js.map +1 -1
  21. package/dist/device/contact.d.ts +2 -0
  22. package/dist/device/contact.d.ts.map +1 -1
  23. package/dist/device/contact.js +67 -59
  24. package/dist/device/contact.js.map +1 -1
  25. package/dist/device/curtain.d.ts +2 -0
  26. package/dist/device/curtain.d.ts.map +1 -1
  27. package/dist/device/curtain.js +81 -94
  28. package/dist/device/curtain.js.map +1 -1
  29. package/dist/device/hub.d.ts +9 -1
  30. package/dist/device/hub.d.ts.map +1 -1
  31. package/dist/device/hub.js +197 -30
  32. package/dist/device/hub.js.map +1 -1
  33. package/dist/device/humidifier.d.ts.map +1 -1
  34. package/dist/device/humidifier.js +58 -62
  35. package/dist/device/humidifier.js.map +1 -1
  36. package/dist/device/iosensor.d.ts +1 -0
  37. package/dist/device/iosensor.d.ts.map +1 -1
  38. package/dist/device/iosensor.js +28 -26
  39. package/dist/device/iosensor.js.map +1 -1
  40. package/dist/device/lightstrip.d.ts +1 -0
  41. package/dist/device/lightstrip.d.ts.map +1 -1
  42. package/dist/device/lightstrip.js +68 -67
  43. package/dist/device/lightstrip.js.map +1 -1
  44. package/dist/device/lock.d.ts +2 -0
  45. package/dist/device/lock.d.ts.map +1 -1
  46. package/dist/device/lock.js +51 -47
  47. package/dist/device/lock.js.map +1 -1
  48. package/dist/device/meter.d.ts +5 -3
  49. package/dist/device/meter.d.ts.map +1 -1
  50. package/dist/device/meter.js +75 -74
  51. package/dist/device/meter.js.map +1 -1
  52. package/dist/device/meterplus.d.ts +4 -3
  53. package/dist/device/meterplus.d.ts.map +1 -1
  54. package/dist/device/meterplus.js +74 -74
  55. package/dist/device/meterplus.js.map +1 -1
  56. package/dist/device/motion.d.ts +2 -0
  57. package/dist/device/motion.d.ts.map +1 -1
  58. package/dist/device/motion.js +66 -54
  59. package/dist/device/motion.js.map +1 -1
  60. package/dist/device/plug.d.ts +1 -0
  61. package/dist/device/plug.d.ts.map +1 -1
  62. package/dist/device/plug.js +39 -39
  63. package/dist/device/plug.js.map +1 -1
  64. package/dist/device/robotvacuumcleaner.d.ts +1 -0
  65. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  66. package/dist/device/robotvacuumcleaner.js +74 -53
  67. package/dist/device/robotvacuumcleaner.js.map +1 -1
  68. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  69. package/dist/irdevice/airconditioner.js +64 -66
  70. package/dist/irdevice/airconditioner.js.map +1 -1
  71. package/dist/irdevice/airpurifier.d.ts +1 -1
  72. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  73. package/dist/irdevice/airpurifier.js +32 -34
  74. package/dist/irdevice/airpurifier.js.map +1 -1
  75. package/dist/irdevice/camera.d.ts +1 -1
  76. package/dist/irdevice/camera.d.ts.map +1 -1
  77. package/dist/irdevice/camera.js +20 -23
  78. package/dist/irdevice/camera.js.map +1 -1
  79. package/dist/irdevice/fan.d.ts +2 -2
  80. package/dist/irdevice/fan.d.ts.map +1 -1
  81. package/dist/irdevice/fan.js +30 -32
  82. package/dist/irdevice/fan.js.map +1 -1
  83. package/dist/irdevice/light.d.ts +1 -1
  84. package/dist/irdevice/light.d.ts.map +1 -1
  85. package/dist/irdevice/light.js +26 -29
  86. package/dist/irdevice/light.js.map +1 -1
  87. package/dist/irdevice/other.d.ts +1 -1
  88. package/dist/irdevice/other.d.ts.map +1 -1
  89. package/dist/irdevice/other.js +22 -23
  90. package/dist/irdevice/other.js.map +1 -1
  91. package/dist/irdevice/tv.d.ts +2 -2
  92. package/dist/irdevice/tv.d.ts.map +1 -1
  93. package/dist/irdevice/tv.js +47 -50
  94. package/dist/irdevice/tv.js.map +1 -1
  95. package/dist/irdevice/vacuumcleaner.d.ts +1 -1
  96. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  97. package/dist/irdevice/vacuumcleaner.js +20 -23
  98. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  99. package/dist/irdevice/waterheater.d.ts +1 -1
  100. package/dist/irdevice/waterheater.d.ts.map +1 -1
  101. package/dist/irdevice/waterheater.js +22 -23
  102. package/dist/irdevice/waterheater.js.map +1 -1
  103. package/dist/platform.d.ts.map +1 -1
  104. package/dist/platform.js +46 -44
  105. package/dist/platform.js.map +1 -1
  106. package/dist/settings.d.ts +1 -0
  107. package/dist/settings.d.ts.map +1 -1
  108. package/dist/settings.js.map +1 -1
  109. package/dist/utils.js +1 -1
  110. package/dist/utils.js.map +1 -1
  111. package/package.json +3 -3
  112. package/src/device/blindtilt.ts +114 -78
  113. package/src/device/bot.ts +102 -63
  114. package/src/device/ceilinglight.ts +113 -73
  115. package/src/device/colorbulb.ts +144 -87
  116. package/src/device/contact.ts +98 -63
  117. package/src/device/curtain.ts +160 -134
  118. package/src/device/hub.ts +228 -39
  119. package/src/device/humidifier.ts +136 -85
  120. package/src/device/iosensor.ts +52 -32
  121. package/src/device/lightstrip.ts +118 -72
  122. package/src/device/lock.ts +85 -50
  123. package/src/device/meter.ts +104 -80
  124. package/src/device/meterplus.ts +103 -80
  125. package/src/device/motion.ts +95 -59
  126. package/src/device/plug.ts +70 -44
  127. package/src/device/robotvacuumcleaner.ts +107 -56
  128. package/src/irdevice/airconditioner.ts +116 -86
  129. package/src/irdevice/airpurifier.ts +60 -39
  130. package/src/irdevice/camera.ts +40 -28
  131. package/src/irdevice/fan.ts +52 -38
  132. package/src/irdevice/light.ts +48 -34
  133. package/src/irdevice/other.ts +44 -28
  134. package/src/irdevice/tv.ts +70 -56
  135. package/src/irdevice/vacuumcleaner.ts +40 -28
  136. package/src/irdevice/waterheater.ts +48 -32
  137. package/src/platform.ts +87 -56
  138. package/src/settings.ts +41 -40
  139. package/src/utils.ts +1 -1
@@ -1,3 +1,4 @@
1
+ /* eslint-disable brace-style */
1
2
  import { Context } from 'vm';
2
3
  import { request } from 'undici';
3
4
  import { sleep } from '../utils';
@@ -18,6 +19,8 @@ export class Lock {
18
19
  LockTargetState!: CharacteristicValue;
19
20
 
20
21
  // OpenAPI Others
22
+ Version: deviceStatus['version'];
23
+ Battery: deviceStatus['battery'];
21
24
  doorState!: deviceStatus['doorState'];
22
25
  lockState!: deviceStatus['lockState'];
23
26
  deviceStatus!: any; //deviceStatusResponse;
@@ -38,8 +41,8 @@ export class Lock {
38
41
  doLockUpdate!: Subject<void>;
39
42
 
40
43
  // Connection
41
- private readonly BLE = (this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI');
42
- private readonly OpenAPI = (this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI');
44
+ private readonly BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
45
+ private readonly OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
43
46
  battery: any;
44
47
  calibration: any;
45
48
  status: any;
@@ -50,13 +53,17 @@ export class Lock {
50
53
  unlocked_alarm: any;
51
54
  auto_lock_paused: any;
52
55
 
53
- constructor(private readonly platform: SwitchBotPlatform, private accessory: PlatformAccessory, public device: device & devicesConfig) {
56
+ constructor(
57
+ private readonly platform: SwitchBotPlatform,
58
+ private accessory: PlatformAccessory,
59
+ public device: device & devicesConfig,
60
+ ) {
54
61
  // default placeholders
55
62
  this.logs(device);
56
63
  this.scan(device);
57
64
  this.refreshRate(device);
58
- this.config(device);
59
65
  this.context();
66
+ this.config(device);
60
67
 
61
68
  // this is subject we use to track when we need to POST changes to the SwitchBot API
62
69
  this.doLockUpdate = new Subject();
@@ -140,8 +147,10 @@ export class Lock {
140
147
  await this.pushChanges();
141
148
  } catch (e: any) {
142
149
  this.apiError(e);
143
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with ${this.device.connectionType} Connection,`
144
- + ` Error Message: ${JSON.stringify(e.message)}`);
150
+ this.errorLog(
151
+ `${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with ${this.device.connectionType} Connection,` +
152
+ ` Error Message: ${JSON.stringify(e.message)}`,
153
+ );
145
154
  }
146
155
  this.lockUpdateInProgress = false;
147
156
  });
@@ -153,14 +162,15 @@ export class Lock {
153
162
  async parseStatus(): Promise<void> {
154
163
  if (!this.device.enableCloudService && this.OpenAPI) {
155
164
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} parseStatus enableCloudService: ${this.device.enableCloudService}`);
156
- } else/*if (this.BLE) {
165
+ } /*if (this.BLE) {
157
166
  await this.BLEparseStatus();
158
- } else*/ if (this.OpenAPI && this.platform.config.credentials?.token) {
167
+ } else*/ else if (this.OpenAPI && this.platform.config.credentials?.token) {
159
168
  await this.openAPIparseStatus();
160
169
  } else {
161
170
  await this.offlineOff();
162
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
163
- + ` ${this.device.connectionType}, parseStatus will not happen.`);
171
+ this.debugWarnLog(
172
+ `${this.device.deviceType}: ${this.accessory.displayName} Connection Type:` + ` ${this.device.connectionType}, parseStatus will not happen.`,
173
+ );
164
174
  }
165
175
  }
166
176
 
@@ -218,8 +228,10 @@ export class Lock {
218
228
  await this.openAPIRefreshStatus();
219
229
  } else {
220
230
  await this.offlineOff();
221
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
222
- + ` ${this.device.connectionType}, refreshStatus will not happen.`);
231
+ this.debugWarnLog(
232
+ `${this.device.deviceType}: ${this.accessory.displayName} Connection Type:` +
233
+ ` ${this.device.connectionType}, refreshStatus will not happen.`,
234
+ );
223
235
  }
224
236
  }
225
237
 
@@ -244,8 +256,10 @@ export class Lock {
244
256
  // Set an event hander
245
257
  switchbot.onadvertisement = async (ad: any) => {
246
258
  this.address = ad.address;
247
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},`
248
- + ` BLE Address Found: ${this.address}`);
259
+ this.debugLog(
260
+ `${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},` +
261
+ ` BLE Address Found: ${this.address}`,
262
+ );
249
263
  this.serviceData = ad.serviceData;
250
264
  this.battery = ad.serviceData.battery;
251
265
  this.calibration = ad.serviceData.calibration;
@@ -260,7 +274,8 @@ export class Lock {
260
274
  this.debugLog(
261
275
  `${this.device.deviceType}: ${this.accessory.displayName} battery: ${ad.serviceData.battery}, ` +
262
276
  `calibration: ${ad.serviceData.calibration}, status: ${ad.serviceData.status}, battery: ${ad.serviceData.battery}, ` +
263
- `door_open: ${ad.serviceData.door_open}`);
277
+ `door_open: ${ad.serviceData.door_open}`,
278
+ );
264
279
 
265
280
  if (this.serviceData) {
266
281
  this.connected = true;
@@ -280,8 +295,10 @@ export class Lock {
280
295
  })
281
296
  .catch(async (e: any) => {
282
297
  this.apiError(e);
283
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}`
284
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
298
+ this.errorLog(
299
+ `${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}` +
300
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`,
301
+ );
285
302
  await this.BLERefreshConnection(switchbot);
286
303
  });
287
304
  } else {
@@ -299,18 +316,19 @@ export class Lock {
299
316
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Devices: ${JSON.stringify(deviceStatus.body)}`);
300
317
  this.statusCode(statusCode);
301
318
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Headers: ${JSON.stringify(headers)}`);
302
- this.debugLog(
303
- `${this.device.deviceType}: ${this.accessory.displayName
304
- } refreshStatus: ${JSON.stringify(deviceStatus)}`,
305
- );
319
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(deviceStatus)}`);
306
320
  this.lockState = deviceStatus.body.lockState;
307
321
  this.doorState = deviceStatus.body.doorState;
322
+ this.Version = deviceStatus.body.version;
323
+ this.Battery = deviceStatus.body.battery;
308
324
  this.openAPIparseStatus();
309
325
  this.updateHomeKitCharacteristics();
310
326
  } catch (e: any) {
311
327
  this.apiError(e);
312
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
313
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
328
+ this.errorLog(
329
+ `${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}` +
330
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`,
331
+ );
314
332
  }
315
333
  }
316
334
 
@@ -323,14 +341,15 @@ export class Lock {
323
341
  async pushChanges(): Promise<void> {
324
342
  if (!this.device.enableCloudService && this.OpenAPI) {
325
343
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} pushChanges enableCloudService: ${this.device.enableCloudService}`);
326
- } else /*if (this.BLE) {
344
+ } /*if (this.BLE) {
327
345
  await this.BLEpushChanges();
328
- } else*/ if (this.OpenAPI && this.platform.config.credentials?.token) {
346
+ } else*/ else if (this.OpenAPI && this.platform.config.credentials?.token) {
329
347
  await this.openAPIpushChanges();
330
348
  } else {
331
349
  await this.offlineOff();
332
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
333
- + ` ${this.device.connectionType}, pushChanges will not happen.`);
350
+ this.debugWarnLog(
351
+ `${this.device.deviceType}: ${this.accessory.displayName} Connection Type:` + ` ${this.device.connectionType}, pushChanges will not happen.`,
352
+ );
334
353
  }
335
354
  // Refresh the status from the API
336
355
  interval(15000)
@@ -344,8 +363,10 @@ export class Lock {
344
363
  async BLEpushChanges(): Promise<void> {
345
364
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`);
346
365
  if (this.LockTargetState !== this.accessory.context.LockTargetState) {
347
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges LockTargetState: ${this.LockTargetState}`
348
- + ` LockTargetStateCached: ${this.accessory.context.LockTargetState}`);
366
+ this.debugLog(
367
+ `${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges LockTargetState: ${this.LockTargetState}` +
368
+ ` LockTargetStateCached: ${this.accessory.context.LockTargetState}`,
369
+ );
349
370
  const switchbot = await this.platform.connectBLE();
350
371
  // Convert to BLE Address
351
372
  this.device.bleMac = this.device
@@ -364,14 +385,17 @@ export class Lock {
364
385
  })
365
386
  .catch(async (e: any) => {
366
387
  this.apiError(e);
367
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
368
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
388
+ this.errorLog(
389
+ `${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}` +
390
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`,
391
+ );
369
392
  await this.BLEPushConnection();
370
393
  });
371
394
  } else {
372
395
  this.debugLog(
373
- `${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges.` + `LockTargetState: ${this.LockTargetState}, `
374
- + `LockTargetStateCached: ${this.accessory.context.LockTargetState}`,
396
+ `${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges.` +
397
+ `LockTargetState: ${this.LockTargetState}, ` +
398
+ `LockTargetStateCached: ${this.accessory.context.LockTargetState}`,
375
399
  );
376
400
  }
377
401
  }
@@ -386,9 +410,9 @@ export class Lock {
386
410
  command = 'unlock';
387
411
  }
388
412
  const bodyChange = JSON.stringify({
389
- 'command': `${command}`,
390
- 'parameter': 'default',
391
- 'commandType': 'command',
413
+ command: `${command}`,
414
+ parameter: 'default',
415
+ commandType: 'command',
392
416
  });
393
417
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
394
418
  try {
@@ -403,13 +427,17 @@ export class Lock {
403
427
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Headers: ${JSON.stringify(headers)}`);
404
428
  } catch (e: any) {
405
429
  this.apiError(e);
406
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
407
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`,
430
+ this.errorLog(
431
+ `${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}` +
432
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`,
408
433
  );
409
434
  }
410
435
  } else {
411
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.` + `LockTargetState: ${this.LockTargetState}, `
412
- + `LockTargetStateCached: ${this.accessory.context.LockTargetState}`);
436
+ this.debugLog(
437
+ `${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.` +
438
+ `LockTargetState: ${this.LockTargetState}, ` +
439
+ `LockTargetStateCached: ${this.accessory.context.LockTargetState}`,
440
+ );
413
441
  }
414
442
  }
415
443
 
@@ -526,8 +554,10 @@ export class Lock {
526
554
  this.offlineOff();
527
555
  break;
528
556
  case 171:
529
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. `
530
- + `Hub: ${this.device.hubDeviceId}`);
557
+ this.errorLog(
558
+ `${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. ` +
559
+ `Hub: ${this.device.hubDeviceId}`,
560
+ );
531
561
  this.offlineOff();
532
562
  break;
533
563
  case 190:
@@ -543,8 +573,10 @@ export class Lock {
543
573
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Request successful, statusCode: ${statusCode}`);
544
574
  break;
545
575
  default:
546
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: `
547
- + `${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`);
576
+ this.infoLog(
577
+ `${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: ` +
578
+ `${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`,
579
+ );
548
580
  }
549
581
  }
550
582
 
@@ -565,14 +597,17 @@ export class Lock {
565
597
 
566
598
  FirmwareRevision(accessory: PlatformAccessory<Context>, device: device & devicesConfig): CharacteristicValue {
567
599
  let FirmwareRevision: string;
568
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
569
- + ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
600
+ this.debugLog(
601
+ `${this.device.deviceType}: ${this.accessory.displayName}` + ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`,
602
+ );
570
603
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
571
604
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
572
- if (accessory.context.FirmwareRevision) {
573
- FirmwareRevision = accessory.context.FirmwareRevision;
574
- } else if (device.firmware) {
605
+ if (device.firmware) {
575
606
  FirmwareRevision = device.firmware;
607
+ } else if (device.version) {
608
+ FirmwareRevision = JSON.stringify(device.version);
609
+ } else if (accessory.context.FirmwareRevision) {
610
+ FirmwareRevision = accessory.context.FirmwareRevision;
576
611
  } else {
577
612
  FirmwareRevision = this.platform.version;
578
613
  }
@@ -618,7 +653,7 @@ export class Lock {
618
653
  config['scanDuration'] = device.scanDuration;
619
654
  }
620
655
  if (Object.entries(config).length !== 0) {
621
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
656
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
622
657
  }
623
658
  }
624
659
 
@@ -1,14 +1,14 @@
1
- import { Context } from 'vm';
2
- import { hostname } from 'os';
3
- import { request } from 'undici';
4
- import { sleep } from '../utils';
5
- import { MqttClient } from 'mqtt';
6
- import { interval, Subject } from 'rxjs';
7
1
  import { connectAsync } from 'async-mqtt';
2
+ import { CharacteristicValue, PlatformAccessory, Service, Units } from 'homebridge';
3
+ import { MqttClient } from 'mqtt';
4
+ import { hostname } from 'os';
5
+ import { Subject, interval } from 'rxjs';
8
6
  import { skipWhile } from 'rxjs/operators';
7
+ import { request } from 'undici';
8
+ import { Context } from 'vm';
9
9
  import { SwitchBotPlatform } from '../platform';
10
- import { Service, PlatformAccessory, Units, CharacteristicValue } from 'homebridge';
11
- import { device, devicesConfig, serviceData, ad, switchbot, temperature, deviceStatus, Devices } from '../settings';
10
+ import { Devices, ad, device, deviceStatus, devicesConfig, serviceData, switchbot, temperature } from '../settings';
11
+ import { sleep } from '../utils';
12
12
 
13
13
  /**
14
14
  * Platform Accessory
@@ -29,6 +29,8 @@ export class Meter {
29
29
  StatusLowBattery?: CharacteristicValue;
30
30
 
31
31
  // OpenAPI Others
32
+ Version: deviceStatus['version'];
33
+ Battery: deviceStatus['battery'];
32
34
  Temperature: deviceStatus['temperature'];
33
35
  Humidity: deviceStatus['humidity'];
34
36
  deviceStatus!: any; //deviceStatusResponse;
@@ -60,18 +62,22 @@ export class Meter {
60
62
  doMeterUpdate: Subject<void>;
61
63
 
62
64
  // Connection
63
- private readonly BLE = (this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI');
64
- private readonly OpenAPI = (this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI');
65
-
66
- constructor(private readonly platform: SwitchBotPlatform, private accessory: PlatformAccessory, public device: device & devicesConfig) {
65
+ private readonly BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
66
+ private readonly OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
67
+
68
+ constructor(
69
+ private readonly platform: SwitchBotPlatform,
70
+ private accessory: PlatformAccessory,
71
+ public device: device & devicesConfig,
72
+ ) {
67
73
  // default placeholders
68
74
  this.logs(device);
69
75
  this.scan(device);
70
76
  this.refreshRate(device);
71
- this.config(device);
72
77
  this.context();
73
78
  this.setupHistoryService(device);
74
79
  this.setupMqtt(device);
80
+ this.config(device);
75
81
 
76
82
  // this is subject we use to track when we need to POST changes to the SwitchBot API
77
83
  this.doMeterUpdate = new Subject();
@@ -149,11 +155,7 @@ export class Meter {
149
155
  }
150
156
 
151
157
  // Battery Service
152
- if (!this.BLE) {
153
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Battery Service`);
154
- this.batteryService = this.accessory.getService(this.platform.Service.Battery);
155
- accessory.removeService(this.batteryService!);
156
- } else if (this.BLE && !this.batteryService) {
158
+ if (!this.batteryService) {
157
159
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Battery Service`);
158
160
  (this.batteryService = this.accessory.getService(this.platform.Service.Battery) || this.accessory.addService(this.platform.Service.Battery)),
159
161
  `${accessory.displayName} Battery`;
@@ -190,8 +192,9 @@ export class Meter {
190
192
  await this.openAPIparseStatus();
191
193
  } else {
192
194
  await this.offlineOff();
193
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
194
- + ` ${this.device.connectionType}, parseStatus will not happen.`);
195
+ this.debugWarnLog(
196
+ `${this.device.deviceType}: ${this.accessory.displayName} Connection Type:` + ` ${this.device.connectionType}, parseStatus will not happen.`,
197
+ );
195
198
  }
196
199
  }
197
200
 
@@ -223,6 +226,16 @@ export class Meter {
223
226
 
224
227
  async openAPIparseStatus(): Promise<void> {
225
228
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
229
+
230
+ // Battery
231
+ this.BatteryLevel = Number(this.Battery);
232
+ if (this.BatteryLevel < 15) {
233
+ this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
234
+ } else {
235
+ this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
236
+ }
237
+ this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
238
+
226
239
  // Current Relative Humidity
227
240
  if (!this.device.meter?.hide_humidity) {
228
241
  this.CurrentRelativeHumidity = this.Humidity!;
@@ -248,8 +261,10 @@ export class Meter {
248
261
  await this.openAPIRefreshStatus();
249
262
  } else {
250
263
  await this.offlineOff();
251
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
252
- + ` ${this.device.connectionType}, refreshStatus will not happen.`);
264
+ this.debugWarnLog(
265
+ `${this.device.deviceType}: ${this.accessory.displayName} Connection Type:` +
266
+ ` ${this.device.connectionType}, refreshStatus will not happen.`,
267
+ );
253
268
  }
254
269
  }
255
270
 
@@ -274,8 +289,10 @@ export class Meter {
274
289
  // Set an event hander
275
290
  switchbot.onadvertisement = async (ad: ad) => {
276
291
  this.address = ad.address;
277
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},`
278
- + ` BLE Address Found: ${this.address}`);
292
+ this.debugLog(
293
+ `${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},` +
294
+ ` BLE Address Found: ${this.address}`,
295
+ );
279
296
  if (ad.serviceData.humidity! > 0) {
280
297
  // reject unreliable data
281
298
  this.humidity = ad.serviceData.humidity;
@@ -310,8 +327,10 @@ export class Meter {
310
327
  })
311
328
  .catch(async (e: any) => {
312
329
  this.apiError(e);
313
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}`
314
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
330
+ this.errorLog(
331
+ `${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}` +
332
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`,
333
+ );
315
334
  await this.BLERefreshConnection(switchbot);
316
335
  });
317
336
  } else {
@@ -329,18 +348,19 @@ export class Meter {
329
348
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Devices: ${JSON.stringify(deviceStatus.body)}`);
330
349
  this.statusCode(statusCode);
331
350
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Headers: ${JSON.stringify(headers)}`);
332
- this.debugLog(
333
- `${this.device.deviceType}: ${this.accessory.displayName
334
- } refreshStatus: ${JSON.stringify(deviceStatus)}`,
335
- );
351
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(deviceStatus)}`);
336
352
  this.Humidity = deviceStatus.body.humidity!;
337
353
  this.Temperature = deviceStatus.body.temperature!;
354
+ this.Battery = deviceStatus.body.battery!;
355
+ this.Version = deviceStatus.body.ver!;
338
356
  this.openAPIparseStatus();
339
357
  this.updateHomeKitCharacteristics();
340
358
  } catch (e: any) {
341
359
  this.apiError(e);
342
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
343
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
360
+ this.errorLog(
361
+ `${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}` +
362
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`,
363
+ );
344
364
  }
345
365
  }
346
366
 
@@ -356,8 +376,10 @@ export class Meter {
356
376
  } else {
357
377
  this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidity;
358
378
  this.humidityservice?.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.CurrentRelativeHumidity);
359
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
360
- + ` CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
379
+ this.debugLog(
380
+ `${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic` +
381
+ ` CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`,
382
+ );
361
383
  if (this.device.mqttURL) {
362
384
  mqttmessage.push(`"humidity": ${this.CurrentRelativeHumidity}`);
363
385
  }
@@ -379,31 +401,30 @@ export class Meter {
379
401
  this.accessory.context.CurrentTemperature = this.CurrentTemperature;
380
402
  this.temperatureservice?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.CurrentTemperature);
381
403
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`);
382
-
383
404
  }
384
405
  }
385
- if (this.BLE) {
386
- if (this.BatteryLevel === undefined) {
387
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
388
- } else {
389
- if (this.device.mqttURL) {
390
- mqttmessage.push(`"battery": ${this.BatteryLevel}`);
391
- }
392
- this.accessory.context.BatteryLevel = this.BatteryLevel;
393
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
394
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
406
+
407
+ if (this.BatteryLevel === undefined) {
408
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
409
+ } else {
410
+ if (this.device.mqttURL) {
411
+ mqttmessage.push(`"battery": ${this.BatteryLevel}`);
395
412
  }
396
- if (this.StatusLowBattery === undefined) {
397
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
398
- } else {
399
- if (this.device.mqttURL) {
400
- mqttmessage.push(`"lowBattery": ${this.StatusLowBattery}`);
401
- }
402
- this.accessory.context.StatusLowBattery = this.StatusLowBattery;
403
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
404
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
413
+ this.accessory.context.BatteryLevel = this.BatteryLevel;
414
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
415
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
416
+ }
417
+ if (this.StatusLowBattery === undefined) {
418
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
419
+ } else {
420
+ if (this.device.mqttURL) {
421
+ mqttmessage.push(`"lowBattery": ${this.StatusLowBattery}`);
405
422
  }
423
+ this.accessory.context.StatusLowBattery = this.StatusLowBattery;
424
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
425
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
406
426
  }
427
+
407
428
  if (this.device.mqttURL) {
408
429
  this.mqttPublish(`{${mqttmessage.join(',')}}`);
409
430
  }
@@ -530,8 +551,10 @@ export class Meter {
530
551
  this.offlineOff();
531
552
  break;
532
553
  case 171:
533
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. `
534
- + `Hub: ${this.device.hubDeviceId}`);
554
+ this.errorLog(
555
+ `${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. ` +
556
+ `Hub: ${this.device.hubDeviceId}`,
557
+ );
535
558
  this.offlineOff();
536
559
  break;
537
560
  case 190:
@@ -547,8 +570,10 @@ export class Meter {
547
570
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Request successful, statusCode: ${statusCode}`);
548
571
  break;
549
572
  default:
550
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: `
551
- + `${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`);
573
+ this.infoLog(
574
+ `${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: ` +
575
+ `${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`,
576
+ );
552
577
  }
553
578
  }
554
579
 
@@ -566,22 +591,23 @@ export class Meter {
566
591
  if (!this.device.meter?.hide_temperature) {
567
592
  this.temperatureservice?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, e);
568
593
  }
569
- if (this.BLE) {
570
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
571
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
572
- }
594
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
595
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
573
596
  }
574
597
 
575
598
  FirmwareRevision(accessory: PlatformAccessory<Context>, device: device & devicesConfig): CharacteristicValue {
576
599
  let FirmwareRevision: string;
577
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
578
- + ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
600
+ this.debugLog(
601
+ `${this.device.deviceType}: ${this.accessory.displayName}` + ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`,
602
+ );
579
603
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
580
604
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
581
- if (accessory.context.FirmwareRevision) {
582
- FirmwareRevision = accessory.context.FirmwareRevision;
583
- } else if (device.firmware) {
605
+ if (device.firmware) {
584
606
  FirmwareRevision = device.firmware;
607
+ } else if (device.version) {
608
+ FirmwareRevision = JSON.stringify(device.version);
609
+ } else if (accessory.context.FirmwareRevision) {
610
+ FirmwareRevision = accessory.context.FirmwareRevision;
585
611
  } else {
586
612
  FirmwareRevision = this.platform.version;
587
613
  }
@@ -599,18 +625,16 @@ export class Meter {
599
625
  } else {
600
626
  this.CurrentTemperature = this.accessory.context.CurrentTemperature;
601
627
  }
602
- if (this.BLE) {
603
- if (this.BatteryLevel === undefined) {
604
- this.BatteryLevel = 100;
605
- } else {
606
- this.BatteryLevel = this.accessory.context.BatteryLevel;
607
- }
608
- if (this.StatusLowBattery === undefined) {
609
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
610
- this.accessory.context.StatusLowBattery = this.StatusLowBattery;
611
- } else {
612
- this.StatusLowBattery = this.accessory.context.StatusLowBattery;
613
- }
628
+ if (this.BatteryLevel === undefined) {
629
+ this.BatteryLevel = 100;
630
+ } else {
631
+ this.BatteryLevel = this.accessory.context.BatteryLevel;
632
+ }
633
+ if (this.StatusLowBattery === undefined) {
634
+ this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
635
+ this.accessory.context.StatusLowBattery = this.StatusLowBattery;
636
+ } else {
637
+ this.StatusLowBattery = this.accessory.context.StatusLowBattery;
614
638
  }
615
639
  }
616
640
 
@@ -648,7 +672,7 @@ export class Meter {
648
672
  config['scanDuration'] = device.scanDuration;
649
673
  }
650
674
  if (Object.entries(config).length !== 0) {
651
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
675
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
652
676
  }
653
677
  }
654
678