@switchbot/homebridge-switchbot 2.0.0-beta.9 → 2.0.1-beta.0

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 (119) hide show
  1. package/CHANGELOG.md +10 -1
  2. package/README.md +3 -2
  3. package/config.schema.json +119 -19
  4. package/dist/device/bot.d.ts +34 -28
  5. package/dist/device/bot.d.ts.map +1 -1
  6. package/dist/device/bot.js +657 -648
  7. package/dist/device/bot.js.map +1 -1
  8. package/dist/device/ceilinglight.d.ts +137 -0
  9. package/dist/device/ceilinglight.d.ts.map +1 -0
  10. package/dist/device/ceilinglight.js +1127 -0
  11. package/dist/device/ceilinglight.js.map +1 -0
  12. package/dist/device/colorbulb.d.ts +36 -14
  13. package/dist/device/colorbulb.d.ts.map +1 -1
  14. package/dist/device/colorbulb.js +472 -297
  15. package/dist/device/colorbulb.js.map +1 -1
  16. package/dist/device/contact.d.ts +18 -8
  17. package/dist/device/contact.d.ts.map +1 -1
  18. package/dist/device/contact.js +306 -237
  19. package/dist/device/contact.js.map +1 -1
  20. package/dist/device/curtain.d.ts +23 -16
  21. package/dist/device/curtain.d.ts.map +1 -1
  22. package/dist/device/curtain.js +566 -502
  23. package/dist/device/curtain.js.map +1 -1
  24. package/dist/device/humidifier.d.ts +24 -18
  25. package/dist/device/humidifier.d.ts.map +1 -1
  26. package/dist/device/humidifier.js +453 -369
  27. package/dist/device/humidifier.js.map +1 -1
  28. package/dist/device/lock.d.ts +27 -8
  29. package/dist/device/lock.d.ts.map +1 -1
  30. package/dist/device/lock.js +305 -148
  31. package/dist/device/lock.js.map +1 -1
  32. package/dist/device/meter.d.ts +22 -16
  33. package/dist/device/meter.d.ts.map +1 -1
  34. package/dist/device/meter.js +367 -267
  35. package/dist/device/meter.js.map +1 -1
  36. package/dist/device/meterplus.d.ts +24 -12
  37. package/dist/device/meterplus.d.ts.map +1 -1
  38. package/dist/device/meterplus.js +381 -213
  39. package/dist/device/meterplus.js.map +1 -1
  40. package/dist/device/motion.d.ts +18 -8
  41. package/dist/device/motion.d.ts.map +1 -1
  42. package/dist/device/motion.js +279 -216
  43. package/dist/device/motion.js.map +1 -1
  44. package/dist/device/plug.d.ts +27 -17
  45. package/dist/device/plug.d.ts.map +1 -1
  46. package/dist/device/plug.js +295 -251
  47. package/dist/device/plug.js.map +1 -1
  48. package/dist/device/striplight.d.ts +43 -19
  49. package/dist/device/striplight.d.ts.map +1 -1
  50. package/dist/device/striplight.js +506 -262
  51. package/dist/device/striplight.js.map +1 -1
  52. package/dist/irdevice/airconditioner.d.ts +12 -12
  53. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  54. package/dist/irdevice/airconditioner.js +316 -234
  55. package/dist/irdevice/airconditioner.js.map +1 -1
  56. package/dist/irdevice/airpurifier.d.ts +5 -3
  57. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  58. package/dist/irdevice/airpurifier.js +154 -104
  59. package/dist/irdevice/airpurifier.js.map +1 -1
  60. package/dist/irdevice/camera.d.ts +11 -8
  61. package/dist/irdevice/camera.d.ts.map +1 -1
  62. package/dist/irdevice/camera.js +133 -86
  63. package/dist/irdevice/camera.js.map +1 -1
  64. package/dist/irdevice/fan.d.ts +10 -7
  65. package/dist/irdevice/fan.d.ts.map +1 -1
  66. package/dist/irdevice/fan.js +160 -109
  67. package/dist/irdevice/fan.js.map +1 -1
  68. package/dist/irdevice/light.d.ts +11 -8
  69. package/dist/irdevice/light.d.ts.map +1 -1
  70. package/dist/irdevice/light.js +137 -103
  71. package/dist/irdevice/light.js.map +1 -1
  72. package/dist/irdevice/other.d.ts +11 -8
  73. package/dist/irdevice/other.d.ts.map +1 -1
  74. package/dist/irdevice/other.js +145 -98
  75. package/dist/irdevice/other.js.map +1 -1
  76. package/dist/irdevice/tv.d.ts +12 -9
  77. package/dist/irdevice/tv.d.ts.map +1 -1
  78. package/dist/irdevice/tv.js +143 -93
  79. package/dist/irdevice/tv.js.map +1 -1
  80. package/dist/irdevice/vacuumcleaner.d.ts +9 -10
  81. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  82. package/dist/irdevice/vacuumcleaner.js +132 -89
  83. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  84. package/dist/irdevice/waterheater.d.ts +8 -9
  85. package/dist/irdevice/waterheater.d.ts.map +1 -1
  86. package/dist/irdevice/waterheater.js +132 -89
  87. package/dist/irdevice/waterheater.js.map +1 -1
  88. package/dist/platform.d.ts +13 -12
  89. package/dist/platform.d.ts.map +1 -1
  90. package/dist/platform.js +530 -421
  91. package/dist/platform.js.map +1 -1
  92. package/dist/settings.d.ts +22 -11
  93. package/dist/settings.d.ts.map +1 -1
  94. package/dist/settings.js.map +1 -1
  95. package/homebridge-ui/public/index.html +1 -0
  96. package/package.json +11 -9
  97. package/src/device/bot.ts +656 -659
  98. package/src/device/ceilinglight.ts +1208 -0
  99. package/src/device/colorbulb.ts +482 -334
  100. package/src/device/contact.ts +309 -241
  101. package/src/device/curtain.ts +571 -516
  102. package/src/device/humidifier.ts +456 -403
  103. package/src/device/lock.ts +314 -149
  104. package/src/device/meter.ts +378 -280
  105. package/src/device/meterplus.ts +391 -219
  106. package/src/device/motion.ts +285 -222
  107. package/src/device/plug.ts +299 -260
  108. package/src/device/striplight.ts +522 -289
  109. package/src/irdevice/airconditioner.ts +322 -245
  110. package/src/irdevice/airpurifier.ts +151 -103
  111. package/src/irdevice/camera.ts +131 -85
  112. package/src/irdevice/fan.ts +157 -106
  113. package/src/irdevice/light.ts +135 -103
  114. package/src/irdevice/other.ts +143 -97
  115. package/src/irdevice/tv.ts +142 -93
  116. package/src/irdevice/vacuumcleaner.ts +131 -89
  117. package/src/irdevice/waterheater.ts +130 -89
  118. package/src/platform.ts +528 -449
  119. package/src/settings.ts +23 -11
