@switchbot/homebridge-switchbot 2.0.0-beta.19 → 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 +6 -1
- package/README.md +1 -2
- package/config.schema.json +51 -32
- package/dist/device/bot.d.ts +32 -29
- package/dist/device/bot.d.ts.map +1 -1
- package/dist/device/bot.js +549 -575
- package/dist/device/bot.js.map +1 -1
- package/dist/device/colorbulb.d.ts +33 -15
- package/dist/device/colorbulb.d.ts.map +1 -1
- package/dist/device/colorbulb.js +426 -341
- package/dist/device/colorbulb.js.map +1 -1
- package/dist/device/contact.d.ts +16 -9
- package/dist/device/contact.d.ts.map +1 -1
- package/dist/device/contact.js +246 -208
- package/dist/device/contact.js.map +1 -1
- package/dist/device/curtain.d.ts +21 -18
- package/dist/device/curtain.d.ts.map +1 -1
- package/dist/device/curtain.js +449 -449
- package/dist/device/curtain.js.map +1 -1
- package/dist/device/humidifier.d.ts +22 -20
- package/dist/device/humidifier.d.ts.map +1 -1
- package/dist/device/humidifier.js +346 -324
- package/dist/device/humidifier.js.map +1 -1
- package/dist/device/lock.d.ts +22 -10
- package/dist/device/lock.d.ts.map +1 -1
- package/dist/device/lock.js +257 -178
- package/dist/device/lock.js.map +1 -1
- package/dist/device/meter.d.ts +21 -14
- package/dist/device/meter.d.ts.map +1 -1
- package/dist/device/meter.js +287 -239
- package/dist/device/meter.js.map +1 -1
- package/dist/device/meterplus.d.ts +23 -10
- package/dist/device/meterplus.d.ts.map +1 -1
- package/dist/device/meterplus.js +308 -190
- package/dist/device/meterplus.js.map +1 -1
- package/dist/device/motion.d.ts +17 -10
- package/dist/device/motion.d.ts.map +1 -1
- package/dist/device/motion.js +225 -193
- package/dist/device/motion.js.map +1 -1
- package/dist/device/plug.d.ts +24 -17
- package/dist/device/plug.d.ts.map +1 -1
- package/dist/device/plug.js +270 -267
- package/dist/device/plug.js.map +1 -1
- package/dist/device/striplight.d.ts +36 -14
- package/dist/device/striplight.d.ts.map +1 -1
- package/dist/device/striplight.js +447 -299
- package/dist/device/striplight.js.map +1 -1
- package/dist/irdevice/airconditioner.d.ts +7 -14
- package/dist/irdevice/airconditioner.d.ts.map +1 -1
- package/dist/irdevice/airconditioner.js +177 -169
- package/dist/irdevice/airconditioner.js.map +1 -1
- package/dist/irdevice/airpurifier.d.ts +3 -5
- package/dist/irdevice/airpurifier.d.ts.map +1 -1
- package/dist/irdevice/airpurifier.js +72 -57
- package/dist/irdevice/airpurifier.js.map +1 -1
- package/dist/irdevice/camera.d.ts +3 -4
- package/dist/irdevice/camera.d.ts.map +1 -1
- package/dist/irdevice/camera.js +47 -33
- package/dist/irdevice/camera.js.map +1 -1
- package/dist/irdevice/fan.d.ts +3 -4
- package/dist/irdevice/fan.d.ts.map +1 -1
- package/dist/irdevice/fan.js +66 -48
- package/dist/irdevice/fan.js.map +1 -1
- package/dist/irdevice/light.d.ts +3 -4
- package/dist/irdevice/light.d.ts.map +1 -1
- package/dist/irdevice/light.js +48 -47
- package/dist/irdevice/light.js.map +1 -1
- package/dist/irdevice/other.d.ts +3 -4
- package/dist/irdevice/other.d.ts.map +1 -1
- package/dist/irdevice/other.js +47 -33
- package/dist/irdevice/other.js.map +1 -1
- package/dist/irdevice/tv.d.ts +10 -11
- package/dist/irdevice/tv.d.ts.map +1 -1
- package/dist/irdevice/tv.js +64 -48
- package/dist/irdevice/tv.js.map +1 -1
- package/dist/irdevice/vacuumcleaner.d.ts +7 -12
- package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
- package/dist/irdevice/vacuumcleaner.js +52 -41
- package/dist/irdevice/vacuumcleaner.js.map +1 -1
- package/dist/irdevice/waterheater.d.ts +6 -11
- package/dist/irdevice/waterheater.d.ts.map +1 -1
- package/dist/irdevice/waterheater.js +52 -42
- package/dist/irdevice/waterheater.js.map +1 -1
- package/dist/platform.d.ts +0 -4
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +44 -66
- package/dist/platform.js.map +1 -1
- package/dist/settings.d.ts +3 -3
- package/dist/settings.d.ts.map +1 -1
- package/package.json +7 -6
- package/src/device/bot.ts +551 -589
- package/src/device/colorbulb.ts +441 -375
- package/src/device/contact.ts +249 -213
- package/src/device/curtain.ts +452 -461
- package/src/device/humidifier.ts +358 -365
- package/src/device/lock.ts +263 -179
- package/src/device/meter.ts +302 -253
- package/src/device/meterplus.ts +319 -194
- package/src/device/motion.ts +230 -199
- package/src/device/plug.ts +273 -274
- package/src/device/striplight.ts +460 -316
- package/src/irdevice/airconditioner.ts +181 -183
- package/src/irdevice/airpurifier.ts +70 -60
- package/src/irdevice/camera.ts +46 -36
- package/src/irdevice/fan.ts +64 -49
- package/src/irdevice/light.ts +47 -51
- package/src/irdevice/other.ts +46 -36
- package/src/irdevice/tv.ts +64 -51
- package/src/irdevice/vacuumcleaner.ts +52 -45
- package/src/irdevice/waterheater.ts +50 -45
- package/src/platform.ts +44 -68
- package/src/settings.ts +3 -3
|
@@ -20,23 +20,15 @@ class StripLight {
|
|
|
20
20
|
this.platform = platform;
|
|
21
21
|
this.accessory = accessory;
|
|
22
22
|
this.device = device;
|
|
23
|
+
// Connection
|
|
24
|
+
this.BLE = (this.device.connectionType === 'BLE' || this.device.connectionType === 'Both');
|
|
25
|
+
this.OpenAPI = (this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'Both');
|
|
23
26
|
// default placeholders
|
|
24
27
|
this.logs(device);
|
|
25
|
-
this.openAPI();
|
|
26
28
|
this.scan(device);
|
|
27
29
|
this.refreshRate(device);
|
|
28
30
|
this.config(device);
|
|
29
|
-
|
|
30
|
-
this.On = false;
|
|
31
|
-
}
|
|
32
|
-
else {
|
|
33
|
-
this.On = this.accessory.context.On;
|
|
34
|
-
}
|
|
35
|
-
this.Hue = 0;
|
|
36
|
-
this.Brightness = 0;
|
|
37
|
-
this.Saturation = 0;
|
|
38
|
-
this.minKelvin = 2000;
|
|
39
|
-
this.maxKelvin = 9000;
|
|
31
|
+
this.context();
|
|
40
32
|
// this is subject we use to track when we need to POST changes to the SwitchBot API
|
|
41
33
|
this.doStripLightUpdate = new rxjs_1.Subject();
|
|
42
34
|
this.stripLightUpdateInProgress = false;
|
|
@@ -54,7 +46,13 @@ class StripLight {
|
|
|
54
46
|
// get the Lightbulb service if it exists, otherwise create a new Lightbulb service
|
|
55
47
|
// you can create multiple services for each accessory
|
|
56
48
|
(this.lightBulbService = accessory.getService(this.platform.Service.Lightbulb) || accessory.addService(this.platform.Service.Lightbulb)),
|
|
57
|
-
`${accessory.displayName}
|
|
49
|
+
`${accessory.displayName} ${device.deviceType}`;
|
|
50
|
+
/*if (this.adaptiveLightingShift === -1 && this.accessory.context.adaptiveLighting) {
|
|
51
|
+
this.accessory.removeService(this.lightBulbService);
|
|
52
|
+
this.lightBulbService = this.accessory.addService(this.platform.Service.Lightbulb);
|
|
53
|
+
this.accessory.context.adaptiveLighting = false;
|
|
54
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLighting: ${this.accessory.context.adaptiveLighting}`);
|
|
55
|
+
}*/
|
|
58
56
|
// To avoid "Cannot add a Service with the same UUID another Service without also defining a unique 'subtype' property." error,
|
|
59
57
|
// when creating multiple services of the same type, you need to use the following syntax to specify a name and subtype id:
|
|
60
58
|
// accessory.getService('NAME') ?? accessory.addService(this.platform.Service.Outlet, 'NAME', 'USER_DEFINED_SUBTYPE');
|
|
@@ -100,6 +98,18 @@ class StripLight {
|
|
|
100
98
|
return this.Saturation;
|
|
101
99
|
})
|
|
102
100
|
.onSet(this.SaturationSet.bind(this));
|
|
101
|
+
/*this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
|
|
102
|
+
if (this.adaptiveLightingShift !== -1) {
|
|
103
|
+
this.AdaptiveLightingController = new platform.api.hap.AdaptiveLightingController(this.lightBulbService, {
|
|
104
|
+
customTemperatureAdjustment: this.adaptiveLightingShift,
|
|
105
|
+
});
|
|
106
|
+
this.accessory.configureController(this.AdaptiveLightingController);
|
|
107
|
+
this.accessory.context.adaptiveLighting = true;
|
|
108
|
+
this.debugLog(
|
|
109
|
+
`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLighting: ${this.accessory.context.adaptiveLighting},` +
|
|
110
|
+
` adaptiveLightingShift: ${this.adaptiveLightingShift}`,
|
|
111
|
+
);
|
|
112
|
+
}*/
|
|
103
113
|
// Update Homekit
|
|
104
114
|
this.updateHomeKitCharacteristics();
|
|
105
115
|
// Start an update interval
|
|
@@ -109,7 +119,7 @@ class StripLight {
|
|
|
109
119
|
await this.refreshStatus();
|
|
110
120
|
});
|
|
111
121
|
// Watch for Bulb change events
|
|
112
|
-
// We put in a debounce of
|
|
122
|
+
// We put in a debounce of 1000ms so we don't make duplicate calls
|
|
113
123
|
this.doStripLightUpdate
|
|
114
124
|
.pipe((0, operators_1.tap)(() => {
|
|
115
125
|
this.stripLightUpdateInProgress = true;
|
|
@@ -119,12 +129,9 @@ class StripLight {
|
|
|
119
129
|
await this.pushChanges();
|
|
120
130
|
}
|
|
121
131
|
catch (e) {
|
|
122
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges`);
|
|
123
|
-
if (this.deviceLogging.includes('debug')) {
|
|
124
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges,`
|
|
125
|
-
+ ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
|
|
126
|
-
}
|
|
127
132
|
this.apiError(e);
|
|
133
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with ${this.device.connectionType} Connection,`
|
|
134
|
+
+ ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
|
|
128
135
|
}
|
|
129
136
|
this.stripLightUpdateInProgress = false;
|
|
130
137
|
// Refresh the status from the API
|
|
@@ -136,16 +143,23 @@ class StripLight {
|
|
|
136
143
|
});
|
|
137
144
|
});
|
|
138
145
|
}
|
|
146
|
+
/**
|
|
147
|
+
* Parse the device status from the SwitchBot api
|
|
148
|
+
*/
|
|
139
149
|
async parseStatus() {
|
|
140
|
-
if (this.
|
|
150
|
+
/*if (this.BLE && !this.SwitchToOpenAPI) {
|
|
151
|
+
await this.BLEparseStatus();
|
|
152
|
+
} else*/ if (this.OpenAPI && this.platform.config.credentials?.token) {
|
|
153
|
+
this.SwitchToOpenAPI = false;
|
|
141
154
|
await this.openAPIparseStatus();
|
|
142
155
|
}
|
|
143
156
|
else {
|
|
144
|
-
|
|
157
|
+
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
|
|
158
|
+
+ ` ${this.device.connectionType}, parseStatus will not happen.`);
|
|
145
159
|
}
|
|
146
160
|
}
|
|
147
161
|
async BLEparseStatus() {
|
|
148
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}
|
|
162
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
|
|
149
163
|
// State
|
|
150
164
|
switch (this.state) {
|
|
151
165
|
case 'on':
|
|
@@ -157,6 +171,7 @@ class StripLight {
|
|
|
157
171
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
|
|
158
172
|
}
|
|
159
173
|
async openAPIparseStatus() {
|
|
174
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
|
|
160
175
|
switch (this.power) {
|
|
161
176
|
case 'on':
|
|
162
177
|
this.On = true;
|
|
@@ -186,16 +201,22 @@ class StripLight {
|
|
|
186
201
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.Saturation}`);
|
|
187
202
|
}
|
|
188
203
|
}
|
|
204
|
+
/**
|
|
205
|
+
* Asks the SwitchBot API for the latest device information
|
|
206
|
+
*/
|
|
189
207
|
async refreshStatus() {
|
|
190
|
-
if (this.
|
|
191
|
-
|
|
208
|
+
/*if (this.BLE) {
|
|
209
|
+
await this.BLERefreshStatus();
|
|
210
|
+
} else*/ if (this.OpenAPI && this.platform.config.credentials?.token) {
|
|
211
|
+
await this.openAPIRefreshStatus();
|
|
192
212
|
}
|
|
193
213
|
else {
|
|
194
|
-
|
|
214
|
+
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
|
|
215
|
+
+ ` ${this.device.connectionType}, refreshStatus will not happen.`);
|
|
195
216
|
}
|
|
196
217
|
}
|
|
197
218
|
async BLERefreshStatus() {
|
|
198
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}
|
|
219
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
|
|
199
220
|
const switchbot = await this.platform.connectBLE();
|
|
200
221
|
// Convert to BLE Address
|
|
201
222
|
this.device.bleMac = this.device
|
|
@@ -256,208 +277,217 @@ class StripLight {
|
|
|
256
277
|
this.updateHomeKitCharacteristics();
|
|
257
278
|
}
|
|
258
279
|
else {
|
|
259
|
-
this.
|
|
260
|
-
if (this.platform.config.credentials?.token) {
|
|
261
|
-
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
262
|
-
this.SwitchToOpenAPI = true;
|
|
263
|
-
await this.openAPIRefreshStatus();
|
|
264
|
-
}
|
|
280
|
+
await this.BLEconnection(switchbot);
|
|
265
281
|
}
|
|
266
282
|
})
|
|
267
283
|
.catch(async (e) => {
|
|
268
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
|
|
269
|
-
if (this.deviceLogging.includes('debug')) {
|
|
270
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
|
|
271
|
-
+ ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
|
|
272
|
-
}
|
|
273
|
-
if (this.platform.config.credentials?.token) {
|
|
274
|
-
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
275
|
-
this.SwitchToOpenAPI = true;
|
|
276
|
-
await this.openAPIRefreshStatus();
|
|
277
|
-
}
|
|
278
284
|
this.apiError(e);
|
|
285
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}`
|
|
286
|
+
+ ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
|
|
287
|
+
await this.BLEconnection(switchbot);
|
|
279
288
|
});
|
|
280
289
|
}
|
|
281
290
|
else {
|
|
282
291
|
await this.BLEconnection(switchbot);
|
|
283
292
|
}
|
|
284
293
|
}
|
|
285
|
-
async
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
294
|
+
async openAPIRefreshStatus() {
|
|
295
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
|
|
296
|
+
try {
|
|
297
|
+
const t = Date.now();
|
|
298
|
+
const nonce = 'requestID';
|
|
299
|
+
const data = this.platform.config.credentials?.token + t + nonce;
|
|
300
|
+
const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
|
|
301
|
+
const sign = signTerm.toString('base64');
|
|
302
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
|
|
303
|
+
const options = {
|
|
304
|
+
hostname: settings_1.HostDomain,
|
|
305
|
+
port: 443,
|
|
306
|
+
path: `${settings_1.DevicePath}/${this.device.deviceId}/status`,
|
|
307
|
+
method: 'GET',
|
|
308
|
+
headers: {
|
|
309
|
+
Authorization: this.platform.config.credentials?.token,
|
|
310
|
+
sign: sign,
|
|
311
|
+
nonce: nonce,
|
|
312
|
+
t: t,
|
|
313
|
+
'Content-Type': 'application/json',
|
|
314
|
+
},
|
|
315
|
+
};
|
|
316
|
+
const req = https_1.default.request(options, (res) => {
|
|
317
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus statusCode: ${res.statusCode}`);
|
|
318
|
+
this.statusCode({ res });
|
|
319
|
+
let rawData = '';
|
|
320
|
+
res.on('data', (d) => {
|
|
321
|
+
rawData += d;
|
|
322
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
|
|
292
323
|
});
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
324
|
+
res.on('end', () => {
|
|
325
|
+
try {
|
|
326
|
+
this.deviceStatus = JSON.parse(rawData);
|
|
327
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus: ${(0, super_stringify_1.default)(this.deviceStatus)}`);
|
|
328
|
+
this.power = this.deviceStatus.body.power;
|
|
329
|
+
this.color = this.deviceStatus.body.color;
|
|
330
|
+
this.brightness = this.deviceStatus.body.brightness;
|
|
331
|
+
this.parseStatus();
|
|
332
|
+
this.updateHomeKitCharacteristics();
|
|
333
|
+
}
|
|
334
|
+
catch (e) {
|
|
335
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
|
|
336
|
+
}
|
|
337
|
+
});
|
|
338
|
+
});
|
|
339
|
+
req.on('error', (e) => {
|
|
340
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
|
|
341
|
+
});
|
|
342
|
+
req.end();
|
|
343
|
+
}
|
|
344
|
+
catch (e) {
|
|
345
|
+
this.apiError(e);
|
|
346
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
|
|
347
|
+
+ ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
|
|
301
348
|
}
|
|
302
349
|
}
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
350
|
+
/**
|
|
351
|
+
* Pushes the requested changes to the SwitchBot API
|
|
352
|
+
* deviceType commandType Command command parameter Description
|
|
353
|
+
* Strip Light - "command" "turnOn" "default" = set to ON state |
|
|
354
|
+
* Strip Light - "command" "turnOff" "default" = set to OFF state |
|
|
355
|
+
* Strip Light - "command" "toggle" "default" = toggle state |
|
|
356
|
+
* Strip Light - "command" "setBrightness" "`{1-100}`" = set brightness |
|
|
357
|
+
* Strip Light - "command" "setColor" "`"{0-255}:{0-255}:{0-255}"`" = set RGB color value |
|
|
358
|
+
*
|
|
359
|
+
*/
|
|
360
|
+
async pushChanges() {
|
|
361
|
+
/*if (this.BLE) {
|
|
362
|
+
await this.BLERefreshStatus();
|
|
363
|
+
} else*/ if (this.OpenAPI && this.platform.config.credentials?.token) {
|
|
364
|
+
await this.openAPIpushChanges();
|
|
309
365
|
}
|
|
366
|
+
else {
|
|
367
|
+
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
|
|
368
|
+
+ ` ${this.device.connectionType}, pushChanges will not happen.`);
|
|
369
|
+
}
|
|
370
|
+
(0, rxjs_1.interval)(5000)
|
|
371
|
+
.pipe((0, operators_1.take)(1))
|
|
372
|
+
.subscribe(async () => {
|
|
373
|
+
await this.refreshStatus();
|
|
374
|
+
});
|
|
310
375
|
}
|
|
311
|
-
async
|
|
312
|
-
|
|
313
|
-
|
|
376
|
+
async BLEpushChanges() {
|
|
377
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`);
|
|
378
|
+
if (this.On !== this.accessory.context.On) {
|
|
379
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges On: ${this.On} OnCached: ${this.accessory.context.On}`);
|
|
380
|
+
const switchbot = await this.platform.connectBLE();
|
|
381
|
+
// Convert to BLE Address
|
|
382
|
+
this.device.bleMac = this.device
|
|
383
|
+
.deviceId.match(/.{1,2}/g)
|
|
384
|
+
.join(':')
|
|
385
|
+
.toLowerCase();
|
|
386
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
387
|
+
switchbot
|
|
388
|
+
.discover({
|
|
389
|
+
model: 'r',
|
|
390
|
+
id: this.device.bleMac,
|
|
391
|
+
})
|
|
392
|
+
.then((device_list) => {
|
|
393
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
|
|
394
|
+
return this.turnOnOff(device_list);
|
|
395
|
+
})
|
|
396
|
+
.then(() => {
|
|
397
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
|
|
398
|
+
this.On = false;
|
|
399
|
+
})
|
|
400
|
+
.catch(async (e) => {
|
|
401
|
+
this.apiError(e);
|
|
402
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
|
|
403
|
+
+ ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
|
|
404
|
+
if (this.platform.config.credentials?.token && this.device.connectionType !== 'Both') {
|
|
405
|
+
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
406
|
+
await this.openAPIpushChanges();
|
|
407
|
+
}
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
else {
|
|
411
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges.` + `On: ${this.On}, `
|
|
412
|
+
+ `OnCached: ${this.accessory.context.On}`);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
async openAPIpushChanges() {
|
|
416
|
+
try {
|
|
417
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
|
|
418
|
+
if (this.On !== this.accessory.context.On) {
|
|
419
|
+
// Make Push On request to the API
|
|
314
420
|
const t = Date.now();
|
|
315
421
|
const nonce = 'requestID';
|
|
316
422
|
const data = this.platform.config.credentials?.token + t + nonce;
|
|
317
|
-
const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret)
|
|
423
|
+
const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret)
|
|
424
|
+
.update(Buffer.from(data, 'utf-8'))
|
|
425
|
+
.digest();
|
|
318
426
|
const sign = signTerm.toString('base64');
|
|
319
427
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
|
|
428
|
+
let command = '';
|
|
429
|
+
if (this.On) {
|
|
430
|
+
command = 'turnOn';
|
|
431
|
+
}
|
|
432
|
+
else {
|
|
433
|
+
command = 'turnOff';
|
|
434
|
+
}
|
|
435
|
+
const body = (0, super_stringify_1.default)({
|
|
436
|
+
'command': `${command}`,
|
|
437
|
+
'parameter': 'default',
|
|
438
|
+
'commandType': 'command',
|
|
439
|
+
});
|
|
440
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
|
|
320
441
|
const options = {
|
|
321
442
|
hostname: settings_1.HostDomain,
|
|
322
443
|
port: 443,
|
|
323
|
-
path: `${settings_1.DevicePath}/${this.device.deviceId}/
|
|
324
|
-
method: '
|
|
444
|
+
path: `${settings_1.DevicePath}/${this.device.deviceId}/commands`,
|
|
445
|
+
method: 'POST',
|
|
325
446
|
headers: {
|
|
326
|
-
Authorization: this.platform.config.credentials?.token,
|
|
327
|
-
sign: sign,
|
|
328
|
-
nonce: nonce,
|
|
329
|
-
t: t,
|
|
447
|
+
'Authorization': this.platform.config.credentials?.token,
|
|
448
|
+
'sign': sign,
|
|
449
|
+
'nonce': nonce,
|
|
450
|
+
't': t,
|
|
330
451
|
'Content-Type': 'application/json',
|
|
452
|
+
'Content-Length': body.length,
|
|
331
453
|
},
|
|
332
454
|
};
|
|
333
|
-
const req = https_1.default.request(options,
|
|
334
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
|
|
335
|
-
|
|
336
|
-
res.on('data',
|
|
337
|
-
rawData += d;
|
|
455
|
+
const req = https_1.default.request(options, res => {
|
|
456
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges statusCode: ${res.statusCode}`);
|
|
457
|
+
this.statusCode({ res });
|
|
458
|
+
res.on('data', d => {
|
|
338
459
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
|
|
339
460
|
});
|
|
340
|
-
res.on('end', () => {
|
|
341
|
-
try {
|
|
342
|
-
this.deviceStatus = JSON.parse(rawData);
|
|
343
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus: ${(0, super_stringify_1.default)(this.deviceStatus)}`);
|
|
344
|
-
this.power = this.deviceStatus.body.power;
|
|
345
|
-
this.color = this.deviceStatus.body.color;
|
|
346
|
-
this.brightness = this.deviceStatus.body.brightness;
|
|
347
|
-
this.parseStatus();
|
|
348
|
-
this.updateHomeKitCharacteristics();
|
|
349
|
-
}
|
|
350
|
-
catch (e) {
|
|
351
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
|
|
352
|
-
}
|
|
353
|
-
});
|
|
354
461
|
});
|
|
355
462
|
req.on('error', (e) => {
|
|
356
463
|
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
|
|
357
464
|
});
|
|
465
|
+
req.write(body);
|
|
358
466
|
req.end();
|
|
467
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges: ${(0, super_stringify_1.default)(req)}`);
|
|
359
468
|
}
|
|
360
|
-
|
|
361
|
-
this.
|
|
362
|
-
|
|
363
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
|
|
364
|
-
+ ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
|
|
365
|
-
}
|
|
366
|
-
this.apiError(e);
|
|
469
|
+
else {
|
|
470
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.` + `On: ${this.On}, `
|
|
471
|
+
+ `OnCached: ${this.accessory.context.On}`);
|
|
367
472
|
}
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
/**
|
|
374
|
-
* Pushes the requested changes to the SwitchBot API
|
|
375
|
-
* deviceType commandType Command command parameter Description
|
|
376
|
-
* Strip Light - "command" "turnOn" "default" = set to ON state |
|
|
377
|
-
* Strip Light - "command" "turnOff" "default" = set to OFF state |
|
|
378
|
-
* Strip Light - "command" "toggle" "default" = toggle state |
|
|
379
|
-
* Strip Light - "command" "setBrightness" "`{1-100}`" = set brightness |
|
|
380
|
-
* Strip Light - "command" "setColor" "`"{0-255}:{0-255}:{0-255}"`" = set RGB color value |
|
|
381
|
-
*
|
|
382
|
-
*/
|
|
383
|
-
async pushChanges() {
|
|
384
|
-
if (this.OpenAPI) {
|
|
385
|
-
try {
|
|
386
|
-
if (this.On !== this.accessory.context.On) {
|
|
387
|
-
// Make Push On request to the API
|
|
388
|
-
const t = Date.now();
|
|
389
|
-
const nonce = 'requestID';
|
|
390
|
-
const data = this.platform.config.credentials?.token + t + nonce;
|
|
391
|
-
const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret)
|
|
392
|
-
.update(Buffer.from(data, 'utf-8'))
|
|
393
|
-
.digest();
|
|
394
|
-
const sign = signTerm.toString('base64');
|
|
395
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
|
|
396
|
-
let command = '';
|
|
397
|
-
if (this.On) {
|
|
398
|
-
command = 'turnOn';
|
|
399
|
-
}
|
|
400
|
-
else {
|
|
401
|
-
command = 'turnOff';
|
|
402
|
-
}
|
|
403
|
-
const body = (0, super_stringify_1.default)({
|
|
404
|
-
'command': `${command}`,
|
|
405
|
-
'parameter': 'default',
|
|
406
|
-
'commandType': 'command',
|
|
407
|
-
});
|
|
408
|
-
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
|
|
409
|
-
const options = {
|
|
410
|
-
hostname: settings_1.HostDomain,
|
|
411
|
-
port: 443,
|
|
412
|
-
path: `${settings_1.DevicePath}/${this.device.deviceId}/commands`,
|
|
413
|
-
method: 'POST',
|
|
414
|
-
headers: {
|
|
415
|
-
'Authorization': this.platform.config.credentials?.token,
|
|
416
|
-
'sign': sign,
|
|
417
|
-
'nonce': nonce,
|
|
418
|
-
't': t,
|
|
419
|
-
'Content-Type': 'application/json',
|
|
420
|
-
'Content-Length': body.length,
|
|
421
|
-
},
|
|
422
|
-
};
|
|
423
|
-
const req = https_1.default.request(options, res => {
|
|
424
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
|
|
425
|
-
this.statusCode({ res });
|
|
426
|
-
res.on('data', d => {
|
|
427
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
|
|
428
|
-
});
|
|
429
|
-
});
|
|
430
|
-
req.on('error', (e) => {
|
|
431
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
|
|
432
|
-
});
|
|
433
|
-
req.write(body);
|
|
434
|
-
req.end();
|
|
435
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushchanges: ${(0, super_stringify_1.default)(req)}`);
|
|
436
|
-
}
|
|
437
|
-
// Push Brightness Update
|
|
438
|
-
if (this.On) {
|
|
439
|
-
await this.pushBrightnessChanges();
|
|
440
|
-
}
|
|
441
|
-
// Push Hue & Saturation Update
|
|
442
|
-
if (this.On) {
|
|
443
|
-
await this.pushHueSaturationChanges();
|
|
444
|
-
}
|
|
473
|
+
// Push Brightness Update
|
|
474
|
+
if (this.On) {
|
|
475
|
+
await this.pushBrightnessChanges();
|
|
445
476
|
}
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
|
|
450
|
-
+ ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
|
|
451
|
-
}
|
|
452
|
-
this.apiError(e);
|
|
477
|
+
// Push Hue & Saturation Update
|
|
478
|
+
if (this.On) {
|
|
479
|
+
await this.pushHueSaturationChanges();
|
|
453
480
|
}
|
|
454
481
|
}
|
|
455
|
-
|
|
456
|
-
this.
|
|
482
|
+
catch (e) {
|
|
483
|
+
this.apiError(e);
|
|
484
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
|
|
485
|
+
+ ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
|
|
457
486
|
}
|
|
458
487
|
}
|
|
459
488
|
async pushHueSaturationChanges() {
|
|
460
489
|
try {
|
|
490
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushHueSaturationChanges`);
|
|
461
491
|
if (this.Hue !== this.accessory.context.Hue || this.Saturation !== this.accessory.context.Saturation) {
|
|
462
492
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${(0, super_stringify_1.default)(this.Hue)}`);
|
|
463
493
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${(0, super_stringify_1.default)(this.Saturation)}`);
|
|
@@ -493,7 +523,7 @@ class StripLight {
|
|
|
493
523
|
},
|
|
494
524
|
};
|
|
495
525
|
const req = https_1.default.request(options, res => {
|
|
496
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
|
|
526
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushHueSaturationChanges statusCode: ${res.statusCode}`);
|
|
497
527
|
this.statusCode({ res });
|
|
498
528
|
res.on('data', d => {
|
|
499
529
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
|
|
@@ -507,22 +537,19 @@ class StripLight {
|
|
|
507
537
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushHueSaturationChanges: ${(0, super_stringify_1.default)(req)}`);
|
|
508
538
|
}
|
|
509
539
|
else {
|
|
510
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No
|
|
511
|
-
`
|
|
512
|
-
+ `SaturationCached: ${this.accessory.context.Saturation}`);
|
|
540
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushHueSaturationChanges. Hue: ${this.Hue}, `
|
|
541
|
+
+ `HueCached: ${this.accessory.context.Hue}, Saturation: ${this.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`);
|
|
513
542
|
}
|
|
514
543
|
}
|
|
515
544
|
catch (e) {
|
|
516
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushHueSaturationChanges with OpenAPI Connection`);
|
|
517
|
-
if (this.deviceLogging.includes('debug')) {
|
|
518
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushHueSaturationChanges with OpenAPI Connection,`
|
|
519
|
-
+ ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
|
|
520
|
-
}
|
|
521
545
|
this.apiError(e);
|
|
546
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushHueSaturationChanges with ${this.device.connectionType}`
|
|
547
|
+
+ ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
|
|
522
548
|
}
|
|
523
549
|
}
|
|
524
550
|
async pushBrightnessChanges() {
|
|
525
551
|
try {
|
|
552
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushBrightnessChanges`);
|
|
526
553
|
if (this.Brightness !== this.accessory.context.Brightness) {
|
|
527
554
|
// Make Push On request to the API
|
|
528
555
|
const t = Date.now();
|
|
@@ -554,7 +581,7 @@ class StripLight {
|
|
|
554
581
|
},
|
|
555
582
|
};
|
|
556
583
|
const req = https_1.default.request(options, res => {
|
|
557
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
|
|
584
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushBrightnessChanges statusCode: ${res.statusCode}`);
|
|
558
585
|
this.statusCode({ res });
|
|
559
586
|
res.on('data', d => {
|
|
560
587
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
|
|
@@ -568,19 +595,74 @@ class StripLight {
|
|
|
568
595
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushBrightnessChanges: ${(0, super_stringify_1.default)(req)}`);
|
|
569
596
|
}
|
|
570
597
|
else {
|
|
571
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No
|
|
598
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushBrightnessChanges.` + `Brightness: ${this.Brightness}, `
|
|
572
599
|
+ `BrightnessCached: ${this.accessory.context.Brightness}`);
|
|
573
600
|
}
|
|
574
601
|
}
|
|
575
602
|
catch (e) {
|
|
576
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushBrightnessChanges with OpenAPI Connection`);
|
|
577
|
-
if (this.deviceLogging.includes('debug')) {
|
|
578
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushBrightnessChanges with OpenAPI Connection,`
|
|
579
|
-
+ ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
|
|
580
|
-
}
|
|
581
603
|
this.apiError(e);
|
|
604
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushBrightnessChanges with ${this.device.connectionType}`
|
|
605
|
+
+ ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
|
|
582
606
|
}
|
|
583
607
|
}
|
|
608
|
+
/**
|
|
609
|
+
* Handle requests to set the value of the "On" characteristic
|
|
610
|
+
*/
|
|
611
|
+
async OnSet(value) {
|
|
612
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set On: ${value}`);
|
|
613
|
+
this.On = value;
|
|
614
|
+
this.doStripLightUpdate.next();
|
|
615
|
+
}
|
|
616
|
+
/**
|
|
617
|
+
* Handle requests to set the value of the "Brightness" characteristic
|
|
618
|
+
*/
|
|
619
|
+
async BrightnessSet(value) {
|
|
620
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Brightness: ${value}`);
|
|
621
|
+
this.Brightness = value;
|
|
622
|
+
this.doStripLightUpdate.next();
|
|
623
|
+
}
|
|
624
|
+
/**
|
|
625
|
+
* Handle requests to set the value of the "ColorTemperature" characteristic
|
|
626
|
+
*/
|
|
627
|
+
/*async ColorTemperatureSet(value: CharacteristicValue): Promise<void> {
|
|
628
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${value}`);
|
|
629
|
+
|
|
630
|
+
// Convert mired to kelvin to nearest 100 (SwitchBot seems to need this)
|
|
631
|
+
const kelvin = Math.round(1000000 / Number(value) / 100) * 100;
|
|
632
|
+
|
|
633
|
+
// Check and increase/decrease kelvin to range of device
|
|
634
|
+
const k = Math.min(Math.max(kelvin, this.minKelvin), this.maxKelvin);
|
|
635
|
+
|
|
636
|
+
if (!this.accessory.context.On || this.cacheKelvin === k) {
|
|
637
|
+
return;
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
// Updating the hue/sat to the corresponding values mimics native adaptive lighting
|
|
641
|
+
const hs = m2hs(value);
|
|
642
|
+
this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Hue, hs[0]);
|
|
643
|
+
this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Saturation, hs[1]);
|
|
644
|
+
|
|
645
|
+
this.ColorTemperature = value;
|
|
646
|
+
this.doColorBulbUpdate.next();
|
|
647
|
+
}*/
|
|
648
|
+
/**
|
|
649
|
+
* Handle requests to set the value of the "Hue" characteristic
|
|
650
|
+
*/
|
|
651
|
+
async HueSet(value) {
|
|
652
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Hue: ${value}`);
|
|
653
|
+
//this.lightBulbService.updateCharacteristic(this.platform.Characteristic.ColorTemperature, 140);
|
|
654
|
+
this.Hue = value;
|
|
655
|
+
this.doStripLightUpdate.next();
|
|
656
|
+
}
|
|
657
|
+
/**
|
|
658
|
+
* Handle requests to set the value of the "Saturation" characteristic
|
|
659
|
+
*/
|
|
660
|
+
async SaturationSet(value) {
|
|
661
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Saturation: ${value}`);
|
|
662
|
+
//this.lightBulbService.updateCharacteristic(this.platform.Characteristic.ColorTemperature, 140);
|
|
663
|
+
this.Saturation = value;
|
|
664
|
+
this.doStripLightUpdate.next();
|
|
665
|
+
}
|
|
584
666
|
async updateHomeKitCharacteristics() {
|
|
585
667
|
if (this.On === undefined) {
|
|
586
668
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
|
|
@@ -598,6 +680,14 @@ class StripLight {
|
|
|
598
680
|
this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Brightness, this.Brightness);
|
|
599
681
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Brightness: ${this.Brightness}`);
|
|
600
682
|
}
|
|
683
|
+
/*
|
|
684
|
+
if (this.ColorTemperature === undefined) {
|
|
685
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${this.ColorTemperature}`);
|
|
686
|
+
} else {
|
|
687
|
+
this.accessory.context.ColorTemperature = this.ColorTemperature;
|
|
688
|
+
this.lightBulbService.updateCharacteristic(this.platform.Characteristic.ColorTemperature, this.ColorTemperature);
|
|
689
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic ColorTemperature: ${this.ColorTemperature}`);
|
|
690
|
+
}*/
|
|
601
691
|
if (this.Hue === undefined) {
|
|
602
692
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.Hue}`);
|
|
603
693
|
}
|
|
@@ -615,12 +705,97 @@ class StripLight {
|
|
|
615
705
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Saturation: ${this.Saturation}`);
|
|
616
706
|
}
|
|
617
707
|
}
|
|
618
|
-
|
|
619
|
-
this.
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
708
|
+
async getCustomBLEAddress(switchbot) {
|
|
709
|
+
if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
|
|
710
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} customBLEaddress: ${this.device.customBLEaddress}`);
|
|
711
|
+
(async () => {
|
|
712
|
+
// Start to monitor advertisement packets
|
|
713
|
+
await switchbot.startScan({
|
|
714
|
+
model: 'r',
|
|
715
|
+
});
|
|
716
|
+
// Set an event handler
|
|
717
|
+
switchbot.onadvertisement = (ad) => {
|
|
718
|
+
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${(0, super_stringify_1.default)(ad, null, ' ')}`);
|
|
719
|
+
};
|
|
720
|
+
await switchbot.wait(10000);
|
|
721
|
+
// Stop to monitor
|
|
722
|
+
switchbot.stopScan();
|
|
723
|
+
})();
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
async BLEconnection(switchbot) {
|
|
727
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
|
|
728
|
+
if (this.platform.config.credentials?.token && this.device.connectionType !== 'Both') {
|
|
729
|
+
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
730
|
+
this.SwitchToOpenAPI = true;
|
|
731
|
+
await this.openAPIRefreshStatus();
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
async turnOnOff(device_list) {
|
|
735
|
+
return await this.retry({
|
|
736
|
+
max: await this.maxRetry(),
|
|
737
|
+
fn: () => {
|
|
738
|
+
if (this.On) {
|
|
739
|
+
return device_list[0].turnOn({ id: this.device.bleMac });
|
|
740
|
+
}
|
|
741
|
+
else {
|
|
742
|
+
return device_list[0].turnOff({ id: this.device.bleMac });
|
|
743
|
+
}
|
|
744
|
+
},
|
|
745
|
+
});
|
|
746
|
+
}
|
|
747
|
+
async retry({ max, fn }) {
|
|
748
|
+
return fn().catch(async (err) => {
|
|
749
|
+
if (max === 0) {
|
|
750
|
+
throw err;
|
|
751
|
+
}
|
|
752
|
+
this.infoLog(err);
|
|
753
|
+
this.infoLog('Retrying');
|
|
754
|
+
await this.switchbot.wait(1000);
|
|
755
|
+
return this.retry({ max: max - 1, fn });
|
|
756
|
+
});
|
|
757
|
+
}
|
|
758
|
+
async maxRetry() {
|
|
759
|
+
let maxRetry;
|
|
760
|
+
if (this.device.bot?.maxRetry) {
|
|
761
|
+
maxRetry = this.device.bot?.maxRetry;
|
|
762
|
+
}
|
|
763
|
+
else {
|
|
764
|
+
maxRetry = 5;
|
|
765
|
+
}
|
|
766
|
+
return maxRetry;
|
|
767
|
+
}
|
|
768
|
+
minStep(device) {
|
|
769
|
+
if (device.colorbulb?.set_minStep) {
|
|
770
|
+
this.set_minStep = device.colorbulb?.set_minStep;
|
|
771
|
+
}
|
|
772
|
+
else {
|
|
773
|
+
this.set_minStep = 1;
|
|
774
|
+
}
|
|
775
|
+
return this.set_minStep;
|
|
776
|
+
}
|
|
777
|
+
/*async adaptiveLighting(device: device & devicesConfig): Promise<void> {
|
|
778
|
+
if (device.colorbulb?.adaptiveLightingShift) {
|
|
779
|
+
this.adaptiveLightingShift = device.colorbulb.adaptiveLightingShift;
|
|
780
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
|
|
781
|
+
} else {
|
|
782
|
+
this.adaptiveLightingShift = 0;
|
|
783
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
|
|
784
|
+
}
|
|
785
|
+
}*/
|
|
786
|
+
async scan(device) {
|
|
787
|
+
if (device.scanDuration) {
|
|
788
|
+
this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
|
|
789
|
+
if (this.BLE) {
|
|
790
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
else {
|
|
794
|
+
this.scanDuration = this.accessory.context.scanDuration = 1;
|
|
795
|
+
if (this.BLE) {
|
|
796
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
|
|
797
|
+
}
|
|
798
|
+
}
|
|
624
799
|
}
|
|
625
800
|
async statusCode({ res }) {
|
|
626
801
|
switch (res.statusCode) {
|
|
@@ -660,66 +835,64 @@ class StripLight {
|
|
|
660
835
|
if (this.device.offline) {
|
|
661
836
|
this.On = false;
|
|
662
837
|
this.lightBulbService.getCharacteristic(this.platform.Characteristic.On).updateValue(this.On);
|
|
838
|
+
await this.updateHomeKitCharacteristics();
|
|
663
839
|
}
|
|
664
840
|
}
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
this.
|
|
670
|
-
this.On = value;
|
|
671
|
-
this.doStripLightUpdate.next();
|
|
672
|
-
}
|
|
673
|
-
/**
|
|
674
|
-
* Handle requests to set the value of the "Brightness" characteristic
|
|
675
|
-
*/
|
|
676
|
-
async BrightnessSet(value) {
|
|
677
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${value}`);
|
|
678
|
-
this.Brightness = value;
|
|
679
|
-
this.doStripLightUpdate.next();
|
|
680
|
-
}
|
|
681
|
-
/**
|
|
682
|
-
* Handle requests to set the value of the "Hue" characteristic
|
|
683
|
-
*/
|
|
684
|
-
async HueSet(value) {
|
|
685
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${value}`);
|
|
686
|
-
this.Hue = value;
|
|
687
|
-
this.doStripLightUpdate.next();
|
|
841
|
+
apiError(e) {
|
|
842
|
+
this.lightBulbService.updateCharacteristic(this.platform.Characteristic.On, e);
|
|
843
|
+
this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Hue, e);
|
|
844
|
+
this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Brightness, e);
|
|
845
|
+
this.lightBulbService.updateCharacteristic(this.platform.Characteristic.Saturation, e);
|
|
688
846
|
}
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}
|
|
694
|
-
this.
|
|
695
|
-
|
|
847
|
+
FirmwareRevision(accessory, device) {
|
|
848
|
+
let FirmwareRevision;
|
|
849
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
850
|
+
+ `accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
|
|
851
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
|
|
852
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
|
|
853
|
+
if (accessory.context.FirmwareRevision) {
|
|
854
|
+
FirmwareRevision = accessory.context.FirmwareRevision;
|
|
855
|
+
}
|
|
856
|
+
else if (device.firmware) {
|
|
857
|
+
FirmwareRevision = device.firmware;
|
|
858
|
+
}
|
|
859
|
+
else {
|
|
860
|
+
FirmwareRevision = this.platform.version;
|
|
861
|
+
}
|
|
862
|
+
return FirmwareRevision;
|
|
696
863
|
}
|
|
697
|
-
async
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
config = device.colorbulb;
|
|
864
|
+
async context() {
|
|
865
|
+
if (this.On === undefined) {
|
|
866
|
+
this.On = false;
|
|
701
867
|
}
|
|
702
|
-
|
|
703
|
-
|
|
868
|
+
else {
|
|
869
|
+
this.On = this.accessory.context.On;
|
|
704
870
|
}
|
|
705
|
-
if (
|
|
706
|
-
|
|
871
|
+
if (this.Hue === undefined) {
|
|
872
|
+
this.Hue = 0;
|
|
707
873
|
}
|
|
708
|
-
|
|
709
|
-
|
|
874
|
+
else {
|
|
875
|
+
this.Hue = this.accessory.context.Hue;
|
|
710
876
|
}
|
|
711
|
-
if (
|
|
712
|
-
|
|
877
|
+
if (this.Brightness === undefined) {
|
|
878
|
+
this.Brightness = 0;
|
|
713
879
|
}
|
|
714
|
-
|
|
715
|
-
|
|
880
|
+
else {
|
|
881
|
+
this.Brightness = this.accessory.context.Brightness;
|
|
716
882
|
}
|
|
717
|
-
if (
|
|
718
|
-
|
|
883
|
+
if (this.Brightness === undefined) {
|
|
884
|
+
this.Saturation = 0;
|
|
719
885
|
}
|
|
720
|
-
|
|
721
|
-
this.
|
|
886
|
+
else {
|
|
887
|
+
this.Saturation = this.accessory.context.Saturation;
|
|
722
888
|
}
|
|
889
|
+
/*if (this.ColorTemperature === undefined) {
|
|
890
|
+
this.ColorTemperature = 140;
|
|
891
|
+
} else {
|
|
892
|
+
this.ColorTemperature = this.accessory.context.ColorTemperature;
|
|
893
|
+
}*/
|
|
894
|
+
this.minKelvin = 2000;
|
|
895
|
+
this.maxKelvin = 9000;
|
|
723
896
|
}
|
|
724
897
|
async refreshRate(device) {
|
|
725
898
|
if (device.refreshRate) {
|
|
@@ -731,27 +904,28 @@ class StripLight {
|
|
|
731
904
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
|
|
732
905
|
}
|
|
733
906
|
}
|
|
734
|
-
async
|
|
735
|
-
|
|
736
|
-
|
|
907
|
+
async config(device) {
|
|
908
|
+
let config = {};
|
|
909
|
+
if (device.colorbulb) {
|
|
910
|
+
config = device.colorbulb;
|
|
737
911
|
}
|
|
738
|
-
|
|
739
|
-
|
|
912
|
+
if (device.connectionType !== undefined) {
|
|
913
|
+
config['connectionType'] = device.connectionType;
|
|
740
914
|
}
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
async scan(device) {
|
|
744
|
-
if (device.scanDuration) {
|
|
745
|
-
this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
|
|
746
|
-
if (device.ble) {
|
|
747
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
|
|
748
|
-
}
|
|
915
|
+
if (device.logging !== undefined) {
|
|
916
|
+
config['logging'] = device.logging;
|
|
749
917
|
}
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
918
|
+
if (device.refreshRate !== undefined) {
|
|
919
|
+
config['refreshRate'] = device.refreshRate;
|
|
920
|
+
}
|
|
921
|
+
if (device.scanDuration !== undefined) {
|
|
922
|
+
config['scanDuration'] = device.scanDuration;
|
|
923
|
+
}
|
|
924
|
+
if (device.offline !== undefined) {
|
|
925
|
+
config['offline'] = device.offline;
|
|
926
|
+
}
|
|
927
|
+
if (Object.entries(config).length !== 0) {
|
|
928
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${(0, super_stringify_1.default)(config)}`);
|
|
755
929
|
}
|
|
756
930
|
}
|
|
757
931
|
async logs(device) {
|
|
@@ -772,32 +946,6 @@ class StripLight {
|
|
|
772
946
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
|
|
773
947
|
}
|
|
774
948
|
}
|
|
775
|
-
FirmwareRevision(accessory, device) {
|
|
776
|
-
let FirmwareRevision;
|
|
777
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
778
|
-
+ `accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
|
|
779
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
|
|
780
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
|
|
781
|
-
if (accessory.context.FirmwareRevision) {
|
|
782
|
-
FirmwareRevision = accessory.context.FirmwareRevision;
|
|
783
|
-
}
|
|
784
|
-
else if (device.firmware) {
|
|
785
|
-
FirmwareRevision = device.firmware;
|
|
786
|
-
}
|
|
787
|
-
else {
|
|
788
|
-
FirmwareRevision = this.platform.version;
|
|
789
|
-
}
|
|
790
|
-
return FirmwareRevision;
|
|
791
|
-
}
|
|
792
|
-
minStep(device) {
|
|
793
|
-
if (device.colorbulb?.set_minStep) {
|
|
794
|
-
this.set_minStep = device.colorbulb?.set_minStep;
|
|
795
|
-
}
|
|
796
|
-
else {
|
|
797
|
-
this.set_minStep = 1;
|
|
798
|
-
}
|
|
799
|
-
return this.set_minStep;
|
|
800
|
-
}
|
|
801
949
|
/**
|
|
802
950
|
* Logging for Device
|
|
803
951
|
*/
|