@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,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,7 @@ export class MeterPlus {
29
29
  StatusLowBattery?: CharacteristicValue;
30
30
 
31
31
  // OpenAPI Others
32
+ Version: deviceStatus['version'];
32
33
  Battery: deviceStatus['battery'];
33
34
  Temperature: deviceStatus['temperature'];
34
35
  Humidity: deviceStatus['humidity'];
@@ -61,18 +62,22 @@ export class MeterPlus {
61
62
  doMeterUpdate: Subject<void>;
62
63
 
63
64
  // Connection
64
- private readonly BLE = (this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI');
65
- private readonly OpenAPI = (this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI');
66
-
67
- 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
+ ) {
68
73
  // default placeholders
69
74
  this.logs(device);
70
75
  this.scan(device);
71
76
  this.refreshRate(device);
72
- this.config(device);
73
77
  this.context();
74
78
  this.setupHistoryService(device);
75
79
  this.setupMqtt(device);
80
+ this.config(device);
76
81
 
77
82
  // this is subject we use to track when we need to POST changes to the SwitchBot API
78
83
  this.doMeterUpdate = new Subject();
@@ -150,11 +155,7 @@ export class MeterPlus {
150
155
  }
151
156
 
152
157
  // Battery Service
153
- if (!this.BLE) {
154
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Battery Service`);
155
- this.batteryService = this.accessory.getService(this.platform.Service.Battery);
156
- accessory.removeService(this.batteryService!);
157
- } else if (this.BLE && !this.batteryService) {
158
+ if (!this.batteryService) {
158
159
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Battery Service`);
159
160
  (this.batteryService = this.accessory.getService(this.platform.Service.Battery) || this.accessory.addService(this.platform.Service.Battery)),
160
161
  `${accessory.displayName} Battery`;
@@ -191,8 +192,9 @@ export class MeterPlus {
191
192
  await this.openAPIparseStatus();
192
193
  } else {
193
194
  await this.offlineOff();
194
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
195
- + ` ${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
+ );
196
198
  }
197
199
  }
198
200
 
@@ -222,9 +224,18 @@ export class MeterPlus {
222
224
  }
223
225
  }
224
226
 
225
-
226
227
  async openAPIparseStatus(): Promise<void> {
227
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
+
228
239
  // Current Relative Humidity
229
240
  if (!this.device.meter?.hide_humidity) {
230
241
  this.CurrentRelativeHumidity = this.Humidity!;
@@ -250,8 +261,10 @@ export class MeterPlus {
250
261
  await this.openAPIRefreshStatus();
251
262
  } else {
252
263
  await this.offlineOff();
253
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
254
- + ` ${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
+ );
255
268
  }
256
269
  }
257
270
 
@@ -276,8 +289,10 @@ export class MeterPlus {
276
289
  // Set an event hander
277
290
  switchbot.onadvertisement = async (ad: ad) => {
278
291
  this.address = ad.address;
279
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},`
280
- + ` 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
+ );
281
296
  if (ad.serviceData.humidity! > 0) {
282
297
  // reject unreliable data
283
298
  this.humidity = ad.serviceData.humidity;
@@ -312,8 +327,10 @@ export class MeterPlus {
312
327
  })
313
328
  .catch(async (e: any) => {
314
329
  this.apiError(e);
315
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}`
316
- + ` 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
+ );
317
334
  await this.BLERefreshConnection(switchbot);
318
335
  });
319
336
  } else {
@@ -331,19 +348,19 @@ export class MeterPlus {
331
348
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Devices: ${JSON.stringify(deviceStatus.body)}`);
332
349
  this.statusCode(statusCode);
333
350
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Headers: ${JSON.stringify(headers)}`);
334
- this.debugLog(
335
- `${this.device.deviceType}: ${this.accessory.displayName
336
- } refreshStatus: ${JSON.stringify(deviceStatus)}`,
337
- );
351
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(deviceStatus)}`);
338
352
  this.Humidity = deviceStatus.body.humidity!;
339
353
  this.Temperature = deviceStatus.body.temperature!;
340
354
  this.Battery = deviceStatus.body.battery!;
355
+ this.Version = deviceStatus.body.version!;
341
356
  this.openAPIparseStatus();
342
357
  this.updateHomeKitCharacteristics();
343
358
  } catch (e: any) {
344
359
  this.apiError(e);
345
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
346
- + ` 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
+ );
347
364
  }
348
365
  }
349
366
 
