@switchbot/homebridge-switchbot 2.0.0-beta.19 → 2.0.0-beta.20

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 (112) hide show
  1. package/CHANGELOG.md +6 -1
  2. package/README.md +1 -2
  3. package/config.schema.json +51 -32
  4. package/dist/device/bot.d.ts +32 -29
  5. package/dist/device/bot.d.ts.map +1 -1
  6. package/dist/device/bot.js +549 -575
  7. package/dist/device/bot.js.map +1 -1
  8. package/dist/device/colorbulb.d.ts +33 -15
  9. package/dist/device/colorbulb.d.ts.map +1 -1
  10. package/dist/device/colorbulb.js +426 -341
  11. package/dist/device/colorbulb.js.map +1 -1
  12. package/dist/device/contact.d.ts +16 -9
  13. package/dist/device/contact.d.ts.map +1 -1
  14. package/dist/device/contact.js +246 -208
  15. package/dist/device/contact.js.map +1 -1
  16. package/dist/device/curtain.d.ts +21 -18
  17. package/dist/device/curtain.d.ts.map +1 -1
  18. package/dist/device/curtain.js +449 -449
  19. package/dist/device/curtain.js.map +1 -1
  20. package/dist/device/humidifier.d.ts +22 -20
  21. package/dist/device/humidifier.d.ts.map +1 -1
  22. package/dist/device/humidifier.js +346 -324
  23. package/dist/device/humidifier.js.map +1 -1
  24. package/dist/device/lock.d.ts +22 -10
  25. package/dist/device/lock.d.ts.map +1 -1
  26. package/dist/device/lock.js +257 -178
  27. package/dist/device/lock.js.map +1 -1
  28. package/dist/device/meter.d.ts +21 -14
  29. package/dist/device/meter.d.ts.map +1 -1
  30. package/dist/device/meter.js +287 -239
  31. package/dist/device/meter.js.map +1 -1
  32. package/dist/device/meterplus.d.ts +23 -10
  33. package/dist/device/meterplus.d.ts.map +1 -1
  34. package/dist/device/meterplus.js +308 -190
  35. package/dist/device/meterplus.js.map +1 -1
  36. package/dist/device/motion.d.ts +17 -10
  37. package/dist/device/motion.d.ts.map +1 -1
  38. package/dist/device/motion.js +225 -193
  39. package/dist/device/motion.js.map +1 -1
  40. package/dist/device/plug.d.ts +24 -17
  41. package/dist/device/plug.d.ts.map +1 -1
  42. package/dist/device/plug.js +270 -267
  43. package/dist/device/plug.js.map +1 -1
  44. package/dist/device/striplight.d.ts +36 -14
  45. package/dist/device/striplight.d.ts.map +1 -1
  46. package/dist/device/striplight.js +447 -299
  47. package/dist/device/striplight.js.map +1 -1
  48. package/dist/irdevice/airconditioner.d.ts +7 -14
  49. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  50. package/dist/irdevice/airconditioner.js +177 -169
  51. package/dist/irdevice/airconditioner.js.map +1 -1
  52. package/dist/irdevice/airpurifier.d.ts +3 -5
  53. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  54. package/dist/irdevice/airpurifier.js +72 -57
  55. package/dist/irdevice/airpurifier.js.map +1 -1
  56. package/dist/irdevice/camera.d.ts +3 -4
  57. package/dist/irdevice/camera.d.ts.map +1 -1
  58. package/dist/irdevice/camera.js +47 -33
  59. package/dist/irdevice/camera.js.map +1 -1
  60. package/dist/irdevice/fan.d.ts +3 -4
  61. package/dist/irdevice/fan.d.ts.map +1 -1
  62. package/dist/irdevice/fan.js +66 -48
  63. package/dist/irdevice/fan.js.map +1 -1
  64. package/dist/irdevice/light.d.ts +3 -4
  65. package/dist/irdevice/light.d.ts.map +1 -1
  66. package/dist/irdevice/light.js +48 -47
  67. package/dist/irdevice/light.js.map +1 -1
  68. package/dist/irdevice/other.d.ts +3 -4
  69. package/dist/irdevice/other.d.ts.map +1 -1
  70. package/dist/irdevice/other.js +47 -33
  71. package/dist/irdevice/other.js.map +1 -1
  72. package/dist/irdevice/tv.d.ts +10 -11
  73. package/dist/irdevice/tv.d.ts.map +1 -1
  74. package/dist/irdevice/tv.js +64 -48
  75. package/dist/irdevice/tv.js.map +1 -1
  76. package/dist/irdevice/vacuumcleaner.d.ts +7 -12
  77. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  78. package/dist/irdevice/vacuumcleaner.js +52 -41
  79. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  80. package/dist/irdevice/waterheater.d.ts +6 -11
  81. package/dist/irdevice/waterheater.d.ts.map +1 -1
  82. package/dist/irdevice/waterheater.js +52 -42
  83. package/dist/irdevice/waterheater.js.map +1 -1
  84. package/dist/platform.d.ts +0 -4
  85. package/dist/platform.d.ts.map +1 -1
  86. package/dist/platform.js +44 -66
  87. package/dist/platform.js.map +1 -1
  88. package/dist/settings.d.ts +3 -3
  89. package/dist/settings.d.ts.map +1 -1
  90. package/package.json +7 -6
  91. package/src/device/bot.ts +551 -589
  92. package/src/device/colorbulb.ts +441 -375
  93. package/src/device/contact.ts +249 -213
  94. package/src/device/curtain.ts +452 -461
  95. package/src/device/humidifier.ts +358 -365
  96. package/src/device/lock.ts +263 -179
  97. package/src/device/meter.ts +302 -253
  98. package/src/device/meterplus.ts +319 -194
  99. package/src/device/motion.ts +230 -199
  100. package/src/device/plug.ts +273 -274
  101. package/src/device/striplight.ts +460 -316
  102. package/src/irdevice/airconditioner.ts +181 -183
  103. package/src/irdevice/airpurifier.ts +70 -60
  104. package/src/irdevice/camera.ts +46 -36
  105. package/src/irdevice/fan.ts +64 -49
  106. package/src/irdevice/light.ts +47 -51
  107. package/src/irdevice/other.ts +46 -36
  108. package/src/irdevice/tv.ts +64 -51
  109. package/src/irdevice/vacuumcleaner.ts +52 -45
  110. package/src/irdevice/waterheater.ts +50 -45
  111. package/src/platform.ts +44 -68
  112. package/src/settings.ts +3 -3
