@switchbot/homebridge-switchbot 3.5.0-beta.22 → 3.5.0-beta.24

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 (150) hide show
  1. package/config.schema.json +23 -2
  2. package/dist/device/blindtilt.d.ts +21 -98
  3. package/dist/device/blindtilt.d.ts.map +1 -1
  4. package/dist/device/blindtilt.js +332 -785
  5. package/dist/device/blindtilt.js.map +1 -1
  6. package/dist/device/bot.d.ts +25 -75
  7. package/dist/device/bot.d.ts.map +1 -1
  8. package/dist/device/bot.js +566 -778
  9. package/dist/device/bot.js.map +1 -1
  10. package/dist/device/ceilinglight.d.ts +13 -71
  11. package/dist/device/ceilinglight.d.ts.map +1 -1
  12. package/dist/device/ceilinglight.js +168 -606
  13. package/dist/device/ceilinglight.js.map +1 -1
  14. package/dist/device/colorbulb.d.ts +13 -78
  15. package/dist/device/colorbulb.d.ts.map +1 -1
  16. package/dist/device/colorbulb.js +218 -655
  17. package/dist/device/colorbulb.js.map +1 -1
  18. package/dist/device/contact.d.ts +14 -69
  19. package/dist/device/contact.d.ts.map +1 -1
  20. package/dist/device/contact.js +196 -553
  21. package/dist/device/contact.js.map +1 -1
  22. package/dist/device/curtain.d.ts +17 -90
  23. package/dist/device/curtain.d.ts.map +1 -1
  24. package/dist/device/curtain.js +324 -810
  25. package/dist/device/curtain.js.map +1 -1
  26. package/dist/device/device.d.ts +58 -0
  27. package/dist/device/device.d.ts.map +1 -0
  28. package/dist/device/device.js +629 -0
  29. package/dist/device/device.js.map +1 -0
  30. package/dist/device/hub.d.ts +15 -63
  31. package/dist/device/hub.d.ts.map +1 -1
  32. package/dist/device/hub.js +223 -610
  33. package/dist/device/hub.js.map +1 -1
  34. package/dist/device/humidifier.d.ts +10 -69
  35. package/dist/device/humidifier.d.ts.map +1 -1
  36. package/dist/device/humidifier.js +192 -595
  37. package/dist/device/humidifier.js.map +1 -1
  38. package/dist/device/iosensor.d.ts +11 -68
  39. package/dist/device/iosensor.d.ts.map +1 -1
  40. package/dist/device/iosensor.js +156 -599
  41. package/dist/device/iosensor.js.map +1 -1
  42. package/dist/device/lightstrip.d.ts +10 -72
  43. package/dist/device/lightstrip.d.ts.map +1 -1
  44. package/dist/device/lightstrip.js +204 -591
  45. package/dist/device/lightstrip.js.map +1 -1
  46. package/dist/device/lock.d.ts +15 -66
  47. package/dist/device/lock.d.ts.map +1 -1
  48. package/dist/device/lock.js +199 -566
  49. package/dist/device/lock.js.map +1 -1
  50. package/dist/device/meter.d.ts +13 -67
  51. package/dist/device/meter.d.ts.map +1 -1
  52. package/dist/device/meter.js +152 -592
  53. package/dist/device/meter.js.map +1 -1
  54. package/dist/device/meterplus.d.ts +13 -68
  55. package/dist/device/meterplus.d.ts.map +1 -1
  56. package/dist/device/meterplus.js +151 -599
  57. package/dist/device/meterplus.js.map +1 -1
  58. package/dist/device/motion.d.ts +13 -73
  59. package/dist/device/motion.d.ts.map +1 -1
  60. package/dist/device/motion.js +129 -540
  61. package/dist/device/motion.js.map +1 -1
  62. package/dist/device/plug.d.ts +9 -61
  63. package/dist/device/plug.d.ts.map +1 -1
  64. package/dist/device/plug.js +71 -476
  65. package/dist/device/plug.js.map +1 -1
  66. package/dist/device/robotvacuumcleaner.d.ts +11 -66
  67. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  68. package/dist/device/robotvacuumcleaner.js +122 -471
  69. package/dist/device/robotvacuumcleaner.js.map +1 -1
  70. package/dist/device/waterdetector.d.ts +9 -66
  71. package/dist/device/waterdetector.d.ts.map +1 -1
  72. package/dist/device/waterdetector.js +105 -478
  73. package/dist/device/waterdetector.js.map +1 -1
  74. package/dist/irdevice/airconditioner.d.ts +5 -36
  75. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  76. package/dist/irdevice/airconditioner.js +228 -337
  77. package/dist/irdevice/airconditioner.js.map +1 -1
  78. package/dist/irdevice/airpurifier.d.ts +5 -34
  79. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  80. package/dist/irdevice/airpurifier.js +19 -267
  81. package/dist/irdevice/airpurifier.js.map +1 -1
  82. package/dist/irdevice/camera.d.ts +5 -34
  83. package/dist/irdevice/camera.d.ts.map +1 -1
  84. package/dist/irdevice/camera.js +13 -261
  85. package/dist/irdevice/camera.js.map +1 -1
  86. package/dist/irdevice/fan.d.ts +5 -34
  87. package/dist/irdevice/fan.d.ts.map +1 -1
  88. package/dist/irdevice/fan.js +13 -260
  89. package/dist/irdevice/fan.js.map +1 -1
  90. package/dist/irdevice/irdevice.d.ts +39 -0
  91. package/dist/irdevice/irdevice.d.ts.map +1 -0
  92. package/dist/irdevice/irdevice.js +502 -0
  93. package/dist/irdevice/irdevice.js.map +1 -0
  94. package/dist/irdevice/light.d.ts +5 -34
  95. package/dist/irdevice/light.d.ts.map +1 -1
  96. package/dist/irdevice/light.js +13 -267
  97. package/dist/irdevice/light.js.map +1 -1
  98. package/dist/irdevice/other.d.ts +18 -48
  99. package/dist/irdevice/other.d.ts.map +1 -1
  100. package/dist/irdevice/other.js +349 -551
  101. package/dist/irdevice/other.js.map +1 -1
  102. package/dist/irdevice/tv.d.ts +7 -40
  103. package/dist/irdevice/tv.d.ts.map +1 -1
  104. package/dist/irdevice/tv.js +79 -325
  105. package/dist/irdevice/tv.js.map +1 -1
  106. package/dist/irdevice/vacuumcleaner.d.ts +6 -36
  107. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  108. package/dist/irdevice/vacuumcleaner.js +38 -288
  109. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  110. package/dist/irdevice/waterheater.d.ts +6 -36
  111. package/dist/irdevice/waterheater.d.ts.map +1 -1
  112. package/dist/irdevice/waterheater.js +39 -291
  113. package/dist/irdevice/waterheater.js.map +1 -1
  114. package/dist/settings.d.ts +15 -2
  115. package/dist/settings.d.ts.map +1 -1
  116. package/dist/utils.d.ts +68 -0
  117. package/dist/utils.d.ts.map +1 -1
  118. package/dist/utils.js +71 -0
  119. package/dist/utils.js.map +1 -1
  120. package/package.json +8 -8
  121. package/src/device/blindtilt.ts +356 -809
  122. package/src/device/bot.ts +601 -865
  123. package/src/device/ceilinglight.ts +182 -628
  124. package/src/device/colorbulb.ts +235 -683
  125. package/src/device/contact.ts +220 -575
  126. package/src/device/curtain.ts +344 -836
  127. package/src/device/device.ts +655 -0
  128. package/src/device/hub.ts +246 -636
  129. package/src/device/humidifier.ts +224 -645
  130. package/src/device/iosensor.ts +181 -625
  131. package/src/device/lightstrip.ts +230 -622
  132. package/src/device/lock.ts +236 -607
  133. package/src/device/meter.ts +177 -616
  134. package/src/device/meterplus.ts +175 -624
  135. package/src/device/motion.ts +147 -563
  136. package/src/device/plug.ts +81 -505
  137. package/src/device/robotvacuumcleaner.ts +139 -494
  138. package/src/device/waterdetector.ts +126 -506
  139. package/src/irdevice/airconditioner.ts +228 -353
  140. package/src/irdevice/airpurifier.ts +23 -282
  141. package/src/irdevice/camera.ts +18 -277
  142. package/src/irdevice/fan.ts +18 -275
  143. package/src/irdevice/irdevice.ts +518 -0
  144. package/src/irdevice/light.ts +18 -283
  145. package/src/irdevice/other.ts +394 -621
  146. package/src/irdevice/tv.ts +97 -344
  147. package/src/irdevice/vacuumcleaner.ts +46 -310
  148. package/src/irdevice/waterheater.ts +47 -315
  149. package/src/settings.ts +20 -3
  150. package/src/utils.ts +71 -0
