@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
  /**
@@ -27,8 +27,7 @@ export class Fan {
27
27
  minStep?: number;
28
28
  minValue?: number;
29
29
  maxValue?: number;
30
- private readonly deviceDebug = this.platform.config.options?.debug === 'device' || this.platform.debugMode;
31
- private readonly debugDebug = this.platform.config.options?.debug === 'debug' || this.platform.debugMode;
30
+ deviceLogging!: string;
32
31
 
33
32
  constructor(
34
33
  private readonly platform: SwitchBotPlatform,
@@ -36,6 +35,7 @@ export class Fan {
36
35
  public device: irdevice & irDevicesConfig,
37
36
  ) {
38
37
  // default placeholders
38
+ this.logs();
39
39
  if (this.Active === undefined) {
40
40
  this.Active = this.platform.Characteristic.Active.INACTIVE;
41
41
  } else {
@@ -96,10 +96,10 @@ export class Fan {
96
96
  !device.irfan?.swing_mode) {
97
97
  const characteristic = this.service.getCharacteristic(this.platform.Characteristic.RotationSpeed);
98
98
  this.service.removeCharacteristic(characteristic);
99
- this.platform.device(`Fan: ${this.accessory.displayName} Rotation Speed Characteristic was removed.`);
99
+ this.debugLog(`Fan: ${this.accessory.displayName} Rotation Speed Characteristic was removed.`);
100
100
  } else {
101
101
  // eslint-disable-next-line max-len
102
- this.platform.device(`Fan: ${this.accessory.displayName} Rotation Speed Characteristic was not removed or not added. To Remove Chracteristic, Clear Cache on this Accessory.`);
102
+ this.debugLog(`Fan: ${this.accessory.displayName} Rotation Speed Characteristic was not removed or not added. To Remove Chracteristic, Clear Cache on this Accessory.`);
103
103
  }
104
104
 
105
105
  if (device.irfan?.swing_mode) {
@@ -108,16 +108,26 @@ export class Fan {
108
108
  } else if (this.service.testCharacteristic(this.platform.Characteristic.SwingMode) && !device.irfan?.swing_mode) {
109
109
  const characteristic = this.service.getCharacteristic(this.platform.Characteristic.SwingMode);
110
110
  this.service.removeCharacteristic(characteristic);
111
- this.platform.device(`Fan: ${this.accessory.displayName} Swing Mode Characteristic was removed.`);
111
+ this.debugLog(`Fan: ${this.accessory.displayName} Swing Mode Characteristic was removed.`);
112
112
  } else {
113
113
  // eslint-disable-next-line max-len
114
- this.platform.device(`Fan: ${this.accessory.displayName} Swing Mode Characteristic was not removed or not added. To Remove Chracteristic, Clear Cache on this Accessory.`);
114
+ this.debugLog(`Fan: ${this.accessory.displayName} Swing Mode Characteristic was not removed or not added. To Remove Chracteristic, Clear Cache on this Accessory.`);
115
115
 
116
116
  }
117
117
  }
118
118
 
119
+ logs() {
120
+ if (this.device.logging) {
121
+ this.deviceLogging = this.accessory.context.logging = this.device.logging;
122
+ } else if (this.platform.config.options?.logging) {
123
+ this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
124
+ } else {
125
+ this.deviceLogging = this.accessory.context.logging = 'standard';
126
+ }
127
+ }
128
+
119
129
  private SwingModeSet(value: CharacteristicValue) {
120
- this.platform.debug(`Fan: ${this.accessory.displayName} SwingMode: ${value}`);
130
+ this.debugLog(`Fan: ${this.accessory.displayName} SwingMode: ${value}`);
121
131
  if (value > this.SwingMode) {
122
132
  this.SwingMode = 1;
123
133
  this.pushFanOnChanges();
@@ -132,27 +142,27 @@ export class Fan {
132
142
 
133
143
  private updateHomeKitCharacteristics() {
134
144
  if (this.Active === undefined) {
135
- this.platform.debug(`Fan: ${this.accessory.displayName} Active: ${this.Active}`);
145
+ this.debugLog(`Fan: ${this.accessory.displayName} Active: ${this.Active}`);
136
146
  } else {
137
147
  this.service?.updateCharacteristic(this.platform.Characteristic.Active, this.Active);
138
- this.platform.device(`Fan: ${this.accessory.displayName} updateCharacteristic Active: ${this.Active}`);
148
+ this.debugLog(`Fan: ${this.accessory.displayName} updateCharacteristic Active: ${this.Active}`);
139
149
  }
140
150
  if (this.SwingMode === undefined) {
141
- this.platform.debug(`Fan: ${this.accessory.displayName} SwingMode: ${this.SwingMode}`);
151
+ this.debugLog(`Fan: ${this.accessory.displayName} SwingMode: ${this.SwingMode}`);
142
152
  } else {
143
153
  this.service?.updateCharacteristic(this.platform.Characteristic.SwingMode, this.SwingMode);
144
- this.platform.device(`Fan: ${this.accessory.displayName} updateCharacteristic SwingMode: ${this.SwingMode}`);
154
+ this.debugLog(`Fan: ${this.accessory.displayName} updateCharacteristic SwingMode: ${this.SwingMode}`);
145
155
  }
146
156
  if (this.RotationSpeed === undefined) {
147
- this.platform.debug(`Fan: ${this.accessory.displayName} RotationSpeed: ${this.RotationSpeed}`);
157
+ this.debugLog(`Fan: ${this.accessory.displayName} RotationSpeed: ${this.RotationSpeed}`);
148
158
  } else {
149
159
  this.service?.updateCharacteristic(this.platform.Characteristic.RotationSpeed, this.RotationSpeed);
150
- this.platform.device(`Fan: ${this.accessory.displayName} updateCharacteristic RotationSpeed: ${this.RotationSpeed}`);
160
+ this.debugLog(`Fan: ${this.accessory.displayName} updateCharacteristic RotationSpeed: ${this.RotationSpeed}`);
151
161
  }
152
162
  }
153
163
 
154
164
  private RotationSpeedSet(value: CharacteristicValue) {
155
- this.platform.debug(`Fan: ${this.accessory.displayName} RotationSpeed: ${value}`);
165
+ this.debugLog(`Fan: ${this.accessory.displayName} RotationSpeed: ${value}`);
156
166
  if (value > this.RotationSpeed) {
157
167
  this.RotationSpeed = 1;
158
168
  this.pushFanSpeedUpChanges();
@@ -165,7 +175,7 @@ export class Fan {
165
175
  }
166
176
 
167
177
  private ActiveSet(value: CharacteristicValue) {
168
- this.platform.debug(`Fan: ${this.accessory.displayName} Active: ${value}`);
178
+ this.debugLog(`Fan: ${this.accessory.displayName} Active: ${value}`);
169
179
  if (value === this.platform.Characteristic.Active.INACTIVE) {
170
180
  this.pushFanOffChanges();
171
181
  } else {
@@ -234,60 +244,93 @@ export class Fan {
234
244
 
235
245
  public async pushTVChanges(payload: payload) {
236
246
  try {
237
- this.platform.log.info(`Fan: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
247
+ this.infoLog(`Fan: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
238
248
  + ` parameter: ${payload.parameter}, commandType: ${payload.commandType}`);
239
249
 
240
250
  // Make the API request
241
251
  const push = await this.platform.axios.post(`${DeviceURL}/${this.device.deviceId}/commands`, payload);
242
- this.platform.debug(`Fan: ${this.accessory.displayName} pushTVChanges: ${push.data}`);
252
+ this.debugLog(`Fan: ${this.accessory.displayName} pushTVChanges: ${push.data}`);
243
253
  this.statusCode(push);
244
254
  this.updateHomeKitCharacteristics();
245
255
  } catch (e: any) {
246
- this.platform.log.error(`Fan: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
247
- if (this.deviceDebug) {
248
- this.platform.log.error(`Fan: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
256
+ this.errorLog(`Fan: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
257
+ if (this.deviceLogging === 'debug') {
258
+ this.errorLog(`Fan: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
249
259
  + ` Error Message: ${JSON.stringify(e.message)}`);
250
260
  }
251
- if (this.debugDebug) {
252
- this.platform.log.error(`Fan: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
261
+ if (this.platform.debugMode) {
262
+ this.errorLog(`Fan: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
253
263
  + ` Error: ${JSON.stringify(e)}`);
254
264
  }
255
- this.apiError(e);
265
+ this.apiError();
256
266
  }
257
267
  }
258
268
 
259
269
  private statusCode(push: AxiosResponse<{ statusCode: number; }>) {
260
270
  switch (push.data.statusCode) {
261
271
  case 151:
262
- this.platform.log.error(`Fan: ${this.accessory.displayName} Command not supported by this device type.`);
272
+ this.errorLog(`Fan: ${this.accessory.displayName} Command not supported by this device type.`);
263
273
  break;
264
274
  case 152:
265
- this.platform.log.error(`Fan: ${this.accessory.displayName} Device not found.`);
275
+ this.errorLog(`Fan: ${this.accessory.displayName} Device not found.`);
266
276
  break;
267
277
  case 160:
268
- this.platform.log.error(`Fan: ${this.accessory.displayName} Command is not supported.`);
278
+ this.errorLog(`Fan: ${this.accessory.displayName} Command is not supported.`);
269
279
  break;
270
280
  case 161:
271
- this.platform.log.error(`Fan: ${this.accessory.displayName} Device is offline.`);
281
+ this.errorLog(`Fan: ${this.accessory.displayName} Device is offline.`);
272
282
  break;
273
283
  case 171:
274
- this.platform.log.error(`Fan: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
284
+ this.errorLog(`Fan: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
275
285
  break;
276
286
  case 190:
277
- this.platform.log.error(`Fan: ${this.accessory.displayName} Device internal error due to device states not synchronized`
287
+ this.errorLog(`Fan: ${this.accessory.displayName} Device internal error due to device states not synchronized`
278
288
  + ` with server, Or command: ${JSON.stringify(push.data)} format is invalid`);
279
289
  break;
280
290
  case 100:
281
- this.platform.debug(`Fan: ${this.accessory.displayName} Command successfully sent.`);
291
+ this.debugLog(`Fan: ${this.accessory.displayName} Command successfully sent.`);
282
292
  break;
283
293
  default:
284
- this.platform.debug(`Fan: ${this.accessory.displayName} Unknown statusCode.`);
294
+ this.debugLog(`Fan: ${this.accessory.displayName} Unknown statusCode.`);
295
+ }
296
+ }
297
+
298
+ public apiError() {
299
+ throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
300
+ }
301
+
302
+ /**
303
+ * Logging for Device
304
+ */
305
+ infoLog(...log: any[]) {
306
+ if (this.enablingDeviceLogging()) {
307
+ this.platform.log.info(String(...log));
308
+ }
309
+ }
310
+
311
+ warnLog(...log: any[]) {
312
+ if (this.enablingDeviceLogging()) {
313
+ this.platform.log.warn(String(...log));
314
+ }
315
+ }
316
+
317
+ errorLog(...log: any[]) {
318
+ if (this.enablingDeviceLogging()) {
319
+ this.platform.log.error(String(...log));
320
+ }
321
+ }
322
+
323
+ debugLog(...log: any[]) {
324
+ if (this.enablingDeviceLogging()) {
325
+ if (this.deviceLogging === 'debug') {
326
+ this.platform.log.info('[DEBUG]', String(...log));
327
+ } else {
328
+ this.platform.log.debug(String(...log));
329
+ }
285
330
  }
286
331
  }
