@switchbot/homebridge-switchbot 3.5.0-beta.4 → 3.5.0-beta.41

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 (168) hide show
  1. package/CHANGELOG.md +4 -1
  2. package/config.schema.json +120 -17
  3. package/dist/device/blindtilt.d.ts +21 -94
  4. package/dist/device/blindtilt.d.ts.map +1 -1
  5. package/dist/device/blindtilt.js +351 -757
  6. package/dist/device/blindtilt.js.map +1 -1
  7. package/dist/device/bot.d.ts +25 -71
  8. package/dist/device/bot.d.ts.map +1 -1
  9. package/dist/device/bot.js +587 -761
  10. package/dist/device/bot.js.map +1 -1
  11. package/dist/device/ceilinglight.d.ts +13 -67
  12. package/dist/device/ceilinglight.d.ts.map +1 -1
  13. package/dist/device/ceilinglight.js +213 -588
  14. package/dist/device/ceilinglight.js.map +1 -1
  15. package/dist/device/colorbulb.d.ts +13 -74
  16. package/dist/device/colorbulb.d.ts.map +1 -1
  17. package/dist/device/colorbulb.js +255 -634
  18. package/dist/device/colorbulb.js.map +1 -1
  19. package/dist/device/contact.d.ts +14 -65
  20. package/dist/device/contact.d.ts.map +1 -1
  21. package/dist/device/contact.js +208 -532
  22. package/dist/device/contact.js.map +1 -1
  23. package/dist/device/curtain.d.ts +17 -86
  24. package/dist/device/curtain.d.ts.map +1 -1
  25. package/dist/device/curtain.js +339 -787
  26. package/dist/device/curtain.js.map +1 -1
  27. package/dist/device/device.d.ts +59 -0
  28. package/dist/device/device.d.ts.map +1 -0
  29. package/dist/device/device.js +671 -0
  30. package/dist/device/device.js.map +1 -0
  31. package/dist/device/fan.d.ts +51 -0
  32. package/dist/device/fan.d.ts.map +1 -0
  33. package/dist/device/fan.js +504 -0
  34. package/dist/device/fan.js.map +1 -0
  35. package/dist/device/hub.d.ts +15 -59
  36. package/dist/device/hub.d.ts.map +1 -1
  37. package/dist/device/hub.js +246 -597
  38. package/dist/device/hub.js.map +1 -1
  39. package/dist/device/humidifier.d.ts +10 -65
  40. package/dist/device/humidifier.d.ts.map +1 -1
  41. package/dist/device/humidifier.js +211 -572
  42. package/dist/device/humidifier.js.map +1 -1
  43. package/dist/device/iosensor.d.ts +11 -64
  44. package/dist/device/iosensor.d.ts.map +1 -1
  45. package/dist/device/iosensor.js +178 -585
  46. package/dist/device/iosensor.js.map +1 -1
  47. package/dist/device/lightstrip.d.ts +10 -68
  48. package/dist/device/lightstrip.d.ts.map +1 -1
  49. package/dist/device/lightstrip.js +256 -569
  50. package/dist/device/lightstrip.js.map +1 -1
  51. package/dist/device/lock.d.ts +15 -62
  52. package/dist/device/lock.d.ts.map +1 -1
  53. package/dist/device/lock.js +214 -543
  54. package/dist/device/lock.js.map +1 -1
  55. package/dist/device/meter.d.ts +13 -63
  56. package/dist/device/meter.d.ts.map +1 -1
  57. package/dist/device/meter.js +174 -578
  58. package/dist/device/meter.js.map +1 -1
  59. package/dist/device/meterplus.d.ts +13 -64
  60. package/dist/device/meterplus.d.ts.map +1 -1
  61. package/dist/device/meterplus.js +163 -578
  62. package/dist/device/meterplus.js.map +1 -1
  63. package/dist/device/motion.d.ts +13 -69
  64. package/dist/device/motion.d.ts.map +1 -1
  65. package/dist/device/motion.js +141 -520
  66. package/dist/device/motion.js.map +1 -1
  67. package/dist/device/plug.d.ts +9 -57
  68. package/dist/device/plug.d.ts.map +1 -1
  69. package/dist/device/plug.js +86 -453
  70. package/dist/device/plug.js.map +1 -1
  71. package/dist/device/robotvacuumcleaner.d.ts +11 -62
  72. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  73. package/dist/device/robotvacuumcleaner.js +144 -452
  74. package/dist/device/robotvacuumcleaner.js.map +1 -1
  75. package/dist/device/waterdetector.d.ts +9 -70
  76. package/dist/device/waterdetector.d.ts.map +1 -1
  77. package/dist/device/waterdetector.js +125 -665
  78. package/dist/device/waterdetector.js.map +1 -1
  79. package/dist/homebridge-ui/public/index.html +2 -2
  80. package/dist/index.js +1 -1
  81. package/dist/irdevice/airconditioner.d.ts +8 -48
  82. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  83. package/dist/irdevice/airconditioner.js +309 -419
  84. package/dist/irdevice/airconditioner.js.map +1 -1
  85. package/dist/irdevice/airpurifier.d.ts +7 -38
  86. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  87. package/dist/irdevice/airpurifier.js +72 -304
  88. package/dist/irdevice/airpurifier.js.map +1 -1
  89. package/dist/irdevice/camera.d.ts +6 -35
  90. package/dist/irdevice/camera.d.ts.map +1 -1
  91. package/dist/irdevice/camera.js +34 -278
  92. package/dist/irdevice/camera.js.map +1 -1
  93. package/dist/irdevice/fan.d.ts +6 -39
  94. package/dist/irdevice/fan.d.ts.map +1 -1
  95. package/dist/irdevice/fan.js +67 -311
  96. package/dist/irdevice/fan.js.map +1 -1
  97. package/dist/irdevice/irdevice.d.ts +40 -0
  98. package/dist/irdevice/irdevice.d.ts.map +1 -0
  99. package/dist/irdevice/irdevice.js +512 -0
  100. package/dist/irdevice/irdevice.js.map +1 -0
  101. package/dist/irdevice/light.d.ts +6 -34
  102. package/dist/irdevice/light.d.ts.map +1 -1
  103. package/dist/irdevice/light.js +23 -272
  104. package/dist/irdevice/light.js.map +1 -1
  105. package/dist/irdevice/other.d.ts +18 -47
  106. package/dist/irdevice/other.d.ts.map +1 -1
  107. package/dist/irdevice/other.js +352 -546
  108. package/dist/irdevice/other.js.map +1 -1
  109. package/dist/irdevice/tv.d.ts +7 -39
  110. package/dist/irdevice/tv.d.ts.map +1 -1
  111. package/dist/irdevice/tv.js +82 -321
  112. package/dist/irdevice/tv.js.map +1 -1
  113. package/dist/irdevice/vacuumcleaner.d.ts +6 -35
  114. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  115. package/dist/irdevice/vacuumcleaner.js +41 -284
  116. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  117. package/dist/irdevice/waterheater.d.ts +6 -35
  118. package/dist/irdevice/waterheater.d.ts.map +1 -1
  119. package/dist/irdevice/waterheater.js +42 -287
  120. package/dist/irdevice/waterheater.js.map +1 -1
  121. package/dist/platform.d.ts +14 -1
  122. package/dist/platform.d.ts.map +1 -1
  123. package/dist/platform.js +343 -198
  124. package/dist/platform.js.map +1 -1
  125. package/dist/settings.d.ts +41 -11
  126. package/dist/settings.d.ts.map +1 -1
  127. package/dist/settings.js +0 -552
  128. package/dist/settings.js.map +1 -1
  129. package/dist/utils.d.ts +77 -0
  130. package/dist/utils.d.ts.map +1 -1
  131. package/dist/utils.js +638 -1
  132. package/dist/utils.js.map +1 -1
  133. package/package.json +16 -15
  134. package/src/custom.d.ts +2 -1
  135. package/src/device/blindtilt.ts +377 -783
  136. package/src/device/bot.ts +620 -845
  137. package/src/device/ceilinglight.ts +226 -608
  138. package/src/device/colorbulb.ts +268 -657
  139. package/src/device/contact.ts +233 -554
  140. package/src/device/curtain.ts +359 -811
  141. package/src/device/device.ts +699 -0
  142. package/src/device/fan.ts +553 -0
  143. package/src/device/hub.ts +273 -625
  144. package/src/device/humidifier.ts +241 -619
  145. package/src/device/iosensor.ts +204 -611
  146. package/src/device/lightstrip.ts +277 -597
  147. package/src/device/lock.ts +251 -583
  148. package/src/device/meter.ts +199 -601
  149. package/src/device/meterplus.ts +188 -603
  150. package/src/device/motion.ts +160 -542
  151. package/src/device/plug.ts +96 -481
  152. package/src/device/robotvacuumcleaner.ts +165 -478
  153. package/src/device/waterdetector.ts +142 -689
  154. package/src/homebridge-ui/public/index.html +2 -2
  155. package/src/index.ts +1 -1
  156. package/src/irdevice/airconditioner.ts +330 -446
  157. package/src/irdevice/airpurifier.ts +89 -321
  158. package/src/irdevice/camera.ts +42 -293
  159. package/src/irdevice/fan.ts +78 -327
  160. package/src/irdevice/irdevice.ts +528 -0
  161. package/src/irdevice/light.ts +32 -287
  162. package/src/irdevice/other.ts +396 -615
  163. package/src/irdevice/tv.ts +100 -339
  164. package/src/irdevice/vacuumcleaner.ts +49 -305
  165. package/src/irdevice/waterheater.ts +50 -310
  166. package/src/platform.ts +353 -206
  167. package/src/settings.ts +57 -566
  168. package/src/utils.ts +642 -1
