@switchbot/homebridge-switchbot 1.6.2-beta.0 → 1.6.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.
Files changed (40) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/dist/devices/bots.d.ts +1 -0
  3. package/dist/devices/bots.d.ts.map +1 -1
  4. package/dist/devices/bots.js +131 -120
  5. package/dist/devices/bots.js.map +1 -1
  6. package/dist/devices/contact.d.ts +1 -0
  7. package/dist/devices/contact.d.ts.map +1 -1
  8. package/dist/devices/contact.js +67 -61
  9. package/dist/devices/contact.js.map +1 -1
  10. package/dist/devices/curtains.d.ts +2 -1
  11. package/dist/devices/curtains.d.ts.map +1 -1
  12. package/dist/devices/curtains.js +96 -81
  13. package/dist/devices/curtains.js.map +1 -1
  14. package/dist/devices/humidifiers.d.ts +2 -1
  15. package/dist/devices/humidifiers.d.ts.map +1 -1
  16. package/dist/devices/humidifiers.js +91 -80
  17. package/dist/devices/humidifiers.js.map +1 -1
  18. package/dist/devices/meters.d.ts +1 -0
  19. package/dist/devices/meters.d.ts.map +1 -1
  20. package/dist/devices/meters.js +70 -68
  21. package/dist/devices/meters.js.map +1 -1
  22. package/dist/devices/motion.d.ts +1 -0
  23. package/dist/devices/motion.d.ts.map +1 -1
  24. package/dist/devices/motion.js +62 -60
  25. package/dist/devices/motion.js.map +1 -1
  26. package/dist/devices/plugs.js +2 -2
  27. package/dist/devices/plugs.js.map +1 -1
  28. package/dist/platform.d.ts +2 -0
  29. package/dist/platform.d.ts.map +1 -1
  30. package/dist/platform.js +12 -0
  31. package/dist/platform.js.map +1 -1
  32. package/package.json +71 -71
  33. package/src/devices/bots.ts +110 -100
  34. package/src/devices/contact.ts +67 -60
  35. package/src/devices/curtains.ts +95 -80
  36. package/src/devices/humidifiers.ts +90 -79
  37. package/src/devices/meters.ts +70 -67
  38. package/src/devices/motion.ts +62 -59
  39. package/src/devices/plugs.ts +2 -2
  40. package/src/platform.ts +13 -0
@@ -213,8 +213,6 @@ export class Bot {
213
213
  }
214
214
  }
215
215
 
