@switchbot/homebridge-switchbot 3.5.1-beta.11 → 3.5.1-beta.13

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 (153) hide show
  1. package/config.schema.json +19 -3
  2. package/dist/device/blindtilt.d.ts +6 -4
  3. package/dist/device/blindtilt.d.ts.map +1 -1
  4. package/dist/device/blindtilt.js +57 -45
  5. package/dist/device/blindtilt.js.map +1 -1
  6. package/dist/device/bot.d.ts +6 -3
  7. package/dist/device/bot.d.ts.map +1 -1
  8. package/dist/device/bot.js +38 -24
  9. package/dist/device/bot.js.map +1 -1
  10. package/dist/device/ceilinglight.d.ts +6 -3
  11. package/dist/device/ceilinglight.d.ts.map +1 -1
  12. package/dist/device/ceilinglight.js +44 -27
  13. package/dist/device/ceilinglight.js.map +1 -1
  14. package/dist/device/colorbulb.d.ts +6 -3
  15. package/dist/device/colorbulb.d.ts.map +1 -1
  16. package/dist/device/colorbulb.js +47 -32
  17. package/dist/device/colorbulb.js.map +1 -1
  18. package/dist/device/contact.d.ts +6 -3
  19. package/dist/device/contact.d.ts.map +1 -1
  20. package/dist/device/contact.js +40 -28
  21. package/dist/device/contact.js.map +1 -1
  22. package/dist/device/curtain.d.ts +6 -4
  23. package/dist/device/curtain.d.ts.map +1 -1
  24. package/dist/device/curtain.js +35 -22
  25. package/dist/device/curtain.js.map +1 -1
  26. package/dist/device/device.d.ts +13 -2
  27. package/dist/device/device.d.ts.map +1 -1
  28. package/dist/device/device.js +223 -108
  29. package/dist/device/device.js.map +1 -1
  30. package/dist/device/fan.d.ts +6 -3
  31. package/dist/device/fan.d.ts.map +1 -1
  32. package/dist/device/fan.js +48 -29
  33. package/dist/device/fan.js.map +1 -1
  34. package/dist/device/hub.d.ts +6 -3
  35. package/dist/device/hub.d.ts.map +1 -1
  36. package/dist/device/hub.js +43 -26
  37. package/dist/device/hub.js.map +1 -1
  38. package/dist/device/humidifier.d.ts +6 -3
  39. package/dist/device/humidifier.d.ts.map +1 -1
  40. package/dist/device/humidifier.js +43 -26
  41. package/dist/device/humidifier.js.map +1 -1
  42. package/dist/device/iosensor.d.ts +6 -3
  43. package/dist/device/iosensor.d.ts.map +1 -1
  44. package/dist/device/iosensor.js +39 -24
  45. package/dist/device/iosensor.js.map +1 -1
  46. package/dist/device/lightstrip.d.ts +6 -3
  47. package/dist/device/lightstrip.d.ts.map +1 -1
  48. package/dist/device/lightstrip.js +43 -27
  49. package/dist/device/lightstrip.js.map +1 -1
  50. package/dist/device/lock.d.ts +6 -3
  51. package/dist/device/lock.d.ts.map +1 -1
  52. package/dist/device/lock.js +33 -22
  53. package/dist/device/lock.js.map +1 -1
  54. package/dist/device/meter.d.ts +6 -3
  55. package/dist/device/meter.d.ts.map +1 -1
  56. package/dist/device/meter.js +38 -24
  57. package/dist/device/meter.js.map +1 -1
  58. package/dist/device/meterplus.d.ts +6 -3
  59. package/dist/device/meterplus.d.ts.map +1 -1
  60. package/dist/device/meterplus.js +37 -24
  61. package/dist/device/meterplus.js.map +1 -1
  62. package/dist/device/motion.d.ts +6 -3
  63. package/dist/device/motion.d.ts.map +1 -1
  64. package/dist/device/motion.js +33 -22
  65. package/dist/device/motion.js.map +1 -1
  66. package/dist/device/plug.d.ts +6 -3
  67. package/dist/device/plug.d.ts.map +1 -1
  68. package/dist/device/plug.js +27 -16
  69. package/dist/device/plug.js.map +1 -1
  70. package/dist/device/robotvacuumcleaner.d.ts +6 -3
  71. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  72. package/dist/device/robotvacuumcleaner.js +34 -21
  73. package/dist/device/robotvacuumcleaner.js.map +1 -1
  74. package/dist/device/waterdetector.d.ts +6 -3
  75. package/dist/device/waterdetector.d.ts.map +1 -1
  76. package/dist/device/waterdetector.js +38 -23
  77. package/dist/device/waterdetector.js.map +1 -1
  78. package/dist/irdevice/airconditioner.d.ts +1 -3
  79. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  80. package/dist/irdevice/airconditioner.js +32 -138
  81. package/dist/irdevice/airconditioner.js.map +1 -1
  82. package/dist/irdevice/airpurifier.d.ts +0 -1
  83. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  84. package/dist/irdevice/airpurifier.js +13 -44
  85. package/dist/irdevice/airpurifier.js.map +1 -1
  86. package/dist/irdevice/camera.d.ts.map +1 -1
  87. package/dist/irdevice/camera.js +3 -10
  88. package/dist/irdevice/camera.js.map +1 -1
  89. package/dist/irdevice/fan.d.ts.map +1 -1
  90. package/dist/irdevice/fan.js +7 -25
  91. package/dist/irdevice/fan.js.map +1 -1
  92. package/dist/irdevice/irdevice.d.ts +1 -1
  93. package/dist/irdevice/irdevice.js +5 -5
  94. package/dist/irdevice/irdevice.js.map +1 -1
  95. package/dist/irdevice/light.d.ts.map +1 -1
  96. package/dist/irdevice/light.js +9 -27
  97. package/dist/irdevice/light.js.map +1 -1
  98. package/dist/irdevice/other.js +1 -1
  99. package/dist/irdevice/other.js.map +1 -1
  100. package/dist/irdevice/tv.d.ts.map +1 -1
  101. package/dist/irdevice/tv.js +3 -18
  102. package/dist/irdevice/tv.js.map +1 -1
  103. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  104. package/dist/irdevice/vacuumcleaner.js +2 -9
  105. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  106. package/dist/irdevice/waterheater.d.ts.map +1 -1
  107. package/dist/irdevice/waterheater.js +2 -9
  108. package/dist/irdevice/waterheater.js.map +1 -1
  109. package/dist/platform.d.ts +0 -11
  110. package/dist/platform.d.ts.map +1 -1
  111. package/dist/platform.js +11 -184
  112. package/dist/platform.js.map +1 -1
  113. package/dist/settings.d.ts +9 -8
  114. package/dist/settings.d.ts.map +1 -1
  115. package/dist/settings.js.map +1 -1
  116. package/dist/types/bledevicestatus.d.ts +2 -1
  117. package/dist/types/bledevicestatus.d.ts.map +1 -1
  118. package/dist/types/devicestatus.d.ts +1 -1
  119. package/dist/types/devicestatus.d.ts.map +1 -1
  120. package/package.json +9 -9
  121. package/src/device/blindtilt.ts +60 -47
  122. package/src/device/bot.ts +47 -25
  123. package/src/device/ceilinglight.ts +53 -27
  124. package/src/device/colorbulb.ts +50 -39
  125. package/src/device/contact.ts +43 -28
  126. package/src/device/curtain.ts +45 -23
  127. package/src/device/device.ts +226 -96
  128. package/src/device/fan.ts +61 -30
  129. package/src/device/hub.ts +55 -29
  130. package/src/device/humidifier.ts +55 -31
  131. package/src/device/iosensor.ts +45 -27
  132. package/src/device/lightstrip.ts +48 -28
  133. package/src/device/lock.ts +49 -23
  134. package/src/device/meter.ts +54 -26
  135. package/src/device/meterplus.ts +50 -26
  136. package/src/device/motion.ts +45 -24
  137. package/src/device/plug.ts +34 -17
  138. package/src/device/robotvacuumcleaner.ts +49 -23
  139. package/src/device/waterdetector.ts +56 -25
  140. package/src/irdevice/airconditioner.ts +41 -125
  141. package/src/irdevice/airpurifier.ts +18 -40
  142. package/src/irdevice/camera.ts +4 -9
  143. package/src/irdevice/fan.ts +10 -22
  144. package/src/irdevice/irdevice.ts +5 -5
  145. package/src/irdevice/light.ts +10 -24
  146. package/src/irdevice/other.ts +1 -1
  147. package/src/irdevice/tv.ts +5 -16
  148. package/src/irdevice/vacuumcleaner.ts +3 -8
  149. package/src/irdevice/waterheater.ts +3 -8
  150. package/src/platform.ts +11 -184
  151. package/src/settings.ts +10 -8
  152. package/src/types/bledevicestatus.ts +4 -2
  153. package/src/types/devicestatus.ts +1 -1
