Skip to content

Commit e2d9eb3

Browse files
authored
Remove unneeded Noble code (#297)
1 parent 168aa75 commit e2d9eb3

File tree

2 files changed

+7
-44
lines changed

2 files changed

+7
-44
lines changed

src/device.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
*
33
* device.ts: Switchbot BLE API registration.
44
*/
5-
import type Noble from '@stoprocent/noble'
6-
import type { Characteristic, Peripheral, Service } from '@stoprocent/noble'
5+
import type { Characteristic, Noble, Peripheral, Service } from '@stoprocent/noble'
76

87
import type { airPurifierServiceData, airPurifierTableServiceData, batteryCirculatorFanServiceData, blindTiltServiceData, botServiceData, ceilingLightProServiceData, ceilingLightServiceData, colorBulbServiceData, contactSensorServiceData, curtain3ServiceData, curtainServiceData, hub2ServiceData, humidifier2ServiceData, humidifierServiceData, keypadDetectorServiceData, lockProServiceData, lockServiceData, meterPlusServiceData, meterProCO2ServiceData, meterProServiceData, meterServiceData, motionSensorServiceData, outdoorMeterServiceData, plugMiniJPServiceData, plugMiniUSServiceData, relaySwitch1PMServiceData, relaySwitch1ServiceData, remoteServiceData, robotVacuumCleanerServiceData, stripLightServiceData, waterLeakDetectorServiceData } from './types/ble.js'
98

@@ -295,7 +294,7 @@ export interface Params {
295294
model?: string
296295
id?: string
297296
quick?: boolean
298-
noble?: typeof Noble
297+
noble?: Noble
299298
}
300299

301300
export interface ErrorObject {
@@ -310,10 +309,7 @@ export interface Chars {
310309
}
311310

312311
export interface NobleTypes {
313-
noble: typeof Noble & {
314-
state: 'unknown' | 'resetting' | 'unsupported' | 'unauthorized' | 'poweredOff' | 'poweredOn'
315-
}
316-
state: 'unknown' | 'resetting' | 'unsupported' | 'unauthorized' | 'poweredOff' | 'poweredOn'
312+
noble: Noble
317313
peripheral: Peripheral
318314
}
319315

@@ -553,7 +549,7 @@ export class ErrorUtils {
553549
* Represents a Switchbot Device.
554550
*/
555551
export class SwitchbotDevice extends EventEmitter {
556-
private noble: typeof Noble
552+
private noble: Noble
557553
private peripheral: NobleTypes['peripheral']
558554
private characteristics: Chars | null = null
559555
private deviceId!: string
@@ -572,7 +568,7 @@ export class SwitchbotDevice extends EventEmitter {
572568
* @param peripheral The peripheral object from noble.
573569
* @param noble The Noble object.
574570
*/
575-
constructor(peripheral: NobleTypes['peripheral'], noble: typeof Noble) {
571+
constructor(peripheral: NobleTypes['peripheral'], noble: Noble) {
576572
super()
577573
this.peripheral = peripheral
578574
this.noble = noble

src/switchbot-ble.ts

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -76,39 +76,6 @@ export class SwitchBotBLE extends EventEmitter {
7676
}
7777
}
7878

79-
/**
80-
* Waits for the noble object to be powered on.
81-
*
82-
* @returns {Promise<void>} - Resolves when the noble object is powered on.
83-
*/
84-
private async waitForPowerOn(): Promise<void> {
85-
await this.ready
86-
if (this.noble && this.noble.state === 'poweredOn') {
87-
return
88-
}
89-
90-
return new Promise<void>((resolve, reject) => {
91-
this.noble?.once('stateChange', (state: NobleTypes['state']) => {
92-
switch (state) {
93-
case 'unsupported':
94-
case 'unauthorized':
95-
case 'poweredOff':
96-
reject(new Error(`Failed to initialize the Noble object: ${state}`))
97-
break
98-
case 'resetting':
99-
case 'unknown':
100-
reject(new Error(`Adapter is not ready: ${state}`))
101-
break
102-
case 'poweredOn':
103-
resolve()
104-
break
105-
default:
106-
reject(new Error(`Unknown state: ${state}`))
107-
}
108-
})
109-
})
110-
}
111-
11279
/**
11380
* Discovers Switchbot devices with enhanced error handling and logging.
11481
* @param params The discovery parameters.
@@ -123,7 +90,7 @@ export class SwitchBotBLE extends EventEmitter {
12390
quick: { required: false, type: 'boolean' },
12491
})
12592

126-
await this.waitForPowerOn()
93+
await this.noble.waitForPoweredOnAsync()
12794

12895
if (!this.noble) {
12996
throw new Error('Noble BLE library failed to initialize properly')
@@ -303,7 +270,7 @@ export class SwitchBotBLE extends EventEmitter {
303270
id: { required: false, type: 'string', min: 12, max: 17 },
304271
})
305272

306-
await this.waitForPowerOn()
273+
await this.noble.waitForPoweredOnAsync()
307274

308275
if (!this.noble) {
309276
throw new Error('noble object failed to initialize')

0 commit comments

Comments
 (0)