@switchbot/homebridge-switchbot 1.7.1-beta.1 → 1.7.1-beta.2

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.
@@ -27,6 +27,7 @@ export class Motion {
27
27
  // BLE Others
28
28
  connected?: boolean;
29
29
  switchbot!: switchbot;
30
+ SwitchToOpenAPI?: boolean;
30
31
  serviceData!: serviceData;
31
32
  battery!: serviceData['battery'];
32
33
  movement!: serviceData['movement'];
@@ -108,12 +109,20 @@ export class Motion {
108
109
  }
109
110
 
110
111
  // Battery Service
111
- if (device.ble) {
112
+ if (!device.ble) {
113
+ this.debugLog(`Motion Sensor: ${accessory.displayName} Removing Battery Service`);
114
+ this.batteryService = this.accessory.getService(this.platform.Service.Battery);
115
+ accessory.removeService(this.batteryService!);
116
+ } else if (!this.batteryService) {
117
+ this.debugLog(`Motion Sensor: ${accessory.displayName} Add Battery Service`);
112
118
  (this.batteryService =
113
- accessory.getService(this.platform.Service.Battery) ||
114
- accessory.addService(this.platform.Service.Battery)), `${accessory.displayName} Battery`;
119
+ this.accessory.getService(this.platform.Service.Battery) ||
120
+ this.accessory.addService(this.platform.Service.Battery)), `${accessory.displayName} Battery`;
115
121
 
116
122
  this.batteryService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Battery`);
123
+
124
+ } else {
125
+ this.debugLog(`Motion Sensor: ${accessory.displayName} Battery Service Not Added`);
117
126
  }
118
127
 
119
128
  // Retrieve initial values and updateHomekit
@@ -196,36 +205,32 @@ export class Motion {
196
205
  * Parse the device status from the SwitchBot api
197
206
  */
198
207
  async parseStatus() {
199
- if (this.device.ble) {
200
- await this.BLEparseStatus();
201
- } else {
208
+ if (this.SwitchToOpenAPI || !this.device.ble) {
202
209
  await this.openAPIparseStatus();
210
+ } else {
211
+ await this.BLEparseStatus();
203
212
  }
204
213
  }
205
214
 
206
215
  private async BLEparseStatus() {
207
216
  this.debugLog(`Motion Sensor: ${this.accessory.displayName} BLE parseStatus`);
208
217
  // Movement
209
- if (this.movement === undefined && this.platform.config.credentials?.openToken) {
210
- this.openAPImotion();
211
- } else if (this.movement === undefined) {
212
- this.movement = false;
213
- this.BLEmotion();
214
- } else {
215
- this.BLEmotion();
216
- }
218
+ this.MotionDetected = Boolean(this.movement);
219
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
217
220
  // Light Level
218
221
  if (!this.device.motion?.hide_lightsensor) {
219
222
  this.set_minLux = this.minLux();
220
223
  this.set_maxLux = this.maxLux();
221
- if (this.lightLevel === undefined && this.platform.config.credentials?.openToken) {
222
- this.openAPIlux();
223
- } else if (this.lightLevel === undefined) {
224
- this.lightLevel = 100;
225
- this.BLElux();
226
- } else {
227
- this.BLElux();
224
+ switch (this.lightLevel) {
225
+ case 'dark':
226
+ case 1:
227
+ this.CurrentAmbientLightLevel = this.set_minLux;
228
+ break;
229
+ default:
230
+ this.CurrentAmbientLightLevel = this.set_maxLux;
228
231
  }
232
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} LightLevel: ${this.lightLevel},`
233
+ + ` CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
229
234
  }
230
235
  // Battery
231
236
  if (this.battery === undefined) {
@@ -240,55 +245,33 @@ export class Motion {
240
245
  this.debugLog(`Motion Sensor: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
241
246
  }
242
247
 
243
- private BLEmotion() {
244
- this.MotionDetected = Boolean(this.movement);
245
- this.debugLog(`Motion Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
246
- }
247
-
248
- private BLElux() {
249
- switch (this.lightLevel) {
250
- case 'dark':
251
- case 1:
252
- this.CurrentAmbientLightLevel = this.set_minLux;
253
- break;
254
- default:
255
- this.CurrentAmbientLightLevel = this.set_maxLux;
256
- }
257
- this.debugLog(`Motion Sensor: ${this.accessory.displayName} LightLevel: ${this.lightLevel},`
258
- + ` CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
259
- }
260
-
261
248
  private async openAPIparseStatus() {
249
+ if (this.device.ble) {
250
+ this.SwitchToOpenAPI = false;
251
+ }
262
252
  if (this.platform.config.credentials?.openToken) {
263
253
  this.debugLog(`Motion Sensor: ${this.accessory.displayName} OpenAPI parseStatus`);
264
254
  // Motion State
265
- this.openAPImotion();
255
+ if (typeof this.deviceStatus.body.moveDetected === 'boolean') {
256
+ this.MotionDetected = this.deviceStatus.body.moveDetected;
257
+ }
258
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
266
259
  // Light Level
267
260
  if (!this.device.motion?.hide_lightsensor) {
268
- this.openAPIlux();
269
- }
270
- }
271
- }
272
-
273
- private openAPImotion() {
274
- if (typeof this.deviceStatus.body.moveDetected === 'boolean') {
275
- this.MotionDetected = this.deviceStatus.body.moveDetected;
276
- }
277
- this.debugLog(`Motion Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
278
- }
261
+ this.set_minLux = this.minLux();
262
+ this.set_maxLux = this.maxLux();
263
+ switch (this.deviceStatus.body.brightness) {
264
+ case 'dim':
265
+ this.CurrentAmbientLightLevel = this.set_minLux;
266
+ break;
267
+ case 'bright':
268
+ default:
269
+ this.CurrentAmbientLightLevel = this.set_maxLux;
270
+ }
271
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
279
272
 
280
- private openAPIlux() {
281
- this.set_minLux = this.minLux();
282
- this.set_maxLux = this.maxLux();
283
- switch (this.deviceStatus.body.brightness) {
284
- case 'dim':
285
- this.CurrentAmbientLightLevel = this.set_minLux;
286
- break;
287
- case 'bright':
288
- default:
289
- this.CurrentAmbientLightLevel = this.set_maxLux;
273
+ }
290
274
  }
291
- this.debugLog(`Motion Sensor: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
292
275
  }
293
276
 
294
277
  /**
@@ -333,7 +316,7 @@ export class Motion {
333
316
  // Start to monitor advertisement packets
334
317
  if (switchbot !== false) {
335
318
  switchbot.startScan({
336
- //model: 's',
319
+ model: 's',
337
320
  id: this.device.bleMac,
338
321
  }).then(() => {
339
322
  // Set an event hander
@@ -377,6 +360,7 @@ export class Motion {
377
360
  }
378
361
  if (this.platform.config.credentials?.openToken) {
379
362
  this.warnLog(`Motion Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
363
+ this.SwitchToOpenAPI = true;
380
364
  await this.openAPIRefreshStatus();
381
365
  }
382
366
  this.apiError(e);
@@ -390,6 +374,7 @@ export class Motion {
390
374
  this.errorLog(`Motion Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
391
375
  if (this.platform.config.credentials?.openToken) {
392
376
  this.warnLog(`Motion Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
377
+ this.SwitchToOpenAPI = true;
393
378
  await this.openAPIRefreshStatus();
394
379
  }
395
380
  }