@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
@@ -3,6 +3,7 @@
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
  export class Fan extends deviceBase {
8
9
  platform;
@@ -107,14 +108,14 @@ export class Fan extends deviceBase {
107
108
  this.refreshStatus();
108
109
  // Update Homekit
109
110
  this.updateHomeKitCharacteristics();
111
+ //regisiter webhook event handler
112
+ this.registerWebhook();
110
113
  // Start an update interval
111
114
  interval(this.deviceRefreshRate * 1000)
112
115
  .pipe(skipWhile(() => this.fanUpdateInProgress))
113
116
  .subscribe(async () => {
114
117
  await this.refreshStatus();
115
118
  });
116
- //regisiter webhook event handler
117
- this.registerWebhook(accessory, device);
118
119
  // Watch for Plug change events
119
120
  // We put in a debounce of 100ms so we don't make duplicate calls
120
121
  this.doFanUpdate
@@ -126,56 +127,85 @@ export class Fan extends deviceBase {
126
127
  await this.pushChanges();
127
128
  }
128
129
  catch (e) {
129
- this.apiError(e);
130
- this.errorLog(`${device.deviceType}: ${accessory.displayName} failed pushChanges with ${device.connectionType} Connection,`
131
- + ` Error Message: ${JSON.stringify(e.message)}`);
130
+ await this.apiError(e);
131
+ await this.errorLog(`failed pushChanges with ${device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
132
132
  }
133
133
  this.fanUpdateInProgress = false;
134
134
  });
135
135
  }
136
136
  async BLEparseStatus(serviceData) {
137
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
138
- // State
139
- switch (serviceData.state) {
140
- case 'on':
141
- this.Fan.Active = true;
142
- break;
143
- default:
144
- this.Fan.Active = false;
145
- }
146
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.Fan.Active}`);
137
+ await this.debugLog('BLEparseStatus');
138
+ // Active
139
+ this.Fan.Active = serviceData.state === 'on' ? this.hap.Characteristic.Active.ACTIVE : this.hap.Characteristic.Active.INACTIVE;
140
+ await this.debugLog(`Active: ${this.Fan.Active}`);
147
141
  }
148
142
  async openAPIparseStatus(deviceStatus) {
149
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
143
+ await this.debugLog('openAPIparseStatus');
150
144
  // Active
151
- this.Fan.Active = deviceStatus.body.power === 'on' ? this.hap.Characteristic.Active.ACTIVE : this.hap.Characteristic.Active.INACTIVE;
152
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Active: ${this.Fan.Active}`);
145
+ this.Fan.Active = deviceStatus.power === 'on' ? this.hap.Characteristic.Active.ACTIVE : this.hap.Characteristic.Active.INACTIVE;
146
+ this.debugLog(`Active: ${this.Fan.Active}`);
153
147
  // SwingMode
154
- this.Fan.SwingMode = deviceStatus.body.oscillation === 'on' ?
148
+ this.Fan.SwingMode = deviceStatus.oscillation === 'on' ?
155
149
  this.hap.Characteristic.SwingMode.SWING_ENABLED : this.hap.Characteristic.SwingMode.SWING_DISABLED;
150
+ await this.debugLog(`SwingMode: ${this.Fan.SwingMode}`);
156
151
  // RotationSpeed
157
- this.Fan.RotationSpeed = deviceStatus.body.fanSpeed;
152
+ this.Fan.RotationSpeed = deviceStatus.fanSpeed;
153
+ await this.debugLog(`RotationSpeed: ${this.Fan.RotationSpeed}`);
158
154
  // ChargingState
159
- this.Battery.ChargingState = deviceStatus.body.chargingStatus === 'charging' ?
155
+ this.Battery.ChargingState = deviceStatus.chargingStatus === 'charging' ?
160
156
  this.hap.Characteristic.ChargingState.CHARGING : this.hap.Characteristic.ChargingState.NOT_CHARGING;
161
157
  // BatteryLevel
162
- this.Battery.BatteryLevel = Number(deviceStatus.body.battery);
163
- if (this.Battery.BatteryLevel < 10) {
164
- this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
165
- }
166
- else {
167
- this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
168
- }
169
- if (Number.isNaN(this.Battery.BatteryLevel)) {
170
- this.Battery.BatteryLevel = 100;
171
- }
172
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel},`
173
- + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
158
+ this.Battery.BatteryLevel = Number(deviceStatus.battery);
159
+ await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
160
+ // StatusLowBattery
161
+ this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
162
+ ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
163
+ await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
174
164
  // Firmware Version
175
- const version = deviceStatus.body.version?.toString();
176
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Firmware Version: ${version?.replace(/^V|-.*$/g, '')}`);
177
- if (deviceStatus.body.version) {
178
- const deviceVersion = version?.replace(/^V|-.*$/g, '') ?? '0.0.0';
165
+ const version = deviceStatus.version.toString();
166
+ await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
167
+ if (deviceStatus.version) {
168
+ const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
169
+ this.accessory
170
+ .getService(this.hap.Service.AccessoryInformation)
171
+ .setCharacteristic(this.hap.Characteristic.HardwareRevision, deviceVersion)
172
+ .setCharacteristic(this.hap.Characteristic.FirmwareRevision, deviceVersion)
173
+ .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
174
+ .updateValue(deviceVersion);
175
+ this.accessory.context.deviceVersion = deviceVersion;
176
+ await this.debugSuccessLog(`deviceVersion: ${this.accessory.context.deviceVersion}`);
177
+ }
178
+ }
179
+ async parseStatusWebhook(context) {
180
+ await this.debugLog('parseStatusWebhook');
181
+ await this.debugLog(`(version, battery, powerState, oscillation, chargingStatus, fanSpeed) = Webhook:(${context.version}, ${context.battery},`
182
+ + ` ${context.powerState}, ${context.oscillation}, ${context.chargingStatus}, ${context.fanSpeed}),`
183
+ + ` current:(${this.accessory.context.deviceVersion}, ${this.Battery.BatteryLevel}, ${this.Fan.Active}, ${this.Fan.SwingMode},`
184
+ + ` ${this.Battery.ChargingState}, ${this.Fan.RotationSpeed})`);
185
+ // Active
186
+ this.Fan.Active = context.powerState === 'ON' ? this.hap.Characteristic.Active.ACTIVE : this.hap.Characteristic.Active.INACTIVE;
187
+ await this.debugLog(`Active: ${this.Fan.Active}`);
188
+ // SwingMode
189
+ this.Fan.SwingMode = context.oscillation === 'on' ?
190
+ this.hap.Characteristic.SwingMode.SWING_ENABLED : this.hap.Characteristic.SwingMode.SWING_DISABLED;
191
+ await this.debugLog(`SwingMode: ${this.Fan.SwingMode}`);
192
+ // RotationSpeed
193
+ this.Fan.RotationSpeed = context.fanSpeed;
194
+ await this.debugLog(`RotationSpeed: ${this.Fan.RotationSpeed}`);
195
+ // BatteryLevel
196
+ this.Battery.BatteryLevel = context.battery;
197
+ await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
198
+ // StatusLowBattery
199
+ this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
200
+ ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
201
+ await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
202
+ // ChargingState
203
+ this.Battery.ChargingState = context.chargingStatus === 'charging' ?
204
+ this.hap.Characteristic.ChargingState.CHARGING : this.hap.Characteristic.ChargingState.NOT_CHARGING;
205
+ await this.debugLog(`ChargingState: ${this.Battery.ChargingState}`);
206
+ // FirmwareVersion
207
+ if (context.version) {
208
+ const deviceVersion = context.version.replace(/^V|-.*$/g, '') ?? '0.0.0';
179
209
  this.accessory
180
210
  .getService(this.hap.Service.AccessoryInformation)
181
211
  .setCharacteristic(this.hap.Characteristic.HardwareRevision, deviceVersion)
@@ -183,7 +213,7 @@ export class Fan extends deviceBase {
183
213
  .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
184
214
  .updateValue(deviceVersion);
185
215
  this.accessory.context.deviceVersion = deviceVersion;
186
- this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceVersion: ${this.accessory.context.deviceVersion}`);
216
+ await this.debugSuccessLog(`deviceVersion: ${this.accessory.context.deviceVersion}`);
187
217
  }
188
218
  }
