@switchbot/homebridge-switchbot 1.4.1-beta.1 → 1.4.1-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/config.schema.json +82 -10
  3. package/dist/devices/bots.d.ts +12 -2
  4. package/dist/devices/bots.d.ts.map +1 -1
  5. package/dist/devices/bots.js +140 -89
  6. package/dist/devices/bots.js.map +1 -1
  7. package/dist/devices/colorbulb.d.ts +12 -2
  8. package/dist/devices/colorbulb.d.ts.map +1 -1
  9. package/dist/devices/colorbulb.js +138 -84
  10. package/dist/devices/colorbulb.js.map +1 -1
  11. package/dist/devices/contact.d.ts +12 -2
  12. package/dist/devices/contact.d.ts.map +1 -1
  13. package/dist/devices/contact.js +101 -52
  14. package/dist/devices/contact.js.map +1 -1
  15. package/dist/devices/curtains.d.ts +13 -4
  16. package/dist/devices/curtains.d.ts.map +1 -1
  17. package/dist/devices/curtains.js +173 -124
  18. package/dist/devices/curtains.js.map +1 -1
  19. package/dist/devices/humidifiers.d.ts +12 -2
  20. package/dist/devices/humidifiers.d.ts.map +1 -1
  21. package/dist/devices/humidifiers.js +156 -108
  22. package/dist/devices/humidifiers.js.map +1 -1
  23. package/dist/devices/meters.d.ts +12 -2
  24. package/dist/devices/meters.d.ts.map +1 -1
  25. package/dist/devices/meters.js +98 -49
  26. package/dist/devices/meters.js.map +1 -1
  27. package/dist/devices/motion.d.ts +12 -2
  28. package/dist/devices/motion.d.ts.map +1 -1
  29. package/dist/devices/motion.js +86 -37
  30. package/dist/devices/motion.js.map +1 -1
  31. package/dist/devices/plugs.d.ts +12 -2
  32. package/dist/devices/plugs.d.ts.map +1 -1
  33. package/dist/devices/plugs.js +80 -31
  34. package/dist/devices/plugs.js.map +1 -1
  35. package/dist/irdevices/airconditioners.d.ts +10 -2
  36. package/dist/irdevices/airconditioners.d.ts.map +1 -1
  37. package/dist/irdevices/airconditioners.js +77 -37
  38. package/dist/irdevices/airconditioners.js.map +1 -1
  39. package/dist/irdevices/airpurifiers.d.ts +10 -2
  40. package/dist/irdevices/airpurifiers.d.ts.map +1 -1
  41. package/dist/irdevices/airpurifiers.js +65 -25
  42. package/dist/irdevices/airpurifiers.js.map +1 -1
  43. package/dist/irdevices/cameras.d.ts +10 -2
  44. package/dist/irdevices/cameras.d.ts.map +1 -1
  45. package/dist/irdevices/cameras.js +61 -21
  46. package/dist/irdevices/cameras.js.map +1 -1
  47. package/dist/irdevices/fans.d.ts +10 -2
  48. package/dist/irdevices/fans.d.ts.map +1 -1
  49. package/dist/irdevices/fans.js +71 -30
  50. package/dist/irdevices/fans.js.map +1 -1
  51. package/dist/irdevices/lights.d.ts +10 -2
  52. package/dist/irdevices/lights.d.ts.map +1 -1
  53. package/dist/irdevices/lights.js +62 -22
  54. package/dist/irdevices/lights.js.map +1 -1
  55. package/dist/irdevices/others.d.ts +10 -2
  56. package/dist/irdevices/others.d.ts.map +1 -1
  57. package/dist/irdevices/others.js +68 -28
  58. package/dist/irdevices/others.js.map +1 -1
  59. package/dist/irdevices/tvs.d.ts +10 -2
  60. package/dist/irdevices/tvs.d.ts.map +1 -1
  61. package/dist/irdevices/tvs.js +78 -38
  62. package/dist/irdevices/tvs.js.map +1 -1
  63. package/dist/irdevices/vacuumcleaners.d.ts +10 -2
  64. package/dist/irdevices/vacuumcleaners.d.ts.map +1 -1
  65. package/dist/irdevices/vacuumcleaners.js +61 -21
  66. package/dist/irdevices/vacuumcleaners.js.map +1 -1
  67. package/dist/irdevices/waterheaters.d.ts +10 -2
  68. package/dist/irdevices/waterheaters.d.ts.map +1 -1
  69. package/dist/irdevices/waterheaters.js +61 -21
  70. package/dist/irdevices/waterheaters.js.map +1 -1
  71. package/dist/platform.d.ts +9 -10
  72. package/dist/platform.d.ts.map +1 -1
  73. package/dist/platform.js +264 -285
  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 +1 -1
  80. package/src/devices/bots.ts +146 -89
  81. package/src/devices/colorbulb.ts +146 -86
  82. package/src/devices/contact.ts +107 -52
  83. package/src/devices/curtains.ts +182 -129
  84. package/src/devices/humidifiers.ts +162 -109
  85. package/src/devices/meters.ts +104 -49
  86. package/src/devices/motion.ts +91 -36
  87. package/src/devices/plugs.ts +84 -31
  88. package/src/irdevices/airconditioners.ts +81 -36
  89. package/src/irdevices/airpurifiers.ts +69 -24
  90. package/src/irdevices/cameras.ts +65 -20
  91. package/src/irdevices/fans.ts +75 -30
  92. package/src/irdevices/lights.ts +66 -21
  93. package/src/irdevices/others.ts +72 -27
  94. package/src/irdevices/tvs.ts +82 -37
  95. package/src/irdevices/vacuumcleaners.ts +65 -20
  96. package/src/irdevices/waterheaters.ts +65 -20
  97. package/src/platform.ts +267 -290
  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
@@ -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,24 +120,24 @@ 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
143
  this.apiError();
@@ -137,33 +147,68 @@ export class Camera {
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.`);
163
173
  }
164
174
  }
165
175
 
166
176
  public apiError() {
167
177
  throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
168
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
+ }
208
+ }
209
+ }
210
+
211
+ enablingDeviceLogging(): boolean {
212
+ return this.deviceLogging === 'debug' || this.deviceLogging === 'standard';
213
+ }
169
214
  }
@@ -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,22 +244,22 @@ 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
265
  this.apiError();
@@ -259,33 +269,68 @@ export class Fan {
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.`);
285
295
  }
286
296
  }
287
297
 
288
298
  public apiError() {
289
299
  throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
290
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
+ }
330
+ }
331
+ }
332
+
333
+ enablingDeviceLogging(): boolean {
334
+ return this.deviceLogging === 'debug' || this.deviceLogging === 'standard';
335
+ }
291
336
  }
@@ -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,24 +153,24 @@ 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
176
  this.apiError();
@@ -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.`);
197
207
  }
198
208
  }
199
209
 
200
210
  public apiError() {
201
211
  throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
202
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
+ }
242
+ }
243
+ }
244
+
245
+ enablingDeviceLogging(): boolean {
246
+ return this.deviceLogging === 'debug' || this.deviceLogging === 'standard';
247
+ }
203
248
  }