@switchbot/homebridge-switchbot 1.7.0 → 1.7.1-beta.11

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 (97) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/config.schema.json +101 -33
  3. package/dist/devices/bots.d.ts +9 -5
  4. package/dist/devices/bots.d.ts.map +1 -1
  5. package/dist/devices/bots.js +144 -77
  6. package/dist/devices/bots.js.map +1 -1
  7. package/dist/devices/colorbulb.d.ts +9 -4
  8. package/dist/devices/colorbulb.d.ts.map +1 -1
  9. package/dist/devices/colorbulb.js +40 -37
  10. package/dist/devices/colorbulb.js.map +1 -1
  11. package/dist/devices/contact.d.ts +10 -2
  12. package/dist/devices/contact.d.ts.map +1 -1
  13. package/dist/devices/contact.js +92 -51
  14. package/dist/devices/contact.js.map +1 -1
  15. package/dist/devices/curtains.d.ts +10 -3
  16. package/dist/devices/curtains.d.ts.map +1 -1
  17. package/dist/devices/curtains.js +52 -34
  18. package/dist/devices/curtains.js.map +1 -1
  19. package/dist/devices/humidifiers.d.ts +8 -2
  20. package/dist/devices/humidifiers.d.ts.map +1 -1
  21. package/dist/devices/humidifiers.js +28 -35
  22. package/dist/devices/humidifiers.js.map +1 -1
  23. package/dist/devices/meters.d.ts +14 -12
  24. package/dist/devices/meters.d.ts.map +1 -1
  25. package/dist/devices/meters.js +63 -103
  26. package/dist/devices/meters.js.map +1 -1
  27. package/dist/devices/motion.d.ts +10 -3
  28. package/dist/devices/motion.d.ts.map +1 -1
  29. package/dist/devices/motion.js +114 -68
  30. package/dist/devices/motion.js.map +1 -1
  31. package/dist/devices/plugs.d.ts +3 -1
  32. package/dist/devices/plugs.d.ts.map +1 -1
  33. package/dist/devices/plugs.js +8 -1
  34. package/dist/devices/plugs.js.map +1 -1
  35. package/dist/irdevices/airconditioners.d.ts +1 -0
  36. package/dist/irdevices/airconditioners.d.ts.map +1 -1
  37. package/dist/irdevices/airconditioners.js +6 -0
  38. package/dist/irdevices/airconditioners.js.map +1 -1
  39. package/dist/irdevices/airpurifiers.d.ts +1 -0
  40. package/dist/irdevices/airpurifiers.d.ts.map +1 -1
  41. package/dist/irdevices/airpurifiers.js +6 -0
  42. package/dist/irdevices/airpurifiers.js.map +1 -1
  43. package/dist/irdevices/cameras.d.ts +1 -0
  44. package/dist/irdevices/cameras.d.ts.map +1 -1
  45. package/dist/irdevices/cameras.js +6 -0
  46. package/dist/irdevices/cameras.js.map +1 -1
  47. package/dist/irdevices/fans.d.ts +1 -0
  48. package/dist/irdevices/fans.d.ts.map +1 -1
  49. package/dist/irdevices/fans.js +6 -0
  50. package/dist/irdevices/fans.js.map +1 -1
  51. package/dist/irdevices/lights.d.ts +1 -0
  52. package/dist/irdevices/lights.d.ts.map +1 -1
  53. package/dist/irdevices/lights.js +6 -0
  54. package/dist/irdevices/lights.js.map +1 -1
  55. package/dist/irdevices/others.d.ts +1 -0
  56. package/dist/irdevices/others.d.ts.map +1 -1
  57. package/dist/irdevices/others.js +6 -0
  58. package/dist/irdevices/others.js.map +1 -1
  59. package/dist/irdevices/tvs.d.ts +1 -0
  60. package/dist/irdevices/tvs.d.ts.map +1 -1
  61. package/dist/irdevices/tvs.js +6 -0
  62. package/dist/irdevices/tvs.js.map +1 -1
  63. package/dist/irdevices/vacuumcleaners.d.ts +1 -0
  64. package/dist/irdevices/vacuumcleaners.d.ts.map +1 -1
  65. package/dist/irdevices/vacuumcleaners.js +6 -0
  66. package/dist/irdevices/vacuumcleaners.js.map +1 -1
  67. package/dist/irdevices/waterheaters.d.ts +1 -0
  68. package/dist/irdevices/waterheaters.d.ts.map +1 -1
  69. package/dist/irdevices/waterheaters.js +6 -0
  70. package/dist/irdevices/waterheaters.js.map +1 -1
  71. package/dist/platform.d.ts +2 -1
  72. package/dist/platform.d.ts.map +1 -1
  73. package/dist/platform.js +14 -1
  74. package/dist/platform.js.map +1 -1
  75. package/dist/settings.d.ts +14 -0
  76. package/dist/settings.d.ts.map +1 -1
  77. package/dist/settings.js.map +1 -1
  78. package/package.json +71 -71
  79. package/src/devices/bots.ts +140 -74
  80. package/src/devices/colorbulb.ts +46 -40
  81. package/src/devices/contact.ts +99 -55
  82. package/src/devices/curtains.ts +60 -36
  83. package/src/devices/humidifiers.ts +36 -36
  84. package/src/devices/meters.ts +79 -113
  85. package/src/devices/motion.ts +125 -74
  86. package/src/devices/plugs.ts +11 -2
  87. package/src/irdevices/airconditioners.ts +7 -0
  88. package/src/irdevices/airpurifiers.ts +7 -0
  89. package/src/irdevices/cameras.ts +7 -0
  90. package/src/irdevices/fans.ts +7 -0
  91. package/src/irdevices/lights.ts +7 -0
  92. package/src/irdevices/others.ts +7 -0
  93. package/src/irdevices/tvs.ts +7 -0
  94. package/src/irdevices/vacuumcleaners.ts +7 -0
  95. package/src/irdevices/waterheaters.ts +7 -0
  96. package/src/platform.ts +14 -1
  97. package/src/settings.ts +15 -0