189
219
  /**
@@ -191,7 +221,7 @@ export class Fan extends deviceBase {
191
221
  */
192
222
  async refreshStatus() {
193
223
  if (!this.device.enableCloudService && this.OpenAPI) {
194
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus enableCloudService: ${this.device.enableCloudService}`);
224
+ await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
195
225
  }
196
226
  else if (this.BLE) {
197
227
  await this.BLERefreshStatus();
@@ -201,12 +231,11 @@ export class Fan extends deviceBase {
201
231
  }
202
232
  else {
203
233
  await this.offlineOff();
204
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
205
- + ` ${this.device.connectionType}, refreshStatus will not happen.`);
234
+ await this.debugWarnLog(`Connection Type: ${this.device.connectionType}, refreshStatus will not happen.`);
206
235
  }
207
236
  }
208
237
  async BLERefreshStatus() {
209
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
238
+ await this.debugLog('BLERefreshStatus');
210
239
  const switchbot = await this.switchbotBLE();
211
240
  if (switchbot === undefined) {
212
241
  await this.BLERefreshConnection(switchbot);
@@ -217,82 +246,55 @@ export class Fan extends deviceBase {
217
246
  // Start to monitor advertisement packets
218
247
  const serviceData = await this.monitorAdvertisementPackets(switchbot);
219
248
  // Update HomeKit
220
- if (serviceData.model !== '' && serviceData.modelName !== '') {
249
+ if (serviceData.model === SwitchBotBLEModel.Unknown && SwitchBotBLEModelName.Unknown) {
221
250
  await this.BLEparseStatus(serviceData);
222
251
  await this.updateHomeKitCharacteristics();
223
252
  }
224
253
  else {
225
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed to get serviceData, serviceData: ${serviceData}`);
254
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
226
255
  await this.BLERefreshConnection(switchbot);
227
256
  }
