@switchbot/homebridge-switchbot 3.5.0-beta.9 → 3.5.1-beta.0

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 (177) hide show
  1. package/.vscode/settings.json +4 -1
  2. package/CHANGELOG.md +4 -1
  3. package/config.schema.json +164 -22
  4. package/dist/device/blindtilt.d.ts +21 -96
  5. package/dist/device/blindtilt.d.ts.map +1 -1
  6. package/dist/device/blindtilt.js +457 -866
  7. package/dist/device/blindtilt.js.map +1 -1
  8. package/dist/device/bot.d.ts +27 -72
  9. package/dist/device/bot.d.ts.map +1 -1
  10. package/dist/device/bot.js +851 -913
  11. package/dist/device/bot.js.map +1 -1
  12. package/dist/device/ceilinglight.d.ts +15 -69
  13. package/dist/device/ceilinglight.d.ts.map +1 -1
  14. package/dist/device/ceilinglight.js +316 -742
  15. package/dist/device/ceilinglight.js.map +1 -1
  16. package/dist/device/colorbulb.d.ts +15 -76
  17. package/dist/device/colorbulb.d.ts.map +1 -1
  18. package/dist/device/colorbulb.js +380 -833
  19. package/dist/device/colorbulb.js.map +1 -1
  20. package/dist/device/contact.d.ts +14 -66
  21. package/dist/device/contact.d.ts.map +1 -1
  22. package/dist/device/contact.js +267 -602
  23. package/dist/device/contact.js.map +1 -1
  24. package/dist/device/curtain.d.ts +18 -89
  25. package/dist/device/curtain.d.ts.map +1 -1
  26. package/dist/device/curtain.js +469 -897
  27. package/dist/device/curtain.js.map +1 -1
  28. package/dist/device/device.d.ts +76 -0
  29. package/dist/device/device.d.ts.map +1 -0
  30. package/dist/device/device.js +846 -0
  31. package/dist/device/device.js.map +1 -0
  32. package/dist/device/fan.d.ts +63 -0
  33. package/dist/device/fan.d.ts.map +1 -0
  34. package/dist/device/fan.js +645 -0
  35. package/dist/device/fan.js.map +1 -0
  36. package/dist/device/hub.d.ts +19 -58
  37. package/dist/device/hub.d.ts.map +1 -1
  38. package/dist/device/hub.js +304 -640
  39. package/dist/device/hub.js.map +1 -1
  40. package/dist/device/humidifier.d.ts +11 -66
  41. package/dist/device/humidifier.d.ts.map +1 -1
  42. package/dist/device/humidifier.js +300 -710
  43. package/dist/device/humidifier.js.map +1 -1
  44. package/dist/device/iosensor.d.ts +12 -64
  45. package/dist/device/iosensor.d.ts.map +1 -1
  46. package/dist/device/iosensor.js +225 -643
  47. package/dist/device/iosensor.js.map +1 -1
  48. package/dist/device/lightstrip.d.ts +12 -70
  49. package/dist/device/lightstrip.d.ts.map +1 -1
  50. package/dist/device/lightstrip.js +371 -742
  51. package/dist/device/lightstrip.js.map +1 -1
  52. package/dist/device/lock.d.ts +17 -63
  53. package/dist/device/lock.d.ts.map +1 -1
  54. package/dist/device/lock.js +309 -642
  55. package/dist/device/lock.js.map +1 -1
  56. package/dist/device/meter.d.ts +15 -64
  57. package/dist/device/meter.d.ts.map +1 -1
  58. package/dist/device/meter.js +223 -638
  59. package/dist/device/meter.js.map +1 -1
  60. package/dist/device/meterplus.d.ts +15 -65
  61. package/dist/device/meterplus.d.ts.map +1 -1
  62. package/dist/device/meterplus.js +216 -644
  63. package/dist/device/meterplus.js.map +1 -1
  64. package/dist/device/motion.d.ts +12 -69
  65. package/dist/device/motion.d.ts.map +1 -1
  66. package/dist/device/motion.js +209 -588
  67. package/dist/device/motion.js.map +1 -1
  68. package/dist/device/plug.d.ts +10 -57
  69. package/dist/device/plug.d.ts.map +1 -1
  70. package/dist/device/plug.js +141 -532
  71. package/dist/device/plug.js.map +1 -1
  72. package/dist/device/robotvacuumcleaner.d.ts +13 -63
  73. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  74. package/dist/device/robotvacuumcleaner.js +252 -609
  75. package/dist/device/robotvacuumcleaner.js.map +1 -1
  76. package/dist/device/waterdetector.d.ts +11 -63
  77. package/dist/device/waterdetector.d.ts.map +1 -1
  78. package/dist/device/waterdetector.js +179 -521
  79. package/dist/device/waterdetector.js.map +1 -1
  80. package/dist/homebridge-ui/public/index.html +22 -16
  81. package/dist/homebridge-ui/server.js +1 -1
  82. package/dist/homebridge-ui/server.js.map +1 -1
  83. package/dist/index.d.ts +1 -1
  84. package/dist/index.d.ts.map +1 -1
  85. package/dist/index.js +2 -2
  86. package/dist/index.js.map +1 -1
  87. package/dist/irdevice/airconditioner.d.ts +10 -50
  88. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  89. package/dist/irdevice/airconditioner.js +337 -482
  90. package/dist/irdevice/airconditioner.js.map +1 -1
  91. package/dist/irdevice/airpurifier.d.ts +8 -39
  92. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  93. package/dist/irdevice/airpurifier.js +108 -333
  94. package/dist/irdevice/airpurifier.js.map +1 -1
  95. package/dist/irdevice/camera.d.ts +7 -36
  96. package/dist/irdevice/camera.d.ts.map +1 -1
  97. package/dist/irdevice/camera.js +47 -299
  98. package/dist/irdevice/camera.js.map +1 -1
  99. package/dist/irdevice/fan.d.ts +7 -43
  100. package/dist/irdevice/fan.d.ts.map +1 -1
  101. package/dist/irdevice/fan.js +95 -364
  102. package/dist/irdevice/fan.js.map +1 -1
  103. package/dist/irdevice/irdevice.d.ts +46 -0
  104. package/dist/irdevice/irdevice.d.ts.map +1 -0
  105. package/dist/irdevice/irdevice.js +393 -0
  106. package/dist/irdevice/irdevice.js.map +1 -0
  107. package/dist/irdevice/light.d.ts +12 -45
  108. package/dist/irdevice/light.d.ts.map +1 -1
  109. package/dist/irdevice/light.js +121 -376
  110. package/dist/irdevice/light.js.map +1 -1
  111. package/dist/irdevice/other.d.ts +19 -48
  112. package/dist/irdevice/other.d.ts.map +1 -1
  113. package/dist/irdevice/other.js +441 -584
  114. package/dist/irdevice/other.js.map +1 -1
  115. package/dist/irdevice/tv.d.ts +8 -40
  116. package/dist/irdevice/tv.d.ts.map +1 -1
  117. package/dist/irdevice/tv.js +94 -353
  118. package/dist/irdevice/tv.js.map +1 -1
  119. package/dist/irdevice/vacuumcleaner.d.ts +7 -36
  120. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  121. package/dist/irdevice/vacuumcleaner.js +48 -299
  122. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  123. package/dist/irdevice/waterheater.d.ts +7 -36
  124. package/dist/irdevice/waterheater.d.ts.map +1 -1
  125. package/dist/irdevice/waterheater.js +50 -305
  126. package/dist/irdevice/waterheater.js.map +1 -1
  127. package/dist/platform.d.ts +22 -8
  128. package/dist/platform.d.ts.map +1 -1
  129. package/dist/platform.js +342 -191
  130. package/dist/platform.js.map +1 -1
  131. package/dist/settings.d.ts +66 -18
  132. package/dist/settings.d.ts.map +1 -1
  133. package/dist/settings.js +16 -552
  134. package/dist/settings.js.map +1 -1
  135. package/dist/utils.d.ts +95 -0
  136. package/dist/utils.d.ts.map +1 -1
  137. package/dist/utils.js +657 -1
  138. package/dist/utils.js.map +1 -1
  139. package/eslint.config.js +96 -0
  140. package/package.json +20 -16
  141. package/src/custom.d.ts +2 -1
  142. package/src/device/blindtilt.ts +488 -937
  143. package/src/device/bot.ts +907 -1016
  144. package/src/device/ceilinglight.ts +330 -798
  145. package/src/device/colorbulb.ts +394 -904
  146. package/src/device/contact.ts +298 -637
  147. package/src/device/curtain.ts +496 -954
  148. package/src/device/device.ts +872 -0
  149. package/src/device/fan.ts +692 -0
  150. package/src/device/hub.ts +338 -671
  151. package/src/device/humidifier.ts +330 -777
  152. package/src/device/iosensor.ts +258 -671
  153. package/src/device/lightstrip.ts +394 -809
  154. package/src/device/lock.ts +345 -700
  155. package/src/device/meter.ts +256 -666
  156. package/src/device/meterplus.ts +248 -672
  157. package/src/device/motion.ts +234 -622
  158. package/src/device/plug.ts +152 -572
  159. package/src/device/robotvacuumcleaner.ts +273 -653
  160. package/src/device/waterdetector.ts +198 -549
  161. package/src/homebridge-ui/public/index.html +22 -16
  162. package/src/homebridge-ui/server.ts +1 -1
  163. package/src/index.ts +4 -3
  164. package/src/irdevice/airconditioner.ts +368 -540
  165. package/src/irdevice/airpurifier.ts +134 -369
  166. package/src/irdevice/camera.ts +60 -324
  167. package/src/irdevice/fan.ts +111 -392
  168. package/src/irdevice/irdevice.ts +406 -0
  169. package/src/irdevice/light.ts +159 -425
  170. package/src/irdevice/other.ts +509 -661
  171. package/src/irdevice/tv.ts +123 -388
  172. package/src/irdevice/vacuumcleaner.ts +61 -324
  173. package/src/irdevice/waterheater.ts +63 -333
  174. package/src/platform.ts +395 -259
  175. package/src/settings.ts +120 -610
  176. package/src/utils.ts +661 -1
  177. package/src/device/waterdetector.json +0 -11
