@switchbot/homebridge-switchbot 3.5.1-beta.0 → 3.5.1-beta.10

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 (197) hide show
  1. package/README.md +16 -0
  2. package/dist/device/blindtilt.d.ts +15 -6
  3. package/dist/device/blindtilt.d.ts.map +1 -1
  4. package/dist/device/blindtilt.js +224 -370
  5. package/dist/device/blindtilt.js.map +1 -1
  6. package/dist/device/bot.d.ts +9 -4
  7. package/dist/device/bot.d.ts.map +1 -1
  8. package/dist/device/bot.js +197 -221
  9. package/dist/device/bot.js.map +1 -1
  10. package/dist/device/ceilinglight.d.ts +10 -5
  11. package/dist/device/ceilinglight.d.ts.map +1 -1
  12. package/dist/device/ceilinglight.js +214 -292
  13. package/dist/device/ceilinglight.js.map +1 -1
  14. package/dist/device/colorbulb.d.ts +10 -5
  15. package/dist/device/colorbulb.d.ts.map +1 -1
  16. package/dist/device/colorbulb.js +335 -421
  17. package/dist/device/colorbulb.js.map +1 -1
  18. package/dist/device/contact.d.ts +9 -4
  19. package/dist/device/contact.d.ts.map +1 -1
  20. package/dist/device/contact.js +121 -200
  21. package/dist/device/contact.js.map +1 -1
  22. package/dist/device/curtain.d.ts +20 -7
  23. package/dist/device/curtain.d.ts.map +1 -1
  24. package/dist/device/curtain.js +229 -403
  25. package/dist/device/curtain.js.map +1 -1
  26. package/dist/device/device.d.ts +41 -22
  27. package/dist/device/device.d.ts.map +1 -1
  28. package/dist/device/device.js +212 -181
  29. package/dist/device/device.js.map +1 -1
  30. package/dist/device/fan.d.ts +9 -4
  31. package/dist/device/fan.d.ts.map +1 -1
  32. package/dist/device/fan.js +204 -249
  33. package/dist/device/fan.js.map +1 -1
  34. package/dist/device/hub.d.ts +9 -5
  35. package/dist/device/hub.d.ts.map +1 -1
  36. package/dist/device/hub.js +107 -233
  37. package/dist/device/hub.js.map +1 -1
  38. package/dist/device/humidifier.d.ts +12 -4
  39. package/dist/device/humidifier.d.ts.map +1 -1
  40. package/dist/device/humidifier.js +193 -235
  41. package/dist/device/humidifier.js.map +1 -1
  42. package/dist/device/iosensor.d.ts +9 -4
  43. package/dist/device/iosensor.d.ts.map +1 -1
  44. package/dist/device/iosensor.js +92 -159
  45. package/dist/device/iosensor.js.map +1 -1
  46. package/dist/device/lightstrip.d.ts +10 -5
  47. package/dist/device/lightstrip.d.ts.map +1 -1
  48. package/dist/device/lightstrip.js +288 -370
  49. package/dist/device/lightstrip.js.map +1 -1
  50. package/dist/device/lock.d.ts +10 -5
  51. package/dist/device/lock.d.ts.map +1 -1
  52. package/dist/device/lock.js +172 -219
  53. package/dist/device/lock.js.map +1 -1
  54. package/dist/device/meter.d.ts +9 -4
  55. package/dist/device/meter.d.ts.map +1 -1
  56. package/dist/device/meter.js +94 -160
  57. package/dist/device/meter.js.map +1 -1
  58. package/dist/device/meterplus.d.ts +9 -4
  59. package/dist/device/meterplus.d.ts.map +1 -1
  60. package/dist/device/meterplus.js +95 -157
  61. package/dist/device/meterplus.js.map +1 -1
  62. package/dist/device/motion.d.ts +9 -4
  63. package/dist/device/motion.d.ts.map +1 -1
  64. package/dist/device/motion.js +85 -144
  65. package/dist/device/motion.js.map +1 -1
  66. package/dist/device/plug.d.ts +9 -4
  67. package/dist/device/plug.d.ts.map +1 -1
  68. package/dist/device/plug.js +100 -127
  69. package/dist/device/plug.js.map +1 -1
  70. package/dist/device/robotvacuumcleaner.d.ts +9 -6
  71. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  72. package/dist/device/robotvacuumcleaner.js +172 -259
  73. package/dist/device/robotvacuumcleaner.js.map +1 -1
  74. package/dist/device/waterdetector.d.ts +12 -4
  75. package/dist/device/waterdetector.d.ts.map +1 -1
  76. package/dist/device/waterdetector.js +106 -131
  77. package/dist/device/waterdetector.js.map +1 -1
  78. package/dist/irdevice/airconditioner.d.ts +2 -1
  79. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  80. package/dist/irdevice/airconditioner.js +2 -3
  81. package/dist/irdevice/airconditioner.js.map +1 -1
  82. package/dist/irdevice/airpurifier.d.ts +2 -1
  83. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  84. package/dist/irdevice/airpurifier.js +2 -3
  85. package/dist/irdevice/airpurifier.js.map +1 -1
  86. package/dist/irdevice/camera.d.ts +2 -1
  87. package/dist/irdevice/camera.d.ts.map +1 -1
  88. package/dist/irdevice/camera.js +2 -3
  89. package/dist/irdevice/camera.js.map +1 -1
  90. package/dist/irdevice/fan.d.ts +2 -1
  91. package/dist/irdevice/fan.d.ts.map +1 -1
  92. package/dist/irdevice/fan.js +2 -3
  93. package/dist/irdevice/fan.js.map +1 -1
  94. package/dist/irdevice/irdevice.d.ts +3 -1
  95. package/dist/irdevice/irdevice.d.ts.map +1 -1
  96. package/dist/irdevice/irdevice.js +5 -1
  97. package/dist/irdevice/irdevice.js.map +1 -1
  98. package/dist/irdevice/light.d.ts +2 -1
  99. package/dist/irdevice/light.d.ts.map +1 -1
  100. package/dist/irdevice/light.js +2 -3
  101. package/dist/irdevice/light.js.map +1 -1
  102. package/dist/irdevice/other.d.ts +2 -1
  103. package/dist/irdevice/other.d.ts.map +1 -1
  104. package/dist/irdevice/other.js +2 -3
  105. package/dist/irdevice/other.js.map +1 -1
  106. package/dist/irdevice/tv.d.ts +2 -1
  107. package/dist/irdevice/tv.d.ts.map +1 -1
  108. package/dist/irdevice/tv.js +2 -3
  109. package/dist/irdevice/tv.js.map +1 -1
  110. package/dist/irdevice/vacuumcleaner.d.ts +2 -1
  111. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  112. package/dist/irdevice/vacuumcleaner.js +2 -3
  113. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  114. package/dist/irdevice/waterheater.d.ts +2 -1
  115. package/dist/irdevice/waterheater.d.ts.map +1 -1
  116. package/dist/irdevice/waterheater.js +2 -3
  117. package/dist/irdevice/waterheater.js.map +1 -1
  118. package/dist/platform.d.ts +14 -14
  119. package/dist/platform.d.ts.map +1 -1
  120. package/dist/platform.js +102 -86
  121. package/dist/platform.js.map +1 -1
  122. package/dist/settings.d.ts +49 -190
  123. package/dist/settings.d.ts.map +1 -1
  124. package/dist/settings.js +19 -0
  125. package/dist/settings.js.map +1 -1
  126. package/dist/types/bledevicestatus.d.ts +279 -0
  127. package/dist/types/bledevicestatus.d.ts.map +1 -0
  128. package/dist/types/bledevicestatus.js +2 -0
  129. package/dist/types/bledevicestatus.js.map +1 -0
  130. package/dist/types/devicelist.d.ts +89 -0
  131. package/dist/types/devicelist.d.ts.map +1 -0
  132. package/dist/types/devicelist.js +6 -0
  133. package/dist/types/devicelist.js.map +1 -0
  134. package/dist/types/deviceresponse.d.ts +13 -0
  135. package/dist/types/deviceresponse.d.ts.map +1 -0
  136. package/dist/types/deviceresponse.js +2 -0
  137. package/dist/types/deviceresponse.js.map +1 -0
  138. package/dist/types/devicestatus.d.ts +149 -0
  139. package/dist/types/devicestatus.d.ts.map +1 -0
  140. package/dist/types/devicestatus.js +3 -0
  141. package/dist/types/devicestatus.js.map +1 -0
  142. package/dist/types/devicewebhookstatus.d.ts +156 -0
  143. package/dist/types/devicewebhookstatus.d.ts.map +1 -0
  144. package/dist/types/devicewebhookstatus.js +2 -0
  145. package/dist/types/devicewebhookstatus.js.map +1 -0
  146. package/dist/types/irdevicelist.d.ts +10 -0
  147. package/dist/types/irdevicelist.d.ts.map +1 -0
  148. package/dist/types/irdevicelist.js +2 -0
  149. package/dist/types/irdevicelist.js.map +1 -0
  150. package/dist/types/pushbody.d.ts +6 -0
  151. package/dist/types/pushbody.d.ts.map +1 -0
  152. package/dist/types/pushbody.js +2 -0
  153. package/dist/types/pushbody.js.map +1 -0
  154. package/dist/utils.d.ts +25 -0
  155. package/dist/utils.d.ts.map +1 -1
  156. package/dist/utils.js +28 -0
  157. package/dist/utils.js.map +1 -1
  158. package/package.json +11 -11
  159. package/src/device/blindtilt.ts +254 -376
  160. package/src/device/bot.ts +209 -224
  161. package/src/device/ceilinglight.ts +227 -297
  162. package/src/device/colorbulb.ts +345 -435
  163. package/src/device/contact.ts +139 -200
  164. package/src/device/curtain.ts +254 -414
  165. package/src/device/device.ts +221 -190
  166. package/src/device/fan.ts +222 -253
  167. package/src/device/hub.ts +127 -248
  168. package/src/device/humidifier.ts +217 -246
  169. package/src/device/iosensor.ts +115 -168
  170. package/src/device/lightstrip.ts +303 -380
  171. package/src/device/lock.ts +188 -224
  172. package/src/device/meter.ts +117 -179
  173. package/src/device/meterplus.ts +118 -176
  174. package/src/device/motion.ts +100 -146
  175. package/src/device/plug.ts +111 -131
  176. package/src/device/robotvacuumcleaner.ts +191 -258
  177. package/src/device/waterdetector.ts +124 -141
  178. package/src/irdevice/airconditioner.ts +4 -4
  179. package/src/irdevice/airpurifier.ts +4 -4
  180. package/src/irdevice/camera.ts +4 -4
  181. package/src/irdevice/fan.ts +4 -4
  182. package/src/irdevice/irdevice.ts +8 -2
  183. package/src/irdevice/light.ts +4 -4
  184. package/src/irdevice/other.ts +4 -4
  185. package/src/irdevice/tv.ts +4 -4
  186. package/src/irdevice/vacuumcleaner.ts +4 -4
  187. package/src/irdevice/waterheater.ts +4 -4
  188. package/src/platform.ts +104 -87
  189. package/src/settings.ts +30 -238
  190. package/src/types/bledevicestatus.ts +305 -0
  191. package/src/types/devicelist.ts +124 -0
  192. package/src/types/deviceresponse.ts +15 -0
  193. package/src/types/devicestatus.ts +178 -0
  194. package/src/types/devicewebhookstatus.ts +196 -0
  195. package/src/types/irdevicelist.ts +15 -0
  196. package/src/types/pushbody.ts +9 -0
  197. package/src/utils.ts +33 -0