@@ -359,8 +376,10 @@ export class MeterPlus {
359
376
  } else {
360
377
  this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidity;
361
378
  this.humidityservice?.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.CurrentRelativeHumidity);
362
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
363
- + ` updateCharacteristic CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
379
+ this.debugLog(
380
+ `${this.device.deviceType}: ${this.accessory.displayName}` +
381
+ ` updateCharacteristic CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`,
382
+ );
364
383
  if (this.device.mqttURL) {
365
384
  mqttmessage.push(`"humidity": ${this.CurrentRelativeHumidity}`);
366
385
  }
@@ -384,28 +403,29 @@ export class MeterPlus {
384
403
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`);
385
404
  }
386
405
  }
387
- if (this.BLE) {
388
- if (this.BatteryLevel === undefined) {
389
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
390
- } else {
391
- if (this.device.mqttURL) {
392
- mqttmessage.push(`"battery": ${this.BatteryLevel}`);
393
- }
394
- this.accessory.context.BatteryLevel = this.BatteryLevel;
395
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
396
- 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}`);
397
412
  }
398
- if (this.StatusLowBattery === undefined) {
399
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
400
- } else {
401
- if (this.device.mqttURL) {
402
- mqttmessage.push(`"lowBattery": ${this.StatusLowBattery}`);
403
- }
404
- this.accessory.context.StatusLowBattery = this.StatusLowBattery;
405
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
406
- 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
+
418
+ if (this.StatusLowBattery === undefined) {
419
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
420
+ } else {
421
+ if (this.device.mqttURL) {
422
+ mqttmessage.push(`"lowBattery": ${this.StatusLowBattery}`);
407
423
  }
424
+ this.accessory.context.StatusLowBattery = this.StatusLowBattery;
425
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
426
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
408
427
  }
428
+
409
429
  if (this.device.mqttURL) {
410
430
  this.mqttPublish(`{${mqttmessage.join(',')}}`);
411
431
  }
@@ -542,8 +562,10 @@ export class MeterPlus {
542
562
  this.offlineOff();
543
563
  break;
544
564
  case 171:
545
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. `
546
- + `Hub: ${this.device.hubDeviceId}`);
565
+ this.errorLog(
566
+ `${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. ` +
567
+ `Hub: ${this.device.hubDeviceId}`,
568
+ );
547
569
  this.offlineOff();
548
570
  break;
549
571
  case 190:
@@ -559,8 +581,10 @@ export class MeterPlus {
559
581
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Request successful, statusCode: ${statusCode}`);
560
582
  break;
561
583
  default:
562
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: `
563
- + `${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`);
584
+ this.infoLog(
585
+ `${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: ` +
586
+ `${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`,
587
+ );
564
588
  }
565
589
  }
566
590
 
@@ -578,22 +602,23 @@ export class MeterPlus {
578
602
  if (!this.device.meter?.hide_temperature) {
579
603
  this.temperatureservice?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, e);
580
604
  }
581
- if (this.BLE) {
582
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
583
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
584
- }
605
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
606
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
585
607
  }
586
608
 