@@ -1,154 +1,98 @@
1
1
  import { request } from 'undici';
2
- import { sleep } from '../utils.js';
2
+ import { deviceBase } from './device.js';
3
3
  import { interval, Subject } from 'rxjs';
4
4
  import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
5
5
  import { Devices } from '../settings.js';
6
- export class Lock {
6
+ export class Lock extends deviceBase {
7
7
  platform;
8
- accessory;
9
- device;
10
- api;
11
- log;
12
- config;
13
- hap;
14
8
  // Services
15
- lockService;
16
- batteryService;
17
- contactSensorService;
18
- latchButtonService;
19
- // Characteristic Values
20
- BatteryLevel;
21
- LockTargetState;
22
- LockCurrentState;
23
- FirmwareRevision;
24
- StatusLowBattery;
25
- ContactSensorState;
26
- // OpenAPI Status
27
- OpenAPI_BatteryLevel;
28
- OpenAPI_FirmwareRevision;
29
- OpenAPI_LockCurrentState;
30
- OpenAPI_ContactSensorState;
31
- // BLE Status
32
- BLE_BatteryLevel;
33
- BLE_LockCurrentState;
34
- BLE_Calibration;
35
- BLE_ContactSensorState;
36
- // BLE Others
37
- BLE_IsConnected;
38
- // Config
39
- scanDuration;
40
- deviceLogging;
41
- deviceRefreshRate;
42
- maxRetries;
43
- delayBetweenRetries;
9
+ LockMechanism;
10
+ Battery;
11
+ ContactSensor;
12
+ Switch;
44
13
  // Updates
45
14
  lockUpdateInProgress;
46
15
  doLockUpdate;
47
- // Connection
48
- OpenAPI;
49
- BLE;
50
16
  constructor(platform, accessory, device) {
17
+ super(platform, accessory, device);
51
18
  this.platform = platform;
52
- this.accessory = accessory;
53
- this.device = device;
54
- this.api = this.platform.api;
55
- this.log = this.platform.log;
56
- this.config = this.platform.config;
57
- this.hap = this.api.hap;
58
- // Connection
59
- this.BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
60
- this.OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
61
- // default placeholders
62
- this.deviceLogs(device);
63
- this.scan(device);
64
- this.refreshRate(device);
65
- this.deviceContext();
66
- this.deviceRetry(device);
67
- this.deviceConfig(device);
68
19
  // this is subject we use to track when we need to POST changes to the SwitchBot API
69
20
  this.doLockUpdate = new Subject();
70
21
  this.lockUpdateInProgress = false;
71
22
  // Retrieve initial values and updateHomekit
72
23
  this.refreshStatus();
73
- // set accessory information
74
- accessory
75
- .getService(this.hap.Service.AccessoryInformation)
76
- .setCharacteristic(this.hap.Characteristic.Manufacturer, 'SwitchBot')
77
- .setCharacteristic(this.hap.Characteristic.Model, 'W1601700')
78
- .setCharacteristic(this.hap.Characteristic.SerialNumber, device.deviceId)
79
- .setCharacteristic(this.hap.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
24
+ // Initialize LockMechanism property
25
+ this.LockMechanism = {
26
+ Service: accessory.addService(this.hap.Service.LockMechanism),
27
+ LockTargetState: accessory.context.LockTargetState || this.hap.Characteristic.LockTargetState.SECURED,
28
+ LockCurrentState: accessory.context.LockCurrentState || this.hap.Characteristic.LockCurrentState.SECURED,
29
+ };
30
+ // Initialize Battery property
31
+ this.Battery = {
32
+ Service: accessory.addService(this.hap.Service.Battery),
33
+ BatteryLevel: accessory.context.BatteryLevel || 100,
34
+ StatusLowBattery: accessory.context.StatusLowBattery || this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL,
35
+ };
36
+ // Initialize ContactSensor property
37
+ if (!this.device.lock?.hide_contactsensor) {
38
+ this.ContactSensor = {
39
+ Service: accessory.addService(this.hap.Service.ContactSensor),
40
+ ContactSensorState: accessory.context.ContactSensorState || this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED,
41
+ };
42
+ }
43
+ // Initialize Latch Button Service
44
+ if (device.lock?.activate_latchbutton) {
45
+ this.Switch = {
46
+ Service: accessory.addService(this.hap.Service.Switch),
47
+ On: accessory.context.On || false,
48
+ };
49
+ }
80
50
  // get the LockMechanism service if it exists, otherwise create a new LockMechanism service
81
51
  // you can create multiple services for each accessory
82
- const lockService = `${accessory.displayName} ${device.deviceType}`;
83
- (this.lockService = accessory.getService(this.hap.Service.LockMechanism)
84
- || accessory.addService(this.hap.Service.LockMechanism)), lockService;
85
- this.lockService.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
86
- if (!this.lockService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
87
- this.lockService.addCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.displayName);
88
- }
89
- // each service must implement at-minimum the "required characteristics" for the given service type
90
- // see https://developers.homebridge.io/#/service/LockMechanism
91
- // create handlers for required characteristics
92
- this.lockService.getCharacteristic(this.hap.Characteristic.LockTargetState).onSet(this.LockTargetStateSet.bind(this));
52
+ const LockMechanismService = `${accessory.displayName} ${device.deviceType}`;
53
+ (this.LockMechanism.Service = accessory.getService(this.hap.Service.LockMechanism)
54
+ || accessory.addService(this.hap.Service.LockMechanism)), LockMechanismService;
55
+ this.LockMechanism.Service.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
56
+ this.LockMechanism.Service.getCharacteristic(this.hap.Characteristic.LockTargetState).onSet(this.LockTargetStateSet.bind(this));
93
57
  // Latch Button Service
94
58
  if (device.lock?.activate_latchbutton === false) { // remove the service when this variable is false
95
59
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Latch Button Service`);
96
- this.latchButtonService = accessory.getService(this.hap.Service.Switch);
97
- if (this.latchButtonService) {
98
- accessory.removeService(this.latchButtonService);
99
- this.latchButtonService = undefined; // Reset the service variable to undefined
100
- }
60
+ this.Switch.Service = accessory.getService(this.hap.Service.Switch);
61
+ accessory.removeService(this.Switch.Service);
101
62
  }
102
- else if (!this.latchButtonService) {
63
+ else if (!this.Switch.Service && device.lock?.activate_latchbutton === true) {
103
64
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Adding Latch Button Service`);
104
65
  const latchServiceName = `${accessory.displayName} Latch`;
105
- this.latchButtonService = accessory.getService(this.hap.Service.Switch)
106
- || accessory.addService(this.hap.Service.Switch, latchServiceName, 'LatchButtonServiceIdentifier');
107
- this.latchButtonService.setCharacteristic(this.hap.Characteristic.Name, latchServiceName);
108
- if (!this.latchButtonService.testCharacteristic(this.hap.Characteristic.On)) {
109
- this.latchButtonService.addCharacteristic(this.hap.Characteristic.On);
110
- }
111
- this.latchButtonService.getCharacteristic(this.hap.Characteristic.On)
112
- .on('set', (value, callback) => {
113
- if (typeof value === 'boolean') {
114
- this.handleLatchCharacteristic(value, callback);
115
- }
116
- else {
117
- callback(new Error('Wrong characteristic value type'));
118
- }
119
- });
66
+ this.Switch.Service = accessory.getService(this.hap.Service.Switch)
67
+ || accessory.addService(this.hap.Service.Switch, latchServiceName, 'Switch.ServiceIdentifier');
68
+ this.Switch.Service.setCharacteristic(this.hap.Characteristic.Name, latchServiceName);
69
+ this.Switch.Service.getCharacteristic(this.hap.Characteristic.On).onSet(this.OnSet.bind(this));
120
70
  }
121
71
  else {
122
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Latch Button Service already exists`);
72
+ this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Latch Button Service Not Added`);
123
73
  }
124
74
  // Contact Sensor Service
125
75
  if (device.lock?.hide_contactsensor) {
126
76
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Contact Sensor Service`);
127
- this.contactSensorService = this.accessory.getService(this.hap.Service.ContactSensor);
128
- accessory.removeService(this.contactSensorService);
77
+ this.ContactSensor.Service = this.accessory.getService(this.hap.Service.ContactSensor);
78
+ accessory.removeService(this.ContactSensor.Service);
129
79
  }
130
- else if (!this.contactSensorService) {
80
+ else if (!this.ContactSensor.Service) {
131
81
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Contact Sensor Service`);
132
- const contactSensorService = `${accessory.displayName} Contact Sensor`;
133
- (this.contactSensorService = this.accessory.getService(this.hap.Service.ContactSensor)
134
- || this.accessory.addService(this.hap.Service.ContactSensor)), contactSensorService;
135
- this.contactSensorService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Contact Sensor`);
136
- if (!this.contactSensorService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
137
- this.contactSensorService.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Contact Sensor`);
138
- }
82
+ const ContactSensorService = `${accessory.displayName} Contact Sensor`;
83
+ (this.ContactSensor.Service = this.accessory.getService(this.hap.Service.ContactSensor)
84
+ || this.accessory.addService(this.hap.Service.ContactSensor)), ContactSensorService;
85
+ this.ContactSensor.Service.setCharacteristic(this.hap.Characteristic.Name, ContactSensorService);
139
86
  }
