@switchbot/homebridge-switchbot 1.4.1-beta.0 → 1.4.1-beta.4

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 +12 -2
  4. package/dist/devices/bots.d.ts.map +1 -1
  5. package/dist/devices/bots.js +155 -89
  6. package/dist/devices/bots.js.map +1 -1
  7. package/dist/devices/colorbulb.d.ts +12 -2
  8. package/dist/devices/colorbulb.d.ts.map +1 -1
  9. package/dist/devices/colorbulb.js +153 -84
  10. package/dist/devices/colorbulb.js.map +1 -1
  11. package/dist/devices/contact.d.ts +12 -2
  12. package/dist/devices/contact.d.ts.map +1 -1
  13. package/dist/devices/contact.js +116 -52
  14. package/dist/devices/contact.js.map +1 -1
  15. package/dist/devices/curtains.d.ts +13 -4
  16. package/dist/devices/curtains.d.ts.map +1 -1
  17. package/dist/devices/curtains.js +191 -124
  18. package/dist/devices/curtains.js.map +1 -1
  19. package/dist/devices/humidifiers.d.ts +12 -2
  20. package/dist/devices/humidifiers.d.ts.map +1 -1
  21. package/dist/devices/humidifiers.js +171 -108
  22. package/dist/devices/humidifiers.js.map +1 -1
  23. package/dist/devices/meters.d.ts +12 -2
  24. package/dist/devices/meters.d.ts.map +1 -1
  25. package/dist/devices/meters.js +113 -49
  26. package/dist/devices/meters.js.map +1 -1
  27. package/dist/devices/motion.d.ts +12 -2
  28. package/dist/devices/motion.d.ts.map +1 -1
  29. package/dist/devices/motion.js +101 -37
  30. package/dist/devices/motion.js.map +1 -1
  31. package/dist/devices/plugs.d.ts +12 -2
  32. package/dist/devices/plugs.d.ts.map +1 -1
  33. package/dist/devices/plugs.js +95 -31
  34. package/dist/devices/plugs.js.map +1 -1
  35. package/dist/irdevices/airconditioners.d.ts +10 -2
  36. package/dist/irdevices/airconditioners.d.ts.map +1 -1
  37. package/dist/irdevices/airconditioners.js +77 -37
  38. package/dist/irdevices/airconditioners.js.map +1 -1
  39. package/dist/irdevices/airpurifiers.d.ts +10 -2
  40. package/dist/irdevices/airpurifiers.d.ts.map +1 -1
  41. package/dist/irdevices/airpurifiers.js +65 -25
  42. package/dist/irdevices/airpurifiers.js.map +1 -1
  43. package/dist/irdevices/cameras.d.ts +10 -2
  44. package/dist/irdevices/cameras.d.ts.map +1 -1
  45. package/dist/irdevices/cameras.js +61 -21
  46. package/dist/irdevices/cameras.js.map +1 -1
  47. package/dist/irdevices/fans.d.ts +10 -2
  48. package/dist/irdevices/fans.d.ts.map +1 -1
  49. package/dist/irdevices/fans.js +71 -30
  50. package/dist/irdevices/fans.js.map +1 -1
  51. package/dist/irdevices/lights.d.ts +10 -2
  52. package/dist/irdevices/lights.d.ts.map +1 -1
  53. package/dist/irdevices/lights.js +62 -22
  54. package/dist/irdevices/lights.js.map +1 -1
  55. package/dist/irdevices/others.d.ts +10 -2
  56. package/dist/irdevices/others.d.ts.map +1 -1
  57. package/dist/irdevices/others.js +68 -28
  58. package/dist/irdevices/others.js.map +1 -1
  59. package/dist/irdevices/tvs.d.ts +10 -2
  60. package/dist/irdevices/tvs.d.ts.map +1 -1
  61. package/dist/irdevices/tvs.js +78 -38
  62. package/dist/irdevices/tvs.js.map +1 -1
  63. package/dist/irdevices/vacuumcleaners.d.ts +10 -2
  64. package/dist/irdevices/vacuumcleaners.d.ts.map +1 -1
  65. package/dist/irdevices/vacuumcleaners.js +61 -21
  66. package/dist/irdevices/vacuumcleaners.js.map +1 -1
  67. package/dist/irdevices/waterheaters.d.ts +10 -2
  68. package/dist/irdevices/waterheaters.d.ts.map +1 -1
  69. package/dist/irdevices/waterheaters.js +61 -21
  70. package/dist/irdevices/waterheaters.js.map +1 -1
  71. package/dist/platform.d.ts +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 +10 -6
  80. package/src/devices/bots.ts +161 -89
  81. package/src/devices/colorbulb.ts +161 -86
  82. package/src/devices/contact.ts +122 -52
  83. package/src/devices/curtains.ts +199 -128
  84. package/src/devices/humidifiers.ts +177 -109
  85. package/src/devices/meters.ts +119 -49
  86. package/src/devices/motion.ts +106 -36
  87. package/src/devices/plugs.ts +99 -31
  88. package/src/irdevices/airconditioners.ts +81 -36
  89. package/src/irdevices/airpurifiers.ts +69 -24
  90. package/src/irdevices/cameras.ts +65 -20
  91. package/src/irdevices/fans.ts +75 -30
  92. package/src/irdevices/lights.ts +66 -21
  93. package/src/irdevices/others.ts +72 -27
  94. package/src/irdevices/tvs.ts +82 -37
  95. package/src/irdevices/vacuumcleaners.ts +65 -20
  96. package/src/irdevices/waterheaters.ts +65 -20
  97. package/src/platform.ts +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