587
609
  FirmwareRevision(accessory: PlatformAccessory<Context>, device: device & devicesConfig): CharacteristicValue {
588
610
  let FirmwareRevision: string;
589
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
590
- + ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
611
+ this.debugLog(
612
+ `${this.device.deviceType}: ${this.accessory.displayName}` + ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`,
613
+ );
591
614
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
592
615
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
593
- if (accessory.context.FirmwareRevision) {
594
- FirmwareRevision = accessory.context.FirmwareRevision;
595
- } else if (device.firmware) {
616
+ if (device.firmware) {
596
617
  FirmwareRevision = device.firmware;
618
+ } else if (device.version) {
619
+ FirmwareRevision = JSON.stringify(device.version);
620
+ } else if (accessory.context.FirmwareRevision) {
621
+ FirmwareRevision = accessory.context.FirmwareRevision;
597
622
  } else {
598
623
  FirmwareRevision = this.platform.version;
599
624
  }
@@ -611,18 +636,16 @@ export class MeterPlus {
611
636
  } else {
612
637
  this.CurrentTemperature = this.accessory.context.CurrentTemperature;
613
638
  }
614
- if (this.BLE) {
615
- if (this.BatteryLevel === undefined) {
616
- this.BatteryLevel = 100;
617
- } else {
618
- this.BatteryLevel = this.accessory.context.BatteryLevel;
619
- }
620
- if (this.StatusLowBattery === undefined) {
621
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
622
- this.accessory.context.StatusLowBattery = this.StatusLowBattery;
623
- } else {
624
- this.StatusLowBattery = this.accessory.context.StatusLowBattery;
625
- }
639
+ if (this.BatteryLevel === undefined) {
640
+ this.BatteryLevel = 100;
641
+ } else {
642
+ this.BatteryLevel = this.accessory.context.BatteryLevel;
643
+ }
644
+ if (this.StatusLowBattery === undefined) {
645
+ this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
646
+ this.accessory.context.StatusLowBattery = this.StatusLowBattery;
647
+ } else {
648
+ this.StatusLowBattery = this.accessory.context.StatusLowBattery;
626
649
  }
627
650
  }
628
651
 
@@ -660,7 +683,7 @@ export class MeterPlus {
660
683
  config['scanDuration'] = device.scanDuration;
661
684
  }
662
685
  if (Object.entries(config).length !== 0) {
663
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
686
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
664
687
  }
665
688
  }
666
689
 
@@ -25,6 +25,8 @@ export class Motion {
25
25
  StatusLowBattery?: CharacteristicValue;
26
26
 
27
27
  // OpenAPI Others
28
+ Version: deviceStatus['version'];
29
+ Battery: deviceStatus['battery'];
28
30
  deviceStatus!: any; //deviceStatusResponse;
29
31
  moveDetected: deviceStatus['moveDetected'];
30
32
  brightness: deviceStatus['brightness'];
@@ -38,7 +40,7 @@ export class Motion {
38
40
  battery!: serviceData['battery'];
39
41
  movement!: serviceData['movement'];
40
42
  lightLevel!: serviceData['lightLevel'];
41
- is_light!: any;//serviceData['is_light'];
43
+ is_light!: any; //serviceData['is_light'];
42
44
  tested!: any;
43
45
  led!: any;
44
46
  iot!: any;
@@ -56,16 +58,20 @@ export class Motion {
56
58
  doMotionUpdate!: Subject<void>;
57
59
 
58
60
  // Connection
59
- private readonly BLE = (this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI');
60
- private readonly OpenAPI = (this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI');
61
-
62
- constructor(private readonly platform: SwitchBotPlatform, private accessory: PlatformAccessory, public device: device & devicesConfig) {
61
+ private readonly BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
62
+ private readonly OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
63
+
64
+ constructor(
65
+ private readonly platform: SwitchBotPlatform,
66
+ private accessory: PlatformAccessory,
67
+ public device: device & devicesConfig,
68
+ ) {
63
69
  // default placeholders
64
70
  this.logs(device);
65
71
  this.scan(device);
66
72
  this.refreshRate(device);
67
- this.config(device);
68
73
  this.context();
74
+ this.config(device);
69
75
 
70
76
  // this is subject we use to track when we need to POST changes to the SwitchBot API
71
77
  this.doMotionUpdate = new Subject();
@@ -120,17 +126,16 @@ export class Motion {
120
126
  }
121
127
 
122
128
  // Battery Service
123
- if (!this.BLE) {
124
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Battery Service`);
125
- this.batteryService = this.accessory.getService(this.platform.Service.Battery);
126
- accessory.removeService(this.batteryService!);
127
- } else if (this.BLE && !this.batteryService) {
129
+ if (!this.batteryService) {
128
130
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Battery Service`);
129
131
  (this.batteryService = this.accessory.getService(this.platform.Service.Battery) || this.accessory.addService(this.platform.Service.Battery)),
130
132
  `${accessory.displayName} Battery`;
131
133
 
132
134
  this.batteryService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Battery`);
133
- this.batteryService.setCharacteristic(this.platform.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
135
+ if (!this.batteryService.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
136
+ this.batteryService.addCharacteristic(this.platform.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
137
+ }
138
+ this.batteryService.setCharacteristic(this.platform.Characteristic.ChargingState, this.platform.Characteristic.ChargingState.NOT_CHARGEABLE);
134
139
  } else {
135
140
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Battery Service Not Added`);
136
141
  }
@@ -158,8 +163,9 @@ export class Motion {
158
163
  await this.openAPIparseStatus();
159
164
  } else {
160
165
  await this.offlineOff();
161
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
162
- + ` ${this.device.connectionType}, parseStatus will not happen.`);
166
+ this.debugWarnLog(
167
+ `${this.device.deviceType}: ${this.accessory.displayName} Connection Type:` + ` ${this.device.connectionType}, parseStatus will not happen.`,
168
+ );
163
169
  }
164
170
  }
165
171
 
@@ -168,7 +174,7 @@ export class Motion {
168
174
  // Movement
169
175
  this.MotionDetected = this.movement!;
170
176
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
171
- if ((this.MotionDetected !== this.accessory.context.MotionDetected) && this.MotionDetected) {
177
+ if (this.MotionDetected !== this.accessory.context.MotionDetected && this.MotionDetected) {
172
178
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Detected Motion`);
173
179
  }
174
180
  // Light Level
@@ -183,8 +189,10 @@ export class Motion {
183
189
  default:
184
190
  this.CurrentAmbientLightLevel = this.set_maxLux;
185
191
  }
186
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${this.lightLevel},` +
187
- ` CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
192
+ this.debugLog(
193
+ `${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${this.lightLevel},` +
194
+ ` CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`,
195
+ );
188
196
  if (this.CurrentAmbientLightLevel !== this.accessory.context.CurrentAmbientLightLevel) {
189
197
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
190
198
  }
@@ -199,8 +207,9 @@ export class Motion {
199
207
  } else {
200
208
  this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
201
209
  }
202
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel},`
203
- + ` StatusLowBattery: ${this.StatusLowBattery}`);
210
+ this.debugLog(
211
+ `${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel},` + ` StatusLowBattery: ${this.StatusLowBattery}`,
212
+ );
204
213
  }
205
214
 
206
215
  async openAPIparseStatus(): Promise<void> {
@@ -222,6 +231,19 @@ export class Motion {
222
231
  }
223
232
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
224
233
  }
234
+ // Battery
235
+ if (this.battery === undefined) {
236
+ this.battery = 100;
237
+ }
238
+ this.BatteryLevel = this.Battery!;
239
+ if (this.BatteryLevel < 10) {
240
+ this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
241
+ } else {
242
+ this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
243
+ }
244
+ this.debugLog(
245
+ `${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel},` + ` StatusLowBattery: ${this.StatusLowBattery}`,
246
+ );
225
247
  }
226
248
 
227
249
  /**
@@ -236,8 +258,10 @@ export class Motion {
236
258
  await this.openAPIRefreshStatus();
237
259
  } else {
238
260
  await this.offlineOff();
239
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
240
- + ` ${this.device.connectionType}, refreshStatus will not happen.`);
261
+ this.debugWarnLog(
262
+ `${this.device.deviceType}: ${this.accessory.displayName} Connection Type:` +
263
+ ` ${this.device.connectionType}, refreshStatus will not happen.`,
264
+ );
241
265
  }
242
266
  }
243
267
 
@@ -268,8 +292,10 @@ export class Motion {
268
292
  this.scanning = true;
269
293
  switchbot.onadvertisement = async (ad: any) => {
270
294
  this.address = ad.address;
271
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},`
272
- + ` BLE Address Found: ${this.address}`);
295
+ this.debugLog(
296
+ `${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},` +
297
+ ` BLE Address Found: ${this.address}`,
298
+ );
273
299
  this.serviceData = ad.serviceData;
