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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. package/CHANGELOG.md +12 -2
  2. package/README.md +3 -2
  3. package/config.schema.json +108 -20
  4. package/dist/device/bot.d.ts +34 -28
  5. package/dist/device/bot.d.ts.map +1 -1
  6. package/dist/device/bot.js +657 -648
  7. package/dist/device/bot.js.map +1 -1
  8. package/dist/device/ceilinglight.d.ts +137 -0
  9. package/dist/device/ceilinglight.d.ts.map +1 -0
  10. package/dist/device/ceilinglight.js +1127 -0
  11. package/dist/device/ceilinglight.js.map +1 -0
  12. package/dist/device/colorbulb.d.ts +36 -19
  13. package/dist/device/colorbulb.d.ts.map +1 -1
  14. package/dist/device/colorbulb.js +486 -309
  15. package/dist/device/colorbulb.js.map +1 -1
  16. package/dist/device/contact.d.ts +18 -8
  17. package/dist/device/contact.d.ts.map +1 -1
  18. package/dist/device/contact.js +306 -237
  19. package/dist/device/contact.js.map +1 -1
  20. package/dist/device/curtain.d.ts +23 -16
  21. package/dist/device/curtain.d.ts.map +1 -1
  22. package/dist/device/curtain.js +566 -502
  23. package/dist/device/curtain.js.map +1 -1
  24. package/dist/device/humidifier.d.ts +24 -18
  25. package/dist/device/humidifier.d.ts.map +1 -1
  26. package/dist/device/humidifier.js +453 -369
  27. package/dist/device/humidifier.js.map +1 -1
  28. package/dist/device/lock.d.ts +27 -8
  29. package/dist/device/lock.d.ts.map +1 -1
  30. package/dist/device/lock.js +305 -148
  31. package/dist/device/lock.js.map +1 -1
  32. package/dist/device/meter.d.ts +22 -16
  33. package/dist/device/meter.d.ts.map +1 -1
  34. package/dist/device/meter.js +367 -267
  35. package/dist/device/meter.js.map +1 -1
  36. package/dist/device/meterplus.d.ts +24 -12
  37. package/dist/device/meterplus.d.ts.map +1 -1
  38. package/dist/device/meterplus.js +381 -213
  39. package/dist/device/meterplus.js.map +1 -1
  40. package/dist/device/motion.d.ts +18 -8
  41. package/dist/device/motion.d.ts.map +1 -1
  42. package/dist/device/motion.js +279 -216
  43. package/dist/device/motion.js.map +1 -1
  44. package/dist/device/plug.d.ts +27 -17
  45. package/dist/device/plug.d.ts.map +1 -1
  46. package/dist/device/plug.js +295 -251
  47. package/dist/device/plug.js.map +1 -1
  48. package/dist/device/striplight.d.ts +43 -19
  49. package/dist/device/striplight.d.ts.map +1 -1
  50. package/dist/device/striplight.js +506 -262
  51. package/dist/device/striplight.js.map +1 -1
  52. package/dist/irdevice/airconditioner.d.ts +9 -12
  53. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  54. package/dist/irdevice/airconditioner.js +278 -230
  55. package/dist/irdevice/airconditioner.js.map +1 -1
  56. package/dist/irdevice/airpurifier.d.ts +5 -3
  57. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  58. package/dist/irdevice/airpurifier.js +154 -104
  59. package/dist/irdevice/airpurifier.js.map +1 -1
  60. package/dist/irdevice/camera.d.ts +11 -8
  61. package/dist/irdevice/camera.d.ts.map +1 -1
  62. package/dist/irdevice/camera.js +133 -86
  63. package/dist/irdevice/camera.js.map +1 -1
  64. package/dist/irdevice/fan.d.ts +10 -7
  65. package/dist/irdevice/fan.d.ts.map +1 -1
  66. package/dist/irdevice/fan.js +160 -109
  67. package/dist/irdevice/fan.js.map +1 -1
  68. package/dist/irdevice/light.d.ts +11 -8
  69. package/dist/irdevice/light.d.ts.map +1 -1
  70. package/dist/irdevice/light.js +137 -103
  71. package/dist/irdevice/light.js.map +1 -1
  72. package/dist/irdevice/other.d.ts +11 -8
  73. package/dist/irdevice/other.d.ts.map +1 -1
  74. package/dist/irdevice/other.js +145 -98
  75. package/dist/irdevice/other.js.map +1 -1
  76. package/dist/irdevice/tv.d.ts +12 -9
  77. package/dist/irdevice/tv.d.ts.map +1 -1
  78. package/dist/irdevice/tv.js +143 -93
  79. package/dist/irdevice/tv.js.map +1 -1
  80. package/dist/irdevice/vacuumcleaner.d.ts +9 -10
  81. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  82. package/dist/irdevice/vacuumcleaner.js +132 -89
  83. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  84. package/dist/irdevice/waterheater.d.ts +8 -9
  85. package/dist/irdevice/waterheater.d.ts.map +1 -1
  86. package/dist/irdevice/waterheater.js +132 -89
  87. package/dist/irdevice/waterheater.js.map +1 -1
  88. package/dist/platform.d.ts +15 -12
  89. package/dist/platform.d.ts.map +1 -1
  90. package/dist/platform.js +574 -454
  91. package/dist/platform.js.map +1 -1
  92. package/dist/settings.d.ts +21 -11
  93. package/dist/settings.d.ts.map +1 -1
  94. package/dist/settings.js.map +1 -1
  95. package/homebridge-ui/public/index.html +1 -0
  96. package/package.json +13 -11
  97. package/src/device/bot.ts +656 -659
  98. package/src/device/ceilinglight.ts +1208 -0
  99. package/src/device/colorbulb.ts +490 -344
  100. package/src/device/contact.ts +309 -241
  101. package/src/device/curtain.ts +571 -516
  102. package/src/device/humidifier.ts +456 -403
  103. package/src/device/lock.ts +314 -149
  104. package/src/device/meter.ts +378 -280
  105. package/src/device/meterplus.ts +391 -219
  106. package/src/device/motion.ts +285 -222
  107. package/src/device/plug.ts +299 -260
  108. package/src/device/striplight.ts +522 -289
  109. package/src/irdevice/airconditioner.ts +282 -241
  110. package/src/irdevice/airpurifier.ts +151 -103
  111. package/src/irdevice/camera.ts +131 -85
  112. package/src/irdevice/fan.ts +157 -106
  113. package/src/irdevice/light.ts +135 -103
  114. package/src/irdevice/other.ts +143 -97
  115. package/src/irdevice/tv.ts +142 -93
  116. package/src/irdevice/vacuumcleaner.ts +131 -89
  117. package/src/irdevice/waterheater.ts +130 -89
  118. package/src/platform.ts +567 -476
  119. package/src/settings.ts +22 -11
@@ -3,6 +3,7 @@ import crypto from 'crypto';
3
3
  import { Context } from 'vm';
4
4
  import { IncomingMessage } from 'http';
5
5
  import { interval, Subject } from 'rxjs';
6
+ import superStringify from 'super-stringify';
6
7
  import { SwitchBotPlatform } from '../platform';
7
8
  import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
8
9
  import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