216
-
217
-
218
216
  logs() {
219
217
  if (this.platform.debugMode) {
220
218
  this.deviceLogging = this.accessory.context.logging = 'debug';
@@ -324,76 +322,80 @@ export class Bot {
324
322
  }
325
323
 
326
324
  private connectBLE() {
327
- let Switchbot: any;
328
- try {
329
- Switchbot = require('node-switchbot');
330
- const switchbot = new Switchbot();
331
- this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
332
- this.debugLog(`Bot: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
333
- return switchbot;
334
- } catch (e) {
335
- Switchbot = false;
336
- }
325
+ // Convert to BLE Address
326
+ this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
327
+ this.debugLog(`Bot: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
328
+ const switchbot = this.platform.connectBLE();
329
+ return switchbot;
337
330
  }
338
331
 
339
332
  private async BLERefreshStatus() {
340
333
  this.debugLog(`Bot: ${this.accessory.displayName} BLE refreshStatus`);
341
- // eslint-disable-next-line @typescript-eslint/no-var-requires
334
+
335
+ // Convert to BLE Address
342
336
  const switchbot = this.connectBLE();
343
337
  // Start to monitor advertisement packets
344
- switchbot.startScan({
345
- model: 'H',
346
- id: this.device.bleMac,
347
- }).then(() => {
348
- // Set an event hander
349
- switchbot.onadvertisement = (ad: ad) => {
350
- this.serviceData = ad.serviceData;
351
- this.mode = ad.serviceData.mode;
352
- this.state = ad.serviceData.state;
353
- this.battery = ad.serviceData.battery;
354
- this.debugLog(`Bot: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
355
- this.debugLog(`Bot: ${this.accessory.displayName}, model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName},`
356
- + ` mode: ${ad.serviceData.mode}, state: ${ad.serviceData.state}, battery: ${ad.serviceData.battery}`);
357
-
358
- if (this.serviceData) {
359
- this.connected = true;
360
- this.debugLog(`Bot: ${this.accessory.displayName} connected: ${this.connected}`);
338
+ if (switchbot !== false) {
339
+ switchbot.startScan({
340
+ model: 'H',
341
+ id: this.device.bleMac,
342
+ }).then(() => {
343
+ // Set an event hander
344
+ switchbot.onadvertisement = (ad: ad) => {
345
+ this.serviceData = ad.serviceData;
346
+ this.mode = ad.serviceData.mode;
347
+ this.state = ad.serviceData.state;
348
+ this.battery = ad.serviceData.battery;
349
+ this.debugLog(`Bot: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
350
+ this.debugLog(`Bot: ${this.accessory.displayName}, model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName},`
351
+ + ` mode: ${ad.serviceData.mode}, state: ${ad.serviceData.state}, battery: ${ad.serviceData.battery}`);
352
+
353
+ if (this.serviceData) {
354
+ this.connected = true;
355
+ this.debugLog(`Bot: ${this.accessory.displayName} connected: ${this.connected}`);
356
+ } else {
357
+ this.connected = false;
358
+ this.debugLog(`Bot: ${this.accessory.displayName} connected: ${this.connected}`);
359
+ }
360
+ };
361
+ // Wait 2 seconds
362
+ return switchbot.wait(this.scanDuration * 1000);
363
+ }).then(async () => {
364
+ // Stop to monitor
365
+ switchbot.stopScan();
366
+ if (this.connected) {
367
+ this.parseStatus();
368
+ this.updateHomeKitCharacteristics();
361
369
  } else {
362
- this.connected = false;
363
- this.debugLog(`Bot: ${this.accessory.displayName} connected: ${this.connected}`);
370
+ await this.BLEconnection();
371
+ }
372
+ }).catch(async (e: any) => {
373
+ this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
374
+ if (this.deviceLogging === 'debug') {
375
+ this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
376
+ + ` Error Message: ${JSON.stringify(e.message)}`);
377
+ }
378
+ if (this.platform.debugMode) {
379
+ this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
380
+ + ` Error: ${JSON.stringify(e)}`);
364
381
  }
365
- };
366
- // Wait 2 seconds
367
- return switchbot.wait(this.scanDuration * 1000);
368
- }).then(async () => {
369
- // Stop to monitor
370
- switchbot.stopScan();
371
- if (this.connected) {
372
- this.parseStatus();
373
- this.updateHomeKitCharacteristics();
374
- } else {
375
- this.errorLog(`Bot: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
376
382
  if (this.platform.config.credentials?.openToken) {
377
383
  this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
378
384
  await this.openAPIRefreshStatus();
379
385
  }
380
- }
381
- }).catch(async (e: any) => {
382
- this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
383
- if (this.deviceLogging === 'debug') {
384
- this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
385
- + ` Error Message: ${JSON.stringify(e.message)}`);
386
- }
387
- if (this.platform.debugMode) {
388
- this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
389
- + ` Error: ${JSON.stringify(e)}`);
390
- }
391
- if (this.platform.config.credentials?.openToken) {
392
- this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
393
- await this.openAPIRefreshStatus();
394
- }
395
- this.apiError(e);
396
- });
386
+ this.apiError(e);
387
+ });
388
+ } else {
389
+ await this.BLEconnection();
390
+ }
391
+ }
392
+
393
+ private async BLEconnection() {
394
+ this.errorLog(`Bot: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
395
+ if (this.platform.config.credentials?.openToken) {
396
+ this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
397
+ await this.openAPIRefreshStatus();
398
+ }
397
399
  }
398
400
 
399
401
  private async openAPIRefreshStatus() {
@@ -444,25 +446,51 @@ export class Bot {
444
446
  if (this.On !== this.OnCached) {
445
447
  this.debugLog(`Bot: ${this.accessory.displayName} BLE pushChanges`);
446
448
  const switchbot = this.connectBLE();
447
- if (this.botMode === 'press') {
448
- this.debugLog(`Bot: ${this.accessory.displayName} Bot Mode: ${this.botMode}`);
449
- switchbot.discover({ model: 'H', quick: true, id: this.device.bleMac })
450
- .then((device_list: { press: (arg0: { id: string | undefined; }) => any; }[]) => {
449
+ if (switchbot !== false) {
450
+ if (this.botMode === 'press') {
451
+ this.debugLog(`Bot: ${this.accessory.displayName} Bot Mode: ${this.botMode}`);
452
+ switchbot.discover({ model: 'H', quick: true, id: this.device.bleMac })
453
+ .then((device_list: { press: (arg0: { id: string | undefined; }) => any; }[]) => {
454
+ this.infoLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
455
+ return device_list[0].press({ id: this.device.bleMac });
456
+ }).then(() => {
457
+ this.debugLog(`Bot: ${this.accessory.displayName} Done.`);
458
+ this.On = false;
459
+ this.OnCached = this.On;
460
+ this.accessory.context.On = this.OnCached;
461
+ setTimeout(() => {
462
+ if (this.device.bot?.deviceType === 'switch') {
463
+ this.switchService?.getCharacteristic(this.platform.Characteristic.On).updateValue(this.On);
464
+ } else {
465
+ this.outletService?.getCharacteristic(this.platform.Characteristic.On).updateValue(this.On);
466
+ }
467
+ this.debugLog(`Bot: ${this.accessory.displayName} On: ${this.On}, Switch Timeout`);
468
+ }, 500);
469
+ }).catch(async (e: any) => {
470
+ this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
471
+ if (this.deviceLogging === 'debug') {
472
+ this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
473
+ + ` Error Message: ${JSON.stringify(e.message)}`);
474
+ }
475
+ if (this.platform.debugMode) {
476
+ this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
477
+ + ` Error: ${JSON.stringify(e)}`);
478
+ }
479
+ if (this.platform.config.credentials?.openToken) {
480
+ this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
481
+ await this.openAPIpushChanges();
482
+ }
483
+ this.apiError(e);
484
+ });
485
+ } else if (this.botMode === 'switch') {
486
+ this.debugLog(`Bot: ${this.accessory.displayName} Press Mode: ${this.botMode}`);
487
+ switchbot.discover({ model: 'H', quick: true, id: this.device.bleMac }).then((device_list: any) => {
451
488
  this.infoLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
452
- return device_list[0].press({ id: this.device.bleMac });
489
+ return this.turnOnOff(device_list);
453
490
  }).then(() => {
454
491
  this.debugLog(`Bot: ${this.accessory.displayName} Done.`);
455
- this.On = false;
456
492
  this.OnCached = this.On;
457
493
  this.accessory.context.On = this.OnCached;
458
- setTimeout(() => {
459
- if (this.device.bot?.deviceType === 'switch') {
460
- this.switchService?.getCharacteristic(this.platform.Characteristic.On).updateValue(this.On);
461
- } else {
462
- this.outletService?.getCharacteristic(this.platform.Characteristic.On).updateValue(this.On);
463
- }
464
- this.debugLog(`Bot: ${this.accessory.displayName} On: ${this.On}, Switch Timeout`);
465
- }, 500);
466
494
  }).catch(async (e: any) => {
467
495
  this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
468
496
  if (this.deviceLogging === 'debug') {
@@ -479,33 +507,15 @@ export class Bot {
479
507
  }
480
508
  this.apiError(e);
481
509
  });
482
- } else if (this.botMode === 'switch') {
483
- this.debugLog(`Bot: ${this.accessory.displayName} Press Mode: ${this.botMode}`);
484
- switchbot.discover({ model: 'H', quick: true, id: this.device.bleMac }).then((device_list: any) => {
485
- this.infoLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
486
- return this.turnOnOff(device_list);
487
- }).then(() => {
488
- this.debugLog(`Bot: ${this.accessory.displayName} Done.`);
489
- this.OnCached = this.On;
490
- this.accessory.context.On = this.OnCached;
491
- }).catch(async (e: any) => {
492
- this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
493
- if (this.deviceLogging === 'debug') {
494
- this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
495
- + ` Error Message: ${JSON.stringify(e.message)}`);
496
- }
497
- if (this.platform.debugMode) {
498
- this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
499
- + ` Error: ${JSON.stringify(e)}`);
500
- }
501
- if (this.platform.config.credentials?.openToken) {
502
- this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
503
- await this.openAPIpushChanges();
504
- }
505
- this.apiError(e);
506
- });
510
+ } else {
511
+ throw new Error(`Bot: ${this.accessory.displayName} Bot Mode: ${this.botMode}`);
512
+ }
507
513
  } else {
508
- throw new Error(`Bot: ${this.accessory.displayName} Bot Mode: ${this.botMode}`);
514
+ this.errorLog(`Bot: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
515
+ if (this.platform.config.credentials?.openToken) {
516
+ this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
517
+ await this.openAPIpushChanges();
518
+ }
509
519
  }
510
520
  }
511
521
  this.OnCached = this.On;
@@ -162,12 +162,12 @@ export class Contact {
162
162
  if (this.device.scanDuration) {
163
163
  this.scanDuration = this.accessory.context.scanDuration = this.device.scanDuration;
164
164
  if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
165
- this.warnLog(`Bot: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
165
+ this.warnLog(`Contact Sensor: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
166
166
  }
167
167
  } else {
168
168
  this.scanDuration = this.accessory.context.scanDuration = 1;
169
169
  if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
170
- this.warnLog(`Bot: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
170
+ this.warnLog(`Contact Sensor: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
171
171
  }
172
172
  }
173
173
  }
@@ -274,16 +274,11 @@ export class Contact {
274
274
  }
275
275
 
276
276
  private connectBLE() {
277
- let Switchbot: any;
278
- try {
279
- Switchbot = require('node-switchbot');
280
- const switchbot = new Switchbot();
281
- this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
282
- this.debugLog(`Bot: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
283
- return switchbot;
284
- } catch (e) {
285
- Switchbot = false;
286
- }
277
+ // Convert to BLE Address
278
+ this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
279
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
280
+ const switchbot = this.platform.connectBLE();
281
+ return switchbot;
287
282
  }
288
283
 
289
284
  private async BLERefreshStatus() {
@@ -291,60 +286,72 @@ export class Contact {
291
286
  // eslint-disable-next-line @typescript-eslint/no-var-requires
292
287
  const switchbot = this.connectBLE();
293
288
  // Start to monitor advertisement packets
294
- switchbot.startScan({
295
- model: 'd',
296
- id: this.device.bleMac,
297
- }).then(() => {
298
- // Set an event hander
299
- switchbot.onadvertisement = (ad: any) => {
300
- this.serviceData = ad.serviceData;
301
- this.movement = ad.serviceData.movement;
302
- this.doorState = ad.serviceData.doorState;
303
- this.lightLevel = ad.serviceData.lightLevel;
304
- this.battery = ad.serviceData.battery;
305
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
306
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} movement: ${ad.serviceData.movement}, doorState: `
307
- + `${ad.serviceData.doorState}, lightLevel: ${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}`);
308
-
309
- if (this.serviceData) {
310
- this.connected = true;
311
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
289
+ if (switchbot !== false) {
290
+ switchbot.startScan({
291
+ model: 'd',
292
+ id: this.device.bleMac,
293
+ }).then(() => {
294
+ // Set an event hander
295
+ switchbot.onadvertisement = (ad: any) => {
296
+ this.serviceData = ad.serviceData;
297
+ this.movement = ad.serviceData.movement;
298
+ this.doorState = ad.serviceData.doorState;
299
+ this.lightLevel = ad.serviceData.lightLevel;
300
+ this.battery = ad.serviceData.battery;
301
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
302
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} movement: ${ad.serviceData.movement}, doorState: `
303
+ + `${ad.serviceData.doorState}, lightLevel: ${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}`);
304
+
305
+ if (this.serviceData) {
306
+ this.connected = true;
307
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
308
+ } else {
309
+ this.connected = false;
310
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
311
+ }
312
+ };
313
+ // Wait 2 seconds
314
+ return switchbot.wait(this.scanDuration * 1000);
315
+ }).then(async () => {
316
+ // Stop to monitor
317
+ switchbot.stopScan();
318
+ if (this.connected) {
319
+ this.parseStatus();
320
+ this.updateHomeKitCharacteristics();
312
321
  } else {
313
- this.connected = false;
314
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
322
+ this.errorLog(`Contact Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
323
+ if (this.platform.config.credentials?.openToken) {
324
+ this.warnLog(`Contact Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
325
+ await this.openAPIRefreshStatus();
326
+ }
327
+ }
328
+ }).catch(async (e: any) => {
329
+ this.errorLog(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
330
+ if (this.deviceLogging === 'debug') {
331
+ this.errorLog(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
332
+ + ` Error Message: ${JSON.stringify(e.message)}`);
333
+ }
334
+ if (this.platform.debugMode) {
335
+ this.errorLog(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
336
+ + ` Error: ${JSON.stringify(e)}`);
315
337
  }
316
- };
317
- // Wait 2 seconds
318
- return switchbot.wait(this.scanDuration * 1000);
319
- }).then(async () => {
320
- // Stop to monitor
321
- switchbot.stopScan();
322
- if (this.connected) {
323
- this.parseStatus();
324
- this.updateHomeKitCharacteristics();
325
- } else {
326
- this.errorLog(`Contact Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
327
338
  if (this.platform.config.credentials?.openToken) {
328
339
  this.warnLog(`Contact Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
329
340
  await this.openAPIRefreshStatus();
330
341
  }
331
- }
332
- }).catch(async (e: any) => {
333
- this.errorLog(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
334
- if (this.deviceLogging === 'debug') {
335
- this.errorLog(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
336
- + ` Error Message: ${JSON.stringify(e.message)}`);
337
- }
338
- if (this.platform.debugMode) {
339
- this.errorLog(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
340
- + ` Error: ${JSON.stringify(e)}`);
341
- }
342
- if (this.platform.config.credentials?.openToken) {
343
- this.warnLog(`Contact Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
344
- await this.openAPIRefreshStatus();
345
- }
346
- this.apiError(e);
347
- });
342
+ this.apiError(e);
343
+ });
344
+ } else {
345
+ await this.BLEconnection();
346
+ }
347
+ }
348
+
349
+ private async BLEconnection() {
350
+ this.errorLog(`Contact Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
351
+ if (this.platform.config.credentials?.openToken) {
352
+ this.warnLog(`Contact Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
353
+ await this.openAPIRefreshStatus();
354
+ }
348
355
  }
349
356
 
350
357
  private async openAPIRefreshStatus() {
@@ -224,12 +224,12 @@ export class Curtain {
224
224
  if (this.device.scanDuration) {
225
225
  this.scanDuration = this.accessory.context.scanDuration = this.device.scanDuration;
226
226
  if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
227
- this.warnLog(`Bot: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
227
+ this.warnLog(`Curtain: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
228
228
  }
229
229
  } else {
230
230
  this.scanDuration = this.accessory.context.scanDuration = 1;
231
231
  if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
232
- this.warnLog(`Bot: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
232
+ this.warnLog(`Curtain: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
233
233
  }
234
234
  }
235
235
  }
@@ -254,16 +254,11 @@ export class Curtain {
254
254
  }
255
255
 
256
256
  private connectBLE() {
257
- let Switchbot: any;
258
- try {
259
- Switchbot = require('node-switchbot');
260
- const switchbot = new Switchbot();
261
- this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
262
- this.debugLog(`Bot: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
263
- return switchbot;
264
- } catch (e) {
265
- Switchbot = false;
266
- }
257
+ // Convert to BLE Address
258
+ this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
259
+ this.debugLog(`Curtain: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
260
+ const switchbot = this.platform.connectBLE();
261
+ return switchbot;
267
262
  }
268
263
 
269
264
  private minStep(): number {
@@ -459,60 +454,72 @@ export class Curtain {
459
454
  // eslint-disable-next-line @typescript-eslint/no-var-requires
460
455
  const switchbot = this.connectBLE();
461
456
  // Start to monitor advertisement packets
462
- switchbot.startScan({
463
- model: 'c',
464
- id: this.device.bleMac,
465
- }).then(() => {
466
- // Set an event hander
467
- switchbot.onadvertisement = (ad: any) => {
468
- this.serviceData = ad.serviceData;
469
- this.calibration = ad.serviceData.calibration;
470
- this.battery = ad.serviceData.battery;
471
- this.position = ad.serviceData.position;
472
- this.lightLevel = ad.serviceData.lightLevel;
473
- this.debugLog(`Curtain: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
474
- this.debugLog(`Curtain: ${this.accessory.displayName} calibration: ${ad.serviceData.calibration}, `
475
- + `position: ${ad.serviceData.position}, lightLevel: ${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}`);
476
-
477
- if (this.serviceData) {
478
- this.connected = true;
479
- this.debugLog(`Curtain: ${this.accessory.displayName} connected: ${this.connected}`);
457
+ if (switchbot !== false) {
458
+ switchbot.startScan({
459
+ model: 'c',
460
+ id: this.device.bleMac,
461
+ }).then(() => {
462
+ // Set an event hander
463
+ switchbot.onadvertisement = (ad: any) => {
464
+ this.serviceData = ad.serviceData;
465
+ this.calibration = ad.serviceData.calibration;
466
+ this.battery = ad.serviceData.battery;
467
+ this.position = ad.serviceData.position;
468
+ this.lightLevel = ad.serviceData.lightLevel;
469
+ this.debugLog(`Curtain: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
470
+ this.debugLog(`Curtain: ${this.accessory.displayName} calibration: ${ad.serviceData.calibration}, `
471
+ + `position: ${ad.serviceData.position}, lightLevel: ${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}`);
472
+
473
+ if (this.serviceData) {
474
+ this.connected = true;
475
+ this.debugLog(`Curtain: ${this.accessory.displayName} connected: ${this.connected}`);
476
+ } else {
477
+ this.connected = false;
478
+ this.debugLog(`Curtain: ${this.accessory.displayName} connected: ${this.connected}`);
479
+ }
480
+ };
481
+ // Wait 2 seconds
482
+ return switchbot.wait(this.scanDuration * 1000);
483
+ }).then(async () => {
484
+ // Stop to monitor
485
+ switchbot.stopScan();
486
+ if (this.connected) {
487
+ this.parseStatus();
488
+ this.updateHomeKitCharacteristics();
480
489
  } else {
481
- this.connected = false;
482
- this.debugLog(`Curtain: ${this.accessory.displayName} connected: ${this.connected}`);
490
+ this.errorLog(`Curtain: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
491
+ if (this.platform.config.credentials?.openToken) {
492
+ this.warnLog(`Curtain: ${this.accessory.displayName} Using OpenAPI Connection`);
493
+ await this.openAPIRefreshStatus();
494
+ }
495
+ }
496
+ }).catch(async (e: any) => {
497
+ this.errorLog(`Curtain: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
498
+ if (this.deviceLogging === 'debug') {
499
+ this.errorLog(`Curtain: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
500
+ + ` Error Message: ${JSON.stringify(e.message)}`);
501
+ }
502
+ if (this.platform.debugMode) {
503
+ this.errorLog(`Curtain: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
504
+ + ` Error: ${JSON.stringify(e)}`);
483
505
  }
484
- };
485
- // Wait 2 seconds
486
- return switchbot.wait(this.scanDuration * 1000);
487
- }).then(async () => {
488
- // Stop to monitor
489
- switchbot.stopScan();
490
- if (this.connected) {
491
- this.parseStatus();
492
- this.updateHomeKitCharacteristics();
493
- } else {
494
- this.errorLog(`Curtain: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
495
506
  if (this.platform.config.credentials?.openToken) {
496
507
  this.warnLog(`Curtain: ${this.accessory.displayName} Using OpenAPI Connection`);
497
508
  await this.openAPIRefreshStatus();
498
509
  }
499
- }
500
- }).catch(async (e: any) => {
501
- this.errorLog(`Curtain: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
502
- if (this.deviceLogging === 'debug') {
503
- this.errorLog(`Curtain: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
504
- + ` Error Message: ${JSON.stringify(e.message)}`);
505
- }
506
- if (this.platform.debugMode) {
507
- this.errorLog(`Curtain: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
508
- + ` Error: ${JSON.stringify(e)}`);
509
- }
510
- if (this.platform.config.credentials?.openToken) {
511
- this.warnLog(`Curtain: ${this.accessory.displayName} Using OpenAPI Connection`);
512
- await this.openAPIRefreshStatus();
513
- }
514
- this.apiError(e);
515
- });
510
+ this.apiError(e);
511
+ });
512
+ } else {
513
+ await this.BLEconnection();
514
+ }
515
+ }
516
+
517
+ private async BLEconnection() {
518
+ this.errorLog(`Curtain: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
519
+ if (this.platform.config.credentials?.openToken) {
520
+ this.warnLog(`Curtain: ${this.accessory.displayName} Using OpenAPI Connection`);
521
+ await this.openAPIRefreshStatus();
522
+ }
516
523
  }
517
524
 
518
525
  private async openAPIRefreshStatus() {
@@ -542,7 +549,7 @@ export class Curtain {
542
549
  if (this.device.ble) {
543
550
  await this.BLEpushChanges();
544
551
  } else {
545
- await this.OpenAPIpushChanges();
552
+ await this.openAPIpushChanges();
546
553
  }
547
554
  interval(5000)
548
555
  .pipe(skipWhile(() => this.curtainUpdateInProgress))
@@ -556,34 +563,42 @@ export class Curtain {
556
563
  if (this.TargetPosition !== this.CurrentPosition) {
557
564
  this.debugLog(`Curtain: ${this.accessory.displayName} BLE pushChanges`);
558
565
  const switchbot = this.connectBLE();
559
- switchbot.discover({ model: 'c', quick: true, id: this.device.bleMac }).then((device_list) => {
560
- this.infoLog(`${this.accessory.displayName} Target Position: ${this.TargetPosition}`);
561
- return device_list[0].runToPos(100 - Number(this.TargetPosition));
562
- }).then(() => {
563
- this.debugLog(`Curtain: ${this.accessory.displayName} Done.`);
564
- }).catch(async (e: any) => {
565
- this.errorLog(`Curtain: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
566
- if (this.deviceLogging === 'debug') {
567
- this.errorLog(`Curtain: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
568
- + ` Error Message: ${JSON.stringify(e.message)}`);
569
- }
570
- if (this.platform.debugMode) {
571
- this.errorLog(`Curtain: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
572
- + ` Error: ${JSON.stringify(e)}`);
573
- }
566
+ if (switchbot !== false) {
567
+ switchbot.discover({ model: 'c', quick: true, id: this.device.bleMac }).then((device_list) => {
568
+ this.infoLog(`${this.accessory.displayName} Target Position: ${this.TargetPosition}`);
569
+ return device_list[0].runToPos(100 - Number(this.TargetPosition));
570
+ }).then(() => {
571
+ this.debugLog(`Curtain: ${this.accessory.displayName} Done.`);
572
+ }).catch(async (e: any) => {
573
+ this.errorLog(`Curtain: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
574
+ if (this.deviceLogging === 'debug') {
575
+ this.errorLog(`Curtain: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
576
+ + ` Error Message: ${JSON.stringify(e.message)}`);
577
+ }
578
+ if (this.platform.debugMode) {
579
+ this.errorLog(`Curtain: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
580
+ + ` Error: ${JSON.stringify(e)}`);
581
+ }
582
+ if (this.platform.config.credentials?.openToken) {
583
+ this.warnLog(`Curtain: ${this.accessory.displayName} Using OpenAPI Connection`);
584
+ await this.openAPIpushChanges();
585
+ }
586
+ this.apiError(e);
587
+ });
588
+ } else {
589
+ this.errorLog(`Curtain: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
574
590
  if (this.platform.config.credentials?.openToken) {
575
591
  this.warnLog(`Curtain: ${this.accessory.displayName} Using OpenAPI Connection`);
576
- await this.OpenAPIpushChanges();
592
+ await this.openAPIpushChanges();
577
593
  }
578
- this.apiError(e);
579
- });
594
+ }
580
595
  } else {
581
596
  this.debugLog(`Curtain: ${this.accessory.displayName} No BLE Changes, CurrentPosition & TargetPosition Are the Same.`
582
597
  + ` CurrentPosition: ${this.CurrentPosition}, TargetPosition ${this.TargetPosition}`);
583
598
  }
584
599
  }
585
600
 
586
- private async OpenAPIpushChanges() {
601
+ private async openAPIpushChanges() {
587
602
  if (this.platform.config.credentials?.openToken) {
588
603
  try {
589
604
  this.debugLog(`Curtain: ${this.accessory.displayName} OpenAPI pushChanges`);