@@ -20,19 +20,15 @@ class Humidifier {
20
20
  this.platform = platform;
21
21
  this.accessory = accessory;
22
22
  this.device = device;
23
+ // Connection
24
+ this.BLE = (this.device.connectionType === 'BLE' || this.device.connectionType === 'Both');
25
+ this.OpenAPI = (this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'Both');
23
26
  // default placeholders
24
27
  this.logs(device);
25
- this.openAPI();
26
28
  this.scan(device);
27
29
  this.refreshRate(device);
28
30
  this.config(device);
29
- this.CurrentRelativeHumidity = 0;
30
- this.TargetHumidifierDehumidifierState = this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER;
31
- this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.INACTIVE;
32
- this.Active = this.platform.Characteristic.Active.ACTIVE;
33
- this.RelativeHumidityHumidifierThreshold = 0;
34
- this.CurrentTemperature = 0;
35
- this.WaterLevel = 0;
31
+ this.context();
36
32
  // this is subject we use to track when we need to POST changes to the SwitchBot API
37
33
  this.doHumidifierUpdate = new rxjs_1.Subject();
38
34
  this.humidifierUpdateInProgress = false;
@@ -70,8 +66,8 @@ class Humidifier {
70
66
  maxValue: 1,
71
67
  validValues: [0, 1],
72
68
  })
73
- .onSet(this.handleTargetHumidifierDehumidifierStateSet.bind(this));
74
- this.humidifierService.getCharacteristic(this.platform.Characteristic.Active).onSet(this.handleActiveSet.bind(this));
69
+ .onSet(this.TargetHumidifierDehumidifierStateSet.bind(this));
70
+ this.humidifierService.getCharacteristic(this.platform.Characteristic.Active).onSet(this.ActiveSet.bind(this));
75
71
  this.humidifierService
76
72
  .getCharacteristic(this.platform.Characteristic.RelativeHumidityHumidifierThreshold)
