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