@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
@@ -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 } from 'homebridge';
5
+ import { Service, PlatformAccessory, CharacteristicValue, ControllerConstructor, Controller, ControllerServiceMap, HAPStatus } 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(e);
194
+ this.apiError();
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(e);
304
+ this.apiError();
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(e);
368
+ this.apiError();
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(e);
410
+ this.apiError();
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(e);
448
+ this.apiError();
414
449
  }
415
450
  }
416
451
 
@@ -423,101 +458,101 @@ 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(e);
483
+ this.apiError();
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(e: any) {
486
- this.service.updateCharacteristic(this.platform.Characteristic.On, e);
487
- this.service.updateCharacteristic(this.platform.Characteristic.Hue, e);
488
- this.service.updateCharacteristic(this.platform.Characteristic.Brightness, e);
489
- this.service.updateCharacteristic(this.platform.Characteristic.Saturation, e);
490
- this.service.updateCharacteristic(this.platform.Characteristic.ColorTemperature, e);
520
+ public apiError() {
521
+ throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
491
522
  }
492
523
 
493
524
  private statusCode(push: AxiosResponse<{ statusCode: number; }>) {
494
525
  switch (push.data.statusCode) {
495
526
  case 151:
496
- this.platform.log.error(`Color Bulb: ${this.accessory.displayName} Command not supported by this device type.`);
527
+ this.errorLog(`Color Bulb: ${this.accessory.displayName} Command not supported by this device type.`);
497
528
  break;
498
529
  case 152:
499
- this.platform.log.error(`Color Bulb: ${this.accessory.displayName} Device not found.`);
530
+ this.errorLog(`Color Bulb: ${this.accessory.displayName} Device not found.`);
500
531
  break;
501
532
  case 160:
502
- this.platform.log.error(`Color Bulb: ${this.accessory.displayName} Command is not supported.`);
533
+ this.errorLog(`Color Bulb: ${this.accessory.displayName} Command is not supported.`);
503
534
  break;
504
535
  case 161:
505
- this.platform.log.error(`Color Bulb: ${this.accessory.displayName} Device is offline.`);
536
+ this.errorLog(`Color Bulb: ${this.accessory.displayName} Device is offline.`);
506
537
  this.offlineOff();
507
538
  break;
508
539
  case 171:
509
- this.platform.log.error(`Color Bulb: ${this.accessory.displayName} is offline. Hub: ${this.device.hubDeviceId}`);
540
+ this.errorLog(`Color Bulb: ${this.accessory.displayName} is offline. Hub: ${this.device.hubDeviceId}`);
510
541
  this.offlineOff();
511
542
  break;
512
543
  case 190:
513
- this.platform.log.error(`Color Bulb: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,`
544
+ this.errorLog(`Color Bulb: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,`
514
545
  + ` Or command: ${JSON.stringify(push.data)} format is invalid`);
515
546
  break;
516
547
  case 100:
517
- this.platform.debug(`Color Bulb: ${this.accessory.displayName} Command successfully sent.`);
548
+ if (this.platform.debugMode) {
549
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} Command successfully sent.`);
550
+ }
518
551
  break;
519
552
  default:
520
- this.platform.debug(`Color Bulb: ${this.accessory.displayName} Unknown statusCode.`);
553
+ if (this.platform.debugMode) {
554
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} Unknown statusCode.`);
555
+ }
521
556
  }
522
557
  }
523
558
 
@@ -532,7 +567,7 @@ export class ColorBulb {
532
567
  * Handle requests to set the value of the "On" characteristic
533
568
  */
534
569
  OnSet(value: CharacteristicValue) {
535
- this.platform.debug(`Color Bulb: ${this.accessory.displayName} On: ${value}`);
570
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} On: ${value}`);
536
571
 
537
572
  this.On = value;
538
573
  this.doColorBulbUpdate.next();
@@ -542,7 +577,7 @@ export class ColorBulb {
542
577
  * Handle requests to set the value of the "Brightness" characteristic
543
578
  */
544
579
  BrightnessSet(value: CharacteristicValue) {
545
- this.platform.debug(`Color Bulb: ${this.accessory.displayName} Brightness: ${value}`);
580
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} Brightness: ${value}`);
546
581
 
547
582
  this.Brightness = value;
548
583
  this.doColorBulbUpdate.next();
@@ -552,7 +587,8 @@ export class ColorBulb {
552
587
  * Handle requests to set the value of the "ColorTemperature" characteristic
553
588
  */
554
589
  ColorTemperatureSet(value: CharacteristicValue) {
555
- this.platform.debug(`Color Bulb: ${this.accessory.displayName} ColorTemperature: ${value}`);
590
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} ColorTemperature: ${value}`);
591
+
556
592
 
557
593
  // Convert mired to kelvin to nearest 100 (SwitchBot seems to need this)
558
594
  const kelvin = Math.round(1000000 / Number(value) / 100) * 100;
@@ -577,7 +613,7 @@ export class ColorBulb {
577
613
  * Handle requests to set the value of the "Hue" characteristic
578
614
  */
579
615
  HueSet(value: CharacteristicValue) {
580
- this.platform.debug(`Color Bulb: ${this.accessory.displayName} Hue: ${value}`);
616
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} Hue: ${value}`);
581
617
 
582
618
  this.service.updateCharacteristic(this.platform.Characteristic.ColorTemperature, 140);
583
619
 
@@ -589,12 +625,47 @@ export class ColorBulb {
589
625
  * Handle requests to set the value of the "Saturation" characteristic
590
626
  */
591
627
  SaturationSet(value: CharacteristicValue) {
592
- this.platform.debug(`Color Bulb: ${this.accessory.displayName} Saturation: ${value}`);
628
+ this.debugLog(`Color Bulb: ${this.accessory.displayName} Saturation: ${value}`);
593
629
 
594
630
  this.service.updateCharacteristic(this.platform.Characteristic.ColorTemperature, 140);
595
631
 
596
632
  this.Saturation = value;
597
633
  this.doColorBulbUpdate.next();
598
634
  }
635
+
636
+ /**
637
+ * Logging for Device
638
+ */
639
+ infoLog(...log: any[]) {
640
+ if (this.enablingDeviceLogging()) {
641
+ this.platform.log.info(String(...log));
642
+ }
643
+ }
644
+
645
+ warnLog(...log: any[]) {
646
+ if (this.enablingDeviceLogging()) {
647
+ this.platform.log.warn(String(...log));
648
+ }
649
+ }
650
+
651
+ errorLog(...log: any[]) {
652
+ if (this.enablingDeviceLogging()) {
653
+ this.platform.log.error(String(...log));
654
+ }
655
+ }
656
+
657
+ debugLog(...log: any[]) {
658
+ if (this.enablingDeviceLogging()) {
659
+ if (this.deviceLogging === 'debug') {
660
+ this.platform.log.info('[DEBUG]', String(...log));
661
+ } else {
662
+ this.platform.log.debug(String(...log));
663
+ }
664
+ }
665
+ }
666
+
667
+ enablingDeviceLogging(): boolean {
668
+ return this.deviceLogging === 'debug' || this.deviceLogging === 'standard';
669
+ }
599
670
  }
600
671