@switchbot/homebridge-switchbot 3.8.3-beta.23 → 3.8.3-beta.25

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/src/platform.ts CHANGED
@@ -19,9 +19,9 @@ import { EveHomeKitTypes } from 'homebridge-lib/EveHomeKitTypes'
19
19
  * For Testing Locally:
20
20
  * import { SwitchBotModel } from '/Users/Shared/GitHub/OpenWonderLabs/node-switchbot/dist/index.js';
21
21
  */
22
- import type { blindTilt, curtain, curtain3, device, devices, irdevice } from 'node-switchbot'
22
+ import type { blindTilt, curtain, curtain3, device, irdevice } from 'node-switchbot'
23
23
 
24
- import { LogLevel, SwitchBot, SwitchBotModel, SwitchBotOpenAPI } from 'node-switchbot'
24
+ import { LogLevel, SwitchBotBLE, SwitchBotModel, SwitchBotOpenAPI } from 'node-switchbot'
25
25
  import { queueScheduler } from 'rxjs'
26
26
 
27
27
  import { BlindTilt } from './device/blindtilt.js'
@@ -79,6 +79,10 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
79
79
  mqttClient: MqttClient | null = null
80
80
  webhookEventListener: Server | null = null
81
81
 
82
+ // SwitchBot APIs
83
+ switchBotAPI!: SwitchBotOpenAPI
84
+ switchBotBLE!: SwitchBotBLE
85
+
82
86
  // External APIs
83
87
  public readonly eve: any
84
88
  public readonly fakegatoAPI: any
@@ -86,7 +90,6 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
86
90
  // Event Handlers
87
91
  public readonly webhookEventHandler: { [x: string]: (context: any) => void } = {}
88
92
  public readonly bleEventHandler: { [x: string]: (context: any) => void } = {}
89
- switchBotAPI!: SwitchBotOpenAPI
90
93
 
91
94
  constructor(
92
95
  log: Logging,
@@ -247,23 +250,39 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
247
250
  }
248
251
 
249
252
  async setupBlE() {
253
+ this.switchBotBLE = new SwitchBotBLE()
254
+ // Listen for log events
255
+ this.switchBotBLE.on('log', (log) => {
256
+ switch (log.level) {
257
+ case LogLevel.ERROR:
258
+ this.errorLog(log.message)
259
+ break
260
+ case LogLevel.WARN:
261
+ this.warnLog(log.message)
262
+ break
263
+ case LogLevel.DEBUG:
264
+ this.debugLog(log.message)
265
+ break
266
+ default:
267
+ this.infoLog(log.message)
268
+ }
269
+ })
250
270
  if (this.config.options?.BLE) {
251
271
  await this.debugLog('setupBLE')
252
- const switchbot = new SwitchBot()
253
- if (switchbot === undefined) {
254
- await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`)
272
+ if (this.switchBotBLE === undefined) {
273
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${this.switchBotBLE}`)
255
274
  } else {
256
275
  // Start to monitor advertisement packets
257
276
  (async () => {
258
277
  // Start to monitor advertisement packets
259
278
  await this.debugLog('Scanning for BLE SwitchBot devices...')
260
279
  try {
261
- await switchbot.startScan()
280
+ await this.switchBotBLE.startScan()
262
281
  } catch (e: any) {
263
282
  await this.errorLog(`Failed to start BLE scanning. Error:${e}`)
264
283
  }
265
284
  // Set an event handler to monitor advertisement packets
266
- switchbot.onadvertisement = async (ad: any) => {
285
+ this.switchBotBLE.onadvertisement = async (ad: any) => {
267
286
  try {
268
287
  this.bleEventHandler[ad.address]?.(ad.serviceData)
269
288
  } catch (e: any) {
@@ -274,7 +293,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
274
293
 
275
294
  this.api.on('shutdown', async () => {
276
295
  try {
277
- switchbot.stopScan()
296
+ this.switchBotBLE.stopScan()
278
297
  await this.infoLog('Stopped BLE scanning to close listening.')
279
298
  } catch (e: any) {
280
299
  await this.errorLog(`Failed to stop Platform BLE scanning. Error:${e.message}`)
@@ -2558,10 +2577,9 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
2558
2577
  // BLE Connection
2559
2578
  async connectBLE(accessory: PlatformAccessory, device: device & devicesConfig): Promise<any> {
2560
2579
  try {
2561
- const switchbot = new SwitchBot()
2562
- queueScheduler.schedule(async () => switchbot)
2563
- await this.debugLog(`${device.deviceType}: ${accessory.displayName} 'node-switchbot' found: ${switchbot}`)
2564
- return switchbot
2580
+ queueScheduler.schedule(async () => this.switchBotBLE)
2581
+ await this.debugLog(`${device.deviceType}: ${accessory.displayName} 'node-switchbot' found: ${this.switchBotBLE}`)
2582
+ return this.switchBotBLE
2565
2583
  } catch (e: any) {
2566
2584
  await this.errorLog(`${device.deviceType}: ${accessory.displayName} 'node-switchbot' not found, Error: ${e}`)
2567
2585
  return false