77
73
  .setProps({
@@ -80,14 +76,14 @@ class Humidifier {
80
76
  maxValue: 100,
81
77
  minStep: this.minStep(),
82
78
  })
83
- .onSet(this.handleRelativeHumidityHumidifierThresholdSet.bind(this));
79
+ .onSet(this.RelativeHumidityHumidifierThresholdSet.bind(this));
84
80
  // Temperature Sensor Service
85
- if (device.humidifier?.hide_temperature || device.ble) {
81
+ if (device.humidifier?.hide_temperature || this.BLE) {
86
82
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Temperature Sensor Service`);
87
83
  this.temperatureservice = this.accessory.getService(this.platform.Service.TemperatureSensor);
88
84
  accessory.removeService(this.temperatureservice);
89
85
  }
90
- else if (!this.temperatureservice && !device.ble) {
86
+ else if (!this.temperatureservice && !this.BLE) {
91
87
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Temperature Sensor Service`);
92
88
  (this.temperatureservice =
93
89
  this.accessory.getService(this.platform.Service.TemperatureSensor) || this.accessory.addService(this.platform.Service.TemperatureSensor)),
@@ -127,12 +123,9 @@ class Humidifier {
127
123
  await this.pushChanges();
128
124
  }
129
125
  catch (e) {
130
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges`);
131
- if (this.deviceLogging.includes('debug')) {
132
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges,`
133
- + ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
134
- }
135
126
  this.apiError(e);
127
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with ${this.device.connectionType} Connection,`
128
+ + ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
136
129
  }
137
130
  this.humidifierUpdateInProgress = false;
138
131
  });
@@ -141,15 +134,20 @@ class Humidifier {
141
134
  * Parse the device status from the SwitchBot api
142
135
  */
143
136
  async parseStatus() {
144
- if (this.device.ble) {
137
+ if (this.BLE && !this.SwitchToOpenAPI) {
145
138
  await this.BLEparseStatus();
146
139
  }
147
- else {
140
+ else if (this.OpenAPI && this.platform.config.credentials?.token) {
141
+ this.SwitchToOpenAPI = false;
148
142
  await this.openAPIparseStatus();
149
143
  }
144
+ else {
145
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
146
+ + ` ${this.device.connectionType}, parseStatus will not happen.`);
147
+ }
150
148
  }
151
149
  async BLEparseStatus() {
152
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE parseStatus`);
150
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
153
151
  // Current Relative Humidity
154
152
  this.CurrentRelativeHumidity = this.percentage;
155
153
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
@@ -163,79 +161,81 @@ class Humidifier {
163
161
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Active: ${this.Active}`);
164
162
  }
165
163
  async openAPIparseStatus() {
166
- if (this.platform.config.credentials?.token) {
167
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} OpenAPI parseStatus`);
168
- // Current Relative Humidity
169
- this.CurrentRelativeHumidity = this.humidity;
170
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
171
- // Current Temperature
172
- if (!this.device.humidifier?.hide_temperature) {
173
- this.CurrentTemperature = this.temperature;
174
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
175
- }
176
- // Target Humidifier Dehumidifier State
177
- switch (this.auto) {
178
- case true:
179
- this.TargetHumidifierDehumidifierState = this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER_OR_DEHUMIDIFIER;
164
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
165
+ // Current Relative Humidity
166
+ this.CurrentRelativeHumidity = this.humidity;
167
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
168
+ // Current Temperature
169
+ if (!this.device.humidifier?.hide_temperature) {
170
+ this.CurrentTemperature = this.temperature;
171
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
172
+ }
173
+ // Target Humidifier Dehumidifier State
174
+ switch (this.auto) {
175
+ case true:
176
+ this.TargetHumidifierDehumidifierState = this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER_OR_DEHUMIDIFIER;
177
+ this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.HUMIDIFYING;
178
+ this.RelativeHumidityHumidifierThreshold = this.CurrentRelativeHumidity;
179
+ break;
180
+ default:
181
+ this.TargetHumidifierDehumidifierState = this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER;
182
+ if (this.nebulizationEfficiency > 100) {
183
+ this.RelativeHumidityHumidifierThreshold = 100;
184
+ }
185
+ else {
186
+ this.RelativeHumidityHumidifierThreshold = this.nebulizationEfficiency;
187
+ }
188
+ if (this.CurrentRelativeHumidity > this.RelativeHumidityHumidifierThreshold) {
189
+ this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.IDLE;
190
+ }
191
+ else if (this.Active === this.platform.Characteristic.Active.INACTIVE) {
192
+ this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.INACTIVE;
193
+ }
194
+ else {
180
195
  this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.HUMIDIFYING;
181
- this.RelativeHumidityHumidifierThreshold = this.CurrentRelativeHumidity;
182
- break;
183
- default:
184
- this.TargetHumidifierDehumidifierState = this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER;
185
- if (this.nebulizationEfficiency > 100) {
186
- this.RelativeHumidityHumidifierThreshold = 100;
187
- }
188
- else {
189
- this.RelativeHumidityHumidifierThreshold = this.nebulizationEfficiency;
190
- }
191
- if (this.CurrentRelativeHumidity > this.RelativeHumidityHumidifierThreshold) {
192
- this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.IDLE;
193
- }
194
- else if (this.Active === this.platform.Characteristic.Active.INACTIVE) {
195
- this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.INACTIVE;
196
- }
197
- else {
198
- this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.HUMIDIFYING;
199
- }
200
- }
201
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
202
- + ` TargetHumidifierDehumidifierState: ${this.TargetHumidifierDehumidifierState}`);
203
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
204
- + ` RelativeHumidityHumidifierThreshold: ${this.RelativeHumidityHumidifierThreshold}`);
205
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
206
- + ` CurrentHumidifierDehumidifierState: ${this.CurrentHumidifierDehumidifierState}`);
207
- // Active
208
- switch (this.power) {
209
- case 'on':
210
- this.Active = this.platform.Characteristic.Active.ACTIVE;
211
- break;
212
- default:
213
- this.Active = this.platform.Characteristic.Active.INACTIVE;
214
- }
215
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Active: ${this.Active}`);
216
- // Water Level
217
- if (this.lackWater) {
218
- this.WaterLevel = 0;
219
- }
220
- else {
221
- this.WaterLevel = 100;
222
- }
223
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} WaterLevel: ${this.WaterLevel}`);
196
+ }
197
+ }
198
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
199
+ + ` TargetHumidifierDehumidifierState: ${this.TargetHumidifierDehumidifierState}`);
200
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
201
+ + ` RelativeHumidityHumidifierThreshold: ${this.RelativeHumidityHumidifierThreshold}`);
202
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
203
+ + ` CurrentHumidifierDehumidifierState: ${this.CurrentHumidifierDehumidifierState}`);
204
+ // Active
205
+ switch (this.power) {
206
+ case 'on':
207
+ this.Active = this.platform.Characteristic.Active.ACTIVE;
208
+ break;
209
+ default:
210
+ this.Active = this.platform.Characteristic.Active.INACTIVE;
211
+ }
212
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Active: ${this.Active}`);
213
+ // Water Level
214
+ if (this.lackWater) {
215
+ this.WaterLevel = 0;
224
216
  }
217
+ else {
218
+ this.WaterLevel = 100;
219
+ }
220
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} WaterLevel: ${this.WaterLevel}`);
225
221
  }
226
222
  /**
227
223
  * Asks the SwitchBot API for the latest device information
228
224
  */
