@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 Switchbot from 'node-switchbot';
2
2
  import { interval, Subject } from 'rxjs';
3
3
  import { skipWhile } from 'rxjs/operators';
4
4
  import { SwitchBotPlatform } from '../platform';
5
- import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
5
+ import { Service, PlatformAccessory, CharacteristicValue, HAPStatus } from 'homebridge';
6
6
  import { DeviceURL, device, devicesConfig, serviceData, switchbot, deviceStatusResponse } from '../settings';
7
7
 
8
8
  /**
@@ -37,8 +37,8 @@ export class Contact {
37
37
  lightLevel!: serviceData['lightLevel'];
38
38
 
39
39
  // Config
40
- private readonly deviceDebug = this.platform.config.options?.debug === 'device' || this.platform.debugMode;
41
- private readonly debugDebug = this.platform.config.options?.debug === 'debug' || this.platform.debugMode;
40
+ deviceLogging!: string;
41
+ deviceRefreshRate!: number;
42
42
 
43
43
  // Updates
44
44
  contactUbpdateInProgress!: boolean;
@@ -49,14 +49,16 @@ export class Contact {
49
49
  private accessory: PlatformAccessory,
50
50
  public device: device & devicesConfig,
51
51
  ) {
52
+ // default placeholders
53
+ this.refreshRate();
54
+ this.logs();
55
+ this.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED;
56
+
52
57
  // Contact Config
53
- this.platform.device(`Contact Sensor: ${this.accessory.displayName} Config: (ble: ${device.ble}, offline: ${device.offline},`
58
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} Config: (ble: ${device.ble}, offline: ${device.offline},`
54
59
  + ` hide_lightsensor: ${device.contact?.hide_lightsensor}, hide_motionsensor: ${device.contact?.hide_motionsensor})`);
55
60
 
56
61
 
57
- // default placeholders
58
- this.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED;
59
-
60
62
  // this is subject we use to track when we need to POST changes to the SwitchBot API
61
63
  this.doContactUpdate = new Subject();
62
64
  this.contactUbpdateInProgress = false;
@@ -90,11 +92,11 @@ export class Contact {
90
92
 
91
93
  // Motion Sensor Service
92
94
  if (device.contact?.hide_motionsensor) {
93
- this.platform.device(`Contact Sensor: ${accessory.displayName} Removing Motion Sensor Service`);
95
+ this.debugLog(`Contact Sensor: ${accessory.displayName} Removing Motion Sensor Service`);
94
96
  this.motionService = this.accessory.getService(this.platform.Service.MotionSensor);
95
97
  accessory.removeService(this.motionService!);
96
98
  } else if (!this.motionService) {
97
- this.platform.device(`Contact Sensor: ${accessory.displayName} Add Motion Sensor Service`);
99
+ this.debugLog(`Contact Sensor: ${accessory.displayName} Add Motion Sensor Service`);
98
100
  (this.motionService =
99
101
  this.accessory.getService(this.platform.Service.MotionSensor) ||
100
102
  this.accessory.addService(this.platform.Service.MotionSensor)), `${accessory.displayName} Motion Sensor`;
@@ -102,16 +104,16 @@ export class Contact {
102
104
  this.motionService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Motion Sensor`);
103
105
 
104
106
  } else {
105
- this.platform.device(`Contact Sensor: ${accessory.displayName} Motion Sensor Service Not Added`);
107
+ this.debugLog(`Contact Sensor: ${accessory.displayName} Motion Sensor Service Not Added`);
106
108
  }
107
109
 
108
110
  // Light Sensor Service
109
111
  if (device.contact?.hide_lightsensor) {
110
- this.platform.device(`Contact Sensor: ${accessory.displayName} Removing Light Sensor Service`);
112
+ this.debugLog(`Contact Sensor: ${accessory.displayName} Removing Light Sensor Service`);
111
113
  this.lightSensorService = this.accessory.getService(this.platform.Service.LightSensor);
112
114
  accessory.removeService(this.lightSensorService!);
113
115
  } else if (!this.lightSensorService) {
114
- this.platform.device(`Contact Sensor: ${accessory.displayName} Add Light Sensor Service`);
116
+ this.debugLog(`Contact Sensor: ${accessory.displayName} Add Light Sensor Service`);
115
117
  (this.lightSensorService =
116
118
  this.accessory.getService(this.platform.Service.LightSensor) ||
117
119
  this.accessory.addService(this.platform.Service.LightSensor)), `${accessory.displayName} Light Sensor`;
@@ -119,7 +121,7 @@ export class Contact {
119
121
  this.lightSensorService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Light Sensor`);
120
122
 
121
123
  } else {
122
- this.platform.device(`Contact Sensor: ${accessory.displayName} Light Sensor Service Not Added`);
124
+ this.debugLog(`Contact Sensor: ${accessory.displayName} Light Sensor Service Not Added`);
123
125
  }
124
126
 
125
127
  if (device.ble) {
@@ -134,13 +136,46 @@ export class Contact {
134
136
  this.updateHomeKitCharacteristics();
135
137
 
136
138
  // Start an update interval
137
- interval(this.platform.config.options!.refreshRate! * 1000)
139
+ interval(this.deviceRefreshRate * 1000)
138
140
  .pipe(skipWhile(() => this.contactUbpdateInProgress))
139
141
  .subscribe(() => {
140
142
  this.refreshStatus();
141
143
  });
142
144
  }
143
145
 
146
+ refreshRate() {
147
+ if (this.device.refreshRate) {
148
+ this.deviceRefreshRate = this.accessory.context.refreshRate = this.device.refreshRate;
149
+ if (this.platform.debugMode) {
150
+ this.warnLog(`Using Device Config refreshRate: ${this.deviceRefreshRate}`);
151
+ }
152
+ } else if (this.platform.config.options!.refreshRate) {
153
+ this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options!.refreshRate;
154
+ if (this.platform.debugMode) {
155
+ this.warnLog(`Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
156
+ }
157
+ }
158
+ }
159
+
160
+ logs() {
161
+ if (this.device.logging) {
162
+ this.deviceLogging = this.accessory.context.logging = this.device.logging;
163
+ if (this.platform.debugMode) {
164
+ this.warnLog(`Using Device Config Logging: ${this.deviceLogging}`);
165
+ }
166
+ } else if (this.platform.config.options?.logging) {
167
+ this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
168
+ if (this.platform.debugMode) {
169
+ this.warnLog(`Using Platform Config Logging: ${this.deviceLogging}`);
170
+ }
171
+ } else {
172
+ this.deviceLogging = this.accessory.context.logging = 'standard';
173
+ if (this.platform.debugMode) {
174
+ this.warnLog('Using Device Standard Logging');
175
+ }
176
+ }
177
+ }
178
+
144
179
  /**
145
180
  * Parse the device status from the SwitchBot api
146
181
  */