228
257
  })();
229
258
  }
230
259
  }
231
260
  async openAPIRefreshStatus() {
232
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
261
+ await this.debugLog('openAPIRefreshStatus');
233
262
  try {
234
263
  const { body, statusCode } = await this.deviceRefreshStatus();
235
264
  const deviceStatus = await body.json();
236
- await this.refreshStatusCodes(statusCode, deviceStatus);
265
+ await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
237
266
  ;
238
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
239
- await this.successfulRefreshStatus(statusCode, deviceStatus);
240
- await this.openAPIparseStatus(deviceStatus);
267
+ if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
268
+ await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
269
+ await this.openAPIparseStatus(deviceStatus.body);
241
270
  await this.updateHomeKitCharacteristics();
242
271
  }
243
272
  else {
244
- await this.statusCodes(statusCode, deviceStatus);
273
+ await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
274
+ await this.debugWarnLog(statusCode, deviceStatus);
245
275
  }
246
276
  }
247
277
  catch (e) {
248
- this.apiError(e);
249
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
250
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
278
+ await this.apiError(e);
279
+ await this.errorLog(`failed openAPIRefreshStatus with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
251
280
  }
252
281
  }
253
- async registerWebhook(accessory, device) {
254
- if (device.webhook) {
255
- this.debugLog(`${device.deviceType}: ${accessory.displayName} is listening webhook.`);
256
- this.platform.webhookEventHandler[device.deviceId] = async (context) => {
282
+ async registerWebhook() {
283
+ if (this.device.webhook) {
284
+ await this.debugLog('is listening webhook.');
285
+ this.platform.webhookEventHandler[this.device.deviceId] = async (context) => {
257
286
  try {
258
- this.debugLog(`${device.deviceType}: ${accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
259
- const { version, battery, powerState, oscillation, chargingStatus, fanSpeed } = context;
260
- const { Active, SwingMode, RotationSpeed } = this.Fan;
261
- const { BatteryLevel, ChargingState } = this.Battery;
262
- const { FirmwareRevision } = accessory.context;
263
- this.debugLog(`${device.deviceType}: ${accessory.displayName} (version, battery, powerState, oscillation, chargingStatus, fanSpeed) = `
264
- + `Webhook:(${version}, ${battery}, ${powerState}, ${oscillation}, ${chargingStatus}, ${fanSpeed}), `
265
- + `current:(${FirmwareRevision}, ${BatteryLevel}, ${Active}, ${SwingMode}, ${ChargingState}, ${RotationSpeed})`);
266
- // Active
267
- this.Fan.Active = powerState === 'ON' ? this.hap.Characteristic.Active.ACTIVE : this.hap.Characteristic.Active.INACTIVE;
268
- // SwingMode
269
- this.Fan.SwingMode = oscillation === 'on' ?
270
- this.hap.Characteristic.SwingMode.SWING_ENABLED : this.hap.Characteristic.SwingMode.SWING_DISABLED;
271
- // RotationSpeed
272
- this.Fan.RotationSpeed = fanSpeed;
273
- // ChargingState
274
- this.Battery.ChargingState = chargingStatus === 'charging' ?
275
- this.hap.Characteristic.ChargingState.CHARGING : this.hap.Characteristic.ChargingState.NOT_CHARGING;
276
- // BatteryLevel
277
- this.Battery.BatteryLevel = battery;
278
- // Firmware Version
279
- if (version) {
280
- accessory.context.version = version;
281
- accessory
282
- .getService(this.hap.Service.AccessoryInformation)
283
- .setCharacteristic(this.hap.Characteristic.FirmwareRevision, accessory.context.version)
284
- .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
285
- .updateValue(accessory.context.version);
286
- }
287
- this.updateHomeKitCharacteristics();
287
+ await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
288
+ await this.parseStatusWebhook(context);
289
+ await this.updateHomeKitCharacteristics();
288
290
  }
289
291
  catch (e) {
290
- this.errorLog(`${device.deviceType}: ${accessory.displayName} failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
292
+ await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
291
293
  }
292
294
  };
293
295
  }
294
296
  else {
295
- this.debugLog(`${device.deviceType}: ${accessory.displayName} is not listening webhook.`);
297
+ await this.debugLog('is not listening webhook.');
296
298
  }
297
299
  }
298
300
  /**
@@ -306,18 +308,29 @@ export class Fan extends deviceBase {
306
308
  */
307
309
  async pushChanges() {
308
310
  if (!this.device.enableCloudService && this.OpenAPI) {
309
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} pushChanges enableCloudService: ${this.device.enableCloudService}`);
311
+ await this.errorLog(`pushChanges enableCloudService: ${this.device.enableCloudService}`);
310
312
  }
311
313
  else if (this.BLE) {
312
314
  await this.BLEpushChanges();
313
315
  }
314
316
  else if (this.OpenAPI && this.platform.config.credentials?.token) {
315
317
  await this.openAPIpushChanges();
318
+ if (this.Fan.Active) {
319
+ await this.debugLog(`Active: ${this.Fan.Active}`);
320
+ // Push RotationSpeed Update
321
+ await this.debugLog(`RotationSpeed: ${this.Fan.RotationSpeed}`);
322
+ await this.pushRotationSpeedChanges();
323
+ // Push SwingMode Update
324
+ await this.debugLog(`SwingMode: ${this.Fan.SwingMode}`);
325
+ await this.pushSwingModeChanges();
326
+ }
327
+ else {
328
+ await this.debugLog('BLE (RotationSpeed) & (SwingMode) changes will not happen, as the device is Off.');
329
+ }
316
330
  }
317
331
  else {
318
332
  await this.offlineOff();
319
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
320
- + ` ${this.device.connectionType}, pushChanges will not happen.`);
333
+ await this.debugWarnLog(`Connection Type: ${this.device.connectionType}, pushChanges will not happen.`);
321
334
  }
322
335
  // Refresh the status from the API
323
336
  interval(15000)
@@ -328,69 +341,62 @@ export class Fan extends deviceBase {
328
341
  });