@@ -3,9 +3,11 @@ import crypto from 'crypto';
3
3
  import { Context } from 'vm';
4
4
  import { interval, Subject } from 'rxjs';
5
5
  import { skipWhile } from 'rxjs/operators';
6
+ import superStringify from 'super-stringify';
6
7
  import { SwitchBotPlatform } from '../platform';
7
8
  import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
8
9
  import { device, devicesConfig, serviceData, switchbot, deviceStatus, ad, HostDomain, DevicePath } from '../settings';
10
+ import { IncomingMessage } from 'http';
9
11
 
10
12
  /**
11
13
  * Platform Accessory
@@ -35,7 +37,6 @@ export class Contact {
35
37
  // BLE Others
36
38
  connected?: boolean;
37
39
  switchbot!: switchbot;
38
- SwitchToOpenAPI!: boolean;
39
40
  serviceData!: serviceData;
40
41
  address!: ad['address'];
41
42
  battery!: serviceData['battery'];
@@ -54,13 +55,17 @@ export class Contact {
54
55
  contactUbpdateInProgress!: boolean;
55
56
  doContactUpdate!: Subject<void>;
56
57
 
58
+ // Connection
59
+ private readonly BLE = (this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI');
60
+ private readonly OpenAPI = (this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI');
61
+
57
62
  constructor(private readonly platform: SwitchBotPlatform, private accessory: PlatformAccessory, public device: device & devicesConfig) {
58
63
  // default placeholders
59
64
  this.logs(device);
60
65
  this.scan(device);
61
66
  this.refreshRate(device);
62
67
  this.config(device);
63
- this.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED;
68
+ this.context();
64
69
 
65
70
  // this is subject we use to track when we need to POST changes to the SwitchBot API
66
71
  this.doContactUpdate = new Subject();
@@ -98,49 +103,49 @@ export class Contact {
98
103
 
99
104
  // Motion Sensor Service
100
105
  if (device.contact?.hide_motionsensor) {
101
- this.debugLog(`Contact Sensor: ${accessory.displayName} Removing Motion Sensor Service`);
106
+ this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Motion Sensor Service`);
102
107
  this.motionService = this.accessory.getService(this.platform.Service.MotionSensor);
103
108
  accessory.removeService(this.motionService!);
104
109
  } else if (!this.motionService) {
105
- this.debugLog(`Contact Sensor: ${accessory.displayName} Add Motion Sensor Service`);
110
+ this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Motion Sensor Service`);
106
111
  (this.motionService =
107
112
  this.accessory.getService(this.platform.Service.MotionSensor) || this.accessory.addService(this.platform.Service.MotionSensor)),
108
113
  `${accessory.displayName} Motion Sensor`;
109
114
 
110
115
  this.motionService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Motion Sensor`);
111
116
  } else {
112
- this.debugLog(`Contact Sensor: ${accessory.displayName} Motion Sensor Service Not Added`);
117
+ this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Motion Sensor Service Not Added`);
113
118
  }
114
119
 
115
120
  // Light Sensor Service
116
121
  if (device.contact?.hide_lightsensor) {
117
- this.debugLog(`Contact Sensor: ${accessory.displayName} Removing Light Sensor Service`);
122
+ this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Light Sensor Service`);
118
123
  this.lightSensorService = this.accessory.getService(this.platform.Service.LightSensor);
119
124
  accessory.removeService(this.lightSensorService!);
120
125
  } else if (!this.lightSensorService) {
121
- this.debugLog(`Contact Sensor: ${accessory.displayName} Add Light Sensor Service`);
126
+ this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Light Sensor Service`);
122
127
  (this.lightSensorService =
123
128
  this.accessory.getService(this.platform.Service.LightSensor) || this.accessory.addService(this.platform.Service.LightSensor)),
124
129
  `${accessory.displayName} Light Sensor`;
125
130
 
126
131
  this.lightSensorService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Light Sensor`);
127
132
  } else {
128
- this.debugLog(`Contact Sensor: ${accessory.displayName} Light Sensor Service Not Added`);
133
+ this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Light Sensor Service Not Added`);
129
134
  }
130
135
 
131
136
  // Battery Service
132
- if (!device.ble) {
133
- this.debugLog(`Contact Sensor: ${accessory.displayName} Removing Battery Service`);
137
+ if (!this.BLE) {
138
+ this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Battery Service`);
134
139
  this.batteryService = this.accessory.getService(this.platform.Service.Battery);
135
140
  accessory.removeService(this.batteryService!);
136
- } else if (device.ble && !this.batteryService) {
137
- this.debugLog(`Contact Sensor: ${accessory.displayName} Add Battery Service`);
141
+ } else if (this.BLE && !this.batteryService) {
142
+ this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Battery Service`);
138
143
  (this.batteryService = this.accessory.getService(this.platform.Service.Battery) || this.accessory.addService(this.platform.Service.Battery)),
139
144
  `${accessory.displayName} Battery`;
140
145
 
141
146
  this.batteryService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Battery`);
142
147
  } else {
143
- this.debugLog(`Contact Sensor: ${accessory.displayName} Battery Service Not Added`);
148
+ this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Battery Service Not Added`);
144
149
  }
145
150
 
146
151
  // Retrieve initial values and updateHomekit
@@ -158,15 +163,18 @@ export class Contact {
158
163
  * Parse the device status from the SwitchBot api
159
164
  */