229
225
  async refreshStatus() {
230
- if (this.device.ble) {
226
+ if (this.BLE) {
231
227
  await this.BLERefreshStatus();
232
228
  }
233
- else {
229
+ else if (this.OpenAPI && this.platform.config.credentials?.token) {
234
230
  await this.openAPIRefreshStatus();
235
231
  }
232
+ else {
233
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
234
+ + ` ${this.device.connectionType}, refreshStatus will not happen.`);
235
+ }
236
236
  }
237
237
  async BLERefreshStatus() {
238
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE refreshStatus`);
238
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
239
239
  const switchbot = await this.platform.connectBLE();
240
240
  // Convert to BLE Address
241
241
  this.device.bleMac = this.device
@@ -292,123 +292,87 @@ class Humidifier {
292
292
  }
293
293
  })
294
294
  .catch(async (e) => {
295
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
296
- if (this.deviceLogging.includes('debug')) {
297
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
298
- + ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
299
- }
300
- if (this.platform.config.credentials?.token) {
301
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
302
- await this.openAPIRefreshStatus();
303
- }
304
295
  this.apiError(e);
296
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}`
297
+ + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
298
+ await this.BLEconnection(switchbot);
305
299
  });
306
300
  }
307
301
  else {
308
302
  await this.BLEconnection(switchbot);
309
303
  }
310
304
  }