@@ -153,7 +188,7 @@ export class Contact {
153
188
  }
154
189
 
155
190
  private async BLEparseStatus() {
156
- this.platform.debug(`Contact Sensor: ${this.accessory.displayName} BLE parseStatus`);
191
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} BLE parseStatus`);
157
192
  if (this.device.contact?.hide_motionsensor) {
158
193
  // Movement
159
194
  this.MotionDetected = Boolean(this.movement);
@@ -169,7 +204,7 @@ export class Contact {
169
204
  this.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED;
170
205
  break;
171
206
  default:
172
- this.platform.log.error(`Contact Sensor: ${this.accessory.displayName} timeout no closed, doorstate: ${this.doorState}`);
207
+ this.errorLog(`Contact Sensor: ${this.accessory.displayName} timeout no closed, doorstate: ${this.doorState}`);
173
208
  }
174
209
  if (this.device.contact?.hide_lightsensor) {// Light Level
175
210
  switch (this.lightLevel) {
@@ -189,26 +224,26 @@ export class Contact {
189
224
  this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
190
225
  }
191
226
 
192
- this.platform.debug(`Contact Sensor: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensorState}, MotionDetected: `
227
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensorState}, MotionDetected: `
193
228
  + `${this.MotionDetected}, CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}, BatteryLevel: ${this.BatteryLevel}`);
194
229
  }
195
230
 
196
231
  private async openAPIparseStatus() {
197
232
  if (this.platform.config.credentials?.openToken) {
198
- this.platform.debug(`Contact Sensor: ${this.accessory.displayName} OpenAPI parseStatus`);
233
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} OpenAPI parseStatus`);
199
234
  if (this.deviceStatus.body.openState === 'open') {
200
235
  this.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED;
201
- this.platform.device(`Contact Sensor: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensorState}`);
236
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensorState}`);
202
237
  } else if (this.deviceStatus.body.openState === 'close') {
203
238
  this.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED;
204
- this.platform.device(`Contact Sensor: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensorState}`);
239
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensorState}`);
205
240
  } else {
206
- this.platform.device(`Contact Sensor: ${this.accessory.displayName} openState: ${this.deviceStatus.body.openState}`);
241
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} openState: ${this.deviceStatus.body.openState}`);
207
242
  }