274
300
  this.movement = ad.serviceData.movement;
275
301
  this.tested = ad.serviceData.tested;
@@ -312,8 +338,10 @@ export class Motion {
312
338
  })
313
339
  .catch(async (e: any) => {
314
340
  this.apiError(e);
315
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}`
316
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
341
+ this.errorLog(
342
+ `${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}` +
343
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`,
344
+ );
317
345
  this.debugErrorLog('3');
318
346
  await this.BLERefreshConnection(switchbot);
319
347
  });
@@ -333,18 +361,19 @@ export class Motion {
333
361
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Devices: ${JSON.stringify(deviceStatus.body)}`);
334
362
  this.statusCode(statusCode);
335
363
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Headers: ${JSON.stringify(headers)}`);
336
- this.debugLog(
337
- `${this.device.deviceType}: ${this.accessory.displayName
338
- } refreshStatus: ${JSON.stringify(deviceStatus)}`,
339
- );
364
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(deviceStatus)}`);
340
365
  this.moveDetected = deviceStatus.body.moveDetected;
341
366
  this.brightness = deviceStatus.body.brightness;
367
+ this.Battery = deviceStatus.body.battery!;
368
+ this.Version = deviceStatus.body.version!;
342
369
  this.openAPIparseStatus();
343
370
  this.updateHomeKitCharacteristics();
344
371
  } catch (e: any) {
345
372
  this.apiError(e);
346
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
347
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
373
+ this.errorLog(
374
+ `${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}` +
375
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`,
376
+ );
348
377
  }
