@switchbot/homebridge-switchbot 1.6.4-beta.0 → 1.6.4-beta.4

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.
@@ -339,18 +339,34 @@ export class Humidifier {
339
339
  }
340
340
  }
341
341
 
342
- private connectBLE() {
343
- // Convert to BLE Address
344
- this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
345
- this.debugLog(`Humidifier: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
346
- const switchbot = this.platform.connectBLE();
342
+ public async connectBLE() {
343
+ let switchbot: any;
344
+ try {
345
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
346
+ const Switchbot = require('node-switchbot');
347
+ switchbot = new Switchbot();
348
+ // Convert to BLE Address
349
+ this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
350
+ this.debugLog(`Humidifier: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
351
+ } catch (e: any) {
352
+ switchbot = false;
353
+ this.errorLog(`Humidifier: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot}`);
354
+ if (this.deviceLogging === 'debug') {
355
+ this.errorLog(`Humidifier: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
356
+ + ` Error Message: ${JSON.stringify(e.message)}`);
357
+ }
358
+ if (this.platform.debugMode) {
359
+ this.errorLog(`Humidifier: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
360
+ + ` Error: ${JSON.stringify(e)}`);
361
+ }
362
+ }
347
363
  return switchbot;
348
364
  }
349
365
 
350
366
  private async BLERefreshStatus() {
351
367
  this.debugLog(`Humidifier: ${this.accessory.displayName} BLE refreshStatus`);
352
368
  // eslint-disable-next-line @typescript-eslint/no-var-requires
353
- const switchbot = this.connectBLE();
369
+ const switchbot = await this.connectBLE();
354
370
  // Start to monitor advertisement packets
355
371
  if (switchbot !== false) {
356
372
  switchbot.startScan({
@@ -384,7 +400,7 @@ export class Humidifier {
384
400
  this.parseStatus();
385
401
  this.updateHomeKitCharacteristics();
386
402
  } else {
387
- await this.BLEconnection();
403
+ await this.BLEconnection(switchbot);
388
404
  }
389
405
  }).catch(async (e: any) => {
390
406
  this.errorLog(`Humidifier: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
@@ -403,12 +419,12 @@ export class Humidifier {
403
419
  this.apiError(e);
404
420
  });
405
421
  } else {
406
- await this.BLEconnection();
422
+ await this.BLEconnection(switchbot);
407
423
  }
408
424
  }
409
425
 
410
- private async BLEconnection() {
411
- this.errorLog(`Humidifier: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
426
+ public async BLEconnection(switchbot: any) {
427
+ this.errorLog(`Humidifier: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
412
428
  if (this.platform.config.credentials?.openToken) {
413
429
  this.warnLog(`Humidifier: ${this.accessory.displayName} Using OpenAPI Connection`);
414
430
  await this.openAPIRefreshStatus();
@@ -461,7 +477,7 @@ export class Humidifier {
461
477
 
462
478
  private async BLEpushChanges() {
463
479
  this.debugLog(`Humidifier: ${this.accessory.displayName} BLE pushChanges`);
464
- const switchbot = this.connectBLE();
480
+ const switchbot = await this.connectBLE();
465
481
  if (switchbot !== false) {
466
482
  switchbot.discover({ model: 'e', quick: true, id: this.device.bleMac }).then((device_list) => {
467
483
  this.infoLog(`${this.accessory.displayName} Target Position: ${this.Active}`);
@@ -303,19 +303,34 @@ export class Meter {
303
303
  }
304
304
  }
305
305
 
306
- private connectBLE() {
307
- // Convert to BLE Address
308
- this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
309
- this.debugLog(`Meter: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
310
- const switchbot = this.platform.connectBLE();
306
+ private async connectBLE() {
307
+ let switchbot: any;
308
+ try {
309
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
310
+ const Switchbot = require('node-switchbot');
311
+ switchbot = new Switchbot();
312
+ // Convert to BLE Address
313
+ this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
314
+ this.debugLog(`Meter: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
315
+ } catch (e: any) {
316
+ switchbot = false;
317
+ this.errorLog(`Meter: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot}`);
318
+ if (this.deviceLogging === 'debug') {
319
+ this.errorLog(`Meter: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
320
+ + ` Error Message: ${JSON.stringify(e.message)}`);
321
+ }
322
+ if (this.platform.debugMode) {
323
+ this.errorLog(`Meter: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
324
+ + ` Error: ${JSON.stringify(e)}`);
325
+ }
326
+ }
311
327
  return switchbot;
312
328
  }
313
329
 
314
-
315
330
  private async BLErefreshStatus() {
316
331
  this.debugLog(`Meter: ${this.accessory.displayName} BLE RefreshStatus`);
317
332
  // eslint-disable-next-line @typescript-eslint/no-var-requires
318
- const switchbot = this.connectBLE();
333
+ const switchbot = await this.connectBLE();
319
334
  // Start to monitor advertisement packets
320
335
  if (switchbot !== false) {
321
336
  switchbot.startScan({
@@ -356,7 +371,7 @@ export class Meter {
356
371
  this.parseStatus();
357
372
  this.updateHomeKitCharacteristics();
358
373
  } else {
359
- await this.BLEconnection();
374
+ await this.BLEconnection(switchbot);
360
375
  }
361
376
  }).catch(async (e: any) => {
362
377
  this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
@@ -375,12 +390,12 @@ export class Meter {
375
390
  this.apiError(e);
376
391
  });
377
392
  } else {
378
- await this.BLEconnection();
393
+ await this.BLEconnection(switchbot);
379
394
  }
380
395
  }
381
396
 
382
- private async BLEconnection() {
383
- this.errorLog(`Meter: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
397
+ private async BLEconnection(switchbot: any) {
398
+ this.errorLog(`Meter: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
384
399
  if (this.platform.config.credentials?.openToken) {
385
400
  this.warnLog(`Meter: ${this.accessory.displayName} Using OpenAPI Connection`);
386
401
  await this.openAPIRefreshStatus();
@@ -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
  */