@@ -0,0 +1,645 @@
1
+ /* Copyright(C) 2021-2024, donavanbecker (https://github.com/donavanbecker). All rights reserved.
2
+ *
3
+ * plug.ts: @switchbot/homebridge-switchbot.
4
+ */
5
+ import { deviceBase } from './device.js';
6
+ import { Subject, debounceTime, interval, skipWhile, take, tap } from 'rxjs';
7
+ export class Fan extends deviceBase {
8
+ platform;
9
+ // Services
10
+ Fan;
11
+ Battery;
12
+ LightBulb;
13
+ // Updates
14
+ fanUpdateInProgress;
15
+ doFanUpdate;
16
+ constructor(platform, accessory, device) {
17
+ super(platform, accessory, device);
18
+ this.platform = platform;
19
+ // this is subject we use to track when we need to POST changes to the SwitchBot API
20
+ this.doFanUpdate = new Subject();
21
+ this.fanUpdateInProgress = false;
22
+ // Initialize Fan Service
23
+ accessory.context.Fan = accessory.context.Fan ?? {};
24
+ this.Fan = {
25
+ Name: accessory.context.Fan.Name ?? accessory.displayName,
26
+ Service: accessory.getService(this.hap.Service.Fanv2) ?? accessory.addService(this.hap.Service.Fanv2),
27
+ Active: accessory.context.Active ?? this.hap.Characteristic.Active.INACTIVE,
28
+ SwingMode: accessory.context.SwingMode ?? this.hap.Characteristic.SwingMode.SWING_DISABLED,
29
+ RotationSpeed: accessory.context.RotationSpeed ?? 0,
30
+ };
31
+ accessory.context.Fan = this.Fan;
32
+ // Initialize Fan Service
33
+ this.Fan.Service
34
+ .setCharacteristic(this.hap.Characteristic.Name, this.Fan.Name)
35
+ .getCharacteristic(this.hap.Characteristic.Active)
36
+ .onGet(() => {
37
+ return this.Fan.Active;
38
+ })
39
+ .onSet(this.ActiveSet.bind(this));
40
+ // Initialize Fan RotationSpeed Characteristic
41
+ this.Fan.Service
42
+ .getCharacteristic(this.hap.Characteristic.RotationSpeed)
43
+ .onGet(() => {
44
+ return this.Fan.RotationSpeed;
45
+ })
46
+ .onSet(this.RotationSpeedSet.bind(this));
47
+ // Initialize Fan SwingMode Characteristic
48
+ this.Fan.Service.getCharacteristic(this.hap.Characteristic.SwingMode)
49
+ .onGet(() => {
50
+ return this.Fan.SwingMode;
51
+ })
52
+ .onSet(this.SwingModeSet.bind(this));
53
+ // Initialize Battery Service
54
+ accessory.context.Battery = accessory.context.Battery ?? {};
55
+ this.Battery = {
56
+ Name: accessory.context.Battery.Name ?? accessory.displayName,
57
+ Service: accessory.getService(this.hap.Service.Battery) ?? accessory.addService(this.hap.Service.Battery),
58
+ BatteryLevel: accessory.context.BatteryLevel ?? 100,
59
+ StatusLowBattery: accessory.context.StatusLowBattery ?? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL,
60
+ ChargingState: accessory.context.ChargingState ?? this.hap.Characteristic.ChargingState.NOT_CHARGING,
61
+ };
62
+ accessory.context.Battery = this.Battery;
63
+ // Initialize Battery Service
64
+ this.Battery.Service
65
+ .setCharacteristic(this.hap.Characteristic.Name, this.Battery.Name)
66
+ .getCharacteristic(this.hap.Characteristic.BatteryLevel)
67
+ .onGet(() => {
68
+ return this.Battery.BatteryLevel;
69
+ });
70
+ // Initialize Battery ChargingState Characteristic
71
+ this.Battery.Service
72
+ .getCharacteristic(this.hap.Characteristic.ChargingState)
73
+ .onGet(() => {
74
+ return this.Battery.ChargingState;
75
+ });
76
+ // Initialize Battery StatusLowBattery Characteristic
77
+ this.Battery.Service
78
+ .getCharacteristic(this.hap.Characteristic.StatusLowBattery)
79
+ .onGet(() => {
80
+ return this.Battery.StatusLowBattery;
81
+ });
82
+ // Initialize LightBulb Service
83
+ accessory.context.LightBulb = accessory.context.LightBulb ?? {};
84
+ this.LightBulb = {
85
+ Name: accessory.context.LightBulb.Name ?? accessory.displayName,
86
+ Service: accessory.getService(this.hap.Service.Lightbulb) ?? accessory.addService(this.hap.Service.Lightbulb),
87
+ On: accessory.context.On ?? false,
88
+ Brightness: accessory.context.Brightness ?? 0,
89
+ };
90
+ accessory.context.LightBulb = this.LightBulb;
91
+ // Initialize LightBulb Characteristics
92
+ this.LightBulb.Service
93
+ .setCharacteristic(this.hap.Characteristic.Name, this.LightBulb.Name)
94
+ .getCharacteristic(this.hap.Characteristic.On)
95
+ .onGet(() => {
96
+ return this.LightBulb.On;
97
+ })
98
+ .onSet(this.OnSet.bind(this));
99
+ // Initialize LightBulb Brightness Characteristic
100
+ this.LightBulb.Service
101
+ .getCharacteristic(this.hap.Characteristic.Brightness)
102
+ .onGet(() => {
103
+ return this.LightBulb.Brightness;
104
+ })
105
+ .onSet(this.BrightnessSet.bind(this));
106
+ // Retrieve initial values and updateHomekit
107
+ this.refreshStatus();
108
+ // Update Homekit
109
+ this.updateHomeKitCharacteristics();
110
+ // Start an update interval
111
+ interval(this.deviceRefreshRate * 1000)
112
+ .pipe(skipWhile(() => this.fanUpdateInProgress))
113
+ .subscribe(async () => {
114
+ await this.refreshStatus();
115
+ });
116
+ //regisiter webhook event handler
117
+ this.registerWebhook(accessory, device);
118
+ // Watch for Plug change events
119
+ // We put in a debounce of 100ms so we don't make duplicate calls
120
+ this.doFanUpdate
121
+ .pipe(tap(() => {
122
+ this.fanUpdateInProgress = true;
123
+ }), debounceTime(this.devicePushRate * 1000))
124
+ .subscribe(async () => {
125
+ try {
126
+ await this.pushChanges();
127
+ }
128
+ 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)}`);
132
+ }
133
+ this.fanUpdateInProgress = false;
134
+ });
135
+ }
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}`);
147
+ }
148
+ async openAPIparseStatus(deviceStatus) {
149
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
150
+ // 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}`);
153
+ // SwingMode
154
+ this.Fan.SwingMode = deviceStatus.body.oscillation === 'on' ?
155
+ this.hap.Characteristic.SwingMode.SWING_ENABLED : this.hap.Characteristic.SwingMode.SWING_DISABLED;
156
+ // RotationSpeed
157
+ this.Fan.RotationSpeed = deviceStatus.body.fanSpeed;
158
+ // ChargingState
159
+ this.Battery.ChargingState = deviceStatus.body.chargingStatus === 'charging' ?
160
+ this.hap.Characteristic.ChargingState.CHARGING : this.hap.Characteristic.ChargingState.NOT_CHARGING;
161
+ // 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}`);
174
+ // 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';
179
+ this.accessory
180
+ .getService(this.hap.Service.AccessoryInformation)
181
+ .setCharacteristic(this.hap.Characteristic.HardwareRevision, deviceVersion)
182
+ .setCharacteristic(this.hap.Characteristic.FirmwareRevision, deviceVersion)
183
+ .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
184
+ .updateValue(deviceVersion);
185
+ this.accessory.context.deviceVersion = deviceVersion;
186
+ this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceVersion: ${this.accessory.context.deviceVersion}`);
187
+ }
188
+ }
189
+ /**
190
+ * Asks the SwitchBot API for the latest device information
191
+ */
192
+ async refreshStatus() {
193
+ if (!this.device.enableCloudService && this.OpenAPI) {
194
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus enableCloudService: ${this.device.enableCloudService}`);
195
+ }
196
+ else if (this.BLE) {
197
+ await this.BLERefreshStatus();
198
+ }
199
+ else if (this.OpenAPI && this.platform.config.credentials?.token) {
200
+ await this.openAPIRefreshStatus();
201
+ }
202
+ else {
203
+ await this.offlineOff();
204
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
205
+ + ` ${this.device.connectionType}, refreshStatus will not happen.`);
206
+ }
207
+ }
208
+ async BLERefreshStatus() {
209
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
210
+ const switchbot = await this.switchbotBLE();
211
+ if (switchbot === undefined) {
212
+ await this.BLERefreshConnection(switchbot);
213
+ }
214
+ else {
215
+ // Start to monitor advertisement packets
216
+ (async () => {
217
+ // Start to monitor advertisement packets
218
+ const serviceData = await this.monitorAdvertisementPackets(switchbot);
219
+ // Update HomeKit
220
+ if (serviceData.model !== '' && serviceData.modelName !== '') {
221
+ await this.BLEparseStatus(serviceData);
222
+ await this.updateHomeKitCharacteristics();
223
+ }
224
+ else {
225
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed to get serviceData, serviceData: ${serviceData}`);
226
+ await this.BLERefreshConnection(switchbot);
227
+ }
228
+ })();
229
+ }
230
+ }
231
+ async openAPIRefreshStatus() {
232
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
233
+ try {
234
+ const { body, statusCode } = await this.deviceRefreshStatus();
235
+ const deviceStatus = await body.json();
236
+ await this.refreshStatusCodes(statusCode, deviceStatus);
237
+ ;
238
+ if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
239
+ await this.successfulRefreshStatus(statusCode, deviceStatus);
240
+ await this.openAPIparseStatus(deviceStatus);
241
+ await this.updateHomeKitCharacteristics();
242
+ }
243
+ else {
244
+ await this.statusCodes(statusCode, deviceStatus);
245
+ }
246
+ }
247
+ 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)}`);
251
+ }
252
+ }
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) => {
257
+ 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();
288
+ }
289
+ catch (e) {
290
+ this.errorLog(`${device.deviceType}: ${accessory.displayName} failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
291
+ }
292
+ };
293
+ }
294
+ else {
295
+ this.debugLog(`${device.deviceType}: ${accessory.displayName} is not listening webhook.`);
296
+ }
297
+ }
298
+ /**
299
+ * Pushes the requested changes to the SwitchBot API
300
+ * commandType command parameter Description
301
+ * "command" "turnOff" "default" = set to OFF state
302
+ * "command" "turnOn" "default" = set to ON state
303
+ * "command" "setNightLightMode" "off, 1, or 2" = off, turn off nightlight, (1, bright) (2, dim)
304
+ * "command" "setWindMode" "direct, natural, sleep, or baby" = Set fan mode
305
+ * "command" "setWindSpeed" "{1-100} e.g. 10" = Set fan speed 1~100
306
+ */
307
+ async pushChanges() {
308
+ if (!this.device.enableCloudService && this.OpenAPI) {
309
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} pushChanges enableCloudService: ${this.device.enableCloudService}`);
310
+ }
311
+ else if (this.BLE) {
312
+ await this.BLEpushChanges();
313
+ }
314
+ else if (this.OpenAPI && this.platform.config.credentials?.token) {
315
+ await this.openAPIpushChanges();
316
+ }
317
+ else {
318
+ await this.offlineOff();
319
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
320
+ + ` ${this.device.connectionType}, pushChanges will not happen.`);
321
+ }
322
+ // Refresh the status from the API
323
+ interval(15000)
324
+ .pipe(skipWhile(() => this.fanUpdateInProgress))
325
+ .pipe(take(1))
326
+ .subscribe(async () => {
327
+ await this.refreshStatus();
328
+ });
329
+ }
330
+ async BLEpushChanges() {
331
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`);
332
+ 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
337
+ 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
+ },
352
+ });
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)}`);
364
+ await this.BLEPushConnection();
365
+ });
366
+ }
367
+ else {
368
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges,`
369
+ + ` Active: ${this.Fan.Active}, ActiveCached: ${this.accessory.context.Active}`);
370
+ }
371
+ }
372
+ async openAPIpushChanges() {
373
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
374
+ 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
+ }
382
+ const bodyChange = JSON.stringify({
383
+ command: `${command}`,
384
+ parameter: 'default',
385
+ commandType: 'command',
386
+ });
387
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
388
+ try {
389
+ const { body, statusCode } = await this.pushChangeRequest(bodyChange);
390
+ 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);
394
+ await this.updateHomeKitCharacteristics();
395
+ }
396
+ else {
397
+ await this.statusCode(statusCode);
398
+ await this.statusCode(deviceStatus.statusCode);
399
+ }
400
+ }
401
+ 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)}`);
405
+ }
406
+ }
407
+ 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();
418
+ }
419
+ }
420
+ async pushRotationSpeedChanges() {
421
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushRotationSpeedChanges`);
422
+ if (this.Fan.SwingMode !== this.accessory.context.SwingMode) {
423
+ const bodyChange = JSON.stringify({
424
+ command: 'setWindSpeed',
425
+ parameter: `${this.Fan.RotationSpeed}`,
426
+ commandType: 'command',
427
+ });
428
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
429
+ try {
430
+ const { body, statusCode } = await this.pushChangeRequest(bodyChange);
431
+ 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);
435
+ await this.updateHomeKitCharacteristics();
436
+ }
437
+ else {
438
+ await this.statusCode(statusCode);
439
+ await this.statusCode(deviceStatus.statusCode);
440
+ }
441
+ }
442
+ 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)}`);
446
+ }
447
+ }
448
+ else {
449
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, pushRotationSpeedChanges: ${this.Fan.RotationSpeed}, `
450
+ + `pushRotationSpeedChangesCached: ${this.accessory.context.RotationSpeed}`);
451
+ }
452
+ }
453
+ async pushSwingModeChanges() {
454
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushSwingModeChanges`);
455
+ 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
+ }
463
+ const bodyChange = JSON.stringify({
464
+ command: 'setOscillation',
465
+ parameter: `${parameter}`,
466
+ commandType: 'command',
467
+ });
468
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
469
+ try {
470
+ const { body, statusCode } = await this.pushChangeRequest(bodyChange);
471
+ 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);
475
+ await this.updateHomeKitCharacteristics();
476
+ }
477
+ else {
478
+ await this.statusCode(statusCode);
479
+ await this.statusCode(deviceStatus.statusCode);
480
+ }
481
+ }
482
+ 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)}`);
486
+ }
487
+ }
488
+ else {
489
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, SwingMode: ${this.Fan.SwingMode}, `
490
+ + `SwingModeCached: ${this.accessory.context.SwingMode}`);
491
+ }
492
+ }
493
+ /**
494
+ * Handle requests to set the value of the "On" characteristic
495
+ */
496
+ 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}`);
499
+ }
500
+ else {
501
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Active: ${value}`);
502
+ }
503
+ this.Fan.Active = value;
504
+ this.doFanUpdate.next();
505
+ }
506
+ /**
507
+ * Handle requests to set the value of the "On" characteristic
508
+ */
509
+ 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}`);
512
+ }
513
+ else {
514
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set RotationSpeed: ${value}`);
515
+ }
516
+ this.Fan.RotationSpeed = value;
517
+ this.doFanUpdate.next();
518
+ }
519
+ /**
520
+ * Handle requests to set the value of the "On" characteristic
521
+ */
522
+ 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}`);
525
+ }
526
+ else {
527
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set SwingMode: ${value}`);
528
+ }
529
+ this.Fan.SwingMode = value;
530
+ this.doFanUpdate.next();
531
+ }
532
+ /**
533
+ * Handle requests to set the value of the "On" characteristic
534
+ */
535
+ 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}`);
538
+ }
539
+ else {
540
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set On: ${value}`);
541
+ }
542
+ this.LightBulb.On = value;
543
+ this.doFanUpdate.next();
544
+ }
545
+ /**
546
+ * Handle requests to set the value of the "Brightness" characteristic
547
+ */
548
+ 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}`);
554
+ }
555
+ else {
556
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Brightness: ${value}`);
557
+ }
558
+ this.LightBulb.Brightness = value;
559
+ this.doFanUpdate.next();
560
+ }
561
+ async updateHomeKitCharacteristics() {
562
+ // 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
+ }
571
+ // 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
+ }
580
+ // 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
+ }
598
+ // 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
+ }
607
+ // 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
+ }
617
+ }
618
+ async BLEPushConnection() {
619
+ 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`);
621
+ await this.openAPIpushChanges();
622
+ }
623
+ }
624
+ 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)}`);
627
+ 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`);
629
+ await this.openAPIRefreshStatus();
630
+ }
631
+ }
632
+ async offlineOff() {
633
+ if (this.device.offline) {
634
+ this.Fan.Service.updateCharacteristic(this.hap.Characteristic.Active, this.hap.Characteristic.Active.INACTIVE);
635
+ this.Fan.Service.updateCharacteristic(this.hap.Characteristic.RotationSpeed, 0);
636
+ this.Fan.Service.updateCharacteristic(this.hap.Characteristic.SwingMode, this.hap.Characteristic.SwingMode.SWING_DISABLED);
637
+ }
638
+ }
639
+ async apiError(e) {
640
+ this.Fan.Service.updateCharacteristic(this.hap.Characteristic.Active, e);
641
+ this.Fan.Service.updateCharacteristic(this.hap.Characteristic.RotationSpeed, e);
642
+ this.Fan.Service.updateCharacteristic(this.hap.Characteristic.SwingMode, e);
643
+ }
644
+ }
645
+ //# sourceMappingURL=fan.js.map