287
332
 
288
- public apiError(e: any) {
289
- this.service.updateCharacteristic(this.platform.Characteristic.Active, e);
290
- this.service.updateCharacteristic(this.platform.Characteristic.RotationSpeed, e);
291
- this.service.updateCharacteristic(this.platform.Characteristic.SwingMode, e);
333
+ enablingDeviceLogging(): boolean {
334
+ return this.deviceLogging === 'debug' || this.deviceLogging === 'standard';
292
335
  }
293
336
  }
@@ -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 Light {
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 Light {
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 {
@@ -60,7 +60,7 @@ export class Light {
60
60
  /* this.service
61
61
  .getCharacteristic(this.platform.Characteristic.Brightness)
62
62
  .on(CharacteristicEventTypes.SET, (value: any, callback: CharacteristicGetCallback) => {
63
- this.platform.debug(`${this.device.remoteType} ${this.accessory.displayName} Set Brightness: ${value}`);
63
+ this.debugLog(`${this.device.remoteType} ${this.accessory.displayName} Set Brightness: ${value}`);
64
64
  this.Brightness = value;
65
65
  if (value > this.Brightness) {
66
66
  this.pushLightBrightnessUpChanges();
@@ -72,8 +72,18 @@ export class Light {
72
72
  });*/
73
73
  }
74
74
 
75
+ logs() {
76
+ if (this.device.logging) {
77
+ this.deviceLogging = this.accessory.context.logging = this.device.logging;
78
+ } else if (this.platform.config.options?.logging) {
79
+ this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
80
+ } else {
81
+ this.deviceLogging = this.accessory.context.logging = 'standard';
82
+ }
83
+ }
84
+
75
85
  private OnSet(value: CharacteristicValue) {
76
- this.platform.debug(`Light: ${this.accessory.displayName} On: ${value}`);
86
+ this.debugLog(`Light: ${this.accessory.displayName} On: ${value}`);
77
87
  this.On = value;
78
88
  if (this.On) {
79
89
  this.pushLightOnChanges();
@@ -84,10 +94,10 @@ export class Light {
84
94
 
85
95
  private updateHomeKitCharacteristics() {
86
96
  if (this.On === undefined) {
87
- this.platform.debug(`Light: ${this.accessory.displayName} On: ${this.On}`);
97
+ this.debugLog(`Light: ${this.accessory.displayName} On: ${this.On}`);
88
98
  } else {
89
99
  this.service?.updateCharacteristic(this.platform.Characteristic.On, this.On);
90
- this.platform.device(`Light: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
100
+ this.debugLog(`Light: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
91
101
  }
92
102
  }
93
103
 
@@ -143,27 +153,27 @@ export class Light {
143
153
 
144
154
  public async pushChanges(payload: payload) {
145
155
  try {
146
- this.platform.log.info(`Light: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
156
+ this.infoLog(`Light: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
147
157
  + ` parameter: ${payload.parameter}, commandType: ${payload.commandType}`);
148
158
 
149
159
  // Make the API request
150
160
  const push = await this.platform.axios.post(`${DeviceURL}/${this.device.deviceId}/commands`, payload);
151
- this.platform.debug(`Light: ${this.accessory.displayName} pushChanges: ${push.data}`);
161
+ this.debugLog(`Light: ${this.accessory.displayName} pushChanges: ${push.data}`);
152
162
  this.statusCode(push);
153
163
  this.OnCached = this.On;
154
164
  this.accessory.context.On = this.OnCached;
155
165
  this.updateHomeKitCharacteristics();
156
166
  } catch (e: any) {
157
- this.platform.log.error(`Light: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
158
- if (this.deviceDebug) {
159
- this.platform.log.error(`Light: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
167
+ this.errorLog(`Light: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
168
+ if (this.deviceLogging === 'debug') {
169
+ this.errorLog(`Light: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
160
170
  + ` Error Message: ${JSON.stringify(e.message)}`);
161
171
  }
162
- if (this.debugDebug) {
163
- this.platform.log.error(`Light: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
172
+ if (this.platform.debugMode) {
173
+ this.errorLog(`Light: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
164
174
  + ` Error: ${JSON.stringify(e)}`);
165
175
  }
166
- this.apiError(e);
176
+ this.apiError();
167
177
  }
168
178
  }
169
179
 
@@ -171,33 +181,68 @@ export class Light {
171
181
  private statusCode(push: AxiosResponse<{ statusCode: number; }>) {
172
182
  switch (push.data.statusCode) {
173
183
  case 151:
174
- this.platform.log.error(`Light: ${this.accessory.displayName} Command not supported by this device type.`);
184
+ this.errorLog(`Light: ${this.accessory.displayName} Command not supported by this device type.`);
175
185
  break;
176
186
  case 152:
177
- this.platform.log.error(`Light: ${this.accessory.displayName} Device not found.`);
187
+ this.errorLog(`Light: ${this.accessory.displayName} Device not found.`);
178
188
  break;
179
189
  case 160:
180
- this.platform.log.error(`Light: ${this.accessory.displayName} Command is not supported.`);
190
+ this.errorLog(`Light: ${this.accessory.displayName} Command is not supported.`);
181
191
  break;
182
192
  case 161:
183
- this.platform.log.error(`Light: ${this.accessory.displayName} Device is offline.`);
193
+ this.errorLog(`Light: ${this.accessory.displayName} Device is offline.`);
184
194
  break;
185
195
  case 171:
186
- this.platform.log.error(`Light: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
196
+ this.errorLog(`Light: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
187
197
  break;
188
198
  case 190:
189
- this.platform.log.error(`Light: ${this.accessory.displayName} Device internal error due to device states not synchronized`
199
+ this.errorLog(`Light: ${this.accessory.displayName} Device internal error due to device states not synchronized`
190
200
  + ` with server, Or command: ${JSON.stringify(push.data)} format is invalid`);
191
201
  break;
192
202
  case 100:
193
- this.platform.debug(`Light: ${this.accessory.displayName} Command successfully sent.`);
203
+ this.debugLog(`Light: ${this.accessory.displayName} Command successfully sent.`);
194
204
  break;
195
205
  default:
196
- this.platform.debug(`Light: ${this.accessory.displayName} Unknown statusCode.`);
206
+ this.debugLog(`Light: ${this.accessory.displayName} Unknown statusCode.`);
207
+ }
208
+ }
209
+
210
+ public apiError() {
211
+ throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
212
+ }
213
+
214
+ /**
215
+ * Logging for Device
216
+ */
217
+ infoLog(...log: any[]) {
218
+ if (this.enablingDeviceLogging()) {
219
+ this.platform.log.info(String(...log));
220
+ }
221
+ }
222
+
223
+ warnLog(...log: any[]) {
224
+ if (this.enablingDeviceLogging()) {
225
+ this.platform.log.warn(String(...log));
226
+ }
227
+ }
228
+
229
+ errorLog(...log: any[]) {
230
+ if (this.enablingDeviceLogging()) {
231
+ this.platform.log.error(String(...log));
232
+ }
233
+ }
234
+
235
+ debugLog(...log: any[]) {
236
+ if (this.enablingDeviceLogging()) {
237
+ if (this.deviceLogging === 'debug') {
238
+ this.platform.log.info('[DEBUG]', String(...log));
239
+ } else {
240
+ this.platform.log.debug(String(...log));
241
+ }
197
242
  }
198
243
  }
199
244
 
200
- public apiError(e: any) {
201
- this.service.updateCharacteristic(this.platform.Characteristic.On, e);
245
+ enablingDeviceLogging(): boolean {
246
+ return this.deviceLogging === 'debug' || this.deviceLogging === 'standard';
202
247
  }
203
248
  }
@@ -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 Others {
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 Others {
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 {
@@ -50,12 +50,22 @@ export class Others {
50
50
  this.service.getCharacteristic(this.platform.Characteristic.Active).onSet(this.ActiveSet.bind(this));
51
51
  } else {
52
52
  accessory.removeService(this.service!);
53
- this.platform.log.error(`Other: ${this.accessory.displayName} No Device Type Set, deviceType: ${device.other?.deviceType}`);
53
+ this.errorLog(`Other: ${this.accessory.displayName} No Device Type Set, deviceType: ${device.other?.deviceType}`);
54
+ }
55
+ }
56
+
57
+ logs() {
58
+ if (this.device.logging) {
59
+ this.deviceLogging = this.accessory.context.logging = this.device.logging;
60
+ } else if (this.platform.config.options?.logging) {
61
+ this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
62
+ } else {
63
+ this.deviceLogging = this.accessory.context.logging = 'standard';
54
64
  }
55
65
  }
56
66
 
57
67
  private ActiveSet(value: CharacteristicValue) {
58
- this.platform.debug(`Other: ${this.accessory.displayName} On: ${value}`);
68
+ this.debugLog(`Other: ${this.accessory.displayName} On: ${value}`);
59
69
  if (this.Active) {
60
70
  this.pushOnChanges();
61
71
  } else {
@@ -68,10 +78,10 @@ export class Others {
68
78
 
69
79
  private updateHomeKitCharacteristics() {
70
80
  if (this.Active === undefined) {
71
- this.platform.debug(`Other: ${this.accessory.displayName} Active: ${this.Active}`);
81
+ this.debugLog(`Other: ${this.accessory.displayName} Active: ${this.Active}`);
72
82
  } else {
73
83
  this.service?.updateCharacteristic(this.platform.Characteristic.Active, this.Active);
74
- this.platform.device(`Other: ${this.accessory.displayName} updateCharacteristic Active: ${this.Active}`);
84
+ this.debugLog(`Other: ${this.accessory.displayName} updateCharacteristic Active: ${this.Active}`);
75
85
  }
76
86
  }
77
87
 
@@ -98,13 +108,13 @@ export class Others {
98
108
  await this.pushChanges(payload);
99
109
  }
100
110
  } else {
101
- this.platform.log.error(`Other: ${this.accessory.displayName} On Command not set, commandOn: ${this.device.other.commandOn}`);
111
+ this.errorLog(`Other: ${this.accessory.displayName} On Command not set, commandOn: ${this.device.other.commandOn}`);
102
112
  }
103
113
  } else {
104
- this.platform.log.error(`Other: ${this.accessory.displayName} On Command not set, other: ${this.device.other}`);
114
+ this.errorLog(`Other: ${this.accessory.displayName} On Command not set, other: ${this.device.other}`);
105
115
  }
106
116
  } else {
107
- this.platform.log.error(`Other: ${this.accessory.displayName} On Command not set`);
117
+ this.errorLog(`Other: ${this.accessory.displayName} On Command not set`);
108
118
  }
109
119
  }
110
120
 
@@ -121,70 +131,105 @@ export class Others {
121
131
  await this.pushChanges(payload);
122
132
  }
123
133
  } else {
124
- this.platform.log.error(`Other: ${this.accessory.displayName} Off Command not set, commandOff: ${this.device.other.commandOff}`);
134
+ this.errorLog(`Other: ${this.accessory.displayName} Off Command not set, commandOff: ${this.device.other.commandOff}`);
125
135
  }
126
136
  } else {
127
- this.platform.log.error(`Other: ${this.accessory.displayName} Off Command not set, other: ${this.device.other}`);
137
+ this.errorLog(`Other: ${this.accessory.displayName} Off Command not set, other: ${this.device.other}`);
128
138
  }
129
139
  } else {
130
- this.platform.log.error(`Other: ${this.accessory.displayName} Off Command not set.`);
140
+ this.errorLog(`Other: ${this.accessory.displayName} Off Command not set.`);
131
141
  }
132
142
  }
133
143
 
134
144
  public async pushChanges(payload: payload) {
135
145
  try {
136
- this.platform.log.info(`Other: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
146
+ this.infoLog(`Other: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
137
147
  + ` parameter: ${payload.parameter}, commandType: ${payload.commandType}`);
138
148
 
139
149
  // Make the API request
140
150
  const push = await this.platform.axios.post(`${DeviceURL}/${this.device.deviceId}/commands`, payload);
141
- this.platform.debug(`Other: ${this.accessory.displayName} pushChanges: ${push.data}`);
151
+ this.debugLog(`Other: ${this.accessory.displayName} pushChanges: ${push.data}`);
142
152
  this.statusCode(push);
143
153
  this.updateHomeKitCharacteristics();
144
154
  } catch (e: any) {
145
- this.platform.log.error(`Other: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
146
- if (this.deviceDebug) {
147
- this.platform.log.error(`Other: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
155
+ this.errorLog(`Other: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
156
+ if (this.deviceLogging === 'debug') {
157
+ this.errorLog(`Other: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
148
158
  + ` Error Message: ${JSON.stringify(e.message)}`);
149
159
  }
150
- if (this.debugDebug) {
151
- this.platform.log.error(`Other: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
160
+ if (this.platform.debugMode) {
161
+ this.errorLog(`Other: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
152
162
  + ` Error: ${JSON.stringify(e)}`);
153
163
  }
154
- this.apiError(e);
164
+ this.apiError();
155
165
  }
156
166
  }
157
167
 
158
168
  private statusCode(push: AxiosResponse<{ statusCode: number; }>) {
159
169
  switch (push.data.statusCode) {
160
170
  case 151:
161
- this.platform.log.error(`Other: ${this.accessory.displayName} Command not supported by this device type.`);
171
+ this.errorLog(`Other: ${this.accessory.displayName} Command not supported by this device type.`);
162
172
  break;
163
173
  case 152:
164
- this.platform.log.error(`Other: ${this.accessory.displayName} Device not found.`);
174
+ this.errorLog(`Other: ${this.accessory.displayName} Device not found.`);
165
175
  break;
166
176
  case 160:
167
- this.platform.log.error(`Other: ${this.accessory.displayName} Command is not supported.`);
177
+ this.errorLog(`Other: ${this.accessory.displayName} Command is not supported.`);
168
178
  break;
169
179
  case 161:
170
- this.platform.log.error(`Other: ${this.accessory.displayName} Device is offline.`);
180
+ this.errorLog(`Other: ${this.accessory.displayName} Device is offline.`);
171
181
  break;
172
182
  case 171:
173
- this.platform.log.error(`Other: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
183
+ this.errorLog(`Other: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
174
184
  break;
175
185
  case 190:
176
- this.platform.log.error(`Other: ${this.accessory.displayName} Device internal error due to device states not synchronized`
186
+ this.errorLog(`Other: ${this.accessory.displayName} Device internal error due to device states not synchronized`
177
187
  + ` with server, Or command: ${JSON.stringify(push.data)} format is invalid`);
178
188
  break;
179
189
  case 100:
180
- this.platform.debug(`Other: ${this.accessory.displayName} Command successfully sent.`);
190
+ this.debugLog(`Other: ${this.accessory.displayName} Command successfully sent.`);
181
191
  break;
182
192
  default:
183
- this.platform.debug(`Other: ${this.accessory.displayName} Unknown statusCode.`);
193
+ this.debugLog(`Other: ${this.accessory.displayName} Unknown statusCode.`);
194
+ }
195
+ }
196
+
197
+ public apiError() {
198
+ throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
199
+ }
200
+
201
+ /**
202
+ * Logging for Device
203
+ */
204
+ infoLog(...log: any[]) {
205
+ if (this.enablingDeviceLogging()) {
206
+ this.platform.log.info(String(...log));
207
+ }
208
+ }
209
+
210
+ warnLog(...log: any[]) {
211
+ if (this.enablingDeviceLogging()) {
212
+ this.platform.log.warn(String(...log));
213
+ }
214
+ }
215
+
216
+ errorLog(...log: any[]) {
217
+ if (this.enablingDeviceLogging()) {
218
+ this.platform.log.error(String(...log));
219
+ }
220
+ }
221
+
222
+ debugLog(...log: any[]) {
223
+ if (this.enablingDeviceLogging()) {
224
+ if (this.deviceLogging === 'debug') {
225
+ this.platform.log.info('[DEBUG]', String(...log));
226
+ } else {
227
+ this.platform.log.debug(String(...log));
228
+ }
184
229
  }
185
230
  }
186
231
 
187
- public apiError(e: any) {
188
- this.service?.updateCharacteristic(this.platform.Characteristic.Active, e);
232
+ enablingDeviceLogging(): boolean {
233
+ return this.deviceLogging === 'debug' || this.deviceLogging === 'standard';
189
234
  }
190
235
  }