329
342
  }
330
343
  async BLEpushChanges() {
331
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`);
344
+ await this.debugLog('BLEpushChanges');
332
345
  if (this.Fan.Active !== this.accessory.context.Active) {
333
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`
334
- + ` On: ${this.Fan.Active} OnCached: ${this.accessory.context.Active}`);
335
- const switchbot = await this.platform.connectBLE();
336
- // Convert to BLE Address
346
+ await this.debugLog(`BLEpushChanges On: ${this.Fan.Active} OnCached: ${this.accessory.context.Active}`);
347
+ const switchbot = await this.platform.connectBLE(this.accessory, this.device);
337
348
  await this.convertBLEAddress();
338
- switchbot
339
- .discover({ model: this.device.bleModel, id: this.device.bleMac })
340
- .then(async (device_list) => {
341
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.Fan.Active}`);
342
- return await this.retryBLE({
343
- max: await this.maxRetryBLE(),
344
- fn: async () => {
345
- if (this.Fan.Active) {
346
- return await device_list[0].turnOn({ id: this.device.bleMac });
347
- }
348
- else {
349
- return await device_list[0].turnOff({ id: this.device.bleMac });
350
- }
351
- },
349
+ if (switchbot !== false) {
350
+ switchbot
351
+ .discover({ model: this.device.bleModel, id: this.device.bleMac })
352
+ .then(async (device_list) => {
353
+ return await this.retryBLE({
354
+ max: await this.maxRetryBLE(),
355
+ fn: async () => {
356
+ if (this.Fan.Active) {
357
+ return await device_list[0].turnOn({ id: this.device.bleMac });
358
+ }
359
+ else {
360
+ return await device_list[0].turnOff({ id: this.device.bleMac });
361
+ }
362
+ },
363
+ });
364
+ })
365
+ .then(async () => {
366
+ await this.successLog(`Active: ${this.Fan.Active} sent over SwitchBot BLE, sent successfully`);
367
+ await this.updateHomeKitCharacteristics();
368
+ })
369
+ .catch(async (e) => {
370
+ await this.apiError(e);
371
+ await this.errorLog(`failed BLEpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
372
+ await this.BLEPushConnection();
352
373
  });