140
87
  else {
141
88
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Contact Sensor Service Not Added`);
142
89
  }
143
90
  // Battery Service
144
- const batteryService = `${accessory.displayName} Battery`;
145
- (this.batteryService = this.accessory.getService(this.hap.Service.Battery)
146
- || accessory.addService(this.hap.Service.Battery)), batteryService;
147
- this.batteryService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Battery`);
148
- if (!this.batteryService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
149
- this.batteryService.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
150
- }
151
- this.batteryService.setCharacteristic(this.hap.Characteristic.ChargingState, this.hap.Characteristic.ChargingState.NOT_CHARGEABLE);
91
+ const BatteryService = `${accessory.displayName} Battery`;
92
+ (this.Battery.Service = this.accessory.getService(this.hap.Service.Battery)
93
+ || accessory.addService(this.hap.Service.Battery)), BatteryService;
94
+ this.Battery.Service.setCharacteristic(this.hap.Characteristic.Name, BatteryService);
95
+ this.Battery.Service.setCharacteristic(this.hap.Characteristic.ChargingState, this.hap.Characteristic.ChargingState.NOT_CHARGEABLE);
152
96
  // Update Homekit
153
97
  this.updateHomeKitCharacteristics();
154
98
  // Start an update interval
@@ -164,12 +108,12 @@ export class Lock {
164
108
  try {
165
109
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
166
110
  const { lockState } = context;
167
- const { LockCurrentState } = this;
111
+ const { LockCurrentState } = this.LockMechanism;
168
112
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
169
113
  '(lockState) = ' +
170
114
  `Webhook:(${lockState}), ` +
171
115
  `current:(${LockCurrentState})`);
172
- this.LockCurrentState = lockState === 'LOCKED' ? 1 : 0;
116
+ this.LockMechanism.LockCurrentState = lockState === 'LOCKED' ? 1 : 0;
173
117
  this.updateHomeKitCharacteristics();
174
118
  }