@@ -54,19 +55,17 @@ export class Humidifier {
54
55
  humidifierUpdateInProgress!: boolean;
55
56
  doHumidifierUpdate!: Subject<void>;
56
57
 
58
+ // Connection
59
+ private readonly BLE = (this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI');
60
+ private readonly OpenAPI = (this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI');
61
+
57
62
  constructor(private readonly platform: SwitchBotPlatform, private accessory: PlatformAccessory, public device: device & devicesConfig) {
58
63
  // default placeholders
59
64
  this.logs(device);
60
65
  this.scan(device);
61
66
  this.refreshRate(device);
62
67
  this.config(device);
63
- this.CurrentRelativeHumidity = 0;
64
- this.TargetHumidifierDehumidifierState = this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER;
65
- this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.INACTIVE;
66
- this.Active = this.platform.Characteristic.Active.ACTIVE;
67
- this.RelativeHumidityHumidifierThreshold = 0;
68
- this.CurrentTemperature = 0;
69
- this.WaterLevel = 0;
68
+ this.context();
70
69
 
71
70
  // this is subject we use to track when we need to POST changes to the SwitchBot API
72
71
  this.doHumidifierUpdate = new Subject();
@@ -116,9 +115,9 @@ export class Humidifier {
116
115
  maxValue: 1,
117
116
  validValues: [0, 1],
118
117
  })
119
- .onSet(this.handleTargetHumidifierDehumidifierStateSet.bind(this));
118
+ .onSet(this.TargetHumidifierDehumidifierStateSet.bind(this));
120
119
 
121
- this.humidifierService.getCharacteristic(this.platform.Characteristic.Active).onSet(this.handleActiveSet.bind(this));
120
+ this.humidifierService.getCharacteristic(this.platform.Characteristic.Active).onSet(this.ActiveSet.bind(this));
122
121
 
123
122
  this.humidifierService
124
123
  .getCharacteristic(this.platform.Characteristic.RelativeHumidityHumidifierThreshold)
@@ -128,15 +127,15 @@ export class Humidifier {
128
127
  maxValue: 100,
129
128
  minStep: this.minStep(),
130
129
  })
131
- .onSet(this.handleRelativeHumidityHumidifierThresholdSet.bind(this));
130
+ .onSet(this.RelativeHumidityHumidifierThresholdSet.bind(this));
132
131
 
133
132
  // Temperature Sensor Service
134
- if (device.humidifier?.hide_temperature || device.ble) {
135
- this.debugLog(`Humidifier: ${accessory.displayName} Removing Temperature Sensor Service`);
133
+ if (device.humidifier?.hide_temperature || this.BLE) {
134
+ this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Temperature Sensor Service`);
136
135
  this.temperatureservice = this.accessory.getService(this.platform.Service.TemperatureSensor);
137
136
  accessory.removeService(this.temperatureservice!);
138
- } else if (!this.temperatureservice && !device.ble) {
139
- this.debugLog(`Humidifier: ${accessory.displayName} Add Temperature Sensor Service`);
137
+ } else if (!this.temperatureservice && !this.BLE) {
138
+ this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Temperature Sensor Service`);
140
139
  (this.temperatureservice =
141
140
  this.accessory.getService(this.platform.Service.TemperatureSensor) || this.accessory.addService(this.platform.Service.TemperatureSensor)),
142
141
  `${accessory.displayName} Temperature Sensor`;
@@ -155,7 +154,7 @@ export class Humidifier {
155
154
  return this.CurrentTemperature;
156
155
  });
157
156
  } else {
158
- this.debugLog(`Humidifier: ${accessory.displayName} Temperature Sensor Service Not Added`);
157
+ this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Temperature Sensor Service Not Added`);
159
158
  }
160
159
 
161
160
  // Retrieve initial values and updateHomekit
@@ -181,11 +180,9 @@ export class Humidifier {
181
180
  try {
182
181
  await this.pushChanges();
183
182
  } catch (e: any) {
184
- this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushChanges`);
185
- if (this.deviceLogging.includes('debug')) {
186
- this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushChanges,` + ` Error Message: ${JSON.stringify(e.message)}`);
187
- }
188
183
  this.apiError(e);
184
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with ${this.device.connectionType} Connection,`
185
+ + ` Error Message: ${superStringify(e.message)}`);
189
186
  }
190
187
  this.humidifierUpdateInProgress = false;
191
188
  });
@@ -195,104 +192,112 @@ export class Humidifier {
195
192
  * Parse the device status from the SwitchBot api
196
193
  */
197
194
  async parseStatus(): Promise<void> {
198
- if (this.device.ble) {
195
+ if (this.BLE) {
199
196
  await this.BLEparseStatus();
200
- } else {
197
+ } else if (this.OpenAPI && this.platform.config.credentials?.token) {
201
198
  await this.openAPIparseStatus();
199
+ } else {
200
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
201
+ + ` ${this.device.connectionType}, parseStatus will not happen.`);
202
202
  }
203
203
  }
204
204
 
205
205
  async BLEparseStatus(): Promise<void> {
206
- this.debugLog(`Humidifier: ${this.accessory.displayName} BLE parseStatus`);
206
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
207
207
  // Current Relative Humidity
208
208
  this.CurrentRelativeHumidity = this.percentage!;
209
- this.debugLog(`Humidifier: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
209
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
210
210
  // Active
211
211
  if (this.onState) {
212
212
  this.Active = this.platform.Characteristic.Active.ACTIVE;
213
213
  } else {
214
214
  this.Active = this.platform.Characteristic.Active.INACTIVE;
215
215
  }
216
- this.debugLog(`Humidifier: ${this.accessory.displayName} Active: ${this.Active}`);
216
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Active: ${this.Active}`);
217
217
  }
218
218
 
219
219
  async openAPIparseStatus(): Promise<void> {
220
- if (this.platform.config.credentials?.token) {
221
- this.debugLog(`Humidifier: ${this.accessory.displayName} OpenAPI parseStatus`);
222
- // Current Relative Humidity
223
- this.CurrentRelativeHumidity = this.humidity!;
224
- this.debugLog(`Humidifier: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
225
- // Current Temperature
226
- if (!this.device.humidifier?.hide_temperature) {
227
- this.CurrentTemperature = this.temperature!;
228
- this.debugLog(`Humidifier: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
229
- }
230
- // Target Humidifier Dehumidifier State
231
- switch (this.auto) {
232
- case true:
233
- this.TargetHumidifierDehumidifierState = this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER_OR_DEHUMIDIFIER;
220
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
221
+ // Current Relative Humidity
222
+ this.CurrentRelativeHumidity = this.humidity!;
223
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
224
+ // Current Temperature
225
+ if (!this.device.humidifier?.hide_temperature) {
226
+ this.CurrentTemperature = this.temperature!;
227
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
228
+ }
229
+ // Target Humidifier Dehumidifier State
230
+ switch (this.auto) {
231
+ case true:
232
+ this.TargetHumidifierDehumidifierState = this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER_OR_DEHUMIDIFIER;
233
+ this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.HUMIDIFYING;
234
+ this.RelativeHumidityHumidifierThreshold = this.CurrentRelativeHumidity;
235
+ break;
236
+ default:
237
+ this.TargetHumidifierDehumidifierState = this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER;
238
+ if (this.nebulizationEfficiency! > 100) {
239
+ this.RelativeHumidityHumidifierThreshold = 100;
240
+ } else {
241
+ this.RelativeHumidityHumidifierThreshold = this.nebulizationEfficiency!;
242
+ }
243
+ if (this.CurrentRelativeHumidity > this.RelativeHumidityHumidifierThreshold) {
244
+ this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.IDLE;
245
+ } else if (this.Active === this.platform.Characteristic.Active.INACTIVE) {
246
+ this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.INACTIVE;
247
+ } else {
234
248
  this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.HUMIDIFYING;
235
- this.RelativeHumidityHumidifierThreshold = this.CurrentRelativeHumidity;
236
- break;
237
- default:
238
- this.TargetHumidifierDehumidifierState = this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER;
239
- if (this.nebulizationEfficiency! > 100) {
240
- this.RelativeHumidityHumidifierThreshold = 100;
241
- } else {
242
- this.RelativeHumidityHumidifierThreshold = this.nebulizationEfficiency!;
243
- }
244
- if (this.CurrentRelativeHumidity > this.RelativeHumidityHumidifierThreshold) {
245
- this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.IDLE;
246
- } else if (this.Active === this.platform.Characteristic.Active.INACTIVE) {
247
- this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.INACTIVE;
248
- } else {
249
- this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.HUMIDIFYING;
250
- }
251
- }
252
- this.debugLog(`Humidifier: ${this.accessory.displayName} TargetHumidifierDehumidifierState: ${this.TargetHumidifierDehumidifierState}`);
253
- this.debugLog(
254
- `Humidifier: ${this.accessory.displayName}` + ` RelativeHumidityHumidifierThreshold: ${this.RelativeHumidityHumidifierThreshold}`,
255
- );
256
- this.debugLog(`Humidifier: ${this.accessory.displayName} CurrentHumidifierDehumidifierState: ${this.CurrentHumidifierDehumidifierState}`);
257
- // Active
258
- switch (this.power) {
259
- case 'on':
260
- this.Active = this.platform.Characteristic.Active.ACTIVE;
261
- break;
262
- default:
263
- this.Active = this.platform.Characteristic.Active.INACTIVE;
264
- }
265
- this.debugLog(`Humidifier: ${this.accessory.displayName} Active: ${this.Active}`);
266
- // Water Level
267
- if (this.lackWater) {
268
- this.WaterLevel = 0;
269
- } else {
270
- this.WaterLevel = 100;
271
- }
272
- this.debugLog(`Humidifier: ${this.accessory.displayName} WaterLevel: ${this.WaterLevel}`);
249
+ }
273
250
  }
251
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
252
+ + ` TargetHumidifierDehumidifierState: ${this.TargetHumidifierDehumidifierState}`);
253
+ this.debugLog(
254
+ `${this.device.deviceType}: ${this.accessory.displayName}`
255
+ + ` RelativeHumidityHumidifierThreshold: ${this.RelativeHumidityHumidifierThreshold}`,
256
+ );
257
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
258
+ + ` CurrentHumidifierDehumidifierState: ${this.CurrentHumidifierDehumidifierState}`);
259
+ // Active
260
+ switch (this.power) {
261
+ case 'on':
262
+ this.Active = this.platform.Characteristic.Active.ACTIVE;
263
+ break;
264
+ default:
265
+ this.Active = this.platform.Characteristic.Active.INACTIVE;
266
+ }
267
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Active: ${this.Active}`);
268
+ // Water Level
269
+ if (this.lackWater) {
270
+ this.WaterLevel = 0;
271
+ } else {
272
+ this.WaterLevel = 100;
273
+ }
274
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} WaterLevel: ${this.WaterLevel}`);
275
+
274
276
  }