160
165
  async parseStatus(): Promise<void> {
161
- if (this.SwitchToOpenAPI || !this.device.ble) {
166
+ if (this.BLE) {
167
+ await this.BLEparseStatus();
168
+ } else if (this.OpenAPI && this.platform.config.credentials?.token) {
162
169
  await this.openAPIparseStatus();
163
170
  } else {
164
- await this.BLEparseStatus();
171
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
172
+ + ` ${this.device.connectionType}, parseStatus will not happen.`);
165
173
  }
166
174
  }
167
175
 
168
176
  async BLEparseStatus(): Promise<void> {
169
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} BLE parseStatus`);
177
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
170
178
  // Door State
171
179
  switch (this.doorState) {
172
180
  case 'open':
@@ -178,12 +186,12 @@ export class Contact {
178
186
  this.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED;
179
187
  break;
180
188
  default:
181
- this.errorLog(`Contact Sensor: ${this.accessory.displayName} timeout no closed, doorstate: ${this.doorState}`);
189
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} timeout no closed, doorstate: ${this.doorState}`);
182
190
  }
183
191
  // Movement
184
192
  if (!this.device.contact?.hide_motionsensor) {
185
193
  this.MotionDetected = Boolean(this.movement);
186
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
194
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
187
195
  }
188
196
  // Light Level
189
197
  if (!this.device.contact?.hide_lightsensor) {
@@ -198,7 +206,7 @@ export class Contact {
198
206
  this.CurrentAmbientLightLevel = this.set_maxLux;
199
207
  }
200
208
  this.debugLog(
201
- `Contact Sensor: ${this.accessory.displayName} LightLevel: ${this.lightLevel},` +
209
+ `${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${this.lightLevel},` +
202
210
  ` CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`,
203
211
  );
204
212
  }