175
119
  catch (e) {
@@ -196,123 +140,76 @@ export class Lock {
196
140
  this.lockUpdateInProgress = false;
197
141
  });
198
142
  }
199
- /**
200
- * Method for handling the LatchCharacteristic
201
- */
202
- async handleLatchCharacteristic(value, callback) {
203
- this.debugLog(`handleLatchCharacteristic called with value: ${value}`);
204
- if (value) {
205
- this.debugLog('Attempting to open the latch');
206
- this.openAPIpushChanges(value).then(() => {
207
- this.debugLog('Latch opened successfully');
208
- this.debugLog(`LatchButtonService is: ${this.latchButtonService ? 'available' : 'not available'}`);
209
- // simulate button press to turn the switch back off
210
- if (this.latchButtonService) {
211
- const latchButtonService = this.latchButtonService;
212
- // Simulate a button press by waiting a short period before turning the switch off
213
- setTimeout(() => {
214
- latchButtonService.getCharacteristic(this.hap.Characteristic.On).updateValue(false);
215
- this.debugLog('Latch button switched off automatically.');
216
- }, 500); // 500 ms delay
217
- }
218
- callback(null);
219
- }).catch((error) => {
220
- // Log the error if the operation failed
221
- this.debugLog(`Error opening latch: ${error}`);
222
- // Ensure we turn the switch back off even in case of an error
223
- if (this.latchButtonService) {
224
- this.latchButtonService.getCharacteristic(this.hap.Characteristic.On).updateValue(false);
225
- this.debugLog('Latch button switched off after an error.');
226
- }
227
- callback(error);
228
- });
229
- }
230
- else {
231
- this.debugLog('Switch is off, nothing to do');
232
- callback(null);
233
- }
234
- }
235
- /**
236
- * Parse the device status from the SwitchBot api
237
- */
238
- async parseStatus() {
239
- if (!this.device.enableCloudService && this.OpenAPI) {
240
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} parseStatus enableCloudService: ${this.device.enableCloudService}`);
241
- /* } else if (this.BLE) {
242
- await this.BLEparseStatus();*/
243
- }
244
- else if (this.OpenAPI && this.platform.config.credentials?.token) {
245
- await this.openAPIparseStatus();
246
- }
247
- else {
248
- await this.offlineOff();
249
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:` + ` ${this.device.connectionType}, parseStatus will not happen.`);
250
- }
251
- }
252
- async BLEparseStatus() {
143
+ async BLEparseStatus(serviceData) {
144
+ // BLE Status
253
145
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
254
- switch (this.BLE_LockCurrentState) {
146
+ switch (serviceData.status) {
255
147
  case 'locked':
256
- this.LockCurrentState = this.hap.Characteristic.LockCurrentState.SECURED;
257
- this.LockTargetState = this.hap.Characteristic.LockTargetState.SECURED;
148
+ this.LockMechanism.LockCurrentState = this.hap.Characteristic.LockCurrentState.SECURED;
149
+ this.LockMechanism.LockTargetState = this.hap.Characteristic.LockTargetState.SECURED;
258
150
  break;
259
151
  default:
260
- this.LockCurrentState = this.hap.Characteristic.LockCurrentState.UNSECURED;
261
- this.LockTargetState = this.hap.Characteristic.LockTargetState.UNSECURED;
152
+ this.LockMechanism.LockCurrentState = this.hap.Characteristic.LockCurrentState.UNSECURED;
153
+ this.LockMechanism.LockTargetState = this.hap.Characteristic.LockTargetState.UNSECURED;
262
154
  }
263
- switch (this.BLE_ContactSensorState) {
264
- case 'opened':
265
- this.ContactSensorState = this.hap.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED;
266
- break;
267
- default:
268
- this.ContactSensorState = this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED;
155
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
156
+ + ` LockTargetState: ${this.LockMechanism.LockTargetState}, LockCurrentState: ${this.LockMechanism.LockCurrentState}`);
157
+ // Contact Sensor
158
+ if (!this.device.lock?.hide_contactsensor) {
159
+ switch (serviceData.door_open) {
160
+ case 'opened':
161
+ this.ContactSensor.ContactSensorState = this.hap.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED;
162
+ break;
163
+ default:
164
+ this.ContactSensor.ContactSensorState = this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED;
165
+ }
166
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensor.ContactSensorState}`);
269
167
  }
270
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LockTargetState}`);
271
168
  // Battery
272
- this.BatteryLevel = Number(this.BLE_BatteryLevel);
273
- if (this.BatteryLevel < 10) {
274
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
169
+ this.Battery.BatteryLevel = Number(serviceData.battery);
170
+ if (this.Battery.BatteryLevel < 10) {
171
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
275
172
  }
276
173
  else {
277
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
174
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
278
175
  }
279
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel},` + ` StatusLowBattery: ${this.StatusLowBattery}`);
176
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
177
+ + ` BatteryLevel: ${this.Battery.BatteryLevel}, StatusLowBattery: ${this.Battery.StatusLowBattery}`);
280
178
  }
281
- async openAPIparseStatus() {
179
+ async openAPIparseStatus(deviceStatus) {
282
180
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
283
- switch (this.OpenAPI_LockCurrentState) {
181
+ switch (deviceStatus.body.lockState) {
284
182
  case 'locked':
285
- this.LockCurrentState = this.hap.Characteristic.LockCurrentState.SECURED;
286
- this.LockTargetState = this.hap.Characteristic.LockTargetState.SECURED;
183
+ this.LockMechanism.LockCurrentState = this.hap.Characteristic.LockCurrentState.SECURED;
184
+ this.LockMechanism.LockTargetState = this.hap.Characteristic.LockTargetState.SECURED;
287
185
  break;
288
186
  default:
289
- this.LockCurrentState = this.hap.Characteristic.LockCurrentState.UNSECURED;
290
- this.LockTargetState = this.hap.Characteristic.LockTargetState.UNSECURED;
187
+ this.LockMechanism.LockCurrentState = this.hap.Characteristic.LockCurrentState.UNSECURED;
188
+ this.LockMechanism.LockTargetState = this.hap.Characteristic.LockTargetState.UNSECURED;
291
189
  }
292
- switch (this.OpenAPI_ContactSensorState) {
190
+ switch (deviceStatus.body.doorState) {
293
191
  case 'opened':
294
- this.ContactSensorState = this.hap.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED;
192
+ this.ContactSensor.ContactSensorState = this.hap.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED;
295
193
  break;
296
194
  default:
297
- this.ContactSensorState = this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED;
195
+ this.ContactSensor.ContactSensorState = this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED;
298
196
  }
299
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LockTargetState}`);
197
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LockMechanism.LockTargetState}`);
300
198
  // Battery
301
- this.BatteryLevel = Number(this.OpenAPI_BatteryLevel);
302
- if (this.BatteryLevel < 10) {
303
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
199
+ this.Battery.BatteryLevel = Number(deviceStatus.body.battery);
200
+ if (this.Battery.BatteryLevel < 10) {
201
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
304
202
  }
305
203
  else {
306
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
204
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
307
205
  }
308
- if (Number.isNaN(this.BatteryLevel)) {
309
- this.BatteryLevel = 100;
206
+ if (Number.isNaN(this.Battery.BatteryLevel)) {
207
+ this.Battery.BatteryLevel = 100;
310
208
  }
311
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel},`
312
- + ` StatusLowBattery: ${this.StatusLowBattery}`);
209
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel},`
210
+ + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
313
211
  // FirmwareRevision
314
- this.FirmwareRevision = this.OpenAPI_FirmwareRevision;
315
- this.accessory.context.FirmwareRevision = this.FirmwareRevision;
212
+ this.accessory.context.FirmwareRevision = deviceStatus.body.version;
316
213
  }
317
214
  /**
318
215
  * Asks the SwitchBot API for the latest device information
@@ -346,91 +243,34 @@ export class Lock {
346
243
  // Start to monitor advertisement packets
347
244
  (async () => {
348
245
  // Start to monitor advertisement packets
349
- await switchbot.startScan({
350
- model: 'o',
351
- id: this.device.bleMac,
352
- });
246
+ await switchbot.startScan({ model: this.device.bleModel, id: this.device.bleMac });
353
247
  // Set an event handler
354
248
  switchbot.onadvertisement = (ad) => {
355
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
356
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.serviceData.model}`);
357
- if (this.device.bleMac === ad.address && ad.serviceData.model === 'o') {
249
+ if (this.device.bleMac === ad.address && ad.model === this.device.bleModel) {
250
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
251
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.model}`);
358
252
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
359
- this.BLE_BatteryLevel = ad.serviceData.battery;
360
- this.BLE_Calibration = ad.serviceData.calibration;
361
- this.BLE_LockCurrentState = ad.serviceData.status;
362
- this.BLE_ContactSensorState = ad.serviceData.door_open;
363
253
  }
364
254
  else {
365
255
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
366
256
  }
367
257
  };
368
- // Wait 1 seconds
258
+ // Wait 10 seconds
369
259
  await switchbot.wait(this.scanDuration * 1000);
370
260
  // Stop to monitor
371
261
  await switchbot.stopScan();
372
262
  // Update HomeKit
373
- await this.BLEparseStatus();
263
+ await this.BLEparseStatus(switchbot.onadvertisement.serviceData);
374
264
  await this.updateHomeKitCharacteristics();
375
265
  })();
376
- /*if (switchbot !== false) {
377
- switchbot
378
- .startScan({
379
- model: 'o',
380
- id: this.device.bleMac,
381
- })
382
- .then(async () => {
383
- // Set an event handler
384
- switchbot.onadvertisement = async (ad: ad) => {
385
- this.debugLog(
386
- `${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},` +
387
- ` BLE Address Found: ${ad.address}`,
388
- );
389
- this.BLE_BatteryLevel = ad.serviceData.battery;
390
- this.BLE_Calibration = ad.serviceData.calibration;
391
- this.BLE_LockCurrentState = ad.serviceData.status;
392
- this.BLE_ContactSensorState = ad.serviceData.door_open;
393
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
394
- this.debugLog(
395
- `${this.device.deviceType}: ${this.accessory.displayName} battery: ${ad.serviceData.battery}, ` +
396
- `calibration: ${ad.serviceData.calibration}, status: ${ad.serviceData.status}, battery: ${ad.serviceData.battery}, ` +
397
- `door_open: ${ad.serviceData.door_open}`,
398
- );
399
-
400
- if (ad.serviceData) {
401
- this.BLE_IsConnected = true;
402
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.BLE_IsConnected}`);
403
- await this.stopScanning(switchbot);
404
- } else {
405
- this.BLE_IsConnected = false;
406
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.BLE_IsConnected}`);
407
- }
408
- };
409
- // Wait
410
- return await sleep(this.scanDuration * 1000);
411
- })
412
- .then(async () => {
413
- // Stop to monitor
414
- await this.stopScanning(switchbot);
415
- })
416
- .catch(async (e: any) => {
417
- this.apiError(e);
418
- this.errorLog(
419
- `${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}` +
420
- ` Connection, Error Message: ${JSON.stringify(e.message)}`,
421
- );
422
- await this.BLERefreshConnection(switchbot);
423
- });
424
- } else {
425
- await this.BLERefreshConnection(switchbot);
426
- }*/
266
+ if (switchbot === undefined) {
267
+ await this.BLERefreshConnection(switchbot);
268
+ }
427
269
  }
428
270
  async openAPIRefreshStatus() {
429
271
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
430
272
  try {
431
- const { body, statusCode } = await this.platform.retryRequest(this.maxRetries, this.delayBetweenRetries, `${Devices}/${this.device.deviceId}/status`, {
432
- headers: this.platform.generateHeaders(),
433
- });
273
+ const { body, statusCode } = await this.platform.retryRequest(this.deviceMaxRetries, this.deviceDelayBetweenRetries, `${Devices}/${this.device.deviceId}/status`, { headers: this.platform.generateHeaders() });
434
274
  this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
435
275
  const deviceStatus = await body.json();
436
276
  this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
@@ -438,11 +278,7 @@ export class Lock {
438
278
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
439
279
  this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
440
280
  + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
441
- this.OpenAPI_LockCurrentState = deviceStatus.body.lockState;
442
- this.OpenAPI_ContactSensorState = deviceStatus.body.doorState;
443
- this.OpenAPI_BatteryLevel = deviceStatus.body.battery;
444
- this.OpenAPI_FirmwareRevision = deviceStatus.body.version;
445
- this.openAPIparseStatus();
281
+ this.openAPIparseStatus(deviceStatus);
446
282
  this.updateHomeKitCharacteristics();
447
283
  }
448
284
  else {
@@ -485,8 +321,8 @@ export class Lock {
485
321
  }
486
322
  async BLEpushChanges() {
487
323
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`);
488
- if (this.LockTargetState !== this.accessory.context.LockTargetState) {
489
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges LockTargetState: ${this.LockTargetState}` +
324
+ if (this.LockMechanism.LockTargetState !== this.accessory.context.LockTargetState) {
325
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges LockTargetState: ${this.LockMechanism.LockTargetState}` +
490
326
  ` LockTargetStateCached: ${this.accessory.context.LockTargetState}`);