@@ -35,6 +35,15 @@ export class RobotVacuumCleaner extends deviceBase {
35
35
  ChargingState: CharacteristicValue;
36
36
  };
37
37
 
38
+ // OpenAPI
39
+ deviceStatus!: robotVacuumCleanerS1Status | robotVacuumCleanerS1PlusStatus | floorCleaningRobotS10Status;
40
+
41
+ //Webhook
42
+ webhookContext!: robotVacuumCleanerS1WebhookContext | robotVacuumCleanerS1PlusWebhookContext | floorCleaningRobotS10WebhookContext;
43
+
44
+ // BLE
45
+ serviceData!: robotVacuumCleanerServiceData;
46
+
38
47
  // Updates
39
48
  robotVacuumCleanerUpdateInProgress!: boolean;
40
49
  doRobotVacuumCleanerUpdate!: Subject<void>;
@@ -115,12 +124,11 @@ export class RobotVacuumCleaner extends deviceBase {
115
124
  });
116
125
 
117
126
  // Retrieve initial values and updateHomekit
127
+ this.debugLog('Retrieve initial values and update Homekit');
118
128
  this.refreshStatus();
119
129
 
120
- // Update Homekit
121
- this.updateHomeKitCharacteristics();
122
-
123
130
  //regisiter webhook event handler
131
+ this.debugLog('Registering Webhook Event Handler');
124
132
  this.registerWebhook();