@@ -212,44 +220,40 @@ export class Contact {
212
220
  } else {
213
221
  this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
214
222
  }
215
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
223
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, `
224
+ + `StatusLowBattery: ${this.StatusLowBattery}`);
216
225
  }
217
226
 
218
227
  async openAPIparseStatus(): Promise<void> {
219
- if (this.device.ble) {
220
- this.SwitchToOpenAPI = false;
228
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
229
+ // Contact State
230
+ if (this.openState === 'open') {
231
+ this.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED;
232
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensorState}`);
233
+ } else if (this.openState === 'close') {
234
+ this.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED;
235
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensorState}`);
236
+ } else {
237
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openState: ${this.openState}`);
221
238
  }
222
- if (this.platform.config.credentials?.token) {
223
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} OpenAPI parseStatus`);
224
- // Contact State
225
- if (this.openState === 'open') {
226
- this.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED;
227
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensorState}`);
228
- } else if (this.openState === 'close') {
229
- this.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED;
230
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensorState}`);
231
- } else {
232
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} openState: ${this.openState}`);
233
- }
234
- // Motion State
235
- if (!this.device.contact?.hide_motionsensor) {
236
- this.MotionDetected = this.moveDetected!;
237
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
238
- }
239
- // Light Level
240
- if (!this.device.contact?.hide_lightsensor) {
241
- this.set_minLux = this.minLux();
242
- this.set_maxLux = this.maxLux();
243
- switch (this.brightness) {
244
- case 'dim':
245
- this.CurrentAmbientLightLevel = this.set_minLux;
246
- break;
247
- case 'bright':
248
- default:
249
- this.CurrentAmbientLightLevel = this.set_maxLux;
250
- }
251
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
239
+ // Motion State
240
+ if (!this.device.contact?.hide_motionsensor) {
241
+ this.MotionDetected = this.moveDetected!;
242
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
243
+ }
244
+ // Light Level
245
+ if (!this.device.contact?.hide_lightsensor) {
246
+ this.set_minLux = this.minLux();
247
+ this.set_maxLux = this.maxLux();
248
+ switch (this.brightness) {
249
+ case 'dim':
250
+ this.CurrentAmbientLightLevel = this.set_minLux;
251
+ break;
252
+ case 'bright':
253
+ default:
254
+ this.CurrentAmbientLightLevel = this.set_maxLux;
252
255
  }
256
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
253
257
  }
254
258
  }
255
259
 
@@ -257,15 +261,18 @@ export class Contact {
257
261
  * Asks the SwitchBot API for the latest device information
258
262
  */
259
263
  async refreshStatus(): Promise<void> {
260
- if (this.device.ble) {
264
+ if (this.BLE) {
261
265
  await this.BLERefreshStatus();
262
- } else {
266
+ } else if (this.OpenAPI) {
263
267
  await this.openAPIRefreshStatus();
268
+ } else {
269
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
270
+ + ` ${this.device.connectionType}, refreshStatus will not happen.`);
264
271
  }
265
272
  }
266
273
 
267
274
  async BLERefreshStatus(): Promise<void> {
268
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} BLE refreshStatus`);
275
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
269
276
  const switchbot = await this.platform.connectBLE();
270
277
  // Convert to BLE Address
271
278
  this.device.bleMac =
@@ -274,7 +281,7 @@ export class Contact {
274
281
  .deviceId!.match(/.{1,2}/g)!
275
282
  .join(':')
276
283
  .toLowerCase();
277
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
284
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
278
285
  this.getCustomBLEAddress(switchbot);
279
286
  // Start to monitor advertisement packets
280
287
  if (switchbot !== false) {
@@ -287,29 +294,25 @@ export class Contact {
287
294
  // Set an event hander
288
295
  switchbot.onadvertisement = (ad: any) => {
289
296
  this.address = ad.address;
290
- if (this.deviceLogging.includes('debug')) {
291
- this.infoLog(this.address);
292
- this.infoLog(this.device.bleMac);
293
- this.infoLog(`Contact Sensor: ${this.accessory.displayName} BLE Address Found: ${this.address}`);
294
- this.infoLog(`Contact Sensor: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac}`);
295
- }
297
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},`
298
+ + ` BLE Address Found: ${this.address}`);
296
299
  this.serviceData = ad.serviceData;
297
300
  this.movement = ad.serviceData.movement;
298
301
  this.doorState = ad.serviceData.doorState;
299
302
  this.lightLevel = ad.serviceData.lightLevel;
300
303
  this.battery = ad.serviceData.battery;
