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

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 +20 -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 +172 -106
  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 +166 -92
  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 +131 -56
  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 +208 -131
  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 +191 -117
  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 +125 -53
  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 +111 -41
  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 +100 -35
  34. package/dist/devices/plugs.js.map +1 -1
  35. package/dist/irdevices/airconditioners.d.ts +11 -6
  36. package/dist/irdevices/airconditioners.d.ts.map +1 -1
  37. package/dist/irdevices/airconditioners.js +91 -85
  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 +72 -28
  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 +65 -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 +77 -33
  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 +66 -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 +72 -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 +83 -41
  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 +65 -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 +65 -24
  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 +10 -6
  80. package/src/devices/bots.ts +178 -107
  81. package/src/devices/colorbulb.ts +175 -95
  82. package/src/devices/contact.ts +138 -57
  83. package/src/devices/curtains.ts +216 -135
  84. package/src/devices/humidifiers.ts +198 -119
  85. package/src/devices/meters.ts +132 -54
  86. package/src/devices/motion.ts +117 -41
  87. package/src/devices/plugs.ts +105 -36
  88. package/src/irdevices/airconditioners.ts +96 -89
  89. package/src/irdevices/airpurifiers.ts +77 -28
  90. package/src/irdevices/cameras.ts +70 -24
  91. package/src/irdevices/fans.ts +82 -34
  92. package/src/irdevices/lights.ts +71 -25
  93. package/src/irdevices/others.ts +76 -30
  94. package/src/irdevices/tvs.ts +88 -41
  95. package/src/irdevices/vacuumcleaners.ts +70 -24
  96. package/src/irdevices/waterheaters.ts +70 -24
  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
@@ -2,7 +2,7 @@ import { AxiosResponse } from 'axios';
2
2
  import { interval, Subject } from 'rxjs';
3
3
  import { SwitchBotPlatform } from '../platform';
4
4
  import { debounceTime, skipWhile, tap } from 'rxjs/operators';
5
- import { Service, PlatformAccessory, CharacteristicValue, ControllerConstructor, Controller, ControllerServiceMap, HAPStatus } from 'homebridge';
5
+ import { Service, PlatformAccessory, CharacteristicValue, ControllerConstructor, Controller, ControllerServiceMap } from 'homebridge';
6
6
  import { DeviceURL, device, devicesConfig, switchbot, deviceStatusResponse, payload, hs2rgb, rgb2hs, m2hs } from '../settings';
7
7
 
