@switchbot/homebridge-switchbot 1.6.2-beta.1 → 1.6.4-beta.0

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.
@@ -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
  }
@@ -276,7 +276,7 @@ export class Contact {
276
276
  private connectBLE() {
277
277
  // Convert to BLE Address
278
278
  this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
279
- this.debugLog(`Bot: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
279
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
280
280
  const switchbot = this.platform.connectBLE();
281
281
  return switchbot;
282
282
  }
@@ -286,60 +286,72 @@ export class Contact {
286
286
  // eslint-disable-next-line @typescript-eslint/no-var-requires
287
287
  const switchbot = this.connectBLE();
288
288
  // Start to monitor advertisement packets
289
- switchbot.startScan({
290
- model: 'd',
291
- id: this.device.bleMac,
292
- }).then(() => {
293
- // Set an event hander
294
- switchbot.onadvertisement = (ad: any) => {
295
- this.serviceData = ad.serviceData;
296
- this.movement = ad.serviceData.movement;
297
- this.doorState = ad.serviceData.doorState;
298
- this.lightLevel = ad.serviceData.lightLevel;
299
- this.battery = ad.serviceData.battery;
300
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
301
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} movement: ${ad.serviceData.movement}, doorState: `
302
- + `${ad.serviceData.doorState}, lightLevel: ${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}`);
303
-
304
- if (this.serviceData) {
305
- this.connected = true;
306
- 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();
307
321
  } else {
308
- this.connected = false;
309
- 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)}`);
310
337
  }
311
- };
312
- // Wait 2 seconds
313
- return switchbot.wait(this.scanDuration * 1000);
314
- }).then(async () => {
315
- // Stop to monitor
316
- switchbot.stopScan();
317
- if (this.connected) {
318
- this.parseStatus();
319
- this.updateHomeKitCharacteristics();
320
- } else {
321
- this.errorLog(`Contact Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
322
338
  if (this.platform.config.credentials?.openToken) {
323
339
  this.warnLog(`Contact Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
324
340
  await this.openAPIRefreshStatus();
325
341
  }
326
- }
327
- }).catch(async (e: any) => {
328
- this.errorLog(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
329
- if (this.deviceLogging === 'debug') {
330
- this.errorLog(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
331
- + ` Error Message: ${JSON.stringify(e.message)}`);
332
- }
333
- if (this.platform.debugMode) {
334
- this.errorLog(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
335
- + ` Error: ${JSON.stringify(e)}`);
336
- }
337
- if (this.platform.config.credentials?.openToken) {
338
- this.warnLog(`Contact Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
339
- await this.openAPIRefreshStatus();
340
- }
341
- this.apiError(e);
342
- });
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
+ }
343
355
  }
344
356
 
345
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
  }