491
327
  const switchbot = await this.platform.connectBLE();
492
328
  // Convert to BLE Address
@@ -503,8 +339,8 @@ export class Lock {
503
339
  .then(() => {
504
340
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
505
341
  this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
506
- + `LockTargetState: ${this.LockTargetState} sent over BLE, sent successfully`);
507
- this.LockTargetState = this.hap.Characteristic.LockTargetState.SECURED;
342
+ + `LockTargetState: ${this.LockMechanism.LockTargetState} sent over BLE, sent successfully`);
343
+ this.LockMechanism.LockTargetState = this.hap.Characteristic.LockTargetState.SECURED;
508
344
  })
509
345
  .catch(async (e) => {
510
346
  this.apiError(e);
@@ -515,20 +351,20 @@ export class Lock {
515
351
  }
516
352
  else {
517
353
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges.` +
518
- `LockTargetState: ${this.LockTargetState}, ` +
354
+ `LockTargetState: ${this.LockMechanism.LockTargetState}, ` +
519
355
  `LockTargetStateCached: ${this.accessory.context.LockTargetState}`);
520
356
  }
521
357
  }
522
358
  async openAPIpushChanges(LatchUnlock) {
523
359
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
524
- if ((this.LockTargetState !== this.accessory.context.LockTargetState) || LatchUnlock) {
360
+ if ((this.LockMechanism.LockTargetState !== this.accessory.context.LockTargetState) || LatchUnlock) {
525
361
  // Determine the command based on the LockTargetState or the forceUnlock parameter
526
362
  let command = '';
527
363
  if (LatchUnlock) {
528
364
  command = 'unlock';
529
365
  }
530
366
  else {
531
- command = this.LockTargetState ? 'lock' : 'unlock';
367
+ command = this.LockMechanism.LockTargetState ? 'lock' : 'unlock';
532
368
  }
533
369
  const bodyChange = JSON.stringify({
534
370
  command: `${command}`,
@@ -566,7 +402,7 @@ export class Lock {
566
402
  }
567
403
  else {
568
404
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.` +
569
- `LockTargetState: ${this.LockTargetState}, ` +
405
+ `LockTargetState: ${this.LockMechanism.LockTargetState}, ` +
570
406
  `LockTargetStateCached: ${this.accessory.context.LockTargetState}`);
571
407
  }
572
408
  }
