@switchbot/homebridge-switchbot 1.4.0 → 1.4.1-beta.3

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 -0
  2. package/config.schema.json +83 -11
  3. package/dist/devices/bots.d.ts +13 -3
  4. package/dist/devices/bots.d.ts.map +1 -1
  5. package/dist/devices/bots.js +162 -105
  6. package/dist/devices/bots.js.map +1 -1
  7. package/dist/devices/colorbulb.d.ts +13 -3
  8. package/dist/devices/colorbulb.d.ts.map +1 -1
  9. package/dist/devices/colorbulb.js +161 -96
  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 +117 -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 +198 -140
  18. package/dist/devices/curtains.js.map +1 -1
  19. package/dist/devices/humidifiers.d.ts +13 -3
  20. package/dist/devices/humidifiers.d.ts.map +1 -1
  21. package/dist/devices/humidifiers.js +180 -127
  22. package/dist/devices/humidifiers.js.map +1 -1
  23. package/dist/devices/meters.d.ts +13 -3
  24. package/dist/devices/meters.d.ts.map +1 -1
  25. package/dist/devices/meters.js +121 -63
  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 +104 -45
  30. package/dist/devices/motion.js.map +1 -1
  31. package/dist/devices/plugs.d.ts +13 -3
  32. package/dist/devices/plugs.d.ts.map +1 -1
  33. package/dist/devices/plugs.js +99 -35
  34. package/dist/devices/plugs.js.map +1 -1
  35. package/dist/irdevices/airconditioners.d.ts +11 -3
  36. package/dist/irdevices/airconditioners.d.ts.map +1 -1
  37. package/dist/irdevices/airconditioners.js +80 -45
  38. package/dist/irdevices/airconditioners.js.map +1 -1
  39. package/dist/irdevices/airpurifiers.d.ts +11 -3
  40. package/dist/irdevices/airpurifiers.d.ts.map +1 -1
  41. package/dist/irdevices/airpurifiers.js +68 -31
  42. package/dist/irdevices/airpurifiers.js.map +1 -1
  43. package/dist/irdevices/cameras.d.ts +11 -3
  44. package/dist/irdevices/cameras.d.ts.map +1 -1
  45. package/dist/irdevices/cameras.js +64 -24
  46. package/dist/irdevices/cameras.js.map +1 -1
  47. package/dist/irdevices/fans.d.ts +11 -3
  48. package/dist/irdevices/fans.d.ts.map +1 -1
  49. package/dist/irdevices/fans.js +74 -35
  50. package/dist/irdevices/fans.js.map +1 -1
  51. package/dist/irdevices/lights.d.ts +11 -3
  52. package/dist/irdevices/lights.d.ts.map +1 -1
  53. package/dist/irdevices/lights.js +65 -25
  54. package/dist/irdevices/lights.js.map +1 -1
  55. package/dist/irdevices/others.d.ts +11 -3
  56. package/dist/irdevices/others.d.ts.map +1 -1
  57. package/dist/irdevices/others.js +71 -31
  58. package/dist/irdevices/others.js.map +1 -1
  59. package/dist/irdevices/tvs.d.ts +11 -3
  60. package/dist/irdevices/tvs.d.ts.map +1 -1
  61. package/dist/irdevices/tvs.js +81 -42
  62. package/dist/irdevices/tvs.js.map +1 -1
  63. package/dist/irdevices/vacuumcleaners.d.ts +11 -3
  64. package/dist/irdevices/vacuumcleaners.d.ts.map +1 -1
  65. package/dist/irdevices/vacuumcleaners.js +64 -24
  66. package/dist/irdevices/vacuumcleaners.js.map +1 -1
  67. package/dist/irdevices/waterheaters.d.ts +11 -3
  68. package/dist/irdevices/waterheaters.d.ts.map +1 -1
  69. package/dist/irdevices/waterheaters.js +64 -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 +4 -1
  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 +70 -66
  80. package/src/devices/bots.ts +169 -105
  81. package/src/devices/colorbulb.ts +170 -99
  82. package/src/devices/contact.ts +124 -64
  83. package/src/devices/curtains.ts +207 -145
  84. package/src/devices/humidifiers.ts +187 -129
  85. package/src/devices/meters.ts +128 -64
  86. package/src/devices/motion.ts +110 -45
  87. package/src/devices/plugs.ts +104 -36
  88. package/src/irdevices/airconditioners.ts +85 -45
  89. package/src/irdevices/airpurifiers.ts +73 -31
  90. package/src/irdevices/cameras.ts +69 -24
  91. package/src/irdevices/fans.ts +79 -36
  92. package/src/irdevices/lights.ts +70 -25
  93. package/src/irdevices/others.ts +76 -31
  94. package/src/irdevices/tvs.ts +86 -42
  95. package/src/irdevices/vacuumcleaners.ts +69 -24
  96. package/src/irdevices/waterheaters.ts +69 -24
  97. package/src/platform.ts +285 -302
  98. package/src/settings.ts +4 -1
  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
