@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
@@ -41,6 +41,7 @@ export class Humidifier {
41
41
  connected?: boolean;
42
42
  serviceData!: serviceData;
43
43
  address!: ad['address'];
44
+ SwitchToOpenAPI?: boolean;
44
45
  onState!: serviceData['onState'];
45
46
  autoMode!: serviceData['autoMode'];
46
47
  percentage!: serviceData['percentage'];
@@ -50,26 +51,22 @@ export class Humidifier {
50
51
  scanDuration!: number;
51
52
  deviceLogging!: string;
52
53
  deviceRefreshRate!: number;
53
- OpenAPI?: boolean;
54
54
 
55
55
  // Updates
56
56
  humidifierUpdateInProgress!: boolean;
57
57
  doHumidifierUpdate!: Subject<void>;
58
58
 
59
+ // Connection
60
+ private readonly BLE = (this.device.connectionType === 'BLE' || this.device.connectionType === 'Both');
61
+ private readonly OpenAPI = (this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'Both');
62
+
59
63
  constructor(private readonly platform: SwitchBotPlatform, private accessory: PlatformAccessory, public device: device & devicesConfig) {
60
64
  // default placeholders
61
65
  this.logs(device);
62
- this.openAPI();
63
66
  this.scan(device);
64
67
  this.refreshRate(device);
65
68
  this.config(device);
66
- this.CurrentRelativeHumidity = 0;
67
- this.TargetHumidifierDehumidifierState = this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER;
68
- this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.INACTIVE;
69
- this.Active = this.platform.Characteristic.Active.ACTIVE;
70
- this.RelativeHumidityHumidifierThreshold = 0;
71
- this.CurrentTemperature = 0;
72
- this.WaterLevel = 0;
69
+ this.context();
73
70
 
74
71
  // this is subject we use to track when we need to POST changes to the SwitchBot API
75
72
  this.doHumidifierUpdate = new Subject();
@@ -119,9 +116,9 @@ export class Humidifier {
119
116
  maxValue: 1,
120
117
  validValues: [0, 1],
121
118
  })
122
- .onSet(this.handleTargetHumidifierDehumidifierStateSet.bind(this));
119
+ .onSet(this.TargetHumidifierDehumidifierStateSet.bind(this));
123
120
 
124
- this.humidifierService.getCharacteristic(this.platform.Characteristic.Active).onSet(this.handleActiveSet.bind(this));
121
+ this.humidifierService.getCharacteristic(this.platform.Characteristic.Active).onSet(this.ActiveSet.bind(this));
125
122
 
126
123
  this.humidifierService
127
124
  .getCharacteristic(this.platform.Characteristic.RelativeHumidityHumidifierThreshold)
@@ -131,14 +128,14 @@ export class Humidifier {
131
128
  maxValue: 100,
132
129
  minStep: this.minStep(),
133
130
  })
134
- .onSet(this.handleRelativeHumidityHumidifierThresholdSet.bind(this));
131
+ .onSet(this.RelativeHumidityHumidifierThresholdSet.bind(this));
135
132
 
136
133
  // Temperature Sensor Service
137
- if (device.humidifier?.hide_temperature || device.ble) {
134
+ if (device.humidifier?.hide_temperature || this.BLE) {
138
135
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Temperature Sensor Service`);
139
136
  this.temperatureservice = this.accessory.getService(this.platform.Service.TemperatureSensor);
140
137
  accessory.removeService(this.temperatureservice!);
141
- } else if (!this.temperatureservice && !device.ble) {
138
+ } else if (!this.temperatureservice && !this.BLE) {
142
139
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Temperature Sensor Service`);
143
140
  (this.temperatureservice =
144
141
  this.accessory.getService(this.platform.Service.TemperatureSensor) || this.accessory.addService(this.platform.Service.TemperatureSensor)),
@@ -184,12 +181,9 @@ export class Humidifier {
184
181
  try {
185
182
  await this.pushChanges();
186
183
  } catch (e: any) {
187
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges`);
188
- if (this.deviceLogging.includes('debug')) {
189
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges,`
190
- + ` Error Message: ${superStringify(e.message)}`);
191
- }
192
184
  this.apiError(e);
185
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with ${this.device.connectionType} Connection,`
186
+ + ` Error Message: ${superStringify(e.message)}`);
193
187
  }
194
188
  this.humidifierUpdateInProgress = false;
195
189
  });
@@ -199,15 +193,19 @@ export class Humidifier {
199
193
  * Parse the device status from the SwitchBot api
200
194
  */
201
195
  async parseStatus(): Promise<void> {
202
- if (this.device.ble) {
196
+ if (this.BLE && !this.SwitchToOpenAPI) {
203
197
  await this.BLEparseStatus();
204
- } else {
198
+ } else if (this.OpenAPI && this.platform.config.credentials?.token) {
199
+ this.SwitchToOpenAPI = false;
205
200
  await this.openAPIparseStatus();
201
+ } else {
202
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
203
+ + ` ${this.device.connectionType}, parseStatus will not happen.`);
206
204
  }
207
205
  }
208
206
 
209
207
  async BLEparseStatus(): Promise<void> {
210
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE parseStatus`);
208
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
211
209
  // Current Relative Humidity
212
210
  this.CurrentRelativeHumidity = this.percentage!;
213
211
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
@@ -221,78 +219,80 @@ export class Humidifier {
221
219
  }
222
220
 
223
221
  async openAPIparseStatus(): Promise<void> {
224
- if (this.platform.config.credentials?.token) {
225
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} OpenAPI parseStatus`);
226
- // Current Relative Humidity
227
- this.CurrentRelativeHumidity = this.humidity!;
228
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
229
- // Current Temperature
230
- if (!this.device.humidifier?.hide_temperature) {
231
- this.CurrentTemperature = this.temperature!;
232
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
233
- }
234
- // Target Humidifier Dehumidifier State
235
- switch (this.auto) {
236
- case true:
237
- this.TargetHumidifierDehumidifierState = this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER_OR_DEHUMIDIFIER;
222
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
223
+ // Current Relative Humidity
224
+ this.CurrentRelativeHumidity = this.humidity!;
225
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
226
+ // Current Temperature
227
+ if (!this.device.humidifier?.hide_temperature) {
228
+ this.CurrentTemperature = this.temperature!;
229
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
230
+ }
231
+ // Target Humidifier Dehumidifier State
232
+ switch (this.auto) {
233
+ case true:
234
+ this.TargetHumidifierDehumidifierState = this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER_OR_DEHUMIDIFIER;
235
+ this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.HUMIDIFYING;
236
+ this.RelativeHumidityHumidifierThreshold = this.CurrentRelativeHumidity;
237
+ break;
238
+ default:
239
+ this.TargetHumidifierDehumidifierState = this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER;
240
+ if (this.nebulizationEfficiency! > 100) {
241
+ this.RelativeHumidityHumidifierThreshold = 100;
242
+ } else {
243
+ this.RelativeHumidityHumidifierThreshold = this.nebulizationEfficiency!;
244
+ }
245
+ if (this.CurrentRelativeHumidity > this.RelativeHumidityHumidifierThreshold) {
246
+ this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.IDLE;
247
+ } else if (this.Active === this.platform.Characteristic.Active.INACTIVE) {
248
+ this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.INACTIVE;
249
+ } else {
238
250
  this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.HUMIDIFYING;
239
- this.RelativeHumidityHumidifierThreshold = this.CurrentRelativeHumidity;
240
- break;
241
- default:
242
- this.TargetHumidifierDehumidifierState = this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER;
243
- if (this.nebulizationEfficiency! > 100) {
244
- this.RelativeHumidityHumidifierThreshold = 100;
245
- } else {
246
- this.RelativeHumidityHumidifierThreshold = this.nebulizationEfficiency!;
247
- }
248
- if (this.CurrentRelativeHumidity > this.RelativeHumidityHumidifierThreshold) {
249
- this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.IDLE;
250
- } else if (this.Active === this.platform.Characteristic.Active.INACTIVE) {
251
- this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.INACTIVE;
252
- } else {
253
- this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.HUMIDIFYING;
254
- }
255
- }
256
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
251
+ }
252
+ }
253
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
257
254
  + ` TargetHumidifierDehumidifierState: ${this.TargetHumidifierDehumidifierState}`);
258
- this.debugLog(
259
- `${this.device.deviceType}: ${this.accessory.displayName}`
255
+ this.debugLog(
256
+ `${this.device.deviceType}: ${this.accessory.displayName}`
260
257
  + ` RelativeHumidityHumidifierThreshold: ${this.RelativeHumidityHumidifierThreshold}`,
261
- );
262
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
258
+ );
259
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
263
260
  + ` CurrentHumidifierDehumidifierState: ${this.CurrentHumidifierDehumidifierState}`);
264
- // Active
265
- switch (this.power) {
266
- case 'on':
267
- this.Active = this.platform.Characteristic.Active.ACTIVE;
268
- break;
269
- default:
270
- this.Active = this.platform.Characteristic.Active.INACTIVE;
271
- }
272
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Active: ${this.Active}`);
273
- // Water Level
274
- if (this.lackWater) {
275
- this.WaterLevel = 0;
276
- } else {
277
- this.WaterLevel = 100;
278
- }
279
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} WaterLevel: ${this.WaterLevel}`);
261
+ // Active
262
+ switch (this.power) {
263
+ case 'on':
264
+ this.Active = this.platform.Characteristic.Active.ACTIVE;
265
+ break;
266
+ default:
267
+ this.Active = this.platform.Characteristic.Active.INACTIVE;
268
+ }
269
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Active: ${this.Active}`);
270
+ // Water Level
271
+ if (this.lackWater) {
272
+ this.WaterLevel = 0;
273
+ } else {
274
+ this.WaterLevel = 100;
280
275
  }