353
- })
354
- .then(() => {
355
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
356
- this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
357
- + `Active: ${this.Fan.Active} sent over BLE, sent successfully`);
358
- this.Fan.Active = false;
359
- })
360
- .catch(async (e) => {
361
- this.apiError(e);
362
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
363
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
374
+ }
375
+ else {
376
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
364
377
  await this.BLEPushConnection();
365
- });
378
+ }
366
379
  }
367
380
  else {
368
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges,`
369
- + ` Active: ${this.Fan.Active}, ActiveCached: ${this.accessory.context.Active}`);
381
+ await this.debugLog(`No change (BLEpushChanges), Active: ${this.Fan.Active}, ActiveCached: ${this.accessory.context.Active}`);
370
382
  }
371
383
  }
372
384
  async openAPIpushChanges() {
373
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
385
+ await this.debugLog('openAPIpushChanges');
374
386
  if (this.Fan.Active !== this.accessory.context.Active) {
375
- let command = '';
376
- if (this.Fan.Active) {
377
- command = 'turnOn';
378
- }
379
- else {
380
- command = 'turnOff';
381
- }
387
+ const command = this.Fan.Active ? 'turnOn' : 'turnOff';
382
388
  const bodyChange = JSON.stringify({
383
389
  command: `${command}`,
384
390
  parameter: 'default',
385
391
  commandType: 'command',
386
392
  });
387
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
393
+ await this.debugLog(`SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`);
388
394
  try {
389
395
  const { body, statusCode } = await this.pushChangeRequest(bodyChange);
390
396
  const deviceStatus = await body.json();
391
- await this.pushStatusCodes(statusCode, deviceStatus);
392
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
393
- await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
397
+ await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
398
+ if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
399
+ await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
394
400
  await this.updateHomeKitCharacteristics();
395
401
  }
396
402
  else {
@@ -399,39 +405,29 @@ export class Fan extends deviceBase {
399
405
  }
400
406
  }
401
407
  catch (e) {
402
- this.apiError(e);
403
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
404
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
408
+ await this.apiError(e);
409
+ await this.errorLog(`failed openAPIpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
405
410
  }
406
411
  }