275
277
 
276
278
  /**
277
279
  * Asks the SwitchBot API for the latest device information
278
280
  */
279
281
  async refreshStatus(): Promise<void> {
280
- if (this.device.ble) {
282
+ if (this.BLE) {
281
283
  await this.BLERefreshStatus();
282
- } else {
284
+ } else if (this.OpenAPI && this.platform.config.credentials?.token) {
283
285
  await this.openAPIRefreshStatus();
286
+ } else {
287
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
288
+ + ` ${this.device.connectionType}, refreshStatus will not happen.`);
284
289
  }
285
290
  }
286
291
 
287
292
  async BLERefreshStatus(): Promise<void> {
288
- this.debugLog(`Humidifier: ${this.accessory.displayName} BLE refreshStatus`);
293
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
289
294
  const switchbot = await this.platform.connectBLE();
290
295
  // Convert to BLE Address
291
296
  this.device.bleMac = this.device
292
297
  .deviceId!.match(/.{1,2}/g)!
293
298
  .join(':')
294
299
  .toLowerCase();
295
- this.debugLog(`Humidifier: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
300
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
296
301
  this.getCustomBLEAddress(switchbot);
297
302
  // Start to monitor advertisement packets
298
303
  if (switchbot !== false) {
@@ -305,28 +310,24 @@ export class Humidifier {
305
310
  // Set an event hander
306
311
  switchbot.onadvertisement = (ad: ad) => {
307
312
  this.address = ad.address;
308
- if (this.deviceLogging.includes('debug')) {
309
- this.infoLog(this.address);
310
- this.infoLog(this.device.bleMac);
311
- this.infoLog(`Humidifier: ${this.accessory.displayName} BLE Address Found: ${this.address}`);
312
- this.infoLog(`Humidifier: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac}`);
313
- }
313
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},`
314
+ + ` BLE Address Found: ${this.address}`);
314
315
  this.serviceData = ad.serviceData;
315
316
  this.autoMode = ad.serviceData.autoMode;
316
317
  this.onState = ad.serviceData.onState;
317
318
  this.percentage = ad.serviceData.percentage;
318
- this.debugLog(`Humidifier: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
319
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${superStringify(ad.serviceData)}`);
319
320
  this.debugLog(
320
- `Humidifier: ${this.accessory.displayName} model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName},` +
321
+ `${this.device.deviceType}: ${this.accessory.displayName} model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName},` +
321
322
  `autoMode: ${ad.serviceData.autoMode}, onState: ${ad.serviceData.onState}, percentage: ${ad.serviceData.percentage}`,
322
323
  );
323
324
 
324
325
  if (this.serviceData) {
325
326
  this.connected = true;
326
- this.debugLog(`Humidifier: ${this.accessory.displayName} connected: ${this.connected}`);
327
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
327
328
  } else {
328
329
  this.connected = false;
329
- this.debugLog(`Humidifier: ${this.accessory.displayName} connected: ${this.connected}`);
330
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
330
331
  }
331
332
  };
332
333
  // Wait 2 seconds
@@ -336,117 +337,77 @@ export class Humidifier {
336
337
  // Stop to monitor
337
338
  switchbot.stopScan();
338
339
  if (this.connected) {
339
- this.parseStatus();
340
+ this.BLEparseStatus();
340
341
  this.updateHomeKitCharacteristics();
341
342
  } else {
342
- await this.BLEconnection(switchbot);
343
+ await this.BLERefreshConnection(switchbot);
343
344
  }
344
345
  })
345
346
  .catch(async (e: any) => {
346
- this.errorLog(`Humidifier: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
347
- if (this.deviceLogging.includes('debug')) {
348
- this.errorLog(
349
- `Humidifier: ${this.accessory.displayName} failed refreshStatus with BLE Connection,` + ` Error Message: ${JSON.stringify(e.message)}`,
350
- );
351
- }
352
- if (this.platform.config.credentials?.token) {
353
- this.warnLog(`Humidifier: ${this.accessory.displayName} Using OpenAPI Connection`);
354
- await this.openAPIRefreshStatus();
355
- }
356
347
  this.apiError(e);
348
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}`
349
+ + ` Connection, Error Message: ${superStringify(e.message)}`);
350
+ await this.BLERefreshConnection(switchbot);
357
351
  });
