@switchbot/homebridge-switchbot 3.5.1-beta.2 → 3.5.1-beta.3

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