407
412
  else {
408
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.`
409
- + `On: ${this.Fan.Active}, ActiveCached: ${this.accessory.context.Active}`);
410
- }
411
- // Push RotationSpeed Update
412
- if (this.Fan.Active) {
413
- await this.pushRotationSpeedChanges();
414
- }
415
- // Push SwingMode Update
416
- if (this.Fan.Active) {
417
- await this.pushSwingModeChanges();
413
+ await this.debugLog(`No changes (openAPIpushChanges), Active: ${this.Fan.Active}, ActiveCached: ${this.accessory.context.Active}`);
418
414
  }
419
415
  }
420
416
  async pushRotationSpeedChanges() {
421
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushRotationSpeedChanges`);
417
+ await this.debugLog('pushRotationSpeedChanges');
422
418
  if (this.Fan.SwingMode !== this.accessory.context.SwingMode) {
423
419
  const bodyChange = JSON.stringify({
424
420
  command: 'setWindSpeed',
425
421
  parameter: `${this.Fan.RotationSpeed}`,
426
422
  commandType: 'command',
427
423
  });
428
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
424
+ await this.debugLog(`SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`);
429
425
  try {
430
426
  const { body, statusCode } = await this.pushChangeRequest(bodyChange);
431
427
  const deviceStatus = await body.json();
432
- await this.pushStatusCodes(statusCode, deviceStatus);
433
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
434
- await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
428
+ await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
429
+ if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
430
+ await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
435
431
  await this.updateHomeKitCharacteristics();
436
432
  }
437
433
  else {
@@ -440,38 +436,32 @@ export class Fan extends deviceBase {
440
436
  }
441
437
  }
442
438
  catch (e) {
443
- this.apiError(e);
444
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushRotationSpeedChanges with ${this.device.connectionType}`
445
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
439
+ await this.apiError(e);
440
+ await this.errorLog(`failed pushRotationSpeedChanges with ${this.device.connectionType} Connection,`
441
+ + ` Error Message: ${JSON.stringify(e.message)}`);
446
442
  }
447
443
  }
448
444
  else {
449
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, pushRotationSpeedChanges: ${this.Fan.RotationSpeed}, `
450
- + `pushRotationSpeedChangesCached: ${this.accessory.context.RotationSpeed}`);
445
+ await this.debugLog(`No changes (pushRotationSpeedChanges), RotationSpeed: ${this.Fan.RotationSpeed},`
446
+ + ` RotationSpeedCached: ${this.accessory.context.RotationSpeed}`);
451
447
  }
452
448
  }
453
449
  async pushSwingModeChanges() {
454
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushSwingModeChanges`);
450
+ await this.debugLog('pushSwingModeChanges');
455
451
  if (this.Fan.SwingMode !== this.accessory.context.SwingMode) {
456
- let parameter = '';
457
- if (this.Fan.SwingMode === this.hap.Characteristic.SwingMode.SWING_ENABLED) {
458
- parameter = 'on';
459
- }
460
- else {
461
- parameter = 'off';
462
- }
452
+ const parameter = this.Fan.SwingMode === this.hap.Characteristic.SwingMode.SWING_ENABLED ? 'on' : 'off';
463
453
  const bodyChange = JSON.stringify({
464
454
  command: 'setOscillation',
465
455
  parameter: `${parameter}`,
466
456
  commandType: 'command',
467
457
  });
468
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
458
+ await this.debugLog(`SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`);
469
459
  try {
470
460
  const { body, statusCode } = await this.pushChangeRequest(bodyChange);
471
461
  const deviceStatus = await body.json();
472
- await this.pushStatusCodes(statusCode, deviceStatus);
473
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
474
- await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
462
+ await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
463
+ if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
464
+ await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
475
465
  await this.updateHomeKitCharacteristics();
476
466
  }
477
467
  else {
@@ -480,25 +470,24 @@ export class Fan extends deviceBase {
480
470
  }
481
471
  }
482
472
  catch (e) {
483
- this.apiError(e);
484
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushSwingModeChanges with ${this.device.connectionType}`
485
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
473
+ await this.apiError(e);
474
+ await this.errorLog(`failed pushSwingModeChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
486
475
  }
487
476
  }
488
477
  else {
489
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, SwingMode: ${this.Fan.SwingMode}, `
490
- + `SwingModeCached: ${this.accessory.context.SwingMode}`);
478
+ await this.debugLog(`No changes (pushSwingModeChanges), SwingMode: ${this.Fan.SwingMode},`
479
+ + ` SwingModeCached: ${this.accessory.context.SwingMode}`);
491
480
  }
492
481
  }
493
482
  /**
494
483
  * Handle requests to set the value of the "On" characteristic
495
484
  */
496
485
  async ActiveSet(value) {
497
- if (this.Fan.Active === this.accessory.context.Active) {
498
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Active: ${value}`);
486
+ if (this.Fan.Active !== this.accessory.context.Active) {
487
+ await this.infoLog(`Set Active: ${value}`);
499
488
  }
500
489
  else {
501
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Active: ${value}`);
490
+ await this.debugLog(`No Changes, Active: ${value}`);
502
491
  }
503
492
  this.Fan.Active = value;
504
493
  this.doFanUpdate.next();