358
352
  } else {
359
- await this.BLEconnection(switchbot);
360
- }
361
- }
362
-
363
- async getCustomBLEAddress(switchbot: any) {
364
- if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
365
- (async () => {
366
- // Start to monitor advertisement packets
367
- await switchbot.startScan({
368
- model: 'e',
369
- });
370
- // Set an event handler
371
- switchbot.onadvertisement = (ad: any) => {
372
- this.warnLog(`Humidifier: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
373
- };
374
- await switchbot.wait(10000);
375
- // Stop to monitor
376
- switchbot.stopScan();
377
- })();
378
- }
379
- }
380
-
381
- async BLEconnection(switchbot: any): Promise<void> {
382
- this.errorLog(`Humidifier: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
383
- if (this.platform.config.credentials?.token) {
384
- this.warnLog(`Humidifier: ${this.accessory.displayName} Using OpenAPI Connection`);
385
- await this.openAPIRefreshStatus();
353
+ await this.BLERefreshConnection(switchbot);
386
354
  }
387
355
  }
388
356
 
389
357
  async openAPIRefreshStatus(): Promise<void> {
390
- if (this.platform.config.credentials?.token) {
391
- this.debugLog(`Humidifier: ${this.accessory.displayName} OpenAPI refreshStatus`);
392
- try {
393
- const t = Date.now();
394
- const nonce = 'requestID';
395
- const data = this.platform.config.credentials?.token + t + nonce;
396
- const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
397
- const sign = signTerm.toString('base64');
398
- this.debugLog(`Humidifier: ${this.accessory.displayName} sign: ${sign}`);
399
- const options = {
400
- hostname: HostDomain,
401
- port: 443,
402
- path: `${DevicePath}/${this.device.deviceId}/status`,
403
- method: 'GET',
404
- headers: {
405
- Authorization: this.platform.config.credentials?.token,
406
- sign: sign,
407
- nonce: nonce,
408
- t: t,
409
- 'Content-Type': 'application/json',
410
- },
411
- };
412
- const req = https.request(options, (res) => {
413
- this.debugLog(`Humidifier: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
414
- let rawData = '';
415
- res.on('data', (d) => {
416
- rawData += d;
417
- this.debugLog(`Humidifier: ${this.accessory.displayName} d: ${d}`);
418
- });
419
- res.on('end', () => {
420
- try {
421
- this.deviceStatus = JSON.parse(rawData);
422
- this.debugLog(`Humidifier: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(this.deviceStatus)}`);
423
- this.auto = this.deviceStatus.body.auto;
424
- this.power = this.deviceStatus.body.power;
425
- this.lackWater = this.deviceStatus.body.lackWater;
426
- this.humidity = this.deviceStatus.body.humidity;
427
- this.temperature = this.deviceStatus.body.temperature;
428
- this.nebulizationEfficiency = this.deviceStatus.body.nebulizationEfficiency;
429
- this.parseStatus();
430
- this.updateHomeKitCharacteristics();
431
- } catch (e: any) {
432
- this.errorLog(`Humidifier: ${this.accessory.displayName} error message: ${e.message}`);
433
- }
434
- });
358
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
359
+ try {
360
+ const t = Date.now();
361
+ const nonce = 'requestID';
362
+ const data = this.platform.config.credentials?.token + t + nonce;
363
+ const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
364
+ const sign = signTerm.toString('base64');
365
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
366
+ const options = {
367
+ hostname: HostDomain,
368
+ port: 443,
369
+ path: `${DevicePath}/${this.device.deviceId}/status`,
370
+ method: 'GET',
371
+ headers: {
372
+ Authorization: this.platform.config.credentials?.token,
373
+ sign: sign,
374
+ nonce: nonce,
375
+ t: t,
376
+ 'Content-Type': 'application/json',
377
+ },
378
+ };
379
+ const req = https.request(options, (res) => {
380
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus statusCode: ${res.statusCode}`);
381
+ let rawData = '';
382
+ res.on('data', (d) => {
383
+ rawData += d;
384
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
435
385
  });
436
- req.on('error', (e: any) => {
437
- this.errorLog(`Humidifier: ${this.accessory.displayName} error message: ${e.message}`);
386
+ res.on('end', () => {
387
+ try {
388
+ this.deviceStatus = JSON.parse(rawData);
389
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus: ${superStringify(this.deviceStatus)}`);
390
+ this.auto = this.deviceStatus.body.auto;
391
+ this.power = this.deviceStatus.body.power;
392
+ this.lackWater = this.deviceStatus.body.lackWater;
393
+ this.humidity = this.deviceStatus.body.humidity;
394
+ this.temperature = this.deviceStatus.body.temperature;
395
+ this.nebulizationEfficiency = this.deviceStatus.body.nebulizationEfficiency;
396
+ this.openAPIparseStatus();
397
+ this.updateHomeKitCharacteristics();
398
+ } catch (e: any) {
399
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
400
+ }
438
401
  });
439
- req.end();
440
- } catch (e: any) {
441
- this.errorLog(`Humidifier: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
442
- if (this.deviceLogging.includes('debug')) {
443
- this.errorLog(
444
- `Humidifier: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,` +
445
- ` Error Message: ${JSON.stringify(e.message)}`,
446
- );
447
- }
448
- this.apiError(e);
449
- }
402
+ });
403
+ req.on('error', (e: any) => {
404
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
405
+ });
406
+ req.end();
407
+ } catch (e: any) {
408
+ this.apiError(e);
409
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
410
+ + ` Connection, Error Message: ${superStringify(e.message)}`);
450
411
  }
451
412
  }
452
413
 
@@ -454,11 +415,14 @@ export class Humidifier {
454
415
  * Pushes the requested changes to the SwitchBot API
455
416
  */
456
417
  async pushChanges(): Promise<void> {
457
- //if (this.device.ble) {
458
- // await this.BLEpushChanges();
459
- //} else {
460
- await this.openAPIpushChanges();
461
- //}
418
+ /*if (this.BLE) {
419
+ await this.BLEpushChanges();
420
+ } else*/ if (this.OpenAPI && this.platform.config.credentials?.token) {
421
+ await this.openAPIpushChanges();
422
+ } else {
423
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
424
+ + ` ${this.device.connectionType}, pushChanges will not happen.`);
425
+ }
462
426
  interval(5000)
463
427
  .pipe(take(1))
464
428
  .subscribe(async () => {
@@ -467,54 +431,41 @@ export class Humidifier {
467
431
  }
468
432
 
469
433
  async BLEpushChanges(): Promise<void> {
470
- this.debugLog(`Humidifier: ${this.accessory.displayName} BLE pushChanges`);
434
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`);
471
435
  const switchbot = await this.platform.connectBLE();
472
436
  // Convert to BLE Address
473
437
  this.device.bleMac = this.device
474
438
  .deviceId!.match(/.{1,2}/g)!
475
439
  .join(':')
476
440
  .toLowerCase();
477
- this.debugLog(`Humidifier: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
478
- if (switchbot !== false) {
479
- switchbot
480
- .discover({ model: 'e', quick: true, id: this.device.bleMac })
481
- .then((device_list) => {
482
- this.infoLog(`${this.accessory.displayName} Target Position: ${this.Active}`);
483
- return device_list[0].percentage(this.RelativeHumidityHumidifierThreshold);
484
- })
485
- .then(() => {
486
- this.debugLog(`Humidifier: ${this.accessory.displayName} Done.`);
487
- })
488
- .catch(async (e: any) => {
489
- this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
490
- if (this.deviceLogging.includes('debug')) {
491
- this.errorLog(
492
- `Humidifier: ${this.accessory.displayName} failed pushChanges with BLE Connection,` + ` Error Message: ${JSON.stringify(e.message)}`,
493
- );
494
- }
495
- if (this.platform.config.credentials?.token) {
496
- this.warnLog(`Humidifier: ${this.accessory.displayName} Using OpenAPI Connection`);
497
- await this.openAPIpushChanges();
498
- }
499
- this.apiError(e);
500
- });
501
- } else {
502
- this.errorLog(`Humidifier: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
503
- if (this.platform.config.credentials?.token) {
504
- this.warnLog(`Humidifier: ${this.accessory.displayName} Using OpenAPI Connection`);
505
- await this.openAPIpushChanges();
506
- }
507
- }
441
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
442
+ switchbot
443
+ .discover({
444
+ model: 'e',
445
+ quick: true,
446
+ id: this.device.bleMac,
447
+ })
448
+ .then((device_list: any) => {
449
+ this.infoLog(`${this.accessory.displayName} Target Position: ${this.Active}`);
450
+ return device_list[0].percentage(this.RelativeHumidityHumidifierThreshold);
451
+ })
452
+ .then(() => {
453
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
454
+ })
455
+ .catch(async (e: any) => {
456
+ this.apiError(e);
457
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
458
+ + ` Connection, Error Message: ${superStringify(e.message)}`);
459
+ await this.BLEPushConnection();
460
+ });
508
461
  }
509
462
 
510
463
  async openAPIpushChanges(): Promise<void> {
511
- if (this.platform.config.credentials?.token) {
512
- this.debugLog(`Humidifier: ${this.accessory.displayName} OpenAPI pushChanges`);
513
- if (
514
- this.TargetHumidifierDehumidifierState === this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER &&
515
- this.Active === this.platform.Characteristic.Active.ACTIVE
516
- ) {
517
- // Make Push On request to the API
464
+ try {
465
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
466
+ if (this.TargetHumidifierDehumidifierState === this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER &&
467
+ this.Active === this.platform.Characteristic.Active.ACTIVE) {
468
+ // Make Push On request to the API
518
469
  const t = Date.now();
519
470
  const nonce = 'requestID';
520
471
  const data = this.platform.config.credentials?.token + t + nonce;
@@ -522,14 +473,14 @@ export class Humidifier {
522
473
  .update(Buffer.from(data, 'utf-8'))
523
474
  .digest();
524
475
  const sign = signTerm.toString('base64');
525
- this.debugLog(`Humidifier: ${this.accessory.displayName} sign: ${sign}`);
476
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
526
477
  this.debugLog(`Pushing Manual: ${this.RelativeHumidityHumidifierThreshold}!`);
527
- const body = JSON.stringify({
478
+ const body = superStringify({
528
479
  'command': 'setMode',
529
480
  'parameter': `${this.RelativeHumidityHumidifierThreshold}`,
530
481
  'commandType': 'command',
531
482
  });
532
- this.infoLog(`Humidifier: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
483
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
533
484
  const options = {
534
485
  hostname: HostDomain,
535
486
  port: 443,
@@ -545,18 +496,18 @@ export class Humidifier {
545
496
  },
546
497
  };
547
498
  const req = https.request(options, res => {
548
- this.debugLog(`Humidifier: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
499
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges statusCode: ${res.statusCode}`);
549
500
  res.on('data', d => {
550
- this.debugLog(`Humidifier: ${this.accessory.displayName} d: ${d}`);
501
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
551
502
  });
552
503
  });
553
504
  req.on('error', (e: any) => {
554
- this.errorLog(`Humidifier: ${this.accessory.displayName} error message: ${e.message}`);
505
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
555
506
  this.apiError(e);
556
507
  });
557
508
  req.write(body);
558
509
  req.end();
559
- this.debugLog(`Humidifier: ${this.accessory.displayName} openAPIpushChanges: ${JSON.stringify(req)}`);
510
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges: ${superStringify(req)}`);
560
511
  } else if (
561
512
  this.TargetHumidifierDehumidifierState === this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER_OR_DEHUMIDIFIER &&
562
513
  this.Active === this.platform.Characteristic.Active.ACTIVE
@@ -565,6 +516,10 @@ export class Humidifier {
565
516
  } else {
566
517
  await this.pushActiveChanges();
567
518
  }
519
+ } catch (e: any) {
520
+ this.apiError(e);
521
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
522
+ + ` Connection, Error Message: ${superStringify(e.message)}`);
568
523
  }
569
524
  }
570
525
 
@@ -573,6 +528,7 @@ export class Humidifier {
573
528
  */
574
529
  async pushAutoChanges(): Promise<void> {
575
530
  try {
531
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushAutoChanges`);
576
532
  if (
577
533
  this.TargetHumidifierDehumidifierState === this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER_OR_DEHUMIDIFIER &&
578
534
  this.Active === this.platform.Characteristic.Active.ACTIVE
@@ -585,14 +541,14 @@ export class Humidifier {
585
541
  .update(Buffer.from(data, 'utf-8'))
586
542
  .digest();
587
543
  const sign = signTerm.toString('base64');
588
- this.debugLog(`Humidifier: ${this.accessory.displayName} sign: ${sign}`);
544
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
589
545
  this.debugLog('Pushing Auto!');
590
- const body = JSON.stringify({
546
+ const body = superStringify({
591
547
  'command': 'setMode',
592
548
  'parameter': 'auto',
593
549
  'commandType': 'command',
594
550
  });
595
- this.infoLog(`Humidifier: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
551
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
596
552
  const options = {
597
553
  hostname: HostDomain,
598
554
  port: 443,
@@ -608,28 +564,26 @@ export class Humidifier {
608
564
  },
609
565
  };
610
566
  const req = https.request(options, res => {
611
- this.debugLog(`Humidifier: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
567
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
612
568
  this.statusCode({ res });
613
569
  res.on('data', d => {
614
- this.debugLog(`Humidifier: ${this.accessory.displayName} d: ${d}`);
570
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
615
571
  });
616
572
  });
617
573
  req.on('error', (e: any) => {
618
- this.errorLog(`Humidifier: ${this.accessory.displayName} error message: ${e.message}`);
574
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
619
575
  });
620
576
  req.write(body);
621
577
  req.end();
622
- this.debugLog(`Humidifier: ${this.accessory.displayName} pushAutoChanges: ${JSON.stringify(req)}`);
578
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushAutoChanges: ${superStringify(req)}`);
579
+ } else {
580
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushAutoChanges.` +
581
+ `TargetHumidifierDehumidifierState: ${this.TargetHumidifierDehumidifierState}, Active: ${this.Active}`);
623
582
  }
624
583
  } catch (e: any) {
625
- this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushAutoChanges with OpenAPI Connection`);
626
- if (this.deviceLogging.includes('debug')) {
627
- this.errorLog(
628
- `Humidifier: ${this.accessory.displayName} failed pushAutoChanges with OpenAPI Connection,` +
629
- ` Error Message: ${JSON.stringify(e.message)}`,
630
- );
631
- }
632
584
  this.apiError(e);
585
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushAutoChanges with ${this.device.connectionType}`
586
+ + ` Connection, Error Message: ${superStringify(e.message)}`);
633
587
  }
634
588
  }
635
589
 
@@ -638,6 +592,7 @@ export class Humidifier {
638
592
  */
639
593
  async pushActiveChanges(): Promise<void> {
640
594
  try {
595
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushActiveChanges`);
641
596
  if (this.Active === this.platform.Characteristic.Active.INACTIVE) {
642
597
  // Make Push On request to the API
643
598
  const t = Date.now();
@@ -647,14 +602,14 @@ export class Humidifier {
647
602
  .update(Buffer.from(data, 'utf-8'))
648
603
  .digest();
649
604
  const sign = signTerm.toString('base64');
650
- this.debugLog(`Humidifier: ${this.accessory.displayName} sign: ${sign}`);
605
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
651
606
  this.debugLog('Pushing Off!');
652
- const body = JSON.stringify({
607
+ const body = superStringify({
653
608
  'command': 'turnOff',
654
609
  'parameter': 'default',
655
610
  'commandType': 'command',
656
611
  });
657
- this.infoLog(`Humidifier: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
612
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
658
613
  const options = {
659
614
  hostname: HostDomain,
660
615
  port: 443,
@@ -670,29 +625,71 @@ export class Humidifier {
670
625
  },
671
626
  };
672
627
  const req = https.request(options, res => {
673
- this.debugLog(`Humidifier: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
628
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushActiveChanges statusCode: ${res.statusCode}`);
674
629
  this.statusCode({ res });
675
630
  res.on('data', d => {
676
- this.debugLog(`Humidifier: ${this.accessory.displayName} d: ${d}`);
631
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
677
632
  });
678
633
  });
679
634
  req.on('error', (e: any) => {
680
- this.errorLog(`Humidifier: ${this.accessory.displayName} error message: ${e.message}`);
635
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
681
636
  });
682
637
  req.write(body);
683
638
  req.end();
684
- this.debugLog(`Humidifier: ${this.accessory.displayName} pushActiveChanges: ${JSON.stringify(req)}`);
639
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushActiveChanges: ${superStringify(req)}`);
685
640
  }
686
641
  } catch (e: any) {
687
- this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushActiveChanges with OpenAPI Connection`);
688
- if (this.deviceLogging.includes('debug')) {
689
- this.errorLog(
690
- `Humidifier: ${this.accessory.displayName} failed pushActiveChanges with OpenAPI Connection,` +
691
- ` Error Message: ${JSON.stringify(e.message)}`,
692
- );
693
- }
694
642
  this.apiError(e);
643
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushActiveChanges with ${this.device.connectionType}`
644
+ + ` Connection, Error Message: ${superStringify(e.message)}`,
645
+ );
646
+ }
647
+ }
648
+
649
+ /**
650
+ * Handle requests to set the "Active" characteristic
651
+ */
652
+ async ActiveSet(value: CharacteristicValue): Promise<void> {
653
+ if (this.Active === this.accessory.context.Active) {
654
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Active: ${value}`);
655
+ } else {
656
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Active: ${value}`);
657
+ }
658
+
659
+ this.Active = value;
660
+ this.doHumidifierUpdate.next();
661
+ }
662
+
663
+ /**
664
+ * Handle requests to set the "Target Humidifier Dehumidifier State" characteristic
665
+ */
666
+ async TargetHumidifierDehumidifierStateSet(value: CharacteristicValue): Promise<void> {
667
+ if (this.Active === this.platform.Characteristic.Active.ACTIVE) {
668
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set TargetHumidifierDehumidifierState: ${value}`);
669
+ } else {
670
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set TargetHumidifierDehumidifierState: ${value}`);
671
+ }
672
+
673
+ this.TargetHumidifierDehumidifierState = value;
674
+ this.doHumidifierUpdate.next();
675
+ }
676
+
677
+ /**
678
+ * Handle requests to set the "Relative Humidity Humidifier Threshold" characteristic
679
+ */
680
+ async RelativeHumidityHumidifierThresholdSet(value: CharacteristicValue): Promise<void> {
681
+ if (this.Active === this.platform.Characteristic.Active.ACTIVE) {
682
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set RelativeHumidityHumidifierThreshold: ${value}`);
683
+ } else {
684
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set RelativeHumidityHumidifierThreshold: ${value}`);
685
+ }
686
+
687
+ this.RelativeHumidityHumidifierThreshold = value;
688
+ if (this.Active === this.platform.Characteristic.Active.INACTIVE) {
689
+ this.Active = this.platform.Characteristic.Active.ACTIVE;
690
+ this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.IDLE;
695
691
  }
692
+ this.doHumidifierUpdate.next();
696
693
  }
697
694
 
698
695
  /**
@@ -700,118 +697,157 @@ export class Humidifier {
700
697
  */
701
698
  async updateHomeKitCharacteristics(): Promise<void> {
702
699
  if (this.CurrentRelativeHumidity === undefined) {
703
- this.debugLog(`Humidifier: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
700
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
704
701
  } else {
702
+ this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidity;
705
703
  this.humidifierService.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.CurrentRelativeHumidity);
706
- this.debugLog(`Humidifier: ${this.accessory.displayName}` + ` updateCharacteristic CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
704
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
705
+ + ` updateCharacteristic CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
707
706
  }
708
- if (!this.device.ble) {
707
+ if (this.OpenAPI) {
709
708
  if (this.WaterLevel === undefined) {
710
- this.debugLog(`Humidifier: ${this.accessory.displayName} WaterLevel: ${this.WaterLevel}`);
709
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} WaterLevel: ${this.WaterLevel}`);
711
710
  } else {
711
+ this.accessory.context.WaterLevel = this.WaterLevel;
712
712
  this.humidifierService.updateCharacteristic(this.platform.Characteristic.WaterLevel, this.WaterLevel);
713
- this.debugLog(`Humidifier: ${this.accessory.displayName} updateCharacteristic WaterLevel: ${this.WaterLevel}`);
713
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic WaterLevel: ${this.WaterLevel}`);
714
714
  }
715
715
  }
716
716
  if (this.CurrentHumidifierDehumidifierState === undefined) {
717
- this.debugLog(`Humidifier: ${this.accessory.displayName} CurrentHumidifierDehumidifierState: ${this.CurrentHumidifierDehumidifierState}`);
717
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
718
+ + ` CurrentHumidifierDehumidifierState: ${this.CurrentHumidifierDehumidifierState}`);
718
719
  } else {
719
- this.humidifierService.updateCharacteristic(
720
- this.platform.Characteristic.CurrentHumidifierDehumidifierState,
721
- this.CurrentHumidifierDehumidifierState,
722
- );
723
- this.debugLog(
724
- `Humidifier: ${this.accessory.displayName}` +
725
- ` updateCharacteristic CurrentHumidifierDehumidifierState: ${this.CurrentHumidifierDehumidifierState}`,
726
- );
720
+ this.accessory.context.CurrentHumidifierDehumidifierState = this.CurrentHumidifierDehumidifierState;
721
+ this.humidifierService.updateCharacteristic(this.platform.Characteristic.CurrentHumidifierDehumidifierState,
722
+ this.CurrentHumidifierDehumidifierState);
723
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}` +
724
+ ` updateCharacteristic CurrentHumidifierDehumidifierState: ${this.CurrentHumidifierDehumidifierState}`);
727
725
  }
728
726
  if (this.TargetHumidifierDehumidifierState === undefined) {
729
- this.debugLog(`Humidifier: ${this.accessory.displayName} TargetHumidifierDehumidifierState: ${this.TargetHumidifierDehumidifierState}`);
727
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
728
+ +` TargetHumidifierDehumidifierState: ${this.TargetHumidifierDehumidifierState}`);
730
729
  } else {
731
- this.humidifierService.updateCharacteristic(
732
- this.platform.Characteristic.TargetHumidifierDehumidifierState,
733
- this.TargetHumidifierDehumidifierState,
734
- );
735
- this.debugLog(
736
- `Humidifier: ${this.accessory.displayName}` +
730
+ this.accessory.context.TargetHumidifierDehumidifierState = this.TargetHumidifierDehumidifierState;
731
+ this.humidifierService.updateCharacteristic(this.platform.Characteristic.TargetHumidifierDehumidifierState,
732
+ this.TargetHumidifierDehumidifierState);
733
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}` +
737
734
  ` updateCharacteristic TargetHumidifierDehumidifierState: ${this.TargetHumidifierDehumidifierState}`,
738
735
  );
739
736
  }
740
737
  if (this.Active === undefined) {
741
- this.debugLog(`Humidifier: ${this.accessory.displayName} Active: ${this.Active}`);
738
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Active: ${this.Active}`);
742
739
  } else {
740
+ this.accessory.context.Active = this.Active;
743
741
  this.humidifierService.updateCharacteristic(this.platform.Characteristic.Active, this.Active);
744
- this.debugLog(`Humidifier: ${this.accessory.displayName} updateCharacteristic Active: ${this.Active}`);
742
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Active: ${this.Active}`);
745
743
  }
746
744
  if (this.RelativeHumidityHumidifierThreshold === undefined) {
747
- this.debugLog(
748
- `Humidifier: ${this.accessory.displayName}` + ` RelativeHumidityHumidifierThreshold: ${this.RelativeHumidityHumidifierThreshold}`,
749
- );
745
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
746
+ + ` RelativeHumidityHumidifierThreshold: ${this.RelativeHumidityHumidifierThreshold}`);
750
747
  } else {
751
- this.humidifierService.updateCharacteristic(
752
- this.platform.Characteristic.RelativeHumidityHumidifierThreshold,
753
- this.RelativeHumidityHumidifierThreshold,
754
- );
755
- this.debugLog(
756
- `Humidifier: ${this.accessory.displayName}` +
757
- ` updateCharacteristic RelativeHumidityHumidifierThreshold: ${this.RelativeHumidityHumidifierThreshold}`,
758
- );
748
+ this.accessory.context.RelativeHumidityHumidifierThreshold = this.RelativeHumidityHumidifierThreshold;
749
+ this.humidifierService.updateCharacteristic(this.platform.Characteristic.RelativeHumidityHumidifierThreshold,
750
+ this.RelativeHumidityHumidifierThreshold);
751
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}` +
752
+ ` updateCharacteristic RelativeHumidityHumidifierThreshold: ${this.RelativeHumidityHumidifierThreshold}`);
759
753
  }
760
- if (!this.device.humidifier?.hide_temperature && !this.device.ble) {
754
+ if (!this.device.humidifier?.hide_temperature && !this.BLE) {
761
755
  if (this.CurrentTemperature === undefined) {
762
- this.debugLog(`Humidifier: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
756
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
763
757
  } else {
758
+ this.accessory.context.CurrentTemperature = this.CurrentTemperature;
764
759
  this.temperatureservice?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.CurrentTemperature);
765
- this.debugLog(`Humidifier: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`);
760
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`);
766
761
  }
767
762
  }
768
763
  }
769
764
 
770
- async apiError(e: any): Promise<void> {
771
- this.humidifierService.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, e);
772
- if (!this.device.ble) {
773
- this.humidifierService.updateCharacteristic(this.platform.Characteristic.WaterLevel, e);
765
+ async getCustomBLEAddress(switchbot: any) {
766
+ if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
767
+ (async () => {
768
+ // Start to monitor advertisement packets
769
+ await switchbot.startScan({
770
+ model: 'e',
771
+ });
772
+ // Set an event handler
773
+ switchbot.onadvertisement = (ad: any) => {
774
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${superStringify(ad, null, ' ')}`);
775
+ };
776
+ await switchbot.wait(10000);
777
+ // Stop to monitor
778
+ switchbot.stopScan();
779
+ })();
774
780
  }
