@switchbot/homebridge-switchbot 3.8.3-beta.3 → 3.8.3-beta.31

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.
Files changed (203) hide show
  1. package/.github/workflows/beta-release.yml +7 -4
  2. package/.github/workflows/release.yml +7 -6
  3. package/CHANGELOG.md +2 -1
  4. package/config.schema.json +24 -0
  5. package/dist/device/blindtilt.d.ts +1 -4
  6. package/dist/device/blindtilt.d.ts.map +1 -1
  7. package/dist/device/blindtilt.js +33 -34
  8. package/dist/device/blindtilt.js.map +1 -1
  9. package/dist/device/bot.d.ts +1 -4
  10. package/dist/device/bot.d.ts.map +1 -1
  11. package/dist/device/bot.js +30 -31
  12. package/dist/device/bot.js.map +1 -1
  13. package/dist/device/ceilinglight.d.ts +1 -4
  14. package/dist/device/ceilinglight.d.ts.map +1 -1
  15. package/dist/device/ceilinglight.js +50 -54
  16. package/dist/device/ceilinglight.js.map +1 -1
  17. package/dist/device/colorbulb.d.ts +1 -4
  18. package/dist/device/colorbulb.d.ts.map +1 -1
  19. package/dist/device/colorbulb.js +62 -66
  20. package/dist/device/colorbulb.js.map +1 -1
  21. package/dist/device/contact.d.ts +1 -4
  22. package/dist/device/contact.d.ts.map +1 -1
  23. package/dist/device/contact.js +18 -18
  24. package/dist/device/contact.js.map +1 -1
  25. package/dist/device/curtain.d.ts +1 -4
  26. package/dist/device/curtain.d.ts.map +1 -1
  27. package/dist/device/curtain.js +31 -32
  28. package/dist/device/curtain.js.map +1 -1
  29. package/dist/device/device.d.ts +6 -6
  30. package/dist/device/device.d.ts.map +1 -1
  31. package/dist/device/device.js +33 -17
  32. package/dist/device/device.js.map +1 -1
  33. package/dist/device/fan.d.ts +1 -4
  34. package/dist/device/fan.d.ts.map +1 -1
  35. package/dist/device/fan.js +47 -49
  36. package/dist/device/fan.js.map +1 -1
  37. package/dist/device/hub.d.ts +1 -4
  38. package/dist/device/hub.d.ts.map +1 -1
  39. package/dist/device/hub.js +18 -18
  40. package/dist/device/hub.js.map +1 -1
  41. package/dist/device/humidifier.d.ts +1 -4
  42. package/dist/device/humidifier.d.ts.map +1 -1
  43. package/dist/device/humidifier.js +43 -46
  44. package/dist/device/humidifier.js.map +1 -1
  45. package/dist/device/iosensor.d.ts +1 -4
  46. package/dist/device/iosensor.d.ts.map +1 -1
  47. package/dist/device/iosensor.js +18 -18
  48. package/dist/device/iosensor.js.map +1 -1
  49. package/dist/device/lightstrip.d.ts +1 -4
  50. package/dist/device/lightstrip.d.ts.map +1 -1
  51. package/dist/device/lightstrip.js +51 -54
  52. package/dist/device/lightstrip.js.map +1 -1
  53. package/dist/device/lock.d.ts +1 -4
  54. package/dist/device/lock.d.ts.map +1 -1
  55. package/dist/device/lock.js +31 -32
  56. package/dist/device/lock.js.map +1 -1
  57. package/dist/device/meter.d.ts +1 -4
  58. package/dist/device/meter.d.ts.map +1 -1
  59. package/dist/device/meter.js +18 -18
  60. package/dist/device/meter.js.map +1 -1
  61. package/dist/device/meterplus.d.ts +1 -4
  62. package/dist/device/meterplus.d.ts.map +1 -1
  63. package/dist/device/meterplus.js +18 -18
  64. package/dist/device/meterplus.js.map +1 -1
  65. package/dist/device/motion.d.ts +1 -4
  66. package/dist/device/motion.d.ts.map +1 -1
  67. package/dist/device/motion.js +18 -18
  68. package/dist/device/motion.js.map +1 -1
  69. package/dist/device/plug.d.ts +1 -4
  70. package/dist/device/plug.d.ts.map +1 -1
  71. package/dist/device/plug.js +29 -30
  72. package/dist/device/plug.js.map +1 -1
  73. package/dist/device/robotvacuumcleaner.d.ts +1 -4
  74. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  75. package/dist/device/robotvacuumcleaner.js +36 -38
  76. package/dist/device/robotvacuumcleaner.js.map +1 -1
  77. package/dist/device/waterdetector.d.ts +1 -4
  78. package/dist/device/waterdetector.d.ts.map +1 -1
  79. package/dist/device/waterdetector.js +18 -18
  80. package/dist/device/waterdetector.js.map +1 -1
  81. package/dist/irdevice/airconditioner.d.ts +1 -1
  82. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  83. package/dist/irdevice/airconditioner.js +11 -12
  84. package/dist/irdevice/airconditioner.js.map +1 -1
  85. package/dist/irdevice/airpurifier.d.ts +1 -1
  86. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  87. package/dist/irdevice/airpurifier.js +11 -12
  88. package/dist/irdevice/airpurifier.js.map +1 -1
  89. package/dist/irdevice/camera.d.ts +1 -1
  90. package/dist/irdevice/camera.d.ts.map +1 -1
  91. package/dist/irdevice/camera.js +9 -10
  92. package/dist/irdevice/camera.js.map +1 -1
  93. package/dist/irdevice/fan.d.ts +1 -1
  94. package/dist/irdevice/fan.d.ts.map +1 -1
  95. package/dist/irdevice/fan.js +15 -16
  96. package/dist/irdevice/fan.js.map +1 -1
  97. package/dist/irdevice/irdevice.d.ts +6 -6
  98. package/dist/irdevice/irdevice.d.ts.map +1 -1
  99. package/dist/irdevice/irdevice.js +7 -13
  100. package/dist/irdevice/irdevice.js.map +1 -1
  101. package/dist/irdevice/light.d.ts +1 -1
  102. package/dist/irdevice/light.d.ts.map +1 -1
  103. package/dist/irdevice/light.js +9 -10
  104. package/dist/irdevice/light.js.map +1 -1
  105. package/dist/irdevice/other.d.ts +1 -1
  106. package/dist/irdevice/other.d.ts.map +1 -1
  107. package/dist/irdevice/other.js +9 -10
  108. package/dist/irdevice/other.js.map +1 -1
  109. package/dist/irdevice/tv.d.ts +1 -1
  110. package/dist/irdevice/tv.d.ts.map +1 -1
  111. package/dist/irdevice/tv.js +27 -28
  112. package/dist/irdevice/tv.js.map +1 -1
  113. package/dist/irdevice/vacuumcleaner.d.ts +1 -1
  114. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  115. package/dist/irdevice/vacuumcleaner.js +9 -10
  116. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  117. package/dist/irdevice/waterheater.d.ts +1 -1
  118. package/dist/irdevice/waterheater.d.ts.map +1 -1
  119. package/dist/irdevice/waterheater.js +9 -10
  120. package/dist/irdevice/waterheater.js.map +1 -1
  121. package/dist/platform.d.ts +6 -14
  122. package/dist/platform.d.ts.map +1 -1
  123. package/dist/platform.js +127 -188
  124. package/dist/platform.js.map +1 -1
  125. package/dist/settings.d.ts +4 -3
  126. package/dist/settings.d.ts.map +1 -1
  127. package/dist/settings.js.map +1 -1
  128. package/dist/utils.d.ts +1 -1
  129. package/dist/utils.d.ts.map +1 -1
  130. package/dist/utils.js.map +1 -1
  131. package/docs/assets/highlight.css +42 -0
  132. package/docs/assets/main.js +1 -1
  133. package/docs/functions/default.html +1 -1
  134. package/docs/index.html +61 -50
  135. package/docs/media/security-privacy-bluetooth.png +0 -0
  136. package/package.json +14 -15
  137. package/src/device/blindtilt.ts +35 -39
  138. package/src/device/bot.ts +31 -35
  139. package/src/device/ceilinglight.ts +51 -58
  140. package/src/device/colorbulb.ts +63 -70
  141. package/src/device/contact.ts +19 -22
  142. package/src/device/curtain.ts +33 -37
  143. package/src/device/device.ts +32 -21
  144. package/src/device/fan.ts +48 -53
  145. package/src/device/hub.ts +19 -22
  146. package/src/device/humidifier.ts +44 -50
  147. package/src/device/iosensor.ts +19 -22
  148. package/src/device/lightstrip.ts +52 -58
  149. package/src/device/lock.ts +32 -36
  150. package/src/device/meter.ts +19 -22
  151. package/src/device/meterplus.ts +19 -22
  152. package/src/device/motion.ts +19 -22
  153. package/src/device/plug.ts +30 -34
  154. package/src/device/robotvacuumcleaner.ts +37 -46
  155. package/src/device/waterdetector.ts +19 -22
  156. package/src/irdevice/airconditioner.ts +12 -14
  157. package/src/irdevice/airpurifier.ts +12 -13
  158. package/src/irdevice/camera.ts +10 -11
  159. package/src/irdevice/fan.ts +16 -17
  160. package/src/irdevice/irdevice.ts +9 -17
  161. package/src/irdevice/light.ts +10 -11
  162. package/src/irdevice/other.ts +10 -11
  163. package/src/irdevice/tv.ts +28 -29
  164. package/src/irdevice/vacuumcleaner.ts +10 -11
  165. package/src/irdevice/waterheater.ts +10 -11
  166. package/src/platform.ts +131 -188
  167. package/src/settings.ts +8 -4
  168. package/src/utils.ts +2 -1
  169. package/dist/types/bledevicestatus.d.ts +0 -281
  170. package/dist/types/bledevicestatus.d.ts.map +0 -1
  171. package/dist/types/bledevicestatus.js +0 -2
  172. package/dist/types/bledevicestatus.js.map +0 -1
  173. package/dist/types/devicelist.d.ts +0 -89
  174. package/dist/types/devicelist.d.ts.map +0 -1
  175. package/dist/types/devicelist.js +0 -6
  176. package/dist/types/devicelist.js.map +0 -1
  177. package/dist/types/deviceresponse.d.ts +0 -13
  178. package/dist/types/deviceresponse.d.ts.map +0 -1
  179. package/dist/types/deviceresponse.js +0 -2
  180. package/dist/types/deviceresponse.js.map +0 -1
  181. package/dist/types/devicestatus.d.ts +0 -149
  182. package/dist/types/devicestatus.d.ts.map +0 -1
  183. package/dist/types/devicestatus.js +0 -3
  184. package/dist/types/devicestatus.js.map +0 -1
  185. package/dist/types/devicewebhookstatus.d.ts +0 -156
  186. package/dist/types/devicewebhookstatus.d.ts.map +0 -1
  187. package/dist/types/devicewebhookstatus.js +0 -2
  188. package/dist/types/devicewebhookstatus.js.map +0 -1
  189. package/dist/types/irdevicelist.d.ts +0 -10
  190. package/dist/types/irdevicelist.d.ts.map +0 -1
  191. package/dist/types/irdevicelist.js +0 -2
  192. package/dist/types/irdevicelist.js.map +0 -1
  193. package/dist/types/pushbody.d.ts +0 -6
  194. package/dist/types/pushbody.d.ts.map +0 -1
  195. package/dist/types/pushbody.js +0 -2
  196. package/dist/types/pushbody.js.map +0 -1
  197. package/src/types/bledevicestatus.ts +0 -303
  198. package/src/types/devicelist.ts +0 -124
  199. package/src/types/deviceresponse.ts +0 -14
  200. package/src/types/devicestatus.ts +0 -178
  201. package/src/types/devicewebhookstatus.ts +0 -191
  202. package/src/types/irdevicelist.ts +0 -15
  203. package/src/types/pushbody.ts +0 -9