@@ -507,11 +496,11 @@ export class Fan extends deviceBase {
507
496
  * Handle requests to set the value of the "On" characteristic
508
497
  */
509
498
  async RotationSpeedSet(value) {
510
- if (this.Fan.RotationSpeed === this.accessory.context.RotationSpeed) {
511
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set RotationSpeed: ${value}`);
499
+ if (this.Fan.RotationSpeed !== this.accessory.context.RotationSpeed) {
500
+ await this.infoLog(`Set RotationSpeed ${value}`);
512
501
  }
513
502
  else {
514
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set RotationSpeed: ${value}`);
503
+ await this.debugLog(`No Changes, RotationSpeed: ${value}`);
515
504
  }
516
505
  this.Fan.RotationSpeed = value;
517
506
  this.doFanUpdate.next();
@@ -520,11 +509,11 @@ export class Fan extends deviceBase {
520
509
  * Handle requests to set the value of the "On" characteristic
521
510
  */
522
511
  async SwingModeSet(value) {
523
- if (this.Fan.SwingMode === this.accessory.context.SwingMode) {
524
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set SwingMode: ${value}`);
512
+ if (this.Fan.SwingMode !== this.accessory.context.SwingMode) {
513
+ await this.infoLog(`Set SwingMode ${value}`);
525
514
  }
526
515
  else {
527
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set SwingMode: ${value}`);
516
+ await this.debugLog(`No Changes, SwingMode: ${value}`);
528
517
  }
529
518
  this.Fan.SwingMode = value;
530
519
  this.doFanUpdate.next();
@@ -533,11 +522,11 @@ export class Fan extends deviceBase {
533
522
  * Handle requests to set the value of the "On" characteristic
534
523
  */
535
524
  async OnSet(value) {
536
- if (this.LightBulb.On === this.accessory.context.On) {
537
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set On: ${value}`);
525
+ if (this.LightBulb.On !== this.accessory.context.On) {
526
+ await this.infoLog(`Set On: ${value}`);
538
527
  }
539
528
  else {
540
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set On: ${value}`);
529
+ await this.debugLog(`No Changes, On: ${value}`);
541
530
  }
542
531
  this.LightBulb.On = value;
543
532
  this.doFanUpdate.next();
@@ -546,86 +535,48 @@ export class Fan extends deviceBase {
546
535
  * Handle requests to set the value of the "Brightness" characteristic
547
536
  */
548
537
  async BrightnessSet(value) {
549
- if (this.LightBulb.Brightness === this.accessory.context.Brightness) {
550
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Brightness: ${value}`);
551
- }
552
- else if (this.LightBulb.On) {
553
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Brightness: ${value}`);
538
+ if (this.LightBulb.On && (this.LightBulb.Brightness !== this.accessory.context.Brightness)) {
539
+ await this.infoLog(`Set Brightness: ${value}`);
554
540
  }
555
541
  else {
556
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Brightness: ${value}`);
542
+ if (this.LightBulb.On) {
543
+ this.debugLog(`No Changes, Brightness: ${value}`);
544
+ }
545
+ else {
546
+ this.debugLog(`Set Brightness: ${value}, On: ${this.LightBulb.On}`);
547
+ }
557
548
  }
558
549
  this.LightBulb.Brightness = value;
559
550
  this.doFanUpdate.next();
560
551
  }
561
552
  async updateHomeKitCharacteristics() {
562
553
  // Active
563
- if (this.Fan.Active === undefined) {
564
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Active: ${this.Fan.Active}`);
565
- }
566
- else {
567
- this.accessory.context.Active = this.Fan.Active;
568
- this.Fan.Service.updateCharacteristic(this.hap.Characteristic.Active, this.Fan.Active);
569
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Active: ${this.Fan.Active}`);
570
- }
554
+ await this.updateCharacteristic(this.Fan.Service, this.hap.Characteristic.Active, this.Fan.Active, 'Active');
571
555
  // RotationSpeed
572
- if (this.Fan.RotationSpeed === undefined) {
573
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} RotationSpeed: ${this.Fan.RotationSpeed}`);
574
- }
575
- else {
576
- this.accessory.context.RotationSpeed = this.Fan.RotationSpeed;
577
- this.Fan.Service.updateCharacteristic(this.hap.Characteristic.RotationSpeed, this.Fan.RotationSpeed);
578
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic RotationSpeed: ${this.Fan.RotationSpeed}`);
579
- }
556
+ await this.updateCharacteristic(this.Fan.Service, this.hap.Characteristic.RotationSpeed, this.Fan.RotationSpeed, 'RotationSpeed');
580
557
  // SwingMode
581
- if (this.Fan.SwingMode === undefined) {
582
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} SwingMode: ${this.Fan.SwingMode}`);
583
- }
584
- else {
585
- this.accessory.context.SwingMode = this.Fan.SwingMode;
586
- this.Fan.Service.updateCharacteristic(this.hap.Characteristic.SwingMode, this.Fan.SwingMode);
587
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic SwingMode: ${this.Fan.SwingMode}`);
588
- }
589
- // BateryLevel
590
- if (this.Battery.BatteryLevel === undefined) {
591
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}`);
592
- }
593
- else {
594
- this.accessory.context.BatteryLevel = this.Battery.BatteryLevel;
595
- this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.Battery.BatteryLevel);
596
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.Battery.BatteryLevel}`);
597
- }
558
+ await this.updateCharacteristic(this.Fan.Service, this.hap.Characteristic.SwingMode, this.Fan.SwingMode, 'SwingMode');
559
+ // BatteryLevel
560
+ await this.updateCharacteristic(this.Battery.Service, this.hap.Characteristic.BatteryLevel, this.Battery.BatteryLevel, 'BatteryLevel');
598
561
  // ChargingState
599
- if (this.Battery.ChargingState === undefined) {
600
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ChargingState: ${this.Battery.ChargingState}`);
601
- }
602
- else {
603
- this.accessory.context.ChargingState = this.Battery.ChargingState;
604
- this.Battery.Service.updateCharacteristic(this.hap.Characteristic.ChargingState, this.Battery.ChargingState);
605
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic ChargingState: ${this.Battery.ChargingState}`);
606
- }
562
+ await this.updateCharacteristic(this.Battery.Service, this.hap.Characteristic.ChargingState, this.Battery.ChargingState, 'ChargingState');
607
563
  // StatusLowBattery
608
- if (this.Battery.StatusLowBattery === undefined) {
609
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.Battery.StatusLowBattery}`);
610
- }
611
- else {
612
- this.accessory.context.StatusLowBattery = this.Battery.StatusLowBattery;
613
- this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.Battery.StatusLowBattery);
614
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
615
- + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
616
- }
564
+ await this.updateCharacteristic(this.Battery.Service, this.hap.Characteristic.StatusLowBattery, this.Battery.StatusLowBattery, 'StatusLowBattery');
565
+ // On
566
+ await this.updateCharacteristic(this.LightBulb.Service, this.hap.Characteristic.On, this.LightBulb.On, 'On');
567
+ // Brightness
568
+ await this.updateCharacteristic(this.LightBulb.Service, this.hap.Characteristic.Brightness, this.LightBulb.Brightness, 'Brightness');
617
569
  }
