@switchbot/homebridge-switchbot 1.6.3 → 1.6.4-beta.3

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.
@@ -226,18 +226,34 @@ export class Motion {
226
226
  }
227
227
  }
228
228
 
229
- private connectBLE() {
230
- // Convert to BLE Address
231
- this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
232
- this.debugLog(`Motion Sensor: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
233
- const switchbot = this.platform.connectBLE();
229
+ public async connectBLE() {
230
+ let switchbot: any;
231
+ try {
232
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
233
+ const Switchbot = require('node-switchbot');
234
+ switchbot = new Switchbot();
235
+ // Convert to BLE Address
236
+ this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
237
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
238
+ } catch (e: any) {
239
+ switchbot = false;
240
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot}`);
241
+ if (this.deviceLogging === 'debug') {
242
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
243
+ + ` Error Message: ${JSON.stringify(e.message)}`);
244
+ }
245
+ if (this.platform.debugMode) {
246
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
247
+ + ` Error: ${JSON.stringify(e)}`);
248
+ }
249
+ }
234
250
  return switchbot;
235
251
  }
236
252
 
237
253
  private async BLERefreshStatus() {
238
254
  this.debugLog(`Motion Sensor: ${this.accessory.displayName} BLE RefreshStatus`);
239
255
  // eslint-disable-next-line @typescript-eslint/no-var-requires
240
- const switchbot = this.connectBLE();
256
+ const switchbot = await this.connectBLE();
241
257
  // Start to monitor advertisement packets
242
258
  if (switchbot !== false) {
243
259
  switchbot.startScan({
@@ -271,7 +287,7 @@ export class Motion {
271
287
  this.parseStatus();
272
288
  this.updateHomeKitCharacteristics();
273
289
  } else {
274
- await this.BLEconnection();
290
+ await this.BLEconnection(switchbot);
275
291
  }
276
292
  }).catch(async (e: any) => {
277
293
  this.errorLog(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
@@ -290,12 +306,12 @@ export class Motion {
290
306
  this.apiError(e);
291
307
  });
292
308
  } else {
293
- await this.BLEconnection();
309
+ await this.BLEconnection(switchbot);
294
310
  }
295
311
  }
296
312
 
297
- private async BLEconnection() {
298
- this.errorLog(`Motion Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
313
+ public async BLEconnection(switchbot: any) {
314
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
299
315
  if (this.platform.config.credentials?.openToken) {
300
316
  this.warnLog(`Motion Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
301
317
  await this.openAPIRefreshStatus();
package/src/platform.ts CHANGED
@@ -41,7 +41,6 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
41
41
  registeringDevice!: boolean;
42
42
  debugMode!: boolean;
43
43
  platformLogging?: string;
44
- switchbot: any;
45
44
 
46
45
  constructor(public readonly log: Logger, public readonly config: SwitchBotPlatformConfig, public readonly api: API) {
47
46
  this.logs();
@@ -193,18 +192,6 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
193
192
  }
194
193
  }
195
194
 
196
- connectBLE() {
197
- try {
198
- // eslint-disable-next-line @typescript-eslint/no-var-requires
199
- const Switchbot = require('node-switchbot');
200
- this.switchbot = new Switchbot();
201
- } catch (e) {
202
- this.switchbot = false;
203
- this.errorLog(`Was 'node-switchbot' found: ${this.switchbot}`);
204
- }
205
- return this.switchbot;
206
- }
207
-
208
195
  /**
209
196
  * this method discovers the Locations
210
197
  */