349
378
  }
350
379
 
@@ -359,29 +388,29 @@ export class Motion {
359
388
  this.motionSensorService.updateCharacteristic(this.platform.Characteristic.MotionDetected, this.MotionDetected);
360
389
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic MotionDetected: ${this.MotionDetected}`);
361
390
  }
391
+ if (this.BatteryLevel === undefined) {
392
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
393
+ } else {
394
+ this.accessory.context.BatteryLevel = this.BatteryLevel;
395
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
396
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
397
+ }
398
+ if (this.StatusLowBattery === undefined) {
399
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
400
+ } else {
401
+ this.accessory.context.StatusLowBattery = this.StatusLowBattery;
402
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
403
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
404
+ }
362
405
  if (this.BLE) {
363
- if (this.BatteryLevel === undefined) {
364
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
365
- } else {
366
- this.accessory.context.BatteryLevel = this.BatteryLevel;
367
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
368
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
369
- }
370
- if (this.StatusLowBattery === undefined) {
371
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
372
- } else {
373
- this.accessory.context.StatusLowBattery = this.StatusLowBattery;
374
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
375
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
376
- }
377
406
  if (this.CurrentAmbientLightLevel === undefined) {
378
407
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
379
408
  } else {
380
409
  this.accessory.context.CurrentAmbientLightLevel = this.CurrentAmbientLightLevel;
381
410
  this.lightSensorService?.updateCharacteristic(this.platform.Characteristic.CurrentAmbientLightLevel, this.CurrentAmbientLightLevel);
382
411
  this.debugLog(
383
- `${this.device.deviceType}: ${this.accessory.displayName}`
384
- + ` updateCharacteristic CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`,
412
+ `${this.device.deviceType}: ${this.accessory.displayName}` +
413
+ ` updateCharacteristic CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`,
385
414
  );
386
415
  }
387
416
  }
@@ -491,8 +520,10 @@ export class Motion {
491
520
  this.offlineOff();
492
521
  break;
493
522
  case 171:
494
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. `
495
- + `Hub: ${this.device.hubDeviceId}`);
523
+ this.errorLog(
524
+ `${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. ` +
525
+ `Hub: ${this.device.hubDeviceId}`,
526
+ );
496
527
  this.offlineOff();
497
528
  break;
498
529
  case 190:
@@ -508,8 +539,10 @@ export class Motion {
508
539
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Request successful, statusCode: ${statusCode}`);
509
540
  break;
510
541
  default:
511
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: `
512
- + `${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`);
542
+ this.infoLog(
543
+ `${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: ` +
544
+ `${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`,
545
+ );
513
546
  }
514
547
  }
515
548
 
@@ -522,23 +555,26 @@ export class Motion {
522
555
 
523
556
  async apiError(e: any): Promise<void> {
524
557
  this.motionSensorService.updateCharacteristic(this.platform.Characteristic.MotionDetected, e);
558
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
559
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
525
560
  if (this.BLE) {
526
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
527
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
528
561
  this.lightSensorService?.updateCharacteristic(this.platform.Characteristic.CurrentAmbientLightLevel, e);
529
562
  }
530
563
  }
531
564
 
532
565
  FirmwareRevision(accessory: PlatformAccessory<Context>, device: device & devicesConfig): CharacteristicValue {
533
566
  let FirmwareRevision: string;
534
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
535
- + ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
567
+ this.debugLog(
568
+ `${this.device.deviceType}: ${this.accessory.displayName}` + ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`,
569
+ );
536
570
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
537
571
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
538
- if (accessory.context.FirmwareRevision) {
539
- FirmwareRevision = accessory.context.FirmwareRevision;
540
- } else if (device.firmware) {
572
+ if (device.firmware) {
541
573
  FirmwareRevision = device.firmware;
574
+ } else if (device.version) {
575
+ FirmwareRevision = JSON.stringify(device.version);
576
+ } else if (accessory.context.FirmwareRevision) {
577
+ FirmwareRevision = accessory.context.FirmwareRevision;
542
578
  } else {
543
579
  FirmwareRevision = this.platform.version;
544
580
  }
@@ -587,7 +623,7 @@ export class Motion {
587
623
  config['offline'] = device.offline;
588
624
  }
589
625
  if (Object.entries(config).length !== 0) {
590
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
626
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
591
627
  }
592
628
  }
593
629