276
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} WaterLevel: ${this.WaterLevel}`);
277
+
281
278
  }
282
279
 
283
280
  /**
284
281
  * Asks the SwitchBot API for the latest device information
285
282
  */
286
283
  async refreshStatus(): Promise<void> {
287
- if (this.device.ble) {
284
+ if (this.BLE) {
288
285
  await this.BLERefreshStatus();
289
- } else {
286
+ } else if (this.OpenAPI && this.platform.config.credentials?.token) {
290
287
  await this.openAPIRefreshStatus();
288
+ } else {
289
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
290
+ + ` ${this.device.connectionType}, refreshStatus will not happen.`);
291
291
  }
292
292
  }
293
293
 
294
294
  async BLERefreshStatus(): Promise<void> {
295
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE refreshStatus`);
295
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
296
296
  const switchbot = await this.platform.connectBLE();
297
297
  // Convert to BLE Address
298
298
  this.device.bleMac = this.device
@@ -350,115 +350,70 @@ export class Humidifier {
350
350
  }
351
351
  })
352
352
  .catch(async (e: any) => {
353
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
354
- if (this.deviceLogging.includes('debug')) {
355
- this.errorLog(
356
- `${this.device.deviceType}: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
357
- + ` Error Message: ${superStringify(e.message)}`,
358
- );
359
- }
360
- if (this.platform.config.credentials?.token) {
361
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
362
- await this.openAPIRefreshStatus();
363
- }
364
353
  this.apiError(e);
354
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}`
355
+ + ` Connection, Error Message: ${superStringify(e.message)}`);
356
+ await this.BLEconnection(switchbot);
365
357
  });
366
358
  } else {
367
359
  await this.BLEconnection(switchbot);
368
360
  }