@@ -256,7 +256,7 @@ export class Curtain {
256
256
  private connectBLE() {
257
257
  // Convert to BLE Address
258
258
  this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
259
- this.debugLog(`Bot: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
259
+ this.debugLog(`Curtain: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
260
260
  const switchbot = this.platform.connectBLE();
261
261
  return switchbot;
262
262
  }
@@ -454,60 +454,72 @@ export class Curtain {
454
454
  // eslint-disable-next-line @typescript-eslint/no-var-requires
455
455
  const switchbot = this.connectBLE();
456
456
  // Start to monitor advertisement packets
457
- switchbot.startScan({
458
- model: 'c',
459
- id: this.device.bleMac,
460
- }).then(() => {
461
- // Set an event hander
462
- switchbot.onadvertisement = (ad: any) => {
463
- this.serviceData = ad.serviceData;
464
- this.calibration = ad.serviceData.calibration;
465
- this.battery = ad.serviceData.battery;
466
- this.position = ad.serviceData.position;
467
- this.lightLevel = ad.serviceData.lightLevel;
468
- this.debugLog(`Curtain: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
469
- this.debugLog(`Curtain: ${this.accessory.displayName} calibration: ${ad.serviceData.calibration}, `
470
- + `position: ${ad.serviceData.position}, lightLevel: ${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}`);
471
-
472
- if (this.serviceData) {
473
- this.connected = true;
474
- 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();
475
489
  } else {
476
- this.connected = false;
477
- 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)}`);
478
505
  }
479
- };
480
- // Wait 2 seconds
481
- return switchbot.wait(this.scanDuration * 1000);
482
- }).then(async () => {
483
- // Stop to monitor
484
- switchbot.stopScan();
485
- if (this.connected) {
486
- this.parseStatus();
487
- this.updateHomeKitCharacteristics();
488
- } else {
489
- this.errorLog(`Curtain: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
490
506
  if (this.platform.config.credentials?.openToken) {
491
507
  this.warnLog(`Curtain: ${this.accessory.displayName} Using OpenAPI Connection`);
492
508
  await this.openAPIRefreshStatus();
493
509
  }
494
- }
495
- }).catch(async (e: any) => {
496
- this.errorLog(`Curtain: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
497
- if (this.deviceLogging === 'debug') {
498
- this.errorLog(`Curtain: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
499
- + ` Error Message: ${JSON.stringify(e.message)}`);
500
- }
501
- if (this.platform.debugMode) {
502
- this.errorLog(`Curtain: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
503
- + ` Error: ${JSON.stringify(e)}`);
504
- }
505
- if (this.platform.config.credentials?.openToken) {
506
- this.warnLog(`Curtain: ${this.accessory.displayName} Using OpenAPI Connection`);
507
- await this.openAPIRefreshStatus();
508
- }
509
- this.apiError(e);
510
- });
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
+ }
511
523
  }
512
524
 
513
525
  private async openAPIRefreshStatus() {
@@ -537,7 +549,7 @@ export class Curtain {
537
549
  if (this.device.ble) {
538
550
  await this.BLEpushChanges();
539
551
  } else {
540
- await this.OpenAPIpushChanges();
552
+ await this.openAPIpushChanges();
541
553
  }
542
554
  interval(5000)
543
555
  .pipe(skipWhile(() => this.curtainUpdateInProgress))
@@ -551,34 +563,42 @@ export class Curtain {
551
563
  if (this.TargetPosition !== this.CurrentPosition) {
552
564
  this.debugLog(`Curtain: ${this.accessory.displayName} BLE pushChanges`);
553
565
  const switchbot = this.connectBLE();
554
- switchbot.discover({ model: 'c', quick: true, id: this.device.bleMac }).then((device_list) => {
555
- this.infoLog(`${this.accessory.displayName} Target Position: ${this.TargetPosition}`);
556
- return device_list[0].runToPos(100 - Number(this.TargetPosition));
557
- }).then(() => {
558
- this.debugLog(`Curtain: ${this.accessory.displayName} Done.`);
559
- }).catch(async (e: any) => {
560
- this.errorLog(`Curtain: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
561
- if (this.deviceLogging === 'debug') {
562
- this.errorLog(`Curtain: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
563
- + ` Error Message: ${JSON.stringify(e.message)}`);
564
- }
565
- if (this.platform.debugMode) {
566
- this.errorLog(`Curtain: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
567
- + ` Error: ${JSON.stringify(e)}`);
568
- }
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`);
569
590
  if (this.platform.config.credentials?.openToken) {
570
591
  this.warnLog(`Curtain: ${this.accessory.displayName} Using OpenAPI Connection`);
571
- await this.OpenAPIpushChanges();
592
+ await this.openAPIpushChanges();
572
593
  }
573
- this.apiError(e);
574
- });
594
+ }
575
595
  } else {
576
596
  this.debugLog(`Curtain: ${this.accessory.displayName} No BLE Changes, CurrentPosition & TargetPosition Are the Same.`
577
597
  + ` CurrentPosition: ${this.CurrentPosition}, TargetPosition ${this.TargetPosition}`);
578
598
  }
579
599
  }
580
600
 
581
- private async OpenAPIpushChanges() {
601
+ private async openAPIpushChanges() {
582
602
  if (this.platform.config.credentials?.openToken) {
583
603
  try {
584
604
  this.debugLog(`Curtain: ${this.accessory.displayName} OpenAPI pushChanges`);
@@ -209,12 +209,12 @@ export class Humidifier {
209
209
  if (this.device.scanDuration) {
210
210
  this.scanDuration = this.accessory.context.scanDuration = this.device.scanDuration;
211
211
  if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
212
- this.warnLog(`Bot: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
212
+ this.warnLog(`Humidifier: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
213
213
  }
214
214
  } else {
215
215
  this.scanDuration = this.accessory.context.scanDuration = 1;
216
216
  if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
217
- this.warnLog(`Bot: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
217
+ this.warnLog(`Humidifier: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
218
218
  }
219
219
  }
220
220
  }
@@ -342,7 +342,7 @@ export class Humidifier {
342
342
  private connectBLE() {
343
343
  // Convert to BLE Address
344
344
  this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
345
- this.debugLog(`Bot: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
345
+ this.debugLog(`Humidifier: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
346
346
  const switchbot = this.platform.connectBLE();
347
347
  return switchbot;
348
348
  }
@@ -352,59 +352,67 @@ export class Humidifier {
352
352
  // eslint-disable-next-line @typescript-eslint/no-var-requires
353
353
  const switchbot = this.connectBLE();
354
354
  // Start to monitor advertisement packets
355
- switchbot.startScan({
356
- model: 'e',
357
- id: this.device.bleMac,
358
- }).then(() => {
359
- // Set an event hander
360
- switchbot.onadvertisement = (ad: ad) => {
361
- this.serviceData = ad.serviceData;
362
- this.autoMode = ad.serviceData.autoMode;
363
- this.onState = ad.serviceData.onState;
364
- this.percentage = ad.serviceData.percentage;
365
- this.debugLog(`Humidifier: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
366
- this.debugLog(`Humidifier: ${this.accessory.displayName} model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName},`
367
- + `autoMode: ${ad.serviceData.autoMode}, onState: ${ad.serviceData.onState}, percentage: ${ad.serviceData.percentage}`);
368
-
369
- if (this.serviceData) {
370
- this.connected = true;
371
- this.debugLog(`Humidifier: ${this.accessory.displayName} connected: ${this.connected}`);
355
+ if (switchbot !== false) {
356
+ switchbot.startScan({
357
+ model: 'e',
358
+ id: this.device.bleMac,
359
+ }).then(() => {
360
+ // Set an event hander
361
+ switchbot.onadvertisement = (ad: ad) => {
362
+ this.serviceData = ad.serviceData;
363
+ this.autoMode = ad.serviceData.autoMode;
364
+ this.onState = ad.serviceData.onState;
365
+ this.percentage = ad.serviceData.percentage;
366
+ this.debugLog(`Humidifier: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
367
+ this.debugLog(`Humidifier: ${this.accessory.displayName} model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName},`
368
+ + `autoMode: ${ad.serviceData.autoMode}, onState: ${ad.serviceData.onState}, percentage: ${ad.serviceData.percentage}`);
369
+
370
+ if (this.serviceData) {
371
+ this.connected = true;
372
+ this.debugLog(`Humidifier: ${this.accessory.displayName} connected: ${this.connected}`);
373
+ } else {
374
+ this.connected = false;
375
+ this.debugLog(`Humidifier: ${this.accessory.displayName} connected: ${this.connected}`);
376
+ }
377
+ };
378
+ // Wait 2 seconds
379
+ return switchbot.wait(this.scanDuration * 1000);
380
+ }).then(async () => {
381
+ // Stop to monitor
382
+ switchbot.stopScan();
383
+ if (this.connected) {
384
+ this.parseStatus();
385
+ this.updateHomeKitCharacteristics();
372
386
  } else {
373
- this.connected = false;
374
- this.debugLog(`Humidifier: ${this.accessory.displayName} connected: ${this.connected}`);
387
+ await this.BLEconnection();
388
+ }
389
+ }).catch(async (e: any) => {
390
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
391
+ if (this.deviceLogging === 'debug') {
392
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
393
+ + ` Error Message: ${JSON.stringify(e.message)}`);
394
+ }
395
+ if (this.platform.debugMode) {
396
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
397
+ + ` Error: ${JSON.stringify(e)}`);
375
398
  }
376
- };
377
- // Wait 2 seconds
378
- return switchbot.wait(this.scanDuration * 1000);
379
- }).then(async () => {
380
- // Stop to monitor
381
- switchbot.stopScan();
382
- if (this.connected) {
383
- this.parseStatus();
384
- this.updateHomeKitCharacteristics();
385
- } else {
386
- this.errorLog(`Humidifier: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
387
399
  if (this.platform.config.credentials?.openToken) {
388
400
  this.warnLog(`Humidifier: ${this.accessory.displayName} Using OpenAPI Connection`);
389
401
  await this.openAPIRefreshStatus();
390
402
  }
391
- }
392
- }).catch(async (e: any) => {
393
- this.errorLog(`Humidifier: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
394
- if (this.deviceLogging === 'debug') {
395
- this.errorLog(`Humidifier: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
396
- + ` Error Message: ${JSON.stringify(e.message)}`);
397
- }
398
- if (this.platform.debugMode) {
399
- this.errorLog(`Humidifier: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
400
- + ` Error: ${JSON.stringify(e)}`);
401
- }
402
- if (this.platform.config.credentials?.openToken) {
403
- this.warnLog(`Humidifier: ${this.accessory.displayName} Using OpenAPI Connection`);
404
- await this.openAPIRefreshStatus();
405
- }
406
- this.apiError(e);
407
- });
403
+ this.apiError(e);
404
+ });
405
+ } else {
406
+ await this.BLEconnection();
407
+ }
408
+ }
409
+
410
+ private async BLEconnection() {
411
+ this.errorLog(`Humidifier: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
412
+ if (this.platform.config.credentials?.openToken) {
413
+ this.warnLog(`Humidifier: ${this.accessory.displayName} Using OpenAPI Connection`);
414
+ await this.openAPIRefreshStatus();
415
+ }
408
416
  }
409
417
 
410
418
  private async openAPIRefreshStatus() {
@@ -441,7 +449,7 @@ export class Humidifier {
441
449
  //if (this.device.ble) {
442
450
  // await this.BLEpushChanges();
443
451
  //} else {
444
- await this.OpenAPIpushChanges();
452
+ await this.openAPIpushChanges();
445
453
  //}
446
454
  interval(5000)
447
455
  .pipe(skipWhile(() => this.humidifierUpdateInProgress))
@@ -454,30 +462,38 @@ export class Humidifier {
454
462
  private async BLEpushChanges() {
455
463
  this.debugLog(`Humidifier: ${this.accessory.displayName} BLE pushChanges`);
456
464
  const switchbot = this.connectBLE();
457
- switchbot.discover({ model: 'e', quick: true, id: this.device.bleMac }).then((device_list) => {
458
- this.infoLog(`${this.accessory.displayName} Target Position: ${this.Active}`);
459
- return device_list[0].percentage(this.RelativeHumidityHumidifierThreshold);
460
- }).then(() => {
461
- this.debugLog(`Humidifier: ${this.accessory.displayName} Done.`);
462
- }).catch(async (e: any) => {
463
- this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
464
- if (this.deviceLogging === 'debug') {
465
- this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
466
- + ` Error Message: ${JSON.stringify(e.message)}`);
467
- }
468
- if (this.platform.debugMode) {
469
- this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
470
- + ` Error: ${JSON.stringify(e)}`);
471
- }
465
+ if (switchbot !== false) {
466
+ switchbot.discover({ model: 'e', quick: true, id: this.device.bleMac }).then((device_list) => {
467
+ this.infoLog(`${this.accessory.displayName} Target Position: ${this.Active}`);
468
+ return device_list[0].percentage(this.RelativeHumidityHumidifierThreshold);
469
+ }).then(() => {
470
+ this.debugLog(`Humidifier: ${this.accessory.displayName} Done.`);
471
+ }).catch(async (e: any) => {
472
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
473
+ if (this.deviceLogging === 'debug') {
474
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
475
+ + ` Error Message: ${JSON.stringify(e.message)}`);
476
+ }
477
+ if (this.platform.debugMode) {
478
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
479
+ + ` Error: ${JSON.stringify(e)}`);
480
+ }
481
+ if (this.platform.config.credentials?.openToken) {
482
+ this.warnLog(`Humidifier: ${this.accessory.displayName} Using OpenAPI Connection`);
483
+ await this.openAPIpushChanges();
484
+ }
485
+ this.apiError(e);
486
+ });
487
+ } else {
488
+ this.errorLog(`Humidifier: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
472
489
  if (this.platform.config.credentials?.openToken) {
473
490
  this.warnLog(`Humidifier: ${this.accessory.displayName} Using OpenAPI Connection`);
474
- await this.OpenAPIpushChanges();
491
+ await this.openAPIpushChanges();
475
492
  }
476
- this.apiError(e);
477
- });
493
+ }
478
494
  }
479
495
 
480
- private async OpenAPIpushChanges() {
496
+ private async openAPIpushChanges() {
481
497
  if (this.platform.config.credentials?.openToken) {
482
498
  this.debugLog(`Humidifier: ${this.accessory.displayName} OpenAPI pushChanges`);
483
499
  if (