@switchbot/homebridge-switchbot 3.8.3-beta.11 → 3.8.3-beta.13

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 (187) hide show
  1. package/dist/device/blindtilt.d.ts +1 -4
  2. package/dist/device/blindtilt.d.ts.map +1 -1
  3. package/dist/device/blindtilt.js +6 -6
  4. package/dist/device/blindtilt.js.map +1 -1
  5. package/dist/device/bot.d.ts +1 -4
  6. package/dist/device/bot.d.ts.map +1 -1
  7. package/dist/device/bot.js +3 -3
  8. package/dist/device/bot.js.map +1 -1
  9. package/dist/device/ceilinglight.d.ts +1 -4
  10. package/dist/device/ceilinglight.d.ts.map +1 -1
  11. package/dist/device/ceilinglight.js +8 -8
  12. package/dist/device/ceilinglight.js.map +1 -1
  13. package/dist/device/colorbulb.d.ts +1 -4
  14. package/dist/device/colorbulb.d.ts.map +1 -1
  15. package/dist/device/colorbulb.js +8 -8
  16. package/dist/device/colorbulb.js.map +1 -1
  17. package/dist/device/contact.d.ts +1 -4
  18. package/dist/device/contact.d.ts.map +1 -1
  19. package/dist/device/contact.js.map +1 -1
  20. package/dist/device/curtain.d.ts +1 -4
  21. package/dist/device/curtain.d.ts.map +1 -1
  22. package/dist/device/curtain.js +4 -4
  23. package/dist/device/curtain.js.map +1 -1
  24. package/dist/device/device.d.ts +3 -3
  25. package/dist/device/device.d.ts.map +1 -1
  26. package/dist/device/device.js +2 -8
  27. package/dist/device/device.js.map +1 -1
  28. package/dist/device/fan.d.ts +1 -4
  29. package/dist/device/fan.d.ts.map +1 -1
  30. package/dist/device/fan.js +6 -6
  31. package/dist/device/fan.js.map +1 -1
  32. package/dist/device/hub.d.ts +1 -4
  33. package/dist/device/hub.d.ts.map +1 -1
  34. package/dist/device/hub.js.map +1 -1
  35. package/dist/device/humidifier.d.ts +1 -4
  36. package/dist/device/humidifier.d.ts.map +1 -1
  37. package/dist/device/humidifier.js +6 -6
  38. package/dist/device/humidifier.js.map +1 -1
  39. package/dist/device/iosensor.d.ts +1 -4
  40. package/dist/device/iosensor.d.ts.map +1 -1
  41. package/dist/device/iosensor.js.map +1 -1
  42. package/dist/device/lightstrip.d.ts +1 -4
  43. package/dist/device/lightstrip.d.ts.map +1 -1
  44. package/dist/device/lightstrip.js +6 -6
  45. package/dist/device/lightstrip.js.map +1 -1
  46. package/dist/device/lock.d.ts +1 -4
  47. package/dist/device/lock.d.ts.map +1 -1
  48. package/dist/device/lock.js +3 -3
  49. package/dist/device/lock.js.map +1 -1
  50. package/dist/device/meter.d.ts +1 -4
  51. package/dist/device/meter.d.ts.map +1 -1
  52. package/dist/device/meter.js.map +1 -1
  53. package/dist/device/meterplus.d.ts +1 -4
  54. package/dist/device/meterplus.d.ts.map +1 -1
  55. package/dist/device/meterplus.js.map +1 -1
  56. package/dist/device/motion.d.ts +1 -4
  57. package/dist/device/motion.d.ts.map +1 -1
  58. package/dist/device/motion.js.map +1 -1
  59. package/dist/device/plug.d.ts +1 -4
  60. package/dist/device/plug.d.ts.map +1 -1
  61. package/dist/device/plug.js +2 -2
  62. package/dist/device/plug.js.map +1 -1
  63. package/dist/device/robotvacuumcleaner.d.ts +1 -4
  64. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  65. package/dist/device/robotvacuumcleaner.js +4 -4
  66. package/dist/device/robotvacuumcleaner.js.map +1 -1
  67. package/dist/device/waterdetector.d.ts +1 -4
  68. package/dist/device/waterdetector.d.ts.map +1 -1
  69. package/dist/device/waterdetector.js.map +1 -1
  70. package/dist/irdevice/airconditioner.d.ts +1 -1
  71. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  72. package/dist/irdevice/airconditioner.js +6 -6
  73. package/dist/irdevice/airconditioner.js.map +1 -1
  74. package/dist/irdevice/airpurifier.d.ts +1 -1
  75. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  76. package/dist/irdevice/airpurifier.js +6 -6
  77. package/dist/irdevice/airpurifier.js.map +1 -1
  78. package/dist/irdevice/camera.d.ts +1 -1
  79. package/dist/irdevice/camera.d.ts.map +1 -1
  80. package/dist/irdevice/camera.js +4 -4
  81. package/dist/irdevice/camera.js.map +1 -1
  82. package/dist/irdevice/fan.d.ts +1 -1
  83. package/dist/irdevice/fan.d.ts.map +1 -1
  84. package/dist/irdevice/fan.js +10 -10
  85. package/dist/irdevice/fan.js.map +1 -1
  86. package/dist/irdevice/irdevice.d.ts +2 -2
  87. package/dist/irdevice/irdevice.d.ts.map +1 -1
  88. package/dist/irdevice/irdevice.js +1 -7
  89. package/dist/irdevice/irdevice.js.map +1 -1
  90. package/dist/irdevice/light.d.ts +1 -1
  91. package/dist/irdevice/light.d.ts.map +1 -1
  92. package/dist/irdevice/light.js +4 -4
  93. package/dist/irdevice/light.js.map +1 -1
  94. package/dist/irdevice/other.d.ts +1 -1
  95. package/dist/irdevice/other.d.ts.map +1 -1
  96. package/dist/irdevice/other.js +4 -4
  97. package/dist/irdevice/other.js.map +1 -1
  98. package/dist/irdevice/tv.d.ts +1 -1
  99. package/dist/irdevice/tv.d.ts.map +1 -1
  100. package/dist/irdevice/tv.js +22 -22
  101. package/dist/irdevice/tv.js.map +1 -1
  102. package/dist/irdevice/vacuumcleaner.d.ts +1 -1
  103. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  104. package/dist/irdevice/vacuumcleaner.js +4 -4
  105. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  106. package/dist/irdevice/waterheater.d.ts +1 -1
  107. package/dist/irdevice/waterheater.d.ts.map +1 -1
  108. package/dist/irdevice/waterheater.js +4 -4
  109. package/dist/irdevice/waterheater.js.map +1 -1
  110. package/dist/platform.d.ts +4 -13
  111. package/dist/platform.d.ts.map +1 -1
  112. package/dist/platform.js +20 -132
  113. package/dist/platform.js.map +1 -1
  114. package/dist/settings.d.ts +1 -3
  115. package/dist/settings.d.ts.map +1 -1
  116. package/dist/settings.js.map +1 -1
  117. package/dist/utils.d.ts +1 -1
  118. package/dist/utils.d.ts.map +1 -1
  119. package/dist/utils.js.map +1 -1
  120. package/package.json +3 -4
  121. package/src/device/blindtilt.ts +8 -11
  122. package/src/device/bot.ts +4 -7
  123. package/src/device/ceilinglight.ts +9 -12
  124. package/src/device/colorbulb.ts +9 -12
  125. package/src/device/contact.ts +1 -4
  126. package/src/device/curtain.ts +6 -9
  127. package/src/device/device.ts +4 -11
  128. package/src/device/fan.ts +7 -10
  129. package/src/device/hub.ts +1 -4
  130. package/src/device/humidifier.ts +7 -10
  131. package/src/device/iosensor.ts +1 -4
  132. package/src/device/lightstrip.ts +7 -10
  133. package/src/device/lock.ts +4 -7
  134. package/src/device/meter.ts +1 -4
  135. package/src/device/meterplus.ts +1 -4
  136. package/src/device/motion.ts +1 -4
  137. package/src/device/plug.ts +3 -6
  138. package/src/device/robotvacuumcleaner.ts +5 -12
  139. package/src/device/waterdetector.ts +1 -4
  140. package/src/irdevice/airconditioner.ts +7 -8
  141. package/src/irdevice/airpurifier.ts +7 -7
  142. package/src/irdevice/camera.ts +5 -5
  143. package/src/irdevice/fan.ts +11 -11
  144. package/src/irdevice/irdevice.ts +3 -11
  145. package/src/irdevice/light.ts +5 -5
  146. package/src/irdevice/other.ts +5 -5
  147. package/src/irdevice/tv.ts +23 -23
  148. package/src/irdevice/vacuumcleaner.ts +5 -5
  149. package/src/irdevice/waterheater.ts +5 -5
  150. package/src/platform.ts +23 -132
  151. package/src/settings.ts +1 -4
  152. package/src/utils.ts +2 -1
  153. package/dist/types/bledevicestatus.d.ts +0 -281
  154. package/dist/types/bledevicestatus.d.ts.map +0 -1
  155. package/dist/types/bledevicestatus.js +0 -2
  156. package/dist/types/bledevicestatus.js.map +0 -1
  157. package/dist/types/devicelist.d.ts +0 -89
  158. package/dist/types/devicelist.d.ts.map +0 -1
  159. package/dist/types/devicelist.js +0 -6
  160. package/dist/types/devicelist.js.map +0 -1
  161. package/dist/types/deviceresponse.d.ts +0 -13
  162. package/dist/types/deviceresponse.d.ts.map +0 -1
  163. package/dist/types/deviceresponse.js +0 -2
  164. package/dist/types/deviceresponse.js.map +0 -1
  165. package/dist/types/devicestatus.d.ts +0 -149
  166. package/dist/types/devicestatus.d.ts.map +0 -1
  167. package/dist/types/devicestatus.js +0 -3
  168. package/dist/types/devicestatus.js.map +0 -1
  169. package/dist/types/devicewebhookstatus.d.ts +0 -156
  170. package/dist/types/devicewebhookstatus.d.ts.map +0 -1
  171. package/dist/types/devicewebhookstatus.js +0 -2
  172. package/dist/types/devicewebhookstatus.js.map +0 -1
  173. package/dist/types/irdevicelist.d.ts +0 -10
  174. package/dist/types/irdevicelist.d.ts.map +0 -1
  175. package/dist/types/irdevicelist.js +0 -2
  176. package/dist/types/irdevicelist.js.map +0 -1
  177. package/dist/types/pushbody.d.ts +0 -6
  178. package/dist/types/pushbody.d.ts.map +0 -1
  179. package/dist/types/pushbody.js +0 -2
  180. package/dist/types/pushbody.js.map +0 -1
  181. package/src/types/bledevicestatus.ts +0 -303
  182. package/src/types/devicelist.ts +0 -124
  183. package/src/types/deviceresponse.ts +0 -14
  184. package/src/types/devicestatus.ts +0 -178
  185. package/src/types/devicewebhookstatus.ts +0 -191
  186. package/src/types/irdevicelist.ts +0 -15
  187. package/src/types/pushbody.ts +0 -9