@@ -1,7 +1,7 @@
1
1
  import { AxiosResponse } from 'axios';
2
2
  import { SwitchBotPlatform } from '../platform';
3
3
  import { irDevicesConfig, DeviceURL, irdevice, payload } from '../settings';
4
- import { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';
4
+ import { CharacteristicValue, HAPStatus, PlatformAccessory, Service } from 'homebridge';
5
5
 
6
6
  /**
7
7
  * Platform Accessory
@@ -35,8 +35,7 @@ export class AirConditioner {
35
35
  static MODE_HEAT: number;
36
36
 
37
37
  // Config
38
- private readonly deviceDebug = this.platform.config.options?.debug === 'device' || this.platform.debugMode;
39
- private readonly debugDebug = this.platform.config.options?.debug === 'debug' || this.platform.debugMode;
38
+ deviceLogging!: string;
40
39
 
41
40
  constructor(
42
41
  private readonly platform: SwitchBotPlatform,
@@ -44,6 +43,7 @@ export class AirConditioner {
44
43
  public device: irdevice & irDevicesConfig,
45
44
  ) {
46
45
  // default placeholders
46
+ this.logs();
47
47
  if (this.Active === undefined) {
48
48
  this.Active = this.platform.Characteristic.Active.INACTIVE;
49
49
  } else {
@@ -143,6 +143,16 @@ export class AirConditioner {
143
143
  .onSet(this.RotationSpeedSet.bind(this));
144
144
  }
145
145
 
146
+ logs() {
147
+ if (this.device.logging) {
148
+ this.deviceLogging = this.accessory.context.logging = this.device.logging;
149
+ } else if (this.platform.config.options?.logging) {
150
+ this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
151
+ } else {
152
+ this.deviceLogging = this.accessory.context.logging = 'standard';
153
+ }
154
+ }
155
+
146
156
  private RotationSpeedSet(value: CharacteristicValue) {
147
157
  if (value === 4) {
148
158
  this.CurrentFanSpeed = 1;
@@ -163,14 +173,14 @@ export class AirConditioner {
163
173
  }
164
174
 
165
175
  private ActiveSet(value: CharacteristicValue) {
166
- this.platform.debug(`Air Conditioner: ${this.accessory.displayName} Set Active: ${value}`);
176
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} Set Active: ${value}`);
167
177
 
168
178
  if (value === this.platform.Characteristic.Active.INACTIVE) {
169
179
  this.pushAirConditionerOffChanges();
170
- this.platform.device(`Air Conditioner: ${this.accessory.displayName} pushAirConditionerOffChanges, Active: ${this.Active}`);
180
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} pushAirConditionerOffChanges, Active: ${this.Active}`);
171
181
  } else {
172
182
  this.pushAirConditionerOnChanges();
173
- this.platform.device(`Air Conditioner: ${this.accessory.displayName} pushAirConditionerOffChanges, Active: ${this.Active}`);
183
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} pushAirConditionerOffChanges, Active: ${this.Active}`);
174
184
  }
175
185
  this.Active = value;
176
186
  this.ActiveCached = this.Active;
@@ -179,49 +189,49 @@ export class AirConditioner {
179
189
 
180
190
  private updateHomeKitCharacteristics() {
181
191
  if (this.Active === undefined) {
182
- this.platform.debug(`Air Conditioner: ${this.accessory.displayName} Active: ${this.Active}`);
192
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} Active: ${this.Active}`);
183
193
  } else {
184
194
  this.service?.updateCharacteristic(this.platform.Characteristic.Active, this.Active);
185
- this.platform.device(`Air Conditioner: ${this.accessory.displayName} updateCharacteristic Active: ${this.Active}`);
195
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} updateCharacteristic Active: ${this.Active}`);
186
196
  }
187
197
  if (this.RotationSpeed === undefined) {
188
- this.platform.debug(`Air Conditioner: ${this.accessory.displayName} RotationSpeed: ${this.RotationSpeed}`);
198
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} RotationSpeed: ${this.RotationSpeed}`);
189
199
  } else {
190
200
  this.service?.updateCharacteristic(this.platform.Characteristic.RotationSpeed, this.RotationSpeed);
191
- this.platform.device(`Air Conditioner: ${this.accessory.displayName} updateCharacteristic RotationSpeed: ${this.RotationSpeed}`);
201
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} updateCharacteristic RotationSpeed: ${this.RotationSpeed}`);
192
202
  }
193
203
  if (this.CurrentTemperature === undefined) {
194
- this.platform.debug(`Air Conditioner: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
204
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
195
205
  } else {
196
206
  this.service?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.CurrentTemperature);
197
- this.platform.device(`Air Conditioner: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`);
207
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`);
198
208
  }
199
209
  if (this.TargetHeaterCoolerState === undefined) {
200
- this.platform.debug(`Air Conditioner: ${this.accessory.displayName} TargetHeaterCoolerState: ${this.TargetHeaterCoolerState}`);
210
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} TargetHeaterCoolerState: ${this.TargetHeaterCoolerState}`);
201
211
  } else {
202
212
  this.service?.updateCharacteristic(this.platform.Characteristic.TargetHeaterCoolerState, this.TargetHeaterCoolerState);
203
- this.platform.device(`Air Conditioner: ${this.accessory.displayName}`
213
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName}`
204
214
  + ` updateCharacteristic TargetHeaterCoolerState: ${this.TargetHeaterCoolerState}`);
205
215
  }
206
216
  if (this.CurrentHeaterCoolerState === undefined) {
207
- this.platform.debug(`Air Conditioner: ${this.accessory.displayName} CurrentHeaterCoolerState: ${this.CurrentHeaterCoolerState}`);
217
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} CurrentHeaterCoolerState: ${this.CurrentHeaterCoolerState}`);
208
218
  } else {
209
219
  this.service?.updateCharacteristic(this.platform.Characteristic.CurrentHeaterCoolerState, this.CurrentHeaterCoolerState);
210
- this.platform.device(`Air Conditioner: ${this.accessory.displayName}`
220
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName}`
211
221
  + ` updateCharacteristic CurrentHeaterCoolerState: ${this.CurrentHeaterCoolerState}`);
212
222
  }
213
223
  if (this.HeatingThresholdTemperature === undefined) {
214
- this.platform.debug(`Air Conditioner: ${this.accessory.displayName} HeatingThresholdTemperature: ${this.HeatingThresholdTemperature}`);
224
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} HeatingThresholdTemperature: ${this.HeatingThresholdTemperature}`);
215
225
  } else {
216
226
  this.service?.updateCharacteristic(this.platform.Characteristic.HeatingThresholdTemperature, this.HeatingThresholdTemperature);
217
- this.platform.device(`Air Conditioner: ${this.accessory.displayName}`
227
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName}`
218
228
  + ` updateCharacteristic HeatingThresholdTemperature: ${this.HeatingThresholdTemperature}`);
219
229
  }
220
230
  }
221
231
 
222
232
  private CurrentTemperatureGet(value: CharacteristicValue) {
223
233
  value = Number(this.CurrentTemperature) || 24;
224
- this.platform.debug(`Air Conditioner: ${this.accessory.displayName} Trigger Get CurrentTemperature: ${value}` );
234
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} Trigger Get CurrentTemperature: ${value}`);
225
235
 
226
236
  this.service
227
237
  .getCharacteristic(this.platform.Characteristic.CurrentTemperature)
@@ -257,13 +267,13 @@ export class AirConditioner {
257
267
  } else {
258
268
  this.CurrentHeaterCoolerState = this.platform.Characteristic.CurrentHeaterCoolerState.INACTIVE;
259
269
  }
260
- this.platform.device(`Air Conditioner: ${this.accessory.displayName} CurrentHeaterCoolerState: ${this.CurrentHeaterCoolerState}`);
270
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} CurrentHeaterCoolerState: ${this.CurrentHeaterCoolerState}`);
261
271
  return this.CurrentHeaterCoolerState;
262
272
  }
263
273
 
264
274
  private HeatingThresholdTemperatureGet() {
265
275
  this.CurrentTemperature = this.CurrentTemperature || 24;
266
- this.platform.device(`Air Conditioner: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
276
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
267
277
  return this.CurrentTemperature;
268
278
  }
269
279
 
@@ -271,7 +281,7 @@ export class AirConditioner {
271
281
  this.pushAirConditionerStatusChanges();
272
282
  this.LastTemperature = Number(this.CurrentTemperature);
273
283
  this.CurrentTemperature = Number(value);
274
- this.platform.device(`Air Conditioner: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature},`
284
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature},`
275
285
  + ` LastTemperature: ${this.LastTemperature}`);
276
286
  }
277
287
 
@@ -356,65 +366,95 @@ export class AirConditioner {
356
366
 
357
367
  public async pushChanges(payload: payload) {
358
368
  try {
359
- this.platform.log.info(`Air Conditioner: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
369
+ this.infoLog(`Air Conditioner: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
360
370
  + ` parameter: [${payload.parameter}], commandType: ${payload.commandType}`);
361
371
 
362
372
  // Make the API request
363
373
  const push: any = await this.platform.axios.post(`${DeviceURL}/${this.device.deviceId}/commands`, payload);
364
- this.platform.log.error(`Air Conditioner: ${this.accessory.displayName} pushChanges: ${JSON.stringify(push.data)}`);
374
+ this.errorLog(`Air Conditioner: ${this.accessory.displayName} pushChanges: ${JSON.stringify(push.data)}`);
365
375
  this.statusCode(push);
366
376
  this.CurrentTemperatureCached = this.CurrentTemperature;
367
377
  this.accessory.context.CurrentTemperature = this.CurrentTemperatureCached;
368
378
  this.updateHomeKitCharacteristics();
369
379
  } catch (e: any) {
370
- this.platform.log.error(`Air Conditioner: ${this.accessory.displayName} failed pushChanges`);
371
- if (this.deviceDebug) {
372
- this.platform.log.error(`Air Conditioner: ${this.accessory.displayName} failed pushChanges,`
380
+ this.errorLog(`Air Conditioner: ${this.accessory.displayName} failed pushChanges`);
381
+ if (this.deviceLogging === 'debug') {
382
+ this.errorLog(`Air Conditioner: ${this.accessory.displayName} failed pushChanges,`
373
383
  + ` Error Message: ${JSON.stringify(e.message)}`);
374
384
  }
375
- if (this.debugDebug) {
376
- this.platform.log.error(`Air Conditioner: ${this.accessory.displayName} failed pushChanges,`
385
+ if (this.platform.debugMode) {
386
+ this.errorLog(`Air Conditioner: ${this.accessory.displayName} failed pushChanges,`
377
387
  + ` Error: ${JSON.stringify(e)}`);
378
388
  }
379
- this.apiError(e);
389
+ this.apiError();
380
390
  }
381
391
  }
382
392
 
383
393
  private statusCode(push: AxiosResponse<{ statusCode: number; }>) {
384
394
  switch (push.data.statusCode) {
385
395
  case 151:
386
- this.platform.log.error(`Air Conditioner: ${this.accessory.displayName} Command not supported by this device type.`);
396
+ this.errorLog(`Air Conditioner: ${this.accessory.displayName} Command not supported by this device type.`);
387
397
  break;
388
398
  case 152:
389
- this.platform.log.error(`Air Conditioner: ${this.accessory.displayName} Device not found.`);
399
+ this.errorLog(`Air Conditioner: ${this.accessory.displayName} Device not found.`);
390
400
  break;
391
401
  case 160:
392
- this.platform.log.error(`Air Conditioner: ${this.accessory.displayName} Command is not supported.`);
402
+ this.errorLog(`Air Conditioner: ${this.accessory.displayName} Command is not supported.`);
393
403
  break;
394
404
  case 161:
395
- this.platform.log.error(`Air Conditioner: ${this.accessory.displayName} Device is offline.`);
405
+ this.errorLog(`Air Conditioner: ${this.accessory.displayName} Device is offline.`);
396
406
  break;
397
407
  case 171:
398
- this.platform.log.error(`Air Conditioner: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
408
+ this.errorLog(`Air Conditioner: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
399
409
  break;
400
410
  case 190:
401
- this.platform.log.error(`Air Conditioner: ${this.accessory.displayName} Device internal error due to device states not synchronized`
411
+ this.errorLog(`Air Conditioner: ${this.accessory.displayName} Device internal error due to device states not synchronized`
402
412
  + ` with server, Or command: ${JSON.stringify(push.data)} format is invalid`);
403
413
  break;
404
414
  case 100:
405
- this.platform.debug(`Air Conditioner: ${this.accessory.displayName} Command successfully sent.`);
415
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} Command successfully sent.`);
406
416
  break;
407
417
  default:
408
- this.platform.debug(`Air Conditioner: ${this.accessory.displayName} Unknown statusCode.`);
418
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} Unknown statusCode.`);
419
+ }
420
+ }
421
+
422
+ public apiError() {
423
+ throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
424
+ }
425
+
426
+ /**
427
+ * Logging for Device
428
+ */
429
+ infoLog(...log: any[]) {
430
+ if (this.enablingDeviceLogging()) {
431
+ this.platform.log.info(String(...log));
432
+ }
433
+ }
434
+
435
+ warnLog(...log: any[]) {
436
+ if (this.enablingDeviceLogging()) {
437
+ this.platform.log.warn(String(...log));
438
+ }
439
+ }
440
+
441
+ errorLog(...log: any[]) {
442
+ if (this.enablingDeviceLogging()) {
443
+ this.platform.log.error(String(...log));
444
+ }
445
+ }
446
+
447
+ debugLog(...log: any[]) {
448
+ if (this.enablingDeviceLogging()) {
449
+ if (this.deviceLogging === 'debug') {
450
+ this.platform.log.info('[DEBUG]', String(...log));
451
+ } else {
452
+ this.platform.log.debug(String(...log));
453
+ }
409
454
  }
410
455
  }
411
456
 
412
- public apiError(e: any) {
413
- this.service.updateCharacteristic(this.platform.Characteristic.Active, e);
414
- this.service.updateCharacteristic(this.platform.Characteristic.RotationSpeed, e);
415
- this.service.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, e);
416
- this.service.updateCharacteristic(this.platform.Characteristic.TargetHeaterCoolerState, e);
417
- this.service.updateCharacteristic(this.platform.Characteristic.CurrentHeaterCoolerState, e);
418
- this.service.updateCharacteristic(this.platform.Characteristic.HeatingThresholdTemperature, e);
457
+ enablingDeviceLogging(): boolean {
458
+ return this.deviceLogging === 'debug' || this.deviceLogging === 'standard';
419
459
  }
420
460
  }
