@switchbot/homebridge-switchbot 2.0.0-beta.2 → 2.0.0-beta.20
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/CHANGELOG.md +8 -1
- package/README.md +1 -2
- package/config.schema.json +75 -20
- package/dist/device/bot.d.ts +32 -28
- package/dist/device/bot.d.ts.map +1 -1
- package/dist/device/bot.js +646 -648
- package/dist/device/bot.js.map +1 -1
- package/dist/device/colorbulb.d.ts +34 -19
- package/dist/device/colorbulb.d.ts.map +1 -1
- package/dist/device/colorbulb.js +429 -304
- package/dist/device/colorbulb.js.map +1 -1
- package/dist/device/contact.d.ts +16 -7
- package/dist/device/contact.d.ts.map +1 -1
- package/dist/device/contact.js +288 -230
- package/dist/device/contact.js.map +1 -1
- package/dist/device/curtain.d.ts +21 -16
- package/dist/device/curtain.d.ts.map +1 -1
- package/dist/device/curtain.js +519 -489
- package/dist/device/curtain.js.map +1 -1
- package/dist/device/humidifier.d.ts +22 -18
- package/dist/device/humidifier.d.ts.map +1 -1
- package/dist/device/humidifier.js +415 -360
- package/dist/device/humidifier.js.map +1 -1
- package/dist/device/lock.d.ts +22 -8
- package/dist/device/lock.d.ts.map +1 -1
- package/dist/device/lock.js +253 -147
- package/dist/device/lock.js.map +1 -1
- package/dist/device/meter.d.ts +21 -16
- package/dist/device/meter.d.ts.map +1 -1
- package/dist/device/meter.js +325 -255
- package/dist/device/meter.js.map +1 -1
- package/dist/device/meterplus.d.ts +23 -12
- package/dist/device/meterplus.d.ts.map +1 -1
- package/dist/device/meterplus.js +346 -208
- package/dist/device/meterplus.js.map +1 -1
- package/dist/device/motion.d.ts +17 -8
- package/dist/device/motion.d.ts.map +1 -1
- package/dist/device/motion.js +261 -209
- package/dist/device/motion.js.map +1 -1
- package/dist/device/plug.d.ts +24 -16
- package/dist/device/plug.d.ts.map +1 -1
- package/dist/device/plug.js +267 -242
- package/dist/device/plug.js.map +1 -1
- package/dist/device/striplight.d.ts +51 -16
- package/dist/device/striplight.d.ts.map +1 -1
- package/dist/device/striplight.js +527 -185
- package/dist/device/striplight.js.map +1 -1
- package/dist/irdevice/airconditioner.d.ts +7 -12
- package/dist/irdevice/airconditioner.d.ts.map +1 -1
- package/dist/irdevice/airconditioner.js +261 -230
- package/dist/irdevice/airconditioner.js.map +1 -1
- package/dist/irdevice/airpurifier.d.ts +3 -3
- package/dist/irdevice/airpurifier.d.ts.map +1 -1
- package/dist/irdevice/airpurifier.js +137 -104
- package/dist/irdevice/airpurifier.js.map +1 -1
- package/dist/irdevice/camera.d.ts +9 -8
- package/dist/irdevice/camera.d.ts.map +1 -1
- package/dist/irdevice/camera.js +116 -86
- package/dist/irdevice/camera.js.map +1 -1
- package/dist/irdevice/fan.d.ts +8 -7
- package/dist/irdevice/fan.d.ts.map +1 -1
- package/dist/irdevice/fan.js +143 -109
- package/dist/irdevice/fan.js.map +1 -1
- package/dist/irdevice/light.d.ts +9 -8
- package/dist/irdevice/light.d.ts.map +1 -1
- package/dist/irdevice/light.js +120 -103
- package/dist/irdevice/light.js.map +1 -1
- package/dist/irdevice/other.d.ts +9 -8
- package/dist/irdevice/other.d.ts.map +1 -1
- package/dist/irdevice/other.js +128 -98
- package/dist/irdevice/other.js.map +1 -1
- package/dist/irdevice/tv.d.ts +10 -9
- package/dist/irdevice/tv.d.ts.map +1 -1
- package/dist/irdevice/tv.js +126 -93
- package/dist/irdevice/tv.js.map +1 -1
- package/dist/irdevice/vacuumcleaner.d.ts +7 -10
- package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
- package/dist/irdevice/vacuumcleaner.js +115 -89
- package/dist/irdevice/vacuumcleaner.js.map +1 -1
- package/dist/irdevice/waterheater.d.ts +6 -9
- package/dist/irdevice/waterheater.d.ts.map +1 -1
- package/dist/irdevice/waterheater.js +115 -89
- package/dist/irdevice/waterheater.js.map +1 -1
- package/dist/platform.d.ts +3 -5
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +203 -200
- package/dist/platform.js.map +1 -1
- package/dist/settings.d.ts +15 -11
- package/dist/settings.d.ts.map +1 -1
- package/dist/settings.js.map +1 -1
- package/package.json +12 -10
- package/src/device/bot.ts +641 -657
- package/src/device/colorbulb.ts +440 -339
- package/src/device/contact.ts +289 -233
- package/src/device/curtain.ts +523 -503
- package/src/device/humidifier.ts +421 -396
- package/src/device/lock.ts +257 -147
- package/src/device/meter.ts +335 -268
- package/src/device/meterplus.ts +354 -213
- package/src/device/motion.ts +264 -213
- package/src/device/plug.ts +270 -251
- package/src/device/striplight.ts +552 -207
- package/src/irdevice/airconditioner.ts +263 -241
- package/src/irdevice/airpurifier.ts +132 -103
- package/src/irdevice/camera.ts +112 -85
- package/src/irdevice/fan.ts +138 -106
- package/src/irdevice/light.ts +116 -103
- package/src/irdevice/other.ts +124 -97
- package/src/irdevice/tv.ts +123 -93
- package/src/irdevice/vacuumcleaner.ts +112 -89
- package/src/irdevice/waterheater.ts +111 -89
- package/src/platform.ts +195 -216
- package/src/settings.ts +15 -11
package/dist/device/lock.js
CHANGED
|
@@ -7,6 +7,7 @@ exports.Lock = void 0;
|
|
|
7
7
|
const https_1 = __importDefault(require("https"));
|
|
8
8
|
const crypto_1 = __importDefault(require("crypto"));
|
|
9
9
|
const rxjs_1 = require("rxjs");
|
|
10
|
+
const super_stringify_1 = __importDefault(require("super-stringify"));
|
|
10
11
|
const operators_1 = require("rxjs/operators");
|
|
11
12
|
const settings_1 = require("../settings");
|
|
12
13
|
class Lock {
|
|
@@ -14,17 +15,15 @@ class Lock {
|
|
|
14
15
|
this.platform = platform;
|
|
15
16
|
this.accessory = accessory;
|
|
16
17
|
this.device = device;
|
|
18
|
+
// Connection
|
|
19
|
+
this.BLE = (this.device.connectionType === 'BLE' || this.device.connectionType === 'Both');
|
|
20
|
+
this.OpenAPI = (this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'Both');
|
|
17
21
|
// default placeholders
|
|
18
22
|
this.logs(device);
|
|
19
23
|
this.scan(device);
|
|
20
24
|
this.refreshRate(device);
|
|
21
25
|
this.config(device);
|
|
22
|
-
|
|
23
|
-
this.LockTargetState = false;
|
|
24
|
-
}
|
|
25
|
-
else {
|
|
26
|
-
this.LockTargetState = this.accessory.context.On;
|
|
27
|
-
}
|
|
26
|
+
this.conext();
|
|
28
27
|
// this is subject we use to track when we need to POST changes to the SwitchBot API
|
|
29
28
|
this.doLockUpdate = new rxjs_1.Subject();
|
|
30
29
|
this.lockUpdateInProgress = false;
|
|
@@ -55,19 +54,19 @@ class Lock {
|
|
|
55
54
|
this.lockService.getCharacteristic(this.platform.Characteristic.LockTargetState).onSet(this.LockTargetStateSet.bind(this));
|
|
56
55
|
// Contact Sensor Service
|
|
57
56
|
if (device.lock?.hide_contactsensor) {
|
|
58
|
-
this.debugLog(
|
|
57
|
+
this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Contact Sensor Service`);
|
|
59
58
|
this.contactSensorService = this.accessory.getService(this.platform.Service.ContactSensor);
|
|
60
59
|
accessory.removeService(this.contactSensorService);
|
|
61
60
|
}
|
|
62
61
|
else if (!this.contactSensorService) {
|
|
63
|
-
this.debugLog(
|
|
62
|
+
this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Contact Sensor Service`);
|
|
64
63
|
(this.contactSensorService =
|
|
65
64
|
this.accessory.getService(this.platform.Service.ContactSensor) || this.accessory.addService(this.platform.Service.ContactSensor)),
|
|
66
65
|
`${accessory.displayName} Contact Sensor`;
|
|
67
66
|
this.contactSensorService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Contact Sensor`);
|
|
68
67
|
}
|
|
69
68
|
else {
|
|
70
|
-
this.debugLog(
|
|
69
|
+
this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Contact Sensor Service Not Added`);
|
|
71
70
|
}
|
|
72
71
|
// Update Homekit
|
|
73
72
|
this.updateHomeKitCharacteristics();
|
|
@@ -88,17 +87,31 @@ class Lock {
|
|
|
88
87
|
await this.pushChanges();
|
|
89
88
|
}
|
|
90
89
|
catch (e) {
|
|
91
|
-
this.errorLog(`Lock: ${this.accessory.displayName} failed pushChanges`);
|
|
92
|
-
if (this.deviceLogging.includes('debug')) {
|
|
93
|
-
this.errorLog(`Lock: ${this.accessory.displayName} failed pushChanges,` + ` Error Message: ${JSON.stringify(e.message)}`);
|
|
94
|
-
}
|
|
95
90
|
this.apiError(e);
|
|
91
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with ${this.device.connectionType} Connection,`
|
|
92
|
+
+ ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
|
|
96
93
|
}
|
|
97
94
|
this.lockUpdateInProgress = false;
|
|
98
95
|
});
|
|
99
96
|
}
|
|
100
|
-
|
|
97
|
+
/**
|
|
98
|
+
* Parse the device status from the SwitchBot api
|
|
99
|
+
*/
|
|
101
100
|
async parseStatus() {
|
|
101
|
+
/*if (this.BLE && !this.SwitchToOpenAPI) {
|
|
102
|
+
await this.BLEparseStatus();
|
|
103
|
+
} else*/ if (this.OpenAPI) {
|
|
104
|
+
//this.SwitchToOpenAPI = false;
|
|
105
|
+
await this.openAPIparseStatus();
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
|
|
109
|
+
+ ` ${this.device.connectionType}, parseStatus will not happen.`);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
//{"deviceId":"DBDC23B53139","deviceType":"Smart Lock","hubDeviceId":"E68B14109DA2","lockState":"locked","doorState":"opened","calibrate":true}
|
|
113
|
+
async openAPIparseStatus() {
|
|
114
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
|
|
102
115
|
switch (this.lockState) {
|
|
103
116
|
case 'locked':
|
|
104
117
|
this.LockCurrentState = this.platform.Characteristic.LockCurrentState.SECURED;
|
|
@@ -115,16 +128,31 @@ class Lock {
|
|
|
115
128
|
default:
|
|
116
129
|
this.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED;
|
|
117
130
|
}
|
|
118
|
-
this.debugLog(
|
|
131
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LockTargetState}`);
|
|
119
132
|
}
|
|
133
|
+
/**
|
|
134
|
+
* Asks the SwitchBot API for the latest device information
|
|
135
|
+
*/
|
|
120
136
|
async refreshStatus() {
|
|
137
|
+
/*if (this.BLE) {
|
|
138
|
+
await this.BLERefreshStatus();
|
|
139
|
+
} else*/ if (this.OpenAPI && this.platform.config.credentials?.token) {
|
|
140
|
+
await this.openAPIRefreshStatus();
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
|
|
144
|
+
+ ` ${this.device.connectionType}, refreshStatus will not happen.`);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
async openAPIRefreshStatus() {
|
|
148
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
|
|
121
149
|
try {
|
|
122
150
|
const t = Date.now();
|
|
123
151
|
const nonce = 'requestID';
|
|
124
152
|
const data = this.platform.config.credentials?.token + t + nonce;
|
|
125
153
|
const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
|
|
126
154
|
const sign = signTerm.toString('base64');
|
|
127
|
-
this.debugLog(
|
|
155
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
|
|
128
156
|
const options = {
|
|
129
157
|
hostname: settings_1.HostDomain,
|
|
130
158
|
port: 443,
|
|
@@ -139,37 +167,35 @@ class Lock {
|
|
|
139
167
|
},
|
|
140
168
|
};
|
|
141
169
|
const req = https_1.default.request(options, (res) => {
|
|
142
|
-
this.debugLog(
|
|
170
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus statusCode: ${res.statusCode}`);
|
|
143
171
|
let rawData = '';
|
|
144
172
|
res.on('data', (d) => {
|
|
145
173
|
rawData += d;
|
|
146
|
-
this.debugLog(
|
|
174
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
|
|
147
175
|
});
|
|
148
176
|
res.on('end', () => {
|
|
149
177
|
try {
|
|
150
178
|
this.deviceStatus = JSON.parse(rawData);
|
|
151
|
-
this.debugLog(
|
|
179
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus: ${(0, super_stringify_1.default)(this.deviceStatus)}`);
|
|
152
180
|
this.lockState = this.deviceStatus.body.lockState;
|
|
153
181
|
this.doorState = this.deviceStatus.body.doorState;
|
|
154
182
|
this.parseStatus();
|
|
155
183
|
this.updateHomeKitCharacteristics();
|
|
156
184
|
}
|
|
157
185
|
catch (e) {
|
|
158
|
-
this.errorLog(
|
|
186
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
|
|
159
187
|
}
|
|
160
188
|
});
|
|
161
189
|
});
|
|
162
190
|
req.on('error', (e) => {
|
|
163
|
-
this.errorLog(
|
|
191
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
|
|
164
192
|
});
|
|
165
193
|
req.end();
|
|
166
194
|
}
|
|
167
195
|
catch (e) {
|
|
168
|
-
this.errorLog(`Lock: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
|
|
169
|
-
if (this.deviceLogging.includes('debug')) {
|
|
170
|
-
this.errorLog(`Lock: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,` + ` Error Message: ${JSON.stringify(e.message)}`);
|
|
171
|
-
}
|
|
172
196
|
this.apiError(e);
|
|
197
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
|
|
198
|
+
+ ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
|
|
173
199
|
}
|
|
174
200
|
}
|
|
175
201
|
/**
|
|
@@ -179,210 +205,290 @@ class Lock {
|
|
|
179
205
|
* Lock - "command" "unlock" "default" = set to ???? state - LockCurrentState
|
|
180
206
|
*/
|
|
181
207
|
async pushChanges() {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret)
|
|
187
|
-
.update(Buffer.from(data, 'utf-8'))
|
|
188
|
-
.digest();
|
|
189
|
-
const sign = signTerm.toString('base64');
|
|
190
|
-
this.debugLog(`Lock: ${this.accessory.displayName} sign: ${sign}`);
|
|
191
|
-
let command = '';
|
|
192
|
-
if (this.LockTargetState) {
|
|
193
|
-
command = 'lock';
|
|
208
|
+
/*if (this.BLE) {
|
|
209
|
+
await this.BLERefreshStatus();
|
|
210
|
+
} else*/ if (this.OpenAPI && this.platform.config.credentials?.token) {
|
|
211
|
+
await this.openAPIpushChanges();
|
|
194
212
|
}
|
|
195
213
|
else {
|
|
196
|
-
|
|
214
|
+
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
|
|
215
|
+
+ ` ${this.device.connectionType}, pushChanges will not happen.`);
|
|
197
216
|
}
|
|
198
|
-
const body = JSON.stringify({
|
|
199
|
-
'command': `${command}`,
|
|
200
|
-
'parameter': 'default',
|
|
201
|
-
'commandType': 'command',
|
|
202
|
-
});
|
|
203
|
-
this.infoLog(`Lock: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
|
|
204
|
-
const options = {
|
|
205
|
-
hostname: settings_1.HostDomain,
|
|
206
|
-
port: 443,
|
|
207
|
-
path: `${settings_1.DevicePath}/${this.device.deviceId}/commands`,
|
|
208
|
-
method: 'POST',
|
|
209
|
-
headers: {
|
|
210
|
-
'Authorization': this.platform.config.credentials?.token,
|
|
211
|
-
'sign': sign,
|
|
212
|
-
'nonce': nonce,
|
|
213
|
-
't': t,
|
|
214
|
-
'Content-Type': 'application/json',
|
|
215
|
-
'Content-Length': body.length,
|
|
216
|
-
},
|
|
217
|
-
};
|
|
218
|
-
const req = https_1.default.request(options, res => {
|
|
219
|
-
this.debugLog(`Lock: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
|
|
220
|
-
this.statusCode({ res });
|
|
221
|
-
res.on('data', d => {
|
|
222
|
-
this.debugLog(`Lock: ${this.accessory.displayName} d: ${d}`);
|
|
223
|
-
});
|
|
224
|
-
});
|
|
225
|
-
req.on('error', (e) => {
|
|
226
|
-
this.errorLog(`Lock: ${this.accessory.displayName} error message: ${e.message}`);
|
|
227
|
-
});
|
|
228
|
-
req.write(body);
|
|
229
|
-
req.end();
|
|
230
|
-
this.debugLog(`Lock: ${this.accessory.displayName} pushchanges: ${JSON.stringify(req)}`);
|
|
231
|
-
this.accessory.context.On = this.LockTargetState;
|
|
232
217
|
(0, rxjs_1.interval)(5000)
|
|
233
218
|
.pipe((0, operators_1.take)(1))
|
|
234
219
|
.subscribe(async () => {
|
|
235
220
|
await this.refreshStatus();
|
|
236
221
|
});
|
|
237
222
|
}
|
|
223
|
+
async BLEpushChanges() {
|
|
224
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`);
|
|
225
|
+
if (this.LockTargetState !== this.accessory.context.LockTargetState) {
|
|
226
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges LockTargetState: ${this.LockTargetState}`
|
|
227
|
+
+ ` LockTargetStateCached: ${this.accessory.context.LockTargetState}`);
|
|
228
|
+
const switchbot = await this.platform.connectBLE();
|
|
229
|
+
// Convert to BLE Address
|
|
230
|
+
this.device.bleMac = this.device
|
|
231
|
+
.deviceId.match(/.{1,2}/g)
|
|
232
|
+
.join(':')
|
|
233
|
+
.toLowerCase();
|
|
234
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
235
|
+
switchbot
|
|
236
|
+
.discover({
|
|
237
|
+
model: '',
|
|
238
|
+
id: this.device.bleMac,
|
|
239
|
+
})
|
|
240
|
+
.then(() => {
|
|
241
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
|
|
242
|
+
this.LockTargetState = this.platform.Characteristic.LockTargetState.SECURED;
|
|
243
|
+
})
|
|
244
|
+
.catch(async (e) => {
|
|
245
|
+
this.apiError(e);
|
|
246
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
|
|
247
|
+
+ ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
|
|
248
|
+
if (this.platform.config.credentials?.token && this.device.connectionType !== 'Both') {
|
|
249
|
+
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
250
|
+
await this.openAPIpushChanges();
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges.` + `LockTargetState: ${this.LockTargetState}, `
|
|
256
|
+
+ `LockTargetStateCached: ${this.accessory.context.LockTargetState}`);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
async openAPIpushChanges() {
|
|
260
|
+
try {
|
|
261
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
|
|
262
|
+
if (this.LockTargetState !== this.accessory.context.LockTargetState) {
|
|
263
|
+
// Make Push On request to the API
|
|
264
|
+
const t = Date.now();
|
|
265
|
+
const nonce = 'requestID';
|
|
266
|
+
const data = this.platform.config.credentials?.token + t + nonce;
|
|
267
|
+
const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret)
|
|
268
|
+
.update(Buffer.from(data, 'utf-8'))
|
|
269
|
+
.digest();
|
|
270
|
+
const sign = signTerm.toString('base64');
|
|
271
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
|
|
272
|
+
let command = '';
|
|
273
|
+
if (this.LockTargetState) {
|
|
274
|
+
command = 'lock';
|
|
275
|
+
}
|
|
276
|
+
else {
|
|
277
|
+
command = 'unlock';
|
|
278
|
+
}
|
|
279
|
+
const body = (0, super_stringify_1.default)({
|
|
280
|
+
'command': `${command}`,
|
|
281
|
+
'parameter': 'default',
|
|
282
|
+
'commandType': 'command',
|
|
283
|
+
});
|
|
284
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
|
|
285
|
+
const options = {
|
|
286
|
+
hostname: settings_1.HostDomain,
|
|
287
|
+
port: 443,
|
|
288
|
+
path: `${settings_1.DevicePath}/${this.device.deviceId}/commands`,
|
|
289
|
+
method: 'POST',
|
|
290
|
+
headers: {
|
|
291
|
+
'Authorization': this.platform.config.credentials?.token,
|
|
292
|
+
'sign': sign,
|
|
293
|
+
'nonce': nonce,
|
|
294
|
+
't': t,
|
|
295
|
+
'Content-Type': 'application/json',
|
|
296
|
+
'Content-Length': body.length,
|
|
297
|
+
},
|
|
298
|
+
};
|
|
299
|
+
const req = https_1.default.request(options, res => {
|
|
300
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges statusCode: ${res.statusCode}`);
|
|
301
|
+
this.statusCode({ res });
|
|
302
|
+
res.on('data', d => {
|
|
303
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
|
|
304
|
+
});
|
|
305
|
+
});
|
|
306
|
+
req.on('error', (e) => {
|
|
307
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
|
|
308
|
+
});
|
|
309
|
+
req.write(body);
|
|
310
|
+
req.end();
|
|
311
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges: ${(0, super_stringify_1.default)(req)}`);
|
|
312
|
+
}
|
|
313
|
+
else {
|
|
314
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.` + `LockTargetState: ${this.LockTargetState}, `
|
|
315
|
+
+ `LockTargetStateCached: ${this.accessory.context.LockTargetState}`);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
catch (e) {
|
|
319
|
+
this.apiError(e);
|
|
320
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
|
|
321
|
+
+ ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* Handle requests to set the value of the "On" characteristic
|
|
326
|
+
*/
|
|
327
|
+
async LockTargetStateSet(value) {
|
|
328
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set LockTargetState: ${value}`);
|
|
329
|
+
this.LockTargetState = value;
|
|
330
|
+
this.doLockUpdate.next();
|
|
331
|
+
}
|
|
238
332
|
async updateHomeKitCharacteristics() {
|
|
239
333
|
if (!this.device.lock?.hide_contactsensor) {
|
|
240
334
|
if (this.ContactSensorState === undefined) {
|
|
241
|
-
this.debugLog(
|
|
335
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensorState}`);
|
|
242
336
|
}
|
|
243
337
|
else {
|
|
338
|
+
this.accessory.context.ContactSensorState = this.ContactSensorState;
|
|
244
339
|
this.contactSensorService?.updateCharacteristic(this.platform.Characteristic.ContactSensorState, this.ContactSensorState);
|
|
245
|
-
this.debugLog(
|
|
340
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic ContactSensorState: ${this.ContactSensorState}`);
|
|
246
341
|
}
|
|
247
342
|
}
|
|
248
343
|
if (this.LockTargetState === undefined) {
|
|
249
|
-
this.debugLog(
|
|
344
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LockTargetState: ${this.LockTargetState}`);
|
|
250
345
|
}
|
|
251
346
|
else {
|
|
347
|
+
this.accessory.context.LockTargetState = this.LockTargetState;
|
|
252
348
|
this.lockService.updateCharacteristic(this.platform.Characteristic.LockTargetState, this.LockTargetState);
|
|
253
|
-
this.debugLog(
|
|
349
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic LockTargetState: ${this.LockTargetState}`);
|
|
254
350
|
}
|
|
255
351
|
if (this.LockCurrentState === undefined) {
|
|
256
|
-
this.debugLog(
|
|
352
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LockCurrentState: ${this.LockCurrentState}`);
|
|
257
353
|
}
|
|
258
354
|
else {
|
|
355
|
+
this.accessory.context.LockCurrentState = this.LockCurrentState;
|
|
259
356
|
this.lockService.updateCharacteristic(this.platform.Characteristic.LockCurrentState, this.LockCurrentState);
|
|
260
|
-
this.debugLog(
|
|
357
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic LockCurrentState: ${this.LockCurrentState}`);
|
|
261
358
|
}
|
|
262
359
|
}
|
|
263
|
-
async
|
|
264
|
-
if (
|
|
265
|
-
this.
|
|
360
|
+
async scan(device) {
|
|
361
|
+
if (device.scanDuration) {
|
|
362
|
+
this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
|
|
363
|
+
if (this.BLE) {
|
|
364
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
else {
|
|
368
|
+
this.scanDuration = this.accessory.context.scanDuration = 1;
|
|
369
|
+
if (this.BLE) {
|
|
370
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
|
|
371
|
+
}
|
|
266
372
|
}
|
|
267
|
-
this.lockService.updateCharacteristic(this.platform.Characteristic.LockTargetState, e);
|
|
268
|
-
this.lockService.updateCharacteristic(this.platform.Characteristic.LockCurrentState, e);
|
|
269
|
-
//throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
|
|
270
373
|
}
|
|
271
374
|
async statusCode({ res }) {
|
|
272
375
|
switch (res.statusCode) {
|
|
273
376
|
case 151:
|
|
274
|
-
this.errorLog(
|
|
377
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this device type.`);
|
|
275
378
|
break;
|
|
276
379
|
case 152:
|
|
277
|
-
this.errorLog(
|
|
380
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found.`);
|
|
278
381
|
break;
|
|
279
382
|
case 160:
|
|
280
|
-
this.errorLog(
|
|
383
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported.`);
|
|
281
384
|
break;
|
|
282
385
|
case 161:
|
|
283
|
-
this.errorLog(
|
|
386
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline.`);
|
|
387
|
+
this.offlineOff();
|
|
284
388
|
break;
|
|
285
389
|
case 171:
|
|
286
|
-
this.errorLog(
|
|
390
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
|
|
391
|
+
this.offlineOff();
|
|
287
392
|
break;
|
|
288
393
|
case 190:
|
|
289
|
-
this.errorLog(
|
|
290
|
-
` Or command: ${
|
|
394
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
|
|
395
|
+
` Or command: ${(0, super_stringify_1.default)(res)} format is invalid`);
|
|
291
396
|
break;
|
|
292
397
|
case 100:
|
|
293
|
-
this.debugLog(
|
|
398
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent.`);
|
|
294
399
|
break;
|
|
295
400
|
default:
|
|
296
|
-
this.debugLog(
|
|
401
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode.`);
|
|
297
402
|
}
|
|
298
403
|
}
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
this.doLockUpdate.next();
|
|
404
|
+
async offlineOff() {
|
|
405
|
+
if (this.device.offline) {
|
|
406
|
+
this.LockCurrentState = this.platform.Characteristic.LockCurrentState.SECURED;
|
|
407
|
+
this.lockService.getCharacteristic(this.platform.Characteristic.LockCurrentState).updateValue(this.LockCurrentState);
|
|
408
|
+
await this.updateHomeKitCharacteristics();
|
|
409
|
+
}
|
|
306
410
|
}
|
|
307
|
-
async
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
config = device.lock;
|
|
411
|
+
async apiError(e) {
|
|
412
|
+
if (!this.device.lock?.hide_contactsensor) {
|
|
413
|
+
this.contactSensorService?.updateCharacteristic(this.platform.Characteristic.ContactSensorState, e);
|
|
311
414
|
}
|
|
312
|
-
|
|
313
|
-
|
|
415
|
+
this.lockService.updateCharacteristic(this.platform.Characteristic.LockTargetState, e);
|
|
416
|
+
this.lockService.updateCharacteristic(this.platform.Characteristic.LockCurrentState, e);
|
|
417
|
+
}
|
|
418
|
+
FirmwareRevision(accessory, device) {
|
|
419
|
+
let FirmwareRevision;
|
|
420
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
|
|
421
|
+
+ ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
|
|
422
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
|
|
423
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
|
|
424
|
+
if (accessory.context.FirmwareRevision) {
|
|
425
|
+
FirmwareRevision = accessory.context.FirmwareRevision;
|
|
314
426
|
}
|
|
315
|
-
if (device.
|
|
316
|
-
|
|
427
|
+
else if (device.firmware) {
|
|
428
|
+
FirmwareRevision = device.firmware;
|
|
317
429
|
}
|
|
318
|
-
|
|
319
|
-
|
|
430
|
+
else {
|
|
431
|
+
FirmwareRevision = this.platform.version;
|
|
320
432
|
}
|
|
321
|
-
|
|
322
|
-
|
|
433
|
+
return FirmwareRevision;
|
|
434
|
+
}
|
|
435
|
+
conext() {
|
|
436
|
+
if (this.LockTargetState === undefined) {
|
|
437
|
+
this.LockTargetState = false;
|
|
323
438
|
}
|
|
324
|
-
|
|
325
|
-
this.
|
|
439
|
+
else {
|
|
440
|
+
this.LockTargetState = this.accessory.context.On;
|
|
326
441
|
}
|
|
327
442
|
}
|
|
328
443
|
async refreshRate(device) {
|
|
329
444
|
if (device.refreshRate) {
|
|
330
445
|
this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
|
|
331
|
-
this.debugLog(
|
|
446
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
|
|
332
447
|
}
|
|
333
448
|
else if (this.platform.config.options.refreshRate) {
|
|
334
449
|
this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options.refreshRate;
|
|
335
|
-
this.debugLog(
|
|
450
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
|
|
336
451
|
}
|
|
337
452
|
}
|
|
338
|
-
async
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
this.debugLog(`Lock: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
|
|
343
|
-
}
|
|
453
|
+
async config(device) {
|
|
454
|
+
let config = {};
|
|
455
|
+
if (device.lock) {
|
|
456
|
+
config = device.lock;
|
|
344
457
|
}
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
458
|
+
if (device.connectionType !== undefined) {
|
|
459
|
+
config['connectionType'] = device.connectionType;
|
|
460
|
+
}
|
|
461
|
+
if (device.logging !== undefined) {
|
|
462
|
+
config['logging'] = device.logging;
|
|
463
|
+
}
|
|
464
|
+
if (device.refreshRate !== undefined) {
|
|
465
|
+
config['refreshRate'] = device.refreshRate;
|
|
466
|
+
}
|
|
467
|
+
if (device.scanDuration !== undefined) {
|
|
468
|
+
config['scanDuration'] = device.scanDuration;
|
|
469
|
+
}
|
|
470
|
+
if (Object.entries(config).length !== 0) {
|
|
471
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${(0, super_stringify_1.default)(config)}`);
|
|
350
472
|
}
|
|
351
473
|
}
|
|
352
474
|
async logs(device) {
|
|
353
475
|
if (this.platform.debugMode) {
|
|
354
476
|
this.deviceLogging = this.accessory.context.logging = 'debugMode';
|
|
355
|
-
this.debugLog(
|
|
477
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
|
|
356
478
|
}
|
|
357
479
|
else if (device.logging) {
|
|
358
480
|
this.deviceLogging = this.accessory.context.logging = device.logging;
|
|
359
|
-
this.debugLog(
|
|
481
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
|
|
360
482
|
}
|
|
361
483
|
else if (this.platform.config.options?.logging) {
|
|
362
484
|
this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
|
|
363
|
-
this.debugLog(
|
|
485
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
|
|
364
486
|
}
|
|
365
487
|
else {
|
|
366
488
|
this.deviceLogging = this.accessory.context.logging = 'standard';
|
|
367
|
-
this.debugLog(
|
|
489
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
|
|
368
490
|
}
|
|
369
491
|
}
|
|
370
|
-
FirmwareRevision(accessory, device) {
|
|
371
|
-
let FirmwareRevision;
|
|
372
|
-
this.debugLog(`Lock: ${this.accessory.displayName} accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
|
|
373
|
-
this.debugLog(`Lock: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
|
|
374
|
-
this.debugLog(`Lock: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
|
|
375
|
-
if (accessory.context.FirmwareRevision) {
|
|
376
|
-
FirmwareRevision = accessory.context.FirmwareRevision;
|
|
377
|
-
}
|
|
378
|
-
else if (device.firmware) {
|
|
379
|
-
FirmwareRevision = device.firmware;
|
|
380
|
-
}
|
|
381
|
-
else {
|
|
382
|
-
FirmwareRevision = this.platform.version;
|
|
383
|
-
}
|
|
384
|
-
return FirmwareRevision;
|
|
385
|
-
}
|
|
386
492
|
/**
|
|
387
493
|
* Logging for Device
|
|
388
494
|
*/
|