@@ -574,84 +410,98 @@ export class Lock {
574
410
  * Handle requests to set the value of the "On" characteristic
575
411
  */
576
412
  async LockTargetStateSet(value) {
577
- if (this.LockTargetState === this.accessory.context.LockTargetState) {
413
+ if (this.LockMechanism.LockTargetState === this.accessory.context.LockTargetState) {
578
414
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set LockTargetState: ${value}`);
579
415
  }
580
416
  else {
581
417
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set LockTargetState: ${value}`);
582
418
  }
583
- this.LockTargetState = value;
419
+ this.LockMechanism.LockTargetState = value;
420
+ this.doLockUpdate.next();
421
+ }
422
+ /**
423
+ * Handle requests to set the value of the "On" characteristic
424
+ */
425
+ async OnSet(value) {
426
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Latch Button Set On: ${value}`);
427
+ if (value) {
428
+ this.debugLog('Attempting to open the latch');
429
+ this.openAPIpushChanges(value).then(() => {
430
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Latch opened successfully`);
431
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
432
+ + ` SwitchService is: ${this.Switch.Service ? 'available' : 'not available'}`);
433
+ // simulate button press to turn the switch back off
434
+ if (this.Switch.Service) {
435
+ const SwitchService = this.Switch.Service;
436
+ // Simulate a button press by waiting a short period before turning the switch off
437
+ setTimeout(() => {
438
+ SwitchService.getCharacteristic(this.hap.Characteristic.On).updateValue(false);
439
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Latch button switched off automatically.`);
440
+ }, 500); // 500 ms delay
441
+ }
442
+ }).catch((e) => {
443
+ // Log the error if the operation failed
444
+ this.debugLog(`Error opening latch: ${e}`);
445
+ // Ensure we turn the switch back off even in case of an error
446
+ if (this.Switch.Service) {
447
+ this.Switch.Service.getCharacteristic(this.hap.Characteristic.On).updateValue(false);
448
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Latch button switched off after an error.`);
449
+ }
450
+ });
451
+ }
452
+ else {
453
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Switch is off, nothing to do`);
454
+ }
455
+ this.Switch.On = value;
584
456
  this.doLockUpdate.next();
585
457
  }
586
458
  async updateHomeKitCharacteristics() {
587
459
  if (!this.device.lock?.hide_contactsensor) {
588
- if (this.ContactSensorState === undefined) {
589
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensorState}`);
460
+ if (this.ContactSensor.ContactSensorState === undefined) {
461
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensor.ContactSensorState}`);
590
462
  }
591
463
  else {
592
- this.accessory.context.ContactSensorState = this.ContactSensorState;
593
- this.contactSensorService?.updateCharacteristic(this.hap.Characteristic.ContactSensorState, this.ContactSensorState);
594
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic ContactSensorState: ${this.ContactSensorState}`);
464
+ this.accessory.context.ContactSensorState = this.ContactSensor.ContactSensorState;
465
+ this.ContactSensor.Service.updateCharacteristic(this.hap.Characteristic.ContactSensorState, this.ContactSensor.ContactSensorState);
466
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
467
+ + ` ContactSensorState: ${this.ContactSensor.ContactSensorState}`);
595
468
  }