@@ -3,7 +3,7 @@ import { interval, Subject } from 'rxjs';
3
3
  import { SwitchBotPlatform } from '../platform';
4
4
  import { debounceTime, skipWhile, tap } from 'rxjs/operators';
5
5
  import { Service, PlatformAccessory, CharacteristicValue, ControllerConstructor, Controller, ControllerServiceMap } from 'homebridge';
6
- import { DeviceURL, device, devicesConfig, switchbot, deviceStatusResponse, payload, hs2rgb, rgb2hs, m2hs } from '../settings';
6
+ import { DeviceURL, device, devicesConfig, switchbot, deviceStatusResponse, payload, hs2rgb, rgb2hs, m2hs, deviceStatus } from '../settings';
7
7
 
8
8
  /**
9
9
  * Platform Accessory
@@ -27,6 +27,10 @@ export class ColorBulb {
27
27
  ColorTemperatureCached!: CharacteristicValue;
28
28
 
29
29
  // OpenAPI Others
30
+ power: deviceStatus['power'];
31
+ color: deviceStatus['color'];
32
+ brightness: deviceStatus['brightness'];
33
+ colorTemperature: deviceStatus['colorTemperature'];
30
34
  deviceStatus!: deviceStatusResponse;
31
35
 
32
36
  // BLE Others
@@ -54,9 +58,10 @@ export class ColorBulb {
54
58
  public device: device & devicesConfig,
55
59
  ) {
56
60
  // default placeholders
57
- this.logs();
58
- this.refreshRate();
59
- this.adaptiveLighting();
61
+ this.logs(device);
62
+ this.refreshRate(device);
63
+ this.adaptiveLighting(device);
64
+ this.config(device);
60
65
  if (this.On === undefined) {
61
66
  this.On = false;
62
67
  } else {
@@ -68,11 +73,6 @@ export class ColorBulb {
68
73
  this.ColorTemperature = 140;
69
74
  this.minKelvin = 2000;
70
75
  this.maxKelvin = 9000;
71
-
72
- // ColorBulb Config
73
- this.debugLog(`Color Bulb: ${this.accessory.displayName} Config: (ble: ${device.ble}, set_minStep: ${device.colorbulb?.set_minStep},`
74
- + ` adaptiveLighting: ${device.colorbulb?.adaptiveLightingShift})`);
75
-
76
76
  // this is subject we use to track when we need to POST changes to the SwitchBot API
77
77
  this.doColorBulbUpdate = new Subject();
78
78
  this.colorBulbUpdateInProgress = false;
@@ -214,42 +214,44 @@ export class ColorBulb {
214
214
  });
215
215
  }
216
216
 
217
- refreshRate() {
218
- if (this.device.refreshRate) {
219
- this.deviceRefreshRate = this.accessory.context.refreshRate = this.device.refreshRate;
220
- if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
221
- this.warnLog(`Color Bulb: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
222
- }
217
+ config(device: device & devicesConfig) {
218
+ if (device.bot !== undefined) {
219
+ device.bot['ble'] = device.ble;
220
+ device.bot['logging'] = this.deviceLogging;
221
+ device.bot['refreshRate'] = this.deviceRefreshRate;
222
+ this.warnLog(`Color Bulb: ${this.accessory.displayName} Config: ${JSON.stringify(device.colorbulb)}`);
223
+ }
224
+ }
225
+
226
+ refreshRate(device: device & devicesConfig) {
227
+ if (device.refreshRate) {
228
+ this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
229
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
223
230
  } else if (this.platform.config.options!.refreshRate) {
224
231
  this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options!.refreshRate;
225
- if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
226
- this.warnLog(`Color Bulb: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
227
- }
232
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
228
233
  }
229
234
  }
230
235
 
231
- logs() {
236
+ logs(device: device & devicesConfig) {
232
237
  if (this.platform.debugMode) {
233
- this.deviceLogging = this.accessory.context.logging = 'debug';
234
- this.warnLog(`Color Bulb: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
235
- } else if (this.device.logging) {
236
- this.deviceLogging = this.accessory.context.logging = this.device.logging;
237
- if (this.deviceLogging === 'debug' || this.deviceLogging === 'standard') {
238
- this.warnLog(`Color Bulb: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
239
- }
238
+ this.deviceLogging = this.accessory.context.logging = 'debugMode';
239
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
240
+ } else if (device.logging) {
241
+ this.deviceLogging = this.accessory.context.logging = device.logging;
242
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
240
243
  } else if (this.platform.config.options?.logging) {
241
244
  this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
242
- if (this.deviceLogging === 'debug' || this.deviceLogging === 'standard') {
243
- this.warnLog(`Color Bulb: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
244
- }
245
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
245
246
  } else {
246
247
  this.deviceLogging = this.accessory.context.logging = 'standard';
248
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
247
249
  }
248
250
  }
249
251
 
250
- adaptiveLighting() {
251
- if (this.device.colorbulb?.adaptiveLightingShift) {
252
- this.adaptiveLightingShift = this.device.colorbulb.adaptiveLightingShift;
252
+ adaptiveLighting(device: device & devicesConfig) {
253
+ if (device.colorbulb?.adaptiveLightingShift) {
254
+ this.adaptiveLightingShift = device.colorbulb.adaptiveLightingShift;
253
255
  this.debugLog(`Color Bulb: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
254
256
  } else {
255
257
  this.adaptiveLightingShift = 0;
@@ -267,7 +269,7 @@ export class ColorBulb {
267
269
  }
268
270
 
269
271
  parseStatus() {
270
- switch (this.deviceStatus.body.power) {
272
+ switch (this.power) {
271
273
  case 'on':
272
274
  this.On = true;
273
275
  break;
@@ -277,13 +279,13 @@ export class ColorBulb {
277
279
  this.debugLog(`Color Bulb: ${this.accessory.displayName} On: ${this.On}`);
278
280
 
279
281
  // Brightness
280
- this.Brightness = Number(this.deviceStatus.body.brightness);
282
+ this.Brightness = Number(this.brightness);
281
283
  this.debugLog(`Color Bulb: ${this.accessory.displayName} Brightness: ${this.Brightness}`);
282
284
 
283
285
  // Color, Hue & Brightness
284
- if (this.deviceStatus.body.color) {
285
- this.debugLog(`Color Bulb: ${this.accessory.displayName} color: ${JSON.stringify(this.deviceStatus.body.color)}`);
286
- const [red, green, blue] = this.deviceStatus.body.color!.split(':');
286
+ if (this.color) {
287
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} color: ${JSON.stringify(this.color)}`);
288
+ const [red, green, blue] = this.color!.split(':');
287
289
  this.debugLog(`Color Bulb: ${this.accessory.displayName} red: ${JSON.stringify(red)}`);
288
290
  this.debugLog(`Color Bulb: ${this.accessory.displayName} green: ${JSON.stringify(green)}`);
289
291
  this.debugLog(`Color Bulb: ${this.accessory.displayName} blue: ${JSON.stringify(blue)}`);
@@ -301,9 +303,9 @@ export class ColorBulb {
301
303
  }
302
304
 
303
305
  // ColorTemperature
304
- if (!Number.isNaN(this.deviceStatus.body.colorTemperature)) {
305
- this.debugLog(`Color Bulb: ${this.accessory.displayName} OpenAPI ColorTemperature: ${this.deviceStatus.body.colorTemperature}`);
306
- const mired = Math.round(1000000 / this.deviceStatus.body.colorTemperature!);
306
+ if (!Number.isNaN(this.colorTemperature)) {
307
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} OpenAPI ColorTemperature: ${this.colorTemperature}`);
308
+ const mired = Math.round(1000000 / this.colorTemperature!);
307
309
 
308
310
  this.ColorTemperature = Number(mired);
309
311
 
@@ -316,6 +318,10 @@ export class ColorBulb {
316
318
  try {
317
319
  this.deviceStatus = (await this.platform.axios.get(`${DeviceURL}/${this.device.deviceId}/status`)).data;
318
320
  this.debugLog(`Color Bulb: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(this.deviceStatus)}`);
321
+ this.power = this.deviceStatus.body.power;
322
+ this.color = this.deviceStatus.body.color;
323
+ this.brightness = this.deviceStatus.body.brightness;
324
+ this.colorTemperature = this.deviceStatus.body.colorTemperature;
319
325
  this.parseStatus();
320
326
  this.updateHomeKitCharacteristics();
321
327
  } catch (e: any) {
@@ -2,7 +2,7 @@ import { interval, Subject } from 'rxjs';
2
2
  import { skipWhile } from 'rxjs/operators';
3
3
  import { SwitchBotPlatform } from '../platform';
4
4
  import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
5
- import { DeviceURL, device, devicesConfig, serviceData, switchbot, deviceStatusResponse } from '../settings';
5
+ import { DeviceURL, device, devicesConfig, serviceData, switchbot, deviceStatusResponse, deviceStatus } from '../settings';
6
6
 
7
7
  /**
8
8
  * Platform Accessory
@@ -24,11 +24,15 @@ export class Contact {
24
24
  StatusLowBattery!: CharacteristicValue;
25
25
 
26
26
  // OpenAPI others
27
+ openState: deviceStatus['openState'];
28
+ moveDetected: deviceStatus['moveDetected'];
29
+ brightness: deviceStatus['brightness'];
27
30
  deviceStatus!: deviceStatusResponse;
28
31
 
29
32
  // BLE Others
30
33
  connected?: boolean;
31
34
  switchbot!: switchbot;
35
+ SwitchToOpenAPI!: boolean;
32
36
  serviceData!: serviceData;
33
37
  battery!: serviceData['battery'];
34
38
  movement!: serviceData['movement'];
@@ -36,6 +40,8 @@ export class Contact {
36
40
  lightLevel!: serviceData['lightLevel'];
37
41
 
38
42
  // Config
43
+ set_minLux!: number;
44
+ set_maxLux!: number;
39
45
  scanDuration!: number;
40
46
  deviceLogging!: string;
41
47
  deviceRefreshRate!: number;
@@ -53,13 +59,9 @@ export class Contact {
53
59
  this.logs();
54
60
  this.scan();
55
61
  this.refreshRate();
62
+ this.config(device);
56
63
  this.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED;
57
64
 
58
- // Contact Config
59
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} Config: (ble: ${device.ble}, offline: ${device.offline},`
60
- + ` hide_lightsensor: ${device.contact?.hide_lightsensor}, hide_motionsensor: ${device.contact?.hide_motionsensor})`);
61
-
62
-
63
65
  // this is subject we use to track when we need to POST changes to the SwitchBot API
64
66
  this.doContactUpdate = new Subject();
65
67
  this.contactUbpdateInProgress = false;
@@ -89,7 +91,7 @@ export class Contact {
89
91
  this.service.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
90
92
 
91
93
  // each service must implement at-minimum the "required characteristics" for the given service type
92
- // see https://developers.homebridge.io/#/service/MotionSensor
94
+ // see https://developers.homebridge.io/#/service/ContactSensor
93
95
 
94
96
  // Motion Sensor Service
95
97
  if (device.contact?.hide_motionsensor) {
@@ -125,12 +127,21 @@ export class Contact {
125
127
  this.debugLog(`Contact Sensor: ${accessory.displayName} Light Sensor Service Not Added`);
126
128
  }
127
129
 
128
- if (device.ble) {
130
+ // Battery Service
131
+ if (!device.ble) {
132
+ this.debugLog(`Contact Sensor: ${accessory.displayName} Removing Battery Service`);
133
+ this.batteryService = this.accessory.getService(this.platform.Service.Battery);
134
+ accessory.removeService(this.batteryService!);
135
+ } else if (device.ble && !this.batteryService) {
136
+ this.debugLog(`Contact Sensor: ${accessory.displayName} Add Battery Service`);
129
137
  (this.batteryService =
130
- accessory.getService(this.platform.Service.Battery) ||
131
- accessory.addService(this.platform.Service.Battery)), `${accessory.displayName} Battery`;
138
+ this.accessory.getService(this.platform.Service.Battery) ||
139
+ this.accessory.addService(this.platform.Service.Battery)), `${accessory.displayName} Battery`;
132
140
 
133
141
  this.batteryService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Battery`);
142
+
143
+ } else {
144
+ this.debugLog(`Contact Sensor: ${accessory.displayName} Battery Service Not Added`);
134
145
  }
135
146
 
136
147
  // Retrieve initial values and updateHomekit
@@ -144,6 +155,12 @@ export class Contact {
144
155
  });
145
156
  }
146
157
 
158
+ config(device: device & devicesConfig) {
159
+ if (device.contact !== undefined) {
160
+ this.warnLog(`Contact Sensor: ${this.accessory.displayName} Config: ${JSON.stringify(device.contact)}`);
161
+ }
162
+ }
163
+
147
164
  refreshRate() {
148
165
  if (this.device.refreshRate) {
149
166
  this.deviceRefreshRate = this.accessory.context.refreshRate = this.device.refreshRate;
@@ -191,23 +208,37 @@ export class Contact {
191
208
  }
192
209
  }
193
210
 
211
+ private minLux(): number {
212
+ if (this.device.contact?.set_minLux) {
213
+ this.set_minLux = this.device.contact!.set_minLux!;
214
+ } else {
215
+ this.set_minLux = 1;
216
+ }
217
+ return this.set_minLux;
218
+ }
219
+
220
+ private maxLux(): number {
221
+ if (this.device.contact?.set_maxLux) {
222
+ this.set_maxLux = this.device.contact!.set_maxLux!;
223
+ } else {
224
+ this.set_maxLux = 6001;
225
+ }
226
+ return this.set_maxLux;
227
+ }
228
+
194
229
  /**
195
230
  * Parse the device status from the SwitchBot api
196
231
  */