208
243
  if (this.device.contact?.hide_motionsensor) {
209
244
  this.MotionDetected = Boolean(this.deviceStatus.body.moveDetected);
210
245
  }
211
- this.platform.debug(`Contact Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
246
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
212
247
  }
213
248
  }
214
249
 
@@ -226,12 +261,12 @@ export class Contact {
226
261
  private connectBLE() {
227
262
  const switchbot = new Switchbot();
228
263
  this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
229
- this.platform.device(`Contact Sensor: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
264
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
230
265
  return switchbot;
231
266
  }
232
267
 
233
268
  private async BLERefreshStatus() {
234
- this.platform.debug(`Contact Sensor: ${this.accessory.displayName} BLE refreshStatus`);
269
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} BLE refreshStatus`);
235
270
  // eslint-disable-next-line @typescript-eslint/no-var-requires
236
271
  const switchbot = this.connectBLE();
237
272
  // Start to monitor advertisement packets
@@ -246,16 +281,16 @@ export class Contact {
246
281
  this.doorState = ad.serviceData.doorState;
247
282
  this.lightLevel = ad.serviceData.lightLevel;
248
283
  this.battery = ad.serviceData.battery;
249
- this.platform.device(`Contact Sensor: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
250
- this.platform.device(`Contact Sensor: ${this.accessory.displayName} movement: ${ad.serviceData.movement}, doorState: `
284
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
285
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} movement: ${ad.serviceData.movement}, doorState: `
251
286
  + `${ad.serviceData.doorState}, lightLevel: ${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}`);
252
287
 
253
288
  if (this.serviceData) {
254
289
  this.connected = true;
255
- this.platform.device(`Contact Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
290
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
256
291
  } else {
257
292
  this.connected = false;
258
- this.platform.device(`Contact Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
293
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
259
294
  }
260
295
  };
261
296
  // Wait 10 seconds
@@ -267,49 +302,49 @@ export class Contact {
267
302
  this.parseStatus();
268
303
  this.updateHomeKitCharacteristics();
269
304
  } else {
270
- this.platform.log.error(`Contact Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
305
+ this.errorLog(`Contact Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
271
306
  if (this.platform.config.credentials?.openToken) {
272
- this.platform.log.warn(`Contact Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
307
+ this.warnLog(`Contact Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
273
308
  await this.openAPIRefreshStatus();
274
309
  }
275
310
  }
276
311
  }).catch(async (e: any) => {
277
- this.platform.log.error(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
278
- if (this.deviceDebug) {
279
- this.platform.log.error(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
312
+ this.errorLog(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
313
+ if (this.deviceLogging === 'debug') {
314
+ this.errorLog(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
280
315
  + ` Error Message: ${JSON.stringify(e.message)}`);
281
316
  }
282
- if (this.debugDebug) {
283
- this.platform.log.error(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
317
+ if (this.platform.debugMode) {
318
+ this.errorLog(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
284
319
  + ` Error: ${JSON.stringify(e)}`);
285
320
  }
286
321
  if (this.platform.config.credentials?.openToken) {
287
- this.platform.log.warn(`Contact Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
322
+ this.warnLog(`Contact Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
288
323
  await this.openAPIRefreshStatus();
289
324
  }
290
- this.apiError(e);
325
+ this.apiError();
291
326
  });
292
327
  }
293
328
 
294
329
  private async openAPIRefreshStatus() {
295
330
  if (this.platform.config.credentials?.openToken) {
296
- this.platform.debug(`Contact Sensor: ${this.accessory.displayName} OpenAPI refreshStatus`);
331
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} OpenAPI refreshStatus`);
297
332
  try {
298
333
  this.deviceStatus = (await this.platform.axios.get(`${DeviceURL}/${this.device.deviceId}/status`)).data;
299
- this.platform.debug(`Contact Sensor: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(this.deviceStatus)}`);
334
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(this.deviceStatus)}`);
300
335
  this.parseStatus();
301
336
  this.updateHomeKitCharacteristics();
302
337
  } catch (e: any) {
303
- this.platform.log.error(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
304
- if (this.deviceDebug) {
305
- this.platform.log.error(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
338
+ this.errorLog(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
339
+ if (this.deviceLogging === 'debug') {
340
+ this.errorLog(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
306
341
  + ` Error Message: ${JSON.stringify(e.message)}`);
307
342
  }
308
- if (this.debugDebug) {
309
- this.platform.log.error(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
343
+ if (this.platform.debugMode) {
344
+ this.errorLog(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
310
345
  + ` Error: ${JSON.stringify(e)}`);
311
346
  }
312
- this.apiError(e);
347
+ this.apiError();
313
348
  }
314
349
  }
315
350
  }
@@ -319,55 +354,80 @@ export class Contact {
319
354
  */
320
355
  updateHomeKitCharacteristics() {
321
356
  if (this.ContactSensorState === undefined) {
322
- this.platform.debug(`Contact Sensor: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensorState}`);
357
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensorState}`);
323
358
  } else {
324
359
  this.service.updateCharacteristic(this.platform.Characteristic.ContactSensorState, this.ContactSensorState);
325
- this.platform.device(`Contact Sensor: ${this.accessory.displayName} updateCharacteristic ContactSensorState: ${this.ContactSensorState}`);
360
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} updateCharacteristic ContactSensorState: ${this.ContactSensorState}`);
326
361
  }
327
362
  if (!this.device.contact?.hide_motionsensor) {
328
363
  if (this.MotionDetected === undefined) {
329
- this.platform.debug(`Contact Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
364
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
330
365
  } else {
331
366
  this.motionService?.updateCharacteristic(this.platform.Characteristic.MotionDetected, this.MotionDetected);
332
- this.platform.device(`Contact Sensor: ${this.accessory.displayName} updateCharacteristic MotionDetected: ${this.MotionDetected}`);
367
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} updateCharacteristic MotionDetected: ${this.MotionDetected}`);
333
368
  }
334
369
  }
335
370
  if (!this.device.contact?.hide_lightsensor) {
336
371
  if (this.CurrentAmbientLightLevel === undefined) {
337
- this.platform.debug(`Contact Sensor: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
372
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
338
373
  } else {
339
374
  this.lightSensorService?.updateCharacteristic(this.platform.Characteristic.CurrentAmbientLightLevel, this.CurrentAmbientLightLevel);
340
- this.platform.device(`Contact Sensor: ${this.accessory.displayName}`
375
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName}`
341
376
  + ` updateCharacteristic CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
342
377
  }
