@switchbot/homebridge-switchbot 3.8.3 → 4.0.0-beta.1

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 (205) hide show
  1. package/.github/npm-version-script.cjs +81 -0
  2. package/.github/workflows/beta-release.yml +12 -4
  3. package/CHANGELOG.md +15 -1
  4. package/config.schema.json +583 -937
  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 +68 -66
  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 +63 -59
  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 +56 -65
  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 +73 -80
  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 +38 -38
  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 +60 -60
  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 +86 -20
  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 +50 -51
  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 +48 -48
  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 +53 -55
  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 +35 -35
  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 +57 -65
  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 +38 -39
  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 +35 -35
  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 +35 -35
  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 +27 -27
  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 +32 -32
  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 +39 -40
  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 +28 -28
  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 +31 -32
  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 +22 -23
  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 +28 -14
  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 +12 -13
  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 +12 -15
  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 +9 -22
  122. package/dist/platform.d.ts.map +1 -1
  123. package/dist/platform.js +154 -260
  124. package/dist/platform.js.map +1 -1
  125. package/dist/settings.d.ts +64 -82
  126. package/dist/settings.d.ts.map +1 -1
  127. package/dist/settings.js +0 -20
  128. package/dist/settings.js.map +1 -1
  129. package/dist/utils.d.ts +1 -102
  130. package/dist/utils.d.ts.map +1 -1
  131. package/dist/utils.js +0 -105
  132. package/dist/utils.js.map +1 -1
  133. package/docs/assets/highlight.css +42 -0
  134. package/docs/assets/main.js +1 -1
  135. package/docs/functions/default.html +1 -1
  136. package/docs/index.html +61 -50
  137. package/docs/media/security-privacy-bluetooth.png +0 -0
  138. package/package.json +5 -7
  139. package/src/device/blindtilt.ts +74 -74
  140. package/src/device/bot.ts +68 -67
  141. package/src/device/ceilinglight.ts +59 -70
  142. package/src/device/colorbulb.ts +79 -88
  143. package/src/device/contact.ts +40 -43
  144. package/src/device/curtain.ts +66 -68
  145. package/src/device/device.ts +86 -40
  146. package/src/device/fan.ts +52 -56
  147. package/src/device/hub.ts +50 -53
  148. package/src/device/humidifier.ts +56 -61
  149. package/src/device/iosensor.ts +37 -40
  150. package/src/device/lightstrip.ts +62 -72
  151. package/src/device/lock.ts +41 -45
  152. package/src/device/meter.ts +37 -40
  153. package/src/device/meterplus.ts +37 -40
  154. package/src/device/motion.ts +29 -32
  155. package/src/device/plug.ts +34 -37
  156. package/src/device/robotvacuumcleaner.ts +41 -49
  157. package/src/device/waterdetector.ts +30 -33
  158. package/src/irdevice/airconditioner.ts +33 -35
  159. package/src/irdevice/airpurifier.ts +12 -13
  160. package/src/irdevice/camera.ts +10 -11
  161. package/src/irdevice/fan.ts +24 -25
  162. package/src/irdevice/irdevice.ts +31 -27
  163. package/src/irdevice/light.ts +14 -15
  164. package/src/irdevice/other.ts +14 -17
  165. package/src/irdevice/tv.ts +28 -29
  166. package/src/irdevice/vacuumcleaner.ts +10 -11
  167. package/src/irdevice/waterheater.ts +10 -11
  168. package/src/platform.ts +178 -287
  169. package/src/settings.ts +67 -88
  170. package/src/utils.ts +2 -106
  171. package/dist/types/bledevicestatus.d.ts +0 -281
  172. package/dist/types/bledevicestatus.d.ts.map +0 -1
  173. package/dist/types/bledevicestatus.js +0 -2
  174. package/dist/types/bledevicestatus.js.map +0 -1
  175. package/dist/types/devicelist.d.ts +0 -89
  176. package/dist/types/devicelist.d.ts.map +0 -1
  177. package/dist/types/devicelist.js +0 -6
  178. package/dist/types/devicelist.js.map +0 -1
  179. package/dist/types/deviceresponse.d.ts +0 -13
  180. package/dist/types/deviceresponse.d.ts.map +0 -1
  181. package/dist/types/deviceresponse.js +0 -2
  182. package/dist/types/deviceresponse.js.map +0 -1
  183. package/dist/types/devicestatus.d.ts +0 -149
  184. package/dist/types/devicestatus.d.ts.map +0 -1
  185. package/dist/types/devicestatus.js +0 -3
  186. package/dist/types/devicestatus.js.map +0 -1
  187. package/dist/types/devicewebhookstatus.d.ts +0 -156
  188. package/dist/types/devicewebhookstatus.d.ts.map +0 -1
  189. package/dist/types/devicewebhookstatus.js +0 -2
  190. package/dist/types/devicewebhookstatus.js.map +0 -1
  191. package/dist/types/irdevicelist.d.ts +0 -10
  192. package/dist/types/irdevicelist.d.ts.map +0 -1
  193. package/dist/types/irdevicelist.js +0 -2
  194. package/dist/types/irdevicelist.js.map +0 -1
  195. package/dist/types/pushbody.d.ts +0 -6
  196. package/dist/types/pushbody.d.ts.map +0 -1
  197. package/dist/types/pushbody.js +0 -2
  198. package/dist/types/pushbody.js.map +0 -1
  199. package/src/types/bledevicestatus.ts +0 -303
  200. package/src/types/devicelist.ts +0 -124
  201. package/src/types/deviceresponse.ts +0 -14
  202. package/src/types/devicestatus.ts +0 -178
  203. package/src/types/devicewebhookstatus.ts +0 -191
  204. package/src/types/irdevicelist.ts +0 -15
  205. package/src/types/pushbody.ts +0 -9
