@switchbot/homebridge-switchbot 1.12.6 → 1.12.7-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.
@@ -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,42 @@ 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
+ this.device.bleMac = this.device.customBLEaddress || this.device
217
218
  .deviceId!.match(/.{1,2}/g)!
218
219
  .join(':')
219
220
  .toLowerCase();
220
221
  this.debugLog(`Motion Sensor: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
222
+ (async () => {
223
+ // Start to monitor advertisement packets
224
+ await switchbot.startScan({
225
+ model: 's',
226
+ //id: this.device.bleMac,
227
+ });
228
+ // Set an event handler
229
+ switchbot.onadvertisement = (ad: any) => {
230
+ this.warnLog(JSON.stringify(ad, null, ' '));
231
+ };
232
+ await switchbot.wait(10000);
233
+ // Stop to monitor
234
+ switchbot.stopScan();
235
+ })();
221
236
  // Start to monitor advertisement packets
222
237
  if (switchbot !== false) {
223
- switchbot
238
+ await switchbot
224
239
  .startScan({
225
240
  model: 's',
226
241
  id: this.device.bleMac,
227
242
  })
228
- .then(() => {
243
+ .then(async () => {
229
244
  // Set an event hander
230
245
  switchbot.onadvertisement = (ad: any) => {
246
+ this.address = ad.address;
247
+ if (this.deviceLogging.includes('debug')) {
248
+ this.infoLog(this.address);
249
+ this.infoLog(this.device.bleMac);
250
+ this.infoLog(`Motion Sensor: ${this.accessory.displayName} BLE Address Found: ${this.address}`);
251
+ this.infoLog(`Motion Sensor: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac}`);
252
+ }
231
253
  this.serviceData = ad.serviceData;
232
254
  this.movement = ad.serviceData.movement;
233
255
  this.battery = ad.serviceData.battery;
@@ -235,7 +257,7 @@ export class Motion {
235
257
  this.debugLog(`Motion Sensor: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
236
258
  this.debugLog(
237
259
  `Motion Sensor: ${this.accessory.displayName} movement: ${ad.serviceData.movement}, lightLevel: ` +
238
- `${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}`,
260
+ `${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}`,
239
261
  );
240
262
 
241
263
  if (this.serviceData) {
@@ -247,7 +269,7 @@ export class Motion {
247
269
  }
248
270
  };
249
271
  // Wait 10 seconds
250
- return switchbot.wait(this.scanDuration * 1000);
272
+ return await switchbot.wait(this.scanDuration * 1000);
251
273
  })
252
274
  .then(async () => {
253
275
  // Stop to monitor
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