@@ -3,10 +3,10 @@
3
3
  * tv.ts: @switchbot/homebridge-switchbot.
4
4
  */
5
5
  import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge'
6
+ import type { bodyChange, irdevice } from 'node-switchbot'
6
7
 
7
8
  import type { SwitchBotPlatform } from '../platform.js'
8
9
  import type { irDevicesConfig } from '../settings.js'
9
- import type { irdevice } from '../types/irdevicelist.js'
10
10
 
11
11
  import { irdeviceBase } from './irdevice.js'
12
12
 
@@ -210,11 +210,11 @@ export class TV extends irdeviceBase {
210
210
  if (this.Television.Active === this.hap.Characteristic.Active.ACTIVE && !this.disablePushOn) {
211
211
  const commandType: string = await this.commandType()
212
212
  const command: string = await this.commandOn()
213
- const bodyChange = JSON.stringify({
213
+ const bodyChange: bodyChange = {
214
214
  command,
215
215
  parameter: 'default',
216
216
  commandType,
217
- })
217
+ }
218
218
  await this.pushTVChanges(bodyChange)
219
219
  }
220
220
  }
@@ -224,11 +224,11 @@ export class TV extends irdeviceBase {
224
224
  if (this.Television.Active === this.hap.Characteristic.Active.INACTIVE && !this.disablePushOff) {
225
225
  const commandType: string = await this.commandType()
226
226
  const command: string = await this.commandOff()
227
- const bodyChange = JSON.stringify({
227
+ const bodyChange: bodyChange = {
228
228
  command,
229
229
  parameter: 'default',
230
230
  commandType,
231
- })
231
+ }
232
232
  await this.pushTVChanges(bodyChange)
233
233
  }
234
234
  }