@@ -3,10 +3,10 @@
3
3
  * airconditioners.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
- import type { irDevicesConfig } from '../settings.js'
9
- import type { irdevice } from '../types/irdevicelist.js'
9
+ import type { irAirConfig, irDevicesConfig } from '../settings.js'
10
10
 
11
11
  import { irdeviceBase } from './irdevice.js'
12
12
 
@@ -124,8 +124,8 @@ export class AirConditioner extends irdeviceBase {
124
124
 
125
125
  // Initialize HumiditySensor property
126
126
 
127
- if (this.device.irair?.meterType && this.device.irair?.meterId) {
128
- const meterUuid = this.platform.api.hap.uuid.generate(`${this.device.irair.meterId}-${this.device.irair.meterType}`)
127
+ if ((this.device as irAirConfig).meterType && (this.device as irAirConfig).meterId) {
128
+ const meterUuid = this.platform.api.hap.uuid.generate(`${(device as irAirConfig).meterId}-${(device as irAirConfig).meterType}`)
129
129
  this.meter = this.platform.accessories.find(accessory => accessory.UUID === meterUuid)
130
130
  accessory.context.HumiditySensor = accessory.context.HumiditySensor ?? {}
131
131
  this.HumiditySensor = {
@@ -136,8 +136,8 @@ export class AirConditioner extends irdeviceBase {
136
136
  accessory.context.HumiditySensor = this.HumiditySensor as object
137
137
  }
138
138
 
139
- if (this.device.irair?.meterType && this.device.irair?.meterId) {
140
- const meterUuid = this.platform.api.hap.uuid.generate(`${this.device.irair.meterId}-${this.device.irair.meterType}`)
139
+ if ((this.device as irAirConfig).meterType && (this.device as irAirConfig).meterId) {
140
+ const meterUuid = this.platform.api.hap.uuid.generate(`${(device as irAirConfig).meterId}-${(device as irAirConfig).meterType}`)
141
141
  this.meter = this.platform.accessories.find(accessory => accessory.UUID === meterUuid)
142
142
  }
143
143
 
@@ -162,11 +162,11 @@ export class AirConditioner extends irdeviceBase {
162
162
  if (this.HeaterCooler.Active === this.hap.Characteristic.Active.ACTIVE && !this.disablePushOn) {
163
163
  const commandType: string = await this.commandType()
164
164
  const command: string = await this.commandOn()
165
- const bodyChange = JSON.stringify({
165
+ const bodyChange: bodyChange = {
166
166
  command,
167
167
  parameter: 'default',
168
168
  commandType,
169
- })
169
+ }
170
170
  await this.pushChanges(bodyChange)
171
171
  }
172
172
  }
@@ -176,11 +176,11 @@ export class AirConditioner extends irdeviceBase {
176
176
  if (this.HeaterCooler.Active === this.hap.Characteristic.Active.INACTIVE && !this.disablePushOff) {
177
177
  const commandType: string = await this.commandType()
178
178
  const command: string = await this.commandOff()
179
- const bodyChange = JSON.stringify({
179
+ const bodyChange: bodyChange = {
180
180
  command,
181
181
  parameter: 'default',
182
182
  commandType,
183
- })
183
+ }
184
184
  await this.pushChanges(bodyChange)
185
185
  }
186
186
  }
@@ -219,12 +219,11 @@ export class AirConditioner extends irdeviceBase {
219
219
  const parameter = `${this.HeaterCooler.ThresholdTemperature},${this.CurrentMode},${this.CurrentFanSpeed},${this.state}`
220
220
 
221
221
  await this.UpdateCurrentHeaterCoolerState()
222
- const bodyChange = JSON.stringify({
222
+ const bodyChange: bodyChange = {
223
223
  command: 'setAll',
224
224
  parameter: `${parameter}`,
225
225
  commandType: 'command',
226
- })
227
-
226
+ }
228
227
  await this.pushChanges(bodyChange)
229
228
  }
230
229
 
@@ -254,38 +253,38 @@ export class AirConditioner extends irdeviceBase {
254
253
  this.HeaterCooler.RotationSpeed = this.HeaterCooler.RotationSpeed || this.accessory.context.RotationSpeed
255
254
  }
256
255
 
257
- if (this.device.irair?.hide_automode) {
258
- this.hide_automode = this.device.irair?.hide_automode
256
+ if ((this.device as irAirConfig).hide_automode) {
257
+ this.hide_automode = (this.device as irAirConfig).hide_automode
259
258
  this.accessory.context.hide_automode = this.hide_automode
260
259
  } else {
261
- this.hide_automode = this.device.irair?.hide_automode
260
+ this.hide_automode = (this.device as irAirConfig).hide_automode
262
261
  this.accessory.context.hide_automode = this.hide_automode
263
262
  }
264
263
 
265
- if (this.device.irair?.set_max_heat) {
266
- this.set_max_heat = this.device.irair?.set_max_heat
264
+ if ((this.device as irAirConfig).set_max_heat) {
265
+ this.set_max_heat = (this.device as irAirConfig).set_max_heat
267
266
  this.accessory.context.set_max_heat = this.set_max_heat
268
267
  } else {
269
268
  this.set_max_heat = 35
270
269
  this.accessory.context.set_max_heat = this.set_max_heat
271
270
  }
272
- if (this.device.irair?.set_min_heat) {
273
- this.set_min_heat = this.device.irair?.set_min_heat
271
+ if ((this.device as irAirConfig).set_min_heat) {
272
+ this.set_min_heat = (this.device as irAirConfig).set_min_heat
274
273
  this.accessory.context.set_min_heat = this.set_min_heat
275
274
  } else {
276
275
  this.set_min_heat = 0
277
276
  this.accessory.context.set_min_heat = this.set_min_heat
278
277
  }
279
278
 
280
- if (this.device.irair?.set_max_cool) {
281
- this.set_max_cool = this.device.irair?.set_max_cool
279
+ if ((this.device as irAirConfig).set_max_cool) {
280
+ this.set_max_cool = (this.device as irAirConfig).set_max_cool
282
281
  this.accessory.context.set_max_cool = this.set_max_cool
283
282
  } else {
284
283
  this.set_max_cool = 35
285
284
  this.accessory.context.set_max_cool = this.set_max_cool
286
285
  }
287
- if (this.device.irair?.set_min_cool) {
288
- this.set_min_cool = this.device.irair?.set_min_cool
286
+ if ((this.device as irAirConfig).set_min_cool) {
287
+ this.set_min_cool = (this.device as irAirConfig).set_min_cool
289
288
  this.accessory.context.set_min_cool = this.set_min_cool
290
289
  } else {
291
290
  this.set_min_cool = 0
@@ -317,14 +316,13 @@ export class AirConditioner extends irdeviceBase {
317
316
  if (this.device.connectionType === 'OpenAPI' && !this.disablePushDetail) {
318
317
  await this.infoLog(`Sending request to SwitchBot API, body: ${bodyChange},`)
319
318
  try {
320
- const { body, statusCode } = await this.pushChangeRequest(bodyChange)
321
- const deviceStatus: any = await body.json()
322
- await this.pushStatusCodes(statusCode, deviceStatus)
323
- if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
324
- await this.successfulPushChange(statusCode, deviceStatus, bodyChange)
319
+ const { body } = await this.pushChangeRequest(bodyChange)
320
+ const deviceStatus: any = await body
321
+ await this.pushStatusCodes(deviceStatus)
322
+ if (await this.successfulStatusCodes(deviceStatus)) {
323
+ await this.successfulPushChange(deviceStatus, bodyChange)
325
324
  await this.updateHomeKitCharacteristics()
326
325
  } else {
327
- await this.statusCode(statusCode)
328
326
  await this.statusCode(deviceStatus.statusCode)
329
327
  }
330
328
  } catch (e: any) {
@@ -527,10 +525,10 @@ export class AirConditioner extends irdeviceBase {
527
525
  }
528
526
 
529
527
  async getAirConditionerConfigSettings(accessory: PlatformAccessory, device: irdevice & irDevicesConfig): Promise<void> {
530
- accessory.context.hide_automode = this.hide_automode = device.irair?.hide_automode
531
- accessory.context.set_max_heat = this.set_max_heat = device.irair?.set_max_heat ?? 35
532
- accessory.context.set_min_heat = this.set_min_heat = device.irair?.set_min_heat ?? 0
533
- accessory.context.set_max_cool = this.set_max_cool = device.irair?.set_max_cool ?? 35
534
- accessory.context.set_min_cool = this.set_min_cool = device.irair?.set_min_cool ?? 0
528
+ accessory.context.hide_automode = this.hide_automode = (device as irAirConfig).hide_automode
529
+ accessory.context.set_max_heat = this.set_max_heat = (device as irAirConfig).set_max_heat ?? 35
530
+ accessory.context.set_min_heat = this.set_min_heat = (device as irAirConfig).set_min_heat ?? 0
531
+ accessory.context.set_max_cool = this.set_max_cool = (device as irAirConfig).set_max_cool ?? 35
532
+ accessory.context.set_min_cool = this.set_min_cool = (device as irAirConfig).set_min_cool ?? 0
535
533
  }
536
534
  }
@@ -3,10 +3,10 @@
3
3
  * airpurifier.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
 
@@ -149,11 +149,11 @@ export class AirPurifier extends irdeviceBase {
149
149
  if (this.AirPurifier.Active === this.hap.Characteristic.Active.ACTIVE && !this.disablePushOn) {
150
150
  const commandType: string = await this.commandType()
151
151
  const command: string = await this.commandOn()
152
- const bodyChange = JSON.stringify({
152
+ const bodyChange: bodyChange = {
153
153
  command,
154
154
  parameter: 'default',
155
155
  commandType,
156
- })
156
+ }
157
157
  await this.pushChanges(bodyChange)
158
158
  }
159
159
  }
@@ -163,11 +163,11 @@ export class AirPurifier extends irdeviceBase {
163
163
  if (this.AirPurifier.Active === this.hap.Characteristic.Active.INACTIVE && !this.disablePushOn) {
164
164
  const commandType: string = await this.commandType()
165
165
  const command: string = await this.commandOff()
166
- const bodyChange = JSON.stringify({
166
+ const bodyChange: bodyChange = {
167
167
  command,
168
168
  parameter: 'default',
169
169
  commandType,
170
- })
170
+ }
171
171
  await this.pushChanges(bodyChange)
172
172
  }
173
173
  }
@@ -191,11 +191,11 @@ export class AirPurifier extends irdeviceBase {
191
191
  this.CurrentAPFanSpeed = this.CurrentFanSpeed ?? 1
192
192
  this.APActive = this.AirPurifier.Active === 1 ? 'on' : 'off'
193
193
  const parameter = `${this.CurrentAPTemp},${this.CurrentAPMode},${this.CurrentAPFanSpeed},${this.APActive}`
194
- const bodyChange = JSON.stringify({
194
+ const bodyChange: bodyChange = {
195
195
  command: 'setAll',
196
196
  parameter: `${parameter}`,
197
197
  commandType: 'command',
198
- })
198
+ }
199
199
  if (this.AirPurifier.Active === 1) {
200
200
  if ((Number(this.TemperatureSensor!.CurrentTemperature) || 24) < (this.LastTemperature || 30)) {
201
201
  this.AirPurifier.CurrentHeaterCoolerState = this.hap.Characteristic.CurrentHeaterCoolerState.COOLING
@@ -213,14 +213,13 @@ export class AirPurifier extends irdeviceBase {
213
213
  if (this.device.connectionType === 'OpenAPI') {
214
214
  this.infoLog(`Sending request to SwitchBot API, body: ${bodyChange},`)
215
215
  try {
216
- const { body, statusCode } = await this.pushChangeRequest(bodyChange)
217
- const deviceStatus: any = await body.json()
218
- await this.pushStatusCodes(statusCode, deviceStatus)
219
- if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
220
- await this.successfulPushChange(statusCode, deviceStatus, bodyChange)
216
+ const { body } = await this.pushChangeRequest(bodyChange)
217
+ const deviceStatus: any = await body
218
+ await this.pushStatusCodes(deviceStatus)
219
+ if (await this.successfulStatusCodes(deviceStatus)) {
220
+ await this.successfulPushChange(deviceStatus, bodyChange)
221
221
  await this.updateHomeKitCharacteristics()
222
222
  } else {
223
- await this.statusCode(statusCode)
224
223
  await this.statusCode(deviceStatus.statusCode)
225
224
  }
226
225
  } catch (e: any) {
@@ -3,10 +3,10 @@
3
3
  * camera.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
 
@@ -72,11 +72,11 @@ export class Camera extends irdeviceBase {
72
72
  if (this.Switch.On && !this.disablePushOn) {
73
73
  const commandType: string = await this.commandType()
74
74
  const command: string = await this.commandOn()
75
- const bodyChange = JSON.stringify({
75
+ const bodyChange: bodyChange = {
76
76
  command,
77
77
  parameter: 'default',
78
78
  commandType,
79
- })
79
+ }
80
80
  await this.pushChanges(bodyChange)
81
81
  }
82
82
  }
@@ -86,11 +86,11 @@ export class Camera extends irdeviceBase {
86
86
  if (!this.Switch.On && !this.disablePushOff) {
87
87
  const commandType: string = await this.commandType()
88
88
  const command: string = await this.commandOff()
89
- const bodyChange = JSON.stringify({
89
+ const bodyChange: bodyChange = {
90
90
  command,
91
91
  parameter: 'default',
92
92
  commandType,
93
- })
93
+ }
94
94
  await this.pushChanges(bodyChange)
95
95
  }
96
96
  }
@@ -100,14 +100,13 @@ export class Camera extends irdeviceBase {
100
100
  if (this.device.connectionType === 'OpenAPI') {
101
101
  this.infoLog(`Sending request to SwitchBot API, body: ${bodyChange},`)
102
102
  try {
103
- const { body, statusCode } = await this.pushChangeRequest(bodyChange)
104
- const deviceStatus: any = await body.json()
105
- await this.pushStatusCodes(statusCode, deviceStatus)
106
- if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
107
- await this.successfulPushChange(statusCode, deviceStatus, bodyChange)
103
+ const { body } = await this.pushChangeRequest(bodyChange)
104
+ const deviceStatus: any = await body
105
+ await this.pushStatusCodes(deviceStatus)
106
+ if (await this.successfulStatusCodes(deviceStatus)) {
107
+ await this.successfulPushChange(deviceStatus, bodyChange)
108
108
  await this.updateHomeKitCharacteristics()
109
109
  } else {
110
- await this.statusCode(statusCode)
111
110
  await this.statusCode(deviceStatus.statusCode)
112
111
  }
113
112
  } catch (e: any) {
@@ -3,10 +3,10 @@
3
3
  * fan.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
- import type { irDevicesConfig } from '../settings.js'
9
- import type { irdevice } from '../types/irdevicelist.js'
9
+ import type { irDevicesConfig, irFanConfig } from '../settings.js'
10
10
 
11
11
  import { irdeviceBase } from './irdevice.js'
12
12
 
@@ -51,16 +51,16 @@ export class IRFan extends irdeviceBase {
51
51
  return this.Fan.Active
52
52
  }).onSet(this.ActiveSet.bind(this))
53
53
 
54
- if (device.irfan?.rotation_speed) {
54
+ if ((device as irFanConfig).rotation_speed) {
55
55
  // handle Rotation Speed events using the RotationSpeed characteristic
56
56
  this.Fan.Service.getCharacteristic(this.hap.Characteristic.RotationSpeed).setProps({
57
- minStep: device.irfan?.set_minStep ?? 1,
58
- minValue: device.irfan?.set_min ?? 1,
59
- maxValue: device.irfan?.set_max ?? 100,
57
+ minStep: (device as irFanConfig).set_minStep ?? 1,
58
+ minValue: (device as irFanConfig).set_min ?? 1,
59
+ maxValue: (device as irFanConfig).set_max ?? 100,
60
60
  }).onGet(() => {
61
61
  return this.Fan.RotationSpeed
62
62
  }).onSet(this.RotationSpeedSet.bind(this))
63
- } else if (this.Fan.Service.testCharacteristic(this.hap.Characteristic.RotationSpeed) && !device.irfan?.swing_mode) {
63
+ } else if (this.Fan.Service.testCharacteristic(this.hap.Characteristic.RotationSpeed) && !(device as irFanConfig).swing_mode) {
64
64
  const characteristic = this.Fan.Service.getCharacteristic(this.hap.Characteristic.RotationSpeed)
65
65
  this.Fan.Service.removeCharacteristic(characteristic)
66
66
  this.debugLog('Rotation Speed Characteristic was removed.')
@@ -68,12 +68,12 @@ export class IRFan extends irdeviceBase {
68
68
  this.debugLog(`RotationSpeed Characteristic was not removed/added, Clear Cache on ${this.accessory.displayName} to remove Chracteristic`)
69
69
  }
70
70
 
71
- if (device.irfan?.swing_mode) {
71
+ if ((device as irFanConfig).swing_mode) {
72
72
  // handle Osolcation events using the SwingMode characteristic
73
73
  this.Fan.Service.getCharacteristic(this.hap.Characteristic.SwingMode).onGet(() => {
74
74
  return this.Fan.SwingMode
75
75
  }).onSet(this.SwingModeSet.bind(this))
76
- } else if (this.Fan.Service.testCharacteristic(this.hap.Characteristic.SwingMode) && !device.irfan?.swing_mode) {
76
+ } else if (this.Fan.Service.testCharacteristic(this.hap.Characteristic.SwingMode) && !(device as irFanConfig).swing_mode) {
77
77
  const characteristic = this.Fan.Service.getCharacteristic(this.hap.Characteristic.SwingMode)
78
78
  this.Fan.Service.removeCharacteristic(characteristic)
79
79
  this.debugLog('Swing Mode Characteristic was removed.')
@@ -136,11 +136,11 @@ export class IRFan extends irdeviceBase {
136
136
  if (this.Fan.Active === this.hap.Characteristic.Active.ACTIVE && !this.disablePushOn) {
137
137
  const commandType: string = await this.commandType()
138
138
  const command: string = await this.commandOn()
139
- const bodyChange = JSON.stringify({
139
+ const bodyChange: bodyChange = {
140
140
  command,
141
141
  parameter: 'default',
142
142
  commandType,
143
- })
143
+ }
144
144
  await this.pushChanges(bodyChange)
145
145
  }
146
146
  }
@@ -150,39 +150,39 @@ export class IRFan extends irdeviceBase {
150
150
  if (this.Fan.Active === this.hap.Characteristic.Active.INACTIVE && !this.disablePushOff) {
151
151
  const commandType: string = await this.commandType()
152
152
  const command: string = await this.commandOff()
153
- const bodyChange = JSON.stringify({
153
+ const bodyChange: bodyChange = {
154
154
  command,
155
155
  parameter: 'default',
156
156
  commandType,
157
- })
157
+ }
158
158
  await this.pushChanges(bodyChange)
159
159
  }
160
160
  }
161
161
 
162
162
  async pushFanSpeedUpChanges(): Promise<void> {
163
- const bodyChange = JSON.stringify({
163
+ const bodyChange: bodyChange = {
164
164
  command: 'highSpeed',
165
165
  parameter: 'default',
166
166
  commandType: 'command',
167
- })
167
+ }
168
168
  await this.pushChanges(bodyChange)
169
169
  }
170
170
 
171
171
  async pushFanSpeedDownChanges(): Promise<void> {
172
- const bodyChange = JSON.stringify({
172
+ const bodyChange: bodyChange = {
173
173
  command: 'lowSpeed',
174
174
  parameter: 'default',
175
175
  commandType: 'command',
176
- })
176
+ }
177
177
  await this.pushChanges(bodyChange)
178
178
  }
179
179
 
180
180
  async pushFanSwingChanges(): Promise<void> {
181
- const bodyChange = JSON.stringify({
181
+ const bodyChange: bodyChange = {
182
182
  command: 'swing',
183
183
  parameter: 'default',
184
184
  commandType: 'command',
185
- })
185
+ }
186
186
  await this.pushChanges(bodyChange)
187
187
  }
188
188
 
@@ -191,14 +191,13 @@ export class IRFan extends irdeviceBase {
191
191
  if (this.device.connectionType === 'OpenAPI') {
192
192
  this.infoLog(`Sending request to SwitchBot API, body: ${bodyChange},`)
193
193
  try {
194
- const { body, statusCode } = await this.pushChangeRequest(bodyChange)
195
- const deviceStatus: any = await body.json()
196
- await this.pushStatusCodes(statusCode, deviceStatus)
197
- if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
198
- await this.successfulPushChange(statusCode, deviceStatus, bodyChange)
194
+ const { body } = await this.pushChangeRequest(bodyChange)
195
+ const deviceStatus: any = await body
196
+ await this.pushStatusCodes(deviceStatus)
197
+ if (await this.successfulStatusCodes(deviceStatus)) {
198
+ await this.successfulPushChange(deviceStatus, bodyChange)
199
199
  await this.updateHomeKitCharacteristics()
200
200
  } else {
201
- await this.statusCode(statusCode)
202
201
  await this.statusCode(deviceStatus.statusCode)
203
202
  }
204
203
  } catch (e: any) {
@@ -3,14 +3,10 @@
3
3
  * device.ts: @switchbot/homebridge-switchbot.
4
4
  */
5
5
  import type { API, CharacteristicValue, HAP, Logging, PlatformAccessory, Service } from 'homebridge'
6
+ import type { bodyChange, irdevice } from 'node-switchbot'
6
7
 
7
8
  import type { SwitchBotPlatform } from '../platform.js'
8
- import type { irDevicesConfig, SwitchBotPlatformConfig } from '../settings.js'
9
- import type { irdevice } from '../types/irdevicelist.js'
10
-
11
- import { request } from 'undici'
12
-
13
- import { Devices } from '../settings.js'
9
+ import type { irAirConfig, irDevicesConfig, irFanConfig, irLightConfig, irOtherConfig, SwitchBotPlatformConfig } from '../settings.js'
14
10
 
15
11
  export abstract class irdeviceBase {
16
12
  public readonly api: API
@@ -82,18 +78,30 @@ export abstract class irdeviceBase {
82
78
  device.disablePushOff === true && { disablePushOff: device.disablePushOff },
83
79
  device.disablePushDetail === true && { disablePushDetail: device.disablePushDetail },
84
80
  )
81
+ let deviceSpecificConfig = {}
82
+ switch (device.configRemoteType) {
83
+ case 'Fan':
84
+ case 'DIY Fan':
85
+ deviceSpecificConfig = device as irFanConfig
86
+ break
87
+ case 'Light':
88
+ case 'DIY Light':
89
+ deviceSpecificConfig = device as irLightConfig
90
+ break
91
+ case 'Air Conditioner':
92
+ case 'DIY Air Conditioner':
93
+ deviceSpecificConfig = device as irAirConfig
94
+ break
95
+ case 'Others':
96
+ deviceSpecificConfig = device as irOtherConfig
97
+ break
98
+ default:
99
+ break
100
+ }
85
101
  const config = Object.assign(
86
102
  {},
87
103
  deviceConfig,
88
- device.irair,
89
- device.irpur,
90
- device.ircam,
91
- device.irfan,
92
- device.irlight,
93
- device.other,
94
- device.irtv,
95
- device.irvc,
96
- device.irwh,
104
+ deviceSpecificConfig,
97
105
  )
98
106
  if (Object.keys(config).length !== 0) {
99
107
  this.debugSuccessLog(`Config: ${JSON.stringify(config)}`)
@@ -129,16 +137,13 @@ export abstract class irdeviceBase {
129
137
  this.debugSuccessLog(`version: ${accessory.context.version}`)
130
138
  }
131
139
 
132
- async pushChangeRequest(bodyChange: string): Promise<{ body: any, statusCode: any }> {
133
- return await request(`${Devices}/${this.device.deviceId}/commands`, {
134
- body: bodyChange,
135
- method: 'POST',
136
- headers: this.platform.generateHeaders(),
137
- })
140
+ async pushChangeRequest(bodyChange: bodyChange): Promise<{ body: any, statusCode: number }> {
141
+ const { response, statusCode } = await this.platform.switchBotAPI.controlDevice(this.device.deviceId, bodyChange.command, bodyChange.parameter, bodyChange.commandType)
142
+ return { body: response, statusCode }
138
143
  }
139
144
 
140
- async successfulStatusCodes(statusCode: any, deviceStatus: any) {
141
- return (statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)
145
+ async successfulStatusCodes(deviceStatus: any) {
146
+ return (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)
142
147
  }
143
148
 
144
149
  /**
@@ -163,14 +168,13 @@ export abstract class irdeviceBase {
163
168
  }
164
169
  }
165
170
 
166
- async pushStatusCodes(statusCode: any, deviceStatus: any) {
167
- await this.debugWarnLog(`statusCode: ${statusCode}`)
171
+ async pushStatusCodes(deviceStatus: any) {
168
172
  await this.debugWarnLog(`deviceStatus: ${JSON.stringify(deviceStatus)}`)
169
173
  await this.debugWarnLog(`deviceStatus statusCode: ${deviceStatus.statusCode}`)
170
174
  }
171
175
 
172
- async successfulPushChange(statusCode: any, deviceStatus: any, bodyChange: any) {
173
- this.debugSuccessLog(`statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`)
176
+ async successfulPushChange(deviceStatus: any, bodyChange: any) {
177
+ this.debugSuccessLog(`deviceStatus StatusCode: ${deviceStatus.statusCode}`)
174
178
  this.successLog(`request to SwitchBot API, body: ${JSON.stringify(JSON.parse(bodyChange))} sent successfully`)
175
179
  }
176
180
 
@@ -3,10 +3,10 @@
3
3
  * light.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
- import type { irDevicesConfig } from '../settings.js'
9
- import type { irdevice } from '../types/irdevicelist.js'
9
+ import type { irDevicesConfig, irLightConfig } from '../settings.js'
10
10
 
11
11
  import { irdeviceBase } from './irdevice.js'
12
12
 
@@ -46,7 +46,7 @@ export class Light extends irdeviceBase {
46
46
  // Set category
47
47
  accessory.category = this.hap.Categories.LIGHTBULB
48
48
 
49
- if (!device.irlight?.stateless) {
49
+ if (!(device as irLightConfig).stateless) {
50
50
  // Initialize LightBulb Service
51
51
  accessory.context.LightBulb = accessory.context.LightBulb ?? {}
52
52
  this.LightBulb = {
@@ -170,11 +170,11 @@ export class Light extends irdeviceBase {
170
170
  if (On === true && this.disablePushOn === false) {
171
171
  const commandType: string = await this.commandType()
172
172
  const command: string = await this.commandOn()
173
- const bodyChange = JSON.stringify({
173
+ const bodyChange: bodyChange = {
174
174
  command,
175
175
  parameter: 'default',
176
176
  commandType,
177
- })
177
+ }
178
178
  await this.pushChanges(bodyChange, On)
179
179
  }
180
180
  }
@@ -184,11 +184,11 @@ export class Light extends irdeviceBase {
184
184
  if (On === false && this.disablePushOff === false) {
185
185
  const commandType: string = await this.commandType()
186
186
  const command: string = await this.commandOff()
187
- const bodyChange = JSON.stringify({
187
+ const bodyChange: bodyChange = {
188
188
  command,
189
189
  parameter: 'default',
190
190
  commandType,
191
- })
191
+ }
192
192
  await this.pushChanges(bodyChange, On)
193
193
  }
194
194
  }
@@ -198,15 +198,14 @@ export class Light extends irdeviceBase {
198
198
  if (this.device.connectionType === 'OpenAPI') {
199
199
  this.infoLog(`Sending request to SwitchBot API, body: ${bodyChange},`)
200
200
  try {
201
- const { body, statusCode } = await this.pushChangeRequest(bodyChange)
202
- const deviceStatus: any = await body.json()
203
- await this.pushStatusCodes(statusCode, deviceStatus)
204
- if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
205
- await this.successfulPushChange(statusCode, deviceStatus, bodyChange)
201
+ const { body } = await this.pushChangeRequest(bodyChange)
202
+ const deviceStatus: any = await body
203
+ await this.pushStatusCodes(deviceStatus)
204
+ if (await this.successfulStatusCodes(deviceStatus)) {
205
+ await this.successfulPushChange(deviceStatus, bodyChange)
206
206
  this.accessory.context.On = On
207
207
  await this.updateHomeKitCharacteristics()
208
208
  } else {
209
- await this.statusCode(statusCode)
210
209
  await this.statusCode(deviceStatus.statusCode)
211
210
  }
212
211
  } catch (e: any) {
@@ -220,7 +219,7 @@ export class Light extends irdeviceBase {
220
219
 
221
220
  async updateHomeKitCharacteristics(): Promise<void> {
222
221
  await this.debugLog('updateHomeKitCharacteristics')
223
- if (!this.device.irlight?.stateless && this.LightBulb?.Service) {
222
+ if (!(this.device as irLightConfig).stateless && this.LightBulb?.Service) {
224
223
  // On
225
224
  await this.updateCharacteristic(this.LightBulb.Service, this.hap.Characteristic.On, this.LightBulb.On, 'On')
226
225
  } else {
@@ -236,7 +235,7 @@ export class Light extends irdeviceBase {
236
235
  }
237
236
 
238
237
  async apiError(e: any): Promise<void> {
239
- if (!this.device.irlight?.stateless) {
238
+ if (!(this.device as irLightConfig).stateless) {
240
239
  this.LightBulb?.Service.updateCharacteristic(this.hap.Characteristic.On, e)
241
240
  } else {
242
241
  this.ProgrammableSwitchOn?.Service.updateCharacteristic(this.hap.Characteristic.ProgrammableSwitchEvent, e)