@switchbot/homebridge-switchbot 1.12.7-beta.0 → 1.12.7-beta.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.
@@ -3,7 +3,7 @@ import { interval, Subject } from 'rxjs';
3
3
  import { SwitchBotPlatform } from '../platform';
4
4
  import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
5
5
  import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
6
- import { DeviceURL, device, devicesConfig, serviceData, switchbot, deviceStatusResponse, payload, deviceStatus } from '../settings';
6
+ import { DeviceURL, device, devicesConfig, serviceData, switchbot, deviceStatusResponse, payload, deviceStatus, ad } from '../settings';
7
7
 
8
8
  export class Curtain {
9
9
  // Services
@@ -31,6 +31,7 @@ export class Curtain {
31
31
  SwitchToOpenAPI?: boolean;
32
32
  serviceData!: serviceData;
33
33
  spaceBetweenLevels!: number;
34
+ address!: ad['address'];
34
35
  calibration: serviceData['calibration'];
35
36
  battery: serviceData['battery'];
36
37
  position: serviceData['position'];
@@ -385,6 +386,7 @@ export class Curtain {
385
386
  .join(':')
386
387
  .toLowerCase();
387
388
  this.debugLog(`Curtain: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
389
+ this.getCustomBLEAddress(switchbot);
388
390
  // Start to monitor advertisement packets
389
391
  if (switchbot !== false) {
390
392
  switchbot
@@ -395,6 +397,13 @@ export class Curtain {
395
397
  .then(() => {
396
398
  // Set an event hander
397
399
  switchbot.onadvertisement = (ad: any) => {
400
+ this.address = ad.address;
401
+ if (this.deviceLogging.includes('debug')) {
402
+ this.infoLog(this.address);
403
+ this.infoLog(this.device.bleMac);
404
+ this.infoLog(`Curtain: ${this.accessory.displayName} BLE Address Found: ${this.address}`);
405
+ this.infoLog(`Curtain: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac}`);
406
+ }
398
407
  this.serviceData = ad.serviceData;
399
408
  this.calibration = ad.serviceData.calibration;
400
409
  this.battery = ad.serviceData.battery;
@@ -424,12 +433,7 @@ export class Curtain {
424
433
  this.parseStatus();
425
434
  this.updateHomeKitCharacteristics();
426
435
  } else {
427
- this.errorLog(`Curtain: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
428
- if (this.platform.config.credentials?.openToken) {
429
- this.warnLog(`Curtain: ${this.accessory.displayName} Using OpenAPI Connection`);
430
- this.SwitchToOpenAPI = true;
431
- await this.openAPIRefreshStatus();
432
- }
436
+ await this.BLEconnection(switchbot);
433
437
  }
434
438
  })
