@switchbot/homebridge-switchbot 1.6.2-beta.1 → 1.6.2-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.
@@ -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,73 @@ 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
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} platform.Switchbot: ${JSON.stringify(switchbot)}`);
290
+ if (switchbot) {
291
+ switchbot.startScan({
292
+ model: 'd',
293
+ id: this.device.bleMac,
294
+ }).then(() => {
295
+ // Set an event hander
296
+ switchbot.onadvertisement = (ad: any) => {
297
+ this.serviceData = ad.serviceData;
298
+ this.movement = ad.serviceData.movement;
299
+ this.doorState = ad.serviceData.doorState;
300
+ this.lightLevel = ad.serviceData.lightLevel;
301
+ this.battery = ad.serviceData.battery;
302
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
303
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} movement: ${ad.serviceData.movement}, doorState: `
304
+ + `${ad.serviceData.doorState}, lightLevel: ${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}`);
305
+
306
+ if (this.serviceData) {
307
+ this.connected = true;
308
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
309
+ } else {
310
+ this.connected = false;
311
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
312
+ }
313
+ };
314
+ // Wait 2 seconds
315
+ return switchbot.wait(this.scanDuration * 1000);
316
+ }).then(async () => {
317
+ // Stop to monitor
318
+ switchbot.stopScan();
319
+ if (this.connected) {
320
+ this.parseStatus();
321
+ this.updateHomeKitCharacteristics();
307
322
  } else {
308
- this.connected = false;
309
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
323
+ this.errorLog(`Contact Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
324
+ if (this.platform.config.credentials?.openToken) {
325
+ this.warnLog(`Contact Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
326
+ await this.openAPIRefreshStatus();
327
+ }
328
+ }
329
+ }).catch(async (e: any) => {
330
+ this.errorLog(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
331
+ if (this.deviceLogging === 'debug') {
332
+ this.errorLog(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
333
+ + ` Error Message: ${JSON.stringify(e.message)}`);
334
+ }
335
+ if (this.platform.debugMode) {
336
+ this.errorLog(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
337
+ + ` Error: ${JSON.stringify(e)}`);
310
338
  }
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
339
  if (this.platform.config.credentials?.openToken) {
323
340
  this.warnLog(`Contact Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
324
341
  await this.openAPIRefreshStatus();
325
342
  }
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
- });
343
+ this.apiError(e);
344
+ });
345
+ } else {
346
+ await this.BLEconnection();
347
+ }
348
+ }
349
+
350
+ private async BLEconnection() {
351
+ this.errorLog(`Contact Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
352
+ if (this.platform.config.credentials?.openToken) {
353
+ this.warnLog(`Contact Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
354
+ await this.openAPIRefreshStatus();
355
+ }
343
356
  }
344
357
 
345
358
  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,73 @@ 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
+ this.debugLog(`Curtain: ${this.accessory.displayName} platform.Switchbot: ${JSON.stringify(switchbot)}`);
458
+ if (switchbot) {
459
+ switchbot.startScan({
460
+ model: 'c',
461
+ id: this.device.bleMac,
462
+ }).then(() => {
463
+ // Set an event hander
464
+ switchbot.onadvertisement = (ad: any) => {
465
+ this.serviceData = ad.serviceData;
466
+ this.calibration = ad.serviceData.calibration;
467
+ this.battery = ad.serviceData.battery;
468
+ this.position = ad.serviceData.position;
469
+ this.lightLevel = ad.serviceData.lightLevel;
470
+ this.debugLog(`Curtain: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
471
+ this.debugLog(`Curtain: ${this.accessory.displayName} calibration: ${ad.serviceData.calibration}, `
472
+ + `position: ${ad.serviceData.position}, lightLevel: ${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}`);
473
+
474
+ if (this.serviceData) {
475
+ this.connected = true;
476
+ this.debugLog(`Curtain: ${this.accessory.displayName} connected: ${this.connected}`);
477
+ } else {
478
+ this.connected = false;
479
+ this.debugLog(`Curtain: ${this.accessory.displayName} connected: ${this.connected}`);
480
+ }
481
+ };
482
+ // Wait 2 seconds
483
+ return switchbot.wait(this.scanDuration * 1000);
484
+ }).then(async () => {
485
+ // Stop to monitor
486
+ switchbot.stopScan();
487
+ if (this.connected) {
488
+ this.parseStatus();
489
+ this.updateHomeKitCharacteristics();
475
490
  } else {
476
- this.connected = false;
477
- this.debugLog(`Curtain: ${this.accessory.displayName} connected: ${this.connected}`);
491
+ this.errorLog(`Curtain: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
492
+ if (this.platform.config.credentials?.openToken) {
493
+ this.warnLog(`Curtain: ${this.accessory.displayName} Using OpenAPI Connection`);
494
+ await this.openAPIRefreshStatus();
495
+ }
496
+ }
497
+ }).catch(async (e: any) => {
498
+ this.errorLog(`Curtain: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
499
+ if (this.deviceLogging === 'debug') {
500
+ this.errorLog(`Curtain: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
501
+ + ` Error Message: ${JSON.stringify(e.message)}`);
502
+ }
503
+ if (this.platform.debugMode) {
504
+ this.errorLog(`Curtain: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
505
+ + ` Error: ${JSON.stringify(e)}`);
478
506
  }
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
507
  if (this.platform.config.credentials?.openToken) {
491
508
  this.warnLog(`Curtain: ${this.accessory.displayName} Using OpenAPI Connection`);
492
509
  await this.openAPIRefreshStatus();
493
510
  }
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
- });
511
+ this.apiError(e);
512
+ });
513
+ } else {
514
+ await this.BLEconnection();
515
+ }
516
+ }
517
+
518
+ private async BLEconnection() {
519
+ this.errorLog(`Curtain: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
520
+ if (this.platform.config.credentials?.openToken) {
521
+ this.warnLog(`Curtain: ${this.accessory.displayName} Using OpenAPI Connection`);
522
+ await this.openAPIRefreshStatus();
523
+ }
511
524
  }
512
525
 
513
526
  private async openAPIRefreshStatus() {
@@ -537,7 +550,7 @@ export class Curtain {
537
550
  if (this.device.ble) {
538
551
  await this.BLEpushChanges();
539
552
  } else {
540
- await this.OpenAPIpushChanges();
553
+ await this.openAPIpushChanges();
541
554
  }
542
555
  interval(5000)
543
556
  .pipe(skipWhile(() => this.curtainUpdateInProgress))
@@ -551,34 +564,43 @@ export class Curtain {
551
564
  if (this.TargetPosition !== this.CurrentPosition) {
552
565
  this.debugLog(`Curtain: ${this.accessory.displayName} BLE pushChanges`);
553
566
  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
- }
567
+ this.debugLog(`Curtain: ${this.accessory.displayName} platform.Switchbot: ${JSON.stringify(switchbot)}`);
568
+ if (switchbot) {
569
+ switchbot.discover({ model: 'c', quick: true, id: this.device.bleMac }).then((device_list) => {
570
+ this.infoLog(`${this.accessory.displayName} Target Position: ${this.TargetPosition}`);
571
+ return device_list[0].runToPos(100 - Number(this.TargetPosition));
572
+ }).then(() => {
573
+ this.debugLog(`Curtain: ${this.accessory.displayName} Done.`);
574
+ }).catch(async (e: any) => {
575
+ this.errorLog(`Curtain: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
576
+ if (this.deviceLogging === 'debug') {
577
+ this.errorLog(`Curtain: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
578
+ + ` Error Message: ${JSON.stringify(e.message)}`);
579
+ }
580
+ if (this.platform.debugMode) {
581
+ this.errorLog(`Curtain: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
582
+ + ` Error: ${JSON.stringify(e)}`);
583
+ }
584
+ if (this.platform.config.credentials?.openToken) {
585
+ this.warnLog(`Curtain: ${this.accessory.displayName} Using OpenAPI Connection`);
586
+ await this.openAPIpushChanges();
587
+ }
588
+ this.apiError(e);
589
+ });
590
+ } else {
591
+ this.errorLog(`Curtain: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
569
592
  if (this.platform.config.credentials?.openToken) {
570
593
  this.warnLog(`Curtain: ${this.accessory.displayName} Using OpenAPI Connection`);
571
- await this.OpenAPIpushChanges();
594
+ await this.openAPIpushChanges();
572
595
  }
573
- this.apiError(e);
574
- });
596
+ }
575
597
  } else {
576
598
  this.debugLog(`Curtain: ${this.accessory.displayName} No BLE Changes, CurrentPosition & TargetPosition Are the Same.`
577
599
  + ` CurrentPosition: ${this.CurrentPosition}, TargetPosition ${this.TargetPosition}`);
578
600
  }
579
601
  }
580
602
 
581
- private async OpenAPIpushChanges() {
603
+ private async openAPIpushChanges() {
582
604
  if (this.platform.config.credentials?.openToken) {
583
605
  try {
584
606
  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,68 @@ 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
+ this.debugLog(`Humidifier: ${this.accessory.displayName} platform.Switchbot: ${JSON.stringify(switchbot)}`);
356
+ if (switchbot) {
357
+ switchbot.startScan({
358
+ model: 'e',
359
+ id: this.device.bleMac,
360
+ }).then(() => {
361
+ // Set an event hander
362
+ switchbot.onadvertisement = (ad: ad) => {
363
+ this.serviceData = ad.serviceData;
364
+ this.autoMode = ad.serviceData.autoMode;
365
+ this.onState = ad.serviceData.onState;
366
+ this.percentage = ad.serviceData.percentage;
367
+ this.debugLog(`Humidifier: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
368
+ this.debugLog(`Humidifier: ${this.accessory.displayName} model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName},`
369
+ + `autoMode: ${ad.serviceData.autoMode}, onState: ${ad.serviceData.onState}, percentage: ${ad.serviceData.percentage}`);
370
+
371
+ if (this.serviceData) {
372
+ this.connected = true;
373
+ this.debugLog(`Humidifier: ${this.accessory.displayName} connected: ${this.connected}`);
374
+ } else {
375
+ this.connected = false;
376
+ this.debugLog(`Humidifier: ${this.accessory.displayName} connected: ${this.connected}`);
377
+ }
378
+ };
379
+ // Wait 2 seconds
380
+ return switchbot.wait(this.scanDuration * 1000);
381
+ }).then(async () => {
382
+ // Stop to monitor
383
+ switchbot.stopScan();
384
+ if (this.connected) {
385
+ this.parseStatus();
386
+ this.updateHomeKitCharacteristics();
372
387
  } else {
373
- this.connected = false;
374
- this.debugLog(`Humidifier: ${this.accessory.displayName} connected: ${this.connected}`);
388
+ await this.BLEconnection();
389
+ }
390
+ }).catch(async (e: any) => {
391
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
392
+ if (this.deviceLogging === 'debug') {
393
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
394
+ + ` Error Message: ${JSON.stringify(e.message)}`);
395
+ }
396
+ if (this.platform.debugMode) {
397
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
398
+ + ` Error: ${JSON.stringify(e)}`);
375
399
  }
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
400
  if (this.platform.config.credentials?.openToken) {
388
401
  this.warnLog(`Humidifier: ${this.accessory.displayName} Using OpenAPI Connection`);
389
402
  await this.openAPIRefreshStatus();
390
403
  }
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
- });
404
+ this.apiError(e);
405
+ });
406
+ } else {
407
+ await this.BLEconnection();
408
+ }
409
+ }
410
+
411
+ private async BLEconnection() {
412
+ this.errorLog(`Humidifier: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
413
+ if (this.platform.config.credentials?.openToken) {
414
+ this.warnLog(`Humidifier: ${this.accessory.displayName} Using OpenAPI Connection`);
415
+ await this.openAPIRefreshStatus();
416
+ }
408
417
  }
409
418
 
410
419
  private async openAPIRefreshStatus() {
@@ -441,7 +450,7 @@ export class Humidifier {
441
450
  //if (this.device.ble) {
442
451
  // await this.BLEpushChanges();
443
452
  //} else {
444
- await this.OpenAPIpushChanges();
453
+ await this.openAPIpushChanges();
445
454
  //}
446
455
  interval(5000)
447
456
  .pipe(skipWhile(() => this.humidifierUpdateInProgress))
@@ -454,30 +463,39 @@ export class Humidifier {
454
463
  private async BLEpushChanges() {
455
464
  this.debugLog(`Humidifier: ${this.accessory.displayName} BLE pushChanges`);
456
465
  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
- }
466
+ this.debugLog(`Humidifier: ${this.accessory.displayName} platform.Switchbot: ${JSON.stringify(switchbot)}`);
467
+ if (switchbot) {
468
+ switchbot.discover({ model: 'e', quick: true, id: this.device.bleMac }).then((device_list) => {
469
+ this.infoLog(`${this.accessory.displayName} Target Position: ${this.Active}`);
470
+ return device_list[0].percentage(this.RelativeHumidityHumidifierThreshold);
471
+ }).then(() => {
472
+ this.debugLog(`Humidifier: ${this.accessory.displayName} Done.`);
473
+ }).catch(async (e: any) => {
474
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
475
+ if (this.deviceLogging === 'debug') {
476
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
477
+ + ` Error Message: ${JSON.stringify(e.message)}`);
478
+ }
479
+ if (this.platform.debugMode) {
480
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
481
+ + ` Error: ${JSON.stringify(e)}`);
482
+ }
483
+ if (this.platform.config.credentials?.openToken) {
484
+ this.warnLog(`Humidifier: ${this.accessory.displayName} Using OpenAPI Connection`);
485
+ await this.openAPIpushChanges();
486
+ }
487
+ this.apiError(e);
488
+ });
489
+ } else {
490
+ this.errorLog(`Humidifier: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
472
491
  if (this.platform.config.credentials?.openToken) {
473
492
  this.warnLog(`Humidifier: ${this.accessory.displayName} Using OpenAPI Connection`);
474
- await this.OpenAPIpushChanges();
493
+ await this.openAPIpushChanges();
475
494
  }
476
- this.apiError(e);
477
- });
495
+ }
478
496
  }
479
497
 
480
- private async OpenAPIpushChanges() {
498
+ private async openAPIpushChanges() {
481
499
  if (this.platform.config.credentials?.openToken) {
482
500
  this.debugLog(`Humidifier: ${this.accessory.displayName} OpenAPI pushChanges`);
483
501
  if (