775
- this.humidifierService.updateCharacteristic(this.platform.Characteristic.CurrentHumidifierDehumidifierState, e);
776
- this.humidifierService.updateCharacteristic(this.platform.Characteristic.TargetHumidifierDehumidifierState, e);
777
- this.humidifierService.updateCharacteristic(this.platform.Characteristic.Active, e);
778
- this.humidifierService.updateCharacteristic(this.platform.Characteristic.RelativeHumidityHumidifierThreshold, e);
779
- if (!this.device.humidifier?.hide_temperature && !this.device.ble) {
780
- this.temperatureservice?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, e);
781
+ }
782
+
783
+ async BLEPushConnection() {
784
+ if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
785
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Push Changes`);
786
+ await this.openAPIpushChanges();
787
+ }
788
+ }
789
+
790
+ async BLERefreshConnection(switchbot: any): Promise<void> {
791
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
792
+ if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
793
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Refresh Status`);
794
+ await this.openAPIRefreshStatus();
795
+ }
796
+ }
797
+
798
+ minStep(): number {
799
+ if (this.device.humidifier?.set_minStep) {
800
+ this.set_minStep = this.device.humidifier?.set_minStep;
801
+ } else {
802
+ this.set_minStep = 1;
803
+ }
804
+ return this.set_minStep;
805
+ }
806
+
807
+ async scan(device: device & devicesConfig): Promise<void> {
808
+ if (device.scanDuration) {
809
+ this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
810
+ if (this.BLE) {
811
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
812
+ }
813
+ } else {
814
+ this.scanDuration = this.accessory.context.scanDuration = 1;
815
+ if (this.BLE) {
816
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
817
+ }
781
818
  }
782
- //throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
783
819
  }