@@ -236,11 +236,11 @@ export class TV extends irdeviceBase {
236
236
  async pushOkChanges(): Promise<void> {
237
237
  await this.debugLog(`pushOkChanges disablePushDetail: ${this.disablePushDetail}`)
238
238
  if (!this.disablePushDetail) {
239
- const bodyChange = JSON.stringify({
239
+ const bodyChange: bodyChange = {
240
240
  command: 'Ok',
241
241
  parameter: 'default',
242
242
  commandType: 'command',
243
- })
243
+ }
244
244
  await this.pushTVChanges(bodyChange)
245
245
  }
246
246
  }
@@ -248,11 +248,11 @@ export class TV extends irdeviceBase {
248
248
  async pushBackChanges(): Promise<void> {
249
249
  await this.debugLog(`pushBackChanges disablePushDetail: ${this.disablePushDetail}`)
250
250
  if (!this.disablePushDetail) {
251
- const bodyChange = JSON.stringify({
251
+ const bodyChange: bodyChange = {
252
252
  command: 'Back',
253
253
  parameter: 'default',
254
254
  commandType: 'command',
255
- })
255
+ }
256
256
  await this.pushTVChanges(bodyChange)
257
257
  }
258
258
  }
@@ -260,11 +260,11 @@ export class TV extends irdeviceBase {
260
260
  async pushMenuChanges(): Promise<void> {
261
261
  await this.debugLog(`pushMenuChanges disablePushDetail: ${this.disablePushDetail}`)
262
262
  if (!this.disablePushDetail) {
263
- const bodyChange = JSON.stringify({
263
+ const bodyChange: bodyChange = {
264
264
  command: 'Menu',
265
265
  parameter: 'default',
266
266
  commandType: 'command',
267
- })
267
+ }
268
268
  await this.pushTVChanges(bodyChange)
269
269
  }
270
270
  }
@@ -272,11 +272,11 @@ export class TV extends irdeviceBase {
272
272
  async pushUpChanges(): Promise<void> {
273
273
  await this.debugLog(`pushUpChanges disablePushDetail: ${this.disablePushDetail}`)
274
274
  if (!this.disablePushDetail) {
275
- const bodyChange = JSON.stringify({
275
+ const bodyChange: bodyChange = {
276
276
  command: 'Up',
277
277
  parameter: 'default',
278
278
  commandType: 'command',
279
- })
279
+ }
280
280
  await this.pushTVChanges(bodyChange)
281
281
  }
282
282
  }
@@ -284,11 +284,11 @@ export class TV extends irdeviceBase {
284
284
  async pushDownChanges(): Promise<void> {
285
285
  await this.debugLog(`pushDownChanges disablePushDetail: ${this.disablePushDetail}`)
286
286
  if (!this.disablePushDetail) {
287
- const bodyChange = JSON.stringify({
287
+ const bodyChange: bodyChange = {
288
288
  command: 'Down',
289
289
  parameter: 'default',
290
290
  commandType: 'command',
291
- })
291
+ }
292
292
  await this.pushTVChanges(bodyChange)
293
293
  }
294
294
  }
@@ -296,11 +296,11 @@ export class TV extends irdeviceBase {
296
296
  async pushRightChanges(): Promise<void> {
297
297
  await this.debugLog(`pushRightChanges disablePushDetail: ${this.disablePushDetail}`)
298
298
  if (!this.disablePushDetail) {
299
- const bodyChange = JSON.stringify({
299
+ const bodyChange: bodyChange = {
300
300
  command: 'Right',
301
301
  parameter: 'default',
302
302
  commandType: 'command',
303
- })
303
+ }
304
304
  await this.pushTVChanges(bodyChange)
305
305
  }
306
306
  }
@@ -308,11 +308,11 @@ export class TV extends irdeviceBase {
308
308
  async pushLeftChanges(): Promise<void> {
309
309
  await this.debugLog(`pushLeftChanges disablePushDetail: ${this.disablePushDetail}`)
310
310
  if (!this.disablePushDetail) {
311
- const bodyChange = JSON.stringify({
311
+ const bodyChange: bodyChange = {
312
312
  command: 'Left',
313
313
  parameter: 'default',
314
314
  commandType: 'command',
315
- })
315
+ }
316
316
  await this.pushTVChanges(bodyChange)
317
317
  }
318
318
  }
@@ -320,11 +320,11 @@ export class TV extends irdeviceBase {
320
320
  async pushVolumeUpChanges(): Promise<void> {
321
321
  await this.debugLog(`pushVolumeUpChanges disablePushDetail: ${this.disablePushDetail}`)
322
322
  if (!this.disablePushDetail) {
323
- const bodyChange = JSON.stringify({
323
+ const bodyChange: bodyChange = {
324
324
  command: 'volumeAdd',
325
325
  parameter: 'default',
326
326
  commandType: 'command',
327
- })
327
+ }
328
328
  await this.pushTVChanges(bodyChange)
329
329
  }
330
330
  }
@@ -332,11 +332,11 @@ export class TV extends irdeviceBase {
332
332
  async pushVolumeDownChanges(): Promise<void> {
333
333
  await this.debugLog(`pushVolumeDownChanges disablePushDetail: ${this.disablePushDetail}`)
334
334
  if (!this.disablePushDetail) {
335
- const bodyChange = JSON.stringify({
335
+ const bodyChange: bodyChange = {
336
336
  command: 'volumeSub',
337
337
  parameter: 'default',
338
338
  commandType: 'command',
339
- })
339
+ }
340
340
  await this.pushTVChanges(bodyChange)
341
341
  }
342
342
  }
@@ -3,10 +3,10 @@
3
3
  * vacuumcleaner.ts: @switchbot/homebridge-switchbot.
4
4
  */
5
5
  import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge'
6
+ import type { bodyChange, irdevice } from 'node-switchbot'
6
7
 
7
8
  import type { SwitchBotPlatform } from '../platform.js'
8
9
  import type { irDevicesConfig } from '../settings.js'
9
- import type { irdevice } from '../types/irdevicelist.js'
10
10
 
11
11
  import { irdeviceBase } from './irdevice.js'
12
12
 
@@ -69,11 +69,11 @@ export class VacuumCleaner extends irdeviceBase {
69
69
  if (this.Switch.On && !this.disablePushOn) {
70
70
  const commandType: string = await this.commandType()
71
71
  const command: string = await this.commandOn()
72
- const bodyChange = JSON.stringify({
72
+ const bodyChange: bodyChange = {
73
73
  command,
74
74
  parameter: 'default',
75
75
  commandType,
76
- })
76
+ }
77
77
  await this.pushChanges(bodyChange)
78
78
  }
79
79
  }
@@ -83,11 +83,11 @@ export class VacuumCleaner extends irdeviceBase {
83
83
  if (!this.Switch.On && !this.disablePushOff) {
84
84
  const commandType: string = await this.commandType()
85
85
  const command: string = await this.commandOff()
86
- const bodyChange = JSON.stringify({
86
+ const bodyChange: bodyChange = {
87
87
  command,
88
88
  parameter: 'default',
89
89
  commandType,
90
- })
90
+ }
91
91
  await this.pushChanges(bodyChange)
92
92
  }
93
93
  }
@@ -3,10 +3,10 @@
3
3
  * waterheater.ts: @switchbot/homebridge-switchbot.
4
4
  */
5
5
  import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge'
6
+ import type { bodyChange, irdevice } from 'node-switchbot'
6
7
 
7
8
  import type { SwitchBotPlatform } from '../platform.js'
8
9
  import type { irDevicesConfig } from '../settings.js'
9
- import type { irdevice } from '../types/irdevicelist.js'
10
10
 
11
11
  import { irdeviceBase } from './irdevice.js'
12
12
 
@@ -70,11 +70,11 @@ export class WaterHeater extends irdeviceBase {
70
70
  if (this.Valve.Active === this.hap.Characteristic.Active.ACTIVE && !this.disablePushOn) {
71
71
  const commandType: string = await this.commandType()
72
72
  const command: string = await this.commandOn()
73
- const bodyChange = JSON.stringify({
73
+ const bodyChange: bodyChange = {
74
74
  command,
75
75
  parameter: 'default',
76
76
  commandType,
77
- })
77
+ }
78
78
  await this.pushChanges(bodyChange)
79
79
  }
80
80
  }
@@ -84,11 +84,11 @@ export class WaterHeater extends irdeviceBase {
84
84
  if (this.Valve.Active === this.hap.Characteristic.Active.INACTIVE && !this.disablePushOff) {
85
85
  const commandType: string = await this.commandType()
86
86
  const command: string = await this.commandOff()
87
- const bodyChange = JSON.stringify({
87
+ const bodyChange: bodyChange = {
88
88
  command,
89
89
  parameter: 'default',
90
90
  commandType,
91
- })
91
+ }
92
92
  await this.pushChanges(bodyChange)
93
93
  }
94
94
  }
package/src/platform.ts CHANGED
@@ -2,21 +2,14 @@
2
2
  *
3
3
  * platform.ts: @switchbot/homebridge-switchbot platform class.
4
4
  */
5
- import type { IncomingMessage, Server, ServerResponse } from 'node:http'
6
- import type { UrlObject } from 'node:url'
5
+ import type { Server } from 'node:http'
7
6
 
8
7
  import type { API, DynamicPlatformPlugin, Logging, PlatformAccessory } from 'homebridge'
9
8
  import type { MqttClient } from 'mqtt'
10
- import type { Dispatcher } from 'undici'
11
9
 
12
10
  import type { devicesConfig, irDevicesConfig, options, SwitchBotPlatformConfig } from './settings.js'
13
- import type { blindTilt, curtain, curtain3, device } from './types/devicelist.js'
14
- import type { irdevice } from './types/irdevicelist.js'
15
11
 
16
- import { Buffer } from 'node:buffer'
17
- import crypto, { randomUUID } from 'node:crypto'
18
12
  import { readFileSync, writeFileSync } from 'node:fs'
19
- import { createServer } from 'node:http'
20
13
  import process from 'node:process'
21
14
 
22
15
  import asyncmqtt from 'async-mqtt'
@@ -26,9 +19,10 @@ import { EveHomeKitTypes } from 'homebridge-lib/EveHomeKitTypes'
26
19
  * For Testing Locally:
27
20
  * import { SwitchBotModel } from '/Users/Shared/GitHub/OpenWonderLabs/node-switchbot/dist/index.js';
28
21
  */
29
- import { SwitchBot, SwitchBotModel } from 'node-switchbot'
22
+ import type { blindTilt, curtain, curtain3, device, irdevice } from 'node-switchbot'
23
+
24
+ import { LogLevel, SwitchBot, SwitchBotModel, SwitchBotOpenAPI } from 'node-switchbot'
30
25
  import { queueScheduler } from 'rxjs'
31
- import { request } from 'undici'
32
26
 
33
27
  import { BlindTilt } from './device/blindtilt.js'
34
28
  import { Bot } from './device/bot.js'
@@ -57,7 +51,7 @@ import { Others } from './irdevice/other.js'
57
51
  import { TV } from './irdevice/tv.js'
58
52
  import { VacuumCleaner } from './irdevice/vacuumcleaner.js'
59
53
  import { WaterHeater } from './irdevice/waterheater.js'
60
- import { deleteWebhook, Devices, PLATFORM_NAME, PLUGIN_NAME, queryWebhook, setupWebhook, updateWebhook } from './settings.js'
54
+ import { PLATFORM_NAME, PLUGIN_NAME } from './settings.js'
61
55
  import { formatDeviceIdAsMac, isBlindTiltDevice, isCurtainDevice, sleep } from './utils.js'
62
56
 
63
57
  /**
@@ -92,6 +86,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
92
86
  // Event Handlers
93
87
  public readonly webhookEventHandler: { [x: string]: (context: any) => void } = {}
94
88
  public readonly bleEventHandler: { [x: string]: (context: any) => void } = {}
89
+ switchBotAPI!: SwitchBotOpenAPI
95
90
 
96
91
  constructor(
97
92
  log: Logging,
@@ -132,6 +127,10 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
132
127
  return
133
128
  }
134
129
 
130
+ // SwitchBot OpenAPI
131
+ this.switchBotAPI = new SwitchBotOpenAPI(this.config.credentials?.token, this.config.credentials?.secret)
132
+ // Set the log level
133
+ this.switchBotAPI.setLogLevel(LogLevel.DEBUG)
135
134
  // import fakegato-history module and EVE characteristics
136
135
  this.fakegatoAPI = fakegato(api)
137
136
  this.eve = new EveHomeKitTypes(api)
@@ -208,121 +207,32 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
208
207
  // webhook configuration
209
208
  if (this.config.options?.webhookURL) {
210
209
  const url = this.config.options?.webhookURL
211
-
212
- try {
213
- const xurl = new URL(url)
214
- const port = Number(xurl.port)
215
- const path = xurl.pathname
216
- this.webhookEventListener = createServer((request: IncomingMessage, response: ServerResponse) => {
217
- try {
218
- if (request.url === path && request.method === 'POST') {
219
- request.on('data', async (data) => {
220
- try {
221
- const body = JSON.parse(data)
222
- await this.debugLog(`Received Webhook: ${JSON.stringify(body)}`)
223
- if (this.config.options?.mqttURL) {
224
- const mac = body.context.deviceMac?.toLowerCase().match(/[\s\S]{1,2}/g)?.join(':')
225
- const options = this.config.options?.mqttPubOptions || {}
226
- this.mqttClient?.publish(`homebridge-switchbot/webhook/${mac}`, `${JSON.stringify(body.context)}`, options)
227
- }
228
- this.webhookEventHandler[body.context.deviceMac]?.(body.context)
229
- } catch (e: any) {
230
- await this.errorLog(`Failed to handle webhook event. Error:${e}`)
231
- }
232
- })
233
- response.writeHead(200, { 'Content-Type': 'text/plain' })
234
- response.end('OK')
235
- }
236
- // else {
237
- // response.writeHead(403, {'Content-Type': 'text/plain'});
238
- // response.end(`NG`);
239
- // }
240
- } catch (e: any) {
241
- this.errorLog(`Failed to handle webhook event. Error:${e}`)
242
- }
243
- }).listen(port || 80)
244
- } catch (e: any) {
245
- await this.errorLog(`Failed to create webhook listener. Error:${e.message}`)
246
- return
247
- }
248
-
249
210
  try {
250
- const { body, statusCode } = await request(setupWebhook, {
251
- method: 'POST',
252
- headers: this.generateHeaders(),
253
- body: JSON.stringify({
254
- action: 'setupWebhook',
255
- url,
256
- deviceList: 'ALL',
257
- }),
211
+ this.switchBotAPI.setupWebhook(url)
212
+ // Listen for webhook events
213
+ this.switchBotAPI.on('webhookEvent', (body) => {
214
+ this.debugLog('Received webhook event:', body)
215
+ this.webhookEventHandler[body.context.deviceMac]?.(body.context)
258
216
  })
259
- const response: any = await body.json()
260
- await this.debugLog(`setupWebhook: url:${url}, body:${JSON.stringify(response)}, statusCode:${statusCode}`)
261
- if (statusCode !== 200 || response?.statusCode !== 100) {
262
- await this.errorLog(`Failed to configure webhook. Existing webhook well be overridden. HTTP:${statusCode} API:${response?.statusCode} message:${response?.message}`)
263
- }
264
217
  } catch (e: any) {
265
- await this.errorLog(`Failed to configure webhook. Error: ${e.message}`)
218
+ await this.errorLog(`Failed to setup webhook. Error:${e.message}`)
266
219
  }
267
220
 
268
221
  try {
269
- const { body, statusCode } = await request(updateWebhook, {
270
- method: 'POST',
271
- headers: this.generateHeaders(),
272
- body: JSON.stringify({
273
- action: 'updateWebhook',
274
- config: {
275
- url,
276
- enable: true,
277
- },
278
- }),
279
- })
280
- const response: any = await body.json()
281
- await this.debugLog(`updateWebhook: url:${url}, body:${JSON.stringify(response)}, statusCode:${statusCode}`)
282
- if (statusCode !== 200 || response?.statusCode !== 100) {
283
- await this.errorLog(`Failed to update webhook. HTTP:${statusCode} API:${response?.statusCode} message:${response?.message}`)
284
- }
222
+ this.switchBotAPI.updateWebhook(url)
285
223
  } catch (e: any) {
286
224
  await this.errorLog(`Failed to update webhook. Error:${e.message}`)
287
225
  }
288
226
 
289
227
  try {
290
- const { body, statusCode } = await request(queryWebhook, {
291
- method: 'POST',
292
- headers: this.generateHeaders(),
293
- body: JSON.stringify({
294
- action: 'queryUrl',
295
- }),
296
- })
297
- const response: any = await body.json()
298
- await this.debugLog(`queryWebhook: body:${JSON.stringify(response)}`)
299
- await this.debugLog(`queryWebhook: statusCode:${statusCode}`)
300
- if (statusCode !== 200 || response?.statusCode !== 100) {
301
- await this.errorLog(`Failed to query webhook. HTTP:${statusCode} API:${response?.statusCode} message:${response?.message}`)
302
- } else {
303
- await this.infoLog(`Listening webhook on ${response?.body?.urls[0]}`)
304
- }
228
+ this.switchBotAPI.queryWebhook()
305
229
  } catch (e: any) {
306
- await this.errorLog(`Failed to query webhook. Error:${e}`)
230
+ await this.errorLog(`Failed to query webhook. Error:${e.message}`)
307
231
  }
308
232
 
309
233
  this.api.on('shutdown', async () => {
310
234
  try {
311
- const { body, statusCode } = await request(deleteWebhook, {
312
- method: 'POST',
313
- headers: this.generateHeaders(),
314
- body: JSON.stringify({
315
- action: 'deleteWebhook',
316
- url,
317
- }),
318
- })
319
- const response: any = await body.json()
320
- await this.debugLog(`deleteWebhook: url:${url}, body:${JSON.stringify(response)}, statusCode:${statusCode}`)
321
- if (statusCode !== 200 || response?.statusCode !== 100) {
322
- await this.errorLog(`Failed to delete webhook. HTTP:${statusCode} API:${response?.statusCode} message:${response?.message}`)
323
- } else {
324
- await this.infoLog('Unregistered webhook to close listening.')
325
- }
235
+ this.switchBotAPI.deleteWebhook(url)
326
236
  } catch (e: any) {
327
237
  await this.errorLog(`Failed to delete webhook. Error:${e.message}`)
328
238
  }
@@ -535,25 +445,6 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
535
445
  }
536
446
  }
537
447
 
538
- generateHeaders = () => {
539
- const t = `${Date.now()}`
540
- const nonce = randomUUID()
541
- const data = this.config.credentials?.token + t + nonce
542
- const signTerm = crypto
543
- .createHmac('sha256', this.config.credentials?.secret)
544
- .update(Buffer.from(data, 'utf-8'))
545
- .digest()
546
- const sign = signTerm.toString('base64')
547
-
548
- return {
549
- 'Authorization': this.config.credentials?.token,
550
- 'sign': sign,
551
- 'nonce': nonce,
552
- 't': t,
553
- 'Content-Type': 'application/json',
554
- }
555
- }
556
-
557
448
  async discoverDevices() {
558
449
  if (!this.config.credentials?.token) {
559
450
  return this.handleManualConfig()
@@ -569,7 +460,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
569
460
 
570
461
  while (retryCount < maxRetries) {
571
462
  try {
572
- const { body, statusCode } = await request(Devices, { headers: this.generateHeaders() })
463
+ const { body, statusCode } = await this.switchBotAPI.getDevices()
573
464
  await this.debugWarnLog(`statusCode: ${statusCode}`)
574
465
  const devicesAPI: any = await body.json()
575
466
  await this.debugWarnLog(`devicesAPI: ${JSON.stringify(devicesAPI)}`)
@@ -2643,13 +2534,13 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
2643
2534
  }
2644
2535
  }
2645
2536
 
2646
- async retryRequest(deviceMaxRetries: number, deviceDelayBetweenRetries: number, url: string | URL | UrlObject, options?: { dispatcher?: Dispatcher } & Omit<Dispatcher.RequestOptions, 'origin' | 'path' | 'method'> & Partial<Pick<Dispatcher.RequestOptions, 'method'>>): Promise<{ body: any, statusCode: number }> {
2537
+ async retryRequest(deviceId: string, deviceMaxRetries: number, deviceDelayBetweenRetries: number): Promise<{ body: any, statusCode: number }> {
2647
2538
  let retryCount = 0
2648
2539
  const maxRetries = deviceMaxRetries
2649
2540
  const delayBetweenRetries = deviceDelayBetweenRetries
2650
2541
  while (retryCount < maxRetries) {
2651
2542
  try {
2652
- const { body, statusCode } = await request(url, options)
2543
+ const { body, statusCode } = await this.switchBotAPI.getDeviceStatus(deviceId)
2653
2544
  if (statusCode === 200 || statusCode === 100) {
2654
2545
  return { body, statusCode }
2655
2546
  } else {
package/src/settings.ts CHANGED
@@ -4,10 +4,7 @@
4
4
  */
5
5
  import type { IClientOptions } from 'async-mqtt'
6
6
  import type { PlatformConfig } from 'homebridge'
7
- import type { SwitchBotBLEModel, SwitchBotBLEModelFriendlyName, SwitchBotBLEModelName } from 'node-switchbot'
8
-
9
- import type { device } from './types/devicelist'
10
- import type { irdevice } from './types/irdevicelist'
7
+ import type { device, irdevice, SwitchBotBLEModel, SwitchBotBLEModelFriendlyName, SwitchBotBLEModelName } from 'node-switchbot'
11
8
  /**
12
9
  * This is the name of the platform that users will use to register the plugin in the Homebridge config.json
13
10
  */
package/src/utils.ts CHANGED
@@ -2,8 +2,9 @@
2
2
  *
3
3
  * util.ts: @switchbot/homebridge-switchbot platform class.
4
4
  */
5
+ import type { blindTilt, curtain, curtain3, device } from 'node-switchbot'
6
+
5
7
  import type { devicesConfig } from './settings.js'
6
- import type { blindTilt, curtain, curtain3, device } from './types/devicelist.js'
7
8
 
8
9
  export enum SwitchBotModel {
9
10
  HubMini = 'W0202200',