@@ -5,18 +5,15 @@
5
5
 
6
6
  import type { API, CharacteristicValue, HAP, Logging, PlatformAccessory, Service } from 'homebridge'
7
7
  import type { MqttClient } from 'mqtt'
8
+ import type { ad, bodyChange, device } from 'node-switchbot'
8
9
 
9
10
  import type { SwitchBotPlatform } from '../platform.js'
10
11
  import type { devicesConfig, SwitchBotPlatformConfig } from '../settings.js'
11
- import type { ad } from '../types/bledevicestatus.js'
12
- import type { device } from '../types/devicelist.js'
13
12
 
14
13
  import { hostname } from 'node:os'
15
14
 
16
15
  import { SwitchBotBLEModel, SwitchBotBLEModelFriendlyName, SwitchBotBLEModelName, SwitchBotModel } from 'node-switchbot'
17
- import { request } from 'undici'
18
16
 
19
- import { Devices } from '../settings.js'
20
17
  import { BlindTiltMappingMode, formatDeviceIdAsMac, sleep } from '../utils.js'
21
18
 
22
19
  export abstract class deviceBase {
@@ -278,14 +275,14 @@ export abstract class deviceBase {
278
275
  }
279
276
 
280
277
  async switchbotBLE(): Promise<any> {
281
- const switchbot = await this.platform.connectBLE(this.accessory, this.device)
278
+ const switchBotBLE = await this.platform.connectBLE(this.accessory, this.device)
282
279
  // Convert to BLE Address
283
280
  try {
284
281
  const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId)
285
282
  this.device.bleMac = formattedDeviceId
286
283
  await this.debugLog(`bleMac: ${this.device.bleMac}`)
287
- await this.getCustomBLEAddress(switchbot)
288
- return switchbot
284
+ await this.getCustomBLEAddress(switchBotBLE)
285
+ return switchBotBLE
289
286
  } catch (error) {
290
287
  await this.errorLog(`failed to format device ID as MAC, Error: ${error}`)
291
288
  }
@@ -293,7 +290,11 @@ export abstract class deviceBase {
293
290
 
294
291
  async monitorAdvertisementPackets(switchbot: any) {
295
292
  await this.debugLog(`Scanning for ${this.device.bleModelName} devices...`)
296
- await switchbot.startScan({ model: this.device.bleModel, id: this.device.bleMac })
293
+ try {
294
+ await switchbot.startScan({ model: this.device.bleModel, id: this.device.bleMac })
295
+ } catch (e: any) {
296
+ await this.errorLog(`Failed to start BLE scanning. Error:${e.message ?? e}`)
297
+ }
297
298
  // Set an event handler
298
299
  let serviceData = { model: this.device.bleModel, modelName: this.device.bleModelName } as ad['serviceData']
299
300
  switchbot.onadvertisement = async (ad: ad) => {
@@ -310,7 +311,11 @@ export abstract class deviceBase {
310
311
  // Wait
311
312
  await switchbot.wait(this.scanDuration * 1000)
312
313
  // Stop to monitor
313
- await switchbot.stopScan()
314
+ try {
315
+ await switchbot.stopScan()
316
+ } catch (e: any) {
317
+ await this.errorLog(`Failed to stop BLE scanning. Error:${e.message ?? e}`)
318
+ }
314
319
  return serviceData
315
320
  }
316
321
 
@@ -319,32 +324,38 @@ export abstract class deviceBase {
319
324
  this.debugLog(`customBLEaddress: ${this.device.customBLEaddress}`);
320
325
  (async () => {
321
326
  // Start to monitor advertisement packets
322
- await switchbot.startScan({ model: this.device.bleModel })
327
+ try {
328
+ await switchbot.startScan({ model: this.device.bleModel })
329
+ } catch (e: any) {
330
+ await this.errorLog(`Failed to start BLE scanning. Error:${e.message ?? e}`)
331
+ }
323
332
  // Set an event handler
324
333
  switchbot.onadvertisement = async (ad: ad) => {
325
334
  this.warnLog(`ad: ${JSON.stringify(ad, null, ' ')}`)
326
335
  }
327
336
  await sleep(10000)
328
337
  // Stop to monitor
329
- switchbot.stopScan()
338
+ try {
339
+ switchbot.stopScan()
340
+ } catch (e: any) {
341
+ await this.errorLog(`Failed to stop BLE scanning. Error:${e.message ?? e}`)
342
+ }
330
343
  })()
331
344
  }
332
345
  }
333
346
 
334
- async pushChangeRequest(bodyChange: string): Promise<{ body: any, statusCode: any }> {
335
- return await request(`${Devices}/${this.device.deviceId}/commands`, {
336
- body: bodyChange,
337
- method: 'POST',
338
- headers: this.platform.generateHeaders(),
339
- })
347
+ async pushChangeRequest(bodyChange: bodyChange): Promise<{ body: any, statusCode: number }> {
348
+ const { response, statusCode } = await this.platform.switchBotAPI.controlDevice(this.device.deviceId, bodyChange.command, bodyChange.parameter, bodyChange.commandType)
349
+ return { body: response, statusCode }
340
350
  }
341
351
 
342
- async deviceRefreshStatus(): Promise<{ body: any, statusCode: any }> {
343
- return await this.platform.retryRequest(this.deviceMaxRetries, this.deviceDelayBetweenRetries, `${Devices}/${this.device.deviceId}/status`, { headers: this.platform.generateHeaders() })
352
+ async deviceRefreshStatus(): Promise<{ body: any, statusCode: number }> {
353
+ const { response, statusCode } = await this.platform.retryRequest(this.device.deviceId, this.deviceMaxRetries, this.deviceDelayBetweenRetries)
354
+ return { body: response, statusCode }
344
355
  }
345
356
 
346
- async successfulStatusCodes(statusCode: any, deviceStatus: any) {
347
- return (statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)
357
+ async successfulStatusCodes(deviceStatus: any) {
358
+ return (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)
348
359
  }
349
360
 
350
361
  /**
package/src/device/fan.ts CHANGED
@@ -3,13 +3,10 @@
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
7
 
7
8
  import type { SwitchBotPlatform } from '../platform.js'
8
9
  import type { devicesConfig } from '../settings.js'
9
- import type { batteryCirculatorFanServiceData } from '../types/bledevicestatus.js'
10
- import type { device } from '../types/devicelist.js'
11
- import type { batteryCirculatorFanStatus } from '../types/devicestatus.js'
12
- import type { batteryCirculatorFanWebhookContext } from '../types/devicewebhookstatus.js'
13
10
 
14
11
  /*
15
12
  * For Testing Locally:
@@ -149,7 +146,7 @@ export class Fan extends deviceBase {
149
146
  this.debugLog('Retrieve initial values and update Homekit')
150
147
  this.refreshStatus()
151
148
  } catch (e: any) {
152
- 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}`)
153
150
  }
154
151
 
155
152
  // regisiter webhook event handler if enabled
@@ -157,7 +154,7 @@ export class Fan extends deviceBase {
157
154
  this.debugLog('Registering Webhook Event Handler')
158
155
  this.registerWebhook()
159
156
  } catch (e: any) {
160
- this.errorLog(`failed to registerWebhook, Error: ${e}`)
157
+ this.errorLog(`failed to registerWebhook, Error: ${e.message ?? e}`)
161
158
  }
162
159
 
163
160
  // regisiter platform BLE event handler if enabled
@@ -165,7 +162,7 @@ export class Fan extends deviceBase {
165
162
  this.debugLog('Registering Platform BLE Event Handler')
166
163
  this.registerPlatformBLE()
167
164
  } catch (e: any) {
168
- this.errorLog(`failed to registerPlatformBLE, Error: ${e}`)
165
+ this.errorLog(`failed to registerPlatformBLE, Error: ${e.message ?? e}`)
169
166
  }
170
167
 
171
168
  // Start an update interval
@@ -204,7 +201,8 @@ export class Fan extends deviceBase {
204
201
  await this.debugLog(`Active: ${this.Fan.Active}`)
205
202
 
206
203
  // RotationSpeed
207
- this.Fan.RotationSpeed = this.serviceData.fanSpeed
204
+ this.Fan.RotationSpeed = this.serviceData.fanSpeed // ?? 0
205
+ await this.debugLog(`RotationSpeed: ${this.Fan.RotationSpeed}`)
208
206
  }
209
207
 
210
208
  async openAPIparseStatus() {
@@ -222,7 +220,7 @@ export class Fan extends deviceBase {
222
220
  await this.debugLog(`SwingMode: ${this.Fan.SwingMode}`)
223
221
 
224
222
  // RotationSpeed
225
- this.Fan.RotationSpeed = this.deviceStatus.fanSpeed
223
+ this.Fan.RotationSpeed = this.deviceStatus.fanSpeed ?? 0
226
224
  await this.debugLog(`RotationSpeed: ${this.Fan.RotationSpeed}`)
227
225
 
228
226
  // ChargingState
@@ -271,7 +269,7 @@ export class Fan extends deviceBase {
271
269
  await this.debugLog(`SwingMode: ${this.Fan.SwingMode}`)
272
270
 
273
271
  // RotationSpeed
274
- this.Fan.RotationSpeed = this.webhookContext.fanSpeed
272
+ this.Fan.RotationSpeed = this.webhookContext.fanSpeed // ?? 0
275
273
  await this.debugLog(`RotationSpeed: ${this.Fan.RotationSpeed}`)
276
274
 
277
275
  // BatteryLevel
@@ -322,22 +320,22 @@ export class Fan extends deviceBase {
322
320
 
323
321
  async BLERefreshStatus(): Promise<void> {
324
322
  await this.debugLog('BLERefreshStatus')
325
- const switchbot = await this.switchbotBLE()
326
- if (switchbot === undefined) {
327
- await this.BLERefreshConnection(switchbot)
323
+ const switchBotBLE = await this.switchbotBLE()
324
+ if (switchBotBLE === undefined) {
325
+ await this.BLERefreshConnection(switchBotBLE)
328
326
  } else {
329
327
  // Start to monitor advertisement packets
330
328
  (async () => {
331
329
  // Start to monitor advertisement packets
332
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as batteryCirculatorFanServiceData
330
+ const serviceData = await this.monitorAdvertisementPackets(switchBotBLE) as batteryCirculatorFanServiceData
333
331
  // Update HomeKit
334
332
  if (serviceData.model === SwitchBotBLEModel.Unknown && SwitchBotBLEModelName.Unknown) {
335
333
  this.serviceData = serviceData
336
334
  await this.BLEparseStatus()
337
335
  await this.updateHomeKitCharacteristics()
338
336
  } else {
339
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`)
340
- await this.BLERefreshConnection(switchbot)
337
+ await this.errorLog(`failed to get serviceData, serviceData: ${JSON.stringify(serviceData)}`)
338
+ await this.BLERefreshConnection(switchBotBLE)
341
339
  }
342
340
  })()
343
341
  }
@@ -358,7 +356,7 @@ export class Fan extends deviceBase {
358
356
  await this.BLEparseStatus()
359
357
  await this.updateHomeKitCharacteristics()
360
358
  } catch (e: any) {
361
- 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}`)
362
360
  }
363
361
  }
364
362
  } catch (error) {
@@ -372,17 +370,17 @@ export class Fan extends deviceBase {
372
370
  async openAPIRefreshStatus(): Promise<void> {
373
371
  await this.debugLog('openAPIRefreshStatus')
374
372
  try {
375
- const { body, statusCode } = await this.deviceRefreshStatus()
376
- const deviceStatus: any = await body.json()
377
- await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
378
- if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
379
- await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
373
+ const { body } = await this.deviceRefreshStatus()
374
+ const deviceStatus: any = await body
375
+ await this.debugLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
376
+ if (await this.successfulStatusCodes(deviceStatus)) {
377
+ await this.debugSuccessLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
380
378
  this.deviceStatus = deviceStatus.body
381
379
  await this.openAPIparseStatus()
382
380
  await this.updateHomeKitCharacteristics()
383
381
  } else {
384
- await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
385
- await this.debugWarnLog(statusCode, deviceStatus)
382
+ await this.debugWarnLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
383
+ await this.debugWarnLog(deviceStatus)
386
384
  }
387
385
  } catch (e: any) {
388
386
  await this.apiError(e)
@@ -400,7 +398,7 @@ export class Fan extends deviceBase {
400
398
  await this.parseStatusWebhook()
401
399
  await this.updateHomeKitCharacteristics()
402
400
  } catch (e: any) {
403
- 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}`)
404
402
  }
405
403
  }
406
404
  } else {
@@ -453,13 +451,13 @@ export class Fan extends deviceBase {
453
451
  await this.debugLog('BLEpushChanges')
454
452
  if (this.Fan.Active !== this.accessory.context.Active) {
455
453
  await this.debugLog(`BLEpushChanges On: ${this.Fan.Active} OnCached: ${this.accessory.context.Active}`)
456
- const switchbot = await this.platform.connectBLE(this.accessory, this.device)
454
+ const switchBotBLE = await this.platform.connectBLE(this.accessory, this.device)
457
455
  try {
458
456
  const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId)
459
457
  this.device.bleMac = formattedDeviceId
460
458
  await this.debugLog(`bleMac: ${this.device.bleMac}`)
461
- if (switchbot !== false) {
462
- switchbot
459
+ if (switchBotBLE !== false) {
460
+ switchBotBLE
463
461
  .discover({ model: this.device.bleModel, id: this.device.bleMac })
464
462
  .then(async (device_list: any) => {
465
463
  return await this.retryBLE({
@@ -483,7 +481,7 @@ export class Fan extends deviceBase {
483
481
  await this.BLEPushConnection()
484
482
  })
485
483
  } else {
486
- await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`)
484
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${JSON.stringify(switchBotBLE)}`)
487
485
  await this.BLEPushConnection()
488
486
  }
489
487
  } catch (error) {
@@ -498,21 +496,20 @@ export class Fan extends deviceBase {
498
496
  await this.debugLog('openAPIpushChanges')
499
497
  if (this.Fan.Active !== this.accessory.context.Active) {
500
498
  const command = this.Fan.Active ? 'turnOn' : 'turnOff'
501
- const bodyChange = JSON.stringify({
499
+ const bodyChange: bodyChange = {
502
500
  command: `${command}`,
503
501
  parameter: 'default',
504
502
  commandType: 'command',
505
- })
503
+ }
506
504
  await this.debugLog(`SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`)
507
505
  try {
508
- const { body, statusCode } = await this.pushChangeRequest(bodyChange)
509
- const deviceStatus: any = await body.json()
510
- await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
511
- if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
512
- await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
506
+ const { body } = await this.pushChangeRequest(bodyChange)
507
+ const deviceStatus: any = await body
508
+ await this.debugLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
509
+ if (await this.successfulStatusCodes(deviceStatus)) {
510
+ await this.debugSuccessLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
513
511
  await this.updateHomeKitCharacteristics()
514
512
  } else {
515
- await this.statusCode(statusCode)
516
513
  await this.statusCode(deviceStatus.statusCode)
517
514
  }
518
515
  } catch (e: any) {
@@ -527,21 +524,20 @@ export class Fan extends deviceBase {
527
524
  async pushRotationSpeedChanges(): Promise<void> {
528
525
  await this.debugLog('pushRotationSpeedChanges')
529
526
  if (this.Fan.SwingMode !== this.accessory.context.SwingMode) {
530
- const bodyChange = JSON.stringify({
527
+ const bodyChange: bodyChange = {
531
528
  command: 'setWindSpeed',
532
529
  parameter: `${this.Fan.RotationSpeed}`,
533
530
  commandType: 'command',
534
- })
531
+ }
535
532
  await this.debugLog(`SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`)
536
533
  try {
537
- const { body, statusCode } = await this.pushChangeRequest(bodyChange)
538
- const deviceStatus: any = await body.json()
539
- await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
540
- if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
541
- await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
534
+ const { body } = await this.pushChangeRequest(bodyChange)
535
+ const deviceStatus: any = await body
536
+ await this.debugLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
537
+ if (await this.successfulStatusCodes(deviceStatus)) {
538
+ await this.debugSuccessLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
542
539
  await this.updateHomeKitCharacteristics()
543
540
  } else {
544
- await this.statusCode(statusCode)
545
541
  await this.statusCode(deviceStatus.statusCode)
546
542
  }
547
543
  } catch (e: any) {
@@ -557,21 +553,20 @@ export class Fan extends deviceBase {
557
553
  await this.debugLog('pushSwingModeChanges')
558
554
  if (this.Fan.SwingMode !== this.accessory.context.SwingMode) {
559
555
  const parameter = this.Fan.SwingMode === this.hap.Characteristic.SwingMode.SWING_ENABLED ? 'on' : 'off'
560
- const bodyChange = JSON.stringify({
556
+ const bodyChange: bodyChange = {
561
557
  command: 'setOscillation',
562
558
  parameter: `${parameter}`,
563
559
  commandType: 'command',
564
- })
560
+ }
565
561
  await this.debugLog(`SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`)
566
562
  try {
567
- const { body, statusCode } = await this.pushChangeRequest(bodyChange)
568
- const deviceStatus: any = await body.json()
569
- await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
570
- if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
571
- await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
563
+ const { body } = await this.pushChangeRequest(bodyChange)
564
+ const deviceStatus: any = await body
565
+ await this.debugLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
566
+ if (await this.successfulStatusCodes(deviceStatus)) {
567
+ await this.debugSuccessLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
572
568
  await this.updateHomeKitCharacteristics()
573
569
  } else {
574
- await this.statusCode(statusCode)
575
570
  await this.statusCode(deviceStatus.statusCode)
576
571
  }
577
572
  } catch (e: any) {
package/src/device/hub.ts CHANGED
@@ -3,13 +3,10 @@
3
3
  * hub.ts: @switchbot/homebridge-switchbot.
4
4
  */
5
5
  import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge'
6
+ import type { device, hub2ServiceData, hub2Status, hub2WebhookContext } from 'node-switchbot'
6
7
 
7
8
  import type { SwitchBotPlatform } from '../platform.js'
8
9
  import type { devicesConfig } from '../settings.js'
9
- import type { hub2ServiceData } from '../types/bledevicestatus.js'
10
- import type { device } from '../types/devicelist.js'
11
- import type { hub2Status } from '../types/devicestatus.js'
12
- import type { hub2WebhookContext } from '../types/devicewebhookstatus.js'
13
10
 
14
11
  import { Units } from 'homebridge'
15
12
  /*
@@ -149,7 +146,7 @@ export class Hub extends deviceBase {
149
146
  this.debugLog('Retrieve initial values and update Homekit')
150
147
  this.refreshStatus()
151
148
  } catch (e: any) {
152
- 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}`)
153
150
  }
154
151
 
155
152
  // regisiter webhook event handler if enabled
@@ -157,7 +154,7 @@ export class Hub extends deviceBase {
157
154
  this.debugLog('Registering Webhook Event Handler')
158
155
  this.registerWebhook()
159
156
  } catch (e: any) {
160
- this.errorLog(`failed to registerWebhook, Error: ${e}`)
157
+ this.errorLog(`failed to registerWebhook, Error: ${e.message ?? e}`)
161
158
  }
162
159
 
163
160
  // regisiter platform BLE event handler if enabled
@@ -165,7 +162,7 @@ export class Hub extends deviceBase {
165
162
  this.debugLog('Registering Platform BLE Event Handler')
166
163
  this.registerPlatformBLE()
167
164
  } catch (e: any) {
168
- this.errorLog(`failed to registerPlatformBLE, Error: ${e}`)
165
+ this.errorLog(`failed to registerPlatformBLE, Error: ${e.message ?? e}`)
169
166
  }
170
167
 
171
168
  // Start an update interval
@@ -290,22 +287,22 @@ export class Hub extends deviceBase {
290
287
 
291
288
  async BLERefreshStatus(): Promise<void> {
292
289
  await this.debugLog('BLERefreshStatus')
293
- const switchbot = await this.switchbotBLE()
294
- if (switchbot === undefined) {
295
- await this.BLERefreshConnection(switchbot)
290
+ const switchBotBLE = await this.switchbotBLE()
291
+ if (switchBotBLE === undefined) {
292
+ await this.BLERefreshConnection(switchBotBLE)
296
293
  } else {
297
294
  // Start to monitor advertisement packets
298
295
  (async () => {
299
296
  // Start to monitor advertisement packets
300
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as hub2ServiceData
297
+ const serviceData = await this.monitorAdvertisementPackets(switchBotBLE) as hub2ServiceData
301
298
  // Update HomeKit
302
299
  if (serviceData.model === SwitchBotBLEModel.Hub2 && serviceData.modelName === SwitchBotBLEModelName.Hub2) {
303
300
  this.serviceData = serviceData
304
301
  await this.BLEparseStatus()
305
302
  await this.updateHomeKitCharacteristics()
306
303
  } else {
307
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`)
308
- await this.BLERefreshConnection(switchbot)
304
+ await this.errorLog(`failed to get serviceData, serviceData: ${JSON.stringify(serviceData)}`)
305
+ await this.BLERefreshConnection(switchBotBLE)
309
306
  }
310
307
  })()
311
308
  }
@@ -326,7 +323,7 @@ export class Hub extends deviceBase {
326
323
  await this.BLEparseStatus()
327
324
  await this.updateHomeKitCharacteristics()
328
325
  } catch (e: any) {
329
- await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
326
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e.message ?? e}`)
330
327
  }
331
328
  }
332
329
  } catch (error) {
@@ -340,17 +337,17 @@ export class Hub extends deviceBase {
340
337
  async openAPIRefreshStatus(): Promise<void> {
341
338
  await this.debugLog('openAPIRefreshStatus')
342
339
  try {
343
- const { body, statusCode } = await this.deviceRefreshStatus()
344
- const deviceStatus: any = await body.json()
345
- await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
346
- if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
347
- await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
340
+ const { body } = await this.deviceRefreshStatus()
341
+ const deviceStatus: any = await body
342
+ await this.debugLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
343
+ if (await this.successfulStatusCodes(deviceStatus)) {
344
+ await this.debugSuccessLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
348
345
  this.deviceStatus = deviceStatus.body
349
346
  await this.openAPIparseStatus()
350
347
  await this.updateHomeKitCharacteristics()
351
348
  } else {
352
- await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
353
- await this.debugWarnLog(statusCode, deviceStatus)
349
+ await this.debugWarnLog(`statusCode: ${deviceStatus.statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
350
+ await this.debugWarnLog(deviceStatus)
354
351
  }
355
352
  } catch (e: any) {
356
353
  await this.apiError(e)
@@ -368,7 +365,7 @@ export class Hub extends deviceBase {
368
365
  await this.parseStatusWebhook()
369
366
  await this.updateHomeKitCharacteristics()
370
367
  } catch (e: any) {
371
- await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`)
368
+ await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e.message ?? e}`)
372
369
  }
373
370
  }
374
371
  } else {