@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.
- package/CHANGELOG.md +17 -0
- package/dist/devices/bots.d.ts +1 -0
- package/dist/devices/bots.d.ts.map +1 -1
- package/dist/devices/bots.js +131 -120
- package/dist/devices/bots.js.map +1 -1
- package/dist/devices/contact.d.ts +1 -0
- package/dist/devices/contact.d.ts.map +1 -1
- package/dist/devices/contact.js +67 -61
- package/dist/devices/contact.js.map +1 -1
- package/dist/devices/curtains.d.ts +2 -1
- package/dist/devices/curtains.d.ts.map +1 -1
- package/dist/devices/curtains.js +96 -81
- package/dist/devices/curtains.js.map +1 -1
- package/dist/devices/humidifiers.d.ts +2 -1
- package/dist/devices/humidifiers.d.ts.map +1 -1
- package/dist/devices/humidifiers.js +91 -80
- package/dist/devices/humidifiers.js.map +1 -1
- package/dist/devices/meters.d.ts +1 -0
- package/dist/devices/meters.d.ts.map +1 -1
- package/dist/devices/meters.js +70 -68
- package/dist/devices/meters.js.map +1 -1
- package/dist/devices/motion.d.ts +1 -0
- package/dist/devices/motion.d.ts.map +1 -1
- package/dist/devices/motion.js +62 -60
- package/dist/devices/motion.js.map +1 -1
- package/dist/devices/plugs.js +2 -2
- package/dist/devices/plugs.js.map +1 -1
- package/dist/platform.d.ts +2 -0
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +12 -0
- package/dist/platform.js.map +1 -1
- package/package.json +71 -71
- package/src/devices/bots.ts +110 -100
- package/src/devices/contact.ts +67 -60
- package/src/devices/curtains.ts +95 -80
- package/src/devices/humidifiers.ts +90 -79
- package/src/devices/meters.ts +70 -67
- package/src/devices/motion.ts +62 -59
- package/src/devices/plugs.ts +2 -2
- package/src/platform.ts +13 -0
|
@@ -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(`
|
|
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(`
|
|
217
|
+
this.warnLog(`Humidifier: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
|
|
218
218
|
}
|
|
219
219
|
}
|
|
220
220
|
}
|
|
@@ -340,16 +340,11 @@ export class Humidifier {
|
|
|
340
340
|
}
|
|
341
341
|
|
|
342
342
|
private connectBLE() {
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
this.debugLog(`Bot: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
349
|
-
return switchbot;
|
|
350
|
-
} catch (e) {
|
|
351
|
-
Switchbot = false;
|
|
352
|
-
}
|
|
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();
|
|
347
|
+
return switchbot;
|
|
353
348
|
}
|
|
354
349
|
|
|
355
350
|
private async BLERefreshStatus() {
|
|
@@ -357,59 +352,67 @@ export class Humidifier {
|
|
|
357
352
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
358
353
|
const switchbot = this.connectBLE();
|
|
359
354
|
// Start to monitor advertisement packets
|
|
360
|
-
switchbot
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
this.
|
|
376
|
-
|
|
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();
|
|
377
386
|
} else {
|
|
378
|
-
this.
|
|
379
|
-
|
|
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)}`);
|
|
380
398
|
}
|
|
381
|
-
};
|
|
382
|
-
// Wait 2 seconds
|
|
383
|
-
return switchbot.wait(this.scanDuration * 1000);
|
|
384
|
-
}).then(async () => {
|
|
385
|
-
// Stop to monitor
|
|
386
|
-
switchbot.stopScan();
|
|
387
|
-
if (this.connected) {
|
|
388
|
-
this.parseStatus();
|
|
389
|
-
this.updateHomeKitCharacteristics();
|
|
390
|
-
} else {
|
|
391
|
-
this.errorLog(`Humidifier: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
|
|
392
399
|
if (this.platform.config.credentials?.openToken) {
|
|
393
400
|
this.warnLog(`Humidifier: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
394
401
|
await this.openAPIRefreshStatus();
|
|
395
402
|
}
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
await this.openAPIRefreshStatus();
|
|
410
|
-
}
|
|
411
|
-
this.apiError(e);
|
|
412
|
-
});
|
|
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
|
+
}
|
|
413
416
|
}
|
|
414
417
|
|
|
415
418
|
private async openAPIRefreshStatus() {
|
|
@@ -446,7 +449,7 @@ export class Humidifier {
|
|
|
446
449
|
//if (this.device.ble) {
|
|
447
450
|
// await this.BLEpushChanges();
|
|
448
451
|
//} else {
|
|
449
|
-
await this.
|
|
452
|
+
await this.openAPIpushChanges();
|
|
450
453
|
//}
|
|
451
454
|
interval(5000)
|
|
452
455
|
.pipe(skipWhile(() => this.humidifierUpdateInProgress))
|
|
@@ -459,30 +462,38 @@ export class Humidifier {
|
|
|
459
462
|
private async BLEpushChanges() {
|
|
460
463
|
this.debugLog(`Humidifier: ${this.accessory.displayName} BLE pushChanges`);
|
|
461
464
|
const switchbot = this.connectBLE();
|
|
462
|
-
switchbot
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
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`);
|
|
477
489
|
if (this.platform.config.credentials?.openToken) {
|
|
478
490
|
this.warnLog(`Humidifier: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
479
|
-
await this.
|
|
491
|
+
await this.openAPIpushChanges();
|
|
480
492
|
}
|
|
481
|
-
|
|
482
|
-
});
|
|
493
|
+
}
|
|
483
494
|
}
|
|
484
495
|
|
|
485
|
-
private async
|
|
496
|
+
private async openAPIpushChanges() {
|
|
486
497
|
if (this.platform.config.credentials?.openToken) {
|
|
487
498
|
this.debugLog(`Humidifier: ${this.accessory.displayName} OpenAPI pushChanges`);
|
|
488
499
|
if (
|
package/src/devices/meters.ts
CHANGED
|
@@ -199,12 +199,12 @@ export class Meter {
|
|
|
199
199
|
if (this.device.scanDuration) {
|
|
200
200
|
this.scanDuration = this.accessory.context.scanDuration = this.device.scanDuration;
|
|
201
201
|
if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
|
|
202
|
-
this.warnLog(`
|
|
202
|
+
this.warnLog(`Meter: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
|
|
203
203
|
}
|
|
204
204
|
} else {
|
|
205
205
|
this.scanDuration = this.accessory.context.scanDuration = 1;
|
|
206
206
|
if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
|
|
207
|
-
this.warnLog(`
|
|
207
|
+
this.warnLog(`Meter: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
|
|
208
208
|
}
|
|
209
209
|
}
|
|
210
210
|
}
|
|
@@ -304,84 +304,87 @@ export class Meter {
|
|
|
304
304
|
}
|
|
305
305
|
|
|
306
306
|
private connectBLE() {
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
this.debugLog(`Bot: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
313
|
-
return switchbot;
|
|
314
|
-
} catch (e) {
|
|
315
|
-
Switchbot = false;
|
|
316
|
-
}
|
|
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();
|
|
311
|
+
return switchbot;
|
|
317
312
|
}
|
|
318
313
|
|
|
319
314
|
|
|
320
|
-
private BLErefreshStatus() {
|
|
315
|
+
private async BLErefreshStatus() {
|
|
321
316
|
this.debugLog(`Meter: ${this.accessory.displayName} BLE RefreshStatus`);
|
|
322
317
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
323
318
|
const switchbot = this.connectBLE();
|
|
324
319
|
// Start to monitor advertisement packets
|
|
325
|
-
switchbot
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
this.
|
|
342
|
-
|
|
320
|
+
if (switchbot !== false) {
|
|
321
|
+
switchbot.startScan({
|
|
322
|
+
model: 'T',
|
|
323
|
+
id: this.device.bleMac,
|
|
324
|
+
}).then(() => {
|
|
325
|
+
// Set an event hander
|
|
326
|
+
switchbot.onadvertisement = (ad: ad) => {
|
|
327
|
+
this.serviceData = ad.serviceData;
|
|
328
|
+
this.temperature = ad.serviceData.temperature!.c;
|
|
329
|
+
this.humidity = ad.serviceData.humidity;
|
|
330
|
+
this.battery = ad.serviceData.battery;
|
|
331
|
+
this.debugLog(`Meter: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
332
|
+
this.debugLog(`Meter: ${this.accessory.displayName} model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName}, `
|
|
333
|
+
+ `temperature: ${JSON.stringify(ad.serviceData.temperature?.c)}, humidity: ${ad.serviceData.humidity}, `
|
|
334
|
+
+ `battery: ${ad.serviceData.battery}`);
|
|
335
|
+
|
|
336
|
+
if (this.serviceData) {
|
|
337
|
+
this.connected = true;
|
|
338
|
+
this.debugLog(`Meter: ${this.accessory.displayName} connected: ${this.connected}`);
|
|
339
|
+
} else {
|
|
340
|
+
this.connected = false;
|
|
341
|
+
this.debugLog(`Meter: ${this.accessory.displayName} connected: ${this.connected}`);
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
// Wait 10 seconds
|
|
345
|
+
return switchbot.wait(this.scanDuration * 1000);
|
|
346
|
+
}).then(async () => {
|
|
347
|
+
// Stop to monitor
|
|
348
|
+
switchbot.stopScan();
|
|
349
|
+
if (this.connected) {
|
|
350
|
+
this.CurrentTemperatureCached = this.temperature;
|
|
351
|
+
this.accessory.context.CurrentTemperature = this.CurrentTemperatureCached;
|
|
352
|
+
this.CurrentRelativeHumidityCached = this.humidity!;
|
|
353
|
+
this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidityCached;
|
|
354
|
+
this.BatteryLevelCached = this.battery!;
|
|
355
|
+
this.accessory.context.BatteryLevel = this.BatteryLevelCached;
|
|
356
|
+
this.parseStatus();
|
|
357
|
+
this.updateHomeKitCharacteristics();
|
|
343
358
|
} else {
|
|
344
|
-
this.
|
|
345
|
-
|
|
359
|
+
await this.BLEconnection();
|
|
360
|
+
}
|
|
361
|
+
}).catch(async (e: any) => {
|
|
362
|
+
this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
|
|
363
|
+
if (this.deviceLogging === 'debug') {
|
|
364
|
+
this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
|
|
365
|
+
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
366
|
+
}
|
|
367
|
+
if (this.platform.debugMode) {
|
|
368
|
+
this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
|
|
369
|
+
+ ` Error: ${JSON.stringify(e)}`);
|
|
346
370
|
}
|
|
347
|
-
};
|
|
348
|
-
// Wait 10 seconds
|
|
349
|
-
return switchbot.wait(this.scanDuration * 1000);
|
|
350
|
-
}).then(async () => {
|
|
351
|
-
// Stop to monitor
|
|
352
|
-
switchbot.stopScan();
|
|
353
|
-
if (this.connected) {
|
|
354
|
-
this.CurrentTemperatureCached = this.temperature;
|
|
355
|
-
this.accessory.context.CurrentTemperature = this.CurrentTemperatureCached;
|
|
356
|
-
this.CurrentRelativeHumidityCached = this.humidity!;
|
|
357
|
-
this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidityCached;
|
|
358
|
-
this.BatteryLevelCached = this.battery!;
|
|
359
|
-
this.accessory.context.BatteryLevel = this.BatteryLevelCached;
|
|
360
|
-
this.parseStatus();
|
|
361
|
-
this.updateHomeKitCharacteristics();
|
|
362
|
-
} else {
|
|
363
|
-
this.errorLog(`Meter: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
|
|
364
371
|
if (this.platform.config.credentials?.openToken) {
|
|
365
372
|
this.warnLog(`Meter: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
366
373
|
this.openAPIRefreshStatus();
|
|
367
374
|
}
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
this.openAPIRefreshStatus();
|
|
382
|
-
}
|
|
383
|
-
this.apiError(e);
|
|
384
|
-
});
|
|
375
|
+
this.apiError(e);
|
|
376
|
+
});
|
|
377
|
+
} else {
|
|
378
|
+
await this.BLEconnection();
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
private async BLEconnection() {
|
|
383
|
+
this.errorLog(`Meter: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
|
|
384
|
+
if (this.platform.config.credentials?.openToken) {
|
|
385
|
+
this.warnLog(`Meter: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
386
|
+
await this.openAPIRefreshStatus();
|
|
387
|
+
}
|
|
385
388
|
}
|
|
386
389
|
|
|
387
390
|
private async openAPIRefreshStatus() {
|
package/src/devices/motion.ts
CHANGED
|
@@ -130,12 +130,12 @@ export class Motion {
|
|
|
130
130
|
if (this.device.scanDuration) {
|
|
131
131
|
this.scanDuration = this.accessory.context.scanDuration = this.device.scanDuration;
|
|
132
132
|
if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
|
|
133
|
-
this.warnLog(`
|
|
133
|
+
this.warnLog(`Motion Sensor: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
|
|
134
134
|
}
|
|
135
135
|
} else {
|
|
136
136
|
this.scanDuration = this.accessory.context.scanDuration = 1;
|
|
137
137
|
if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
|
|
138
|
-
this.warnLog(`
|
|
138
|
+
this.warnLog(`Motion Sensor: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
|
|
139
139
|
}
|
|
140
140
|
}
|
|
141
141
|
}
|
|
@@ -227,16 +227,11 @@ export class Motion {
|
|
|
227
227
|
}
|
|
228
228
|
|
|
229
229
|
private connectBLE() {
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
this.debugLog(`Bot: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
236
|
-
return switchbot;
|
|
237
|
-
} catch (e) {
|
|
238
|
-
Switchbot = false;
|
|
239
|
-
}
|
|
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();
|
|
234
|
+
return switchbot;
|
|
240
235
|
}
|
|
241
236
|
|
|
242
237
|
private async BLERefreshStatus() {
|
|
@@ -244,59 +239,67 @@ export class Motion {
|
|
|
244
239
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
245
240
|
const switchbot = this.connectBLE();
|
|
246
241
|
// Start to monitor advertisement packets
|
|
247
|
-
switchbot
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
this.
|
|
263
|
-
|
|
242
|
+
if (switchbot !== false) {
|
|
243
|
+
switchbot.startScan({
|
|
244
|
+
model: 's',
|
|
245
|
+
id: this.device.bleMac,
|
|
246
|
+
}).then(() => {
|
|
247
|
+
// Set an event hander
|
|
248
|
+
switchbot.onadvertisement = (ad: any) => {
|
|
249
|
+
this.serviceData = ad.serviceData;
|
|
250
|
+
this.movement = ad.serviceData.movement;
|
|
251
|
+
this.battery = ad.serviceData.battery;
|
|
252
|
+
this.lightLevel = ad.serviceData.lightLevel;
|
|
253
|
+
this.debugLog(`Motion Sensor: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
254
|
+
this.debugLog(`Motion Sensor: ${this.accessory.displayName} movement: ${ad.serviceData.movement}, lightLevel: `
|
|
255
|
+
+ `${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}`);
|
|
256
|
+
|
|
257
|
+
if (this.serviceData) {
|
|
258
|
+
this.connected = true;
|
|
259
|
+
this.debugLog(`Motion Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
|
|
260
|
+
} else {
|
|
261
|
+
this.connected = false;
|
|
262
|
+
this.debugLog(`Motion Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
// Wait 10 seconds
|
|
266
|
+
return switchbot.wait(this.scanDuration * 1000);
|
|
267
|
+
}).then(async () => {
|
|
268
|
+
// Stop to monitor
|
|
269
|
+
switchbot.stopScan();
|
|
270
|
+
if (this.connected) {
|
|
271
|
+
this.parseStatus();
|
|
272
|
+
this.updateHomeKitCharacteristics();
|
|
264
273
|
} else {
|
|
265
|
-
this.
|
|
266
|
-
|
|
274
|
+
await this.BLEconnection();
|
|
275
|
+
}
|
|
276
|
+
}).catch(async (e: any) => {
|
|
277
|
+
this.errorLog(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
|
|
278
|
+
if (this.deviceLogging === 'debug') {
|
|
279
|
+
this.errorLog(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
|
|
280
|
+
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
281
|
+
}
|
|
282
|
+
if (this.platform.debugMode) {
|
|
283
|
+
this.errorLog(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
|
|
284
|
+
+ ` Error: ${JSON.stringify(e)}`);
|
|
267
285
|
}
|
|
268
|
-
};
|
|
269
|
-
// Wait 10 seconds
|
|
270
|
-
return switchbot.wait(this.scanDuration * 1000);
|
|
271
|
-
}).then(async () => {
|
|
272
|
-
// Stop to monitor
|
|
273
|
-
switchbot.stopScan();
|
|
274
|
-
if (this.connected) {
|
|
275
|
-
this.parseStatus();
|
|
276
|
-
this.updateHomeKitCharacteristics();
|
|
277
|
-
} else {
|
|
278
|
-
this.errorLog(`Motion Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
|
|
279
286
|
if (this.platform.config.credentials?.openToken) {
|
|
280
287
|
this.warnLog(`Motion Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
281
288
|
await this.openAPIRefreshStatus();
|
|
282
289
|
}
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
await this.openAPIRefreshStatus();
|
|
297
|
-
}
|
|
298
|
-
this.apiError(e);
|
|
299
|
-
});
|
|
290
|
+
this.apiError(e);
|
|
291
|
+
});
|
|
292
|
+
} else {
|
|
293
|
+
await this.BLEconnection();
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
private async BLEconnection() {
|
|
298
|
+
this.errorLog(`Motion Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
|
|
299
|
+
if (this.platform.config.credentials?.openToken) {
|
|
300
|
+
this.warnLog(`Motion Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
301
|
+
await this.openAPIRefreshStatus();
|
|
302
|
+
}
|
|
300
303
|
}
|
|
301
304
|
|
|
302
305
|
private async openAPIRefreshStatus() {
|
package/src/devices/plugs.ts
CHANGED
|
@@ -134,12 +134,12 @@ export class Plug {
|
|
|
134
134
|
if (this.device.scanDuration) {
|
|
135
135
|
this.scanDuration = this.accessory.context.scanDuration = this.device.scanDuration;
|
|
136
136
|
if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
|
|
137
|
-
this.warnLog(`
|
|
137
|
+
this.warnLog(`Plug: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
|
|
138
138
|
}
|
|
139
139
|
} else {
|
|
140
140
|
this.scanDuration = this.accessory.context.scanDuration = 1;
|
|
141
141
|
if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
|
|
142
|
-
this.warnLog(`
|
|
142
|
+
this.warnLog(`Plug: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
|
|
143
143
|
}
|
|
144
144
|
}
|
|
145
145
|
}
|
package/src/platform.ts
CHANGED
|
@@ -41,6 +41,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
|
|
|
41
41
|
registeringDevice!: boolean;
|
|
42
42
|
debugMode!: boolean;
|
|
43
43
|
platformLogging?: string;
|
|
44
|
+
switchbot: any;
|
|
44
45
|
|
|
45
46
|
constructor(public readonly log: Logger, public readonly config: SwitchBotPlatformConfig, public readonly api: API) {
|
|
46
47
|
this.logs();
|
|
@@ -192,6 +193,18 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
|
|
|
192
193
|
}
|
|
193
194
|
}
|
|
194
195
|
|
|
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
|
+
|
|
195
208
|
/**
|
|
196
209
|
* this method discovers the Locations
|
|
197
210
|
*/
|