369
361
  }
370
362
 
371
- async getCustomBLEAddress(switchbot: any) {
372
- if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
373
- (async () => {
374
- // Start to monitor advertisement packets
375
- await switchbot.startScan({
376
- model: 'e',
377
- });
378
- // Set an event handler
379
- switchbot.onadvertisement = (ad: any) => {
380
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${superStringify(ad, null, ' ')}`);
381
- };
382
- await switchbot.wait(10000);
383
- // Stop to monitor
384
- switchbot.stopScan();
385
- })();
386
- }
387
- }
388
-
389
- async BLEconnection(switchbot: any): Promise<void> {
390
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
391
- if (this.platform.config.credentials?.token) {
392
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
393
- await this.openAPIRefreshStatus();
394
- }
395
- }
396
-
397
363
  async openAPIRefreshStatus(): Promise<void> {
398
- if (this.platform.config.credentials?.token && this.OpenAPI) {
399
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} OpenAPI refreshStatus`);
400
- try {
401
- const t = Date.now();
402
- const nonce = 'requestID';
403
- const data = this.platform.config.credentials?.token + t + nonce;
404
- const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
405
- const sign = signTerm.toString('base64');
406
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
407
- const options = {
408
- hostname: HostDomain,
409
- port: 443,
410
- path: `${DevicePath}/${this.device.deviceId}/status`,
411
- method: 'GET',
412
- headers: {
413
- Authorization: this.platform.config.credentials?.token,
414
- sign: sign,
415
- nonce: nonce,
416
- t: t,
417
- 'Content-Type': 'application/json',
418
- },
419
- };
420
- const req = https.request(options, (res) => {
421
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
422
- let rawData = '';
423
- res.on('data', (d) => {
424
- rawData += d;
425
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
426
- });
427
- res.on('end', () => {
428
- try {
429
- this.deviceStatus = JSON.parse(rawData);
430
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus: ${superStringify(this.deviceStatus)}`);
431
- this.auto = this.deviceStatus.body.auto;
432
- this.power = this.deviceStatus.body.power;
433
- this.lackWater = this.deviceStatus.body.lackWater;
434
- this.humidity = this.deviceStatus.body.humidity;
435
- this.temperature = this.deviceStatus.body.temperature;
436
- this.nebulizationEfficiency = this.deviceStatus.body.nebulizationEfficiency;
437
- this.parseStatus();
438
- this.updateHomeKitCharacteristics();
439
- } catch (e: any) {
440
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
441
- }
442
- });
364
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
365
+ try {
366
+ const t = Date.now();
367
+ const nonce = 'requestID';
368
+ const data = this.platform.config.credentials?.token + t + nonce;
369
+ const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
370
+ const sign = signTerm.toString('base64');
371
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
372
+ const options = {
373
+ hostname: HostDomain,
374
+ port: 443,
375
+ path: `${DevicePath}/${this.device.deviceId}/status`,
376
+ method: 'GET',
377
+ headers: {
378
+ Authorization: this.platform.config.credentials?.token,
379
+ sign: sign,
380
+ nonce: nonce,
381
+ t: t,
382
+ 'Content-Type': 'application/json',
383
+ },
384
+ };
385
+ const req = https.request(options, (res) => {
386
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus statusCode: ${res.statusCode}`);
387
+ let rawData = '';
388
+ res.on('data', (d) => {
389
+ rawData += d;
390
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
443
391
  });
444
- req.on('error', (e: any) => {
445
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
392
+ res.on('end', () => {
393
+ try {
394
+ this.deviceStatus = JSON.parse(rawData);
395
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus: ${superStringify(this.deviceStatus)}`);
396
+ this.auto = this.deviceStatus.body.auto;
397
+ this.power = this.deviceStatus.body.power;
398
+ this.lackWater = this.deviceStatus.body.lackWater;
399
+ this.humidity = this.deviceStatus.body.humidity;
400
+ this.temperature = this.deviceStatus.body.temperature;
401
+ this.nebulizationEfficiency = this.deviceStatus.body.nebulizationEfficiency;
402
+ this.parseStatus();
403
+ this.updateHomeKitCharacteristics();
404
+ } catch (e: any) {
405
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
406
+ }
446
407
  });
447
- req.end();
448
- } catch (e: any) {
449
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
450
- if (this.deviceLogging.includes('debug')) {
451
- this.errorLog(
452
- `${this.device.deviceType}: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,` +
453
- ` Error Message: ${superStringify(e.message)}`,
454
- );
455
- }
456
- this.apiError(e);
457
- }
458
- } else {
459
- if (this.deviceLogging.includes('debug')) {
460
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} OpenAPI is disabled, device status will not be sent.`);
461
- }
408
+ });
409
+ req.on('error', (e: any) => {
410
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
411
+ });
412
+ req.end();
413
+ } catch (e: any) {
414
+ this.apiError(e);
415
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
416
+ + ` Connection, Error Message: ${superStringify(e.message)}`);
462
417
  }
463
418
  }
464
419
 
@@ -466,11 +421,14 @@ export class Humidifier {
466
421
  * Pushes the requested changes to the SwitchBot API
467
422
  */
468
423
  async pushChanges(): Promise<void> {
469
- //if (this.device.ble) {
470
- // await this.BLEpushChanges();
471
- //} else {
472
- await this.openAPIpushChanges();
473
- //}
424
+ /*if (this.BLE) {
425
+ await this.BLERefreshStatus();
426
+ } else*/ if (this.OpenAPI && this.platform.config.credentials?.token) {
427
+ await this.openAPIpushChanges();
428
+ } else {
429
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
430
+ + ` ${this.device.connectionType}, pushChanges will not happen.`);
431
+ }
474
432
  interval(5000)
475
433
  .pipe(take(1))
476
434
  .subscribe(async () => {
@@ -479,7 +437,7 @@ export class Humidifier {
479
437
  }
480
438
 
481
439
  async BLEpushChanges(): Promise<void> {
482
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE pushChanges`);
440
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`);
483
441
  const switchbot = await this.platform.connectBLE();
484
442
  // Convert to BLE Address
485
443
  this.device.bleMac = this.device
@@ -487,47 +445,36 @@ export class Humidifier {
487
445
  .join(':')
488
446
  .toLowerCase();
489
447
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
490
- if (switchbot !== false) {
491
- switchbot
492
- .discover({ model: 'e', quick: true, id: this.device.bleMac })
493
- .then((device_list) => {
494
- this.infoLog(`${this.accessory.displayName} Target Position: ${this.Active}`);
495
- return device_list[0].percentage(this.RelativeHumidityHumidifierThreshold);
496
- })
497
- .then(() => {
498
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
499
- })
500
- .catch(async (e: any) => {
501
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
502
- if (this.deviceLogging.includes('debug')) {
503
- this.errorLog(
504
- `${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
505
- + ` Error Message: ${superStringify(e.message)}`,
506
- );
507
- }
508
- if (this.platform.config.credentials?.token) {
509
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
510
- await this.openAPIpushChanges();
511
- }
512
- this.apiError(e);
513
- });
514
- } else {
515
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
516
- if (this.platform.config.credentials?.token) {
517
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
518
- await this.openAPIpushChanges();
519
- }
520
- }
448
+ switchbot
449
+ .discover({
450
+ model: 'e',
451
+ quick: true,
452
+ id: this.device.bleMac,
453
+ })
454
+ .then((device_list: any) => {
455
+ this.infoLog(`${this.accessory.displayName} Target Position: ${this.Active}`);
456
+ return device_list[0].percentage(this.RelativeHumidityHumidifierThreshold);
457
+ })
458
+ .then(() => {
459
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
460
+ })
461
+ .catch(async (e: any) => {
462
+ this.apiError(e);
463
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
464
+ + ` Connection, Error Message: ${superStringify(e.message)}`);
465
+ if (this.platform.config.credentials?.token && this.device.connectionType !== 'Both') {
466
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
467
+ await this.openAPIpushChanges();
468
+ }
469
+ });
521
470
  }
522
471
 
523
472
  async openAPIpushChanges(): Promise<void> {
524
- if (this.platform.config.credentials?.token && this.OpenAPI) {
525
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} OpenAPI pushChanges`);
526
- if (
527
- this.TargetHumidifierDehumidifierState === this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER &&
528
- this.Active === this.platform.Characteristic.Active.ACTIVE
529
- ) {
530
- // Make Push On request to the API
473
+ try {
474
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
475
+ if (this.TargetHumidifierDehumidifierState === this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER &&
476
+ this.Active === this.platform.Characteristic.Active.ACTIVE) {
477
+ // Make Push On request to the API
531
478
  const t = Date.now();
532
479
  const nonce = 'requestID';
533
480
  const data = this.platform.config.credentials?.token + t + nonce;
@@ -558,7 +505,7 @@ export class Humidifier {
558
505
  },
559
506
  };
560
507
  const req = https.request(options, res => {
561
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
508
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges statusCode: ${res.statusCode}`);
562
509
  res.on('data', d => {
563
510
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
564
511
  });
@@ -578,8 +525,10 @@ export class Humidifier {
578
525
  } else {
579
526
  await this.pushActiveChanges();
580
527
  }
581
- } else {
582
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} OpenAPI is disabled, commands will not be sent.`);
528
+ } catch (e: any) {
529
+ this.apiError(e);
530
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
531
+ + ` Connection, Error Message: ${superStringify(e.message)}`);
583
532
  }
584
533
  }
585
534
 
@@ -588,6 +537,7 @@ export class Humidifier {
588
537
  */
589
538
  async pushAutoChanges(): Promise<void> {
590
539
  try {
540
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushAutoChanges`);
591
541
  if (
592
542
  this.TargetHumidifierDehumidifierState === this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER_OR_DEHUMIDIFIER &&
593
543
  this.Active === this.platform.Characteristic.Active.ACTIVE
@@ -635,16 +585,14 @@ export class Humidifier {
635
585
  req.write(body);
636
586
  req.end();
637
587
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushAutoChanges: ${superStringify(req)}`);
588
+ } else {
589
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushAutoChanges.` +
590
+ `TargetHumidifierDehumidifierState: ${this.TargetHumidifierDehumidifierState}, Active: ${this.Active}`);
638
591
  }
639
592
  } catch (e: any) {
640
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushAutoChanges with OpenAPI Connection`);
641
- if (this.deviceLogging.includes('debug')) {
642
- this.errorLog(
643
- `${this.device.deviceType}: ${this.accessory.displayName} failed pushAutoChanges with OpenAPI Connection,` +
644
- ` Error Message: ${superStringify(e.message)}`,
645
- );
646
- }
647
593
  this.apiError(e);
594
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushAutoChanges with ${this.device.connectionType}`
595
+ + ` Connection, Error Message: ${superStringify(e.message)}`);
648
596
  }
649
597
  }
650
598
 
@@ -653,6 +601,7 @@ export class Humidifier {
653
601
  */
654
602
  async pushActiveChanges(): Promise<void> {
655
603
  try {
604
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushActiveChanges`);
656
605
  if (this.Active === this.platform.Characteristic.Active.INACTIVE) {
657
606
  // Make Push On request to the API
658
607
  const t = Date.now();
@@ -685,7 +634,7 @@ export class Humidifier {
685
634
  },
686
635
  };
687
636
  const req = https.request(options, res => {
688
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
637
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushActiveChanges statusCode: ${res.statusCode}`);
689
638
  this.statusCode({ res });
690
639
  res.on('data', d => {
691
640
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
@@ -699,17 +648,47 @@ export class Humidifier {
699
648
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushActiveChanges: ${superStringify(req)}`);
700
649
  }
701
650
  } catch (e: any) {
702
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushActiveChanges with OpenAPI Connection`);
703
- if (this.deviceLogging.includes('debug')) {
704
- this.errorLog(
705
- `${this.device.deviceType}: ${this.accessory.displayName} failed pushActiveChanges with OpenAPI Connection,` +
706
- ` Error Message: ${superStringify(e.message)}`,
707
- );
708
- }
709
651
  this.apiError(e);
652
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushActiveChanges with ${this.device.connectionType}`
653
+ + ` Connection, Error Message: ${superStringify(e.message)}`,
654
+ );
710
655
  }
711
656
  }
712
657
 
658
+ /**
659
+ * Handle requests to set the "Target Humidifier Dehumidifier State" characteristic
660
+ */
661
+ async TargetHumidifierDehumidifierStateSet(value: CharacteristicValue): Promise<void> {
662
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set TargetHumidifierDehumidifierState: ${value}`);
663
+
664
+ this.TargetHumidifierDehumidifierState = value;
665
+ this.doHumidifierUpdate.next();
666
+ }
667
+
668
+ /**
669
+ * Handle requests to set the "Active" characteristic
670
+ */
671
+ async ActiveSet(value: CharacteristicValue): Promise<void> {
672
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Active: ${value}`);
673
+
674
+ this.Active = value;
675
+ this.doHumidifierUpdate.next();
676
+ }
677
+
678
+ /**
679
+ * Handle requests to set the "Relative Humidity Humidifier Threshold" characteristic
680
+ */
681
+ async RelativeHumidityHumidifierThresholdSet(value: CharacteristicValue): Promise<void> {
682
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set RelativeHumidityHumidifierThreshold: ${value}`);
683
+
684
+ this.RelativeHumidityHumidifierThreshold = value;
685
+ if (this.Active === this.platform.Characteristic.Active.INACTIVE) {
686
+ this.Active = this.platform.Characteristic.Active.ACTIVE;
687
+ this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.IDLE;
688
+ }
689
+ this.doHumidifierUpdate.next();
690
+ }
691
+
713
692
  /**
714
693
  * Updates the status for each of the HomeKit Characteristics
715
694
  */
@@ -717,14 +696,16 @@ export class Humidifier {
717
696
  if (this.CurrentRelativeHumidity === undefined) {
718
697
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
719
698
  } else {
699
+ this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidity;
720
700
  this.humidifierService.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.CurrentRelativeHumidity);
721
701
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
722
702
  + ` updateCharacteristic CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
723
703
  }
724
- if (!this.device.ble) {
704
+ if (this.OpenAPI) {
725
705
  if (this.WaterLevel === undefined) {
726
706
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} WaterLevel: ${this.WaterLevel}`);
727
707
  } else {
708
+ this.accessory.context.WaterLevel = this.WaterLevel;
728
709
  this.humidifierService.updateCharacteristic(this.platform.Characteristic.WaterLevel, this.WaterLevel);
729
710
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic WaterLevel: ${this.WaterLevel}`);
730
711
  }
@@ -733,72 +714,99 @@ export class Humidifier {
733
714
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
734
715
  + ` CurrentHumidifierDehumidifierState: ${this.CurrentHumidifierDehumidifierState}`);
735
716
  } else {
736
- this.humidifierService.updateCharacteristic(
737
- this.platform.Characteristic.CurrentHumidifierDehumidifierState,
738
- this.CurrentHumidifierDehumidifierState,
739
- );
740
- this.debugLog(
741
- `${this.device.deviceType}: ${this.accessory.displayName}` +
742
- ` updateCharacteristic CurrentHumidifierDehumidifierState: ${this.CurrentHumidifierDehumidifierState}`,
743
- );
717
+ this.accessory.context.CurrentHumidifierDehumidifierState = this.CurrentHumidifierDehumidifierState;
718
+ this.humidifierService.updateCharacteristic(this.platform.Characteristic.CurrentHumidifierDehumidifierState,
719
+ this.CurrentHumidifierDehumidifierState);
720
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}` +
721
+ ` updateCharacteristic CurrentHumidifierDehumidifierState: ${this.CurrentHumidifierDehumidifierState}`);
744
722
  }
745
723
  if (this.TargetHumidifierDehumidifierState === undefined) {
746
724
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
747
725
  +` TargetHumidifierDehumidifierState: ${this.TargetHumidifierDehumidifierState}`);
748
726
  } else {
749
- this.humidifierService.updateCharacteristic(
750
- this.platform.Characteristic.TargetHumidifierDehumidifierState,
751
- this.TargetHumidifierDehumidifierState,
752
- );
753
- this.debugLog(
754
- `${this.device.deviceType}: ${this.accessory.displayName}` +
727
+ this.accessory.context.TargetHumidifierDehumidifierState = this.TargetHumidifierDehumidifierState;
728
+ this.humidifierService.updateCharacteristic(this.platform.Characteristic.TargetHumidifierDehumidifierState,
729
+ this.TargetHumidifierDehumidifierState);
730
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}` +
755
731
  ` updateCharacteristic TargetHumidifierDehumidifierState: ${this.TargetHumidifierDehumidifierState}`,
756
732
  );
757
733
  }
758
734
  if (this.Active === undefined) {
759
735
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Active: ${this.Active}`);
760
736
  } else {
737
+ this.accessory.context.Active = this.Active;
761
738
  this.humidifierService.updateCharacteristic(this.platform.Characteristic.Active, this.Active);
762
739
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Active: ${this.Active}`);
763
740
  }
764
741
  if (this.RelativeHumidityHumidifierThreshold === undefined) {
765
- this.debugLog(
766
- `${this.device.deviceType}: ${this.accessory.displayName}`
767
- + ` RelativeHumidityHumidifierThreshold: ${this.RelativeHumidityHumidifierThreshold}`,
768
- );
742
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
743
+ + ` RelativeHumidityHumidifierThreshold: ${this.RelativeHumidityHumidifierThreshold}`);
769
744
  } else {
770
- this.humidifierService.updateCharacteristic(
771
- this.platform.Characteristic.RelativeHumidityHumidifierThreshold,
772
- this.RelativeHumidityHumidifierThreshold,
773
- );
774
- this.debugLog(
775
- `${this.device.deviceType}: ${this.accessory.displayName}` +
776
- ` updateCharacteristic RelativeHumidityHumidifierThreshold: ${this.RelativeHumidityHumidifierThreshold}`,
777
- );
745
+ this.accessory.context.RelativeHumidityHumidifierThreshold = this.RelativeHumidityHumidifierThreshold;
746
+ this.humidifierService.updateCharacteristic(this.platform.Characteristic.RelativeHumidityHumidifierThreshold,
747
+ this.RelativeHumidityHumidifierThreshold);
748
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}` +
749
+ ` updateCharacteristic RelativeHumidityHumidifierThreshold: ${this.RelativeHumidityHumidifierThreshold}`);
778
750
  }
779
- if (!this.device.humidifier?.hide_temperature && !this.device.ble) {
751
+ if (!this.device.humidifier?.hide_temperature && !this.BLE) {
780
752
  if (this.CurrentTemperature === undefined) {
781
753
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
782
754
  } else {
755
+ this.accessory.context.CurrentTemperature = this.CurrentTemperature;
783
756
  this.temperatureservice?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.CurrentTemperature);
784
757
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`);
785
758
  }
786
759
  }
787
760
  }
788
761
 
789
- async apiError(e: any): Promise<void> {
790
- this.humidifierService.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, e);
791
- if (!this.device.ble) {
792
- this.humidifierService.updateCharacteristic(this.platform.Characteristic.WaterLevel, e);
762
+ async getCustomBLEAddress(switchbot: any) {
763
+ if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
764
+ (async () => {
765
+ // Start to monitor advertisement packets
766
+ await switchbot.startScan({
767
+ model: 'e',
768
+ });
769
+ // Set an event handler
770
+ switchbot.onadvertisement = (ad: any) => {
771
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${superStringify(ad, null, ' ')}`);
772
+ };
773
+ await switchbot.wait(10000);
774
+ // Stop to monitor
775
+ switchbot.stopScan();
776
+ })();
793
777
  }
794
- this.humidifierService.updateCharacteristic(this.platform.Characteristic.CurrentHumidifierDehumidifierState, e);
795
- this.humidifierService.updateCharacteristic(this.platform.Characteristic.TargetHumidifierDehumidifierState, e);
796
- this.humidifierService.updateCharacteristic(this.platform.Characteristic.Active, e);
797
- this.humidifierService.updateCharacteristic(this.platform.Characteristic.RelativeHumidityHumidifierThreshold, e);
798
- if (!this.device.humidifier?.hide_temperature && !this.device.ble) {
799
- this.temperatureservice?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, e);
778
+ }
779
+
780
+ async BLEconnection(switchbot: any): Promise<void> {
781
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
782
+ if (this.platform.config.credentials?.token && this.device.connectionType !== 'Both') {
783
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
784
+ this.SwitchToOpenAPI = true;
785
+ await this.openAPIRefreshStatus();
786
+ }
787
+ }
788
+
789
+ minStep(): number {
790
+ if (this.device.humidifier?.set_minStep) {
791
+ this.set_minStep = this.device.humidifier?.set_minStep;
792
+ } else {
793
+ this.set_minStep = 1;
794
+ }
795
+ return this.set_minStep;
796
+ }
797
+
798
+ async scan(device: device & devicesConfig): Promise<void> {
799
+ if (device.scanDuration) {
800
+ this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
801
+ if (this.BLE) {
802
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
803
+ }
804
+ } else {
805
+ this.scanDuration = this.accessory.context.scanDuration = 1;
806
+ if (this.BLE) {
807
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
808
+ }
800
809
  }
801
- //throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
802
810
  }
803
811
 
804
812
  async statusCode({ res }: { res: IncomingMessage }): Promise<void> {
@@ -838,40 +846,86 @@ export class Humidifier {
838
846
  if (this.device.offline) {
839
847
  this.Active = this.platform.Characteristic.Active.INACTIVE;
840
848
  this.humidifierService.getCharacteristic(this.platform.Characteristic.Active).updateValue(this.Active);
849
+ await this.updateHomeKitCharacteristics();
841
850
  }
842
851
  }
843
852
 
844
- /**
845
- * Handle requests to set the "Target Humidifier Dehumidifier State" characteristic
846
- */
847
- async handleTargetHumidifierDehumidifierStateSet(value: CharacteristicValue): Promise<void> {
848
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} TargetHumidifierDehumidifierState: ${value}`);
849
-
850
- this.TargetHumidifierDehumidifierState = value;
851
- this.doHumidifierUpdate.next();
853
+ async apiError(e: any): Promise<void> {
854
+ this.humidifierService.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, e);
855
+ if (!this.BLE) {
856
+ this.humidifierService.updateCharacteristic(this.platform.Characteristic.WaterLevel, e);
857
+ }
858
+ this.humidifierService.updateCharacteristic(this.platform.Characteristic.CurrentHumidifierDehumidifierState, e);
859
+ this.humidifierService.updateCharacteristic(this.platform.Characteristic.TargetHumidifierDehumidifierState, e);
860
+ this.humidifierService.updateCharacteristic(this.platform.Characteristic.Active, e);
861
+ this.humidifierService.updateCharacteristic(this.platform.Characteristic.RelativeHumidityHumidifierThreshold, e);
862
+ if (!this.device.humidifier?.hide_temperature && !this.BLE) {
863
+ this.temperatureservice?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, e);
864
+ }
852
865
  }
853
866
 
854
- /**
855
- * Handle requests to set the "Active" characteristic
856
- */
857
- async handleActiveSet(value: CharacteristicValue): Promise<void> {
858
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Active: ${value}`);
859
- this.Active = value;
860
- this.doHumidifierUpdate.next();
867
+ FirmwareRevision(accessory: PlatformAccessory<Context>, device: device & devicesConfig): CharacteristicValue {
868
+ let FirmwareRevision: string;
869
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
870
+ + ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
871
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
872
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
873
+ if (accessory.context.FirmwareRevision) {
874
+ FirmwareRevision = accessory.context.FirmwareRevision;
875
+ } else if (device.firmware) {
876
+ FirmwareRevision = device.firmware;
877
+ } else {
878
+ FirmwareRevision = this.platform.version;
879
+ }
880
+ return FirmwareRevision;
861
881
  }
862
882
 
863
- /**
864
- * Handle requests to set the "Relative Humidity Humidifier Threshold" characteristic
865
- */
866
- async handleRelativeHumidityHumidifierThresholdSet(value: CharacteristicValue): Promise<void> {
867
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} RelativeHumidityHumidifierThreshold: ${value}`);
868
-
869
- this.RelativeHumidityHumidifierThreshold = value;
870
- if (this.Active === this.platform.Characteristic.Active.INACTIVE) {
883
+ async context() {
884
+ if (this.Active === undefined) {
871
885
  this.Active = this.platform.Characteristic.Active.ACTIVE;
872
- this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.IDLE;
886
+ } else {
887
+ this.Active = this.accessory.context.Active;
888
+ }
889
+ if (this.CurrentTemperature === undefined) {
890
+ this.CurrentTemperature = 0;
891
+ } else {
892
+ this.CurrentTemperature = this.accessory.context.CurrentTemperature;
893
+ }
894
+ if (this.CurrentRelativeHumidity === undefined) {
895
+ this.CurrentRelativeHumidity = 0;
896
+ } else {
897
+ this.CurrentRelativeHumidity = this.accessory.context.CurrentRelativeHumidity;
898
+ }
899
+ if (this.TargetHumidifierDehumidifierState === undefined) {
900
+ this.TargetHumidifierDehumidifierState = this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER;
901
+ } else {
902
+ this.TargetHumidifierDehumidifierState = this.accessory.context.TargetHumidifierDehumidifierState;
903
+ }
904
+ if (this.CurrentHumidifierDehumidifierState === undefined) {
905
+ this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.INACTIVE;
906
+ } else {
907
+ this.CurrentHumidifierDehumidifierState = this.accessory.context.CurrentHumidifierDehumidifierState;
908
+ }
909
+ if (this.RelativeHumidityHumidifierThreshold === undefined) {
910
+ this.RelativeHumidityHumidifierThreshold = 0;
911
+ } else {
912
+ this.RelativeHumidityHumidifierThreshold = this.accessory.context.RelativeHumidityHumidifierThreshold;
913
+ }
914
+ if (this.WaterLevel === undefined) {
915
+ this.WaterLevel = 0;
916
+ } else {
917
+ this.WaterLevel = this.accessory.context.WaterLevel;
918
+ }
919
+ }
920
+
921
+ async refreshRate(device: device & devicesConfig): Promise<void> {
922
+ if (device.refreshRate) {
923
+ this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
924
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
925
+ } else if (this.platform.config.options!.refreshRate) {
926
+ this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options!.refreshRate;
927
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
873
928
  }
874
- this.doHumidifierUpdate.next();
875
929
  }
876
930
 
877
931
  async config(device: device & devicesConfig): Promise<void> {
@@ -879,11 +933,8 @@ export class Humidifier {
879
933
  if (device.humidifier) {
880
934
  config = device.humidifier;
881
935
  }
882
- if (device.ble !== undefined) {
883
- config['ble'] = device.ble;
884
- }
885
- if (device.openAPI !== undefined) {
886
- config['openAPI'] = device.openAPI;
936
+ if (device.connectionType !== undefined) {
937
+ config['connectionType'] = device.connectionType;
887
938
  }
888
939
  if (device.logging !== undefined) {
889
940
  config['logging'] = device.logging;
@@ -902,39 +953,6 @@ export class Humidifier {
902
953
  }
903
954
  }
904
955
 
905
- async refreshRate(device: device & devicesConfig): Promise<void> {
906
- if (device.refreshRate) {
907
- this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
908
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
909
- } else if (this.platform.config.options!.refreshRate) {
910
- this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options!.refreshRate;
911
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
912
- }
913
- }
914
-
915
- async openAPI() {
916
- if (!this.device.openAPI) {
917
- this.OpenAPI = true;
918
- } else {
919
- this.OpenAPI = this.device.openAPI;
920
- }
921
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI: ${this.OpenAPI}`);
922
- }
923
-
924
- async scan(device: device & devicesConfig): Promise<void> {
925
- if (device.scanDuration) {
926
- this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
927
- if (device.ble) {
928
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
929
- }
930
- } else {
931
- this.scanDuration = this.accessory.context.scanDuration = 1;
932
- if (this.device.ble) {
933
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
934
- }
935
- }
936
- }
937
-
938
956
  async logs(device: device & devicesConfig): Promise<void> {
939
957
  if (this.platform.debugMode) {
940
958
  this.deviceLogging = this.accessory.context.logging = 'debugMode';
@@ -951,31 +969,6 @@ export class Humidifier {
951
969
  }
952
970
  }
953
971
 
954
- FirmwareRevision(accessory: PlatformAccessory<Context>, device: device & devicesConfig): CharacteristicValue {
955
- let FirmwareRevision: string;
956
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
957
- + ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
958
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
959
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
960
- if (accessory.context.FirmwareRevision) {
961
- FirmwareRevision = accessory.context.FirmwareRevision;
962
- } else if (device.firmware) {
963
- FirmwareRevision = device.firmware;
964
- } else {
965
- FirmwareRevision = this.platform.version;
966
- }
967
- return FirmwareRevision;
968
- }
969
-
970
- minStep(): number {
971
- if (this.device.humidifier?.set_minStep) {
972
- this.set_minStep = this.device.humidifier?.set_minStep;
973
- } else {
974
- this.set_minStep = 1;
975
- }
976
- return this.set_minStep;
977
- }
978
-
979
972
  /**
980
973
  * Logging for Device
981
974
  */