package/src/device/fan.ts CHANGED
@@ -3,11 +3,16 @@
3
3
  * plug.ts: @switchbot/homebridge-switchbot.
4
4
  */
5
5
  import { deviceBase } from './device.js';
6
+ import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot';
6
7
  import { Subject, debounceTime, interval, skipWhile, take, tap } from 'rxjs';
7
8
 
9
+ import type { devicesConfig } from '../settings.js';
10
+ import type { device } from '../types/devicelist.js';
8
11
  import type { SwitchBotPlatform } from '../platform.js';
9
12
  import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';
10
- import type { device, devicesConfig, serviceData, deviceStatus } from '../settings.js';
13
+ import type { batteryCirculatorFanServiceData } from '../types/bledevicestatus.js';
14
+ import type { batteryCirculatorFanStatus } from '../types/devicestatus.js';
15
+ import type { batteryCirculatorFanWebhookContext } from '../types/devicewebhookstatus.js';
11
16
 
12
17
  export class Fan extends deviceBase {
13
18
  // Services
@@ -149,6 +154,9 @@ export class Fan extends deviceBase {
149
154
  // Update Homekit
150
155
  this.updateHomeKitCharacteristics();
151
156
 
157
+ //regisiter webhook event handler
158
+ this.registerWebhook();
159
+
152
160
  // Start an update interval
153
161
  interval(this.deviceRefreshRate * 1000)
154
162
  .pipe(skipWhile(() => this.fanUpdateInProgress))
@@ -156,9 +164,6 @@ export class Fan extends deviceBase {
156
164
  await this.refreshStatus();
157
165
  });
158
166
 
159
- //regisiter webhook event handler
160
- this.registerWebhook(accessory, device);
161
-
162
167
  // Watch for Plug change events
163
168
  // We put in a debounce of 100ms so we don't make duplicate calls
164
169
  this.doFanUpdate
@@ -172,64 +177,54 @@ export class Fan extends deviceBase {
172
177
  try {
173
178
  await this.pushChanges();
174
179
  } catch (e: any) {
175
- this.apiError(e);
176
- this.errorLog(`${device.deviceType}: ${accessory.displayName} failed pushChanges with ${device.connectionType} Connection,`
177
- + ` Error Message: ${JSON.stringify(e.message)}`);
180
+ await this.apiError(e);
181
+ await this.errorLog(`failed pushChanges with ${device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
178
182
  }
179
183
  this.fanUpdateInProgress = false;
180
184
  });
181
185
  }
182
186
 
183
- async BLEparseStatus(serviceData: serviceData): Promise<void> {
184
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
185
-
186
- // State
187
- switch (serviceData.state) {
188
- case 'on':
189
- this.Fan.Active = true;
190
- break;
191
- default:
192
- this.Fan.Active = false;
193
- }
194
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.Fan.Active}`);
187
+ async BLEparseStatus(serviceData: batteryCirculatorFanServiceData): Promise<void> {
188
+ await this.debugLog('BLEparseStatus');
189
+ // Active
190
+ this.Fan.Active = serviceData.state === 'on' ? this.hap.Characteristic.Active.ACTIVE : this.hap.Characteristic.Active.INACTIVE;
191
+ await this.debugLog(`Active: ${this.Fan.Active}`);
195
192
  }
196
193
 
197
- async openAPIparseStatus(deviceStatus: deviceStatus) {
198
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
194
+ async openAPIparseStatus(deviceStatus: batteryCirculatorFanStatus) {
195
+ await this.debugLog('openAPIparseStatus');
199
196
 
200
197
  // Active
201
- this.Fan.Active = deviceStatus.body.power === 'on' ? this.hap.Characteristic.Active.ACTIVE : this.hap.Characteristic.Active.INACTIVE;
202
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Active: ${this.Fan.Active}`);
198
+ this.Fan.Active = deviceStatus.power === 'on' ? this.hap.Characteristic.Active.ACTIVE : this.hap.Characteristic.Active.INACTIVE;
199
+ this.debugLog(`Active: ${this.Fan.Active}`);
203
200
 
204
201
  // SwingMode
205
- this.Fan.SwingMode = deviceStatus.body.oscillation === 'on' ?
202
+ this.Fan.SwingMode = deviceStatus.oscillation === 'on' ?
206
203
  this.hap.Characteristic.SwingMode.SWING_ENABLED : this.hap.Characteristic.SwingMode.SWING_DISABLED;
204
+ await this.debugLog(`SwingMode: ${this.Fan.SwingMode}`);
207
205
 
208
206
  // RotationSpeed
209
- this.Fan.RotationSpeed = deviceStatus.body.fanSpeed;
207
+ this.Fan.RotationSpeed = deviceStatus.fanSpeed;
208
+ await this.debugLog(`RotationSpeed: ${this.Fan.RotationSpeed}`);
210
209
 
211
210
  // ChargingState
212
- this.Battery.ChargingState = deviceStatus.body.chargingStatus === 'charging' ?
211
+ this.Battery.ChargingState = deviceStatus.chargingStatus === 'charging' ?
213
212
  this.hap.Characteristic.ChargingState.CHARGING : this.hap.Characteristic.ChargingState.NOT_CHARGING;
214
213
 
215
214
  // BatteryLevel
216
- this.Battery.BatteryLevel = Number(deviceStatus.body.battery);
217
- if (this.Battery.BatteryLevel < 10) {
218
- this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
219
- } else {
220
- this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
221
- }
222
- if (Number.isNaN(this.Battery.BatteryLevel)) {
223
- this.Battery.BatteryLevel = 100;
224
- }
225
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel},`
226
- + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
215
+ this.Battery.BatteryLevel = Number(deviceStatus.battery);
216
+ await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
217
+
218
+ // StatusLowBattery
219
+ this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
220
+ ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
221
+ await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
227
222
 
228
223
  // Firmware Version
229
- const version = deviceStatus.body.version?.toString();
230
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Firmware Version: ${version?.replace(/^V|-.*$/g, '')}`);
231
- if (deviceStatus.body.version) {
232
- const deviceVersion = version?.replace(/^V|-.*$/g, '') ?? '0.0.0';
224
+ const version = deviceStatus.version.toString();
225
+ await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
226
+ if (deviceStatus.version) {
227
+ const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
233
228
  this.accessory
234
229
  .getService(this.hap.Service.AccessoryInformation)!
235
230
  .setCharacteristic(this.hap.Characteristic.HardwareRevision, deviceVersion)
@@ -237,7 +232,48 @@ export class Fan extends deviceBase {
237
232
  .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
238
233
  .updateValue(deviceVersion);
239
234
  this.accessory.context.deviceVersion = deviceVersion;
240
- this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceVersion: ${this.accessory.context.deviceVersion}`);
235
+ await this.debugSuccessLog(`deviceVersion: ${this.accessory.context.deviceVersion}`);
236
+ }
237
+ }
238
+
239
+ async parseStatusWebhook(context: batteryCirculatorFanWebhookContext): Promise<void> {
240
+ await this.debugLog('parseStatusWebhook');
241
+ await this.debugLog(`(version, battery, powerState, oscillation, chargingStatus, fanSpeed) = Webhook:(${context.version}, ${context.battery},`
242
+ + ` ${context.powerState}, ${context.oscillation}, ${context.chargingStatus}, ${context.fanSpeed}),`
243
+ + ` current:(${this.accessory.context.deviceVersion}, ${this.Battery.BatteryLevel}, ${this.Fan.Active}, ${this.Fan.SwingMode},`
244
+ + ` ${this.Battery.ChargingState}, ${this.Fan.RotationSpeed})`);
245
+ // Active
246
+ this.Fan.Active = context.powerState === 'ON' ? this.hap.Characteristic.Active.ACTIVE : this.hap.Characteristic.Active.INACTIVE;
247
+ await this.debugLog(`Active: ${this.Fan.Active}`);
248
+ // SwingMode
249
+ this.Fan.SwingMode = context.oscillation === 'on' ?
250
+ this.hap.Characteristic.SwingMode.SWING_ENABLED : this.hap.Characteristic.SwingMode.SWING_DISABLED;
251
+ await this.debugLog(`SwingMode: ${this.Fan.SwingMode}`);
252
+ // RotationSpeed
253
+ this.Fan.RotationSpeed = context.fanSpeed;
254
+ await this.debugLog(`RotationSpeed: ${this.Fan.RotationSpeed}`);
255
+ // BatteryLevel
256
+ this.Battery.BatteryLevel = context.battery;
257
+ await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
258
+ // StatusLowBattery
259
+ this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
260
+ ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
261
+ await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
262
+ // ChargingState
263
+ this.Battery.ChargingState = context.chargingStatus === 'charging' ?
264
+ this.hap.Characteristic.ChargingState.CHARGING : this.hap.Characteristic.ChargingState.NOT_CHARGING;
265
+ await this.debugLog(`ChargingState: ${this.Battery.ChargingState}`);
266
+ // FirmwareVersion
267
+ if (context.version) {
268
+ const deviceVersion = context.version.replace(/^V|-.*$/g, '') ?? '0.0.0';
269
+ this.accessory
270
+ .getService(this.hap.Service.AccessoryInformation)!
271
+ .setCharacteristic(this.hap.Characteristic.HardwareRevision, deviceVersion)
272
+ .setCharacteristic(this.hap.Characteristic.FirmwareRevision, deviceVersion)
273
+ .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
274
+ .updateValue(deviceVersion);
275
+ this.accessory.context.deviceVersion = deviceVersion;
276
+ await this.debugSuccessLog(`deviceVersion: ${this.accessory.context.deviceVersion}`);
241
277
  }
242
278
  }
243
279
 
@@ -246,35 +282,34 @@ export class Fan extends deviceBase {
246
282
  */
247
283
  async refreshStatus(): Promise<void> {
248
284
  if (!this.device.enableCloudService && this.OpenAPI) {
249
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus enableCloudService: ${this.device.enableCloudService}`);
285
+ await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
250
286
  } else if (this.BLE) {
251
287
  await this.BLERefreshStatus();
252
288
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
253
289
  await this.openAPIRefreshStatus();
254
290
  } else {
255
291
  await this.offlineOff();
256
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
257
- + ` ${this.device.connectionType}, refreshStatus will not happen.`);
292
+ await this.debugWarnLog(`Connection Type: ${this.device.connectionType}, refreshStatus will not happen.`);
258
293
  }
259
294
  }
260
295
 
261
296
  async BLERefreshStatus(): Promise<void> {
262
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
297
+ await this.debugLog('BLERefreshStatus');
263
298
  const switchbot = await this.switchbotBLE();
264
299
 
265
300
  if (switchbot === undefined) {
266
301
  await this.BLERefreshConnection(switchbot);
267
302
  } else {
268
- // Start to monitor advertisement packets
269
- (async () => {
270
303
  // Start to monitor advertisement packets
271
- const serviceData: serviceData = await this.monitorAdvertisementPackets(switchbot);
304
+ (async () => {
305
+ // Start to monitor advertisement packets
306
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as unknown as batteryCirculatorFanServiceData;
272
307
  // Update HomeKit
273
- if (serviceData.model !== '' && serviceData.modelName !== '') {
308
+ if (serviceData.model === SwitchBotBLEModel.Unknown && SwitchBotBLEModelName.Unknown) {
274
309
  await this.BLEparseStatus(serviceData);
275
310
  await this.updateHomeKitCharacteristics();
276
311
  } else {
277
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed to get serviceData, serviceData: ${serviceData}`);
312
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
278
313
  await this.BLERefreshConnection(switchbot);
279
314
  }
280
315
  })();
@@ -282,72 +317,39 @@ export class Fan extends deviceBase {
282
317
  }
283
318
 
284
319
  async openAPIRefreshStatus(): Promise<void> {
285
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
320
+ await this.debugLog('openAPIRefreshStatus');
286
321
  try {
287
322
  const { body, statusCode } = await this.deviceRefreshStatus();
288
323
  const deviceStatus: any = await body.json();
289
- await this.refreshStatusCodes(statusCode, deviceStatus);;
290
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
291
- await this.successfulRefreshStatus(statusCode, deviceStatus);
292
- await this.openAPIparseStatus(deviceStatus);
324
+ await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
325
+ if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
326
+ await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
327
+ await this.openAPIparseStatus(deviceStatus.body);
293
328
  await this.updateHomeKitCharacteristics();
294
329
  } else {
295
- await this.statusCodes(statusCode, deviceStatus);
330
+ await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
331
+ await this.debugWarnLog(statusCode, deviceStatus);
296
332
  }
297
333
  } catch (e: any) {
298
- this.apiError(e);
299
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
300
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
334
+ await this.apiError(e);
335
+ await this.errorLog(`failed openAPIRefreshStatus with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
301
336
  }
302
337
  }
303
338
 
304
- async registerWebhook(accessory: PlatformAccessory, device: device & devicesConfig) {
305
- if (device.webhook) {
306
- this.debugLog(`${device.deviceType}: ${accessory.displayName} is listening webhook.`);
307
- this.platform.webhookEventHandler[device.deviceId] = async (context) => {
339
+ async registerWebhook() {
340
+ if (this.device.webhook) {
341
+ await this.debugLog('is listening webhook.');
342
+ this.platform.webhookEventHandler[this.device.deviceId] = async (context: batteryCirculatorFanWebhookContext) => {
308
343
  try {
309
- this.debugLog(`${device.deviceType}: ${accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
310
- const { version, battery, powerState, oscillation, chargingStatus, fanSpeed } = context;
311
- const { Active, SwingMode, RotationSpeed } = this.Fan;
312
- const { BatteryLevel, ChargingState } = this.Battery;
313
- const { FirmwareRevision } = accessory.context;
314
- this.debugLog(`${device.deviceType}: ${accessory.displayName} (version, battery, powerState, oscillation, chargingStatus, fanSpeed) = `
315
- + `Webhook:(${version}, ${battery}, ${powerState}, ${oscillation}, ${chargingStatus}, ${fanSpeed}), `
316
- + `current:(${FirmwareRevision}, ${BatteryLevel}, ${Active}, ${SwingMode}, ${ChargingState}, ${RotationSpeed})`);
317
-
318
- // Active
319
- this.Fan.Active = powerState === 'ON' ? this.hap.Characteristic.Active.ACTIVE : this.hap.Characteristic.Active.INACTIVE;
320
-
321
- // SwingMode
322
- this.Fan.SwingMode = oscillation === 'on' ?
323
- this.hap.Characteristic.SwingMode.SWING_ENABLED : this.hap.Characteristic.SwingMode.SWING_DISABLED;
324
-
325
- // RotationSpeed
326
- this.Fan.RotationSpeed = fanSpeed;
327
-
328
- // ChargingState
329
- this.Battery.ChargingState = chargingStatus === 'charging' ?
330
- this.hap.Characteristic.ChargingState.CHARGING : this.hap.Characteristic.ChargingState.NOT_CHARGING;
331
-
332
- // BatteryLevel
333
- this.Battery.BatteryLevel = battery;
334
-
335
- // Firmware Version
336
- if (version) {
337
- accessory.context.version = version;
338
- accessory
339
- .getService(this.hap.Service.AccessoryInformation)!
340
- .setCharacteristic(this.hap.Characteristic.FirmwareRevision, accessory.context.version)
341
- .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
342
- .updateValue(accessory.context.version);
343
- }
344
- this.updateHomeKitCharacteristics();
344
+ await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
345
+ await this.parseStatusWebhook(context);
346
+ await this.updateHomeKitCharacteristics();
345
347
  } catch (e: any) {
346
- this.errorLog(`${device.deviceType}: ${accessory.displayName} failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
348
+ await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
347
349
  }
348
350
  };
349
351
  } else {
350
- this.debugLog(`${device.deviceType}: ${accessory.displayName} is not listening webhook.`);
352
+ await this.debugLog('is not listening webhook.');
351
353
  }
352
354
  }
353
355
 
@@ -363,15 +365,25 @@ export class Fan extends deviceBase {
363
365
 
364
366
  async pushChanges(): Promise<void> {
365
367
  if (!this.device.enableCloudService && this.OpenAPI) {
366
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} pushChanges enableCloudService: ${this.device.enableCloudService}`);
368
+ await this.errorLog(`pushChanges enableCloudService: ${this.device.enableCloudService}`);
367
369
  } else if (this.BLE) {
368
370
  await this.BLEpushChanges();
369
371
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
370
372
  await this.openAPIpushChanges();
373
+ if (this.Fan.Active) {
374
+ await this.debugLog(`Active: ${this.Fan.Active}`);
375
+ // Push RotationSpeed Update
376
+ await this.debugLog(`RotationSpeed: ${this.Fan.RotationSpeed}`);
377
+ await this.pushRotationSpeedChanges();
378
+ // Push SwingMode Update
379
+ await this.debugLog(`SwingMode: ${this.Fan.SwingMode}`);
380
+ await this.pushSwingModeChanges();
381
+ } else {
382
+ await this.debugLog('BLE (RotationSpeed) & (SwingMode) changes will not happen, as the device is Off.');
383
+ }
371
384
  } else {
372
385
  await this.offlineOff();
373
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
374
- + ` ${this.device.connectionType}, pushChanges will not happen.`);
386
+ await this.debugWarnLog(`Connection Type: ${this.device.connectionType}, pushChanges will not happen.`);
375
387
  }
376
388
  // Refresh the status from the API
377
389
  interval(15000)
@@ -383,156 +395,133 @@ export class Fan extends deviceBase {
383
395
  }
384
396
 
385
397
  async BLEpushChanges(): Promise<void> {
386
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`);
398
+ await this.debugLog('BLEpushChanges');
387
399
  if (this.Fan.Active !== this.accessory.context.Active) {
388
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`
389
- + ` On: ${this.Fan.Active} OnCached: ${this.accessory.context.Active}`);
390
- const switchbot = await this.platform.connectBLE();
391
- // Convert to BLE Address
400
+ await this.debugLog(`BLEpushChanges On: ${this.Fan.Active} OnCached: ${this.accessory.context.Active}`);
401
+ const switchbot = await this.platform.connectBLE(this.accessory, this.device);
392
402
  await this.convertBLEAddress();
393
- switchbot
394
- .discover({ model: this.device.bleModel, id: this.device.bleMac })
395
- .then(async (device_list: any) => {
396
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.Fan.Active}`);
397
- return await this.retryBLE({
398
- max: await this.maxRetryBLE(),
399
- fn: async () => {
400
- if (this.Fan.Active) {
401
- return await device_list[0].turnOn({ id: this.device.bleMac });
402
- } else {
403
- return await device_list[0].turnOff({ id: this.device.bleMac });
404
- }
405
- },
403
+ if (switchbot !== false) {
404
+ switchbot
405
+ .discover({ model: this.device.bleModel, id: this.device.bleMac })
406
+ .then(async (device_list: any) => {
407
+ return await this.retryBLE({
408
+ max: await this.maxRetryBLE(),
409
+ fn: async () => {
410
+ if (this.Fan.Active) {
411
+ return await device_list[0].turnOn({ id: this.device.bleMac });
412
+ } else {
413
+ return await device_list[0].turnOff({ id: this.device.bleMac });
414
+ }
415
+ },
416
+ });
417
+ })
418
+ .then(async () => {
419
+ await this.successLog(`Active: ${this.Fan.Active} sent over SwitchBot BLE, sent successfully`);
420
+ await this.updateHomeKitCharacteristics();
421
+ })
422
+ .catch(async (e: any) => {
423
+ await this.apiError(e);
424
+ await this.errorLog(`failed BLEpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
425
+ await this.BLEPushConnection();
406
426
  });
407
- })
408
- .then(() => {
409
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
410
- this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
411
- + `Active: ${this.Fan.Active} sent over BLE, sent successfully`);
412
- this.Fan.Active = false;
413
- })
414
- .catch(async (e: any) => {
415
- this.apiError(e);
416
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
417
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
418
- await this.BLEPushConnection();
419
- });
427
+ } else {
428
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
429
+ await this.BLEPushConnection();
430
+ }
420
431
  } else {
421
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges,`
422
- + ` Active: ${this.Fan.Active}, ActiveCached: ${this.accessory.context.Active}`);
432
+ await this.debugLog(`No change (BLEpushChanges), Active: ${this.Fan.Active}, ActiveCached: ${this.accessory.context.Active}`);
423
433
  }
424
434
  }
425
435
 
426
436
  async openAPIpushChanges() {
427
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
437
+ await this.debugLog('openAPIpushChanges');
428
438
  if (this.Fan.Active !== this.accessory.context.Active) {
429
- let command = '';
430
- if (this.Fan.Active) {
431
- command = 'turnOn';
432
- } else {
433
- command = 'turnOff';
434
- }
439
+ const command = this.Fan.Active ? 'turnOn' : 'turnOff';
435
440
  const bodyChange = JSON.stringify({
436
441
  command: `${command}`,
437
442
  parameter: 'default',
438
443
  commandType: 'command',
439
444
  });
440
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
445
+ await this.debugLog(`SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`);
441
446
  try {
442
447
  const { body, statusCode } = await this.pushChangeRequest(bodyChange);
443
448
  const deviceStatus: any = await body.json();
444
- await this.pushStatusCodes(statusCode, deviceStatus);
445
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
446
- await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
449
+ await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
450
+ if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
451
+ await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
447
452
  await this.updateHomeKitCharacteristics();
448
453
  } else {
449
454
  await this.statusCode(statusCode);
450
455
  await this.statusCode(deviceStatus.statusCode);
451
456
  }
452
457
  } catch (e: any) {
453
- this.apiError(e);
454
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
455
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
458
+ await this.apiError(e);
459
+ await this.errorLog(`failed openAPIpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
456
460
  }