311
- async getCustomBLEAddress(switchbot) {
312
- if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
313
- (async () => {
314
- // Start to monitor advertisement packets
315
- await switchbot.startScan({
316
- model: 'e',
317
- });
318
- // Set an event handler
319
- switchbot.onadvertisement = (ad) => {
320
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${(0, super_stringify_1.default)(ad, null, ' ')}`);
321
- };
322
- await switchbot.wait(10000);
323
- // Stop to monitor
324
- switchbot.stopScan();
325
- })();
326
- }
327
- }
328
- async BLEconnection(switchbot) {
329
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
330
- if (this.platform.config.credentials?.token) {
331
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
332
- await this.openAPIRefreshStatus();
333
- }
334
- }
335
305
  async openAPIRefreshStatus() {
336
- if (this.platform.config.credentials?.token && this.OpenAPI) {
337
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} OpenAPI refreshStatus`);
338
- try {
339
- const t = Date.now();
340
- const nonce = 'requestID';
341
- const data = this.platform.config.credentials?.token + t + nonce;
342
- const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
343
- const sign = signTerm.toString('base64');
344
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
345
- const options = {
346
- hostname: settings_1.HostDomain,
347
- port: 443,
348
- path: `${settings_1.DevicePath}/${this.device.deviceId}/status`,
349
- method: 'GET',
350
- headers: {
351
- Authorization: this.platform.config.credentials?.token,
352
- sign: sign,
353
- nonce: nonce,
354
- t: t,
355
- 'Content-Type': 'application/json',
356
- },
357
- };
358
- const req = https_1.default.request(options, (res) => {
359
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
360
- let rawData = '';
361
- res.on('data', (d) => {
362
- rawData += d;
363
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
364
- });
365
- res.on('end', () => {
366
- try {
367
- this.deviceStatus = JSON.parse(rawData);
368
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus: ${(0, super_stringify_1.default)(this.deviceStatus)}`);
369
- this.auto = this.deviceStatus.body.auto;
370
- this.power = this.deviceStatus.body.power;
371
- this.lackWater = this.deviceStatus.body.lackWater;
372
- this.humidity = this.deviceStatus.body.humidity;
373
- this.temperature = this.deviceStatus.body.temperature;
374
- this.nebulizationEfficiency = this.deviceStatus.body.nebulizationEfficiency;
375
- this.parseStatus();
376
- this.updateHomeKitCharacteristics();
377
- }
378
- catch (e) {
379
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
380
- }
381
- });
306
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
307
+ try {
308
+ const t = Date.now();
309
+ const nonce = 'requestID';
310
+ const data = this.platform.config.credentials?.token + t + nonce;
311
+ const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
312
+ const sign = signTerm.toString('base64');
313
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
314
+ const options = {
315
+ hostname: settings_1.HostDomain,
316
+ port: 443,
317
+ path: `${settings_1.DevicePath}/${this.device.deviceId}/status`,
318
+ method: 'GET',
319
+ headers: {
320
+ Authorization: this.platform.config.credentials?.token,
321
+ sign: sign,
322
+ nonce: nonce,
323
+ t: t,
324
+ 'Content-Type': 'application/json',
325
+ },
326
+ };
327
+ const req = https_1.default.request(options, (res) => {
328
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus statusCode: ${res.statusCode}`);
329
+ let rawData = '';
330
+ res.on('data', (d) => {
331
+ rawData += d;
332
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
382
333
  });
383
- req.on('error', (e) => {
384
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
334
+ res.on('end', () => {
335
+ try {
336
+ this.deviceStatus = JSON.parse(rawData);
337
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus: ${(0, super_stringify_1.default)(this.deviceStatus)}`);
338
+ this.auto = this.deviceStatus.body.auto;
339
+ this.power = this.deviceStatus.body.power;
340
+ this.lackWater = this.deviceStatus.body.lackWater;
341
+ this.humidity = this.deviceStatus.body.humidity;
342
+ this.temperature = this.deviceStatus.body.temperature;
343
+ this.nebulizationEfficiency = this.deviceStatus.body.nebulizationEfficiency;
344
+ this.parseStatus();
345
+ this.updateHomeKitCharacteristics();
346
+ }
347
+ catch (e) {
348
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
349
+ }
385
350
  });
386
- req.end();
387
- }
388
- catch (e) {
389
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
390
- if (this.deviceLogging.includes('debug')) {
391
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,` +
392
- ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
393
- }
394
- this.apiError(e);
395
- }
351
+ });
352
+ req.on('error', (e) => {
353
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
354
+ });
355
+ req.end();
396
356
  }
397
- else {
398
- if (this.deviceLogging.includes('debug')) {
399
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} OpenAPI is disabled, device status will not be sent.`);
400
- }
357
+ catch (e) {
358
+ this.apiError(e);
359
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
360
+ + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
401
361
  }
402
362
  }
403
363
  /**
404
364
  * Pushes the requested changes to the SwitchBot API
405
365
  */
406
366
  async pushChanges() {
407
- //if (this.device.ble) {
408
- // await this.BLEpushChanges();
409
- //} else {
410
- await this.openAPIpushChanges();
411
- //}
367
+ /*if (this.BLE) {
368
+ await this.BLERefreshStatus();
369
+ } else*/ if (this.OpenAPI && this.platform.config.credentials?.token) {
370
+ await this.openAPIpushChanges();
371
+ }
372
+ else {
373
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
374
+ + ` ${this.device.connectionType}, pushChanges will not happen.`);
375
+ }
412
376
  (0, rxjs_1.interval)(5000)
413
377
  .pipe((0, operators_1.take)(1))
414
378
  .subscribe(async () => {
@@ -416,7 +380,7 @@ class Humidifier {
416
380
  });
417
381
  }
418
382
  async BLEpushChanges() {
419
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE pushChanges`);
383
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`);
420
384
  const switchbot = await this.platform.connectBLE();
421
385
  // Convert to BLE Address
422
386
  this.device.bleMac = this.device
@@ -424,40 +388,32 @@ class Humidifier {
424
388
  .join(':')
425
389
  .toLowerCase();
426
390
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
427
- if (switchbot !== false) {
428
- switchbot
429
- .discover({ model: 'e', quick: true, id: this.device.bleMac })
430
- .then((device_list) => {
431
- this.infoLog(`${this.accessory.displayName} Target Position: ${this.Active}`);
432
- return device_list[0].percentage(this.RelativeHumidityHumidifierThreshold);
433
- })
434
- .then(() => {
435
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
436
- })
437
- .catch(async (e) => {
438
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
439
- if (this.deviceLogging.includes('debug')) {
440
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
441
- + ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
442
- }
443
- if (this.platform.config.credentials?.token) {
444
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
445
- await this.openAPIpushChanges();
446
- }
447
- this.apiError(e);
448
- });
449
- }
450
- else {
451
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
452
- if (this.platform.config.credentials?.token) {
391
+ switchbot
392
+ .discover({
393
+ model: 'e',
394
+ quick: true,
395
+ id: this.device.bleMac,
396
+ })
397
+ .then((device_list) => {
398
+ this.infoLog(`${this.accessory.displayName} Target Position: ${this.Active}`);
399
+ return device_list[0].percentage(this.RelativeHumidityHumidifierThreshold);
400
+ })
401
+ .then(() => {
402
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
403
+ })
404
+ .catch(async (e) => {
405
+ this.apiError(e);
406
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
407
+ + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
408
+ if (this.platform.config.credentials?.token && this.device.connectionType !== 'Both') {
453
409
  this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
454
410
  await this.openAPIpushChanges();
455
411
  }
456
- }
412
+ });
457
413
  }
458
414
  async openAPIpushChanges() {
459
- if (this.platform.config.credentials?.token && this.OpenAPI) {
460
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} OpenAPI pushChanges`);
415
+ try {
416
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
461
417
  if (this.TargetHumidifierDehumidifierState === this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER &&
462
418
  this.Active === this.platform.Characteristic.Active.ACTIVE) {
463
419
  // Make Push On request to the API
@@ -491,7 +447,7 @@ class Humidifier {
491
447
  },
492
448
  };
493
449
  const req = https_1.default.request(options, res => {
494
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
450
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges statusCode: ${res.statusCode}`);
495
451
  res.on('data', d => {
496
452
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
497
453
  });
@@ -512,8 +468,10 @@ class Humidifier {
512
468
  await this.pushActiveChanges();
513
469
  }
514
470
  }
515
- else {
516
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} OpenAPI is disabled, commands will not be sent.`);
471
+ catch (e) {
472
+ this.apiError(e);
473
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
474
+ + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
517
475
  }
518
476
  }
519
477
  /**
@@ -521,6 +479,7 @@ class Humidifier {
521
479
  */
522
480
  async pushAutoChanges() {
523
481
  try {
482
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushAutoChanges`);
524
483
  if (this.TargetHumidifierDehumidifierState === this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER_OR_DEHUMIDIFIER &&
525
484
  this.Active === this.platform.Characteristic.Active.ACTIVE) {
526
485
  // Make Push On request to the API
@@ -567,14 +526,15 @@ class Humidifier {
567
526
  req.end();
568
527
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushAutoChanges: ${(0, super_stringify_1.default)(req)}`);
569
528
  }
529
+ else {
530
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushAutoChanges.` +
531
+ `TargetHumidifierDehumidifierState: ${this.TargetHumidifierDehumidifierState}, Active: ${this.Active}`);
532
+ }
570
533
  }
571
534
  catch (e) {
572
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushAutoChanges with OpenAPI Connection`);
573
- if (this.deviceLogging.includes('debug')) {
574
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushAutoChanges with OpenAPI Connection,` +
575
- ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
576
- }
577
535
  this.apiError(e);
536
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushAutoChanges with ${this.device.connectionType}`
537
+ + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
578
538
  }
579
539
  }
580
540
  /**
@@ -582,6 +542,7 @@ class Humidifier {
582
542
  */
583
543
  async pushActiveChanges() {
584
544
  try {
545
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushActiveChanges`);
585
546
  if (this.Active === this.platform.Characteristic.Active.INACTIVE) {
586
547
  // Make Push On request to the API
587
548
  const t = Date.now();
@@ -614,7 +575,7 @@ class Humidifier {
614
575
  },
615
576
  };
616
577
  const req = https_1.default.request(options, res => {
617
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
578
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushActiveChanges statusCode: ${res.statusCode}`);
618
579
  this.statusCode({ res });
619
580
  res.on('data', d => {
620
581
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
@@ -629,13 +590,38 @@ class Humidifier {
629
590
  }
630
591
  }
631
592
  catch (e) {
632
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushActiveChanges with OpenAPI Connection`);
633
- if (this.deviceLogging.includes('debug')) {
634
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushActiveChanges with OpenAPI Connection,` +
635
- ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
636
- }
637
593
  this.apiError(e);
594
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushActiveChanges with ${this.device.connectionType}`
595
+ + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
596
+ }
597
+ }
598
+ /**
599
+ * Handle requests to set the "Target Humidifier Dehumidifier State" characteristic
600
+ */
601
+ async TargetHumidifierDehumidifierStateSet(value) {
602
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set TargetHumidifierDehumidifierState: ${value}`);
603
+ this.TargetHumidifierDehumidifierState = value;
604
+ this.doHumidifierUpdate.next();
605
+ }
606
+ /**
607
+ * Handle requests to set the "Active" characteristic
608
+ */
609
+ async ActiveSet(value) {
610
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Active: ${value}`);
611
+ this.Active = value;
612
+ this.doHumidifierUpdate.next();
613
+ }
614
+ /**
615
+ * Handle requests to set the "Relative Humidity Humidifier Threshold" characteristic
616
+ */
617
+ async RelativeHumidityHumidifierThresholdSet(value) {
618
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set RelativeHumidityHumidifierThreshold: ${value}`);
619
+ this.RelativeHumidityHumidifierThreshold = value;
620
+ if (this.Active === this.platform.Characteristic.Active.INACTIVE) {
621
+ this.Active = this.platform.Characteristic.Active.ACTIVE;
622
+ this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.IDLE;
638
623
  }
624
+ this.doHumidifierUpdate.next();
639
625
  }
640
626
  /**
641
627
  * Updates the status for each of the HomeKit Characteristics
@@ -645,15 +631,17 @@ class Humidifier {
645
631
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
646
632
  }
647
633
  else {
634
+ this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidity;
648
635
  this.humidifierService.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.CurrentRelativeHumidity);
649
636
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
650
637
  + ` updateCharacteristic CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
651
638
  }
652
- if (!this.device.ble) {
639
+ if (this.OpenAPI) {
653
640
  if (this.WaterLevel === undefined) {
654
641
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} WaterLevel: ${this.WaterLevel}`);
655
642
  }
656
643
  else {
644
+ this.accessory.context.WaterLevel = this.WaterLevel;
657
645
  this.humidifierService.updateCharacteristic(this.platform.Characteristic.WaterLevel, this.WaterLevel);
658
646
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic WaterLevel: ${this.WaterLevel}`);
659
647
  }
@@ -663,6 +651,7 @@ class Humidifier {
663
651
  + ` CurrentHumidifierDehumidifierState: ${this.CurrentHumidifierDehumidifierState}`);
664
652
  }
665
653
  else {
654
+ this.accessory.context.CurrentHumidifierDehumidifierState = this.CurrentHumidifierDehumidifierState;
666
655
  this.humidifierService.updateCharacteristic(this.platform.Characteristic.CurrentHumidifierDehumidifierState, this.CurrentHumidifierDehumidifierState);
667
656
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}` +
668
657
  ` updateCharacteristic CurrentHumidifierDehumidifierState: ${this.CurrentHumidifierDehumidifierState}`);
@@ -672,6 +661,7 @@ class Humidifier {
672
661
  + ` TargetHumidifierDehumidifierState: ${this.TargetHumidifierDehumidifierState}`);
673
662
  }
674
663
  else {
664
+ this.accessory.context.TargetHumidifierDehumidifierState = this.TargetHumidifierDehumidifierState;
675
665
  this.humidifierService.updateCharacteristic(this.platform.Characteristic.TargetHumidifierDehumidifierState, this.TargetHumidifierDehumidifierState);
676
666
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}` +
677
667
  ` updateCharacteristic TargetHumidifierDehumidifierState: ${this.TargetHumidifierDehumidifierState}`);
@@ -680,6 +670,7 @@ class Humidifier {
680
670
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Active: ${this.Active}`);
681
671
  }
682
672
  else {
673
+ this.accessory.context.Active = this.Active;
683
674
  this.humidifierService.updateCharacteristic(this.platform.Characteristic.Active, this.Active);
684
675
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Active: ${this.Active}`);
685
676
  }
@@ -688,33 +679,69 @@ class Humidifier {
688
679
  + ` RelativeHumidityHumidifierThreshold: ${this.RelativeHumidityHumidifierThreshold}`);
689
680
  }
690
681
  else {
682
+ this.accessory.context.RelativeHumidityHumidifierThreshold = this.RelativeHumidityHumidifierThreshold;
691
683
  this.humidifierService.updateCharacteristic(this.platform.Characteristic.RelativeHumidityHumidifierThreshold, this.RelativeHumidityHumidifierThreshold);
692
684
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}` +
693
685
  ` updateCharacteristic RelativeHumidityHumidifierThreshold: ${this.RelativeHumidityHumidifierThreshold}`);
694
686
  }
695
- if (!this.device.humidifier?.hide_temperature && !this.device.ble) {
687
+ if (!this.device.humidifier?.hide_temperature && !this.BLE) {
696
688
  if (this.CurrentTemperature === undefined) {
697
689
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
698
690
  }
699
691
  else {
692
+ this.accessory.context.CurrentTemperature = this.CurrentTemperature;
700
693
  this.temperatureservice?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.CurrentTemperature);
701
694
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`);
702
695
  }
703
696
  }
704
697
  }
705
- async apiError(e) {
706
- this.humidifierService.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, e);
707
- if (!this.device.ble) {
708
- this.humidifierService.updateCharacteristic(this.platform.Characteristic.WaterLevel, e);
698
+ async getCustomBLEAddress(switchbot) {
699
+ if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
700
+ (async () => {
701
+ // Start to monitor advertisement packets
702
+ await switchbot.startScan({
703
+ model: 'e',
704
+ });
705
+ // Set an event handler
706
+ switchbot.onadvertisement = (ad) => {
707
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${(0, super_stringify_1.default)(ad, null, ' ')}`);
708
+ };
709
+ await switchbot.wait(10000);
710
+ // Stop to monitor
711
+ switchbot.stopScan();
712
+ })();
709
713
  }
710
- this.humidifierService.updateCharacteristic(this.platform.Characteristic.CurrentHumidifierDehumidifierState, e);
711
- this.humidifierService.updateCharacteristic(this.platform.Characteristic.TargetHumidifierDehumidifierState, e);
712
- this.humidifierService.updateCharacteristic(this.platform.Characteristic.Active, e);
713
- this.humidifierService.updateCharacteristic(this.platform.Characteristic.RelativeHumidityHumidifierThreshold, e);
714
- if (!this.device.humidifier?.hide_temperature && !this.device.ble) {
715
- this.temperatureservice?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, e);
714
+ }
715
+ async BLEconnection(switchbot) {
716
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
717
+ if (this.platform.config.credentials?.token && this.device.connectionType !== 'Both') {
718
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
719
+ this.SwitchToOpenAPI = true;
720
+ await this.openAPIRefreshStatus();
721
+ }
722
+ }
723
+ minStep() {
724
+ if (this.device.humidifier?.set_minStep) {
725
+ this.set_minStep = this.device.humidifier?.set_minStep;
726
+ }
727
+ else {
728
+ this.set_minStep = 1;
729
+ }
730
+ return this.set_minStep;
731
+ }
732
+ async scan(device) {
733
+ if (device.scanDuration) {
734
+ this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
735
+ if (this.BLE) {
736
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
737
+ }
738
+ }
739
+ else {
740
+ this.scanDuration = this.accessory.context.scanDuration = 1;
741
+ if (this.BLE) {
742
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
743
+ }
716
744
  }
717
- //throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
718
745
  }
719
746
  async statusCode({ res }) {
720
747
  switch (res.statusCode) {
@@ -750,61 +777,81 @@ class Humidifier {
750
777
  if (this.device.offline) {
751
778
  this.Active = this.platform.Characteristic.Active.INACTIVE;
752
779
  this.humidifierService.getCharacteristic(this.platform.Characteristic.Active).updateValue(this.Active);
780
+ await this.updateHomeKitCharacteristics();
753
781
  }
754
782
  }
755
- /**
756
- * Handle requests to set the "Target Humidifier Dehumidifier State" characteristic
757
- */
758
- async handleTargetHumidifierDehumidifierStateSet(value) {
759
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} TargetHumidifierDehumidifierState: ${value}`);
760
- this.TargetHumidifierDehumidifierState = value;
761
- this.doHumidifierUpdate.next();
783
+ async apiError(e) {
784
+ this.humidifierService.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, e);
785
+ if (!this.BLE) {
786
+ this.humidifierService.updateCharacteristic(this.platform.Characteristic.WaterLevel, e);
787
+ }
788
+ this.humidifierService.updateCharacteristic(this.platform.Characteristic.CurrentHumidifierDehumidifierState, e);
789
+ this.humidifierService.updateCharacteristic(this.platform.Characteristic.TargetHumidifierDehumidifierState, e);
790
+ this.humidifierService.updateCharacteristic(this.platform.Characteristic.Active, e);
791
+ this.humidifierService.updateCharacteristic(this.platform.Characteristic.RelativeHumidityHumidifierThreshold, e);
792
+ if (!this.device.humidifier?.hide_temperature && !this.BLE) {
793
+ this.temperatureservice?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, e);
794
+ }
762
795
  }
763
- /**
764
- * Handle requests to set the "Active" characteristic
765
- */
766
- async handleActiveSet(value) {
767
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Active: ${value}`);
768
- this.Active = value;
769
- this.doHumidifierUpdate.next();
796
+ FirmwareRevision(accessory, device) {
797
+ let FirmwareRevision;
798
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
799
+ + ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
800
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
801
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
802
+ if (accessory.context.FirmwareRevision) {
803
+ FirmwareRevision = accessory.context.FirmwareRevision;
804
+ }
805
+ else if (device.firmware) {
806
+ FirmwareRevision = device.firmware;
807
+ }
808
+ else {
809
+ FirmwareRevision = this.platform.version;
810
+ }
811
+ return FirmwareRevision;
770
812
  }
771
- /**
772
- * Handle requests to set the "Relative Humidity Humidifier Threshold" characteristic
773
- */
774
- async handleRelativeHumidityHumidifierThresholdSet(value) {
775
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} RelativeHumidityHumidifierThreshold: ${value}`);
776
- this.RelativeHumidityHumidifierThreshold = value;
777
- if (this.Active === this.platform.Characteristic.Active.INACTIVE) {
813
+ async context() {
814
+ if (this.Active === undefined) {
778
815
  this.Active = this.platform.Characteristic.Active.ACTIVE;
779
- this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.IDLE;
780
816
  }
781
- this.doHumidifierUpdate.next();
782
- }
783
- async config(device) {
784
- let config = {};
785
- if (device.humidifier) {
786
- config = device.humidifier;
817
+ else {
818
+ this.Active = this.accessory.context.Active;
787
819
  }
788
- if (device.ble !== undefined) {
789
- config['ble'] = device.ble;
820
+ if (this.CurrentTemperature === undefined) {
821
+ this.CurrentTemperature = 0;
790
822
  }
791
- if (device.openAPI !== undefined) {
792
- config['openAPI'] = device.openAPI;
823
+ else {
824
+ this.CurrentTemperature = this.accessory.context.CurrentTemperature;
793
825
  }
794
- if (device.logging !== undefined) {
795
- config['logging'] = device.logging;
826
+ if (this.CurrentRelativeHumidity === undefined) {
827
+ this.CurrentRelativeHumidity = 0;
796
828
  }
797
- if (device.refreshRate !== undefined) {
798
- config['refreshRate'] = device.refreshRate;
829
+ else {
830
+ this.CurrentRelativeHumidity = this.accessory.context.CurrentRelativeHumidity;
799
831
  }
800
- if (device.scanDuration !== undefined) {
801
- config['scanDuration'] = device.scanDuration;
832
+ if (this.TargetHumidifierDehumidifierState === undefined) {
833
+ this.TargetHumidifierDehumidifierState = this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER;
802
834
  }
803
- if (device.offline !== undefined) {
804
- config['offline'] = device.offline;
835
+ else {
836
+ this.TargetHumidifierDehumidifierState = this.accessory.context.TargetHumidifierDehumidifierState;
805
837
  }
806
- if (Object.entries(config).length !== 0) {
807
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${(0, super_stringify_1.default)(config)}`);
838
+ if (this.CurrentHumidifierDehumidifierState === undefined) {
839
+ this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.INACTIVE;
840
+ }
841
+ else {
842
+ this.CurrentHumidifierDehumidifierState = this.accessory.context.CurrentHumidifierDehumidifierState;
843
+ }
844
+ if (this.RelativeHumidityHumidifierThreshold === undefined) {
845
+ this.RelativeHumidityHumidifierThreshold = 0;
846
+ }
847
+ else {
848
+ this.RelativeHumidityHumidifierThreshold = this.accessory.context.RelativeHumidityHumidifierThreshold;
849
+ }
850
+ if (this.WaterLevel === undefined) {
851
+ this.WaterLevel = 0;
852
+ }
853
+ else {
854
+ this.WaterLevel = this.accessory.context.WaterLevel;
808
855
  }
809
856
  }
810
857
  async refreshRate(device) {
@@ -817,27 +864,28 @@ class Humidifier {
817
864
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
818
865
  }
819
866
  }
820
- async openAPI() {
821
- if (!this.device.openAPI) {
822
- this.OpenAPI = true;
867
+ async config(device) {
868
+ let config = {};
869
+ if (device.humidifier) {
870
+ config = device.humidifier;
823
871
  }
824
- else {
825
- this.OpenAPI = this.device.openAPI;
872
+ if (device.connectionType !== undefined) {
873
+ config['connectionType'] = device.connectionType;
826
874
  }
827
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI: ${this.OpenAPI}`);
828
- }
829
- async scan(device) {
830
- if (device.scanDuration) {
831
- this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
832
- if (device.ble) {
833
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
834
- }
875
+ if (device.logging !== undefined) {
876
+ config['logging'] = device.logging;
835
877
  }
836
- else {
837
- this.scanDuration = this.accessory.context.scanDuration = 1;
838
- if (this.device.ble) {
839
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
840
- }
878
+ if (device.refreshRate !== undefined) {
879
+ config['refreshRate'] = device.refreshRate;
880
+ }
881
+ if (device.scanDuration !== undefined) {
882
+ config['scanDuration'] = device.scanDuration;
883
+ }
884
+ if (device.offline !== undefined) {
885
+ config['offline'] = device.offline;
886
+ }
887
+ if (Object.entries(config).length !== 0) {
888
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${(0, super_stringify_1.default)(config)}`);
841
889
  }
842
890
  }
843
891
  async logs(device) {
@@ -858,32 +906,6 @@ class Humidifier {
858
906
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
859
907
  }
860
908
  }
861
- FirmwareRevision(accessory, device) {
862
- let FirmwareRevision;
863
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
864
- + ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
865
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
866
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
867
- if (accessory.context.FirmwareRevision) {
868
- FirmwareRevision = accessory.context.FirmwareRevision;
869
- }
870
- else if (device.firmware) {
871
- FirmwareRevision = device.firmware;
872
- }
873
- else {
874
- FirmwareRevision = this.platform.version;
875
- }
876
- return FirmwareRevision;
877
- }
878
- minStep() {
879
- if (this.device.humidifier?.set_minStep) {
880
- this.set_minStep = this.device.humidifier?.set_minStep;
881
- }
882
- else {
883
- this.set_minStep = 1;
884
- }
885
- return this.set_minStep;
886
- }
887
909
  /**
888
910
  * Logging for Device
889
911
  */