596
469
  }
597
- if (this.LockTargetState === undefined) {
598
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LockTargetState: ${this.LockTargetState}`);
599
- }
600
- else {
601
- this.accessory.context.LockTargetState = this.LockTargetState;
602
- this.lockService.updateCharacteristic(this.hap.Characteristic.LockTargetState, this.LockTargetState);
603
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic LockTargetState: ${this.LockTargetState}`);
604
- }
605
- if (this.LockCurrentState === undefined) {
606
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LockCurrentState: ${this.LockCurrentState}`);
470
+ if (this.LockMechanism.LockTargetState === undefined) {
471
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LockTargetState: ${this.LockMechanism.LockTargetState}`);
607
472
  }
608
473
  else {
609
- this.accessory.context.LockCurrentState = this.LockCurrentState;
610
- this.lockService.updateCharacteristic(this.hap.Characteristic.LockCurrentState, this.LockCurrentState);
611
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic LockCurrentState: ${this.LockCurrentState}`);
474
+ this.accessory.context.LockTargetState = this.LockMechanism.LockTargetState;
475
+ this.LockMechanism.Service.updateCharacteristic(this.hap.Characteristic.LockTargetState, this.LockMechanism.LockTargetState);
476
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
477
+ + ` LockTargetState: ${this.LockMechanism.LockTargetState}`);
612
478
  }
613
- if (this.BatteryLevel === undefined) {
614
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
479
+ if (this.LockMechanism.LockCurrentState === undefined) {
480
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LockCurrentState: ${this.LockMechanism.LockCurrentState}`);
615
481
  }
616
482
  else {
617
- this.accessory.context.BatteryLevel = this.BatteryLevel;
618
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.BatteryLevel);
619
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
483
+ this.accessory.context.LockCurrentState = this.LockMechanism.LockCurrentState;
484
+ this.LockMechanism.Service.updateCharacteristic(this.hap.Characteristic.LockCurrentState, this.LockMechanism.LockCurrentState);
485
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
486
+ + ` LockCurrentState: ${this.LockMechanism.LockCurrentState}`);
620
487
  }
621
- if (this.StatusLowBattery === undefined) {
622
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
488
+ if (this.Battery.BatteryLevel === undefined) {
489
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}`);
623
490
  }
624
491
  else {
625
- this.accessory.context.StatusLowBattery = this.StatusLowBattery;
626
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.StatusLowBattery);
627
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
492
+ this.accessory.context.BatteryLevel = this.Battery.BatteryLevel;
493
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.Battery.BatteryLevel);
494
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
495
+ + ` BatteryLevel: ${this.Battery.BatteryLevel}`);
628
496
  }
629
- }
630
- async stopScanning(switchbot) {
631
- switchbot.stopScan();
632
- if (this.BLE_IsConnected) {
633
- await this.BLEparseStatus();
634
- await this.updateHomeKitCharacteristics();
497
+ if (this.Battery.StatusLowBattery === undefined) {
498
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.Battery.StatusLowBattery}`);
635
499
  }
636
500
  else {
637
- await this.BLERefreshConnection(switchbot);
638
- }
639
- }
640
- async getCustomBLEAddress(switchbot) {
641
- if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
642
- (async () => {
643
- // Start to monitor advertisement packets
644
- await switchbot.startScan({
645
- model: 'c',
646
- });
647
- // Set an event handler
648
- switchbot.onadvertisement = (ad) => {
649
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
650
- };
651
- await sleep(10000);
652
- // Stop to monitor
653
- switchbot.stopScan();
654
- })();
501
+ this.accessory.context.StatusLowBattery = this.Battery.StatusLowBattery;
502
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.Battery.StatusLowBattery);
503
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
504
+ + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
655
505
  }
656
506
  }
657
507
  async BLEPushConnection() {
@@ -668,238 +518,21 @@ export class Lock {
668
518
  await this.openAPIRefreshStatus();
669
519
  }
670
520
  }
