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