8
8
  /**
@@ -34,8 +34,8 @@ export class ColorBulb {
34
34
 
35
35
  // Config
36
36
  set_minStep?: number;
37
- private readonly deviceDebug = this.platform.config.options?.debug === 'device' || this.platform.debugMode;
38
- private readonly debugDebug = this.platform.config.options?.debug === 'debug' || this.platform.debugMode;
37
+ deviceLogging!: string;
38
+ deviceRefreshRate!: number;
39
39
 
40
40
  // Adaptive Lighting
41
41
  AdaptiveLightingController: ControllerConstructor | Controller<ControllerServiceMap>;
@@ -52,10 +52,9 @@ export class ColorBulb {
52
52
  private accessory: PlatformAccessory,
53
53
  public device: device & devicesConfig,
54
54
  ) {
55
- // ColorBulb Config
56
- this.platform.device(`Color Bulb: ${this.accessory.displayName} Config: (ble: ${device.ble}, set_minStep: ${device.colorbulb?.set_minStep}`);
57
-
58
55
  // default placeholders
56
+ this.refreshRate();
57
+ this.logs();
59
58
  if (this.On === undefined) {
60
59
  this.On = false;
61
60
  } else {
@@ -68,6 +67,9 @@ export class ColorBulb {
68
67
  this.minKelvin = 2000;
69
68
  this.maxKelvin = 9000;
70
69
 
70
+ // ColorBulb Config
71
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} Config: (ble: ${device.ble}, set_minStep: ${device.colorbulb?.set_minStep}`);
72
+
71
73
  // this is subject we use to track when we need to POST changes to the SwitchBot API
72
74
  this.doColorBulbUpdate = new Subject();
73
75
  this.colorBulbUpdateInProgress = false;
@@ -161,7 +163,7 @@ export class ColorBulb {
161
163
  this.updateHomeKitCharacteristics();
162
164
 
163
165
  // Start an update interval
164
- interval(this.platform.config.options!.refreshRate! * 1000)
166
+ interval(this.deviceRefreshRate * 1000)
165
167
  .pipe(skipWhile(() => this.colorBulbUpdateInProgress))
166
168
  .subscribe(() => {
167
169
  this.refreshStatus();
@@ -180,22 +182,55 @@ export class ColorBulb {
180
182
  try {
181
183
  await this.pushChanges();
182
184
  } catch (e: any) {
183
- this.platform.log.error(`Color Bulb: ${this.accessory.displayName} failed pushChanges`);
184
- if (this.deviceDebug) {
185
- this.platform.log.error(`Color Bulb: ${this.accessory.displayName} failed pushChanges,`
185
+ this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges`);
186
+ if (this.deviceLogging === 'debug') {
187
+ this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges,`
186
188
  + ` Error Message: ${JSON.stringify(e.message)}`);
187
189
  }
188
- if (this.debugDebug) {
189
- this.platform.log.error(`Color Bulb: ${this.accessory.displayName} failed pushChanges,`
190
+ if (this.platform.debugMode) {
191
+ this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges,`
190
192
  + ` Error: ${JSON.stringify(e)}`);
191
193
  }
192
- this.apiError();
194
+ this.apiError(e);
193
195
  }
194
196
  this.colorBulbUpdateInProgress = false;
195
197
  });
196
198
  }
197
199
 
198
- private minStep(): number | undefined {
200
+ refreshRate() {
201
+ if (this.device.refreshRate) {
202
+ this.deviceRefreshRate = this.accessory.context.refreshRate = this.device.refreshRate;
203
+ if (this.platform.debugMode) {
204
+ this.warnLog(`Using Device Config refreshRate: ${this.deviceRefreshRate}`);
205
+ }
206
+ } else if (this.platform.config.options!.refreshRate) {
207
+ this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options!.refreshRate;
208
+ if (this.platform.debugMode) {
209
+ this.warnLog(`Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
210
+ }
211
+ }
212
+ }
213
+
214
+ logs() {
215
+ if (this.device.logging) {
216
+ this.deviceLogging = this.accessory.context.logging = this.device.logging;
217
+ if (this.platform.debugMode) {
218
+ this.warnLog(`Using Device Config Logging: ${this.deviceLogging}`);
219
+ }
220
+ } else if (this.platform.config.options?.logging) {
221
+ this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
222
+ if (this.platform.debugMode) {
223
+ this.warnLog(`Using Platform Config Logging: ${this.deviceLogging}`);
224
+ }
225
+ } else {
226
+ this.deviceLogging = this.accessory.context.logging = 'standard';
227
+ if (this.platform.debugMode) {
228
+ this.warnLog('Using Device Standard Logging');
229
+ }
230
+ }
231
+ }
232
+
233
+ private minStep(): number {
199
234
  if (this.device.colorbulb?.set_minStep) {
200
235
  this.set_minStep = this.device.colorbulb?.set_minStep;
201
236
  } else {
@@ -212,61 +247,61 @@ export class ColorBulb {
212
247
  default:
213
248
  this.On = false;
214
249
  }
215
- this.platform.device(`Color Bulb: ${this.accessory.displayName} On: ${this.On}`);
250
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} On: ${this.On}`);
216
251
 
217
252
  // Brightness
218
253
  this.Brightness = Number(this.deviceStatus.body.brightness);
219
- this.platform.device(`Color Bulb: ${this.accessory.displayName} Brightness: ${this.Brightness}`);
254
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} Brightness: ${this.Brightness}`);
220
255
 
221
256
  // Color, Hue & Brightness
222
257
  if (this.deviceStatus.body.color) {
223
- this.platform.device(`Color Bulb: ${this.accessory.displayName} color: ${JSON.stringify(this.deviceStatus.body.color)}`);
258
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} color: ${JSON.stringify(this.deviceStatus.body.color)}`);
224
259
  const [red, green, blue] = this.deviceStatus.body.color!.split(':');
225
- this.platform.device(`Color Bulb: ${this.accessory.displayName} red: ${JSON.stringify(red)}`);
226
- this.platform.device(`Color Bulb: ${this.accessory.displayName} green: ${JSON.stringify(green)}`);
227
- this.platform.device(`Color Bulb: ${this.accessory.displayName} blue: ${JSON.stringify(blue)}`);
260
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} red: ${JSON.stringify(red)}`);
261
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} green: ${JSON.stringify(green)}`);
262
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} blue: ${JSON.stringify(blue)}`);
228
263
 
229
264
  const [hue, saturation] = rgb2hs(Number(red), Number(green), Number(blue));
230
- this.platform.device(`Color Bulb: ${this.accessory.displayName} hs: ${JSON.stringify(rgb2hs(Number(red), Number(green), Number(blue)))}`);
265
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} hs: ${JSON.stringify(rgb2hs(Number(red), Number(green), Number(blue)))}`);
231
266
 
232
267
  // Hue
233
268
  this.Hue = hue;
234
- this.platform.device(`Color Bulb: ${this.accessory.displayName} Hue: ${this.Hue}`);
269
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} Hue: ${this.Hue}`);
235
270
 
236
271
  // Saturation
237
272
  this.Saturation = saturation;
238
- this.platform.device(`Color Bulb: ${this.accessory.displayName} Saturation: ${this.Saturation}`);
273
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} Saturation: ${this.Saturation}`);
239
274
  }
240
275
 
241
276
  // ColorTemperature
242
277
  if (!Number.isNaN(this.deviceStatus.body.colorTemperature)) {
243
- this.platform.device(`Color Bulb: ${this.accessory.displayName} OpenAPI ColorTemperature: ${this.deviceStatus.body.colorTemperature}`);
278
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} OpenAPI ColorTemperature: ${this.deviceStatus.body.colorTemperature}`);
244
279
  const mired = Math.round(1000000 / this.deviceStatus.body.colorTemperature!);
245
280
 
246
281
  this.ColorTemperature = Number(mired);
247
282
 
248
283
  this.ColorTemperature = Math.max(Math.min(this.ColorTemperature, 500), 140);
249
- this.platform.device(`Color Bulb: ${this.accessory.displayName} ColorTemperature: ${this.ColorTemperature}`);
284
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} ColorTemperature: ${this.ColorTemperature}`);
250
285
  }
251
286
  }
252
287
 
253
288
  async refreshStatus() {
254
289
  try {
255
290
  this.deviceStatus = (await this.platform.axios.get(`${DeviceURL}/${this.device.deviceId}/status`)).data;
256
- this.platform.device(`Color Bulb: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(this.deviceStatus)}`);
291
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(this.deviceStatus)}`);
257
292
  this.parseStatus();
258
293
  this.updateHomeKitCharacteristics();
259
294
  } catch (e: any) {
260
- this.platform.log.error(`Color Bulb: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
261
- if (this.deviceDebug) {
262
- this.platform.log.error(`Color Bulb: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
295
+ this.errorLog(`Color Bulb: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
296
+ if (this.deviceLogging === 'debug') {
297
+ this.errorLog(`Color Bulb: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
263
298
  + ` Error Message: ${JSON.stringify(e.message)}`);
264
299
  }
265
- if (this.debugDebug) {
266
- this.platform.log.error(`Color Bulb: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
300
+ if (this.platform.debugMode) {
301
+ this.errorLog(`Color Bulb: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
267
302
  + ` Error: ${JSON.stringify(e)}`);
268
303
  }
269
- this.apiError();
304
+ this.apiError(e);
270
305
  }
271
306
  }
272
307
 
@@ -295,12 +330,12 @@ export class ColorBulb {
295
330
  payload.command = 'turnOff';
296
331
  }
297
332
 
298
- this.platform.log.info(`Color Bulb: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
333
+ this.infoLog(`Color Bulb: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
299
334
  + ` parameter: ${payload.parameter}, commandType: ${payload.commandType}`);
300
335
 
301
336
  // Make the API request
302
337
  const push: any = (await this.platform.axios.post(`${DeviceURL}/${this.device.deviceId}/commands`, payload));
303
- this.platform.debug(`Color Bulb: ${this.accessory.displayName} pushChanges: ${JSON.stringify(push.data)}`);
338
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} pushChanges: ${JSON.stringify(push.data)}`);
304
339
  this.statusCode(push);
305
340
  this.OnCached = this.On;
306
341
  this.accessory.context.On = this.OnCached;
@@ -321,27 +356,27 @@ export class ColorBulb {
321
356
  }
322
357
 
323
358
  } catch (e: any) {
324
- this.platform.log.error(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
325
- if (this.deviceDebug) {
326
- this.platform.log.error(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
359
+ this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
360
+ if (this.deviceLogging === 'debug') {
361
+ this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
327
362
  + ` Error Message: ${JSON.stringify(e.message)}`);
328
363
  }
329
- if (this.debugDebug) {
330
- this.platform.log.error(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
364
+ if (this.platform.debugMode) {
365
+ this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
331
366
  + ` Error: ${JSON.stringify(e)}`);
332
367
  }
333
- this.apiError();
368
+ this.apiError(e);
334
369
  }
335
370
  }
336
371
 
337
372
  private async pushHueSaturationChanges() {
338
373
  try {
339
374
  if ((this.Hue !== this.HueCached) || (this.Saturation !== this.SaturationCached)) {
340
- this.platform.device(`Color Bulb: ${this.accessory.displayName} Hue: ${JSON.stringify(this.Hue)}`);
341
- this.platform.device(`Color Bulb: ${this.accessory.displayName} Saturation: ${JSON.stringify(this.Saturation)}`);
375
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} Hue: ${JSON.stringify(this.Hue)}`);
376
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} Saturation: ${JSON.stringify(this.Saturation)}`);
342
377
 
343
378
  const [red, green, blue] = hs2rgb(Number(this.Hue), Number(this.Saturation));
344
- this.platform.device(`Color Bulb: ${this.accessory.displayName} rgb: ${JSON.stringify([red, green, blue])}`);
379
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} rgb: ${JSON.stringify([red, green, blue])}`);
345
380
 
346
381
  const payload = {
347
382
  commandType: 'command',
@@ -349,30 +384,30 @@ export class ColorBulb {
349
384
  parameter: `${red}:${green}:${blue}`,
350
385
  } as payload;
351
386
 
352
- this.platform.log.info(`Color Bulb: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
387
+ this.infoLog(`Color Bulb: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
353
388
  + ` parameter: ${payload.parameter}, commandType: ${payload.commandType}`);
354
389
 
355
390
  // Make the API request
356
391
  const push: any = (await this.platform.axios.post(`${DeviceURL}/${this.device.deviceId}/commands`, payload));
357
- this.platform.debug(`Color Bulb: ${this.accessory.displayName} pushChanges: ${JSON.stringify(push.data)}`);
392
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} pushChanges: ${JSON.stringify(push.data)}`);
358
393
  this.statusCode(push);
359
394
  this.HueCached = this.Hue;
360
395
  this.SaturationCached = this.Saturation;
361
396
  } else {
362
- this.platform.debug(`Color Bulb: ${this.accessory.displayName} No Changes.`
397
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} No Changes.`
363
398
  + `Hue: ${this.Hue}, HueCached: ${this.HueCached}, Saturation: ${this.Saturation}, SaturationCached: ${this.SaturationCached}`);
364
399
  }
365
400
  } catch (e: any) {
366
- this.platform.log.error(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
367
- if (this.deviceDebug) {
368
- this.platform.log.error(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
401
+ this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
402
+ if (this.deviceLogging === 'debug') {
403
+ this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
369
404
  + ` Error Message: ${JSON.stringify(e.message)}`);
370
405
  }
371
- if (this.debugDebug) {
372
- this.platform.log.error(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
406
+ if (this.platform.debugMode) {
407
+ this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
373
408
  + ` Error: ${JSON.stringify(e)}`);
374
409
  }
375
- this.apiError();
410
+ this.apiError(e);
376
411
  }
377
412
  }
378
413
 
@@ -388,29 +423,29 @@ export class ColorBulb {
388
423
  parameter: `${kelvin}`,
389
424
  } as payload;
390
425
 
391
- this.platform.log.info(`Color Bulb: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
426
+ this.infoLog(`Color Bulb: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
392
427
  + ` parameter: ${payload.parameter}, commandType: ${payload.commandType}`);
393
428
 
394
429
  // Make the API request
395
430
  const push: any = (await this.platform.axios.post(`${DeviceURL}/${this.device.deviceId}/commands`, payload));
396
- this.platform.debug(`Color Bulb: ${this.accessory.displayName} pushChanges: ${JSON.stringify(push.data)}`);
431
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} pushChanges: ${JSON.stringify(push.data)}`);
397
432
  this.statusCode(push);
398
433
  this.ColorTemperatureCached = this.ColorTemperature;
399
434
  } else {
400
- this.platform.debug(`Color Bulb: ${this.accessory.displayName} No Changes.`
435
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} No Changes.`
401
436
  + `ColorTemperature: ${this.ColorTemperature}, ColorTemperatureCached: ${this.ColorTemperatureCached}`);
402
437
  }
403
438
  } catch (e: any) {
404
- this.platform.log.error(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
405
- if (this.deviceDebug) {
406
- this.platform.log.error(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
439
+ this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
440
+ if (this.deviceLogging === 'debug') {
441
+ this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
407
442
  + ` Error Message: ${JSON.stringify(e.message)}`);
408
443
  }
409
- if (this.debugDebug) {
410
- this.platform.log.error(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
444
+ if (this.platform.debugMode) {
445
+ this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
411
446
  + ` Error: ${JSON.stringify(e)}`);
412
447
  }
413
- this.apiError();
448
+ this.apiError(e);
414
449
  }
415
450
  }
416
451
 
@@ -423,97 +458,106 @@ export class ColorBulb {
423
458
  parameter: `${this.Brightness}`,
424
459
  } as payload;
425
460
 
426
- this.platform.log.info(`Color Bulb: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
461
+ this.infoLog(`Color Bulb: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
427
462
  + ` parameter: ${payload.parameter}, commandType: ${payload.commandType}`);
428
463
 
429
464
  // Make the API request
430
465
  const push: any = (await this.platform.axios.post(`${DeviceURL}/${this.device.deviceId}/commands`, payload));
431
- this.platform.debug(`Color Bulb: ${this.accessory.displayName} pushChanges: ${JSON.stringify(push.data)}`);
466
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} pushChanges: ${JSON.stringify(push.data)}`);
432
467
  this.statusCode(push);
433
468
  this.BrightnessCached = this.Brightness;
434
469
  } else {
435
- this.platform.debug(`Color Bulb: ${this.accessory.displayName} No Changes.`
470
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} No Changes.`
436
471
  + `Brightness: ${this.Brightness}, BrightnessCached: ${this.BrightnessCached}`);
437
472
  }
438
473
  } catch (e: any) {
439
- this.platform.log.error(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
440
- if (this.deviceDebug) {
441
- this.platform.log.error(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
474
+ this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
475
+ if (this.deviceLogging === 'debug') {
476
+ this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
442
477
  + ` Error Message: ${JSON.stringify(e.message)}`);
443
478
  }
444
- if (this.debugDebug) {
445
- this.platform.log.error(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
479
+ if (this.platform.debugMode) {
480
+ this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
446
481
  + ` Error: ${JSON.stringify(e)}`);
447
482
  }
448
- this.apiError();
483
+ this.apiError(e);
449
484
  }
450
485
  }
451
486
 
452
487
  updateHomeKitCharacteristics() {
453
488
  if (this.On === undefined) {
454
- this.platform.debug(`Color Bulb: ${this.accessory.displayName} On: ${this.On}`);
489
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} On: ${this.On}`);
455
490
  } else {
456
491
  this.service.updateCharacteristic(this.platform.Characteristic.On, this.On);
457
- this.platform.device(`Color Bulb: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
492
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
458
493
  }
459
494
  if (this.Brightness === undefined) {
460
- this.platform.debug(`Color Bulb: ${this.accessory.displayName} Brightness: ${this.Brightness}`);
495
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} Brightness: ${this.Brightness}`);
461
496
  } else {
462
497
  this.service.updateCharacteristic(this.platform.Characteristic.Brightness, this.Brightness);
463
- this.platform.device(`Color Bulb: ${this.accessory.displayName} updateCharacteristic Brightness: ${this.Brightness}`);
498
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} updateCharacteristic Brightness: ${this.Brightness}`);
464
499
  }
465
500
  if (this.ColorTemperature === undefined) {
466
- this.platform.debug(`Color Bulb: ${this.accessory.displayName} ColorTemperature: ${this.ColorTemperature}`);
501
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} ColorTemperature: ${this.ColorTemperature}`);
467
502
  } else {
468
503
  this.service.updateCharacteristic(this.platform.Characteristic.ColorTemperature, this.ColorTemperature);
469
- this.platform.debug(`Color Bulb: ${this.accessory.displayName} updateCharacteristic ColorTemperature: ${this.ColorTemperature}`);
504
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} updateCharacteristic ColorTemperature: ${this.ColorTemperature}`);
470
505
  }
471
506
  if (this.Hue === undefined) {
472
- this.platform.debug(`Color Bulb: ${this.accessory.displayName} Hue: ${this.Hue}`);
507
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} Hue: ${this.Hue}`);
473
508
  } else {
474
509
  this.service.updateCharacteristic(this.platform.Characteristic.Hue, this.Hue);
475
- this.platform.debug(`Color Bulb: ${this.accessory.displayName} updateCharacteristic Hue: ${this.Hue}`);
510
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} updateCharacteristic Hue: ${this.Hue}`);
476
511
  }
477
512
  if (this.Saturation === undefined) {
478
- this.platform.debug(`Color Bulb: ${this.accessory.displayName} Saturation: ${this.Saturation}`);
513
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} Saturation: ${this.Saturation}`);
479
514
  } else {
480
515
  this.service.updateCharacteristic(this.platform.Characteristic.Saturation, this.Saturation);
481
- this.platform.debug(`Color Bulb: ${this.accessory.displayName} updateCharacteristic Saturation: ${this.Saturation}`);
516
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} updateCharacteristic Saturation: ${this.Saturation}`);
482
517
  }
483
518
  }
484
519
 
485
- public apiError() {
486
- throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
520
+ public apiError(e: any) {
521
+ this.service.updateCharacteristic(this.platform.Characteristic.On, e);
522
+ this.service.updateCharacteristic(this.platform.Characteristic.Hue, e);
523
+ this.service.updateCharacteristic(this.platform.Characteristic.Brightness, e);
524
+ this.service.updateCharacteristic(this.platform.Characteristic.Saturation, e);
525
+ this.service.updateCharacteristic(this.platform.Characteristic.ColorTemperature, e);
526
+ //throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
487
527
  }
488
528
 
489
529
  private statusCode(push: AxiosResponse<{ statusCode: number; }>) {
490
530
  switch (push.data.statusCode) {
491
531
  case 151:
492
- this.platform.log.error(`Color Bulb: ${this.accessory.displayName} Command not supported by this device type.`);
532
+ this.errorLog(`Color Bulb: ${this.accessory.displayName} Command not supported by this device type.`);
493
533
  break;
494
534
  case 152:
495
- this.platform.log.error(`Color Bulb: ${this.accessory.displayName} Device not found.`);
535
+ this.errorLog(`Color Bulb: ${this.accessory.displayName} Device not found.`);
496
536
  break;
497
537
  case 160:
498
- this.platform.log.error(`Color Bulb: ${this.accessory.displayName} Command is not supported.`);
538
+ this.errorLog(`Color Bulb: ${this.accessory.displayName} Command is not supported.`);
499
539
  break;
500
540
  case 161:
501
- this.platform.log.error(`Color Bulb: ${this.accessory.displayName} Device is offline.`);
541
+ this.errorLog(`Color Bulb: ${this.accessory.displayName} Device is offline.`);
502
542
  this.offlineOff();
503
543
  break;
504
544
  case 171:
505
- this.platform.log.error(`Color Bulb: ${this.accessory.displayName} is offline. Hub: ${this.device.hubDeviceId}`);
545
+ this.errorLog(`Color Bulb: ${this.accessory.displayName} is offline. Hub: ${this.device.hubDeviceId}`);
506
546
  this.offlineOff();
507
547
  break;
508
548
  case 190:
509
- this.platform.log.error(`Color Bulb: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,`
549
+ this.errorLog(`Color Bulb: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,`
510
550
  + ` Or command: ${JSON.stringify(push.data)} format is invalid`);
511
551
  break;
512
552
  case 100:
513
- this.platform.debug(`Color Bulb: ${this.accessory.displayName} Command successfully sent.`);
553
+ if (this.platform.debugMode) {
554
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} Command successfully sent.`);
555
+ }
514
556
  break;
515
557
  default:
516
- this.platform.debug(`Color Bulb: ${this.accessory.displayName} Unknown statusCode.`);
558
+ if (this.platform.debugMode) {
559
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} Unknown statusCode.`);
560
+ }
517
561
  }
518
562
  }
519
563
 
@@ -528,7 +572,7 @@ export class ColorBulb {
528
572
  * Handle requests to set the value of the "On" characteristic
529
573
  */