301
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
304
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${superStringify(ad.serviceData)}`);
302
305
  this.debugLog(
303
- `Contact Sensor: ${this.accessory.displayName} movement: ${ad.serviceData.movement}, doorState: ` +
306
+ `${this.device.deviceType}: ${this.accessory.displayName} movement: ${ad.serviceData.movement}, doorState: ` +
304
307
  `${ad.serviceData.doorState}, lightLevel: ${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}`,
305
308
  );
306
309
 
307
310
  if (this.serviceData) {
308
311
  this.connected = true;
309
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
312
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
310
313
  } else {
311
314
  this.connected = false;
312
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
315
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
313
316
  }
314
317
  };
315
318
  // Wait 2 seconds
@@ -319,117 +322,74 @@ export class Contact {
319
322
  // Stop to monitor
320
323
  switchbot.stopScan();
321
324
  if (this.connected) {
322
- this.parseStatus();
325
+ this.BLEparseStatus();
323
326
  this.updateHomeKitCharacteristics();
324
327
  } else {
325
- await this.BLEconnection(switchbot);
328
+ await this.BLERefreshConnection(switchbot);
326
329
  }
327
330
  })
328
331
  .catch(async (e: any) => {
329
- this.errorLog(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
330
- if (this.deviceLogging.includes('debug')) {
331
- this.errorLog(
332
- `Contact Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,` +
333
- ` Error Message: ${JSON.stringify(e.message)}`,
334
- );
335
- }
336
- if (this.platform.config.credentials?.token) {
337
- this.warnLog(`Contact Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
338
- this.SwitchToOpenAPI = true;
339
- await this.openAPIRefreshStatus();
340
- }
341
332
  this.apiError(e);
333
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}`
334
+ + ` Connection, Error Message: ${superStringify(e.message)}`);
335
+ await this.BLERefreshConnection(switchbot);
342
336
  });
343
337
  } else {
344
- await this.BLEconnection(switchbot);
345
- }
346
- }
347
-
348
- async getCustomBLEAddress(switchbot: any) {
349
- if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
350
- (async () => {
351
- // Start to monitor advertisement packets
352
- await switchbot.startScan({
353
- model: 'd',
354
- });
355
- // Set an event handler
356
- switchbot.onadvertisement = (ad: any) => {
357
- this.warnLog(`Contact Sensor: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
358
- };
359
- await switchbot.wait(10000);
360
- // Stop to monitor
361
- switchbot.stopScan();
362
- })();
363
- }
364
- }
365
-
366
- async BLEconnection(switchbot: any): Promise<void> {
367
- this.errorLog(`Contact Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
368
- if (this.platform.config.credentials?.token) {
369
- this.warnLog(`Contact Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
370
- this.SwitchToOpenAPI = true;
371
- await this.openAPIRefreshStatus();
338
+ await this.BLERefreshConnection(switchbot);
372
339
  }
373
340
  }
374
341
 
375
342
  async openAPIRefreshStatus(): Promise<void> {
376
- if (this.platform.config.credentials?.token) {
377
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} OpenAPI refreshStatus`);
378
- try {
379
- const t = Date.now();
380
- const nonce = 'requestID';
381
- const data = this.platform.config.credentials?.token + t + nonce;
382
- const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
383
- const sign = signTerm.toString('base64');
384
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} sign: ${sign}`);
385
- const options = {
386
- hostname: HostDomain,
387
- port: 443,
388
- path: `${DevicePath}/${this.device.deviceId}/status`,
389
- method: 'GET',
390
- headers: {
391
- Authorization: this.platform.config.credentials?.token,
392
- sign: sign,
393
- nonce: nonce,
394
- t: t,
395
- 'Content-Type': 'application/json',
396
- },
397
- };
398
- const req = https.request(options, (res) => {
399
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
400
- let rawData = '';
401
- res.on('data', (d) => {
402
- rawData += d;
403
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} d: ${d}`);
404
- });
405
- res.on('end', () => {
406
- try {
407
- this.deviceStatus = JSON.parse(rawData);
408
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(this.deviceStatus)}`);
409
- this.openState = this.deviceStatus.body.openState;
410
- this.moveDetected = this.deviceStatus.body.moveDetected;
411
- this.brightness = this.deviceStatus.body.brightness;
412
- this.parseStatus();
413
- this.updateHomeKitCharacteristics();
414
- } catch (e: any) {
415
- this.errorLog(`Contact Sensor: ${this.accessory.displayName} error message: ${e.message}`);
416
- }
417
- });
343
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
344
+ try {
345
+ const t = Date.now();
346
+ const nonce = 'requestID';
347
+ const data = this.platform.config.credentials?.token + t + nonce;
348
+ const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
349
+ const sign = signTerm.toString('base64');
350
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
351
+ const options = {
352
+ hostname: HostDomain,
353
+ port: 443,
354
+ path: `${DevicePath}/${this.device.deviceId}/status`,
355
+ method: 'GET',
356
+ headers: {
357
+ Authorization: this.platform.config.credentials?.token,
358
+ sign: sign,
359
+ nonce: nonce,
360
+ t: t,
361
+ 'Content-Type': 'application/json',
362
+ },
363
+ };
364
+ const req = https.request(options, (res) => {
365
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus statusCode: ${res.statusCode}`);
366
+ let rawData = '';
367
+ res.on('data', (d) => {
368
+ rawData += d;
369
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
418
370
  });
419
- req.on('error', (e: any) => {
420
- this.errorLog(`Contact Sensor: ${this.accessory.displayName} error message: ${e.message}`);
371
+ res.on('end', () => {
372
+ try {
373
+ this.deviceStatus = JSON.parse(rawData);
374
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus: ${superStringify(this.deviceStatus)}`);
375
+ this.openState = this.deviceStatus.body.openState;
376
+ this.moveDetected = this.deviceStatus.body.moveDetected;
377
+ this.brightness = this.deviceStatus.body.brightness;
378
+ this.openAPIparseStatus();
379
+ this.updateHomeKitCharacteristics();
380
+ } catch (e: any) {
381
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
382
+ }
421
383
  });
