@switchbot/homebridge-switchbot 3.8.3-beta.30 → 3.8.3-beta.32
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/.github/workflows/release.yml +1 -1
- package/CHANGELOG.md +2 -1
- package/config.schema.json +24 -0
- package/dist/device/blindtilt.d.ts.map +1 -1
- package/dist/device/blindtilt.js +26 -23
- package/dist/device/blindtilt.js.map +1 -1
- package/dist/device/bot.d.ts.map +1 -1
- package/dist/device/bot.js +30 -22
- package/dist/device/bot.js.map +1 -1
- package/dist/device/ceilinglight.d.ts.map +1 -1
- package/dist/device/ceilinglight.js +18 -17
- package/dist/device/ceilinglight.js.map +1 -1
- package/dist/device/colorbulb.d.ts.map +1 -1
- package/dist/device/colorbulb.js +35 -32
- package/dist/device/colorbulb.js.map +1 -1
- package/dist/device/contact.js +11 -11
- package/dist/device/contact.js.map +1 -1
- package/dist/device/curtain.d.ts.map +1 -1
- package/dist/device/curtain.js +21 -20
- package/dist/device/curtain.js.map +1 -1
- package/dist/device/device.js +8 -8
- package/dist/device/device.js.map +1 -1
- package/dist/device/fan.d.ts.map +1 -1
- package/dist/device/fan.js +18 -17
- package/dist/device/fan.js.map +1 -1
- package/dist/device/hub.js +11 -11
- package/dist/device/hub.js.map +1 -1
- package/dist/device/humidifier.d.ts.map +1 -1
- package/dist/device/humidifier.js +17 -16
- package/dist/device/humidifier.js.map +1 -1
- package/dist/device/iosensor.js +11 -11
- package/dist/device/iosensor.js.map +1 -1
- package/dist/device/lightstrip.d.ts.map +1 -1
- package/dist/device/lightstrip.js +26 -25
- package/dist/device/lightstrip.js.map +1 -1
- package/dist/device/lock.d.ts.map +1 -1
- package/dist/device/lock.js +16 -16
- package/dist/device/lock.js.map +1 -1
- package/dist/device/meter.js +11 -11
- package/dist/device/meter.js.map +1 -1
- package/dist/device/meterplus.js +11 -11
- package/dist/device/meterplus.js.map +1 -1
- package/dist/device/motion.js +11 -11
- package/dist/device/motion.js.map +1 -1
- package/dist/device/plug.d.ts.map +1 -1
- package/dist/device/plug.js +18 -17
- package/dist/device/plug.js.map +1 -1
- package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
- package/dist/device/robotvacuumcleaner.js +18 -17
- package/dist/device/robotvacuumcleaner.js.map +1 -1
- package/dist/device/waterdetector.js +11 -11
- package/dist/device/waterdetector.js.map +1 -1
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +80 -76
- package/dist/platform.js.map +1 -1
- package/dist/settings.d.ts +3 -0
- package/dist/settings.d.ts.map +1 -1
- package/dist/settings.js.map +1 -1
- package/package.json +3 -3
- package/src/device/blindtilt.ts +29 -26
- package/src/device/bot.ts +34 -26
- package/src/device/ceilinglight.ts +20 -19
- package/src/device/colorbulb.ts +40 -37
- package/src/device/contact.ts +11 -11
- package/src/device/curtain.ts +25 -23
- package/src/device/device.ts +8 -8
- package/src/device/fan.ts +20 -19
- package/src/device/hub.ts +11 -11
- package/src/device/humidifier.ts +19 -18
- package/src/device/iosensor.ts +11 -11
- package/src/device/lightstrip.ts +30 -29
- package/src/device/lock.ts +18 -18
- package/src/device/meter.ts +11 -11
- package/src/device/meterplus.ts +11 -11
- package/src/device/motion.ts +11 -11
- package/src/device/plug.ts +20 -19
- package/src/device/robotvacuumcleaner.ts +20 -19
- package/src/device/waterdetector.ts +11 -11
- package/src/platform.ts +82 -78
- package/src/settings.ts +3 -0
package/src/device/colorbulb.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* blindtilt.ts: @switchbot/homebridge-switchbot.
|
|
4
4
|
*/
|
|
5
5
|
import type { CharacteristicValue, Controller, ControllerConstructor, ControllerServiceMap, PlatformAccessory, Service } from 'homebridge'
|
|
6
|
-
import type { bodyChange, colorBulbServiceData, colorBulbStatus, colorBulbWebhookContext, device } from 'node-switchbot'
|
|
6
|
+
import type { bodyChange, colorBulbServiceData, colorBulbStatus, colorBulbWebhookContext, device, SwitchbotDevice, WoBulb } from 'node-switchbot'
|
|
7
7
|
|
|
8
8
|
import type { SwitchBotPlatform } from '../platform.js'
|
|
9
9
|
import type { devicesConfig } from '../settings.js'
|
|
@@ -144,7 +144,7 @@ export class ColorBulb extends deviceBase {
|
|
|
144
144
|
this.debugLog('Retrieve initial values and update Homekit')
|
|
145
145
|
this.refreshStatus()
|
|
146
146
|
} catch (e: any) {
|
|
147
|
-
this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e}`)
|
|
147
|
+
this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e.message ?? e}`)
|
|
148
148
|
}
|
|
149
149
|
|
|
150
150
|
// regisiter webhook event handler if enabled
|
|
@@ -152,7 +152,7 @@ export class ColorBulb extends deviceBase {
|
|
|
152
152
|
this.debugLog('Registering Webhook Event Handler')
|
|
153
153
|
this.registerWebhook()
|
|
154
154
|
} catch (e: any) {
|
|
155
|
-
this.errorLog(`failed to registerWebhook, Error: ${e}`)
|
|
155
|
+
this.errorLog(`failed to registerWebhook, Error: ${e.message ?? e}`)
|
|
156
156
|
}
|
|
157
157
|
|
|
158
158
|
// regisiter platform BLE event handler if enabled
|
|
@@ -160,7 +160,7 @@ export class ColorBulb extends deviceBase {
|
|
|
160
160
|
this.debugLog('Registering Platform BLE Event Handler')
|
|
161
161
|
this.registerPlatformBLE()
|
|
162
162
|
} catch (e: any) {
|
|
163
|
-
this.errorLog(`failed to registerPlatformBLE, Error: ${e}`)
|
|
163
|
+
this.errorLog(`failed to registerPlatformBLE, Error: ${e.message ?? e}`)
|
|
164
164
|
}
|
|
165
165
|
|
|
166
166
|
// Start an update interval
|
|
@@ -305,22 +305,22 @@ export class ColorBulb extends deviceBase {
|
|
|
305
305
|
|
|
306
306
|
async BLERefreshStatus(): Promise<void> {
|
|
307
307
|
await this.debugLog('BLERefreshStatus')
|
|
308
|
-
const
|
|
309
|
-
if (
|
|
310
|
-
await this.BLERefreshConnection(
|
|
308
|
+
const switchBotBLE = await this.switchbotBLE()
|
|
309
|
+
if (switchBotBLE === undefined) {
|
|
310
|
+
await this.BLERefreshConnection(switchBotBLE)
|
|
311
311
|
} else {
|
|
312
312
|
// Start to monitor advertisement packets
|
|
313
313
|
(async () => {
|
|
314
314
|
// Start to monitor advertisement packets
|
|
315
|
-
const serviceData = await this.monitorAdvertisementPackets(
|
|
315
|
+
const serviceData = await this.monitorAdvertisementPackets(switchBotBLE) as colorBulbServiceData
|
|
316
316
|
// Update HomeKit
|
|
317
317
|
if (serviceData.model === SwitchBotBLEModel.ColorBulb && serviceData.modelName === SwitchBotBLEModelName.ColorBulb) {
|
|
318
318
|
this.serviceData = serviceData
|
|
319
319
|
await this.BLEparseStatus()
|
|
320
320
|
await this.updateHomeKitCharacteristics()
|
|
321
321
|
} else {
|
|
322
|
-
await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`)
|
|
323
|
-
await this.BLERefreshConnection(
|
|
322
|
+
await this.errorLog(`failed to get serviceData, serviceData: ${JSON.stringify(serviceData)}`)
|
|
323
|
+
await this.BLERefreshConnection(switchBotBLE)
|
|
324
324
|
}
|
|
325
325
|
})()
|
|
326
326
|
}
|
|
@@ -341,7 +341,7 @@ export class ColorBulb extends deviceBase {
|
|
|
341
341
|
await this.BLEparseStatus()
|
|
342
342
|
await this.updateHomeKitCharacteristics()
|
|
343
343
|
} catch (e: any) {
|
|
344
|
-
await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
|
|
344
|
+
await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e.message ?? e}`)
|
|
345
345
|
}
|
|
346
346
|
}
|
|
347
347
|
} catch (error) {
|
|
@@ -383,7 +383,7 @@ export class ColorBulb extends deviceBase {
|
|
|
383
383
|
await this.parseStatusWebhook()
|
|
384
384
|
await this.updateHomeKitCharacteristics()
|
|
385
385
|
} catch (e: any) {
|
|
386
|
-
await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`)
|
|
386
|
+
await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e.message ?? e}`)
|
|
387
387
|
}
|
|
388
388
|
}
|
|
389
389
|
} else {
|
|
@@ -452,23 +452,23 @@ export class ColorBulb extends deviceBase {
|
|
|
452
452
|
await this.debugLog('BLEpushChanges')
|
|
453
453
|
if (this.LightBulb.On !== this.accessory.context.On) {
|
|
454
454
|
await this.debugLog(`BLEpushChanges On: ${this.LightBulb.On}, OnCached: ${this.accessory.context.On}`)
|
|
455
|
-
const
|
|
455
|
+
const switchBotBLE = await this.platform.connectBLE(this.accessory, this.device)
|
|
456
456
|
try {
|
|
457
457
|
const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId)
|
|
458
458
|
this.device.bleMac = formattedDeviceId
|
|
459
459
|
await this.debugLog(`bleMac: ${this.device.bleMac}`)
|
|
460
|
-
if (
|
|
461
|
-
|
|
460
|
+
if (switchBotBLE !== false) {
|
|
461
|
+
switchBotBLE
|
|
462
462
|
.discover({ model: this.device.bleModel, id: this.device.bleMac })
|
|
463
|
-
.then(async (device_list:
|
|
463
|
+
.then(async (device_list: WoBulb[]) => {
|
|
464
464
|
await this.infoLog(`On: ${this.LightBulb.On}`)
|
|
465
465
|
return await this.retryBLE({
|
|
466
466
|
max: await this.maxRetryBLE(),
|
|
467
467
|
fn: async () => {
|
|
468
468
|
if (this.LightBulb.On) {
|
|
469
|
-
return await device_list[0].turnOn(
|
|
469
|
+
return await device_list[0].turnOn()
|
|
470
470
|
} else {
|
|
471
|
-
return await device_list[0].turnOff(
|
|
471
|
+
return await device_list[0].turnOff()
|
|
472
472
|
}
|
|
473
473
|
},
|
|
474
474
|
})
|
|
@@ -483,7 +483,7 @@ export class ColorBulb extends deviceBase {
|
|
|
483
483
|
await this.BLEPushConnection()
|
|
484
484
|
})
|
|
485
485
|
} else {
|
|
486
|
-
await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${
|
|
486
|
+
await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${JSON.stringify(switchBotBLE)}`)
|
|
487
487
|
await this.BLEPushConnection()
|
|
488
488
|
}
|
|
489
489
|
} catch (error) {
|
|
@@ -497,17 +497,18 @@ export class ColorBulb extends deviceBase {
|
|
|
497
497
|
async BLEpushBrightnessChanges(): Promise<void> {
|
|
498
498
|
await this.debugLog('BLEpushBrightnessChanges')
|
|
499
499
|
if (this.LightBulb.Brightness !== this.accessory.context.Brightness) {
|
|
500
|
-
const
|
|
500
|
+
const switchBotBLE = await this.platform.connectBLE(this.accessory, this.device)
|
|
501
501
|
try {
|
|
502
502
|
const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId)
|
|
503
503
|
this.device.bleMac = formattedDeviceId
|
|
504
504
|
await this.debugLog(`bleMac: ${this.device.bleMac}`)
|
|
505
|
-
if (
|
|
506
|
-
|
|
505
|
+
if (switchBotBLE !== false) {
|
|
506
|
+
switchBotBLE
|
|
507
507
|
.discover({ model: this.device.bleModel, id: this.device.bleMac })
|
|
508
|
-
.then(async (device_list:
|
|
508
|
+
.then(async (device_list: SwitchbotDevice[]) => {
|
|
509
|
+
const deviceList = device_list as unknown as WoBulb[]
|
|
509
510
|
await this.infoLog(`Target Brightness: ${this.LightBulb.Brightness}`)
|
|
510
|
-
return await
|
|
511
|
+
return await deviceList[0].setBrightness(Number(this.LightBulb.Brightness))
|
|
511
512
|
})
|
|
512
513
|
.then(async () => {
|
|
513
514
|
await this.successLog(`Brightness: ${this.LightBulb.Brightness} sent over SwitchBot BLE, sent successfully`)
|
|
@@ -519,7 +520,7 @@ export class ColorBulb extends deviceBase {
|
|
|
519
520
|
await this.BLEPushConnection()
|
|
520
521
|
})
|
|
521
522
|
} else {
|
|
522
|
-
await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${
|
|
523
|
+
await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${JSON.stringify(switchBotBLE)}`)
|
|
523
524
|
await this.BLEPushConnection()
|
|
524
525
|
}
|
|
525
526
|
} catch (error) {
|
|
@@ -535,17 +536,18 @@ export class ColorBulb extends deviceBase {
|
|
|
535
536
|
if (this.LightBulb.ColorTemperature !== this.accessory.context.ColorTemperature) {
|
|
536
537
|
const kelvin = Math.round(1000000 / Number(this.LightBulb.ColorTemperature))
|
|
537
538
|
this.accessory.context.kelvin = kelvin
|
|
538
|
-
const
|
|
539
|
+
const switchBotBLE = await this.platform.connectBLE(this.accessory, this.device)
|
|
539
540
|
try {
|
|
540
541
|
const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId)
|
|
541
542
|
this.device.bleMac = formattedDeviceId
|
|
542
543
|
await this.debugLog(`bleMac: ${this.device.bleMac}`)
|
|
543
|
-
if (
|
|
544
|
-
|
|
544
|
+
if (switchBotBLE !== false) {
|
|
545
|
+
switchBotBLE
|
|
545
546
|
.discover({ model: this.device.bleModel, id: this.device.bleMac })
|
|
546
|
-
.then(async (device_list:
|
|
547
|
+
.then(async (device_list: SwitchbotDevice[]) => {
|
|
548
|
+
const deviceList = device_list as unknown as WoBulb[]
|
|
547
549
|
await this.infoLog(`ColorTemperature: ${this.LightBulb.ColorTemperature}`)
|
|
548
|
-
return await
|
|
550
|
+
return await deviceList[0].setColorTemperature(kelvin)
|
|
549
551
|
})
|
|
550
552
|
.then(async () => {
|
|
551
553
|
await this.successLog(`ColorTemperature: ${this.LightBulb.ColorTemperature} sent over SwitchBot BLE, sent successfully`)
|
|
@@ -557,7 +559,7 @@ export class ColorBulb extends deviceBase {
|
|
|
557
559
|
await this.BLEPushConnection()
|
|
558
560
|
})
|
|
559
561
|
} else {
|
|
560
|
-
await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${
|
|
562
|
+
await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${JSON.stringify(switchBotBLE)}`)
|
|
561
563
|
await this.BLEPushConnection()
|
|
562
564
|
}
|
|
563
565
|
} catch (error) {
|
|
@@ -574,17 +576,18 @@ export class ColorBulb extends deviceBase {
|
|
|
574
576
|
await this.debugLog(`Hue: ${JSON.stringify(this.LightBulb.Hue)}, Saturation: ${JSON.stringify(this.LightBulb.Saturation)}`)
|
|
575
577
|
const [red, green, blue] = hs2rgb(this.LightBulb.Hue, this.LightBulb.Saturation)
|
|
576
578
|
await this.debugLog(`rgb: ${JSON.stringify([red, green, blue])}`)
|
|
577
|
-
const
|
|
579
|
+
const switchBotBLE = await this.platform.connectBLE(this.accessory, this.device)
|
|
578
580
|
try {
|
|
579
581
|
const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId)
|
|
580
582
|
this.device.bleMac = formattedDeviceId
|
|
581
583
|
await this.debugLog(`bleMac: ${this.device.bleMac}`)
|
|
582
|
-
if (
|
|
583
|
-
|
|
584
|
+
if (switchBotBLE !== false) {
|
|
585
|
+
switchBotBLE
|
|
584
586
|
.discover({ model: this.device.bleModel, id: this.device.bleMac })
|
|
585
|
-
.then(async (device_list:
|
|
587
|
+
.then(async (device_list: SwitchbotDevice[]) => {
|
|
588
|
+
const deviceList = device_list as unknown as WoBulb[]
|
|
586
589
|
await this.infoLog(`RGB: ${(this.LightBulb.Brightness, red, green, blue)}`)
|
|
587
|
-
return await
|
|
590
|
+
return await deviceList[0].setRGB(Number(this.LightBulb.Brightness), red, green, blue)
|
|
588
591
|
})
|
|
589
592
|
.then(async () => {
|
|
590
593
|
await this.successLog(`RGB: ${(this.LightBulb.Brightness, red, green, blue)} sent over SwitchBot BLE, sent successfully`)
|
|
@@ -596,7 +599,7 @@ export class ColorBulb extends deviceBase {
|
|
|
596
599
|
await this.BLEPushConnection()
|
|
597
600
|
})
|
|
598
601
|
} else {
|
|
599
|
-
await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${
|
|
602
|
+
await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${JSON.stringify(switchBotBLE)}`)
|
|
600
603
|
await this.BLEPushConnection()
|
|
601
604
|
}
|
|
602
605
|
} catch (error) {
|
package/src/device/contact.ts
CHANGED
|
@@ -158,7 +158,7 @@ export class Contact extends deviceBase {
|
|
|
158
158
|
this.debugLog('Retrieve initial values and update Homekit')
|
|
159
159
|
this.refreshStatus()
|
|
160
160
|
} catch (e: any) {
|
|
161
|
-
this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e}`)
|
|
161
|
+
this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e.message ?? e}`)
|
|
162
162
|
}
|
|
163
163
|
|
|
164
164
|
// regisiter webhook event handler if enabled
|
|
@@ -166,7 +166,7 @@ export class Contact extends deviceBase {
|
|
|
166
166
|
this.debugLog('Registering Webhook Event Handler')
|
|
167
167
|
this.registerWebhook()
|
|
168
168
|
} catch (e: any) {
|
|
169
|
-
this.errorLog(`failed to registerWebhook, Error: ${e}`)
|
|
169
|
+
this.errorLog(`failed to registerWebhook, Error: ${e.message ?? e}`)
|
|
170
170
|
}
|
|
171
171
|
|
|
172
172
|
// regisiter platform BLE event handler if enabled
|
|
@@ -174,7 +174,7 @@ export class Contact extends deviceBase {
|
|
|
174
174
|
this.debugLog('Registering Platform BLE Event Handler')
|
|
175
175
|
this.registerPlatformBLE()
|
|
176
176
|
} catch (e: any) {
|
|
177
|
-
this.errorLog(`failed to registerPlatformBLE, Error: ${e}`)
|
|
177
|
+
this.errorLog(`failed to registerPlatformBLE, Error: ${e.message ?? e}`)
|
|
178
178
|
}
|
|
179
179
|
|
|
180
180
|
// Start an update interval
|
|
@@ -295,22 +295,22 @@ export class Contact extends deviceBase {
|
|
|
295
295
|
|
|
296
296
|
async BLERefreshStatus(): Promise<void> {
|
|
297
297
|
await this.debugLog('BLERefreshStatus')
|
|
298
|
-
const
|
|
299
|
-
if (
|
|
300
|
-
await this.BLERefreshConnection(
|
|
298
|
+
const switchBotBLE = await this.switchbotBLE()
|
|
299
|
+
if (switchBotBLE === undefined) {
|
|
300
|
+
await this.BLERefreshConnection(switchBotBLE)
|
|
301
301
|
} else {
|
|
302
302
|
// Start to monitor advertisement packets
|
|
303
303
|
(async () => {
|
|
304
304
|
// Start to monitor advertisement packets
|
|
305
|
-
const serviceData = await this.monitorAdvertisementPackets(
|
|
305
|
+
const serviceData = await this.monitorAdvertisementPackets(switchBotBLE) as contactSensorServiceData
|
|
306
306
|
// Update HomeKit
|
|
307
307
|
if (serviceData.model === SwitchBotBLEModel.ContactSensor && serviceData.modelName === SwitchBotBLEModelName.ContactSensor) {
|
|
308
308
|
this.serviceData = serviceData
|
|
309
309
|
await this.BLEparseStatus()
|
|
310
310
|
await this.updateHomeKitCharacteristics()
|
|
311
311
|
} else {
|
|
312
|
-
await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`)
|
|
313
|
-
await this.BLERefreshConnection(
|
|
312
|
+
await this.errorLog(`failed to get serviceData, serviceData: ${JSON.stringify(serviceData)}`)
|
|
313
|
+
await this.BLERefreshConnection(switchBotBLE)
|
|
314
314
|
}
|
|
315
315
|
})()
|
|
316
316
|
}
|
|
@@ -331,7 +331,7 @@ export class Contact extends deviceBase {
|
|
|
331
331
|
await this.BLEparseStatus()
|
|
332
332
|
await this.updateHomeKitCharacteristics()
|
|
333
333
|
} catch (e: any) {
|
|
334
|
-
await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
|
|
334
|
+
await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e.message ?? e}`)
|
|
335
335
|
}
|
|
336
336
|
}
|
|
337
337
|
} catch (error) {
|
|
@@ -373,7 +373,7 @@ export class Contact extends deviceBase {
|
|
|
373
373
|
await this.parseStatusWebhook()
|
|
374
374
|
await this.updateHomeKitCharacteristics()
|
|
375
375
|
} catch (e: any) {
|
|
376
|
-
await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`)
|
|
376
|
+
await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e.message ?? e}`)
|
|
377
377
|
}
|
|
378
378
|
}
|
|
379
379
|
} else {
|
package/src/device/curtain.ts
CHANGED
|
@@ -3,22 +3,23 @@
|
|
|
3
3
|
* curtain.ts: @switchbot/homebridge-switchbot.
|
|
4
4
|
*/
|
|
5
5
|
import type { CharacteristicChange, CharacteristicValue, PlatformAccessory, Service } from 'homebridge'
|
|
6
|
-
import type { bodyChange, curtain3ServiceData, curtain3WebhookContext, curtainServiceData, curtainStatus, curtainWebhookContext, device } from 'node-switchbot'
|
|
6
|
+
import type { bodyChange, curtain3ServiceData, curtain3WebhookContext, curtainServiceData, curtainStatus, curtainWebhookContext, device, SwitchbotDevice, WoCurtain } from 'node-switchbot'
|
|
7
7
|
|
|
8
8
|
import type { SwitchBotPlatform } from '../platform.js'
|
|
9
9
|
import type { devicesConfig } from '../settings.js'
|
|
10
10
|
|
|
11
11
|
import { hostname } from 'node:os'
|
|
12
12
|
|
|
13
|
+
import { debounceTime, interval, skipWhile, Subject, take, tap } from 'rxjs'
|
|
14
|
+
|
|
15
|
+
import { formatDeviceIdAsMac } from '../utils.js'
|
|
16
|
+
import { deviceBase } from './device.js'
|
|
17
|
+
|
|
13
18
|
/*
|
|
14
19
|
* For Testing Locally:
|
|
15
20
|
* import { SwitchBotBLEModel, SwitchBotBLEModelName } from '/Users/Shared/GitHub/OpenWonderLabs/node-switchbot/dist/index.js';
|
|
16
21
|
*/
|
|
17
22
|
import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot'
|
|
18
|
-
import { debounceTime, interval, skipWhile, Subject, take, tap } from 'rxjs'
|
|
19
|
-
|
|
20
|
-
import { formatDeviceIdAsMac } from '../utils.js'
|
|
21
|
-
import { deviceBase } from './device.js'
|
|
22
23
|
|
|
23
24
|
export class Curtain extends deviceBase {
|
|
24
25
|
// Services
|
|
@@ -241,7 +242,7 @@ export class Curtain extends deviceBase {
|
|
|
241
242
|
this.debugLog('Retrieve initial values and update Homekit')
|
|
242
243
|
this.refreshStatus()
|
|
243
244
|
} catch (e: any) {
|
|
244
|
-
this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e}`)
|
|
245
|
+
this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e.message ?? e}`)
|
|
245
246
|
}
|
|
246
247
|
|
|
247
248
|
// regisiter webhook event handler if enabled
|
|
@@ -249,7 +250,7 @@ export class Curtain extends deviceBase {
|
|
|
249
250
|
this.debugLog('Registering Webhook Event Handler')
|
|
250
251
|
this.registerWebhook()
|
|
251
252
|
} catch (e: any) {
|
|
252
|
-
this.errorLog(`failed to registerWebhook, Error: ${e}`)
|
|
253
|
+
this.errorLog(`failed to registerWebhook, Error: ${e.message ?? e}`)
|
|
253
254
|
}
|
|
254
255
|
|
|
255
256
|
// regisiter platform BLE event handler if enabled
|
|
@@ -257,7 +258,7 @@ export class Curtain extends deviceBase {
|
|
|
257
258
|
this.debugLog('Registering Platform BLE Event Handler')
|
|
258
259
|
this.registerPlatformBLE()
|
|
259
260
|
} catch (e: any) {
|
|
260
|
-
this.errorLog(`failed to registerPlatformBLE, Error: ${e}`)
|
|
261
|
+
this.errorLog(`failed to registerPlatformBLE, Error: ${e.message ?? e}`)
|
|
261
262
|
}
|
|
262
263
|
|
|
263
264
|
// History
|
|
@@ -477,14 +478,14 @@ export class Curtain extends deviceBase {
|
|
|
477
478
|
|
|
478
479
|
async BLERefreshStatus(): Promise<void> {
|
|
479
480
|
await this.debugLog('BLERefreshStatus')
|
|
480
|
-
const
|
|
481
|
-
if (
|
|
482
|
-
await this.BLERefreshConnection(
|
|
481
|
+
const switchBotBLE = await this.switchbotBLE()
|
|
482
|
+
if (switchBotBLE === undefined) {
|
|
483
|
+
await this.BLERefreshConnection(switchBotBLE)
|
|
483
484
|
} else {
|
|
484
485
|
// Start to monitor advertisement packets
|
|
485
486
|
(async () => {
|
|
486
487
|
// Start to monitor advertisement packets
|
|
487
|
-
const serviceData = await this.monitorAdvertisementPackets(
|
|
488
|
+
const serviceData = await this.monitorAdvertisementPackets(switchBotBLE) as curtainServiceData | curtain3ServiceData
|
|
488
489
|
// Update HomeKit
|
|
489
490
|
if ((serviceData.model === SwitchBotBLEModel.Curtain || SwitchBotBLEModel.Curtain3)
|
|
490
491
|
&& (serviceData.modelName === SwitchBotBLEModelName.Curtain || SwitchBotBLEModelName.Curtain3)) {
|
|
@@ -492,8 +493,8 @@ export class Curtain extends deviceBase {
|
|
|
492
493
|
await this.BLEparseStatus()
|
|
493
494
|
await this.updateHomeKitCharacteristics()
|
|
494
495
|
} else {
|
|
495
|
-
await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`)
|
|
496
|
-
await this.BLERefreshConnection(
|
|
496
|
+
await this.errorLog(`failed to get serviceData, serviceData: ${JSON.stringify(serviceData)}`)
|
|
497
|
+
await this.BLERefreshConnection(switchBotBLE)
|
|
497
498
|
}
|
|
498
499
|
})()
|
|
499
500
|
}
|
|
@@ -530,7 +531,7 @@ export class Curtain extends deviceBase {
|
|
|
530
531
|
await this.parseStatusWebhook()
|
|
531
532
|
await this.updateHomeKitCharacteristics()
|
|
532
533
|
} catch (e: any) {
|
|
533
|
-
await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`)
|
|
534
|
+
await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e.message ?? e}`)
|
|
534
535
|
}
|
|
535
536
|
}
|
|
536
537
|
} else {
|
|
@@ -553,7 +554,7 @@ export class Curtain extends deviceBase {
|
|
|
553
554
|
await this.BLEparseStatus()
|
|
554
555
|
await this.updateHomeKitCharacteristics()
|
|
555
556
|
} catch (e: any) {
|
|
556
|
-
await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
|
|
557
|
+
await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e.message ?? e}`)
|
|
557
558
|
}
|
|
558
559
|
}
|
|
559
560
|
} catch (error) {
|
|
@@ -590,7 +591,7 @@ export class Curtain extends deviceBase {
|
|
|
590
591
|
async BLEpushChanges(): Promise<void> {
|
|
591
592
|
await this.debugLog('BLEpushChanges')
|
|
592
593
|
if (this.WindowCovering.TargetPosition !== this.WindowCovering.CurrentPosition) {
|
|
593
|
-
const
|
|
594
|
+
const switchBotBLE = await this.platform.connectBLE(this.accessory, this.device)
|
|
594
595
|
try {
|
|
595
596
|
const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId)
|
|
596
597
|
this.device.bleMac = formattedDeviceId
|
|
@@ -598,14 +599,15 @@ export class Curtain extends deviceBase {
|
|
|
598
599
|
const { setPositionMode, Mode }: { setPositionMode: number, Mode: string } = await this.setPerformance()
|
|
599
600
|
const adjustedMode = setPositionMode === 1 ? 0x01 : 0xFF
|
|
600
601
|
await this.debugLog(`Mode: ${Mode}, setPositionMode: ${setPositionMode}`)
|
|
601
|
-
if (
|
|
602
|
-
|
|
602
|
+
if (switchBotBLE !== false) {
|
|
603
|
+
switchBotBLE
|
|
603
604
|
.discover({ model: this.device.bleModel, quick: true, id: this.device.bleMac })
|
|
604
|
-
.then(async (device_list:
|
|
605
|
+
.then(async (device_list: SwitchbotDevice[]) => {
|
|
606
|
+
const deviceList = device_list as unknown as WoCurtain[]
|
|
605
607
|
return await this.retryBLE({
|
|
606
608
|
max: await this.maxRetryBLE(),
|
|
607
609
|
fn: async () => {
|
|
608
|
-
return await
|
|
610
|
+
return await deviceList[0].runToPos(100 - Number(this.WindowCovering.TargetPosition), adjustedMode)
|
|
609
611
|
},
|
|
610
612
|
})
|
|
611
613
|
})
|
|
@@ -619,7 +621,7 @@ export class Curtain extends deviceBase {
|
|
|
619
621
|
await this.BLEPushConnection()
|
|
620
622
|
})
|
|
621
623
|
} else {
|
|
622
|
-
await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${
|
|
624
|
+
await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${JSON.stringify(switchBotBLE)}`)
|
|
623
625
|
await this.BLEPushConnection()
|
|
624
626
|
}
|
|
625
627
|
} catch (error) {
|
|
@@ -834,7 +836,7 @@ export class Curtain extends deviceBase {
|
|
|
834
836
|
await this.setMinMax()
|
|
835
837
|
await this.debugLog(`CurrentPosition ${this.WindowCovering.CurrentPosition}`)
|
|
836
838
|
this.hasLoggedStandby = this.hasLoggedStandby ?? false
|
|
837
|
-
if (this.setNewTarget || this.deviceStatus.moving) {
|
|
839
|
+
if (this.deviceStatus ? (this.setNewTarget || this.deviceStatus.moving) : this.setNewTarget) {
|
|
838
840
|
this.hasLoggedStandby = false
|
|
839
841
|
this.infoLog('Checking Status ...')
|
|
840
842
|
this.curtainMoving = true
|
package/src/device/device.ts
CHANGED
|
@@ -275,14 +275,14 @@ export abstract class deviceBase {
|
|
|
275
275
|
}
|
|
276
276
|
|
|
277
277
|
async switchbotBLE(): Promise<any> {
|
|
278
|
-
const
|
|
278
|
+
const switchBotBLE = await this.platform.connectBLE(this.accessory, this.device)
|
|
279
279
|
// Convert to BLE Address
|
|
280
280
|
try {
|
|
281
281
|
const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId)
|
|
282
282
|
this.device.bleMac = formattedDeviceId
|
|
283
283
|
await this.debugLog(`bleMac: ${this.device.bleMac}`)
|
|
284
|
-
await this.getCustomBLEAddress(
|
|
285
|
-
return
|
|
284
|
+
await this.getCustomBLEAddress(switchBotBLE)
|
|
285
|
+
return switchBotBLE
|
|
286
286
|
} catch (error) {
|
|
287
287
|
await this.errorLog(`failed to format device ID as MAC, Error: ${error}`)
|
|
288
288
|
}
|
|
@@ -293,7 +293,7 @@ export abstract class deviceBase {
|
|
|
293
293
|
try {
|
|
294
294
|
await switchbot.startScan({ model: this.device.bleModel, id: this.device.bleMac })
|
|
295
295
|
} catch (e: any) {
|
|
296
|
-
await this.errorLog(`Failed to start BLE scanning. Error:${e}`)
|
|
296
|
+
await this.errorLog(`Failed to start BLE scanning. Error:${e.message ?? e}`)
|
|
297
297
|
}
|
|
298
298
|
// Set an event handler
|
|
299
299
|
let serviceData = { model: this.device.bleModel, modelName: this.device.bleModelName } as ad['serviceData']
|
|
@@ -304,7 +304,7 @@ export abstract class deviceBase {
|
|
|
304
304
|
this.debugLog(`serviceData: ${JSON.stringify(ad.serviceData)}`)
|
|
305
305
|
serviceData = ad.serviceData
|
|
306
306
|
} else {
|
|
307
|
-
serviceData = { model: '', modelName: '' } as ad['serviceData']
|
|
307
|
+
serviceData = { model: '', modelName: '', modelFriendlyName: '' } as unknown as ad['serviceData']
|
|
308
308
|
this.debugLog(`serviceData: ${JSON.stringify(ad.serviceData)}`)
|
|
309
309
|
}
|
|
310
310
|
}
|
|
@@ -314,7 +314,7 @@ export abstract class deviceBase {
|
|
|
314
314
|
try {
|
|
315
315
|
await switchbot.stopScan()
|
|
316
316
|
} catch (e: any) {
|
|
317
|
-
await this.errorLog(`Failed to stop BLE scanning. Error:${e}`)
|
|
317
|
+
await this.errorLog(`Failed to stop BLE scanning. Error:${e.message ?? e}`)
|
|
318
318
|
}
|
|
319
319
|
return serviceData
|
|
320
320
|
}
|
|
@@ -327,7 +327,7 @@ export abstract class deviceBase {
|
|
|
327
327
|
try {
|
|
328
328
|
await switchbot.startScan({ model: this.device.bleModel })
|
|
329
329
|
} catch (e: any) {
|
|
330
|
-
await this.errorLog(`Failed to start BLE scanning. Error:${e}`)
|
|
330
|
+
await this.errorLog(`Failed to start BLE scanning. Error:${e.message ?? e}`)
|
|
331
331
|
}
|
|
332
332
|
// Set an event handler
|
|
333
333
|
switchbot.onadvertisement = async (ad: ad) => {
|
|
@@ -338,7 +338,7 @@ export abstract class deviceBase {
|
|
|
338
338
|
try {
|
|
339
339
|
switchbot.stopScan()
|
|
340
340
|
} catch (e: any) {
|
|
341
|
-
await this.errorLog(`Failed to stop BLE scanning. Error:${e}`)
|
|
341
|
+
await this.errorLog(`Failed to stop BLE scanning. Error:${e.message ?? e}`)
|
|
342
342
|
}
|
|
343
343
|
})()
|
|
344
344
|
}
|
package/src/device/fan.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* plug.ts: @switchbot/homebridge-switchbot.
|
|
4
4
|
*/
|
|
5
5
|
import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge'
|
|
6
|
-
import type { batteryCirculatorFanServiceData, batteryCirculatorFanStatus, batteryCirculatorFanWebhookContext, bodyChange, device } from 'node-switchbot'
|
|
6
|
+
import type { batteryCirculatorFanServiceData, batteryCirculatorFanStatus, batteryCirculatorFanWebhookContext, bodyChange, device, SwitchbotDevice } from 'node-switchbot'
|
|
7
7
|
|
|
8
8
|
import type { SwitchBotPlatform } from '../platform.js'
|
|
9
9
|
import type { devicesConfig } from '../settings.js'
|
|
@@ -146,7 +146,7 @@ export class Fan extends deviceBase {
|
|
|
146
146
|
this.debugLog('Retrieve initial values and update Homekit')
|
|
147
147
|
this.refreshStatus()
|
|
148
148
|
} catch (e: any) {
|
|
149
|
-
this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e}`)
|
|
149
|
+
this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e.message ?? e}`)
|
|
150
150
|
}
|
|
151
151
|
|
|
152
152
|
// regisiter webhook event handler if enabled
|
|
@@ -154,7 +154,7 @@ export class Fan extends deviceBase {
|
|
|
154
154
|
this.debugLog('Registering Webhook Event Handler')
|
|
155
155
|
this.registerWebhook()
|
|
156
156
|
} catch (e: any) {
|
|
157
|
-
this.errorLog(`failed to registerWebhook, Error: ${e}`)
|
|
157
|
+
this.errorLog(`failed to registerWebhook, Error: ${e.message ?? e}`)
|
|
158
158
|
}
|
|
159
159
|
|
|
160
160
|
// regisiter platform BLE event handler if enabled
|
|
@@ -162,7 +162,7 @@ export class Fan extends deviceBase {
|
|
|
162
162
|
this.debugLog('Registering Platform BLE Event Handler')
|
|
163
163
|
this.registerPlatformBLE()
|
|
164
164
|
} catch (e: any) {
|
|
165
|
-
this.errorLog(`failed to registerPlatformBLE, Error: ${e}`)
|
|
165
|
+
this.errorLog(`failed to registerPlatformBLE, Error: ${e.message ?? e}`)
|
|
166
166
|
}
|
|
167
167
|
|
|
168
168
|
// Start an update interval
|
|
@@ -320,22 +320,22 @@ export class Fan extends deviceBase {
|
|
|
320
320
|
|
|
321
321
|
async BLERefreshStatus(): Promise<void> {
|
|
322
322
|
await this.debugLog('BLERefreshStatus')
|
|
323
|
-
const
|
|
324
|
-
if (
|
|
325
|
-
await this.BLERefreshConnection(
|
|
323
|
+
const switchBotBLE = await this.switchbotBLE()
|
|
324
|
+
if (switchBotBLE === undefined) {
|
|
325
|
+
await this.BLERefreshConnection(switchBotBLE)
|
|
326
326
|
} else {
|
|
327
327
|
// Start to monitor advertisement packets
|
|
328
328
|
(async () => {
|
|
329
329
|
// Start to monitor advertisement packets
|
|
330
|
-
const serviceData = await this.monitorAdvertisementPackets(
|
|
330
|
+
const serviceData = await this.monitorAdvertisementPackets(switchBotBLE) as batteryCirculatorFanServiceData
|
|
331
331
|
// Update HomeKit
|
|
332
332
|
if (serviceData.model === SwitchBotBLEModel.Unknown && SwitchBotBLEModelName.Unknown) {
|
|
333
333
|
this.serviceData = serviceData
|
|
334
334
|
await this.BLEparseStatus()
|
|
335
335
|
await this.updateHomeKitCharacteristics()
|
|
336
336
|
} else {
|
|
337
|
-
await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`)
|
|
338
|
-
await this.BLERefreshConnection(
|
|
337
|
+
await this.errorLog(`failed to get serviceData, serviceData: ${JSON.stringify(serviceData)}`)
|
|
338
|
+
await this.BLERefreshConnection(switchBotBLE)
|
|
339
339
|
}
|
|
340
340
|
})()
|
|
341
341
|
}
|
|
@@ -356,7 +356,7 @@ export class Fan extends deviceBase {
|
|
|
356
356
|
await this.BLEparseStatus()
|
|
357
357
|
await this.updateHomeKitCharacteristics()
|
|
358
358
|
} catch (e: any) {
|
|
359
|
-
await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
|
|
359
|
+
await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e.message ?? e}`)
|
|
360
360
|
}
|
|
361
361
|
}
|
|
362
362
|
} catch (error) {
|
|
@@ -398,7 +398,7 @@ export class Fan extends deviceBase {
|
|
|
398
398
|
await this.parseStatusWebhook()
|
|
399
399
|
await this.updateHomeKitCharacteristics()
|
|
400
400
|
} catch (e: any) {
|
|
401
|
-
await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`)
|
|
401
|
+
await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e.message ?? e}`)
|
|
402
402
|
}
|
|
403
403
|
}
|
|
404
404
|
} else {
|
|
@@ -451,22 +451,23 @@ export class Fan extends deviceBase {
|
|
|
451
451
|
await this.debugLog('BLEpushChanges')
|
|
452
452
|
if (this.Fan.Active !== this.accessory.context.Active) {
|
|
453
453
|
await this.debugLog(`BLEpushChanges On: ${this.Fan.Active} OnCached: ${this.accessory.context.Active}`)
|
|
454
|
-
const
|
|
454
|
+
const switchBotBLE = await this.platform.connectBLE(this.accessory, this.device)
|
|
455
455
|
try {
|
|
456
456
|
const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId)
|
|
457
457
|
this.device.bleMac = formattedDeviceId
|
|
458
458
|
await this.debugLog(`bleMac: ${this.device.bleMac}`)
|
|
459
|
-
if (
|
|
460
|
-
|
|
459
|
+
if (switchBotBLE !== false) {
|
|
460
|
+
switchBotBLE
|
|
461
461
|
.discover({ model: this.device.bleModel, id: this.device.bleMac })
|
|
462
|
-
.then(async (device_list:
|
|
462
|
+
.then(async (device_list: SwitchbotDevice[]) => {
|
|
463
|
+
const deviceList = device_list as unknown as SwitchbotDevice[]
|
|
463
464
|
return await this.retryBLE({
|
|
464
465
|
max: await this.maxRetryBLE(),
|
|
465
466
|
fn: async () => {
|
|
466
467
|
if (this.Fan.Active) {
|
|
467
|
-
return await
|
|
468
|
+
return await deviceList[0].turnOn()
|
|
468
469
|
} else {
|
|
469
|
-
return await
|
|
470
|
+
return await deviceList[0].turnOff()
|
|
470
471
|
}
|
|
471
472
|
},
|
|
472
473
|
})
|
|
@@ -481,7 +482,7 @@ export class Fan extends deviceBase {
|
|
|
481
482
|
await this.BLEPushConnection()
|
|
482
483
|
})
|
|
483
484
|
} else {
|
|
484
|
-
await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${
|
|
485
|
+
await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${JSON.stringify(switchBotBLE)}`)
|
|
485
486
|
await this.BLEPushConnection()
|
|
486
487
|
}
|
|
487
488
|
} catch (error) {
|