@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 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},`
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,31 @@ 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
+ } else if (this.platform.config.options!.refreshRate) {
150
+ this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options!.refreshRate;
151
+ }
152
+ }
153
+
154
+ logs() {
155
+ if (this.device.logging) {
156
+ this.deviceLogging = this.accessory.context.logging = this.device.logging;
157
+ } else if (this.platform.config.options?.logging) {
158
+ this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
159
+ } else {
160
+ this.deviceLogging = this.accessory.context.logging = 'standard';
161
+ }
162
+ }
163
+
144
164
  /**
145
165
  * Parse the device status from the SwitchBot api
146
166
  */
@@ -153,7 +173,7 @@ export class Contact {
153
173
  }
154
174
 
155
175
  private async BLEparseStatus() {
156
- this.platform.debug(`Contact Sensor: ${this.accessory.displayName} BLE parseStatus`);
176
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} BLE parseStatus`);
157
177
  if (this.device.contact?.hide_motionsensor) {
158
178
  // Movement
159
179
  this.MotionDetected = Boolean(this.movement);
@@ -169,7 +189,7 @@ export class Contact {
169
189
  this.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED;
170
190
  break;
171
191
  default:
172
- this.platform.log.error(`Contact Sensor: ${this.accessory.displayName} timeout no closed, doorstate: ${this.doorState}`);
192
+ this.errorLog(`Contact Sensor: ${this.accessory.displayName} timeout no closed, doorstate: ${this.doorState}`);
173
193
  }
174
194
  if (this.device.contact?.hide_lightsensor) {// Light Level
175
195
  switch (this.lightLevel) {
@@ -189,26 +209,26 @@ export class Contact {
189
209
  this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
190
210
  }
191
211
 
192
- this.platform.debug(`Contact Sensor: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensorState}, MotionDetected: `
212
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensorState}, MotionDetected: `
193
213
  + `${this.MotionDetected}, CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}, BatteryLevel: ${this.BatteryLevel}`);
194
214
  }
195
215
 
196
216
  private async openAPIparseStatus() {
197
217
  if (this.platform.config.credentials?.openToken) {
198
- this.platform.debug(`Contact Sensor: ${this.accessory.displayName} OpenAPI parseStatus`);
218
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} OpenAPI parseStatus`);
199
219
  if (this.deviceStatus.body.openState === 'open') {
200
220
  this.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED;
201
- this.platform.device(`Contact Sensor: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensorState}`);
221
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensorState}`);
202
222
  } else if (this.deviceStatus.body.openState === 'close') {
203
223
  this.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED;
204
- this.platform.device(`Contact Sensor: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensorState}`);
224
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensorState}`);
205
225
  } else {
206
- this.platform.device(`Contact Sensor: ${this.accessory.displayName} openState: ${this.deviceStatus.body.openState}`);
226
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} openState: ${this.deviceStatus.body.openState}`);
207
227
  }
208
228
  if (this.device.contact?.hide_motionsensor) {
209
229
  this.MotionDetected = Boolean(this.deviceStatus.body.moveDetected);
210
230
  }
211
- this.platform.debug(`Contact Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
231
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
212
232
  }
213
233
  }
214
234
 
@@ -226,12 +246,12 @@ export class Contact {
226
246
  private connectBLE() {
227
247
  const switchbot = new Switchbot();
228
248
  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}`);
249
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
230
250
  return switchbot;
231
251
  }
232
252
 
233
253
  private async BLERefreshStatus() {
234
- this.platform.debug(`Contact Sensor: ${this.accessory.displayName} BLE refreshStatus`);
254
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} BLE refreshStatus`);
235
255
  // eslint-disable-next-line @typescript-eslint/no-var-requires
236
256
  const switchbot = this.connectBLE();
237
257
  // Start to monitor advertisement packets
@@ -246,16 +266,16 @@ export class Contact {
246
266
  this.doorState = ad.serviceData.doorState;
247
267
  this.lightLevel = ad.serviceData.lightLevel;
248
268
  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: `
269
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
270
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} movement: ${ad.serviceData.movement}, doorState: `
251
271
  + `${ad.serviceData.doorState}, lightLevel: ${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}`);
252
272
 
253
273
  if (this.serviceData) {
254
274
  this.connected = true;
255
- this.platform.device(`Contact Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
275
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
256
276
  } else {
257
277
  this.connected = false;
258
- this.platform.device(`Contact Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
278
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
259
279
  }
260
280
  };
261
281
  // Wait 10 seconds
@@ -267,49 +287,49 @@ export class Contact {
267
287
  this.parseStatus();
268
288
  this.updateHomeKitCharacteristics();
269
289
  } else {
270
- this.platform.log.error(`Contact Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
290
+ this.errorLog(`Contact Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
271
291
  if (this.platform.config.credentials?.openToken) {
272
- this.platform.log.warn(`Contact Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
292
+ this.warnLog(`Contact Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
273
293
  await this.openAPIRefreshStatus();
274
294
  }
275
295
  }
276
296
  }).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,`
297
+ this.errorLog(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
298
+ if (this.deviceLogging === 'debug') {
299
+ this.errorLog(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
280
300
  + ` Error Message: ${JSON.stringify(e.message)}`);
281
301
  }
282
- if (this.debugDebug) {
283
- this.platform.log.error(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
302
+ if (this.platform.debugMode) {
303
+ this.errorLog(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
284
304
  + ` Error: ${JSON.stringify(e)}`);
285
305
  }
286
306
  if (this.platform.config.credentials?.openToken) {
287
- this.platform.log.warn(`Contact Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
307
+ this.warnLog(`Contact Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
288
308
  await this.openAPIRefreshStatus();
289
309
  }
290
- this.apiError(e);
310
+ this.apiError();
291
311
  });
292
312
  }
293
313
 
294
314
  private async openAPIRefreshStatus() {
295
315
  if (this.platform.config.credentials?.openToken) {
296
- this.platform.debug(`Contact Sensor: ${this.accessory.displayName} OpenAPI refreshStatus`);
316
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} OpenAPI refreshStatus`);
297
317
  try {
298
318
  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)}`);
319
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(this.deviceStatus)}`);
300
320
  this.parseStatus();