435
439
  .catch(async (e: any) => {
@@ -451,6 +455,24 @@ export class Curtain {
451
455
  }
452
456
  }
453
457
 
458
+ async getCustomBLEAddress(switchbot: any) {
459
+ if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
460
+ (async () => {
461
+ // Start to monitor advertisement packets
462
+ await switchbot.startScan({
463
+ model: 'c',
464
+ });
465
+ // Set an event handler
466
+ switchbot.onadvertisement = (ad: any) => {
467
+ this.warnLog(JSON.stringify(ad, null, ' '));
468
+ };
469
+ await switchbot.wait(10000);
470
+ // Stop to monitor
471
+ switchbot.stopScan();
472
+ })();
473
+ }
474
+ }
475
+
454
476
  async BLEconnection(switchbot: any): Promise<void> {
455
477
  this.errorLog(`Curtain: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
456
478
  if (this.platform.config.credentials?.openToken) {
@@ -36,6 +36,7 @@ export class Humidifier {
36
36
  // BLE Others
37
37
  connected?: boolean;
38
38
  serviceData!: serviceData;
39
+ address!: ad['address'];
39
40
  onState!: serviceData['onState'];
40
41
  autoMode!: serviceData['autoMode'];
41
42
  percentage!: serviceData['percentage'];
@@ -287,6 +288,7 @@ export class Humidifier {
287
288
  .join(':')
288
289
  .toLowerCase();
289
290
  this.debugLog(`Humidifier: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
291
+ this.getCustomBLEAddress(switchbot);
290
292
  // Start to monitor advertisement packets
291
293
  if (switchbot !== false) {
292
294
  switchbot
@@ -297,6 +299,13 @@ export class Humidifier {
297
299
  .then(() => {
298
300
  // Set an event hander
299
301
  switchbot.onadvertisement = (ad: ad) => {
302
+ this.address = ad.address;
303
+ if (this.deviceLogging.includes('debug')) {
304
+ this.infoLog(this.address);
305
+ this.infoLog(this.device.bleMac);
306
+ this.infoLog(`Humidifier: ${this.accessory.displayName} BLE Address Found: ${this.address}`);
307
+ this.infoLog(`Humidifier: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac}`);
308
+ }
300
309
  this.serviceData = ad.serviceData;
301
310
  this.autoMode = ad.serviceData.autoMode;
302
311
  this.onState = ad.serviceData.onState;
@@ -346,6 +355,24 @@ export class Humidifier {
346
355
  }
347
356
  }
348
357
 
358
+ async getCustomBLEAddress(switchbot: any) {
359
+ if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
360
+ (async () => {
361
+ // Start to monitor advertisement packets
362
+ await switchbot.startScan({
363
+ model: 'e',
364
+ });
365
+ // Set an event handler
366
+ switchbot.onadvertisement = (ad: any) => {
367
+ this.warnLog(JSON.stringify(ad, null, ' '));
368
+ };
369
+ await switchbot.wait(10000);
370
+ // Stop to monitor
371
+ switchbot.stopScan();
372
+ })();
373
+ }
374
+ }
375
+
349
376
  async BLEconnection(switchbot: any): Promise<void> {
350
377
  this.errorLog(`Humidifier: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
351
378
  if (this.platform.config.credentials?.openToken) {
@@ -36,6 +36,7 @@ export class Meter {
36
36
  switchbot!: switchbot;
37
37
  SwitchToOpenAPI?: boolean;
38
38
  serviceData!: serviceData;
39
+ address!: ad['address'];
39
40
  temperature!: temperature['c'];
40
41
  battery!: serviceData['battery'];
41
42
  humidity!: serviceData['humidity'];
@@ -239,6 +240,7 @@ export class Meter {
239
240
  .join(':')
240
241
  .toLowerCase();
241
242
  this.debugLog(`Meter: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
243
+ this.getCustomBLEAddress(switchbot);
242
244
  // Start to monitor advertisement packets
243
245
  if (switchbot !== false) {
244
246
  switchbot
@@ -249,6 +251,13 @@ export class Meter {
249
251
  .then(() => {
250
252
  // Set an event hander
251
253
  switchbot.onadvertisement = (ad: ad) => {
254
+ this.address = ad.address;
255
+ if (this.deviceLogging.includes('debug')) {
256
+ this.infoLog(this.address);
257
+ this.infoLog(this.device.bleMac);
258
+ this.infoLog(`Meter: ${this.accessory.displayName} BLE Address Found: ${this.address}`);
259
+ this.infoLog(`Meter: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac}`);
260
+ }
252
261
  this.serviceData = ad.serviceData;
253
262
  this.temperature = ad.serviceData.temperature!.c;
254
263
  this.humidity = ad.serviceData.humidity;
@@ -300,6 +309,24 @@ export class Meter {
300
309
  }
301
310
  }
302
311
 
312
+ async getCustomBLEAddress(switchbot: any) {
313
+ if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
314
+ (async () => {
315
+ // Start to monitor advertisement packets
316
+ await switchbot.startScan({
317
+ model: 'T',
318
+ });
319
+ // Set an event handler
320
+ switchbot.onadvertisement = (ad: any) => {
321
+ this.warnLog(JSON.stringify(ad, null, ' '));
322
+ };
323
+ await switchbot.wait(10000);
324
+ // Stop to monitor
325
+ switchbot.stopScan();
326
+ })();
327
+ }
328
+ }
329
+
303
330
  async BLEconnection(switchbot: any): Promise<void> {
304
331
  this.errorLog(`Meter: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
305
332
  if (this.platform.config.credentials?.openToken) {
@@ -2,7 +2,7 @@ import { interval, Subject } from 'rxjs';
2
2
  import { skipWhile } from 'rxjs/operators';
3
3
  import { SwitchBotPlatform } from '../platform';
4
4
  import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
5
- import { DeviceURL, device, devicesConfig, serviceData, switchbot, deviceStatusResponse, deviceStatus } from '../settings';
5
+ import { DeviceURL, device, devicesConfig, serviceData, switchbot, deviceStatusResponse, deviceStatus, ad } from '../settings';
6
6
 
7
7
  /**
8
8
  * Platform Accessory
@@ -31,6 +31,7 @@ export class Motion {
31
31
  switchbot!: switchbot;
32
32
  SwitchToOpenAPI?: boolean;
33
33
  serviceData!: serviceData;
34
+ address!: ad['address'];
34
35
  battery!: serviceData['battery'];
35
36
  movement!: serviceData['movement'];
36
37
  lightLevel!: serviceData['lightLevel'];
@@ -213,21 +214,32 @@ export class Motion {
213
214
  this.debugLog(`Motion Sensor: ${this.accessory.displayName} BLE RefreshStatus`);
214
215
  const switchbot = await this.platform.connectBLE();
215
216
  // Convert to BLE Address
216
- this.device.bleMac = this.device
217
- .deviceId!.match(/.{1,2}/g)!
218
- .join(':')
219
- .toLowerCase();
217
+ this.device.bleMac =
218
+ this.device.customBLEaddress ||
219
+ this.device
220
+ .deviceId!.match(/.{1,2}/g)!
221
+ .join(':')
222
+ .toLowerCase();
220
223
  this.debugLog(`Motion Sensor: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
224
+
225
+ this.getCustomBLEAddress(switchbot);
221
226
  // Start to monitor advertisement packets
222
227
  if (switchbot !== false) {
223
228
  await switchbot
224
229
  .startScan({
225
- //model: 's',
230
+ model: 's',
226
231
  id: this.device.bleMac,
227
232
  })
228
233
  .then(async () => {
229
234
  // Set an event hander
230
235
  switchbot.onadvertisement = (ad: any) => {
236
+ this.address = ad.address;
237
+ if (this.deviceLogging.includes('debug')) {
238
+ this.infoLog(this.address);
239
+ this.infoLog(this.device.bleMac);
240
+ this.infoLog(`Motion Sensor: ${this.accessory.displayName} BLE Address Found: ${this.address}`);
241
+ this.infoLog(`Motion Sensor: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac}`);
242
+ }
231
243
  this.serviceData = ad.serviceData;
232
244
  this.movement = ad.serviceData.movement;
233
245
  this.battery = ad.serviceData.battery;
@@ -279,6 +291,24 @@ export class Motion {
279
291
  }
280
292
  }
281
293
 
294
+ async getCustomBLEAddress(switchbot: any) {
295
+ if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
296
+ (async () => {
297
+ // Start to monitor advertisement packets
298
+ await switchbot.startScan({
299
+ model: 's',
300
+ });
301
+ // Set an event handler
302
+ switchbot.onadvertisement = (ad: any) => {
303
+ this.warnLog(JSON.stringify(ad, null, ' '));
304
+ };
305
+ await switchbot.wait(10000);
306
+ // Stop to monitor
307
+ switchbot.stopScan();
308
+ })();
309
+ }
310
+ }
311
+
282
312
  async BLEconnection(switchbot: any): Promise<void> {
283
313
  this.errorLog(`Motion Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
284
314
  if (this.platform.config.credentials?.openToken) {
package/src/settings.ts CHANGED
@@ -53,6 +53,7 @@ export interface devicesConfig extends device {
53
53
  plug?: Record<any, any>;
54
54
  lock?: lock;
55
55
  ble?: boolean;
56
+ customBLEaddress?: string;
56
57
  scanDuration?: number;
57
58
  hide_device?: boolean;
58
59
  offline?: boolean;
@@ -299,6 +300,9 @@ export type deviceStatus = {
299
300
  };
300
301
 
301
302
  export type ad = {
303
+ id: string;
304
+ address: string;
305
+ rssi: number;
302
306
  serviceData: serviceData;
303
307
  };
304
308