618
570
  async BLEPushConnection() {
619
571
  if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
620
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Push Changes`);
572
+ await this.warnLog('Using OpenAPI Connection to Push Changes');
621
573
  await this.openAPIpushChanges();
622
574
  }
623
575
  }
624
576
  async BLERefreshConnection(switchbot) {
625
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot:`
626
- + ` ${JSON.stringify(switchbot)}`);
577
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${JSON.stringify(switchbot)}`);
627
578
  if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
628
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Refresh Status`);
579
+ await this.warnLog('Using OpenAPI Connection to Refresh Status');
629
580
  await this.openAPIRefreshStatus();
630
581
  }
631
582
  }
@@ -634,12 +585,16 @@ export class Fan extends deviceBase {
634
585
  this.Fan.Service.updateCharacteristic(this.hap.Characteristic.Active, this.hap.Characteristic.Active.INACTIVE);
635
586
  this.Fan.Service.updateCharacteristic(this.hap.Characteristic.RotationSpeed, 0);
636
587
  this.Fan.Service.updateCharacteristic(this.hap.Characteristic.SwingMode, this.hap.Characteristic.SwingMode.SWING_DISABLED);
588
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.On, false);
589
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Brightness, 0);
637
590
  }
638
591
  }
639
592
  async apiError(e) {
640
593
  this.Fan.Service.updateCharacteristic(this.hap.Characteristic.Active, e);
641
594
  this.Fan.Service.updateCharacteristic(this.hap.Characteristic.RotationSpeed, e);
642
595
  this.Fan.Service.updateCharacteristic(this.hap.Characteristic.SwingMode, e);
596
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.On, e);
597
+ this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Brightness, e);
643
598
  }
644
599
  }
645
600
  //# sourceMappingURL=fan.js.map