@switchbot/homebridge-switchbot 1.3.1-beta.9 → 1.4.1-beta.2

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 (103) hide show
  1. package/CHANGELOG.md +18 -2
  2. package/config.schema.json +82 -32
  3. package/dist/devices/bots.d.ts +15 -3
  4. package/dist/devices/bots.d.ts.map +1 -1
  5. package/dist/devices/bots.js +227 -165
  6. package/dist/devices/bots.js.map +1 -1
  7. package/dist/devices/colorbulb.d.ts +14 -3
  8. package/dist/devices/colorbulb.d.ts.map +1 -1
  9. package/dist/devices/colorbulb.js +163 -105
  10. package/dist/devices/colorbulb.js.map +1 -1
  11. package/dist/devices/contact.d.ts +13 -3
  12. package/dist/devices/contact.d.ts.map +1 -1
  13. package/dist/devices/contact.js +102 -63
  14. package/dist/devices/contact.js.map +1 -1
  15. package/dist/devices/curtains.d.ts +14 -5
  16. package/dist/devices/curtains.d.ts.map +1 -1
  17. package/dist/devices/curtains.js +180 -140
  18. package/dist/devices/curtains.js.map +1 -1
  19. package/dist/devices/humidifiers.d.ts +14 -3
  20. package/dist/devices/humidifiers.d.ts.map +1 -1
  21. package/dist/devices/humidifiers.js +173 -133
  22. package/dist/devices/humidifiers.js.map +1 -1
  23. package/dist/devices/meters.d.ts +17 -13
  24. package/dist/devices/meters.d.ts.map +1 -1
  25. package/dist/devices/meters.js +150 -105
  26. package/dist/devices/meters.js.map +1 -1
  27. package/dist/devices/motion.d.ts +13 -3
  28. package/dist/devices/motion.d.ts.map +1 -1
  29. package/dist/devices/motion.js +89 -45
  30. package/dist/devices/motion.js.map +1 -1
  31. package/dist/devices/plugs.d.ts +15 -3
  32. package/dist/devices/plugs.d.ts.map +1 -1
  33. package/dist/devices/plugs.js +115 -53
  34. package/dist/devices/plugs.js.map +1 -1
  35. package/dist/irdevices/airconditioners.d.ts +13 -3
  36. package/dist/irdevices/airconditioners.d.ts.map +1 -1
  37. package/dist/irdevices/airconditioners.js +97 -46
  38. package/dist/irdevices/airconditioners.js.map +1 -1
  39. package/dist/irdevices/airpurifiers.d.ts +13 -3
  40. package/dist/irdevices/airpurifiers.d.ts.map +1 -1
  41. package/dist/irdevices/airpurifiers.js +85 -31
  42. package/dist/irdevices/airpurifiers.js.map +1 -1
  43. package/dist/irdevices/cameras.d.ts +12 -3
  44. package/dist/irdevices/cameras.d.ts.map +1 -1
  45. package/dist/irdevices/cameras.js +73 -24
  46. package/dist/irdevices/cameras.js.map +1 -1
  47. package/dist/irdevices/fans.d.ts +12 -3
  48. package/dist/irdevices/fans.d.ts.map +1 -1
  49. package/dist/irdevices/fans.js +83 -35
  50. package/dist/irdevices/fans.js.map +1 -1
  51. package/dist/irdevices/lights.d.ts +12 -3
  52. package/dist/irdevices/lights.d.ts.map +1 -1
  53. package/dist/irdevices/lights.js +74 -25
  54. package/dist/irdevices/lights.js.map +1 -1
  55. package/dist/irdevices/others.d.ts +12 -3
  56. package/dist/irdevices/others.d.ts.map +1 -1
  57. package/dist/irdevices/others.js +80 -31
  58. package/dist/irdevices/others.js.map +1 -1
  59. package/dist/irdevices/tvs.d.ts +12 -3
  60. package/dist/irdevices/tvs.d.ts.map +1 -1
  61. package/dist/irdevices/tvs.js +90 -42
  62. package/dist/irdevices/tvs.js.map +1 -1
  63. package/dist/irdevices/vacuumcleaners.d.ts +12 -3
  64. package/dist/irdevices/vacuumcleaners.d.ts.map +1 -1
  65. package/dist/irdevices/vacuumcleaners.js +73 -24
  66. package/dist/irdevices/vacuumcleaners.js.map +1 -1
  67. package/dist/irdevices/waterheaters.d.ts +12 -3
  68. package/dist/irdevices/waterheaters.d.ts.map +1 -1
  69. package/dist/irdevices/waterheaters.js +73 -24
  70. package/dist/irdevices/waterheaters.js.map +1 -1
  71. package/dist/platform.d.ts +10 -10
  72. package/dist/platform.d.ts.map +1 -1
  73. package/dist/platform.js +290 -307
  74. package/dist/platform.js.map +1 -1
  75. package/dist/settings.d.ts +9 -6
  76. package/dist/settings.d.ts.map +1 -1
  77. package/dist/settings.js.map +1 -1
  78. package/nodemon.json +1 -1
  79. package/package.json +6 -6
  80. package/src/devices/bots.ts +221 -152
  81. package/src/devices/colorbulb.ts +172 -108
  82. package/src/devices/contact.ts +109 -64
  83. package/src/devices/curtains.ts +190 -146
  84. package/src/devices/humidifiers.ts +181 -135
  85. package/src/devices/meters.ts +156 -105
  86. package/src/devices/motion.ts +95 -45
  87. package/src/devices/plugs.ts +121 -53
  88. package/src/irdevices/airconditioners.ts +102 -46
  89. package/src/irdevices/airpurifiers.ts +91 -31
  90. package/src/irdevices/cameras.ts +79 -24
  91. package/src/irdevices/fans.ts +89 -36
  92. package/src/irdevices/lights.ts +80 -25
  93. package/src/irdevices/others.ts +86 -31
  94. package/src/irdevices/tvs.ts +96 -42
  95. package/src/irdevices/vacuumcleaners.ts +79 -24
  96. package/src/irdevices/waterheaters.ts +79 -24
  97. package/src/platform.ts +285 -302
  98. package/src/settings.ts +10 -6
  99. package/dist/devices/indoorcam.d.ts +0 -64
  100. package/dist/devices/indoorcam.d.ts.map +0 -1
  101. package/dist/devices/indoorcam.js +0 -249
  102. package/dist/devices/indoorcam.js.map +0 -1
  103. package/src/devices/indoorcam.ts +0 -309