422
- req.end();
423
- } catch (e: any) {
424
- this.errorLog(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
425
- if (this.deviceLogging.includes('debug')) {
426
- this.errorLog(
427
- `Contact Sensor: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,` +
428
- ` Error Message: ${JSON.stringify(e.message)}`,
429
- );
430
- }
431
- this.apiError(e);
432
- }
384
+ });
385
+ req.on('error', (e: any) => {
386
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
387
+ });
388
+ req.end();
389
+ } catch (e: any) {
390
+ this.apiError(e);
391
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
392
+ + ` Connection, Error Message: ${superStringify(e.message)}`);
433
393
  }
434
394
  }
435
395
 
@@ -438,45 +398,153 @@ export class Contact {
438
398
  */
439
399
  async updateHomeKitCharacteristics(): Promise<void> {
440
400
  if (this.ContactSensorState === undefined) {
441
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensorState}`);
401
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensorState}`);
442
402
  } else {
403
+ this.accessory.context.ContactSensorState = this.ContactSensorState;
443
404
  this.contactSensorservice.updateCharacteristic(this.platform.Characteristic.ContactSensorState, this.ContactSensorState);
444
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} updateCharacteristic ContactSensorState: ${this.ContactSensorState}`);
405
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic ContactSensorState: ${this.ContactSensorState}`);
445
406
  }
446
407
  if (!this.device.contact?.hide_motionsensor) {
447
408
  if (this.MotionDetected === undefined) {
448
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
409
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
449
410
  } else {
411
+ this.accessory.context.MotionDetected = this.MotionDetected;
450
412
  this.motionService?.updateCharacteristic(this.platform.Characteristic.MotionDetected, this.MotionDetected);
451
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} updateCharacteristic MotionDetected: ${this.MotionDetected}`);
413
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic MotionDetected: ${this.MotionDetected}`);
452
414
  }
453
415
  }
454
416
  if (!this.device.contact?.hide_lightsensor) {
455
417
  if (this.CurrentAmbientLightLevel === undefined) {
456
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
418
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
457
419
  } else {
420
+ this.accessory.context.CurrentAmbientLightLevel = this.CurrentAmbientLightLevel;
458
421
  this.lightSensorService?.updateCharacteristic(this.platform.Characteristic.CurrentAmbientLightLevel, this.CurrentAmbientLightLevel);
459
- this.debugLog(
460
- `Contact Sensor: ${this.accessory.displayName}` + ` updateCharacteristic CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`,
422
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
423
+ + ` updateCharacteristic CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`,
461
424
  );
462
425
  }
463
426
  }