671
- async scan(device) {
672
- if (device.scanDuration) {
673
- this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
674
- if (this.BLE) {
675
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
676
- }
677
- }
678
- else {
679
- this.scanDuration = this.accessory.context.scanDuration = 1;
680
- if (this.BLE) {
681
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
682
- }
683
- }
684
- }
685
- async statusCode(statusCode) {
686
- switch (statusCode) {
687
- case 151:
688
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this deviceType, statusCode: ${statusCode}`);
689
- break;
690
- case 152:
691
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found, statusCode: ${statusCode}`);
692
- break;
693
- case 160:
694
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported, statusCode: ${statusCode}`);
695
- break;
696
- case 161:
697
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline, statusCode: ${statusCode}`);
698
- this.offlineOff();
699
- break;
700
- case 171:
701
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. ` +
702
- `Hub: ${this.device.hubDeviceId}`);
703
- this.offlineOff();
704
- break;
705
- case 190:
706
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
707
- ` Or command format is invalid, statusCode: ${statusCode}`);
708
- break;
709
- case 100:
710
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent, statusCode: ${statusCode}`);
711
- break;
712
- case 200:
713
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Request successful, statusCode: ${statusCode}`);
714
- break;
715
- case 400:
716
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Bad Request, The client has issued an invalid request. `
717
- + `This is commonly used to specify validation errors in a request payload, statusCode: ${statusCode}`);
718
- break;
719
- case 401:
720
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unauthorized, Authorization for the API is required, `
721
- + `but the request has not been authenticated, statusCode: ${statusCode}`);
722
- break;
723
- case 403:
724
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Forbidden, The request has been authenticated but does not `
725
- + `have appropriate permissions, or a requested resource is not found, statusCode: ${statusCode}`);
726
- break;
727
- case 404:
728
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Found, Specifies the requested path does not exist, `
729
- + `statusCode: ${statusCode}`);
730
- break;
731
- case 406:
732
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Acceptable, The client has requested a MIME type via `
733
- + `the Accept header for a value not supported by the server, statusCode: ${statusCode}`);
734
- break;
735
- case 415:
736
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unsupported Media Type, The client has defined a contentType `
737
- + `header that is not supported by the server, statusCode: ${statusCode}`);
738
- break;
739
- case 422:
740
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unprocessable Entity, The client has made a valid request, `
741
- + `but the server cannot process it. This is often used for APIs for which certain limits have been exceeded, statusCode: ${statusCode}`);
742
- break;
743
- case 429:
744
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Too Many Requests, The client has exceeded the number of `
745
- + `requests allowed for a given time window, statusCode: ${statusCode}`);
746
- break;
747
- case 500:
748
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Internal Server Error, An unexpected error on the SmartThings `
749
- + `servers has occurred. These errors should be rare, statusCode: ${statusCode}`);
750
- break;
751
- default:
752
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: ` +
753
- `${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`);
754
- }
755
- }
756
521
  async offlineOff() {
757
522
  if (this.device.offline) {
758
- await this.deviceContext();
759
- await this.updateHomeKitCharacteristics();
523
+ if (!this.device.lock?.hide_contactsensor) {
524
+ this.ContactSensor.Service.updateCharacteristic(this.hap.Characteristic.ContactSensorState, this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED);
525
+ }
526
+ this.LockMechanism.Service.updateCharacteristic(this.hap.Characteristic.LockTargetState, this.hap.Characteristic.LockTargetState.SECURED);
527
+ this.LockMechanism.Service.updateCharacteristic(this.hap.Characteristic.LockCurrentState, this.hap.Characteristic.LockCurrentState.SECURED);
760
528
  }
761
529
  }
762
530
  async apiError(e) {
763
531
  if (!this.device.lock?.hide_contactsensor) {
764
- this.contactSensorService?.updateCharacteristic(this.hap.Characteristic.ContactSensorState, e);
765
- }
766
- this.lockService.updateCharacteristic(this.hap.Characteristic.LockTargetState, e);
767
- this.lockService.updateCharacteristic(this.hap.Characteristic.LockCurrentState, e);
768
- }
769
- async deviceContext() {
770
- if (this.LockTargetState === undefined) {
771
- this.LockTargetState = false;
772
- }
773
- else {
774
- this.LockTargetState = this.accessory.context.On;
775
- }
776
- if (this.FirmwareRevision === undefined) {
777
- this.FirmwareRevision = this.platform.version;
778
- this.accessory.context.FirmwareRevision = this.FirmwareRevision;
779
- }
780
- }
781
- async refreshRate(device) {
782
- if (device.refreshRate) {
783
- this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
784
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
785
- }
786
- else if (this.platform.config.options.refreshRate) {
787
- this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options.refreshRate;
788
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
789
- }
790
- }
791
- async deviceRetry(device) {
792
- if (!device.maxRetries) {
793
- this.maxRetries = 5; // Maximum number of retries
794
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Max Retries Not Set, Using: ${this.maxRetries}`);
795
- }
796
- else {
797
- this.maxRetries = device.maxRetries;
798
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Max Retries: ${this.maxRetries}`);
799
- }
800
- if (!device.delayBetweenRetries) {
801
- this.delayBetweenRetries = 3000; // Delay between retries in milliseconds
802
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Delay Between Retries Not Set, Using: ${this.delayBetweenRetries}`);
803
- }
804
- else {
805
- this.delayBetweenRetries = device.delayBetweenRetries * 1000;
806
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Delay Between Retries: ${this.delayBetweenRetries}`);
807
- }
808
- }
809
- async deviceConfig(device) {
810
- let config = {};
811
- if (device.lock) {
812
- config = device.lock;
813
- }
814
- if (device.connectionType !== undefined) {
815
- config['connectionType'] = device.connectionType;
816
- }
817
- if (device.external !== undefined) {
818
- config['external'] = device.external;
819
- }
820
- if (device.logging !== undefined) {
821
- config['logging'] = device.logging;
822
- }
823
- if (device.refreshRate !== undefined) {
824
- config['refreshRate'] = device.refreshRate;
825
- }
826
- if (device.scanDuration !== undefined) {
827
- config['scanDuration'] = device.scanDuration;
532
+ this.ContactSensor.Service.updateCharacteristic(this.hap.Characteristic.ContactSensorState, e);
828
533
  }
829
- if (device.webhook !== undefined) {
830
- config['webhook'] = device.webhook;
831
- }
832
- if (Object.entries(config).length !== 0) {
833
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
834
- }
835
- }
836
- async deviceLogs(device) {
837
- if (this.platform.debugMode) {
838
- this.deviceLogging = this.accessory.context.logging = 'debugMode';
839
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
840
- }
841
- else if (device.logging) {
842
- this.deviceLogging = this.accessory.context.logging = device.logging;
843
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
844
- }
845
- else if (this.platform.config.options?.logging) {
846
- this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
847
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
848
- }
849
- else {
850
- this.deviceLogging = this.accessory.context.logging = 'standard';
851
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
852
- }
853
- }
854
- /**
855
- * Logging for Device
856
- */
857
- successLog(...log) {
858
- if (this.enablingDeviceLogging()) {
859
- this.platform.log.success(String(...log));
860
- }
861
- }
862
- infoLog(...log) {
863
- if (this.enablingDeviceLogging()) {
864
- this.platform.log.info(String(...log));
865
- }
866
- }
867
- warnLog(...log) {
868
- if (this.enablingDeviceLogging()) {
869
- this.platform.log.warn(String(...log));
870
- }
871
- }
872
- debugWarnLog(...log) {
873
- if (this.enablingDeviceLogging()) {
874
- if (this.deviceLogging?.includes('debug')) {
875
- this.platform.log.warn('[DEBUG]', String(...log));
876
- }
877
- }
878
- }
879
- errorLog(...log) {
880
- if (this.enablingDeviceLogging()) {
881
- this.platform.log.error(String(...log));
882
- }
883
- }
884
- debugErrorLog(...log) {
885
- if (this.enablingDeviceLogging()) {
886
- if (this.deviceLogging?.includes('debug')) {
887
- this.platform.log.error('[DEBUG]', String(...log));
888
- }
889
- }
890
- }
891
- debugLog(...log) {
892
- if (this.enablingDeviceLogging()) {
893
- if (this.deviceLogging === 'debug') {
894
- this.platform.log.info('[DEBUG]', String(...log));
895
- }
896
- else {
897
- this.platform.log.debug(String(...log));
898
- }
899
- }
900
- }
901
- enablingDeviceLogging() {
902
- return this.deviceLogging.includes('debug') || this.deviceLogging === 'standard';
534
+ this.LockMechanism.Service.updateCharacteristic(this.hap.Characteristic.LockTargetState, e);
535
+ this.LockMechanism.Service.updateCharacteristic(this.hap.Characteristic.LockCurrentState, e);
903
536
  }
904
537
  }
905
538
  //# sourceMappingURL=lock.js.map