457
461
  } else {
458
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.`
459
- + `On: ${this.Fan.Active}, ActiveCached: ${this.accessory.context.Active}`);
460
- }
461
- // Push RotationSpeed Update
462
- if (this.Fan.Active) {
463
- await this.pushRotationSpeedChanges();
464
- }
465
- // Push SwingMode Update
466
- if (this.Fan.Active) {
467
- await this.pushSwingModeChanges();
462
+ await this.debugLog(`No changes (openAPIpushChanges), Active: ${this.Fan.Active}, ActiveCached: ${this.accessory.context.Active}`);
468
463
  }
469
464
  }
470
465
 
471
466
  async pushRotationSpeedChanges(): Promise<void> {
472
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushRotationSpeedChanges`);
467
+ await this.debugLog('pushRotationSpeedChanges');
473
468
  if (this.Fan.SwingMode !== this.accessory.context.SwingMode) {
474
469
  const bodyChange = JSON.stringify({
475
470
  command: 'setWindSpeed',
476
471
  parameter: `${this.Fan.RotationSpeed}`,
477
472
  commandType: 'command',
478
473
  });
479
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
474
+ await this.debugLog(`SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`);
480
475
  try {
481
476
  const { body, statusCode } = await this.pushChangeRequest(bodyChange);
482
477
  const deviceStatus: any = await body.json();
483
- await this.pushStatusCodes(statusCode, deviceStatus);
484
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
485
- await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
478
+ await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
479
+ if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
480
+ await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
486
481
  await this.updateHomeKitCharacteristics();
487
482
  } else {
488
483
  await this.statusCode(statusCode);
489
484
  await this.statusCode(deviceStatus.statusCode);
490
485
  }
491
486
  } catch (e: any) {
492
- this.apiError(e);
493
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushRotationSpeedChanges with ${this.device.connectionType}`
494
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
487
+ await this.apiError(e);
488
+ await this.errorLog(`failed pushRotationSpeedChanges with ${this.device.connectionType} Connection,`
489
+ + ` Error Message: ${JSON.stringify(e.message)}`);
495
490
  }
496
491
  } else {
497
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, pushRotationSpeedChanges: ${this.Fan.RotationSpeed}, `
498
- + `pushRotationSpeedChangesCached: ${this.accessory.context.RotationSpeed}`);
492
+ await this.debugLog(`No changes (pushRotationSpeedChanges), RotationSpeed: ${this.Fan.RotationSpeed},`
493
+ + ` RotationSpeedCached: ${this.accessory.context.RotationSpeed}`);
499
494
  }