343
378
  }
344
379
  if (this.device.ble) {
345
380
  if (this.BatteryLevel === undefined) {
346
- this.platform.debug(`Contact Sensor: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
381
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
347
382
  } else {
348
383
  this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
349
- this.platform.device(`Contact Sensor: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
384
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
350
385
  }
351
386
  if (this.StatusLowBattery === undefined) {
352
- this.platform.debug(`Contact Sensor: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
387
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
353
388
  } else {
354
389
  this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
355
- this.platform.device(`Contact Sensor: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
390
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
356
391
  }
357
392
  }
358
393
  }
359
394
 
360
- public apiError(e: any) {
361
- this.service.updateCharacteristic(this.platform.Characteristic.ContactSensorState, e);
362
- if (!this.device.contact?.hide_motionsensor) {
363
- this.motionService?.updateCharacteristic(this.platform.Characteristic.MotionDetected, e);
395
+ public apiError() {
396
+ throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
397
+ }
398
+
399
+ /**
400
+ * Logging for Device
401
+ */
402
+ infoLog(...log: any[]) {
403
+ if (this.enablingDeviceLogging()) {
404
+ this.platform.log.info(String(...log));
364
405
  }
365
- if (!this.device.contact?.hide_lightsensor) {
366
- this.lightSensorService?.updateCharacteristic(this.platform.Characteristic.CurrentAmbientLightLevel, e);
406
+ }
407
+
408
+ warnLog(...log: any[]) {
409
+ if (this.enablingDeviceLogging()) {
410
+ this.platform.log.warn(String(...log));
367
411
  }
368
- if (this.device.ble) {
369
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
370
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
412
+ }
413
+
414
+ errorLog(...log: any[]) {
415
+ if (this.enablingDeviceLogging()) {
416
+ this.platform.log.error(String(...log));
371
417
  }
372
418
  }
419
+
420
+ debugLog(...log: any[]) {
421
+ if (this.enablingDeviceLogging()) {
422
+ if (this.deviceLogging === 'debug') {
423
+ this.platform.log.info('[DEBUG]', String(...log));
424
+ } else {
425
+ this.platform.log.debug(String(...log));
426
+ }
427
+ }
428
+ }
429
+
430
+ enablingDeviceLogging(): boolean {
431
+ return this.deviceLogging === 'debug' || this.deviceLogging === 'standard';
432
+ }
373
433
  }