784
820
 
785
821
  async statusCode({ res }: { res: IncomingMessage }): Promise<void> {
786
822
  switch (res.statusCode) {
787
823
  case 151:
788
- this.errorLog(`Humidifier: ${this.accessory.displayName} Command not supported by this device type.`);
824
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this device type.`);
789
825
  break;
790
826
  case 152:
791
- this.errorLog(`Humidifier: ${this.accessory.displayName} Device not found.`);
827
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found.`);
792
828
  break;
793
829
  case 160:
794
- this.errorLog(`Humidifier: ${this.accessory.displayName} Command is not supported.`);
830
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported.`);
795
831
  break;
796
832
  case 161:
797
- this.errorLog(`Humidifier: ${this.accessory.displayName} Device is offline.`);
833
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline.`);
798
834
  this.offlineOff();
799
835
  break;
800
836
  case 171:
801
- this.errorLog(`Humidifier: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
837
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
802
838
  this.offlineOff();
803
839
  break;
804
840
  case 190:
805
841
  this.errorLog(
806
- `Humidifier: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
807
- ` Or command: ${JSON.stringify(res)} format is invalid`,
842
+ `${this.device.deviceType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
843
+ ` Or command: ${superStringify(res)} format is invalid`,
808
844
  );
809
845
  break;
810
846
  case 100:
811
- this.debugLog(`Humidifier: ${this.accessory.displayName} Command successfully sent.`);
847
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent.`);
812
848
  break;