464
- if (this.device.ble) {
427
+ if (this.BLE) {
465
428
  if (this.BatteryLevel === undefined) {
466
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
429
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
467
430
  } else {
431
+ this.accessory.context.BatteryLevel = this.BatteryLevel;
468
432
  this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
469
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
433
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
470
434
  }
471
435
  if (this.StatusLowBattery === undefined) {
472
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
436
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
473
437
  } else {
438
+ this.accessory.context.StatusLowBattery = this.StatusLowBattery;
474
439
  this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
475
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
440
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
441
+ }
442
+ }
443
+ }
444
+
445
+ async getCustomBLEAddress(switchbot: any) {
446
+ if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
447
+ (async () => {
448
+ // Start to monitor advertisement packets
449
+ await switchbot.startScan({
450
+ model: 'd',
451
+ });
452
+ // Set an event handler
453
+ switchbot.onadvertisement = (ad: any) => {
454
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${superStringify(ad, null, ' ')}`);
455
+ };
456
+ await switchbot.wait(10000);
457
+ // Stop to monitor
458
+ switchbot.stopScan();
459
+ })();
460
+ }
461
+ }
462
+
463
+ async BLERefreshConnection(switchbot: any): Promise<void> {
464
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
465
+ if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
466
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Refresh Status`);
467
+ await this.openAPIRefreshStatus();
468
+ }
469
+ }
470
+
471
+ minLux(): number {
472
+ if (this.device.contact?.set_minLux) {
473
+ this.set_minLux = this.device.contact!.set_minLux!;
474
+ } else {
475
+ this.set_minLux = 1;
476
+ }
477
+ return this.set_minLux;
478
+ }
479
+
480
+ maxLux(): number {
481
+ if (this.device.contact?.set_maxLux) {
482
+ this.set_maxLux = this.device.contact!.set_maxLux!;
483
+ } else {
484
+ this.set_maxLux = 6001;
485
+ }
486
+ return this.set_maxLux;
487
+ }
488
+
489
+ async scan(device: device & devicesConfig): Promise<void> {
490
+ if (device.scanDuration) {
491
+ this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
492
+ if (this.BLE) {
493
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
494
+ }
495
+ } else {
496
+ this.scanDuration = this.accessory.context.scanDuration = 1;
497
+ if (this.BLE) {
498
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
476
499
  }
477
500
  }
478
501
  }
479
502
 
503
+ async statusCode({ res }: { res: IncomingMessage }): Promise<void> {
504
+ switch (res.statusCode) {
505
+ case 151:
506
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this device type.`);
507
+ break;
508
+ case 152:
509
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found.`);
510
+ break;
511
+ case 160:
512
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported.`);
513
+ break;
514
+ case 161:
515
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline.`);
516
+ this.offlineOff();
517
+ break;
518
+ case 171:
519
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} is offline. Hub: ${this.device.hubDeviceId}`);
520
+ this.offlineOff();
521
+ break;
522
+ case 190:
523
+ this.errorLog(
524
+ `${this.device.deviceType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
525
+ ` Or command: ${superStringify(res)} format is invalid`,
526
+ );
527
+ break;
528
+ case 100:
529
+ if (this.platform.debugMode) {
530
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent.`);
531
+ }
532
+ break;
533
+ default:
534
+ if (this.platform.debugMode) {
535
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode.`);
536
+ }
537
+ }
538
+ }
539
+
540
+ async offlineOff(): Promise<void> {
541
+ if (this.device.offline) {
542
+ this.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED;
543
+ this.contactSensorservice.getCharacteristic(this.platform.Characteristic.ContactSensorState).updateValue(this.ContactSensorState);
544
+ await this.updateHomeKitCharacteristics();
545
+ }
546
+ }
547
+
480
548
  async apiError(e: any): Promise<void> {
481
549
  this.contactSensorservice.updateCharacteristic(this.platform.Characteristic.ContactSensorState, e);
482
550
  if (!this.device.contact?.hide_motionsensor) {
@@ -485,11 +553,49 @@ export class Contact {
485
553
  if (!this.device.contact?.hide_lightsensor) {
486
554
  this.lightSensorService?.updateCharacteristic(this.platform.Characteristic.CurrentAmbientLightLevel, e);
487
555
  }
488
- if (this.device.ble) {
556
+ if (this.BLE) {
489
557
  this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
490
558
  this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
491
559
  }
492
- //throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
560
+ }
561
+
562
+ FirmwareRevision(accessory: PlatformAccessory<Context>, device: device & devicesConfig): CharacteristicValue {
563
+ let FirmwareRevision: string;
564
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
565
+ + ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
566
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
567
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
568
+ if (accessory.context.FirmwareRevision) {
569
+ FirmwareRevision = accessory.context.FirmwareRevision;
570
+ } else if (device.firmware) {
571
+ FirmwareRevision = device.firmware;
572
+ } else {
573
+ FirmwareRevision = this.platform.version;
574
+ }
575
+ return FirmwareRevision;
576
+ }
577
+
578
+ async context() {
579
+ if (this.MotionDetected === undefined) {
580
+ this.MotionDetected = false;
581
+ } else {
582
+ this.MotionDetected = this.accessory.context.MotionDetected;
583
+ }
584
+ if (this.ContactSensorState === undefined) {
585
+ this.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED;
586
+ } else {
587
+ this.ContactSensorState = this.accessory.context.ContactSensorState;
588
+ }
589
+ }
590
+
591
+ async refreshRate(device: device & devicesConfig): Promise<void> {
592
+ if (device.refreshRate) {
593
+ this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
594
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
595
+ } else if (this.platform.config.options!.refreshRate) {
596
+ this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options!.refreshRate;
597
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
598
+ }
493
599
  }
494
600
 
495
601
  async config(device: device & devicesConfig): Promise<void> {
@@ -497,8 +603,11 @@ export class Contact {
497
603
  if (device.contact) {
498
604
  config = device.contact;
499
605
  }
500
- if (device.ble) {
501
- config['ble'] = device.ble;
606
+ if (device.connectionType !== undefined) {
607
+ config['connectionType'] = device.connectionType;
608
+ }
609
+ if (device.external !== undefined) {
610
+ config['external'] = device.external;
502
611
  }
503
612
  if (device.logging !== undefined) {
504
613
  config['logging'] = device.logging;
@@ -510,81 +619,24 @@ export class Contact {
510
619
  config['scanDuration'] = device.scanDuration;
511
620
  }
512
621
  if (Object.entries(config).length !== 0) {
513
- this.infoLog(`Contact Sensor: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
514
- }
515
- }
516
-
517
- async refreshRate(device: device & devicesConfig): Promise<void> {
518
- if (device.refreshRate) {
519
- this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
520
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
521
- } else if (this.platform.config.options!.refreshRate) {
522
- this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options!.refreshRate;
523
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
524
- }
525
- }
526
-
527
- async scan(device: device & devicesConfig): Promise<void> {
528
- if (device.scanDuration) {
529
- this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
530
- if (device.ble) {
531
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
532
- }
533
- } else {
534
- this.scanDuration = this.accessory.context.scanDuration = 1;
535
- if (this.device.ble) {
536
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
537
- }
622
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${superStringify(config)}`);
538
623
  }
539
624
  }
540
625
 
541
626
  async logs(device: device & devicesConfig): Promise<void> {
542
627
  if (this.platform.debugMode) {
543
628
  this.deviceLogging = this.accessory.context.logging = 'debugMode';
544
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
629
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
545
630
  } else if (device.logging) {
546
631
  this.deviceLogging = this.accessory.context.logging = device.logging;
547
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
632
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
548
633
  } else if (this.platform.config.options?.logging) {
549
634
  this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
550
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
635
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
551
636
  } else {
552
637
  this.deviceLogging = this.accessory.context.logging = 'standard';
553
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
554
- }
555
- }
556
-
557
- FirmwareRevision(accessory: PlatformAccessory<Context>, device: device & devicesConfig): CharacteristicValue {
558
- let FirmwareRevision: string;
559
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
560
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
561
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
562
- if (accessory.context.FirmwareRevision) {
563
- FirmwareRevision = accessory.context.FirmwareRevision;
564
- } else if (device.firmware) {
565
- FirmwareRevision = device.firmware;
566
- } else {
567
- FirmwareRevision = this.platform.version;
638
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
568
639
  }
569
- return FirmwareRevision;
570
- }
571
-
572
- minLux(): number {
573
- if (this.device.contact?.set_minLux) {
574
- this.set_minLux = this.device.contact!.set_minLux!;
575
- } else {
576
- this.set_minLux = 1;
577
- }
578
- return this.set_minLux;
579
- }
580
-
581
- maxLux(): number {
582
- if (this.device.contact?.set_maxLux) {
583
- this.set_maxLux = this.device.contact!.set_maxLux!;
584
- } else {
585
- this.set_maxLux = 6001;
586
- }
587
- return this.set_maxLux;
588
640
  }
589
641
 
590
642
  /**
@@ -602,12 +654,28 @@ export class Contact {
602
654
  }
603
655
  }
604
656
 
657
+ debugWarnLog(...log: any[]): void {
658
+ if (this.enablingDeviceLogging()) {
659
+ if (this.deviceLogging?.includes('debug')) {
660
+ this.platform.log.warn('[DEBUG]', String(...log));
661
+ }
662
+ }
663
+ }
664
+
605
665
  errorLog(...log: any[]): void {
606
666
  if (this.enablingDeviceLogging()) {
607
667
  this.platform.log.error(String(...log));
608
668
  }
609
669
  }
610
670
 
671
+ debugErrorLog(...log: any[]): void {
672
+ if (this.enablingDeviceLogging()) {
673
+ if (this.deviceLogging?.includes('debug')) {
674
+ this.platform.log.error('[DEBUG]', String(...log));
675
+ }
676
+ }
677
+ }
678
+
611
679
  debugLog(...log: any[]): void {
612
680
  if (this.enablingDeviceLogging()) {
613
681
  if (this.deviceLogging === 'debug') {