@@ -3,7 +3,7 @@ import Switchbot from 'node-switchbot';
3
3
  import { interval, Subject } from 'rxjs';
4
4
  import { SwitchBotPlatform } from '../platform';
5
5
  import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
6
- import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
6
+ import { Service, PlatformAccessory, CharacteristicValue, HAPStatus } from 'homebridge';
7
7
  import { DeviceURL, device, devicesConfig, serviceData, ad, deviceStatusResponse, payload } from '../settings';
8
8
 
9
9
  /**
@@ -37,8 +37,8 @@ export class Humidifier {
37
37
 
38
38
  // Config
39
39
  set_minStep?: number;
40
- private readonly deviceDebug = this.platform.config.options?.debug === 'device' || this.platform.debugMode;
41
- private readonly debugDebug = this.platform.config.options?.debug === 'debug' || this.platform.debugMode;
40
+ deviceLogging!: string;
41
+ deviceRefreshRate!: number;
42
42
 
43
43
  // Updates
44
44
  humidifierUpdateInProgress!: boolean;
@@ -49,11 +49,9 @@ export class Humidifier {
49
49
  private accessory: PlatformAccessory,
50
50
  public device: device & devicesConfig,
51
51
  ) {
52
- // Humidifiers Config
53
- this.platform.device(`Humidifier: ${this.accessory.displayName} Config: (ble: ${device.ble}, hide_temperature: `
54
- + `${device.humidifier?.hide_temperature}, set_minStep: ${device.humidifier?.set_minStep})`);
55
-
56
52
  // default placeholders
53
+ this.refreshRate();
54
+ this.logs();
57
55
  this.CurrentRelativeHumidity = 0;
58
56
  this.TargetHumidifierDehumidifierState = this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER;
59
57
  this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.INACTIVE;
@@ -62,6 +60,10 @@ export class Humidifier {
62
60
  this.CurrentTemperature = 0;
63
61
  this.WaterLevel = 0;
64
62
 
63
+ // Humidifiers Config
64
+ this.debugLog(`Humidifier: ${this.accessory.displayName} Config: (ble: ${device.ble}, offline: ${device.offline}, hide_temperature: `
65
+ + `${device.humidifier?.hide_temperature}, set_minStep: ${device.humidifier?.set_minStep})`);
66
+
65
67
  // this is subject we use to track when we need to POST changes to the SwitchBot API
66
68
  this.doHumidifierUpdate = new Subject();
67
69
  this.humidifierUpdateInProgress = false;
@@ -124,11 +126,11 @@ export class Humidifier {
124
126
  // create a new Temperature Sensor service
125
127
  // Temperature Sensor Service
126
128
  if (device.humidifier?.hide_temperature || device.ble) {
127
- this.platform.device(`Humidifier: ${accessory.displayName} Removing Temperature Sensor Service`);
129
+ this.debugLog(`Humidifier: ${accessory.displayName} Removing Temperature Sensor Service`);
128
130
  this.temperatureservice = this.accessory.getService(this.platform.Service.TemperatureSensor);
129
131
  accessory.removeService(this.temperatureservice!);
130
132
  } else if (!this.temperatureservice && !device.ble) {
131
- this.platform.device(`Humidifier: ${accessory.displayName} Add Temperature Sensor Service`);
133
+ this.debugLog(`Humidifier: ${accessory.displayName} Add Temperature Sensor Service`);
132
134
  (this.temperatureservice =
133
135
  this.accessory.getService(this.platform.Service.TemperatureSensor) ||
134
136
  this.accessory.addService(this.platform.Service.TemperatureSensor)), `${accessory.displayName} Temperature Sensor`;
@@ -147,16 +149,14 @@ export class Humidifier {
147
149
  return Number.isNaN(this.CurrentTemperature);
148
150
  });
149
151
  } else {
150
- if (this.platform.config.options?.debug) {
151
- this.platform.device(`Humidifier: ${accessory.displayName} Temperature Sensor Service Not Added`);
152
- }
152
+ this.debugLog(`Humidifier: ${accessory.displayName} Temperature Sensor Service Not Added`);
153
153
  }
154
154
 
155
155
  // Retrieve initial values and updateHomekit
156
156
  this.updateHomeKitCharacteristics();
157
157
 
158
158
  // Start an update interval
159
- interval(this.platform.config.options!.refreshRate! * 1000)
159
+ interval(this.deviceRefreshRate * 1000)
160
160
  .pipe(skipWhile(() => this.humidifierUpdateInProgress))
161
161
  .subscribe(() => {
162
162
  this.refreshStatus();
@@ -175,21 +175,39 @@ export class Humidifier {
175
175
  try {
176
176
  await this.pushChanges();
177
177
  } catch (e: any) {
178
- this.platform.log.error(`Humidifier: ${this.accessory.displayName} failed pushChanges`);
179
- if (this.deviceDebug) {
180
- this.platform.log.error(`Humidifier: ${this.accessory.displayName} failed pushChanges,`
178
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushChanges`);
179
+ if (this.deviceLogging === 'debug') {
180
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushChanges,`
181
181
  + ` Error Message: ${JSON.stringify(e.message)}`);
182
182
  }
183
- if (this.debugDebug) {
184
- this.platform.log.error(`Humidifier: ${this.accessory.displayName} failed pushChanges,`
183
+ if (this.platform.debugMode) {
184
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushChanges,`
185
185
  + ` Error: ${JSON.stringify(e)}`);
186
186
  }
187
- this.apiError(e);
187
+ this.apiError();
188
188
  }
189
189
  this.humidifierUpdateInProgress = false;
190
190
  });
191
191
  }
192
192
 
193
+ refreshRate() {
194
+ if (this.device.refreshRate) {
195
+ this.deviceRefreshRate = this.accessory.context.refreshRate = this.device.refreshRate;
196
+ } else if (this.platform.config.options!.refreshRate) {
197
+ this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options!.refreshRate;
198
+ }
199
+ }
200
+
201
+ logs() {
202
+ if (this.device.logging) {
203
+ this.deviceLogging = this.accessory.context.logging = this.device.logging;
204
+ } else if (this.platform.config.options?.logging) {
205
+ this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
206
+ } else {
207
+ this.deviceLogging = this.accessory.context.logging = 'standard';
208
+ }
209
+ }
210
+
193
211
  private minStep(): number | undefined {
194
212
  if (this.device.humidifier?.set_minStep) {
195
213
  this.set_minStep = this.device.humidifier?.set_minStep;
@@ -211,29 +229,29 @@ export class Humidifier {
211
229
  }
212
230
 
213
231
  private BLEparseStatus() {
214
- this.platform.debug(`Humidifier: ${this.accessory.displayName} BLE parseStatus`);
232
+ this.debugLog(`Humidifier: ${this.accessory.displayName} BLE parseStatus`);
215
233
  // Current Relative Humidity
216
234
  this.CurrentRelativeHumidity = this.percentage!;
217
- this.platform.debug(`Humidifier: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
235
+ this.debugLog(`Humidifier: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
218
236
  // Active
219
237
  if (this.onState) {
220
238
  this.Active = this.platform.Characteristic.Active.ACTIVE;
221
239
  } else {
222
240
  this.Active = this.platform.Characteristic.Active.INACTIVE;
223
241
  }
224
- this.platform.debug(`Humidifier: ${this.accessory.displayName} Active: ${this.Active}`);
242
+ this.debugLog(`Humidifier: ${this.accessory.displayName} Active: ${this.Active}`);
225
243
  }
226
244
 
227
245
  private openAPIparseStatus() {
228
246
  if (this.platform.config.credentials?.openToken) {
229
- this.platform.debug(`Humidifier: ${this.accessory.displayName} OpenAPI parseStatus`);
247
+ this.debugLog(`Humidifier: ${this.accessory.displayName} OpenAPI parseStatus`);
230
248
  // Current Relative Humidity
231
249
  this.CurrentRelativeHumidity = this.deviceStatus.body.humidity!;
232
- this.platform.debug(`Humidifier: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
250
+ this.debugLog(`Humidifier: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
233
251
  // Current Temperature
234
252
  if (!this.device.humidifier?.hide_temperature) {
235
253
  this.CurrentTemperature = Number(this.deviceStatus.body.temperature);
236
- this.platform.debug(`Humidifier: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
254
+ this.debugLog(`Humidifier: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
237
255
  }
238
256
  // Target Humidifier Dehumidifier State
239
257
  switch (this.deviceStatus.body.auto) {
@@ -257,10 +275,10 @@ export class Humidifier {
257
275
  this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.HUMIDIFYING;
258
276
  }
259
277
  }
260
- this.platform.debug(`Humidifier: ${this.accessory.displayName} TargetHumidifierDehumidifierState: ${this.TargetHumidifierDehumidifierState}`);
261
- this.platform.debug(`Humidifier: ${this.accessory.displayName}`
278
+ this.debugLog(`Humidifier: ${this.accessory.displayName} TargetHumidifierDehumidifierState: ${this.TargetHumidifierDehumidifierState}`);
279
+ this.debugLog(`Humidifier: ${this.accessory.displayName}`
262
280
  + ` RelativeHumidityHumidifierThreshold: ${this.RelativeHumidityHumidifierThreshold}`);
263
- this.platform.debug(`Humidifier: ${this.accessory.displayName} CurrentHumidifierDehumidifierState: ${this.CurrentHumidifierDehumidifierState}`);
281
+ this.debugLog(`Humidifier: ${this.accessory.displayName} CurrentHumidifierDehumidifierState: ${this.CurrentHumidifierDehumidifierState}`);
264
282
  // Active
265
283
  switch (this.deviceStatus.body.power) {
266
284
  case 'on':
@@ -269,14 +287,14 @@ export class Humidifier {
269
287
  default:
270
288
  this.Active = this.platform.Characteristic.Active.INACTIVE;
271
289
  }
272
- this.platform.debug(`Humidifier: ${this.accessory.displayName} Active: ${this.Active}`);
290
+ this.debugLog(`Humidifier: ${this.accessory.displayName} Active: ${this.Active}`);
273
291
  // Water Level
274
292
  if (this.deviceStatus.body.lackWater) {
275
293
  this.WaterLevel = 0;
276
294
  } else {
277
295
  this.WaterLevel = 100;
278
296
  }
279
- this.platform.debug(`Humidifier: ${this.accessory.displayName} WaterLevel: ${this.WaterLevel}`);
297
+ this.debugLog(`Humidifier: ${this.accessory.displayName} WaterLevel: ${this.WaterLevel}`);
280
298
  }
281
299
  }
282
300
 
@@ -294,12 +312,12 @@ export class Humidifier {
294
312
  private connectBLE() {
295
313
  const switchbot = new Switchbot();
296
314
  this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
297
- this.platform.device(`Humidifier: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
315
+ this.debugLog(`Humidifier: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
298
316
  return switchbot;
299
317
  }
300
318
 
301
319
  private async BLERefreshStatus() {
302
- this.platform.device(`Humidifier: ${this.accessory.displayName} BLE refreshStatus`);
320
+ this.debugLog(`Humidifier: ${this.accessory.displayName} BLE refreshStatus`);
303
321
  // eslint-disable-next-line @typescript-eslint/no-var-requires
304
322
  const switchbot = this.connectBLE();
305
323
  // Start to monitor advertisement packets
@@ -313,16 +331,16 @@ export class Humidifier {
313
331
  this.autoMode = ad.serviceData.autoMode;
314
332
  this.onState = ad.serviceData.onState;
315
333
  this.percentage = ad.serviceData.percentage;
316
- this.platform.device(`Humidifier: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
317
- this.platform.device(`Humidifier: ${this.accessory.displayName} model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName},`
334
+ this.debugLog(`Humidifier: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
335
+ this.debugLog(`Humidifier: ${this.accessory.displayName} model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName},`
318
336
  + `autoMode: ${ad.serviceData.autoMode}, onState: ${ad.serviceData.onState}, percentage: ${ad.serviceData.percentage}`);
319
337
 
320
338
  if (this.serviceData) {
321
339
  this.connected = true;
322
- this.platform.device(`Humidifier: ${this.accessory.displayName} connected: ${this.connected}`);
340
+ this.debugLog(`Humidifier: ${this.accessory.displayName} connected: ${this.connected}`);
323
341
  } else {
324
342
  this.connected = false;
325
- this.platform.device(`Humidifier: ${this.accessory.displayName} connected: ${this.connected}`);
343
+ this.debugLog(`Humidifier: ${this.accessory.displayName} connected: ${this.connected}`);
326
344
  }
327
345
  };
328
346
  // Wait 10 seconds
@@ -334,53 +352,53 @@ export class Humidifier {
334
352
  this.parseStatus();
335
353
  this.updateHomeKitCharacteristics();
336
354
  } else {
337
- this.platform.log.error(`Humidifier: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
355
+ this.errorLog(`Humidifier: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
338
356
  if (this.platform.config.credentials?.openToken) {
339
- this.platform.log.warn(`Humidifier: ${this.accessory.displayName} Using OpenAPI Connection`);
357
+ this.warnLog(`Humidifier: ${this.accessory.displayName} Using OpenAPI Connection`);
340
358
  await this.openAPIRefreshStatus();
341
359
  }
342
360
  }
343
361
  }).catch(async (e: any) => {
344
- this.platform.log.error(`Humidifier: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
345
- if (this.deviceDebug) {
346
- this.platform.log.error(`Humidifier: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
362
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
363
+ if (this.deviceLogging === 'debug') {
364
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
347
365
  + ` Error Message: ${JSON.stringify(e.message)}`);
348
366
  }
349
- if (this.debugDebug) {
350
- this.platform.log.error(`Humidifier: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
367
+ if (this.platform.debugMode) {
368
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
351
369
  + ` Error: ${JSON.stringify(e)}`);
352
370
  }
353
371
  if (this.platform.config.credentials?.openToken) {
354
- this.platform.log.warn(`Humidifier: ${this.accessory.displayName} Using OpenAPI Connection`);
372
+ this.warnLog(`Humidifier: ${this.accessory.displayName} Using OpenAPI Connection`);
355
373
  await this.openAPIRefreshStatus();
356
374
  }
357
- this.apiError(e);
375
+ this.apiError();
358
376
  });
359
377
  }
360
378
 
361
379
  private async openAPIRefreshStatus() {
362
380
  if (this.platform.config.credentials?.openToken) {
363
- this.platform.device(`Humidifier: ${this.accessory.displayName} OpenAPI refreshStatus`);
381
+ this.debugLog(`Humidifier: ${this.accessory.displayName} OpenAPI refreshStatus`);
364
382
  try {
365
383
  this.deviceStatus = (await this.platform.axios.get(`${DeviceURL}/${this.device.deviceId}/status`)).data;
366
384
  if (this.deviceStatus.message === 'success') {
367
- this.platform.debug(`Humidifier: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(this.deviceStatus)}`);
385
+ this.debugLog(`Humidifier: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(this.deviceStatus)}`);
368
386
  this.parseStatus();
369
387
  this.updateHomeKitCharacteristics();
370
388
  } else {
371
- this.platform.debug(this.deviceStatus);
389
+ this.debugLog(this.deviceStatus);
372
390
  }
373
391
  } catch (e: any) {
374
- this.platform.log.error(`Humidifier: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
375
- if (this.deviceDebug) {
376
- this.platform.log.error(`Humidifier: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
392
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
393
+ if (this.deviceLogging === 'debug') {
394
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
377
395
  + ` Error Message: ${JSON.stringify(e.message)}`);
378
396
  }
379
- if (this.debugDebug) {
380
- this.platform.log.error(`Humidifier: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
397
+ if (this.platform.debugMode) {
398
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
381
399
  + ` Error: ${JSON.stringify(e)}`);
382
400
  }
383
- this.apiError(e);
401
+ this.apiError();
384
402
  }
385
403
  }
386
404
  }
@@ -403,65 +421,65 @@ export class Humidifier {
403
421
  }
404
422
 
405
423
  private async BLEpushChanges() {
406
- this.platform.debug(`Humidifier: ${this.accessory.displayName} BLE pushChanges`);
424
+ this.debugLog(`Humidifier: ${this.accessory.displayName} BLE pushChanges`);
407
425
  const switchbot = this.connectBLE();
408
426
  switchbot.discover({ model: 'e', quick: true, id: this.device.bleMac }).then((device_list) => {
409
- this.platform.log.info(`${this.accessory.displayName} Target Position: ${this.Active}`);
427
+ this.infoLog(`${this.accessory.displayName} Target Position: ${this.Active}`);
410
428
  return device_list[0].percentage(this.RelativeHumidityHumidifierThreshold);
411
429
  }).then(() => {
412
- this.platform.device(`Humidifier: ${this.accessory.displayName} Done.`);
430
+ this.debugLog(`Humidifier: ${this.accessory.displayName} Done.`);
413
431
  }).catch(async (e: any) => {
414
- this.platform.log.error(`Humidifier: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
415
- if (this.deviceDebug) {
416
- this.platform.log.error(`Humidifier: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
432
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
433
+ if (this.deviceLogging === 'debug') {
434
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
417
435
  + ` Error Message: ${JSON.stringify(e.message)}`);
418
436
  }
419
- if (this.debugDebug) {
420
- this.platform.log.error(`Humidifier: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
437
+ if (this.platform.debugMode) {
438
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
421
439
  + ` Error: ${JSON.stringify(e)}`);
422
440
  }
423
441
  if (this.platform.config.credentials?.openToken) {
424
- this.platform.log.warn(`Humidifier: ${this.accessory.displayName} Using OpenAPI Connection`);
442
+ this.warnLog(`Humidifier: ${this.accessory.displayName} Using OpenAPI Connection`);
425
443
  await this.OpenAPIpushChanges();
426
444
  }
427
- this.apiError(e);
445
+ this.apiError();
428
446
  });
429
447
  }
430
448
 
431
449
  private async OpenAPIpushChanges() {
432
450
  if (this.platform.config.credentials?.openToken) {
433
- this.platform.device(`Humidifier: ${this.accessory.displayName} OpenAPI pushChanges`);
451
+ this.debugLog(`Humidifier: ${this.accessory.displayName} OpenAPI pushChanges`);
434
452
  if (
435
453
  this.TargetHumidifierDehumidifierState ===
436
454
  this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER &&
437
455
  this.Active === this.platform.Characteristic.Active.ACTIVE
438
456
  ) {
439
457
  try {
440
- this.platform.debug(`Pushing Manual: ${this.RelativeHumidityHumidifierThreshold}!`);
458
+ this.debugLog(`Pushing Manual: ${this.RelativeHumidityHumidifierThreshold}!`);
441
459
  const payload = {
442
460
  commandType: 'command',
443
461
  command: 'setMode',
444
462
  parameter: `${this.RelativeHumidityHumidifierThreshold}`,
445
463
  } as payload;
446
464
 
447
- this.platform.log.info(`Humidifier: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
465
+ this.infoLog(`Humidifier: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
448
466
  + ` parameter: ${payload.parameter}, commandType: ${payload.commandType}`);
449
467
 
450
468
  // Make the API request
451
469
  const push: any = (await this.platform.axios.post(`${DeviceURL}/${this.device.deviceId}/commands`, payload));
452
- this.platform.debug(`Humidifier: ${this.accessory.displayName} pushChanges: ${JSON.stringify(push.data)}`);
470
+ this.debugLog(`Humidifier: ${this.accessory.displayName} pushChanges: ${JSON.stringify(push.data)}`);
453
471
  this.statusCode(push);
454
472
  } catch (e: any) {
455
- this.platform.log.error(`Humidifier: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
456
- if (this.deviceDebug) {
457
- this.platform.log.error(`Humidifier: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
473
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
474
+ if (this.deviceLogging === 'debug') {
475
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
458
476
  + ` Error Message: ${JSON.stringify(e.message)}`);
459
477
  }
460
- if (this.debugDebug) {
461
- this.platform.log.error(`Humidifier: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
478
+ if (this.platform.debugMode) {
479
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
462
480
  + ` Error: ${JSON.stringify(e)}`);
463
481
  }
464
- this.apiError(e);
482
+ this.apiError();
465
483
  }
466
484
  } else if (
467
485
  this.TargetHumidifierDehumidifierState ===
@@ -485,32 +503,32 @@ export class Humidifier {
485
503
  this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER_OR_DEHUMIDIFIER &&
486
504
  this.Active === this.platform.Characteristic.Active.ACTIVE
487
505
  ) {
488
- this.platform.debug('Pushing Auto!');
506
+ this.debugLog('Pushing Auto!');
489
507
  const payload = {
490
508
  commandType: 'command',
491
509
  command: 'setMode',
492
510
  parameter: 'auto',
493
511
  } as payload;
494
512
 
495
- this.platform.log.info(`Humidifier: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
513
+ this.infoLog(`Humidifier: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
496
514
  + ` parameter: ${payload.parameter}, commandType: ${payload.commandType}`);
497
515
 
498
516
  // Make the API request
499
517
  const push: any = (await this.platform.axios.post(`${DeviceURL}/${this.device.deviceId}/commands`, payload));
500
- this.platform.debug(`Humidifier: ${this.accessory.displayName} pushAutoChanges: ${JSON.stringify(push.data)}`);
518
+ this.debugLog(`Humidifier: ${this.accessory.displayName} pushAutoChanges: ${JSON.stringify(push.data)}`);
501
519
  this.statusCode(push);
502
520
  }
503
521
  } catch (e: any) {
504
- this.platform.log.error(`Humidifier: ${this.accessory.displayName} failed pushAutoChanges with OpenAPI Connection`);
505
- if (this.deviceDebug) {
506
- this.platform.log.error(`Humidifier: ${this.accessory.displayName} failed pushAutoChanges with OpenAPI Connection,`
522
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushAutoChanges with OpenAPI Connection`);
523
+ if (this.deviceLogging === 'debug') {
524
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushAutoChanges with OpenAPI Connection,`
507
525
  + ` Error Message: ${JSON.stringify(e.message)}`);
508
526
  }
509
- if (this.debugDebug) {
510
- this.platform.log.error(`Humidifier: ${this.accessory.displayName} failed pushAutoChanges with OpenAPI Connection,`
527
+ if (this.platform.debugMode) {
528
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushAutoChanges with OpenAPI Connection,`
511
529
  + ` Error: ${JSON.stringify(e)}`);
512
530
  }
513
- this.apiError(e);
531
+ this.apiError();
514
532
  }
515
533
  }
516
534
 
@@ -520,32 +538,32 @@ export class Humidifier {
520
538
  async pushActiveChanges() {
521
539
  try {
522
540
  if (this.Active === this.platform.Characteristic.Active.INACTIVE) {
523
- this.platform.debug('Pushing Off!');
541
+ this.debugLog('Pushing Off!');
524
542
  const payload = {
525
543
  commandType: 'command',
526
544
  command: 'turnOff',
527
545
  parameter: 'default',
528
546
  } as payload;
529
547
 
530
- this.platform.log.info(`Humidifier: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
548
+ this.infoLog(`Humidifier: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
531
549
  + ` parameter: ${payload.parameter}, commandType: ${payload.commandType}`);
532
550
 
533
551
  // Make the API request
534
552
  const push: any = (await this.platform.axios.post(`${DeviceURL}/${this.device.deviceId}/commands`, payload));
535
- this.platform.debug(`Humidifier: ${this.accessory.displayName} pushActiveChanges: ${JSON.stringify(push.data)}`);
553
+ this.debugLog(`Humidifier: ${this.accessory.displayName} pushActiveChanges: ${JSON.stringify(push.data)}`);
536
554
  this.statusCode(push);
537
555
  }
538
556
  } catch (e: any) {
539
- this.platform.log.error(`Humidifier: ${this.accessory.displayName} failed pushActiveChanges with OpenAPI Connection`);
540
- if (this.deviceDebug) {
541
- this.platform.log.error(`Humidifier: ${this.accessory.displayName} failed pushActiveChanges with OpenAPI Connection,`
557
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushActiveChanges with OpenAPI Connection`);
558
+ if (this.deviceLogging === 'debug') {
559
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushActiveChanges with OpenAPI Connection,`
542
560
  + ` Error Message: ${JSON.stringify(e.message)}`);
543
561
  }
544
- if (this.debugDebug) {
545
- this.platform.log.error(`Humidifier: ${this.accessory.displayName} failed pushActiveChanges with OpenAPI Connection,`
562
+ if (this.platform.debugMode) {
563
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushActiveChanges with OpenAPI Connection,`
546
564
  + ` Error: ${JSON.stringify(e)}`);
547
565
  }
548
- this.apiError(e);
566
+ this.apiError();
549
567
  }
550
568
  }
551
569
 
@@ -554,104 +572,97 @@ export class Humidifier {
554
572
  */