125
133
 
126
134
  // Start an update interval
@@ -150,39 +158,51 @@ export class RobotVacuumCleaner extends deviceBase {
150
158
  });
151
159
  }
152
160
 
153
- async BLEparseStatus(serviceData: robotVacuumCleanerServiceData): Promise<void> {
161
+ async BLEparseStatus(): Promise<void> {
154
162
  await this.debugLog('BLEparseStatus');
163
+ await this.debugLog(`(state, battery) = BLE: (${this.serviceData.state}, ${this.serviceData.battery}),`
164
+ + ` current: (${this.LightBulb.On}, ${this.Battery.BatteryLevel})`);
165
+
155
166
  // On
156
- this.LightBulb.On = serviceData.state === 'on' ? true : false;
167
+ this.LightBulb.On = this.serviceData.state === 'on' ? true : false;
157
168
  await this.debugLog(`On: ${this.LightBulb.On}`);
169
+
158
170
  // BatteryLevel
159
- this.Battery.BatteryLevel = Number(serviceData.battery);
171
+ this.Battery.BatteryLevel = this.serviceData.battery;
160
172
  await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
173
+
161
174
  // StatusLowBattery
162
175
  this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
163
176
  ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
164
177
  await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
165
178
  }
166
179
 
167
- async openAPIparseStatus(deviceStatus: robotVacuumCleanerS1Status | robotVacuumCleanerS1PlusStatus | floorCleaningRobotS10Status) {
180
+ async openAPIparseStatus() {
168
181
  await this.debugLog('openAPIparseStatus');
182
+ await this.debugLog(`(onlineStatus, battery, workingStatus) = API: (${this.deviceStatus.onlineStatus}, ${this.deviceStatus.battery},`
183
+ + ` ${this.deviceStatus.workingStatus}), current: (${this.LightBulb.On}, ${this.Battery.BatteryLevel}, ${this.Battery.ChargingState})`);
184
+
169
185
  // On
170
- this.LightBulb.On = deviceStatus.onlineStatus === 'online' ? true : false;
186
+ this.LightBulb.On = this.deviceStatus.onlineStatus === 'online' ? true : false;
171
187
  await this.debugLog(`On: ${this.LightBulb.On}`);
188
+
172
189
  // BatteryLevel
173
- this.Battery.BatteryLevel = Number(deviceStatus.battery);
190
+ this.Battery.BatteryLevel = this.deviceStatus.battery;
174
191
  await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
192
+
175
193
  // StatusLowBattery
176
194
  this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
177
195
  ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
178
196
  await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
197
+
179
198
  // ChargingState
180
- this.Battery.ChargingState = deviceStatus.workingStatus === 'Charging'
199
+ this.Battery.ChargingState = this.deviceStatus.workingStatus === 'Charging'
181
200
  ? this.hap.Characteristic.ChargingState.CHARGING : this.hap.Characteristic.ChargingState.NOT_CHARGING;
182
201
  await this.debugLog(`ChargingState: ${this.Battery.ChargingState}`);
202
+
183
203
  // Firmware Version
184
- if (deviceStatus.version) {
185
- const version = deviceStatus.version.toString();
204
+ if (this.deviceStatus.version) {
205
+ const version = this.deviceStatus.version.toString();
186
206
  await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
187
207
  const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
188
208
  this.accessory
@@ -196,23 +216,26 @@ export class RobotVacuumCleaner extends deviceBase {
196
216
  }
197
217
  }
198
218
 
199
- async parseStatusWebhook(context: robotVacuumCleanerS1WebhookContext | robotVacuumCleanerS1PlusWebhookContext
200
- | floorCleaningRobotS10WebhookContext): Promise<void> {
219
+ async parseStatusWebhook(): Promise<void> {
201
220
  await this.debugLog('parseStatusWebhook');
202
- await this.debugLog(`(onlineStatus, battery, workingStatus) = Webhook: (${context.onlineStatus}, ${context.battery}, ${context.workingStatus}),`
203
- + ` current: (${this.LightBulb.On}, ${this.Battery.BatteryLevel}, ${this.Battery.ChargingState})`);
221
+ await this.debugLog(`(onlineStatus, battery, workingStatus) = Webhook: (${this.webhookContext.onlineStatus}, ${this.webhookContext.battery},`
222
+ + ` ${this.webhookContext.workingStatus}), current: (${this.LightBulb.On}, ${this.Battery.BatteryLevel}, ${this.Battery.ChargingState})`);
223
+
204
224
  // On
205
- this.LightBulb.On = context.onlineStatus === 'online' ? true : false;
225
+ this.LightBulb.On = this.webhookContext.onlineStatus === 'online' ? true : false;
206
226
  await this.debugLog(`On: ${this.LightBulb.On}`);
227
+
207
228
  // BatteryLevel
208
- this.Battery.BatteryLevel = Number(context.battery);
229
+ this.Battery.BatteryLevel = this.webhookContext.battery;
209
230
  await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
231
+
210
232
  // StatusLowBattery
211
233
  this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
212
234
  ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
213
235
  await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
236
+
214
237
  // ChargingState
215
- this.Battery.ChargingState = context.workingStatus === 'Charging'
238
+ this.Battery.ChargingState = this.webhookContext.workingStatus === 'Charging'
216
239
  ? this.hap.Characteristic.ChargingState.CHARGING : this.hap.Characteristic.ChargingState.NOT_CHARGING;
217
240
  await this.debugLog(`ChargingState: ${this.Battery.ChargingState}`);
218
241
  }
@@ -246,7 +269,8 @@ export class RobotVacuumCleaner extends deviceBase {
246
269
  const serviceData = await this.monitorAdvertisementPackets(switchbot) as unknown as robotVacuumCleanerServiceData;
247
270
  // Update HomeKit
248
271
  if (serviceData.model === SwitchBotBLEModel.Unknown && serviceData.modelName === SwitchBotBLEModelName.Unknown) {
249
- await this.BLEparseStatus(serviceData);
272
+ this.serviceData = serviceData;
273
+ await this.BLEparseStatus();
250
274
  await this.updateHomeKitCharacteristics();
251
275
  } else {
252
276
  await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
@@ -264,7 +288,8 @@ export class RobotVacuumCleaner extends deviceBase {
264
288
  await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
265
289
  if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
266
290
  await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
267
- await this.openAPIparseStatus(deviceStatus.body);
291
+ this.deviceStatus = deviceStatus.body;
292
+ await this.openAPIparseStatus();
268
293
  await this.updateHomeKitCharacteristics();
269
294
  } else {
270
295
  await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
@@ -279,11 +304,12 @@ export class RobotVacuumCleaner extends deviceBase {
279
304
  async registerWebhook() {
280
305
  if (this.device.webhook) {
281
306
  await this.debugLog('is listening webhook.');
282
- this.platform.webhookEventHandler[this.device.deviceId] = async (context: robotVacuumCleanerS1WebhookContext
307
+ this.webhookEventHandler[this.device.deviceId] = async (context: robotVacuumCleanerS1WebhookContext
283
308
  | robotVacuumCleanerS1PlusWebhookContext | floorCleaningRobotS10WebhookContext) => {
284
309
  try {
285
310
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
286
- await this.parseStatusWebhook(context);
311
+ this.webhookContext = context;
312
+ await this.parseStatusWebhook();
287
313
  await this.updateHomeKitCharacteristics();
288
314
  } catch (e: any) {
289
315
  await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
@@ -36,6 +36,15 @@ export class WaterDetector extends deviceBase {
36
36
  LeakDetected: CharacteristicValue;
37
37
  };
38
38
 
39
+ // OpenAPI
40
+ deviceStatus!: waterLeakDetectorStatus;
41
+
42
+ //Webhook
43
+ webhookContext!: waterLeakDetectorWebhookContext;
44
+
45
+ // BLE
46
+ serviceData!: waterLeakDetectorServiceData;
47
+
39
48
  // Updates
40
49
  WaterDetectorUpdateInProgress!: boolean;
41
50
  doWaterDetectorUpdate: Subject<void>;
@@ -106,13 +115,11 @@ export class WaterDetector extends deviceBase {
106
115
  }
107
116
 
108
117
  // Retrieve initial values and updateHomekit
109
- this.debugLog('retrieve initial values and Update Homekit');
118
+ this.debugLog('Retrieve initial values and update Homekit');
110
119
  this.refreshStatus();
111
120
 
112
- // Retrieve initial values and updateHomekit
113
- // this.updateHomeKitCharacteristics();
114
-
115
121
  //regisiter webhook event handler
122
+ this.debugLog('Registering Webhook Event Handler');
116
123
  this.registerWebhook();
117
124
 
118
125
  // Start an update interval
@@ -124,18 +131,24 @@ export class WaterDetector extends deviceBase {
124
131
  });
125
132
  }
126
133
 
127
- async BLEparseStatus(serviceData: waterLeakDetectorServiceData): Promise<void> {
134
+ async BLEparseStatus(): Promise<void> {
128
135
  await this.debugLog('BLEparseStatus');
136
+ await this.debugLog(`(state, status, battery) = BLE: (${this.serviceData.state}, ${this.serviceData.status}, ${this.serviceData.battery}),`
137
+ + ` current:(${this.LeakSensor?.LeakDetected}, ${this.Battery.BatteryLevel})`);
138
+
139
+ //LeakSensor
129
140
  if (this.device.waterdetector?.hide_leak && this.LeakSensor?.Service) {
141
+
130
142
  // StatusActive
131
- this.LeakSensor.StatusActive = serviceData.state;
143
+ this.LeakSensor.StatusActive = this.serviceData.state;
132
144
  await this.debugLog(`StatusActive: ${this.LeakSensor.StatusActive}`);
145
+
133
146
  // LeakDetected
134
- this.LeakSensor.LeakDetected = serviceData.status;
147
+ this.LeakSensor.LeakDetected = this.serviceData.status;
135
148
  this.debugLog(`LeakDetected: ${this.LeakSensor.LeakDetected}`);
136
149
  }
137
150
  // BatteryLevel
138
- this.Battery.BatteryLevel = Number(serviceData.battery);
151
+ this.Battery.BatteryLevel = this.serviceData.battery;
139
152
  await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
140
153
  // StatusLowBattery
141
154
  this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
@@ -143,27 +156,36 @@ export class WaterDetector extends deviceBase {
143
156
  await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
144
157
  }
145
158
 
146
- async openAPIparseStatus(deviceStatus: waterLeakDetectorStatus): Promise<void> {
159
+ async openAPIparseStatus(): Promise<void> {
147
160
  await this.debugLog('openAPIparseStatus');
161
+ await this.debugLog(`(status, battery) = OpenAPI: (${this.deviceStatus.status}, ${this.deviceStatus.battery}),`
162
+ + ` current:(${this.LeakSensor?.LeakDetected}, ${this.Battery.BatteryLevel})`);
163
+
164
+ //LeakSensor
148
165
  if (!this.device.waterdetector?.hide_leak && this.LeakSensor?.Service) {
166
+
149
167
  // StatusActive
150
- this.LeakSensor.StatusActive = deviceStatus.battery === 0 ? false : true;
168
+ this.LeakSensor.StatusActive = this.deviceStatus.battery === 0 ? false : true;
151
169
  await this.debugLog(`StatusActive: ${this.LeakSensor.StatusActive}`);
170
+
152
171
  // LeakDetected
153
- this.LeakSensor.LeakDetected = deviceStatus.status;
172
+ this.LeakSensor.LeakDetected = this.deviceStatus.status;
154
173
  this.debugLog(`LeakDetected: ${this.LeakSensor.LeakDetected}`);
155
174
  }
175
+
156
176
  // BatteryLevel
157
- this.Battery.BatteryLevel = Number(deviceStatus.battery);
177
+ this.Battery.BatteryLevel = this.deviceStatus.battery;
158
178
  await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
179
+
159
180
  // StatusLowBattery
160
181
  this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
161
182
  ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
162
183
  await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
184
+
163
185
  // FirmwareVersion
164
- const version = deviceStatus.version.toString();
165
- await this.debugLog(`FirmwareVersion: ${version.replace(/^V|-.*$/g, '')}`);
166
- if (deviceStatus.version) {
186
+ if (this.deviceStatus.version) {
187
+ const version = this.deviceStatus.version.toString();
188
+ await this.debugLog(`FirmwareVersion: ${version.replace(/^V|-.*$/g, '')}`);
167
189
  const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
168
190
  this.accessory
169
191
  .getService(this.hap.Service.AccessoryInformation)!
@@ -176,21 +198,27 @@ export class WaterDetector extends deviceBase {
176
198
  }
177
199
  }
178
200
 
179
- async parseStatusWebhook(context: waterLeakDetectorWebhookContext): Promise<void> {
201
+ async parseStatusWebhook(): Promise<void> {
180
202
  await this.debugLog('parseStatusWebhook');
181
- await this.debugLog(`(detectionState, battery) = Webhook: (${context.detectionState}, ${context.battery}),`
203
+ await this.debugLog(`(detectionState, battery) = Webhook: (${this.webhookContext.detectionState}, ${this.webhookContext.battery}),`
182
204
  + ` current:(${this.LeakSensor?.LeakDetected}, ${this.Battery.BatteryLevel})`);
205
+
206
+ //LeakSensor
183
207
  if (!this.device.waterdetector?.hide_leak && this.LeakSensor?.Service) {
208
+
184
209
  // StatusActive
185
- this.LeakSensor.StatusActive = context.detectionState ? true : false;
210
+ this.LeakSensor.StatusActive = this.webhookContext.detectionState ? true : false;
186
211
  await this.debugLog(`StatusActive: ${this.LeakSensor.StatusActive}`);
212
+
187
213
  // LeakDetected
188
- this.LeakSensor.LeakDetected = context.detectionState;
214
+ this.LeakSensor.LeakDetected = this.webhookContext.detectionState;
189
215
  await this.debugLog(`LeakDetected: ${this.LeakSensor.LeakDetected}`);
190
216
  }
217
+
191
218
  // BatteryLevel
192
- this.Battery.BatteryLevel = Number(context.battery);
219
+ this.Battery.BatteryLevel = this.webhookContext.battery;
193
220
  await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
221
+
194
222
  // StatusLowBattery
195
223
  this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
196
224
  ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
@@ -223,10 +251,11 @@ export class WaterDetector extends deviceBase {
223
251
  // Start to monitor advertisement packets
224
252
  (async () => {
225
253
  // Start to monitor advertisement packets
226
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as unknown as waterLeakDetectorServiceData;
254
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as waterLeakDetectorServiceData;
227
255
  // Update HomeKit
228
256
  if (serviceData.model === SwitchBotBLEModel.Unknown && serviceData.modelName === SwitchBotBLEModelName.Unknown) {
229
- await this.BLEparseStatus(serviceData);
257
+ this.serviceData = serviceData;
258
+ await this.BLEparseStatus();
230
259
  await this.updateHomeKitCharacteristics();
231
260
  } else {
232
261
  await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
@@ -244,7 +273,8 @@ export class WaterDetector extends deviceBase {
244
273
  await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
245
274
  if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
246
275
  await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
247
- await this.openAPIparseStatus(deviceStatus.body);
276
+ this.deviceStatus = deviceStatus.body;
277
+ await this.openAPIparseStatus();
248
278
  await this.updateHomeKitCharacteristics();
249
279
  } else {
250
280
  await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
@@ -259,10 +289,11 @@ export class WaterDetector extends deviceBase {
259
289
  async registerWebhook() {
260
290
  if (this.device.webhook) {
261
291
  await this.debugLog('is listening webhook.');
262
- this.platform.webhookEventHandler[this.device.deviceId] = async (context: waterLeakDetectorWebhookContext) => {
292
+ this.webhookEventHandler[this.device.deviceId] = async (context: waterLeakDetectorWebhookContext) => {
263
293
  try {
264
294
  await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
265
- await this.parseStatusWebhook(context);
295
+ this.webhookContext = context;
296
+ await this.parseStatusWebhook();
266
297
  await this.updateHomeKitCharacteristics();
267
298
  } catch (e: any) {
268
299
  await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
@@ -28,6 +28,7 @@ export class AirConditioner extends irdeviceBase {
28
28
  };
29
29
 
30
30
  meter?: PlatformAccessory;
31
+
31
32
  private HumiditySensor?: {
32
33
  Name: CharacteristicValue;
33
34
  Service: Service;
@@ -351,7 +352,7 @@ export class AirConditioner extends irdeviceBase {
351
352
  const { body, statusCode } = await this.pushChangeRequest(bodyChange);
352
353
  const deviceStatus: any = await body.json();
353
354
  await this.pushStatusCodes(statusCode, deviceStatus);
354
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
355
+ if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
355
356
  await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
356
357
  await this.updateHomeKitCharacteristics();
357
358
  } else {
@@ -500,39 +501,25 @@ export class AirConditioner extends irdeviceBase {
500
501
  }
501
502
 
502
503
  async ThresholdTemperatureGet(): Promise<CharacteristicValue> {
504
+ this.HeaterCooler.Active = this.HeaterCooler.Active === undefined ? this.hap.Characteristic.Active.INACTIVE
505
+ : this.HeaterCooler.Active ?? this.accessory.context.Active;
503
506
 
504
- if (this.HeaterCooler.Active === undefined) {
505
- this.HeaterCooler.Active = this.hap.Characteristic.Active.INACTIVE;
506
- } else {
507
- this.HeaterCooler.Active = this.HeaterCooler.Active ? this.HeaterCooler.Active : this.accessory.context.Active;
508
- }
507
+ this.HeaterCooler.CurrentTemperature = (this.HeaterCooler.CurrentTemperature === undefined
508
+ && this.accessory.context.CurrentTemperature === undefined) ? 24 : this.HeaterCooler.CurrentTemperature
509
+ ?? this.accessory.context.CurrentTemperature;
509
510
 
510
- if (this.HeaterCooler.CurrentTemperature === undefined && this.accessory.context.CurrentTemperature === undefined) {
511
- this.HeaterCooler.CurrentTemperature = 24;
512
- } else {
513
- this.HeaterCooler.CurrentTemperature = this.HeaterCooler.CurrentTemperature || this.accessory.context.CurrentTemperature;
514
- }
511
+ this.HeaterCooler.ThresholdTemperature = this.HeaterCooler.ThresholdTemperature === undefined ? 24 : this.HeaterCooler.ThresholdTemperature
512
+ ?? this.accessory.context.ThresholdTemperature;
515
513
 
516
- if (this.HeaterCooler.ThresholdTemperature === undefined && this.accessory.context.ThresholdTemperature === undefined) {
517
- this.HeaterCooler.ThresholdTemperature = 24;
518
- } else {
519
- this.HeaterCooler.ThresholdTemperature = this.HeaterCooler.ThresholdTemperature || this.accessory.context.ThresholdTemperature;
520
- }
521
-
522
- if (this.HeaterCooler.RotationSpeed === undefined && this.accessory.context.RotationSpeed === undefined) {
523
- this.HeaterCooler.RotationSpeed = 4;
524
- } else {
525
- this.HeaterCooler.RotationSpeed = this.HeaterCooler.RotationSpeed || this.accessory.context.RotationSpeed;
526
- }
514
+ this.HeaterCooler.RotationSpeed = (this.HeaterCooler.RotationSpeed === undefined && this.accessory.context.RotationSpeed === undefined) ? 4
515
+ : this.HeaterCooler.RotationSpeed ?? this.accessory.context.RotationSpeed;
527
516
 
528
517
  await this.getAirConditionerConfigSettings(this.accessory, this.device);
529
518
 
530
- if (this.meter) {
531
- if (this.HumiditySensor!.CurrentRelativeHumidity === undefined && this.accessory.context.CurrentRelativeHumidity === undefined) {
532
- this.HumiditySensor!.CurrentRelativeHumidity = 0;
533
- } else {
534
- this.HumiditySensor!.CurrentRelativeHumidity = this.HumiditySensor!.CurrentRelativeHumidity || this.accessory.context.CurrentRelativeHumidity;
535
- }
519
+ if (this.meter && this.HumiditySensor?.Service) {
520
+ this.HumiditySensor.CurrentRelativeHumidity = (this.HumiditySensor.CurrentRelativeHumidity === undefined
521
+ && this.accessory.context.CurrentRelativeHumidity === undefined) ? 0 : this.HumiditySensor.CurrentRelativeHumidity
522
+ ?? this.accessory.context.CurrentRelativeHumidity;
536
523
  }
537
524
  await this.debugLog(`Get ThresholdTemperature: ${this.HeaterCooler.ThresholdTemperature}`);
538
525
  return this.HeaterCooler.ThresholdTemperature;
@@ -548,73 +535,34 @@ export class AirConditioner extends irdeviceBase {
548
535
  async updateHomeKitCharacteristics(): Promise<void> {
549
536
  await this.debugLog('updateHomeKitCharacteristics');
550
537
  // Active
551
- if (this.HeaterCooler.Active === undefined) {
552
- await this.debugLog(`Active: ${this.HeaterCooler.Active}`);
553
- } else {
554
- this.accessory.context.Active = this.HeaterCooler.Active;
555
- this.HeaterCooler.Service.updateCharacteristic(this.hap.Characteristic.Active, this.HeaterCooler.Active);
556
- await this.debugLog(`updateCharacteristic Active: ${this.HeaterCooler.Active}`);
557
- }
538
+ await this.updateCharacteristic(this.HeaterCooler.Service, this.hap.Characteristic.Active,
539
+ this.HeaterCooler.Active, 'Active');
558
540
  // RotationSpeed
559
- if (this.HeaterCooler.RotationSpeed === undefined) {
560
- await this.debugLog(`RotationSpeed: ${this.HeaterCooler.RotationSpeed}`);
561
- } else {
562
- this.accessory.context.RotationSpeed = this.HeaterCooler.RotationSpeed;
563
- this.HeaterCooler.Service.updateCharacteristic(this.hap.Characteristic.RotationSpeed, this.HeaterCooler.RotationSpeed);
564
- await this.debugLog('updateCharacteristic'
565
- + ` RotationSpeed: ${this.HeaterCooler.RotationSpeed}`);
566
- }
541
+ await this.updateCharacteristic(this.HeaterCooler.Service, this.hap.Characteristic.RotationSpeed,
542
+ this.HeaterCooler.RotationSpeed, 'RotationSpeed');
567
543
  // CurrentTemperature
568
- if (this.HeaterCooler.CurrentTemperature === undefined) {
569
- await this.debugLog(`CurrentTemperature: ${this.HeaterCooler.CurrentTemperature}`);
570
- } else {
571
- this.accessory.context.CurrentTemperature = this.HeaterCooler.CurrentTemperature;
572
- this.HeaterCooler.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, this.HeaterCooler.CurrentTemperature);
573
- await this.debugLog('updateCharacteristic'
574
- + ` CurrentTemperature: ${this.HeaterCooler.CurrentTemperature}`);
575
- }
576
- // CurrentRelativeHumidity
577
- if (this.meter) {
578
- if (this.HumiditySensor!.CurrentRelativeHumidity === undefined) {
579
- await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor!.CurrentRelativeHumidity}`);
580
- } else {
581
- this.accessory.context.CurrentRelativeHumidity = this.HumiditySensor!.CurrentRelativeHumidity;
582
- this.HeaterCooler.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity,
583
- this.HumiditySensor!.CurrentRelativeHumidity);
584
- await this.debugLog('updateCharacteristic'
585
- + ` CurrentRelativeHumidity: ${this.HumiditySensor!.CurrentRelativeHumidity}`);
586
- }
587
- }
544
+ await this.updateCharacteristic(this.HeaterCooler.Service, this.hap.Characteristic.CurrentTemperature,
545
+ this.HeaterCooler.CurrentTemperature, 'CurrentTemperature');
588
546
  // TargetHeaterCoolerState
589
- if (this.HeaterCooler.TargetHeaterCoolerState === undefined) {
590
- await this.debugLog(`TargetHeaterCoolerState: ${this.HeaterCooler.TargetHeaterCoolerState}`);
591
- } else {
592
- this.accessory.context.TargetHeaterCoolerState = this.HeaterCooler.TargetHeaterCoolerState;
593
- this.HeaterCooler.Service.updateCharacteristic(this.hap.Characteristic.TargetHeaterCoolerState, this.HeaterCooler.TargetHeaterCoolerState);
594
- await this.debugLog('updateCharacteristic'
595
- + ` TargetHeaterCoolerState: ${this.HeaterCooler.TargetHeaterCoolerState}`);
596
- }
547
+ await this.updateCharacteristic(this.HeaterCooler.Service, this.hap.Characteristic.TargetHeaterCoolerState,
548
+ this.HeaterCooler.TargetHeaterCoolerState, 'TargetHeaterCoolerState');
597
549
  // CurrentHeaterCoolerState
598
- if (this.HeaterCooler.CurrentHeaterCoolerState === undefined) {
599
- await this.debugLog(`CurrentHeaterCoolerState: ${this.HeaterCooler.CurrentHeaterCoolerState}`);
600
- } else {
601
- this.accessory.context.CurrentHeaterCoolerState = this.HeaterCooler.CurrentHeaterCoolerState;
602
- this.HeaterCooler.Service.updateCharacteristic(this.hap.Characteristic.CurrentHeaterCoolerState, this.HeaterCooler.CurrentHeaterCoolerState);
603
- await this.debugLog(`updateCharacteristic CurrentHeaterCoolerState: ${this.HeaterCooler.CurrentHeaterCoolerState}`);
604
- }
605
- // ThresholdTemperature
606
- if (this.HeaterCooler.ThresholdTemperature === undefined) {
607
- await this.debugLog(`ThresholdTemperature: ${this.HeaterCooler.ThresholdTemperature}`);
608
- } else {
609
- this.accessory.context.ThresholdTemperature = this.HeaterCooler.ThresholdTemperature;
610
- this.HeaterCooler.Service.updateCharacteristic(this.hap.Characteristic.HeatingThresholdTemperature, this.HeaterCooler.ThresholdTemperature);
611
- this.HeaterCooler.Service.updateCharacteristic(this.hap.Characteristic.CoolingThresholdTemperature, this.HeaterCooler.ThresholdTemperature);
612
- await this.debugLog('updateCharacteristic'
613
- + ` ThresholdTemperature: ${this.HeaterCooler.ThresholdTemperature}`);
550
+ await this.updateCharacteristic(this.HeaterCooler.Service, this.hap.Characteristic.CurrentHeaterCoolerState,
551
+ this.HeaterCooler.CurrentHeaterCoolerState, 'CurrentHeaterCoolerState');
552
+ // HeatingThresholdTemperature
553
+ await this.updateCharacteristic(this.HeaterCooler.Service, this.hap.Characteristic.HeatingThresholdTemperature,
554
+ this.HeaterCooler.ThresholdTemperature, 'ThresholdTemperature');
555
+ // CoolingThresholdTemperature
556
+ await this.updateCharacteristic(this.HeaterCooler.Service, this.hap.Characteristic.CoolingThresholdTemperature,
557
+ this.HeaterCooler.ThresholdTemperature, 'ThresholdTemperature');
558
+ if (this.meter && this.HumiditySensor?.Service) {
559
+ // CurrentRelativeHumidity
560
+ await this.updateCharacteristic(this.HumiditySensor.Service, this.hap.Characteristic.CurrentRelativeHumidity,
561
+ this.HumiditySensor.CurrentRelativeHumidity, 'CurrentRelativeHumidity');
614
562
  }
615
563
  }
616
564
 
617
- async apiError({ e }: { e: any }): Promise<void> {
565
+ async apiError(e: any): Promise<void> {
618
566
  this.HeaterCooler.Service.updateCharacteristic(this.hap.Characteristic.Active, e);
619
567
  this.HeaterCooler.Service.updateCharacteristic(this.hap.Characteristic.RotationSpeed, e);
620
568
  this.HeaterCooler.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, e);
@@ -626,42 +574,10 @@ export class AirConditioner extends irdeviceBase {
626
574
  }
627
575
 
628
576
  async getAirConditionerConfigSettings(accessory: PlatformAccessory, device: irdevice & irDevicesConfig): Promise<void> {
629
- if (this.device.irair?.hide_automode) {
630
- this.hide_automode = device.irair?.hide_automode;
631
- accessory.context.hide_automode = this.hide_automode;
632
- } else {
633
- this.hide_automode = device.irair?.hide_automode;
634
- accessory.context.hide_automode = this.hide_automode;
635
- }
636
-
637
- if (this.device.irair?.set_max_heat) {
638
- this.set_max_heat = device.irair?.set_max_heat;
639
- accessory.context.set_max_heat = this.set_max_heat;
640
- } else {
641
- this.set_max_heat = 35;
642
- accessory.context.set_max_heat = this.set_max_heat;
643
- }
644
- if (this.device.irair?.set_min_heat) {
645
- this.set_min_heat = device.irair?.set_min_heat;
646
- accessory.context.set_min_heat = this.set_min_heat;
647
- } else {
648
- this.set_min_heat = 0;
649
- accessory.context.set_min_heat = this.set_min_heat;
650
- }
651
-
652
- if (this.device.irair?.set_max_cool) {
653
- this.set_max_cool = device.irair?.set_max_cool;
654
- accessory.context.set_max_cool = this.set_max_cool;
655
- } else {
656
- this.set_max_cool = 35;
657
- accessory.context.set_max_cool = this.set_max_cool;
658
- }
659
- if (this.device.irair?.set_min_cool) {
660
- this.set_min_cool = device.irair?.set_min_cool;
661
- accessory.context.set_min_cool = this.set_min_cool;
662
- } else {
663
- this.set_min_cool = 0;
664
- accessory.context.set_min_cool = this.set_min_cool;
665
- }
577
+ accessory.context.hide_automode = this.hide_automode = device.irair?.hide_automode;
578
+ accessory.context.set_max_heat = this.set_max_heat = device.irair?.set_max_heat ?? 35;
579
+ accessory.context.set_min_heat = this.set_min_heat = device.irair?.set_min_heat ?? 0;
580
+ accessory.context.set_max_cool = this.set_max_cool = device.irair?.set_max_cool ?? 35;
581
+ accessory.context.set_min_cool = this.set_min_cool = device.irair?.set_min_cool ?? 0;
666
582
  }
667
583
  }