530
574
  OnSet(value: CharacteristicValue) {
531
- this.platform.debug(`Color Bulb: ${this.accessory.displayName} On: ${value}`);
575
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} On: ${value}`);
532
576
 
533
577
  this.On = value;
534
578
  this.doColorBulbUpdate.next();
@@ -538,7 +582,7 @@ export class ColorBulb {
538
582
  * Handle requests to set the value of the "Brightness" characteristic
539
583
  */
540
584
  BrightnessSet(value: CharacteristicValue) {
541
- this.platform.debug(`Color Bulb: ${this.accessory.displayName} Brightness: ${value}`);
585
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} Brightness: ${value}`);
542
586
 
543
587
  this.Brightness = value;
544
588
  this.doColorBulbUpdate.next();
@@ -548,7 +592,8 @@ export class ColorBulb {
548
592
  * Handle requests to set the value of the "ColorTemperature" characteristic
549
593
  */
550
594
  ColorTemperatureSet(value: CharacteristicValue) {
551
- this.platform.debug(`Color Bulb: ${this.accessory.displayName} ColorTemperature: ${value}`);
595
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} ColorTemperature: ${value}`);
596
+
552
597
 
553
598
  // Convert mired to kelvin to nearest 100 (SwitchBot seems to need this)
554
599
  const kelvin = Math.round(1000000 / Number(value) / 100) * 100;
@@ -573,7 +618,7 @@ export class ColorBulb {
573
618
  * Handle requests to set the value of the "Hue" characteristic
574
619
  */
575
620
  HueSet(value: CharacteristicValue) {
576
- this.platform.debug(`Color Bulb: ${this.accessory.displayName} Hue: ${value}`);
621
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} Hue: ${value}`);
577
622
 