500
495
  }
501
496
 
502
497
  async pushSwingModeChanges(): Promise<void> {
503
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushSwingModeChanges`);
498
+ await this.debugLog('pushSwingModeChanges');
504
499
  if (this.Fan.SwingMode !== this.accessory.context.SwingMode) {
505
- let parameter = '';
506
- if (this.Fan.SwingMode === this.hap.Characteristic.SwingMode.SWING_ENABLED) {
507
- parameter = 'on';
508
- } else {
509
- parameter = 'off';
510
- }
500
+ const parameter = this.Fan.SwingMode === this.hap.Characteristic.SwingMode.SWING_ENABLED ? 'on' : 'off';
511
501
  const bodyChange = JSON.stringify({
512
502
  command: 'setOscillation',
513
503
  parameter: `${parameter}`,
514
504
  commandType: 'command',
515
505
  });
516
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
506
+ await this.debugLog(`SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`);
517
507
  try {
518
508
  const { body, statusCode } = await this.pushChangeRequest(bodyChange);
519
509
  const deviceStatus: any = await body.json();
520
- await this.pushStatusCodes(statusCode, deviceStatus);
521
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
522
- await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
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)}`);
523
513
  await this.updateHomeKitCharacteristics();
524
514
  } else {
525
515
  await this.statusCode(statusCode);
526
516
  await this.statusCode(deviceStatus.statusCode);
527
517
  }
