@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,6 +1,6 @@
1
1
  import { AxiosResponse } from 'axios';
2
2
  import { SwitchBotPlatform } from '../platform';
3
- import { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';
3
+ import { CharacteristicValue, HAPStatus, PlatformAccessory, Service } from 'homebridge';
4
4
  import { DeviceURL, irdevice, deviceStatusResponse, irDevicesConfig, payload } from '../settings';
5
5
 
6
6
  /**
@@ -22,8 +22,7 @@ export class TV {
22
22
  deviceStatus!: deviceStatusResponse;
23
23
 
24
24
  // Config
25
- private readonly deviceDebug = this.platform.config.options?.debug === 'device' || this.platform.debugMode;
26
- private readonly debugDebug = this.platform.config.options?.debug === 'debug' || this.platform.debugMode;
25
+ deviceLogging!: string;
27
26
 
28
27
  constructor(
29
28
  private readonly platform: SwitchBotPlatform,
@@ -31,6 +30,7 @@ export class TV {
31
30
  public device: irdevice & irDevicesConfig,
32
31
  ) {
33
32
  // default placeholders
33
+ this.logs();
34
34
  if (this.Active === undefined) {
35
35
  this.Active = this.platform.Characteristic.Active.INACTIVE;
36
36
  } else {
@@ -128,8 +128,18 @@ export class TV {
128
128
  .onSet(this.VolumeSelectorSet.bind(this));
129
129
  }
130
130
 
131
+ logs() {
132
+ if (this.device.logging) {
133
+ this.deviceLogging = this.accessory.context.logging = this.device.logging;
134
+ } else if (this.platform.config.options?.logging) {
135
+ this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
136
+ } else {
137
+ this.deviceLogging = this.accessory.context.logging = 'standard';
138
+ }
139
+ }
140
+
131
141
  private VolumeSelectorSet(value: CharacteristicValue) {
132
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} VolumeSelector: ${value}`);
142
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} VolumeSelector: ${value}`);
133
143
  if (value === this.platform.Characteristic.VolumeSelector.INCREMENT) {
134
144
  this.pushVolumeUpChanges();
135
145
  } else {
@@ -140,61 +150,61 @@ export class TV {
140
150
  private RemoteKeySet(value: CharacteristicValue) {
141
151
  switch (value) {
142
152
  case this.platform.Characteristic.RemoteKey.REWIND: {
143
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: REWIND`);
153
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: REWIND`);
144
154
  break;
145
155
  }
146
156
  case this.platform.Characteristic.RemoteKey.FAST_FORWARD: {
147
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: FAST_FORWARD`);
157
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: FAST_FORWARD`);
148
158
  break;
149
159
  }
150
160
  case this.platform.Characteristic.RemoteKey.NEXT_TRACK: {
151
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: NEXT_TRACK`);
161
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: NEXT_TRACK`);
152
162
  break;
153
163
  }
154
164
  case this.platform.Characteristic.RemoteKey.PREVIOUS_TRACK: {
155
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: PREVIOUS_TRACK`);
165
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: PREVIOUS_TRACK`);
156
166
  break;
157
167
  }
158
168
  case this.platform.Characteristic.RemoteKey.ARROW_UP: {
159
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: ARROW_UP`);
169
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: ARROW_UP`);
160
170
  //this.pushUpChanges();
161
171
  break;
162
172
  }
163
173
  case this.platform.Characteristic.RemoteKey.ARROW_DOWN: {
164
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: ARROW_DOWN`);
174
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: ARROW_DOWN`);
165
175
  //this.pushDownChanges();
166
176
  break;
167
177
  }
168
178
  case this.platform.Characteristic.RemoteKey.ARROW_LEFT: {
169
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: ARROW_LEFT`);
179
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: ARROW_LEFT`);
170
180
  //this.pushLeftChanges();
171
181
  break;
172
182
  }
173
183
  case this.platform.Characteristic.RemoteKey.ARROW_RIGHT: {
174
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: ARROW_RIGHT`);
184
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: ARROW_RIGHT`);
175
185
  //this.pushRightChanges();
176
186
  break;
177
187
  }
178
188
  case this.platform.Characteristic.RemoteKey.SELECT: {
179
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: SELECT`);
189
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: SELECT`);
180
190
  //this.pushOkChanges();
181
191
  break;
182
192
  }
183
193
  case this.platform.Characteristic.RemoteKey.BACK: {
184
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: BACK`);
194
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: BACK`);
185
195
  //this.pushBackChanges();
186
196
  break;
187
197
  }
188
198
  case this.platform.Characteristic.RemoteKey.EXIT: {
189
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: EXIT`);
199
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: EXIT`);
190
200
  break;
191
201
  }
192
202
  case this.platform.Characteristic.RemoteKey.PLAY_PAUSE: {
193
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: PLAY_PAUSE`);
203
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: PLAY_PAUSE`);
194
204
  break;
195
205
  }
196
206
  case this.platform.Characteristic.RemoteKey.INFORMATION: {
197
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: INFORMATION`);
207
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: INFORMATION`);
198
208
  //this.pushMenuChanges();
199
209
  break;
200
210
  }
@@ -202,12 +212,12 @@ export class TV {
202
212
  }
203
213
 
204
214
  private ActiveIdentifierSet(value: CharacteristicValue) {
205
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} ActiveIdentifier: ${value}`);
215
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} ActiveIdentifier: ${value}`);
206
216
  this.ActiveIdentifier = value;
207
217
  }
208
218
 
209
219
  private ActiveSet(value: CharacteristicValue) {
210
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} Active: ${value}`);
220
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Active: ${value}`);
211
221
  if (!this.device.irtv?.disable_power) {
212
222
  if (value === this.platform.Characteristic.Active.INACTIVE) {
213
223
  this.pushTvOffChanges();
@@ -222,16 +232,16 @@ export class TV {
222
232
 
223
233
  private updateHomeKitCharacteristics() {
224
234
  if (this.Active === undefined) {
225
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} Active: ${this.Active}`);
235
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Active: ${this.Active}`);
226
236
  } else {
227
237
  this.service?.updateCharacteristic(this.platform.Characteristic.Active, this.Active);
228
- this.platform.device(`${this.device.remoteType}: ${this.accessory.displayName} updateCharacteristic Active: ${this.Active}`);
238
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} updateCharacteristic Active: ${this.Active}`);
229
239
  }
230
240
  if (this.ActiveIdentifier === undefined) {
231
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} ActiveIdentifier: ${this.ActiveIdentifier}`);
241
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} ActiveIdentifier: ${this.ActiveIdentifier}`);
232
242
  } else {
233
243
  this.service?.updateCharacteristic(this.platform.Characteristic.ActiveIdentifier, this.ActiveIdentifier);
234
- this.platform.device(`${this.device.remoteType}: ${this.accessory.displayName}`
244
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName}`
235
245
  + ` updateCharacteristic ActiveIdentifier: ${this.ActiveIdentifier}`);
236
246
  }
237
247
  }
@@ -349,25 +359,25 @@ export class TV {
349
359
 
350
360
  public async pushTVChanges(payload: payload) {
351
361
  try {
352
- this.platform.log.info(`${this.device.remoteType}: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
362
+ this.infoLog(`${this.device.remoteType}: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
353
363
  + ` parameter: ${payload.parameter}, commandType: ${payload.commandType}`);
354
364
 
355
365
  // Make the API request
356
366
  const push = await this.platform.axios.post(`${DeviceURL}/${this.device.deviceId}/commands`, payload);
357
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} pushChanges: ${push.data}`);
367
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} pushChanges: ${push.data}`);
358
368
  this.statusCode(push);
359
369
  this.updateHomeKitCharacteristics();
360
370
  } catch (e: any) {
361
- this.platform.log.error(`${this.device.remoteType}: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
362
- if (this.deviceDebug) {
363
- this.platform.log.error(`${this.device.remoteType}: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
371
+ this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
372
+ if (this.deviceLogging === 'debug') {
373
+ this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
364
374
  + ` Error Message: ${JSON.stringify(e.message)}`);
365
375
  }
366
- if (this.debugDebug) {
367
- this.platform.log.error(`${this.device.remoteType}: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
376
+ if (this.platform.debugMode) {
377
+ this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
368
378
  + ` Error: ${JSON.stringify(e)}`);
369
379
  }
370
- this.apiError(e);
380
+ this.apiError();
371
381
  }
372
382
  }
373
383
 
@@ -375,35 +385,69 @@ export class TV {
375
385
  private statusCode(push: AxiosResponse<{ statusCode: number; }>) {
376
386
  switch (push.data.statusCode) {
377
387
  case 151:
378
- this.platform.log.error(`${this.device.remoteType}: ${this.accessory.displayName} Command not supported by this device type.`);
388
+ this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} Command not supported by this device type.`);
379
389
  break;
380
390
  case 152:
381
- this.platform.log.error(`${this.device.remoteType}: ${this.accessory.displayName} Device not found.`);
391
+ this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} Device not found.`);
382
392
  break;
383
393
  case 160:
384
- this.platform.log.error(`${this.device.remoteType}: ${this.accessory.displayName} Command is not supported.`);
394
+ this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} Command is not supported.`);
385
395
  break;
386
396
  case 161:
387
- this.platform.log.error(`${this.device.remoteType}: ${this.accessory.displayName} Device is offline.`);
397
+ this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} Device is offline.`);
388
398
  break;
389
399
  case 171:
390
- this.platform.log.error(`${this.device.remoteType}: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
400
+ this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
391
401
  break;
392
402
  case 190:
393
- this.platform.log.error(`${this.device.remoteType}: `
403
+ this.errorLog(`${this.device.remoteType}: `
394
404
  + `${this.accessory.displayName} Device internal error due to device states not synchronized with server,`
395
405
  + ` Or command: ${JSON.stringify(push.data)} format is invalid`);
396
406
  break;
397
407
  case 100:
398
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} Command successfully sent.`);
408
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Command successfully sent.`);
399
409
  break;
400
410
  default:
401
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} Unknown statusCode.`);
411
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Unknown statusCode.`);
412
+ }
413
+ }
414
+
415
+ public apiError() {
416
+ throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
417
+ }
418
+
419
+ /**
420
+ * Logging for Device
421
+ */
422
+ infoLog(...log: any[]) {
423
+ if (this.enablingDeviceLogging()) {
424
+ this.platform.log.info(String(...log));
425
+ }
426
+ }
427
+
428
+ warnLog(...log: any[]) {
429
+ if (this.enablingDeviceLogging()) {
430
+ this.platform.log.warn(String(...log));
431
+ }
432
+ }
433
+
434
+ errorLog(...log: any[]) {
435
+ if (this.enablingDeviceLogging()) {
436
+ this.platform.log.error(String(...log));
437
+ }
438
+ }
439
+
440
+ debugLog(...log: any[]) {
441
+ if (this.enablingDeviceLogging()) {
442
+ if (this.deviceLogging === 'debug') {
443
+ this.platform.log.info('[DEBUG]', String(...log));
444
+ } else {
445
+ this.platform.log.debug(String(...log));
446
+ }
402
447
  }
403
448
  }
404
449
 
405
- public apiError(e: any) {
406
- this.service.updateCharacteristic(this.platform.Characteristic.Active, e);
407
- this.service.updateCharacteristic(this.platform.Characteristic.ActiveIdentifier, e);
450
+ enablingDeviceLogging(): boolean {
451
+ return this.deviceLogging === 'debug' || this.deviceLogging === 'standard';
408
452
  }
409
453
  }
@@ -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 VacuumCleaner {
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 VacuumCleaner {
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 VacuumCleaner {
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(`Vacuum Cleaner: ${this.accessory.displayName} On: ${value}`);
71
+ this.debugLog(`Vacuum Cleaner: ${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 VacuumCleaner {
69
79
 
70
80
  private updateHomeKitCharacteristics() {
71
81
  if (this.On === undefined) {
72
- this.platform.debug(`Vacuum Cleaner: ${this.accessory.displayName} On: ${this.On}`);
82
+ this.debugLog(`Vacuum Cleaner: ${this.accessory.displayName} On: ${this.On}`);
73
83
  } else {
74
84
  this.service?.updateCharacteristic(this.platform.Characteristic.On, this.On);
75
- this.platform.device(`Vacuum Cleaner: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
85
+ this.debugLog(`Vacuum Cleaner: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
76
86
  }
77
87
  }
78
88
 
@@ -110,60 +120,95 @@ export class VacuumCleaner {
110
120
 
111
121
  public async pushChanges(payload: payload) {
112
122
  try {
113
- this.platform.log.info(`Vacuum Cleaner: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
123
+ this.infoLog(`Vacuum Cleaner: ${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(`Vacuum Cleaner: ${this.accessory.displayName} pushChanges: ${push.data}`);
128
+ this.debugLog(`Vacuum Cleaner: ${this.accessory.displayName} pushChanges: ${push.data}`);
119
129
  this.statusCode(push);
120
130
  this.OnCached = this.On;
121
131
  this.accessory.context.On = this.OnCached;
122
132
  this.updateHomeKitCharacteristics();
123
133
  } catch (e: any) {
124
- this.platform.log.error(`Vacuum Cleaner: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
125
- if (this.deviceDebug) {
126
- this.platform.log.error(`Vacuum Cleaner: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
134
+ this.errorLog(`Vacuum Cleaner: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
135
+ if (this.deviceLogging === 'debug') {
136
+ this.errorLog(`Vacuum Cleaner: ${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(`Vacuum Cleaner: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
139
+ if (this.platform.debugMode) {
140
+ this.errorLog(`Vacuum Cleaner: ${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(`Vacuum Cleaner: ${this.accessory.displayName} Command not supported by this device type.`);
150
+ this.errorLog(`Vacuum Cleaner: ${this.accessory.displayName} Command not supported by this device type.`);
141
151
  break;
142
152
  case 152:
143
- this.platform.log.error(`Vacuum Cleaner: ${this.accessory.displayName} Device not found.`);
153
+ this.errorLog(`Vacuum Cleaner: ${this.accessory.displayName} Device not found.`);
144
154
  break;
145
155
  case 160:
146
- this.platform.log.error(`Vacuum Cleaner: ${this.accessory.displayName} Command is not supported.`);
156
+ this.errorLog(`Vacuum Cleaner: ${this.accessory.displayName} Command is not supported.`);
147
157
  break;
148
158
  case 161:
149
- this.platform.log.error(`Vacuum Cleaner: ${this.accessory.displayName} Device is offline.`);
159
+ this.errorLog(`Vacuum Cleaner: ${this.accessory.displayName} Device is offline.`);
150
160
  break;
151
161
  case 171:
152
- this.platform.log.error(`Vacuum Cleaner: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
162
+ this.errorLog(`Vacuum Cleaner: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
153
163
  break;
154
164
  case 190:
155
- this.platform.log.error(`Vacuum Cleaner: ${this.accessory.displayName} Device internal error due to device states not synchronized`
165
+ this.errorLog(`Vacuum Cleaner: ${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(`Vacuum Cleaner: ${this.accessory.displayName} Command successfully sent.`);
169
+ this.debugLog(`Vacuum Cleaner: ${this.accessory.displayName} Command successfully sent.`);
160
170
  break;
161
171
  default:
162
- this.platform.debug(`Vacuum Cleaner: ${this.accessory.displayName} Unknown statusCode.`);
172
+ this.debugLog(`Vacuum Cleaner: ${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
  }
@@ -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 WaterHeater {
17
17
  ActiveCached!: 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 WaterHeater {
26
25
  public device: irdevice & irDevicesConfig,
27
26
  ) {
28
27
  // default placeholders
28
+ this.logs();
29
29
  if (this.Active === undefined) {
30
30
  this.Active = this.platform.Characteristic.Active.INACTIVE;
31
31
  } else {
@@ -66,8 +66,18 @@ export class WaterHeater {
66
66
  this.service.getCharacteristic(this.platform.Characteristic.Active).onSet(this.ActiveSet.bind(this));
67
67
  }
68
68
 
69
+ logs() {
70
+ if (this.device.logging) {
71
+ this.deviceLogging = this.accessory.context.logging = this.device.logging;
72
+ } else if (this.platform.config.options?.logging) {
73
+ this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
74
+ } else {
75
+ this.deviceLogging = this.accessory.context.logging = 'standard';
76
+ }
77
+ }
78
+
69
79
  private ActiveSet(value: CharacteristicValue) {
70
- this.platform.debug(`Water Heater: ${this.accessory.displayName} Active: ${value}`);
80
+ this.debugLog(`Water Heater: ${this.accessory.displayName} Active: ${value}`);
71
81
  if (value === this.platform.Characteristic.Active.INACTIVE) {
72
82
  this.pushWaterHeaterOffChanges();
73
83
  this.service.setCharacteristic(
@@ -85,10 +95,10 @@ export class WaterHeater {
85
95
 
86
96
  private updateHomeKitCharacteristics() {
87
97
  if (this.Active === undefined) {
88
- this.platform.debug(`Water Heater: ${this.accessory.displayName} Active: ${this.Active}`);
98
+ this.debugLog(`Water Heater: ${this.accessory.displayName} Active: ${this.Active}`);
89
99
  } else {
90
100
  this.service?.updateCharacteristic(this.platform.Characteristic.Active, this.Active);
91
- this.platform.device(`Water Heater: ${this.accessory.displayName} updateCharacteristic Active: ${this.Active}`);
101
+ this.debugLog(`Water Heater: ${this.accessory.displayName} updateCharacteristic Active: ${this.Active}`);
92
102
  }
93
103
  }
94
104
 
@@ -126,58 +136,93 @@ export class WaterHeater {
126
136
 
127
137
  public async pushChanges(payload: payload) {
128
138
  try {
129
- this.platform.log.info(`Water Heater: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
139
+ this.infoLog(`Water Heater: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
130
140
  + ` parameter: ${payload.parameter}, commandType: ${payload.commandType}`);
131
141
 
132
142
  // Make the API request
133
143
  const push = await this.platform.axios.post(`${DeviceURL}/${this.device.deviceId}/commands`, payload);
134
- this.platform.debug(`Water Heater: ${this.accessory.displayName} pushChanges: ${push.data}`);
144
+ this.debugLog(`Water Heater: ${this.accessory.displayName} pushChanges: ${push.data}`);
135
145
  this.statusCode(push);
136
146
  this.updateHomeKitCharacteristics();
137
147
  } catch (e: any) {
138
- this.platform.log.error(`Water Heater: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
139
- if (this.deviceDebug) {
140
- this.platform.log.error(`Water Heater: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
148
+ this.errorLog(`Water Heater: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
149
+ if (this.deviceLogging === 'debug') {
150
+ this.errorLog(`Water Heater: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
141
151
  + ` Error Message: ${JSON.stringify(e.message)}`);
142
152
  }
143
- if (this.debugDebug) {
144
- this.platform.log.error(`Water Heater: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
153
+ if (this.platform.debugMode) {
154
+ this.errorLog(`Water Heater: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
145
155
  + ` Error: ${JSON.stringify(e)}`);
146
156
  }
147
- this.apiError(e);
157
+ this.apiError();
148
158
  }
149
159
  }
150
160
 
151
161
  private statusCode(push: AxiosResponse<{ statusCode: number; }>) {
152
162
  switch (push.data.statusCode) {
153
163
  case 151:
154
- this.platform.log.error(`Water Heater: ${this.accessory.displayName} Command not supported by this device type.`);
164
+ this.errorLog(`Water Heater: ${this.accessory.displayName} Command not supported by this device type.`);
155
165
  break;
156
166
  case 152:
157
- this.platform.log.error(`Water Heater: ${this.accessory.displayName} Device not found.`);
167
+ this.errorLog(`Water Heater: ${this.accessory.displayName} Device not found.`);
158
168
  break;
159
169
  case 160:
160
- this.platform.log.error(`Water Heater: ${this.accessory.displayName} Command is not supported.`);
170
+ this.errorLog(`Water Heater: ${this.accessory.displayName} Command is not supported.`);
161
171
  break;
162
172
  case 161:
163
- this.platform.log.error(`Water Heater: ${this.accessory.displayName} Device is offline.`);
173
+ this.errorLog(`Water Heater: ${this.accessory.displayName} Device is offline.`);
164
174
  break;
165
175
  case 171:
166
- this.platform.log.error(`Water Heater: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
176
+ this.errorLog(`Water Heater: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
167
177
  break;
168
178
  case 190:
169
- this.platform.log.error(`Water Heater: ${this.accessory.displayName} Device internal error due to device states not synchronized`
179
+ this.errorLog(`Water Heater: ${this.accessory.displayName} Device internal error due to device states not synchronized`
170
180
  + ` with server, Or command: ${JSON.stringify(push.data)} format is invalid`);
171
181
  break;
172
182
  case 100:
173
- this.platform.debug(`Water Heater: ${this.accessory.displayName} Command successfully sent.`);
183
+ this.debugLog(`Water Heater: ${this.accessory.displayName} Command successfully sent.`);
174
184
  break;
175
185
  default:
176
- this.platform.debug(`Water Heater: ${this.accessory.displayName} Unknown statusCode.`);
186
+ this.debugLog(`Water Heater: ${this.accessory.displayName} Unknown statusCode.`);
187
+ }
188
+ }
189
+
190
+ public apiError() {
191
+ throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
192
+ }
193
+
194
+ /**
195
+ * Logging for Device
196
+ */
197
+ infoLog(...log: any[]) {
198
+ if (this.enablingDeviceLogging()) {
199
+ this.platform.log.info(String(...log));
200
+ }
201
+ }
202
+
203
+ warnLog(...log: any[]) {
204
+ if (this.enablingDeviceLogging()) {
205
+ this.platform.log.warn(String(...log));
206
+ }
207
+ }
208
+
209
+ errorLog(...log: any[]) {
210
+ if (this.enablingDeviceLogging()) {
211
+ this.platform.log.error(String(...log));
212
+ }
213
+ }
214
+
215
+ debugLog(...log: any[]) {
216
+ if (this.enablingDeviceLogging()) {
217
+ if (this.deviceLogging === 'debug') {
218
+ this.platform.log.info('[DEBUG]', String(...log));
219
+ } else {
220
+ this.platform.log.debug(String(...log));
221
+ }
177
222
  }
178
223
  }
179
224
 
180
- public apiError(e: any) {
181
- this.service.updateCharacteristic(this.platform.Characteristic.Active, e);
225
+ enablingDeviceLogging(): boolean {
226
+ return this.deviceLogging === 'debug' || this.deviceLogging === 'standard';
182
227
  }
183
228
  }