578
623
  this.service.updateCharacteristic(this.platform.Characteristic.ColorTemperature, 140);
579
624
 
@@ -585,12 +630,47 @@ export class ColorBulb {
585
630
  * Handle requests to set the value of the "Saturation" characteristic
586
631
  */
587
632
  SaturationSet(value: CharacteristicValue) {
588
- this.platform.debug(`Color Bulb: ${this.accessory.displayName} Saturation: ${value}`);
633
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} Saturation: ${value}`);
589
634
 
590
635
  this.service.updateCharacteristic(this.platform.Characteristic.ColorTemperature, 140);
591
636
 
592
637
  this.Saturation = value;
593
638
  this.doColorBulbUpdate.next();
594
639
  }
640
+
641
+ /**
642
+ * Logging for Device
643
+ */
644
+ infoLog(...log: any[]) {
645
+ if (this.enablingDeviceLogging()) {
646
+ this.platform.log.info(String(...log));
647
+ }
648
+ }
649
+
650
+ warnLog(...log: any[]) {
651
+ if (this.enablingDeviceLogging()) {
652
+ this.platform.log.warn(String(...log));
653
+ }
654
+ }
655
+
656
+ errorLog(...log: any[]) {
657
+ if (this.enablingDeviceLogging()) {
658
+ this.platform.log.error(String(...log));
659
+ }
660
+ }
661
+
662
+ debugLog(...log: any[]) {
663
+ if (this.enablingDeviceLogging()) {
664
+ if (this.deviceLogging === 'debug') {
665
+ this.platform.log.info('[DEBUG]', String(...log));
666
+ } else {
667
+ this.platform.log.debug(String(...log));
668
+ }
669
+ }
670
+ }
671
+
672
+ enablingDeviceLogging(): boolean {
673
+ return this.deviceLogging === 'debug' || this.deviceLogging === 'standard';
674
+ }
595
675
  }
596
676