813
849
  default:
814
- this.debugLog(`Humidifier: ${this.accessory.displayName} Unknown statusCode.`);
850
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode.`);
815
851
  }
816
852
  }
817
853
 
@@ -819,40 +855,86 @@ export class Humidifier {
819
855
  if (this.device.offline) {
820
856
  this.Active = this.platform.Characteristic.Active.INACTIVE;
821
857
  this.humidifierService.getCharacteristic(this.platform.Characteristic.Active).updateValue(this.Active);
858
+ await this.updateHomeKitCharacteristics();
822
859
  }
823
860
  }
824
861
 
825
- /**
826
- * Handle requests to set the "Target Humidifier Dehumidifier State" characteristic
827
- */
828
- async handleTargetHumidifierDehumidifierStateSet(value: CharacteristicValue): Promise<void> {
829
- this.debugLog(`Humidifier: ${this.accessory.displayName} TargetHumidifierDehumidifierState: ${value}`);
830
-
831
- this.TargetHumidifierDehumidifierState = value;
832
- this.doHumidifierUpdate.next();
862
+ async apiError(e: any): Promise<void> {
863
+ this.humidifierService.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, e);
864
+ if (!this.BLE) {
865
+ this.humidifierService.updateCharacteristic(this.platform.Characteristic.WaterLevel, e);
866
+ }
867
+ this.humidifierService.updateCharacteristic(this.platform.Characteristic.CurrentHumidifierDehumidifierState, e);
868
+ this.humidifierService.updateCharacteristic(this.platform.Characteristic.TargetHumidifierDehumidifierState, e);
869
+ this.humidifierService.updateCharacteristic(this.platform.Characteristic.Active, e);
870
+ this.humidifierService.updateCharacteristic(this.platform.Characteristic.RelativeHumidityHumidifierThreshold, e);
871
+ if (!this.device.humidifier?.hide_temperature && !this.BLE) {
872
+ this.temperatureservice?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, e);
873
+ }
833
874
  }
834
875
 
835
- /**
836
- * Handle requests to set the "Active" characteristic
837
- */
838
- async handleActiveSet(value: CharacteristicValue): Promise<void> {
839
- this.debugLog(`Humidifier: ${this.accessory.displayName} Active: ${value}`);
840
- this.Active = value;
841
- this.doHumidifierUpdate.next();
876
+ FirmwareRevision(accessory: PlatformAccessory<Context>, device: device & devicesConfig): CharacteristicValue {
877
+ let FirmwareRevision: string;
878
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
879
+ + ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
880
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
881
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
882
+ if (accessory.context.FirmwareRevision) {
883
+ FirmwareRevision = accessory.context.FirmwareRevision;
884
+ } else if (device.firmware) {
885
+ FirmwareRevision = device.firmware;
886
+ } else {
887
+ FirmwareRevision = this.platform.version;
888
+ }
889
+ return FirmwareRevision;
842
890
  }
843
891
 
844
- /**
845
- * Handle requests to set the "Relative Humidity Humidifier Threshold" characteristic
846
- */
847
- async handleRelativeHumidityHumidifierThresholdSet(value: CharacteristicValue): Promise<void> {
848
- this.debugLog(`Humidifier: ${this.accessory.displayName} RelativeHumidityHumidifierThreshold: ${value}`);
849
-
850
- this.RelativeHumidityHumidifierThreshold = value;
851
- if (this.Active === this.platform.Characteristic.Active.INACTIVE) {
892
+ async context() {
893
+ if (this.Active === undefined) {
852
894
  this.Active = this.platform.Characteristic.Active.ACTIVE;
853
- this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.IDLE;
895
+ } else {
896
+ this.Active = this.accessory.context.Active;
897
+ }
898
+ if (this.CurrentTemperature === undefined) {
899
+ this.CurrentTemperature = 0;
900
+ } else {
901
+ this.CurrentTemperature = this.accessory.context.CurrentTemperature;
902
+ }
903
+ if (this.CurrentRelativeHumidity === undefined) {
904
+ this.CurrentRelativeHumidity = 0;
905
+ } else {
906
+ this.CurrentRelativeHumidity = this.accessory.context.CurrentRelativeHumidity;
907
+ }
908
+ if (this.TargetHumidifierDehumidifierState === undefined) {
909
+ this.TargetHumidifierDehumidifierState = this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER;
910
+ } else {
911
+ this.TargetHumidifierDehumidifierState = this.accessory.context.TargetHumidifierDehumidifierState;
912
+ }
913
+ if (this.CurrentHumidifierDehumidifierState === undefined) {
914
+ this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.INACTIVE;
915
+ } else {
916
+ this.CurrentHumidifierDehumidifierState = this.accessory.context.CurrentHumidifierDehumidifierState;
917
+ }
918
+ if (this.RelativeHumidityHumidifierThreshold === undefined) {
919
+ this.RelativeHumidityHumidifierThreshold = 0;
920
+ } else {
921
+ this.RelativeHumidityHumidifierThreshold = this.accessory.context.RelativeHumidityHumidifierThreshold;
922
+ }
923
+ if (this.WaterLevel === undefined) {
924
+ this.WaterLevel = 0;
925
+ } else {
926
+ this.WaterLevel = this.accessory.context.WaterLevel;
927
+ }
928
+ }
929
+
930
+ async refreshRate(device: device & devicesConfig): Promise<void> {
931
+ if (device.refreshRate) {
932
+ this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
933
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
934
+ } else if (this.platform.config.options!.refreshRate) {
935
+ this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options!.refreshRate;
936
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
854
937
  }
855
- this.doHumidifierUpdate.next();
856
938
  }
857
939
 
858
940
  async config(device: device & devicesConfig): Promise<void> {
@@ -860,8 +942,11 @@ export class Humidifier {
860
942
  if (device.humidifier) {
861
943
  config = device.humidifier;
862
944
  }
863
- if (device.ble !== undefined) {
864
- config['ble'] = device.ble;
945
+ if (device.connectionType !== undefined) {
946
+ config['connectionType'] = device.connectionType;
947
+ }
948
+ if (device.external !== undefined) {
949
+ config['external'] = device.external;
865
950
  }
866
951
  if (device.logging !== undefined) {
867
952
  config['logging'] = device.logging;
@@ -876,72 +961,24 @@ export class Humidifier {
876
961
  config['offline'] = device.offline;
877
962
  }
878
963
  if (Object.entries(config).length !== 0) {
879
- this.infoLog(`Humidifier: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
880
- }
881
- }
882
-
883
- async refreshRate(device: device & devicesConfig): Promise<void> {
884
- if (device.refreshRate) {
885
- this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
886
- this.debugLog(`Humidifier: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
887
- } else if (this.platform.config.options!.refreshRate) {
888
- this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options!.refreshRate;
889
- this.debugLog(`Humidifier: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
890
- }
891
- }
892
-
893
- async scan(device: device & devicesConfig): Promise<void> {
894
- if (device.scanDuration) {
895
- this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
896
- if (device.ble) {
897
- this.debugLog(`Humidifier: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
898
- }
899
- } else {
900
- this.scanDuration = this.accessory.context.scanDuration = 1;
901
- if (this.device.ble) {
902
- this.debugLog(`Humidifier: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
903
- }
964
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${superStringify(config)}`);
904
965
  }
905
966
  }
906
967
 
907
968
  async logs(device: device & devicesConfig): Promise<void> {
908
969
  if (this.platform.debugMode) {
909
970
  this.deviceLogging = this.accessory.context.logging = 'debugMode';
910
- this.debugLog(`Humidifier: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
971
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
911
972
  } else if (device.logging) {
912
973
  this.deviceLogging = this.accessory.context.logging = device.logging;
913
- this.debugLog(`Humidifier: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
974
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
914
975
  } else if (this.platform.config.options?.logging) {
915
976
  this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
916
- this.debugLog(`Humidifier: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
977
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
917
978
  } else {
918
979
  this.deviceLogging = this.accessory.context.logging = 'standard';
919
- this.debugLog(`Humidifier: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
920
- }
921
- }
922
-
923
- FirmwareRevision(accessory: PlatformAccessory<Context>, device: device & devicesConfig): CharacteristicValue {
924
- let FirmwareRevision: string;
925
- this.debugLog(`Humidifier: ${this.accessory.displayName} accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
926
- this.debugLog(`Humidifier: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
927
- this.debugLog(`Humidifier: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
928
- if (accessory.context.FirmwareRevision) {
929
- FirmwareRevision = accessory.context.FirmwareRevision;
930
- } else if (device.firmware) {
931
- FirmwareRevision = device.firmware;
932
- } else {
933
- FirmwareRevision = this.platform.version;
980
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
934
981
  }
935
- return FirmwareRevision;
936
- }
937
-
938
- minStep(): number {
939
- if (this.device.humidifier?.set_minStep) {
940
- this.set_minStep = this.device.humidifier?.set_minStep;
941
- } else {
942
- this.set_minStep = 1;
943
- }
944
- return this.set_minStep;
945
982
  }
946
983
 
947
984
  /**
@@ -959,12 +996,28 @@ export class Humidifier {
959
996
  }
960
997
  }
961
998
 
999
+ debugWarnLog(...log: any[]): void {
1000
+ if (this.enablingDeviceLogging()) {
1001
+ if (this.deviceLogging?.includes('debug')) {
1002
+ this.platform.log.warn('[DEBUG]', String(...log));
1003
+ }
1004
+ }
1005
+ }
1006
+
962
1007
  errorLog(...log: any[]): void {
963
1008
  if (this.enablingDeviceLogging()) {
964
1009
  this.platform.log.error(String(...log));
965
1010
  }
966
1011
  }
967
1012
 
1013
+ debugErrorLog(...log: any[]): void {
1014
+ if (this.enablingDeviceLogging()) {
1015
+ if (this.deviceLogging?.includes('debug')) {
1016
+ this.platform.log.error('[DEBUG]', String(...log));
1017
+ }
1018
+ }
1019
+ }
1020
+
968
1021
  debugLog(...log: any[]): void {
969
1022
  if (this.enablingDeviceLogging()) {
970
1023
  if (this.deviceLogging === 'debug') {