@@ -40,8 +40,8 @@ export class Meter {
40
40
  humidity!: serviceData['humidity'];
41
41
 
42
42
  // Config
43
- private readonly deviceDebug = this.platform.config.options?.debug === 'device' || this.platform.debugMode;
44
- private readonly debugDebug = this.platform.config.options?.debug === 'debug' || this.platform.debugMode;
43
+ deviceLogging!: string;
44
+ deviceRefreshRate!: number;
45
45
 
46
46
  // Updates
47
47
  meterUpdateInProgress!: boolean;
@@ -52,11 +52,9 @@ export class Meter {
52
52
  private accessory: PlatformAccessory,
53
53
  public device: device & devicesConfig,
54
54
  ) {
55
- // Meter Config
56
- this.platform.device(`Meter: ${this.accessory.displayName} Config: (ble: ${device.ble}, hide_temperature: ${device.meter?.hide_temperature},`
57
- + ` hide_humidity: ${device.meter?.hide_humidity})`);
58
-
59
55
  // default placeholders
56
+ this.refreshRate();
57
+ this.logs();
60
58
  if (this.BatteryLevel === undefined) {
61
59
  this.BatteryLevel = 100;
62
60
  } else {
@@ -79,6 +77,10 @@ export class Meter {
79
77
  this.CurrentTemperature = this.accessory.context.CurrentTemperature;
80
78
  }
81
79
 
80
+ // Meter Config
81
+ this.debugLog(`Meter: ${this.accessory.displayName} Config: (ble: ${device.ble}, hide_temperature: ${device.meter?.hide_temperature},`
82
+ + ` hide_humidity: ${device.meter?.hide_humidity})`);
83
+
82
84
  // this is subject we use to track when we need to POST changes to the SwitchBot API
83
85
  this.doMeterUpdate = new Subject();
84
86
  this.meterUpdateInProgress = false;
@@ -115,11 +117,11 @@ export class Meter {
115
117
 
116
118
  // Temperature Sensor Service
117
119
  if (device.meter?.hide_temperature) {
118
- this.platform.device(`Meter: ${accessory.displayName} Removing Temperature Sensor Service`);
120
+ this.debugLog(`Meter: ${accessory.displayName} Removing Temperature Sensor Service`);
119
121
  this.temperatureservice = this.accessory.getService(this.platform.Service.TemperatureSensor);
120
122
  accessory.removeService(this.temperatureservice!);
121
123
  } else if (!this.temperatureservice) {
122
- this.platform.device(`Meter: ${accessory.displayName} Add Temperature Sensor Service`);
124
+ this.debugLog(`Meter: ${accessory.displayName} Add Temperature Sensor Service`);
123
125
  (this.temperatureservice =
124
126
  this.accessory.getService(this.platform.Service.TemperatureSensor) ||
125
127
  this.accessory.addService(this.platform.Service.TemperatureSensor)), `${accessory.displayName} Temperature Sensor`;
@@ -139,16 +141,16 @@ export class Meter {
139
141
  return this.CurrentTemperature;
140
142
  });
141
143
  } else {
142
- this.platform.device(`Meter: ${accessory.displayName} Temperature Sensor Service Not Added`);
144
+ this.debugLog(`Meter: ${accessory.displayName} Temperature Sensor Service Not Added`);
143
145
  }
144
146
 
145
147
  // Humidity Sensor Service
146
148
  if (device.meter?.hide_humidity) {
147
- this.platform.device(`Meter: ${accessory.displayName} Removing Humidity Sensor Service`);
149
+ this.debugLog(`Meter: ${accessory.displayName} Removing Humidity Sensor Service`);
148
150
  this.humidityservice = this.accessory.getService(this.platform.Service.HumiditySensor);
149
151
  accessory.removeService(this.humidityservice!);
150
152
  } else if (!this.humidityservice) {
151
- this.platform.device(`Meter: ${accessory.displayName} Add Humidity Sensor Service`);
153
+ this.debugLog(`Meter: ${accessory.displayName} Add Humidity Sensor Service`);
152
154
  (this.humidityservice =
153
155
  this.accessory.getService(this.platform.Service.HumiditySensor) ||
154
156
  this.accessory.addService(this.platform.Service.HumiditySensor)), `${accessory.displayName} Humidity Sensor`;
@@ -164,20 +166,53 @@ export class Meter {
164
166
  return this.CurrentRelativeHumidity;
165
167
  });
166
168
  } else {
167
- this.platform.device(`Meter: ${accessory.displayName} Humidity Sensor Service Not Added`);
169
+ this.debugLog(`Meter: ${accessory.displayName} Humidity Sensor Service Not Added`);
168
170
  }
169
171
 
170
172
  // Retrieve initial values and updateHomekit
171
173
  this.updateHomeKitCharacteristics();
172
174
 
173
175
  // Start an update interval
174
- interval(this.platform.config.options!.refreshRate! * 1000)
176
+ interval(this.deviceRefreshRate * 1000)
175
177
  .pipe(skipWhile(() => this.meterUpdateInProgress))
176
178
  .subscribe(() => {
177
179
  this.refreshStatus();
178
180
  });
179
181
  }
180
182
 
183
+ refreshRate() {
184
+ if (this.device.refreshRate) {
185
+ this.deviceRefreshRate = this.accessory.context.refreshRate = this.device.refreshRate;
186
+ if (this.platform.debugMode) {
187
+ this.warnLog(`Using Device Config refreshRate: ${this.deviceRefreshRate}`);
188
+ }
189
+ } else if (this.platform.config.options!.refreshRate) {
190
+ this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options!.refreshRate;
191
+ if (this.platform.debugMode) {
192
+ this.warnLog(`Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
193
+ }
194
+ }
195
+ }
196
+
197
+ logs() {
198
+ if (this.device.logging) {
199
+ this.deviceLogging = this.accessory.context.logging = this.device.logging;
200
+ if (this.platform.debugMode) {
201
+ this.warnLog(`Using Device Config Logging: ${this.deviceLogging}`);
202
+ }
203
+ } else if (this.platform.config.options?.logging) {
204
+ this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
205
+ if (this.platform.debugMode) {
206
+ this.warnLog(`Using Platform Config Logging: ${this.deviceLogging}`);
207
+ }
208
+ } else {
209
+ this.deviceLogging = this.accessory.context.logging = 'standard';
210
+ if (this.platform.debugMode) {
211
+ this.warnLog('Using Device Standard Logging');
212
+ }
213
+ }
214
+ }
215
+
181
216
  /**
182
217
  * Parse the device status from the SwitchBot api
183
218
  */
@@ -190,7 +225,7 @@ export class Meter {
190
225
  }
191
226
 
192
227
  private async BLEparseStatus() {
193
- this.platform.debug(`Meter: ${this.accessory.displayName} BLE parseStatus`);
228
+ this.debugLog(`Meter: ${this.accessory.displayName} BLE parseStatus`);
194
229
 
195
230
  // Battery
196
231
  this.BatteryLevel = Number(this.battery);
@@ -199,25 +234,25 @@ export class Meter {
199
234
  } else {
200
235
  this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
201
236
  }
202
- this.platform.debug(`${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
237
+ this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
203
238
 
204
239
  if (!this.device.meter?.hide_humidity) {
205
240
  // Humidity
206
241
  this.CurrentRelativeHumidity = Number(this.humidity);
207
- this.platform.debug(`Meter: ${this.accessory.displayName} Humidity: ${this.CurrentRelativeHumidity}%`);
242
+ this.debugLog(`Meter: ${this.accessory.displayName} Humidity: ${this.CurrentRelativeHumidity}%`);
208
243
  }
209
244
 
210
245
  // Current Temperature
211
246
  if (!this.device.meter?.hide_temperature) {
212
247
  this.temperature < 0 ? 0 : this.temperature > 100 ? 100 : this.temperature;
213
248
  this.CurrentTemperature = this.temperature;
214
- this.platform.debug(`Meter: ${this.accessory.displayName} Temperature: ${this.CurrentTemperature}°c`);
249
+ this.debugLog(`Meter: ${this.accessory.displayName} Temperature: ${this.CurrentTemperature}°c`);
215
250
  }
216
251
  }
217
252
 
218
253
  private async openAPIparseStatus() {
219
254
  if (this.platform.config.credentials?.openToken) {
220
- this.platform.debug(`Meter: ${this.accessory.displayName} OpenAPI parseStatus`);
255
+ this.debugLog(`Meter: ${this.accessory.displayName} OpenAPI parseStatus`);
221
256
  if (this.deviceStatus.body) {
222
257
  this.BatteryLevel = 100;
223
258
  } else {
@@ -231,13 +266,13 @@ export class Meter {
231
266
  // Current Relative Humidity
232
267
  if (!this.device.meter?.hide_humidity) {
233
268
  this.CurrentRelativeHumidity = this.deviceStatus.body.humidity!;
234
- this.platform.debug(`Meter: ${this.accessory.displayName} Humidity: ${this.CurrentRelativeHumidity}%`);
269
+ this.debugLog(`Meter: ${this.accessory.displayName} Humidity: ${this.CurrentRelativeHumidity}%`);
235
270
  }
236
271
 
237
272
  // Current Temperature
238
273
  if (!this.device.meter?.hide_temperature) {
239
274
  this.CurrentTemperature = Number(this.deviceStatus.body.temperature);
240
- this.platform.debug(`Meter: ${this.accessory.displayName} Temperature: ${this.CurrentTemperature}°c`);
275
+ this.debugLog(`Meter: ${this.accessory.displayName} Temperature: ${this.CurrentTemperature}°c`);
241
276
  }
242
277
  }
243
278
  }
@@ -256,12 +291,12 @@ export class Meter {
256
291
  private connectBLE() {
257
292
  const switchbot = new Switchbot();
258
293
  this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
259
- this.platform.device(`Meter: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
294
+ this.debugLog(`Meter: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
260
295
  return switchbot;
261
296
  }
262
297
 
263
298
  private BLErefreshStatus() {
264
- this.platform.debug(`Meter: ${this.accessory.displayName} BLE RefreshStatus`);
299
+ this.debugLog(`Meter: ${this.accessory.displayName} BLE RefreshStatus`);
265
300
  // eslint-disable-next-line @typescript-eslint/no-var-requires
266
301
  const switchbot = this.connectBLE();
267
302
  // Start to monitor advertisement packets
@@ -275,17 +310,17 @@ export class Meter {
275
310
  this.temperature = ad.serviceData.temperature!.c;
276
311
  this.humidity = ad.serviceData.humidity;
277
312
  this.battery = ad.serviceData.battery;
278
- this.platform.device(`Meter: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
279
- this.platform.device(`Meter: ${this.accessory.displayName} model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName}, `
313
+ this.debugLog(`Meter: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
314
+ this.debugLog(`Meter: ${this.accessory.displayName} model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName}, `
280
315
  + `temperature: ${JSON.stringify(ad.serviceData.temperature?.c)}, humidity: ${ad.serviceData.humidity}, `
281
316
  + `battery: ${ad.serviceData.battery}`);
282
317
 
283
318
  if (this.serviceData) {
284
319
  this.connected = true;
285
- this.platform.device(`Meter: ${this.accessory.displayName} connected: ${this.connected}`);
320
+ this.debugLog(`Meter: ${this.accessory.displayName} connected: ${this.connected}`);
286
321
  } else {
287
322
  this.connected = false;
288
- this.platform.device(`Meter: ${this.accessory.displayName} connected: ${this.connected}`);
323
+ this.debugLog(`Meter: ${this.accessory.displayName} connected: ${this.connected}`);
289
324
  }
290
325
  };
291
326
  // Wait 10 seconds
@@ -303,24 +338,24 @@ export class Meter {
303
338
  this.parseStatus();
304
339
  this.updateHomeKitCharacteristics();
305
340
  } else {
306
- this.platform.log.error(`Meter: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
341
+ this.errorLog(`Meter: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
307
342
  if (this.platform.config.credentials?.openToken) {
308
- this.platform.log.warn(`Meter: ${this.accessory.displayName} Using OpenAPI Connection`);
343
+ this.warnLog(`Meter: ${this.accessory.displayName} Using OpenAPI Connection`);
309
344
  this.openAPIRefreshStatus();
310
345
  }
311
346
  }
312
347
  }).catch(async (e: any) => {
313
- this.platform.log.error(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
314
- if (this.deviceDebug) {
315
- this.platform.log.error(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
348
+ this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
349
+ if (this.deviceLogging === 'debug') {
350
+ this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
316
351
  + ` Error Message: ${JSON.stringify(e.message)}`);
317
352
  }
318
- if (this.debugDebug) {
319
- this.platform.log.error(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
353
+ if (this.platform.debugMode) {
354
+ this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
320
355
  + ` Error: ${JSON.stringify(e)}`);
321
356
  }
322
357
  if (this.platform.config.credentials?.openToken) {
323
- this.platform.log.warn(`Meter: ${this.accessory.displayName} Using OpenAPI Connection`);
358
+ this.warnLog(`Meter: ${this.accessory.displayName} Using OpenAPI Connection`);
324
359
  this.openAPIRefreshStatus();
325
360
  }
326
361
  this.apiError();
@@ -329,10 +364,10 @@ export class Meter {
329
364
 
330
365
  private async openAPIRefreshStatus() {
331
366
  if (this.platform.config.credentials?.openToken) {
332
- this.platform.debug(`Meter: ${this.accessory.displayName} OpenAPI RefreshStatus`);
367
+ this.debugLog(`Meter: ${this.accessory.displayName} OpenAPI RefreshStatus`);
333
368
  try {
334
369
  this.deviceStatus = (await this.platform.axios.get(`${DeviceURL}/${this.device.deviceId}/status`)).data;
335
- this.platform.debug(`Meter: ${this.accessory.displayName} openAPIRefreshStatus: ${JSON.stringify(this.deviceStatus)}`);
370
+ this.debugLog(`Meter: ${this.accessory.displayName} openAPIRefreshStatus: ${JSON.stringify(this.deviceStatus)}`);
336
371
  this.CurrentTemperatureCached = this.deviceStatus.body.humidity!;
337
372
  this.accessory.context.CurrentTemperature = this.CurrentTemperatureCached;
338
373
  this.CurrentRelativeHumidityCached = this.deviceStatus.body.humidity!;
@@ -342,13 +377,13 @@ export class Meter {
342
377
  this.parseStatus();
343
378
  this.updateHomeKitCharacteristics();
344
379
  } catch (e: any) {
345
- this.platform.log.error(`Meter: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
346
- if (this.deviceDebug) {
347
- this.platform.log.error(`Meter: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
380
+ this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
381
+ if (this.deviceLogging === 'debug') {
382
+ this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
348
383
  + ` Error Message: ${JSON.stringify(e.message)}`);
349
384
  }
350
- if (this.debugDebug) {
351
- this.platform.log.error(`Meter: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
385
+ if (this.platform.debugMode) {
386
+ this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
352
387
  + ` Error: ${JSON.stringify(e)}`);
353
388
  }
354
389
  this.apiError();
@@ -361,31 +396,31 @@ export class Meter {
361
396
  */
362
397
  updateHomeKitCharacteristics() {
363
398
  if (this.StatusLowBattery === undefined) {
364
- this.platform.debug(`Meter: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
399
+ this.debugLog(`Meter: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
365
400
  } else {
366
401
  this.service.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
367
- this.platform.device(`Meter: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
402
+ this.debugLog(`Meter: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
368
403
  }
369
404
  if (this.BatteryLevel === undefined) {
370
- this.platform.debug(`Meter: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
405
+ this.debugLog(`Meter: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
371
406
  } else {
372
407
  this.service.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
373
- this.platform.device(`Meter: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
408
+ this.debugLog(`Meter: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
374
409
  }
375
410
  if (!this.device.meter?.hide_humidity) {
376
411
  if (this.CurrentRelativeHumidity === undefined) {
377
- this.platform.debug(`Meter: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
412
+ this.debugLog(`Meter: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
378
413
  } else {
379
414
  this.humidityservice?.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.CurrentRelativeHumidity);
380
- this.platform.device(`Meter: ${this.accessory.displayName} updateCharacteristic CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
415
+ this.debugLog(`Meter: ${this.accessory.displayName} updateCharacteristic CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
381
416
  }
382
417
  }
383
418
  if (!this.device.meter?.hide_temperature) {
384
419
  if (this.CurrentTemperature === undefined) {
385
- this.platform.debug(`Meter: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
420
+ this.debugLog(`Meter: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
386
421
  } else {
387
422
  this.temperatureservice?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.CurrentTemperature);
388
- this.platform.device(`Meter: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`);
423
+ this.debugLog(`Meter: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`);
389
424
  }
390
425
  }
391
426
  }
@@ -393,4 +428,39 @@ export class Meter {
393
428
  public apiError() {
394
429
  throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
395
430
  }
431
+
432
+ /**
433
+ * Logging for Device
434
+ */
435
+ infoLog(...log: any[]) {
436
+ if (this.enablingDeviceLogging()) {
437
+ this.platform.log.info(String(...log));
438
+ }
439
+ }
440
+
441
+ warnLog(...log: any[]) {
442
+ if (this.enablingDeviceLogging()) {
443
+ this.platform.log.warn(String(...log));
444
+ }
445
+ }
446
+
447
+ errorLog(...log: any[]) {
448
+ if (this.enablingDeviceLogging()) {
449
+ this.platform.log.error(String(...log));
450
+ }
451
+ }
452
+
453
+ debugLog(...log: any[]) {
454
+ if (this.enablingDeviceLogging()) {
455
+ if (this.deviceLogging === 'debug') {
456
+ this.platform.log.info('[DEBUG]', String(...log));
457
+ } else {
458
+ this.platform.log.debug(String(...log));
459
+ }
460
+ }
461
+ }
462
+
463
+ enablingDeviceLogging(): boolean {
464
+ return this.deviceLogging === 'debug' || this.deviceLogging === 'standard';
465
+ }
396
466
  }
@@ -34,8 +34,8 @@ export class Motion {
34
34
  lightLevel!: serviceData['lightLevel'];
35
35
 
36
36
  // Config
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
  // Updates
41
41
  motionUbpdateInProgress!: boolean;
@@ -47,6 +47,8 @@ export class Motion {
47
47
  public device: device & devicesConfig,
48
48
  ) {
49
49
  // default placeholders
50
+ this.refreshRate();
51
+ this.logs();
50
52
  this.MotionDetected = false;
51
53
 
52
54
  // this is subject we use to track when we need to POST changes to the SwitchBot API
@@ -102,13 +104,46 @@ export class Motion {
102
104
  this.updateHomeKitCharacteristics();
103
105
 
104
106
  // Start an update interval
105
- interval(this.platform.config.options!.refreshRate! * 1000)
107
+ interval(this.deviceRefreshRate * 1000)
106
108
  .pipe(skipWhile(() => this.motionUbpdateInProgress))
107
109
  .subscribe(() => {
108
110
  this.refreshStatus();
109
111
  });
110
112
  }
111
113
 
114
+ refreshRate() {
115
+ if (this.device.refreshRate) {
116
+ this.deviceRefreshRate = this.accessory.context.refreshRate = this.device.refreshRate;
117
+ if (this.platform.debugMode) {
118
+ this.warnLog(`Using Device Config refreshRate: ${this.deviceRefreshRate}`);
119
+ }
120
+ } else if (this.platform.config.options!.refreshRate) {
121
+ this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options!.refreshRate;
122
+ if (this.platform.debugMode) {
123
+ this.warnLog(`Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
124
+ }
125
+ }
126
+ }
127
+
128
+ logs() {
129
+ if (this.device.logging) {
130
+ this.deviceLogging = this.accessory.context.logging = this.device.logging;
131
+ if (this.platform.debugMode) {
132
+ this.warnLog(`Using Device Config Logging: ${this.deviceLogging}`);
133
+ }
134
+ } else if (this.platform.config.options?.logging) {
135
+ this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
136
+ if (this.platform.debugMode) {
137
+ this.warnLog(`Using Platform Config Logging: ${this.deviceLogging}`);
138
+ }
139
+ } else {
140
+ this.deviceLogging = this.accessory.context.logging = 'standard';
141
+ if (this.platform.debugMode) {
142
+ this.warnLog('Using Device Standard Logging');
143
+ }
144
+ }
145
+ }
146
+
112
147
  /**
113
148
  * Parse the device status from the SwitchBot api
114
149
  */
@@ -121,9 +156,9 @@ export class Motion {
121
156
  }
122
157
 
123
158
  private async BLEparseStatus() {
124
- this.platform.device(`Motion Sensor: ${this.accessory.displayName} BLE parseStatus`);
159
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} BLE parseStatus`);
125
160
  this.MotionDetected = Boolean(this.movement);
126
- this.platform.debug(`Motion Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
161
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
127
162
  // Light Level
128
163
  switch (this.lightLevel) {
129
164
  case 'dark':
@@ -144,9 +179,9 @@ export class Motion {
144
179
 
145
180
  private async openAPIparseStatus() {
146
181
  if (this.platform.config.credentials?.openToken) {
147
- this.platform.device(`Motion Sensor: ${this.accessory.displayName} OpenAPI parseStatus`);
182
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} OpenAPI parseStatus`);
148
183
  this.MotionDetected = Boolean(this.deviceStatus.body.moveDetected);
149
- this.platform.debug(`Motion Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
184
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
150
185
  }
151
186
  }
152
187
 
@@ -164,12 +199,12 @@ export class Motion {
164
199
  private connectBLE() {
165
200
  const switchbot = new Switchbot();
166
201
  this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
167
- this.platform.device(`Motion Sensor: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
202
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
168
203
  return switchbot;
169
204
  }
170
205
 
171
206
  private async BLERefreshStatus() {
172
- this.platform.debug(`Motion Sensor: ${this.accessory.displayName} BLE RefreshStatus`);
207
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} BLE RefreshStatus`);
173
208
  // eslint-disable-next-line @typescript-eslint/no-var-requires
174
209
  const switchbot = this.connectBLE();
175
210
  // Start to monitor advertisement packets
@@ -183,16 +218,16 @@ export class Motion {
183
218
  this.movement = ad.serviceData.movement;
184
219
  this.battery = ad.serviceData.battery;
185
220
  this.lightLevel = ad.serviceData.lightLevel;
186
- this.platform.device(`Motion Sensor: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
187
- this.platform.device(`Motion Sensor: ${this.accessory.displayName} movement: ${ad.serviceData.movement}, lightLevel: `
221
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
222
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} movement: ${ad.serviceData.movement}, lightLevel: `
188
223
  + `${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}`);
189
224
 
190
225
  if (this.serviceData) {
191
226
  this.connected = true;
192
- this.platform.device(`Motion Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
227
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
193
228
  } else {
194
229
  this.connected = false;
195
- this.platform.device(`Motion Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
230
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
196
231
  }
197
232
  };
198
233
  // Wait 10 seconds
@@ -204,24 +239,24 @@ export class Motion {
204
239
  this.parseStatus();
205
240
  this.updateHomeKitCharacteristics();
206
241
  } else {
207
- this.platform.log.error(`Motion Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
242
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
208
243
  if (this.platform.config.credentials?.openToken) {
209
- this.platform.log.warn(`Motion Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
244
+ this.warnLog(`Motion Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
210
245
  await this.openAPIRefreshStatus();
211
246
  }
212
247
  }
213
248
  }).catch(async (e: any) => {
214
- this.platform.log.error(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
215
- if (this.deviceDebug) {
216
- this.platform.log.error(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
249
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
250
+ if (this.deviceLogging === 'debug') {
251
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
217
252
  + ` Error Message: ${JSON.stringify(e.message)}`);
218
253
  }
219
- if (this.debugDebug) {
220
- this.platform.log.error(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
254
+ if (this.platform.debugMode) {
255
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
221
256
  + ` Error: ${JSON.stringify(e)}`);
222
257
  }
223
258
  if (this.platform.config.credentials?.openToken) {
224
- this.platform.log.warn(`Motion Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
259
+ this.warnLog(`Motion Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
225
260
  await this.openAPIRefreshStatus();
226
261
  }
227
262
  this.apiError();
@@ -230,20 +265,20 @@ export class Motion {
230
265
 
231
266
  private async openAPIRefreshStatus() {
232
267
  if (this.platform.config.credentials?.openToken) {
233
- this.platform.debug(`Motion Sensor: ${this.accessory.displayName} OpenAPI RefreshStatus`);
268
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} OpenAPI RefreshStatus`);
234
269
  try {
235
270
  this.deviceStatus = (await this.platform.axios.get(`${DeviceURL}/${this.device.deviceId}/status`)).data;
236
- this.platform.debug(`Motion Sensor: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(this.deviceStatus)}`);
271
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(this.deviceStatus)}`);
237
272
  this.parseStatus();
238
273
  this.updateHomeKitCharacteristics();
239
274
  } catch (e: any) {
240
- this.platform.log.error(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
241
- if (this.deviceDebug) {
242
- this.platform.log.error(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
275
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
276
+ if (this.deviceLogging === 'debug') {
277
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
243
278
  + ` Error Message: ${JSON.stringify(e.message)}`);
244
279
  }
245
- if (this.debugDebug) {
246
- this.platform.log.error(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
280
+ if (this.platform.debugMode) {
281
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
247
282
  + ` Error: ${JSON.stringify(e)}`);
248
283
  }
249
284
  this.apiError();
@@ -256,29 +291,29 @@ export class Motion {
256
291
  */
257
292
  updateHomeKitCharacteristics() {
258
293
  if (this.MotionDetected === undefined) {
259
- this.platform.debug(`Motion Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
294
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
260
295
  } else {
261
296
  this.service.updateCharacteristic(this.platform.Characteristic.MotionDetected, this.MotionDetected);
262
- this.platform.device(`Motion Sensor: ${this.accessory.displayName} updateCharacteristic MotionDetected: ${this.MotionDetected}`);
297
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} updateCharacteristic MotionDetected: ${this.MotionDetected}`);
263
298
  }
264
299
  if (this.device.ble) {
265
300
  if (this.BatteryLevel === undefined) {
266
- this.platform.debug(`Motion Sensor: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
301
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
267
302
  } else {
268
303
  this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
269
- this.platform.device(`Motion Sensor: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
304
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
270
305
  }
271
306
  if (this.StatusLowBattery === undefined) {
272
- this.platform.debug(`Motion Sensor: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
307
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
273
308
  } else {
274
309
  this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
275
- this.platform.device(`Motion Sensor: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
310
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
276
311
  }
277
312
  if (this.CurrentAmbientLightLevel === undefined) {
278
- this.platform.debug(`Motion Sensor: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
313
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
279
314
  } else {
280
315
  this.lightSensorService?.updateCharacteristic(this.platform.Characteristic.CurrentAmbientLightLevel, this.CurrentAmbientLightLevel);
281
- this.platform.device(`Motion Sensor: ${this.accessory.displayName}`
316
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName}`
282
317
  + ` updateCharacteristic CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
283
318
  }
284
319
  }
@@ -287,4 +322,39 @@ export class Motion {
287
322
  public apiError() {
288
323
  throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
289
324
  }
325
+
326
+ /**
327
+ * Logging for Device
328
+ */
329
+ infoLog(...log: any[]) {
330
+ if (this.enablingDeviceLogging()) {
331
+ this.platform.log.info(String(...log));
332
+ }
333
+ }
334
+
335
+ warnLog(...log: any[]) {
336
+ if (this.enablingDeviceLogging()) {
337
+ this.platform.log.warn(String(...log));
338
+ }
339
+ }
340
+
341
+ errorLog(...log: any[]) {
342
+ if (this.enablingDeviceLogging()) {
343
+ this.platform.log.error(String(...log));
344
+ }
345
+ }
346
+
347
+ debugLog(...log: any[]) {
348
+ if (this.enablingDeviceLogging()) {
349
+ if (this.deviceLogging === 'debug') {
350
+ this.platform.log.info('[DEBUG]', String(...log));
351
+ } else {
352
+ this.platform.log.debug(String(...log));
353
+ }
354
+ }
355
+ }
356
+
357
+ enablingDeviceLogging(): boolean {
358
+ return this.deviceLogging === 'debug' || this.deviceLogging === 'standard';
359
+ }
290
360
  }