@@ -0,0 +1,553 @@
1
+ /* eslint-disable max-len */
2
+ /* Copyright(C) 2021-2024, donavanbecker (https://github.com/donavanbecker). All rights reserved.
3
+ *
4
+ * plug.ts: @switchbot/homebridge-switchbot.
5
+ */
6
+ import { request } from 'undici';
7
+ import { deviceBase } from './device.js';
8
+ import { SwitchBotPlatform } from '../platform.js';
9
+ import { Subject, debounceTime, interval, skipWhile, take, tap } from 'rxjs';
10
+ import { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';
11
+ import { device, devicesConfig, serviceData, deviceStatus, Devices } from '../settings.js';
12
+
13
+ export class Fan extends deviceBase {
14
+ // Services
15
+ private Fan: {
16
+ Service: Service;
17
+ Active: CharacteristicValue;
18
+ SwingMode: CharacteristicValue;
19
+ RotationSpeed: CharacteristicValue;
20
+ };
21
+
22
+ private Battery: {
23
+ Service: Service;
24
+ BatteryLevel: CharacteristicValue;
25
+ StatusLowBattery: CharacteristicValue;
26
+ ChargingState: CharacteristicValue;
27
+ };
28
+
29
+ // Updates
30
+ plugUpdateInProgress!: boolean;
31
+ doPlugUpdate!: Subject<void>;
32
+
33
+ constructor(
34
+ readonly platform: SwitchBotPlatform,
35
+ accessory: PlatformAccessory,
36
+ device: device & devicesConfig,
37
+ ) {
38
+ super(platform, accessory, device);
39
+ // this is subject we use to track when we need to POST changes to the SwitchBot API
40
+ this.doPlugUpdate = new Subject();
41
+ this.plugUpdateInProgress = false;
42
+
43
+ // Initialize Fan property
44
+ this.Fan = {
45
+ Service: accessory.getService(this.hap.Service.Fanv2)!,
46
+ Active: accessory.context.Active || this.hap.Characteristic.Active.INACTIVE,
47
+ SwingMode: accessory.context.SwingMode || this.hap.Characteristic.SwingMode.SWING_DISABLED,
48
+ RotationSpeed: accessory.context.RotationSpeed || 0,
49
+ };
50
+
51
+ // Initialize Battery property
52
+ this.Battery = {
53
+ Service: accessory.getService(this.hap.Service.Battery)!,
54
+ BatteryLevel: accessory.context.BatteryLevel || 100,
55
+ StatusLowBattery: accessory.context.StatusLowBattery || this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL,
56
+ ChargingState: accessory.context.ChargingState || this.hap.Characteristic.ChargingState.NOT_CHARGING,
57
+ };
58
+
59
+ // Retrieve initial values and updateHomekit
60
+ this.refreshStatus();
61
+
62
+ // get the Fan service if it exists, otherwise create a new Fanv2 service
63
+ // you can create multiple services for each accessory
64
+ const FanService = `${accessory.displayName} ${device.deviceType}`;
65
+ (this.Fan.Service = accessory.getService(this.hap.Service.Fanv2)
66
+ || accessory.addService(this.hap.Service.Fanv2)), FanService;
67
+
68
+ this.Fan.Service.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
69
+ // each service must implement at-minimum the "required characteristics" for the given service type
70
+ // see https://developers.homebridge.io/#/service/Fanv2
71
+
72
+ // create handlers for required characteristics
73
+ this.Fan.Service.getCharacteristic(this.hap.Characteristic.Active).onSet(this.ActiveSet.bind(this));
74
+ // create handlers for required characteristics
75
+ this.Fan.Service.getCharacteristic(this.hap.Characteristic.RotationSpeed).onSet(this.RotationSpeedSet.bind(this));
76
+ // create handlers for required characteristics
77
+ this.Fan.Service.getCharacteristic(this.hap.Characteristic.SwingMode).onSet(this.SwingModeSet.bind(this));
78
+
79
+ // Update Homekit
80
+ this.updateHomeKitCharacteristics();
81
+
82
+ // Start an update interval
83
+ interval(this.deviceRefreshRate * 1000)
84
+ .pipe(skipWhile(() => this.plugUpdateInProgress))
85
+ .subscribe(async () => {
86
+ await this.refreshStatus();
87
+ });
88
+
89
+ //regisiter webhook event handler
90
+ if (device.webhook) {
91
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} is listening webhook.`);
92
+ this.platform.webhookEventHandler[this.device.deviceId] = async (context) => {
93
+ try {
94
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
95
+ const { version, battery, powerState, oscillation, chargingStatus, fanSpeed } = context;
96
+ const { Active, SwingMode, RotationSpeed } = this.Fan;
97
+ const { BatteryLevel, ChargingState } = this.Battery;
98
+ const { FirmwareRevision } = this.accessory.context;
99
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
100
+ '(version, battery, powerState, oscillation, chargingStatus, fanSpeed) = '
101
+ + `Webhook:(${version}, ${battery}, ${powerState}, ${oscillation}, ${chargingStatus}, ${fanSpeed}), `
102
+ + `current:(${FirmwareRevision}, ${BatteryLevel}, ${Active}, ${SwingMode}, ${ChargingState}, ${RotationSpeed})`);
103
+
104
+ // Active
105
+ this.Fan.Active = powerState === 'ON' ? this.hap.Characteristic.Active.ACTIVE : this.hap.Characteristic.Active.INACTIVE;
106
+
107
+ // SwingMode
108
+ this.Fan.SwingMode = oscillation === 'on' ?
109
+ this.hap.Characteristic.SwingMode.SWING_ENABLED : this.hap.Characteristic.SwingMode.SWING_DISABLED;
110
+
111
+ // RotationSpeed
112
+ this.Fan.RotationSpeed = fanSpeed;
113
+
114
+ // ChargingState
115
+ this.Battery.ChargingState = chargingStatus === 'charging' ?
116
+ this.hap.Characteristic.ChargingState.CHARGING : this.hap.Characteristic.ChargingState.NOT_CHARGING;
117
+
118
+ // BatteryLevel
119
+ this.Battery.BatteryLevel = battery;
120
+
121
+ // Firmware Version
122
+ if (version) {
123
+ this.accessory.context.version = version;
124
+ this.accessory
125
+ .getService(this.hap.Service.AccessoryInformation)!
126
+ .setCharacteristic(this.hap.Characteristic.FirmwareRevision, this.accessory.context.version)
127
+ .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
128
+ .updateValue(this.accessory.context.version);
129
+ }
130
+ this.updateHomeKitCharacteristics();
131
+ } catch (e: any) {
132
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
133
+ + `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
134
+ }
135
+ };
136
+ }
137
+
138
+ // Watch for Plug change events
139
+ // We put in a debounce of 100ms so we don't make duplicate calls
140
+ this.doPlugUpdate
141
+ .pipe(
142
+ tap(() => {
143
+ this.plugUpdateInProgress = true;
144
+ }),
145
+ debounceTime(this.platform.config.options!.pushRate! * 1000),
146
+ )
147
+ .subscribe(async () => {
148
+ try {
149
+ await this.pushChanges();
150
+ } catch (e: any) {
151
+ this.apiError(e);
152
+ this.errorLog(
153
+ `${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with ${this.device.connectionType} Connection,` +
154
+ ` Error Message: ${JSON.stringify(e.message)}`,
155
+ );
156
+ }
157
+ this.plugUpdateInProgress = false;
158
+ });
159
+ }
160
+
161
+ async BLEparseStatus(serviceData: serviceData): Promise<void> {
162
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
163
+
164
+ // State
165
+ switch (serviceData.state) {
166
+ case 'on':
167
+ this.Fan.Active = true;
168
+ break;
169
+ default:
170
+ this.Fan.Active = false;
171
+ }
172
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.Fan.Active}`);
173
+ }
174
+
175
+ async openAPIparseStatus(deviceStatus: deviceStatus) {
176
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
177
+
178
+ // Active
179
+ this.Fan.Active = deviceStatus.body.power === 'on' ? this.hap.Characteristic.Active.ACTIVE : this.hap.Characteristic.Active.INACTIVE;
180
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Active: ${this.Fan.Active}`);
181
+
182
+ // SwingMode
183
+ this.Fan.SwingMode = deviceStatus.body.oscillation === 'on' ?
184
+ this.hap.Characteristic.SwingMode.SWING_ENABLED : this.hap.Characteristic.SwingMode.SWING_DISABLED;
185
+
186
+ // RotationSpeed
187
+ this.Fan.RotationSpeed = deviceStatus.body.fanSpeed;
188
+
189
+ // ChargingState
190
+ this.Battery.ChargingState = deviceStatus.body.chargingStatus === 'charging' ?
191
+ this.hap.Characteristic.ChargingState.CHARGING : this.hap.Characteristic.ChargingState.NOT_CHARGING;
192
+
193
+ // BatteryLevel
194
+ this.Battery.BatteryLevel = Number(deviceStatus.body.battery);
195
+ if (this.Battery.BatteryLevel < 10) {
196
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
197
+ } else {
198
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
199
+ }
200
+ if (Number.isNaN(this.Battery.BatteryLevel)) {
201
+ this.Battery.BatteryLevel = 100;
202
+ }
203
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel},`
204
+ + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
205
+
206
+ // Firmware Version
207
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Firmware Version: ${deviceStatus.body.version}`);
208
+ if (deviceStatus.body.version) {
209
+ this.accessory.context.version = deviceStatus.body.version;
210
+ this.accessory
211
+ .getService(this.hap.Service.AccessoryInformation)!
212
+ .setCharacteristic(this.hap.Characteristic.FirmwareRevision, this.accessory.context.version)
213
+ .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
214
+ .updateValue(this.accessory.context.version);
215
+ }
216
+ }
217
+
218
+
219
+
220
+ /**
221
+ * Asks the SwitchBot API for the latest device information
222
+ */
223
+ async refreshStatus(): Promise<void> {
224
+ if (!this.device.enableCloudService && this.OpenAPI) {
225
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus enableCloudService: ${this.device.enableCloudService}`);
226
+ } else if (this.BLE) {
227
+ await this.BLERefreshStatus();
228
+ } else if (this.OpenAPI && this.platform.config.credentials?.token) {
229
+ await this.openAPIRefreshStatus();
230
+ } else {
231
+ await this.offlineOff();
232
+ this.debugWarnLog(
233
+ `${this.device.deviceType}: ${this.accessory.displayName} Connection Type:` +
234
+ ` ${this.device.connectionType}, refreshStatus will not happen.`,
235
+ );
236
+ }
237
+ }
238
+
239
+ async BLERefreshStatus(): Promise<void> {
240
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
241
+ const switchbot = await this.platform.connectBLE();
242
+ // Convert to BLE Address
243
+ this.device.bleMac = this.device
244
+ .deviceId!.match(/.{1,2}/g)!
245
+ .join(':')
246
+ .toLowerCase();
247
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
248
+ this.getCustomBLEAddress(switchbot);
249
+ // Start to monitor advertisement packets
250
+ (async () => {
251
+ // Start to monitor advertisement packets
252
+ await switchbot.startScan({ model: this.device.bleModel, id: this.device.bleMac });
253
+ // Set an event handler
254
+ switchbot.onadvertisement = (ad: any) => {
255
+ if (this.device.bleMac === ad.address && ad.model === this.device.bleModel) {
256
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
257
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.model}`);
258
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
259
+ } else {
260
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
261
+ }
262
+ };
263
+ // Wait 10 seconds
264
+ await switchbot.wait(this.scanDuration * 1000);
265
+ // Stop to monitor
266
+ await switchbot.stopScan();
267
+ // Update HomeKit
268
+ await this.BLEparseStatus(switchbot.onadvertisement.serviceData);
269
+ await this.updateHomeKitCharacteristics();
270
+ })();
271
+ if (switchbot === undefined) {
272
+ await this.BLERefreshConnection(switchbot);
273
+ }
274
+ }
275
+
276
+ async openAPIRefreshStatus(): Promise<void> {
277
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
278
+ try {
279
+ const { body, statusCode } = await this.platform.retryRequest(this.deviceMaxRetries, this.deviceDelayBetweenRetries,
280
+ `${Devices}/${this.device.deviceId}/status`, { headers: this.platform.generateHeaders() });
281
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
282
+ const deviceStatus: any = await body.json();
283
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
284
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
285
+ if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
286
+ this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} `
287
+ + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
288
+ this.openAPIparseStatus(deviceStatus);
289
+ this.updateHomeKitCharacteristics();
290
+ } else {
291
+ this.statusCode(statusCode);
292
+ this.statusCode(deviceStatus.statusCode);
293
+ }
294
+ } catch (e: any) {
295
+ this.apiError(e);
296
+ this.errorLog(
297
+ `${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}` +
298
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`,
299
+ );
300
+ }
301
+ }
302
+
303
+ /**
304
+ * Pushes the requested changes to the SwitchBot API
305
+ * deviceType commandType Command command parameter Description
306
+ * Battery Circulator Fan - "command" "turnOff" "default" = set to OFF state
307
+ * Battery Circulator Fan - "command" "turnOn" "default" = set to ON state
308
+ * Battery Circulator Fan - "command" "setNightLightMode" "off, 1, or 2" = off, turn off nightlight, (1, bright) (2, dim)
309
+ * Battery Circulator Fan - "command" "setWindMode" "direct, natural, sleep, or baby" = Set fan mode. direct: direct mode. natural: natural mode. sleep: sleep mode. baby: ultra quiet mode
310
+ * Battery Circulator Fan - "command" "setWindSpeed" "{1-100} e.g. 10" = Set fan speed.1~100
311
+ */
312
+
313
+ async pushChanges(): Promise<void> {
314
+ if (!this.device.enableCloudService && this.OpenAPI) {
315
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} pushChanges enableCloudService: ${this.device.enableCloudService}`);
316
+ } else if (this.BLE) {
317
+ await this.BLEpushChanges();
318
+ } else if (this.OpenAPI && this.platform.config.credentials?.token) {
319
+ await this.openAPIpushChanges();
320
+ } else {
321
+ await this.offlineOff();
322
+ this.debugWarnLog(
323
+ `${this.device.deviceType}: ${this.accessory.displayName} Connection Type:` + ` ${this.device.connectionType}, pushChanges will not happen.`,
324
+ );
325
+ }
326
+ // Refresh the status from the API
327
+ interval(15000)
328
+ .pipe(skipWhile(() => this.plugUpdateInProgress))
329
+ .pipe(take(1))
330
+ .subscribe(async () => {
331
+ await this.refreshStatus();
332
+ });
333
+ }
334
+
335
+ async BLEpushChanges(): Promise<void> {
336
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`);
337
+ if (this.Fan.Active !== this.accessory.context.Active) {
338
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`
339
+ + ` On: ${this.Fan.Active} OnCached: ${this.accessory.context.Active}`);
340
+ const switchbot = await this.platform.connectBLE();
341
+ // Convert to BLE Address
342
+ this.device.bleMac = this.device
343
+ .deviceId!.match(/.{1,2}/g)!
344
+ .join(':')
345
+ .toLowerCase();
346
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
347
+ switchbot
348
+ .discover({
349
+ model: this.device.bleModel,
350
+ id: this.device.bleMac,
351
+ })
352
+ .then(async (device_list: any) => {
353
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.Fan.Active}`);
354
+ return await this.retryBLE({
355
+ max: await this.maxRetryBLE(),
356
+ fn: async () => {
357
+ if (this.Fan.Active) {
358
+ return await device_list[0].turnOn({ id: this.device.bleMac });
359
+ } else {
360
+ return await device_list[0].turnOff({ id: this.device.bleMac });
361
+ }
362
+ },
363
+ });
364
+ })
365
+ .then(() => {
366
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
367
+ this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
368
+ + `Active: ${this.Fan.Active} sent over BLE, sent successfully`);
369
+ this.Fan.Active = false;
370
+ })
371
+ .catch(async (e: any) => {
372
+ this.apiError(e);
373
+ this.errorLog(
374
+ `${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}` +
375
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`,
376
+ );
377
+ await this.BLEPushConnection();
378
+ });
379
+ } else {
380
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges,`
381
+ + ` Active: ${this.Fan.Active}, ActiveCached: ${this.accessory.context.Active}`);
382
+ }
383
+ }
384
+
385
+ async openAPIpushChanges() {
386
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
387
+ if (this.Fan.Active !== this.accessory.context.Active) {
388
+ let command = '';
389
+ if (this.Fan.Active) {
390
+ command = 'turnOn';
391
+ } else {
392
+ command = 'turnOff';
393
+ }
394
+ const bodyChange = JSON.stringify({
395
+ command: `${command}`,
396
+ parameter: 'default',
397
+ commandType: 'command',
398
+ });
399
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
400
+ try {
401
+ const { body, statusCode } = await request(`${Devices}/${this.device.deviceId}/commands`, {
402
+ body: bodyChange,
403
+ method: 'POST',
404
+ headers: this.platform.generateHeaders(),
405
+ });
406
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
407
+ const deviceStatus: any = await body.json();
408
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
409
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
410
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
411
+ if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
412
+ this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
413
+ + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
414
+ this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
415
+ + `request to SwitchBot API, body: ${deviceStatus} sent successfully`);
416
+ } else {
417
+ this.statusCode(statusCode);
418
+ this.statusCode(deviceStatus.statusCode);
419
+ }
420
+ } catch (e: any) {
421
+ this.apiError(e);
422
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
423
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
424
+ }
425
+ } else {
426
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.`
427
+ + `On: ${this.Fan.Active}, ActiveCached: ${this.accessory.context.Active}`);
428
+ }
429
+ }
430
+
431
+ /**
432
+ * Handle requests to set the value of the "On" characteristic
433
+ */
434
+ async ActiveSet(value: CharacteristicValue): Promise<void> {
435
+ if (this.Fan.Active === this.accessory.context.Active) {
436
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Active: ${value}`);
437
+ } else {
438
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Active: ${value}`);
439
+ }
440
+
441
+ this.Fan.Active = value;
442
+ this.doPlugUpdate.next();
443
+ }
444
+
445
+ /**
446
+ * Handle requests to set the value of the "On" characteristic
447
+ */
448
+ async RotationSpeedSet(value: CharacteristicValue): Promise<void> {
449
+ if (this.Fan.RotationSpeed === this.accessory.context.RotationSpeed) {
450
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set RotationSpeed: ${value}`);
451
+ } else {
452
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set RotationSpeed: ${value}`);
453
+ }
454
+
455
+ this.Fan.RotationSpeed = value;
456
+ this.doPlugUpdate.next();
457
+ }
458
+
459
+ /**
460
+ * Handle requests to set the value of the "On" characteristic
461
+ */
462
+ async SwingModeSet(value: CharacteristicValue): Promise<void> {
463
+ if (this.Fan.SwingMode === this.accessory.context.SwingMode) {
464
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set SwingMode: ${value}`);
465
+ } else {
466
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set SwingMode: ${value}`);
467
+ }
468
+
469
+ this.Fan.SwingMode = value;
470
+ this.doPlugUpdate.next();
471
+ }
472
+
473
+ async updateHomeKitCharacteristics(): Promise<void> {
474
+ // Active
475
+ if (this.Fan.Active === undefined) {
476
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Active: ${this.Fan.Active}`);
477
+ } else {
478
+ this.accessory.context.Active = this.Fan.Active;
479
+ this.Fan.Service.updateCharacteristic(this.hap.Characteristic.Active, this.Fan.Active);
480
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Active: ${this.Fan.Active}`);
481
+ }
482
+ // RotationSpeed
483
+ if (this.Fan.RotationSpeed === undefined) {
484
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} RotationSpeed: ${this.Fan.RotationSpeed}`);
485
+ } else {
486
+ this.accessory.context.RotationSpeed = this.Fan.RotationSpeed;
487
+ this.Fan.Service.updateCharacteristic(this.hap.Characteristic.RotationSpeed, this.Fan.RotationSpeed);
488
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic RotationSpeed: ${this.Fan.RotationSpeed}`);
489
+ }
490
+ // SwingMode
491
+ if (this.Fan.SwingMode === undefined) {
492
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} SwingMode: ${this.Fan.SwingMode}`);
493
+ } else {
494
+ this.accessory.context.SwingMode = this.Fan.SwingMode;
495
+ this.Fan.Service.updateCharacteristic(this.hap.Characteristic.SwingMode, this.Fan.SwingMode);
496
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic SwingMode: ${this.Fan.SwingMode}`);
497
+ }
498
+ // BateryLevel
499
+ if (this.Battery.BatteryLevel === undefined) {
500
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}`);
501
+ } else {
502
+ this.accessory.context.BatteryLevel = this.Battery.BatteryLevel;
503
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.Battery.BatteryLevel);
504
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.Battery.BatteryLevel}`);
505
+ }
506
+ // ChargingState
507
+ if (this.Battery.ChargingState === undefined) {
508
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ChargingState: ${this.Battery.ChargingState}`);
509
+ } else {
510
+ this.accessory.context.ChargingState = this.Battery.ChargingState;
511
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.ChargingState, this.Battery.ChargingState);
512
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic ChargingState: ${this.Battery.ChargingState}`);
513
+ }
514
+ // StatusLowBattery
515
+ if (this.Battery.StatusLowBattery === undefined) {
516
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.Battery.StatusLowBattery}`);
517
+ } else {
518
+ this.accessory.context.StatusLowBattery = this.Battery.StatusLowBattery;
519
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.Battery.StatusLowBattery);
520
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.Battery.StatusLowBattery}`);
521
+ }
522
+ }
523
+
524
+ async BLEPushConnection() {
525
+ if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
526
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Push Changes`);
527
+ await this.openAPIpushChanges();
528
+ }
529
+ }
530
+
531
+ async BLERefreshConnection(switchbot: any): Promise<void> {
532
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot:`
533
+ + ` ${JSON.stringify(switchbot)}`);
534
+ if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
535
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Refresh Status`);
536
+ await this.openAPIRefreshStatus();
537
+ }
538
+ }
539
+
540
+ async offlineOff(): Promise<void> {
541
+ if (this.device.offline) {
542
+ this.Fan.Service.updateCharacteristic(this.hap.Characteristic.Active, this.hap.Characteristic.Active.INACTIVE);
543
+ this.Fan.Service.updateCharacteristic(this.hap.Characteristic.RotationSpeed, 0);
544
+ this.Fan.Service.updateCharacteristic(this.hap.Characteristic.SwingMode, this.hap.Characteristic.SwingMode.SWING_DISABLED);
545
+ }
546
+ }
547
+
548
+ async apiError(e: any): Promise<void> {
549
+ this.Fan.Service.updateCharacteristic(this.hap.Characteristic.Active, e);
550
+ this.Fan.Service.updateCharacteristic(this.hap.Characteristic.RotationSpeed, e);
551
+ this.Fan.Service.updateCharacteristic(this.hap.Characteristic.SwingMode, e);
552
+ }
553
+ }