528
518
  } catch (e: any) {
529
- this.apiError(e);
530
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushSwingModeChanges with ${this.device.connectionType}`
531
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
519
+ await this.apiError(e);
520
+ await this.errorLog(`failed pushSwingModeChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
532
521
  }
533
522
  } else {
534
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, SwingMode: ${this.Fan.SwingMode}, `
535
- + `SwingModeCached: ${this.accessory.context.SwingMode}`);
523
+ await this.debugLog(`No changes (pushSwingModeChanges), SwingMode: ${this.Fan.SwingMode},`
524
+ + ` SwingModeCached: ${this.accessory.context.SwingMode}`);
536
525
  }
537
526
  }
538
527
 
@@ -540,10 +529,10 @@ export class Fan extends deviceBase {
540
529
  * Handle requests to set the value of the "On" characteristic
541
530
  */
542
531
  async ActiveSet(value: CharacteristicValue): Promise<void> {
543
- if (this.Fan.Active === this.accessory.context.Active) {
544
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Active: ${value}`);
532
+ if (this.Fan.Active !== this.accessory.context.Active) {
533
+ await this.infoLog(`Set Active: ${value}`);
545
534
  } else {
546
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Active: ${value}`);
535
+ await this.debugLog(`No Changes, Active: ${value}`);
547
536
  }
548
537
 
549
538
  this.Fan.Active = value;
@@ -554,10 +543,10 @@ export class Fan extends deviceBase {
554
543
  * Handle requests to set the value of the "On" characteristic
555
544
  */
556
545
  async RotationSpeedSet(value: CharacteristicValue): Promise<void> {
557
- if (this.Fan.RotationSpeed === this.accessory.context.RotationSpeed) {
558
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set RotationSpeed: ${value}`);
546
+ if (this.Fan.RotationSpeed !== this.accessory.context.RotationSpeed) {
547
+ await this.infoLog(`Set RotationSpeed ${value}`);
559
548
  } else {
560
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set RotationSpeed: ${value}`);
549
+ await this.debugLog(`No Changes, RotationSpeed: ${value}`);
561
550
  }
562
551
 
563
552
  this.Fan.RotationSpeed = value;
@@ -568,10 +557,10 @@ export class Fan extends deviceBase {
568
557
  * Handle requests to set the value of the "On" characteristic
569
558
  */
570
559
  async SwingModeSet(value: CharacteristicValue): Promise<void> {
571
- if (this.Fan.SwingMode === this.accessory.context.SwingMode) {
572
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set SwingMode: ${value}`);
560
+ if (this.Fan.SwingMode !== this.accessory.context.SwingMode) {
561
+ await this.infoLog(`Set SwingMode ${value}`);
573
562
  } else {
574
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set SwingMode: ${value}`);
563
+ await this.debugLog(`No Changes, SwingMode: ${value}`);
575
564
  }
576
565
 
577
566
  this.Fan.SwingMode = value;
@@ -582,10 +571,10 @@ export class Fan extends deviceBase {
582
571
  * Handle requests to set the value of the "On" characteristic
583
572
  */
584
573
  async OnSet(value: CharacteristicValue): Promise<void> {
585
- if (this.LightBulb.On === this.accessory.context.On) {
586
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set On: ${value}`);
574
+ if (this.LightBulb.On !== this.accessory.context.On) {
575
+ await this.infoLog(`Set On: ${value}`);
587
576
  } else {
588
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set On: ${value}`);
577
+ await this.debugLog(`No Changes, On: ${value}`);
589
578
  }
590
579
 
591
580
  this.LightBulb.On = value;
@@ -596,12 +585,14 @@ export class Fan extends deviceBase {
596
585
  * Handle requests to set the value of the "Brightness" characteristic
597
586
  */
598
587
  async BrightnessSet(value: CharacteristicValue): Promise<void> {
599
- if (this.LightBulb.Brightness === this.accessory.context.Brightness) {
600
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Brightness: ${value}`);
601
- } else if (this.LightBulb.On) {
602
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Brightness: ${value}`);
588
+ if (this.LightBulb.On && (this.LightBulb.Brightness !== this.accessory.context.Brightness)) {
589
+ await this.infoLog(`Set Brightness: ${value}`);
603
590
  } else {
604
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Brightness: ${value}`);
591
+ if (this.LightBulb.On) {
592
+ this.debugLog(`No Changes, Brightness: ${value}`);
593
+ } else {
594
+ this.debugLog(`Set Brightness: ${value}, On: ${this.LightBulb.On}`);
595
+ }
605
596
  }