301
321
  this.updateHomeKitCharacteristics();
302
322
  } 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,`
323
+ this.errorLog(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
324
+ if (this.deviceLogging === 'debug') {
325
+ this.errorLog(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
306
326
  + ` Error Message: ${JSON.stringify(e.message)}`);
307
327
  }
308
- if (this.debugDebug) {
309
- this.platform.log.error(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
328
+ if (this.platform.debugMode) {
329
+ this.errorLog(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
310
330
  + ` Error: ${JSON.stringify(e)}`);
311
331
  }
312
- this.apiError(e);
332
+ this.apiError();
313
333
  }
314
334
  }
315
335
  }
@@ -319,55 +339,80 @@ export class Contact {
319
339
  */
320
340
  updateHomeKitCharacteristics() {
321
341
  if (this.ContactSensorState === undefined) {
322
- this.platform.debug(`Contact Sensor: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensorState}`);
342
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensorState}`);
323
343
  } else {
324
344
  this.service.updateCharacteristic(this.platform.Characteristic.ContactSensorState, this.ContactSensorState);
325
- this.platform.device(`Contact Sensor: ${this.accessory.displayName} updateCharacteristic ContactSensorState: ${this.ContactSensorState}`);
345
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} updateCharacteristic ContactSensorState: ${this.ContactSensorState}`);
326
346
  }
327
347
  if (!this.device.contact?.hide_motionsensor) {
328
348
  if (this.MotionDetected === undefined) {
329
- this.platform.debug(`Contact Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
349
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
330
350
  } else {
331
351
  this.motionService?.updateCharacteristic(this.platform.Characteristic.MotionDetected, this.MotionDetected);
332
- this.platform.device(`Contact Sensor: ${this.accessory.displayName} updateCharacteristic MotionDetected: ${this.MotionDetected}`);
352
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} updateCharacteristic MotionDetected: ${this.MotionDetected}`);
333
353
  }
334
354
  }
335
355
  if (!this.device.contact?.hide_lightsensor) {
336
356
  if (this.CurrentAmbientLightLevel === undefined) {
337
- this.platform.debug(`Contact Sensor: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
357
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
338
358
  } else {
339
359
  this.lightSensorService?.updateCharacteristic(this.platform.Characteristic.CurrentAmbientLightLevel, this.CurrentAmbientLightLevel);
340
- this.platform.device(`Contact Sensor: ${this.accessory.displayName}`
360
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName}`
341
361
  + ` updateCharacteristic CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
342
362
  }
343
363
  }
344
364
  if (this.device.ble) {
345
365
  if (this.BatteryLevel === undefined) {
346
- this.platform.debug(`Contact Sensor: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
366
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
347
367
  } else {
348
368
  this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
349
- this.platform.device(`Contact Sensor: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
369
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
350
370
  }
351
371
  if (this.StatusLowBattery === undefined) {
352
- this.platform.debug(`Contact Sensor: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
372
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
353
373
  } else {
354
374
  this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
355
- this.platform.device(`Contact Sensor: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
375
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
356
376
  }
357
377
  }
358
378
  }
359
379
 
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);
380
+ public apiError() {
381
+ throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
382
+ }
383
+
384
+ /**
385
+ * Logging for Device
386
+ */
387
+ infoLog(...log: any[]) {
388
+ if (this.enablingDeviceLogging()) {
389
+ this.platform.log.info(String(...log));
364
390
  }
365
- if (!this.device.contact?.hide_lightsensor) {
366
- this.lightSensorService?.updateCharacteristic(this.platform.Characteristic.CurrentAmbientLightLevel, e);
391
+ }
392
+
393
+ warnLog(...log: any[]) {
394
+ if (this.enablingDeviceLogging()) {
395
+ this.platform.log.warn(String(...log));
367
396
  }
368
- if (this.device.ble) {
369
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
370
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
397
+ }
398
+
399
+ errorLog(...log: any[]) {
400
+ if (this.enablingDeviceLogging()) {
401
+ this.platform.log.error(String(...log));
371
402
  }
372
403
  }
404
+
405
+ debugLog(...log: any[]) {
406
+ if (this.enablingDeviceLogging()) {
407
+ if (this.deviceLogging === 'debug') {
408
+ this.platform.log.info('[DEBUG]', String(...log));
409
+ } else {
410
+ this.platform.log.debug(String(...log));
411
+ }
412
+ }
413
+ }
414
+
415
+ enablingDeviceLogging(): boolean {
416
+ return this.deviceLogging === 'debug' || this.deviceLogging === 'standard';
417
+ }
373
418
  }