@@ -1,7 +1,7 @@
1
1
  import { AxiosResponse } from 'axios';
2
2
  import { SwitchBotPlatform } from '../platform';
3
3
  import { irDevicesConfig, DeviceURL, irdevice, payload } from '../settings';
4
- import { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';
4
+ import { CharacteristicValue, HAPStatus, PlatformAccessory, Service } from 'homebridge';
5
5
 
6
6
  /**
7
7
  * Platform Accessory
@@ -36,8 +36,7 @@ export class AirPurifier {
36
36
  static PURIFYING_AIR: number;
37
37
 
38
38
  // Config
39
- private readonly deviceDebug = this.platform.config.options?.debug === 'device' || this.platform.debugMode;
40
- private readonly debugDebug = this.platform.config.options?.debug === 'debug' || this.platform.debugMode;
39
+ deviceLogging!: string;
41
40
 
42
41
  constructor(
43
42
  private readonly platform: SwitchBotPlatform,
@@ -45,6 +44,7 @@ export class AirPurifier {
45
44
  public device: irdevice & irDevicesConfig,
46
45
  ) {
47
46
  // default placeholders
47
+ this.logs();
48
48
  if (this.Active === undefined) {
49
49
  this.Active = this.platform.Characteristic.Active.INACTIVE;
50
50
  } else {
@@ -87,8 +87,18 @@ export class AirPurifier {
87
87
  this.service.getCharacteristic(this.platform.Characteristic.TargetAirPurifierState).onSet(this.TargetAirPurifierStateSet.bind(this));
88
88
  }
89
89
 
90
+ logs() {
91
+ if (this.device.logging) {
92
+ this.deviceLogging = this.accessory.context.logging = this.device.logging;
93
+ } else if (this.platform.config.options?.logging) {
94
+ this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
95
+ } else {
96
+ this.deviceLogging = this.accessory.context.logging = 'standard';
97
+ }
98
+ }
99
+
90
100
  private ActiveSet(value: CharacteristicValue) {
91
- this.platform.debug(`Air Purifier: ${this.accessory.displayName} Set Active: ${value}`);
101
+ this.debugLog(`Air Purifier: ${this.accessory.displayName} Set Active: ${value}`);
92
102
  if (value === this.platform.Characteristic.Active.INACTIVE) {
93
103
  this.pushAirConditionerOffChanges();
94
104
  } else {
@@ -101,23 +111,23 @@ export class AirPurifier {
101
111
 
102
112
  private updateHomeKitCharacteristics() {
103
113
  if (this.Active === undefined) {
104
- this.platform.debug(`Air Purifier: ${this.accessory.displayName} Active: ${this.Active}`);
114
+ this.debugLog(`Air Purifier: ${this.accessory.displayName} Active: ${this.Active}`);
105
115
  } else {
106
116
  this.service?.updateCharacteristic(this.platform.Characteristic.Active, this.Active);
107
- this.platform.device(`Air Purifier: ${this.accessory.displayName} updateCharacteristic Active: ${this.Active}`);
117
+ this.debugLog(`Air Purifier: ${this.accessory.displayName} updateCharacteristic Active: ${this.Active}`);
108
118
  }
109
119
  if (this.CurrentAirPurifierState === undefined) {
110
- this.platform.debug(`Air Purifier: ${this.accessory.displayName} CurrentAirPurifierState: ${this.CurrentAirPurifierState}`);
120
+ this.debugLog(`Air Purifier: ${this.accessory.displayName} CurrentAirPurifierState: ${this.CurrentAirPurifierState}`);
111
121
  } else {
112
122
  this.service?.updateCharacteristic(this.platform.Characteristic.CurrentAirPurifierState, this.CurrentAirPurifierState);
113
- this.platform.device(`Air Purifier: ${this.accessory.displayName}`
123
+ this.debugLog(`Air Purifier: ${this.accessory.displayName}`
114
124
  + ` updateCharacteristic CurrentAirPurifierState: ${this.CurrentAirPurifierState}`);
115
125
  }
116
126
  if (this.CurrentHeaterCoolerState === undefined) {
117
- this.platform.debug(`Air Purifier: ${this.accessory.displayName} CurrentHeaterCoolerState: ${this.CurrentHeaterCoolerState}`);
127
+ this.debugLog(`Air Purifier: ${this.accessory.displayName} CurrentHeaterCoolerState: ${this.CurrentHeaterCoolerState}`);
118
128
  } else {
119
129
  this.service?.updateCharacteristic(this.platform.Characteristic.CurrentHeaterCoolerState, this.CurrentHeaterCoolerState);
120
- this.platform.device(`Air Purifier: ${this.accessory.displayName}`
130
+ this.debugLog(`Air Purifier: ${this.accessory.displayName}`
121
131
  + ` updateCharacteristic CurrentHeaterCoolerState: ${this.CurrentHeaterCoolerState}`);
122
132
  }
123
133
  }
@@ -218,63 +228,95 @@ export class AirPurifier {
218
228
 
219
229
  public async pushChanges(payload: payload) {
220
230
  try {
221
- this.platform.log.info(`Air Purifier: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
231
+ this.infoLog(`Air Purifier: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
222
232
  + ` parameter: ${payload.parameter}, commandType: ${payload.commandType}`);
223
233
 
224
234
  // Make the API request
225
235
  const push = await this.platform.axios.post(`${DeviceURL}/${this.device.deviceId}/commands`, payload);
226
- this.platform.debug(`Air Purifier: ${this.accessory.displayName} pushChanges: ${push.data}`);
236
+ this.debugLog(`Air Purifier: ${this.accessory.displayName} pushChanges: ${push.data}`);
227
237
  this.statusCode(push);
228
238
  this.updateHomeKitCharacteristics();
229
239
  this.CurrentTemperatureCached = this.CurrentTemperature;
230
240
  this.accessory.context.CurrentTemperature = this.CurrentTemperatureCached;
231
241
  } catch (e: any) {
232
- this.platform.log.error(`Air Purifier: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
233
- if (this.deviceDebug) {
234
- this.platform.log.error(`Air Purifier: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
242
+ this.errorLog(`Air Purifier: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
243
+ if (this.deviceLogging === 'debug') {
244
+ this.errorLog(`Air Purifier: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
235
245
  + ` Error Message: ${JSON.stringify(e.message)}`);
236
246
  }
237
- if (this.debugDebug) {
238
- this.platform.log.error(`Air Purifier: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
247
+ if (this.platform.debugMode) {
248
+ this.errorLog(`Air Purifier: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
239
249
  + ` Error: ${JSON.stringify(e)}`);
240
250
  }
241
- this.apiError(e);
251
+ this.apiError();
242
252
  }
243
253
  }
244
254
 
245
255
  private statusCode(push: AxiosResponse<{ statusCode: number; }>) {
246
256
  switch (push.data.statusCode) {
247
257
  case 151:
248
- this.platform.log.error(`Air Purifier: ${this.accessory.displayName} Command not supported by this device type.`);
258
+ this.errorLog(`Air Purifier: ${this.accessory.displayName} Command not supported by this device type.`);
249
259
  break;
250
260
  case 152:
251
- this.platform.log.error(`Air Purifier: ${this.accessory.displayName} Device not found.`);
261
+ this.errorLog(`Air Purifier: ${this.accessory.displayName} Device not found.`);
252
262
  break;
253
263
  case 160:
254
- this.platform.log.error(`Air Purifier: ${this.accessory.displayName} Command is not supported.`);
264
+ this.errorLog(`Air Purifier: ${this.accessory.displayName} Command is not supported.`);
255
265
  break;
256
266
  case 161:
257
- this.platform.log.error(`Air Purifier: ${this.accessory.displayName} Device is offline.`);
267
+ this.errorLog(`Air Purifier: ${this.accessory.displayName} Device is offline.`);
258
268
  break;
259
269
  case 171:
260
- this.platform.log.error(`Air Purifier: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
270
+ this.errorLog(`Air Purifier: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
261
271
  break;
262
272
  case 190:
263
- this.platform.log.error(`Air Purifier: ${this.accessory.displayName} Device internal error due to device states not synchronized`
273
+ this.errorLog(`Air Purifier: ${this.accessory.displayName} Device internal error due to device states not synchronized`
264
274
  + ` with server, Or command: ${JSON.stringify(push.data)} format is invalid`);
265
275
  break;
266
276
  case 100:
267
- this.platform.debug(`Air Purifier: ${this.accessory.displayName} Command successfully sent.`);
277
+ this.debugLog(`Air Purifier: ${this.accessory.displayName} Command successfully sent.`);
268
278
  break;
269
279
  default:
270
- this.platform.debug(`Air Purifier: ${this.accessory.displayName} Unknown statusCode.`);
280
+ this.debugLog(`Air Purifier: ${this.accessory.displayName} Unknown statusCode.`);
281
+ }
282
+ }
283
+
284
+ public apiError() {
285
+ throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
286
+ }
287
+
288
+ /**
289
+ * Logging for Device
290
+ */
291
+ infoLog(...log: any[]) {
292
+ if (this.enablingDeviceLogging()) {
293
+ this.platform.log.info(String(...log));
294
+ }
295
+ }
296
+
297
+ warnLog(...log: any[]) {
298
+ if (this.enablingDeviceLogging()) {
299
+ this.platform.log.warn(String(...log));
300
+ }
301
+ }
302
+
303
+ errorLog(...log: any[]) {
304
+ if (this.enablingDeviceLogging()) {
305
+ this.platform.log.error(String(...log));
306
+ }
307
+ }
308
+
309
+ debugLog(...log: any[]) {
310
+ if (this.enablingDeviceLogging()) {
311
+ if (this.deviceLogging === 'debug') {
312
+ this.platform.log.info('[DEBUG]', String(...log));
313
+ } else {
314
+ this.platform.log.debug(String(...log));
315
+ }
271
316
  }
272
317
  }
273
318
 
274
- public apiError(e: any) {
275
- this.service.updateCharacteristic(this.platform.Characteristic.CurrentHeaterCoolerState, e);
276
- this.service.updateCharacteristic(this.platform.Characteristic.CurrentAirPurifierState, e);
277
- this.service.updateCharacteristic(this.platform.Characteristic.TargetAirPurifierState, e);
278
- this.service.updateCharacteristic(this.platform.Characteristic.Active, e);
319
+ enablingDeviceLogging(): boolean {
320
+ return this.deviceLogging === 'debug' || this.deviceLogging === 'standard';
279
321
  }
280
322
  }
@@ -1,7 +1,7 @@
1
1
  import { AxiosResponse } from 'axios';
2
2
  import { SwitchBotPlatform } from '../platform';
3
3
  import { irDevicesConfig, DeviceURL, irdevice, payload } from '../settings';
4
- import { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';
4
+ import { CharacteristicValue, HAPStatus, PlatformAccessory, Service } from 'homebridge';
5
5
 
6
6
  /**
7
7
  * Platform Accessory
@@ -17,8 +17,7 @@ export class Camera {
17
17
  OnCached!: CharacteristicValue;
18
18
 
19
19
  // Config
20
- private readonly deviceDebug = this.platform.config.options?.debug === 'device' || this.platform.debugMode;
21
- private readonly debugDebug = this.platform.config.options?.debug === 'debug' || this.platform.debugMode;
20
+ deviceLogging!: string;
22
21
 
23
22
  constructor(
24
23
  private readonly platform: SwitchBotPlatform,
@@ -26,6 +25,7 @@ export class Camera {
26
25
  public device: irdevice & irDevicesConfig,
27
26
  ) {
28
27
  // default placeholders
28
+ this.logs();
29
29
  if (this.On === undefined) {
30
30
  this.On = false;
31
31
  } else {
@@ -57,8 +57,18 @@ export class Camera {
57
57
  this.service.getCharacteristic(this.platform.Characteristic.On).onSet(this.OnSet.bind(this));
58
58
  }
59
59
 
60
+ logs() {
61
+ if (this.device.logging) {
62
+ this.deviceLogging = this.accessory.context.logging = this.device.logging;
63
+ } else if (this.platform.config.options?.logging) {
64
+ this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
65
+ } else {
66
+ this.deviceLogging = this.accessory.context.logging = 'standard';
67
+ }
68
+ }
69
+
60
70
  private OnSet(value: CharacteristicValue) {
61
- this.platform.debug(`Camera: ${this.accessory.displayName} On: ${value}`);
71
+ this.debugLog(`Camera: ${this.accessory.displayName} On: ${value}`);
62
72
  this.On = value;
63
73
  if (this.On) {
64
74
  this.pushOnChanges();
@@ -69,10 +79,10 @@ export class Camera {
69
79
 
70
80
  private updateHomeKitCharacteristics() {
71
81
  if (this.On === undefined) {
72
- this.platform.debug(`Camera: ${this.accessory.displayName} On: ${this.On}`);
82
+ this.debugLog(`Camera: ${this.accessory.displayName} On: ${this.On}`);
73
83
  } else {
74
84
  this.service?.updateCharacteristic(this.platform.Characteristic.On, this.On);
75
- this.platform.device(`Camera: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
85
+ this.debugLog(`Camera: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
76
86
  }
77
87
  }
78
88
 
@@ -110,60 +120,95 @@ export class Camera {
110
120
 
111
121
  public async pushChanges(payload: payload) {
112
122
  try {
113
- this.platform.log.info(`Camera: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
123
+ this.infoLog(`Camera: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
114
124
  + ` parameter: ${payload.parameter}, commandType: ${payload.commandType}`);
115
125
 
116
126
  // Make the API request
117
127
  const push = await this.platform.axios.post(`${DeviceURL}/${this.device.deviceId}/commands`, payload);
118
- this.platform.debug(`Camera: ${this.accessory.displayName} pushChanges: ${push.data}`);
128
+ this.debugLog(`Camera: ${this.accessory.displayName} pushChanges: ${push.data}`);
119
129
  this.statusCode(push);
120
130
  this.updateHomeKitCharacteristics();
121
131
  this.OnCached = this.On;
122
132
  this.accessory.context.On = this.OnCached;
123
133
  } catch (e: any) {
124
- this.platform.log.error(`Camera: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
125
- if (this.deviceDebug) {
126
- this.platform.log.error(`Camera: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
134
+ this.errorLog(`Camera: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
135
+ if (this.deviceLogging === 'debug') {
136
+ this.errorLog(`Camera: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
127
137
  + ` Error Message: ${JSON.stringify(e.message)}`);
128
138
  }
129
- if (this.debugDebug) {
130
- this.platform.log.error(`Camera: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
139
+ if (this.platform.debugMode) {
140
+ this.errorLog(`Camera: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
131
141
  + ` Error: ${JSON.stringify(e)}`);
132
142
  }
133
- this.apiError(e);
143
+ this.apiError();
134
144
  }
135
145
  }
136
146
 
137
147
  private statusCode(push: AxiosResponse<{ statusCode: number; }>) {
138
148
  switch (push.data.statusCode) {
139
149
  case 151:
140
- this.platform.log.error(`Camera: ${this.accessory.displayName} Command not supported by this device type.`);
150
+ this.errorLog(`Camera: ${this.accessory.displayName} Command not supported by this device type.`);
141
151
  break;
142
152
  case 152:
143
- this.platform.log.error(`Camera: ${this.accessory.displayName} Device not found.`);
153
+ this.errorLog(`Camera: ${this.accessory.displayName} Device not found.`);
144
154
  break;
145
155
  case 160:
146
- this.platform.log.error(`Camera: ${this.accessory.displayName} Command is not supported.`);
156
+ this.errorLog(`Camera: ${this.accessory.displayName} Command is not supported.`);
147
157
  break;
148
158
  case 161:
149
- this.platform.log.error(`Camera: ${this.accessory.displayName} Device is offline.`);
159
+ this.errorLog(`Camera: ${this.accessory.displayName} Device is offline.`);
150
160
  break;
151
161
  case 171:
152
- this.platform.log.error(`Camera: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
162
+ this.errorLog(`Camera: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
153
163
  break;
154
164
  case 190:
155
- this.platform.log.error(`Camera: ${this.accessory.displayName} Device internal error due to device states not synchronized`
165
+ this.errorLog(`Camera: ${this.accessory.displayName} Device internal error due to device states not synchronized`
156
166
  + ` with server, Or command: ${JSON.stringify(push.data)} format is invalid`);
157
167
  break;
158
168
  case 100:
159
- this.platform.debug(`Camera: ${this.accessory.displayName} Command successfully sent.`);
169
+ this.debugLog(`Camera: ${this.accessory.displayName} Command successfully sent.`);
160
170
  break;
161
171
  default:
162
- this.platform.debug(`Camera: ${this.accessory.displayName} Unknown statusCode.`);
172
+ this.debugLog(`Camera: ${this.accessory.displayName} Unknown statusCode.`);
173
+ }
174
+ }
175
+
176
+ public apiError() {
177
+ throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
178
+ }
179
+
180
+ /**
181
+ * Logging for Device
182
+ */
183
+ infoLog(...log: any[]) {
184
+ if (this.enablingDeviceLogging()) {
185
+ this.platform.log.info(String(...log));
186
+ }
187
+ }
188
+
189
+ warnLog(...log: any[]) {
190
+ if (this.enablingDeviceLogging()) {
191
+ this.platform.log.warn(String(...log));
192
+ }
193
+ }
194
+
195
+ errorLog(...log: any[]) {
196
+ if (this.enablingDeviceLogging()) {
197
+ this.platform.log.error(String(...log));
198
+ }
199
+ }
200
+
201
+ debugLog(...log: any[]) {
202
+ if (this.enablingDeviceLogging()) {
203
+ if (this.deviceLogging === 'debug') {
204
+ this.platform.log.info('[DEBUG]', String(...log));
205
+ } else {
206
+ this.platform.log.debug(String(...log));
207
+ }
163
208
  }
164
209
  }
165
210
 
166
- public apiError(e: any) {
167
- this.service.updateCharacteristic(this.platform.Characteristic.On, e);
211
+ enablingDeviceLogging(): boolean {
212
+ return this.deviceLogging === 'debug' || this.deviceLogging === 'standard';
168
213
  }
169
214
  }