606
597
 
607
598
  this.LightBulb.Brightness = value;
@@ -610,68 +601,42 @@ export class Fan extends deviceBase {
610
601
 
611
602
  async updateHomeKitCharacteristics(): Promise<void> {
612
603
  // Active
613
- if (this.Fan.Active === undefined) {
614
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Active: ${this.Fan.Active}`);
615
- } else {
616
- this.accessory.context.Active = this.Fan.Active;
617
- this.Fan.Service.updateCharacteristic(this.hap.Characteristic.Active, this.Fan.Active);
618
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Active: ${this.Fan.Active}`);
619
- }
604
+ await this.updateCharacteristic(this.Fan.Service, this.hap.Characteristic.Active,
605
+ this.Fan.Active, 'Active');
620
606
  // RotationSpeed
621
- if (this.Fan.RotationSpeed === undefined) {
622
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} RotationSpeed: ${this.Fan.RotationSpeed}`);
623
- } else {
624
- this.accessory.context.RotationSpeed = this.Fan.RotationSpeed;
625
- this.Fan.Service.updateCharacteristic(this.hap.Characteristic.RotationSpeed, this.Fan.RotationSpeed);
626
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic RotationSpeed: ${this.Fan.RotationSpeed}`);
627
- }
607
+ await this.updateCharacteristic(this.Fan.Service, this.hap.Characteristic.RotationSpeed,
608
+ this.Fan.RotationSpeed, 'RotationSpeed');
628
609
  // SwingMode