197
232
  async parseStatus() {
198
- if (this.device.ble) {
199
- await this.BLEparseStatus();
200
- } else {
233
+ if (this.SwitchToOpenAPI || !this.device.ble) {
201
234
  await this.openAPIparseStatus();
235
+ } else {
236
+ await this.BLEparseStatus();
202
237
  }
203
238
  }
204
239
 
205
240
  private async BLEparseStatus() {
206
241
  this.debugLog(`Contact Sensor: ${this.accessory.displayName} BLE parseStatus`);
207
- if (this.device.contact?.hide_motionsensor) {
208
- // Movement
209
- this.MotionDetected = Boolean(this.movement);
210
- }
211
242
  // Door State
212
243
  switch (this.doorState) {
213
244
  case 'open':
@@ -221,44 +252,74 @@ export class Contact {
221
252
  default:
222
253
  this.errorLog(`Contact Sensor: ${this.accessory.displayName} timeout no closed, doorstate: ${this.doorState}`);
223
254
  }
224
- if (this.device.contact?.hide_lightsensor) {// Light Level
255
+ // Movement
256
+ if (!this.device.contact?.hide_motionsensor) {
257
+ this.MotionDetected = Boolean(this.movement);
258
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
259
+ }
260
+ // Light Level
261
+ if (!this.device.contact?.hide_lightsensor) {
262
+ this.set_minLux = this.minLux();
263
+ this.set_maxLux = this.maxLux();
225
264
  switch (this.lightLevel) {
226
265
  case 'dark':
227
266
  case 0:
228
- this.CurrentAmbientLightLevel = 0.0001;
267
+ this.CurrentAmbientLightLevel = this.set_minLux;
229
268
  break;
230
269
  default:
231
- this.CurrentAmbientLightLevel = 100000;
270
+ this.CurrentAmbientLightLevel = this.set_maxLux;
232
271
  }
272
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} LightLevel: ${this.lightLevel},`
273
+ + ` CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
233
274
  }
234
275
  // Battery
276
+ if (this.battery === undefined) {
277
+ this.battery === 100;
278
+ }
235
279
  this.BatteryLevel = Number(this.battery);
236
280
  if (this.BatteryLevel < 10) {
237
281
  this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
238
282
  } else {
239
283
  this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
240
284
  }
241
-
242
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensorState}, MotionDetected: `
243
- + `${this.MotionDetected}, CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}, BatteryLevel: ${this.BatteryLevel}`);
285
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
244
286
  }
245
287
 
246
288
  private async openAPIparseStatus() {
289
+ if (this.device.ble) {
290
+ this.SwitchToOpenAPI = false;
291
+ }
247
292
  if (this.platform.config.credentials?.openToken) {
248
293
  this.debugLog(`Contact Sensor: ${this.accessory.displayName} OpenAPI parseStatus`);
249
- if (this.deviceStatus.body.openState === 'open') {
294
+ // Contact State
295
+ if (this.openState === 'open') {
250
296
  this.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED;
251
297
  this.debugLog(`Contact Sensor: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensorState}`);
252
- } else if (this.deviceStatus.body.openState === 'close') {
298
+ } else if (this.openState === 'close') {
253
299
  this.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED;
254
300
  this.debugLog(`Contact Sensor: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensorState}`);
255
301
  } else {
256
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} openState: ${this.deviceStatus.body.openState}`);
302
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} openState: ${this.openState}`);
257
303
  }
258
- if (this.device.contact?.hide_motionsensor) {
259
- this.MotionDetected = Boolean(this.deviceStatus.body.moveDetected);
304
+ // Motion State
305
+ if (!this.device.contact?.hide_motionsensor) {
306
+ this.MotionDetected = this.moveDetected!;
307
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
308
+ }
309
+ // Light Level
310
+ if (!this.device.contact?.hide_lightsensor) {
311
+ this.set_minLux = this.minLux();
312
+ this.set_maxLux = this.maxLux();
313
+ switch (this.brightness) {
314
+ case 'dim':
315
+ this.CurrentAmbientLightLevel = this.set_minLux;
316
+ break;
317
+ case 'bright':
318
+ default:
319
+ this.CurrentAmbientLightLevel = this.set_maxLux;
320
+ }
321
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
260
322
  }
261
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
262
323
  }
263
324
  }
264
325
 
@@ -273,34 +334,12 @@ export class Contact {
273
334
  }
274
335
  }
275
336
 
276
- public async connectBLE() {
277
- let switchbot: any;
278
- try {
279
- // eslint-disable-next-line @typescript-eslint/no-var-requires
280
- const Switchbot = require('node-switchbot');
281
- switchbot = new Switchbot();
282
- // Convert to BLE Address
283
- this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
284
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
285
- } catch (e: any) {
286
- switchbot = false;
287
- this.errorLog(`Contact Sensor: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot}`);
288
- if (this.deviceLogging === 'debug') {
289
- this.errorLog(`Contact Sensor: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
290
- + ` Error Message: ${JSON.stringify(e.message)}`);
291
- }
292
- if (this.platform.debugMode) {
293
- this.errorLog(`Contact Sensor: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
294
- + ` Error: ${JSON.stringify(e)}`);
295
- }
296
- }
297
- return switchbot;
298
- }
299
-
300
337
  private async BLERefreshStatus() {
301
338
  this.debugLog(`Contact Sensor: ${this.accessory.displayName} BLE refreshStatus`);
302
- // eslint-disable-next-line @typescript-eslint/no-var-requires
303
- const switchbot = await this.connectBLE();
339
+ const switchbot = await this.platform.connectBLE();
340
+ // Convert to BLE Address
341
+ this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
342
+ this.debugLog(`Curtain: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
304
343
  // Start to monitor advertisement packets
305
344
  if (switchbot !== false) {
306
345
  switchbot.startScan({
@@ -353,6 +392,7 @@ export class Contact {
353
392
  }
354
393
  if (this.platform.config.credentials?.openToken) {
355
394
  this.warnLog(`Contact Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
395
+ this.SwitchToOpenAPI = true;
356
396
  await this.openAPIRefreshStatus();
357
397
  }
358
398
  this.apiError(e);
@@ -366,6 +406,7 @@ export class Contact {
366
406
  this.errorLog(`Contact Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
367
407
  if (this.platform.config.credentials?.openToken) {
368
408
  this.warnLog(`Contact Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
409
+ this.SwitchToOpenAPI = true;
369
410
  await this.openAPIRefreshStatus();
370
411
  }
371
412
  }
@@ -376,6 +417,9 @@ export class Contact {
376
417
  try {
377
418
  this.deviceStatus = (await this.platform.axios.get(`${DeviceURL}/${this.device.deviceId}/status`)).data;
378
419
  this.debugLog(`Contact Sensor: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(this.deviceStatus)}`);
420
+ this.openState = this.deviceStatus.body.openState;
421
+ this.moveDetected = this.deviceStatus.body.moveDetected;
422
+ this.brightness = this.deviceStatus.body.brightness;
379
423
  this.parseStatus();
380
424
  this.updateHomeKitCharacteristics();
381
425
  } catch (e: any) {