555
573
  updateHomeKitCharacteristics() {
556
574
  if (this.CurrentRelativeHumidity === undefined) {
557
- this.platform.debug(`Humidifier: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
575
+ this.debugLog(`Humidifier: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
558
576
  } else {
559
577
  this.service.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.CurrentRelativeHumidity);
560
- this.platform.device(`Humidifier: ${this.accessory.displayName}`
578
+ this.debugLog(`Humidifier: ${this.accessory.displayName}`
561
579
  + ` updateCharacteristic CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
562
580
  }
563
581
  if (!this.device.ble) {
564
582
  if (this.WaterLevel === undefined) {
565
- this.platform.debug(`Humidifier: ${this.accessory.displayName} WaterLevel: ${this.WaterLevel}`);
583
+ this.debugLog(`Humidifier: ${this.accessory.displayName} WaterLevel: ${this.WaterLevel}`);
566
584
  } else {
567
585
  this.service.updateCharacteristic(this.platform.Characteristic.WaterLevel, this.WaterLevel);
568
- this.platform.device(`Humidifier: ${this.accessory.displayName} updateCharacteristic WaterLevel: ${this.WaterLevel}`);
586
+ this.debugLog(`Humidifier: ${this.accessory.displayName} updateCharacteristic WaterLevel: ${this.WaterLevel}`);
569
587
  }
570
588
  }
571
589
  if (this.CurrentHumidifierDehumidifierState === undefined) {
572
- this.platform.debug(`Humidifier: ${this.accessory.displayName} CurrentHumidifierDehumidifierState: ${this.CurrentHumidifierDehumidifierState}`);
590
+ this.debugLog(`Humidifier: ${this.accessory.displayName} CurrentHumidifierDehumidifierState: ${this.CurrentHumidifierDehumidifierState}`);
573
591
  } else {
574
592
  this.service.updateCharacteristic(this.platform.Characteristic.CurrentHumidifierDehumidifierState, this.CurrentHumidifierDehumidifierState);
575
- this.platform.device(`Humidifier: ${this.accessory.displayName}`
593
+ this.debugLog(`Humidifier: ${this.accessory.displayName}`
576
594
  + ` updateCharacteristic CurrentHumidifierDehumidifierState: ${this.CurrentHumidifierDehumidifierState}`);
577
595
  }
578
596
  if (this.TargetHumidifierDehumidifierState === undefined) {
579
- this.platform.debug(`Humidifier: ${this.accessory.displayName} TargetHumidifierDehumidifierState: ${this.TargetHumidifierDehumidifierState}`);
597
+ this.debugLog(`Humidifier: ${this.accessory.displayName} TargetHumidifierDehumidifierState: ${this.TargetHumidifierDehumidifierState}`);
580
598
  } else {
581
599
  this.service.updateCharacteristic(this.platform.Characteristic.TargetHumidifierDehumidifierState, this.TargetHumidifierDehumidifierState);
582
- this.platform.device(`Humidifier: ${this.accessory.displayName}`
600
+ this.debugLog(`Humidifier: ${this.accessory.displayName}`
583
601
  + ` updateCharacteristic TargetHumidifierDehumidifierState: ${this.TargetHumidifierDehumidifierState}`);
584
602
  }
585
603
  if (this.Active === undefined) {
586
- this.platform.debug(`Humidifier: ${this.accessory.displayName} Active: ${this.Active}`);
604
+ this.debugLog(`Humidifier: ${this.accessory.displayName} Active: ${this.Active}`);
587
605
  } else {
588
606
  this.service.updateCharacteristic(this.platform.Characteristic.Active, this.Active);
589
- this.platform.device(`Humidifier: ${this.accessory.displayName} updateCharacteristic Active: ${this.Active}`);
607
+ this.debugLog(`Humidifier: ${this.accessory.displayName} updateCharacteristic Active: ${this.Active}`);
590
608
  }
591
609
  if (this.RelativeHumidityHumidifierThreshold === undefined) {
592
- this.platform.debug(`Humidifier: ${this.accessory.displayName}`
610
+ this.debugLog(`Humidifier: ${this.accessory.displayName}`
593
611
  + ` RelativeHumidityHumidifierThreshold: ${this.RelativeHumidityHumidifierThreshold}`);
594
612
  } else {
595
613
  this.service.updateCharacteristic(this.platform.Characteristic.RelativeHumidityHumidifierThreshold, this.RelativeHumidityHumidifierThreshold);
596
- this.platform.device(`Humidifier: ${this.accessory.displayName}`
614
+ this.debugLog(`Humidifier: ${this.accessory.displayName}`
597
615
  + ` updateCharacteristic RelativeHumidityHumidifierThreshold: ${this.RelativeHumidityHumidifierThreshold}`);
598
616
  }
599
617
  if (!this.device.humidifier?.hide_temperature && !this.device.ble) {
600
618
  if (this.CurrentTemperature === undefined) {
601
- this.platform.debug(`Humidifier: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
619
+ this.debugLog(`Humidifier: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
602
620
  } else {
603
621
  this.temperatureservice?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.CurrentTemperature);
604
- this.platform.device(`Humidifier: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`);
622
+ this.debugLog(`Humidifier: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`);
605
623
  }
606
624
  }
607
625
  }
608
626
 
609
- public apiError(e: any) {
610
- this.service.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, e);
611
- if (!this.device.ble) {
612
- this.service.updateCharacteristic(this.platform.Characteristic.WaterLevel, e);
613
- }
614
- this.service.updateCharacteristic(this.platform.Characteristic.CurrentHumidifierDehumidifierState, e);
615
- this.service.updateCharacteristic(this.platform.Characteristic.TargetHumidifierDehumidifierState, e);
616
- this.service.updateCharacteristic(this.platform.Characteristic.Active, e);
617
- this.service.updateCharacteristic(this.platform.Characteristic.RelativeHumidityHumidifierThreshold, e);
618
- if (!this.device.humidifier?.hide_temperature && !this.device.ble) {
619
- this.temperatureservice?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, e);
620
- }
627
+ public apiError() {
628
+ throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
621
629
  }
622
630
 
623
631
  private statusCode(push: AxiosResponse<{ statusCode: number; }>) {
624
632
  switch (push.data.statusCode) {
625
633
  case 151:
626
- this.platform.log.error(`Humidifier: ${this.accessory.displayName} Command not supported by this device type.`);
634
+ this.errorLog(`Humidifier: ${this.accessory.displayName} Command not supported by this device type.`);
627
635
  break;
628
636
  case 152:
629
- this.platform.log.error(`Humidifier: ${this.accessory.displayName} Device not found.`);
637
+ this.errorLog(`Humidifier: ${this.accessory.displayName} Device not found.`);
630
638
  break;
631
639
  case 160:
632
- this.platform.log.error(`Humidifier: ${this.accessory.displayName} Command is not supported.`);
640
+ this.errorLog(`Humidifier: ${this.accessory.displayName} Command is not supported.`);
633
641
  break;
634
642
  case 161:
635
- this.platform.log.error(`Humidifier: ${this.accessory.displayName} Device is offline.`);
636
- if (this.device.offline) {
637
- this.Active = false;
638
- }
643
+ this.errorLog(`Humidifier: ${this.accessory.displayName} Device is offline.`);
644
+ this.offlineOff();
639
645
  break;
640
646
  case 171:
641
- this.platform.log.error(`Humidifier: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
642
- if (this.device.offline) {
643
- this.Active = false;
644
- }
647
+ this.errorLog(`Humidifier: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
648
+ this.offlineOff();
645
649
  break;
646
650
  case 190:
647
- this.platform.log.error(`Humidifier: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,`
651
+ this.errorLog(`Humidifier: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,`
648
652
  + ` Or command: ${JSON.stringify(push.data)} format is invalid`);
649
653
  break;
650
654
  case 100:
651
- this.platform.debug(`Humidifier: ${this.accessory.displayName} Command successfully sent.`);
655
+ this.debugLog(`Humidifier: ${this.accessory.displayName} Command successfully sent.`);
652
656
  break;
653
657
  default:
654
- this.platform.debug(`Humidifier: ${this.accessory.displayName} Unknown statusCode.`);
658
+ this.debugLog(`Humidifier: ${this.accessory.displayName} Unknown statusCode.`);
659
+ }
660
+ }
661
+
662
+ private offlineOff() {
663
+ if (this.device.offline) {
664
+ this.Active = this.platform.Characteristic.Active.INACTIVE;
665
+ this.service.getCharacteristic(this.platform.Characteristic.Active).updateValue(this.Active);
655
666
  }
656
667
  }
657
668
 
@@ -659,7 +670,7 @@ export class Humidifier {
659
670
  * Handle requests to set the "Target Humidifier Dehumidifier State" characteristic
660
671
  */
661
672
  private handleTargetHumidifierDehumidifierStateSet(value: CharacteristicValue) {
662
- this.platform.debug(`Humidifier: ${this.accessory.displayName} TargetHumidifierDehumidifierState: ${value}`);
673
+ this.debugLog(`Humidifier: ${this.accessory.displayName} TargetHumidifierDehumidifierState: ${value}`);
663
674
 
664
675
  this.TargetHumidifierDehumidifierState = value;
665
676
  this.doHumidifierUpdate.next();
@@ -669,7 +680,7 @@ export class Humidifier {
669
680
  * Handle requests to set the "Active" characteristic
670
681
  */
671
682
  private handleActiveSet(value: CharacteristicValue) {
672
- this.platform.debug(`Humidifier: ${this.accessory.displayName} Active: ${value}`);
683
+ this.debugLog(`Humidifier: ${this.accessory.displayName} Active: ${value}`);
673
684
  this.Active = value;
674
685
  this.doHumidifierUpdate.next();
675
686
  }
@@ -678,7 +689,7 @@ export class Humidifier {
678
689
  * Handle requests to set the "Relative Humidity Humidifier Threshold" characteristic
679
690
  */
680
691
  private handleRelativeHumidityHumidifierThresholdSet(value: CharacteristicValue) {
681
- this.platform.debug(`Humidifier: ${this.accessory.displayName} RelativeHumidityHumidifierThreshold: ${value}`);
692
+ this.debugLog(`Humidifier: ${this.accessory.displayName} RelativeHumidityHumidifierThreshold: ${value}`);
682
693
 
683
694
  this.RelativeHumidityHumidifierThreshold = value;
684
695
  if (this.Active === this.platform.Characteristic.Active.INACTIVE) {
@@ -687,4 +698,39 @@ export class Humidifier {
687
698
  }
688
699
  this.doHumidifierUpdate.next();
689
700
  }
701
+
702
+ /**
703
+ * Logging for Device
704
+ */
705
+ infoLog(...log: any[]) {
706
+ if (this.enablingDeviceLogging()) {
707
+ this.platform.log.info(String(...log));
708
+ }
709
+ }
710
+
711
+ warnLog(...log: any[]) {
712
+ if (this.enablingDeviceLogging()) {
713
+ this.platform.log.warn(String(...log));
714
+ }
715
+ }
716
+
717
+ errorLog(...log: any[]) {
718
+ if (this.enablingDeviceLogging()) {
719
+ this.platform.log.error(String(...log));
720
+ }
721
+ }
722
+
723
+ debugLog(...log: any[]) {
724
+ if (this.enablingDeviceLogging()) {
725
+ if (this.deviceLogging === 'debug') {
726
+ this.platform.log.info('[DEBUG]', String(...log));
727
+ } else {
728
+ this.platform.log.debug(String(...log));
729
+ }
730
+ }
731
+ }
732
+
733
+ enablingDeviceLogging(): boolean {
734
+ return this.deviceLogging === 'debug' || this.deviceLogging === 'standard';
735
+ }
690
736
  }