629
- if (this.Fan.SwingMode === undefined) {
630
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} SwingMode: ${this.Fan.SwingMode}`);
631
- } else {
632
- this.accessory.context.SwingMode = this.Fan.SwingMode;
633
- this.Fan.Service.updateCharacteristic(this.hap.Characteristic.SwingMode, this.Fan.SwingMode);
634
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic SwingMode: ${this.Fan.SwingMode}`);
635
- }
636
- // BateryLevel
637
- if (this.Battery.BatteryLevel === undefined) {
638
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}`);
639
- } else {
640
- this.accessory.context.BatteryLevel = this.Battery.BatteryLevel;
641
- this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.Battery.BatteryLevel);
642
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.Battery.BatteryLevel}`);
643
- }
610
+ await this.updateCharacteristic(this.Fan.Service, this.hap.Characteristic.SwingMode,
611
+ this.Fan.SwingMode, 'SwingMode');
612
+ // BatteryLevel
613
+ await this.updateCharacteristic(this.Battery.Service, this.hap.Characteristic.BatteryLevel,
614
+ this.Battery.BatteryLevel, 'BatteryLevel');
644
615
  // ChargingState
645
- if (this.Battery.ChargingState === undefined) {
646
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ChargingState: ${this.Battery.ChargingState}`);
647
- } else {
648
- this.accessory.context.ChargingState = this.Battery.ChargingState;
649
- this.Battery.Service.updateCharacteristic(this.hap.Characteristic.ChargingState, this.Battery.ChargingState);
650
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic ChargingState: ${this.Battery.ChargingState}`);
651
- }
616
+ await this.updateCharacteristic(this.Battery.Service, this.hap.Characteristic.ChargingState,
617
+ this.Battery.ChargingState, 'ChargingState');
652
618
  // StatusLowBattery
653
- if (this.Battery.StatusLowBattery === undefined) {
654
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.Battery.StatusLowBattery}`);
655
- } else {
656
- this.accessory.context.StatusLowBattery = this.Battery.StatusLowBattery;
657
- this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.Battery.StatusLowBattery);
658
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
659
- + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
660
- }
619
+ await this.updateCharacteristic(this.Battery.Service, this.hap.Characteristic.StatusLowBattery,
620
+ this.Battery.StatusLowBattery, 'StatusLowBattery');
621
+ // On
622
+ await this.updateCharacteristic(this.LightBulb.Service, this.hap.Characteristic.On,
623
+ this.LightBulb.On, 'On');
624
+ // Brightness
625
+ await this.updateCharacteristic(this.LightBulb.Service, this.hap.Characteristic.Brightness,
626
+ this.LightBulb.Brightness, 'Brightness');
661
627
  }
662
628
 
663
629
  async BLEPushConnection() {
664
630
  if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
665
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Push Changes`);
631
+ await this.warnLog('Using OpenAPI Connection to Push Changes');
666
632
  await this.openAPIpushChanges();
667
633
  }
668
634
  }
669
635
 
670
636
  async BLERefreshConnection(switchbot: any): Promise<void> {
671
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot:`
672
- + ` ${JSON.stringify(switchbot)}`);
637
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${JSON.stringify(switchbot)}`);
673
638
  if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
674
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Refresh Status`);
639
+ await this.warnLog('Using OpenAPI Connection to Refresh Status');
675
640
  await this.openAPIRefreshStatus();
676
641
  }
677
642
  }
@@ -681,6 +646,8 @@ export class Fan extends deviceBase {
681
646
  this.Fan.Service.updateCharacteristic(this.hap.Characteristic.Active, this.hap.Characteristic.Active.INACTIVE);
682
647
  this.Fan.Service.updateCharacteristic(this.hap.Characteristic.RotationSpeed, 0);
683
648
  this.Fan.Service.updateCharacteristic(this.hap.Characteristic.SwingMode, this.hap.Characteristic.SwingMode.SWING_DISABLED);
649
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.On, false);
650
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Brightness, 0);
684
651
  }
685
652
  }
686
653
 
@@ -688,5 +655,7 @@ export class Fan extends deviceBase {
688
655
  this.Fan.Service.updateCharacteristic(this.hap.Characteristic.Active, e);
689
656
  this.Fan.Service.updateCharacteristic(this.hap.Characteristic.RotationSpeed, e);
690
657
  this.Fan.Service.updateCharacteristic(this.hap.Characteristic.SwingMode, e);
658
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.On, e);
659
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Brightness, e);
691
660
  }
692
661
  }