@switchbot/homebridge-switchbot 1.9.0 → 1.10.1
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/.eslintrc +14 -45
- package/.github/ISSUE_TEMPLATE/bug-report.yml +3 -3
- package/.github/ISSUE_TEMPLATE/feature-request.yml +2 -2
- package/.github/ISSUE_TEMPLATE/support-request.yml +3 -3
- package/.github/labeler.yml +1 -1
- package/.github/workflows/codeql-analysis.yml +29 -29
- package/.github/workflows/nodejs-beta.yml +1 -1
- package/.github/workflows/prerelease-beta.js +1 -1
- package/.github/workflows/stale.yml +1 -1
- package/.prettierrc +9 -0
- package/.vscode/extensions.json +2 -4
- package/.vscode/settings.json +13 -1
- package/CHANGELOG.md +16 -0
- package/README.md +100 -99
- package/SECURITY.md +2 -2
- package/config.schema.json +8 -0
- package/dist/devices/bots.d.ts +2 -2
- package/dist/devices/bots.d.ts.map +1 -1
- package/dist/devices/bots.js +91 -86
- package/dist/devices/bots.js.map +1 -1
- package/dist/devices/colorbulb.d.ts +15 -15
- package/dist/devices/colorbulb.d.ts.map +1 -1
- package/dist/devices/colorbulb.js +60 -71
- package/dist/devices/colorbulb.js.map +1 -1
- package/dist/devices/contact.d.ts +2 -2
- package/dist/devices/contact.d.ts.map +1 -1
- package/dist/devices/contact.js +33 -31
- package/dist/devices/contact.js.map +1 -1
- package/dist/devices/curtains.d.ts +2 -2
- package/dist/devices/curtains.d.ts.map +1 -1
- package/dist/devices/curtains.js +71 -71
- package/dist/devices/curtains.js.map +1 -1
- package/dist/devices/humidifiers.d.ts +2 -2
- package/dist/devices/humidifiers.d.ts.map +1 -1
- package/dist/devices/humidifiers.js +74 -76
- package/dist/devices/humidifiers.js.map +1 -1
- package/dist/devices/meters.d.ts +2 -2
- package/dist/devices/meters.d.ts.map +1 -1
- package/dist/devices/meters.js +29 -27
- package/dist/devices/meters.js.map +1 -1
- package/dist/devices/motion.d.ts +2 -2
- package/dist/devices/motion.d.ts.map +1 -1
- package/dist/devices/motion.js +34 -32
- package/dist/devices/motion.js.map +1 -1
- package/dist/devices/plugs.d.ts +7 -7
- package/dist/devices/plugs.d.ts.map +1 -1
- package/dist/devices/plugs.js +18 -23
- package/dist/devices/plugs.js.map +1 -1
- package/dist/irdevices/airconditioners.d.ts +5 -2
- package/dist/irdevices/airconditioners.d.ts.map +1 -1
- package/dist/irdevices/airconditioners.js +61 -56
- package/dist/irdevices/airconditioners.js.map +1 -1
- package/dist/irdevices/airpurifiers.d.ts +2 -2
- package/dist/irdevices/airpurifiers.d.ts.map +1 -1
- package/dist/irdevices/airpurifiers.js +12 -17
- package/dist/irdevices/airpurifiers.js.map +1 -1
- package/dist/irdevices/cameras.d.ts +2 -2
- package/dist/irdevices/cameras.d.ts.map +1 -1
- package/dist/irdevices/cameras.js +10 -13
- package/dist/irdevices/cameras.js.map +1 -1
- package/dist/irdevices/fans.d.ts +2 -2
- package/dist/irdevices/fans.d.ts.map +1 -1
- package/dist/irdevices/fans.js +15 -17
- package/dist/irdevices/fans.js.map +1 -1
- package/dist/irdevices/lights.d.ts +2 -2
- package/dist/irdevices/lights.d.ts.map +1 -1
- package/dist/irdevices/lights.js +10 -13
- package/dist/irdevices/lights.js.map +1 -1
- package/dist/irdevices/others.d.ts +2 -2
- package/dist/irdevices/others.d.ts.map +1 -1
- package/dist/irdevices/others.js +10 -13
- package/dist/irdevices/others.js.map +1 -1
- package/dist/irdevices/tvs.d.ts +2 -2
- package/dist/irdevices/tvs.d.ts.map +1 -1
- package/dist/irdevices/tvs.js +22 -30
- package/dist/irdevices/tvs.js.map +1 -1
- package/dist/irdevices/vacuumcleaners.d.ts +2 -2
- package/dist/irdevices/vacuumcleaners.d.ts.map +1 -1
- package/dist/irdevices/vacuumcleaners.js +11 -13
- package/dist/irdevices/vacuumcleaners.js.map +1 -1
- package/dist/irdevices/waterheaters.d.ts +2 -2
- package/dist/irdevices/waterheaters.d.ts.map +1 -1
- package/dist/irdevices/waterheaters.js +10 -13
- package/dist/irdevices/waterheaters.js.map +1 -1
- package/dist/platform.d.ts +3 -3
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +13 -14
- package/dist/platform.js.map +1 -1
- package/dist/settings.d.ts +1 -0
- package/dist/settings.d.ts.map +1 -1
- package/dist/settings.js.map +1 -1
- package/homebridge-ui/public/index.html +231 -245
- package/homebridge-ui/server.js +2 -4
- package/nodemon.json +2 -4
- package/package.json +15 -9
- package/src/devices/bots.ts +217 -186
- package/src/devices/colorbulb.ts +90 -84
- package/src/devices/contact.ts +85 -78
- package/src/devices/curtains.ts +152 -135
- package/src/devices/humidifiers.ts +162 -137
- package/src/devices/meters.ts +76 -73
- package/src/devices/motion.ts +87 -78
- package/src/devices/plugs.ts +27 -31
- package/src/irdevices/airconditioners.ts +91 -59
- package/src/irdevices/airpurifiers.ts +22 -23
- package/src/irdevices/cameras.ts +18 -19
- package/src/irdevices/fans.ts +27 -25
- package/src/irdevices/lights.ts +18 -20
- package/src/irdevices/others.ts +18 -19
- package/src/irdevices/tvs.ts +33 -41
- package/src/irdevices/vacuumcleaners.ts +19 -19
- package/src/irdevices/waterheaters.ts +21 -31
- package/src/platform.ts +32 -33
- package/src/settings.ts +9 -19
- package/tsconfig.json +5 -14
- package/.github/PULL_REQUEST_TEMPLATE/pull_requests.md +0 -27
package/src/devices/curtains.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
|
|
2
1
|
import { AxiosResponse } from 'axios';
|
|
3
2
|
import { interval, Subject } from 'rxjs';
|
|
4
3
|
import { SwitchBotPlatform } from '../platform';
|
|
@@ -54,11 +53,7 @@ export class Curtain {
|
|
|
54
53
|
curtainUpdateInProgress!: boolean;
|
|
55
54
|
doCurtainUpdate!: Subject<void>;
|
|
56
55
|
|
|
57
|
-
constructor(
|
|
58
|
-
private readonly platform: SwitchBotPlatform,
|
|
59
|
-
private accessory: PlatformAccessory,
|
|
60
|
-
public device: device & devicesConfig,
|
|
61
|
-
) {
|
|
56
|
+
constructor(private readonly platform: SwitchBotPlatform, private accessory: PlatformAccessory, public device: device & devicesConfig) {
|
|
62
57
|
// default placeholders
|
|
63
58
|
this.logs(device);
|
|
64
59
|
this.scan(device);
|
|
@@ -86,9 +81,8 @@ export class Curtain {
|
|
|
86
81
|
|
|
87
82
|
// get the WindowCovering service if it exists, otherwise create a new WindowCovering service
|
|
88
83
|
// you can create multiple services for each accessory
|
|
89
|
-
(this.service =
|
|
90
|
-
|
|
91
|
-
accessory.addService(this.platform.Service.WindowCovering)), `${device.deviceName} ${device.deviceType}`;
|
|
84
|
+
(this.service = accessory.getService(this.platform.Service.WindowCovering) || accessory.addService(this.platform.Service.WindowCovering)),
|
|
85
|
+
`${device.deviceName} ${device.deviceType}`;
|
|
92
86
|
|
|
93
87
|
// To avoid "Cannot add a Service with the same UUID another Service without also defining a unique 'subtype' property." error,
|
|
94
88
|
// when creating multiple services of the same type, you need to use the following syntax to specify a name and subtype id:
|
|
@@ -132,11 +126,10 @@ export class Curtain {
|
|
|
132
126
|
} else if (!this.lightSensorService) {
|
|
133
127
|
this.debugLog(`Curtain: ${accessory.displayName} Add Light Sensor Service`);
|
|
134
128
|
(this.lightSensorService =
|
|
135
|
-
this.accessory.getService(this.platform.Service.LightSensor) ||
|
|
136
|
-
|
|
129
|
+
this.accessory.getService(this.platform.Service.LightSensor) || this.accessory.addService(this.platform.Service.LightSensor)),
|
|
130
|
+
`${accessory.displayName} Light Sensor`;
|
|
137
131
|
|
|
138
132
|
this.lightSensorService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Light Sensor`);
|
|
139
|
-
|
|
140
133
|
} else {
|
|
141
134
|
this.debugLog(`Curtain: ${accessory.displayName} Light Sensor Service Not Added`);
|
|
142
135
|
}
|
|
@@ -148,17 +141,14 @@ export class Curtain {
|
|
|
148
141
|
accessory.removeService(this.batteryService!);
|
|
149
142
|
} else if (device.ble && !this.batteryService) {
|
|
150
143
|
this.debugLog(`Curtain: ${accessory.displayName} Add Battery Service`);
|
|
151
|
-
(this.batteryService =
|
|
152
|
-
|
|
153
|
-
this.accessory.addService(this.platform.Service.Battery)), `${accessory.displayName} Battery`;
|
|
144
|
+
(this.batteryService = this.accessory.getService(this.platform.Service.Battery) || this.accessory.addService(this.platform.Service.Battery)),
|
|
145
|
+
`${accessory.displayName} Battery`;
|
|
154
146
|
|
|
155
147
|
this.batteryService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Battery`);
|
|
156
|
-
|
|
157
148
|
} else {
|
|
158
149
|
this.debugLog(`Curtain: ${accessory.displayName} Battery Service Not Added`);
|
|
159
150
|
}
|
|
160
151
|
|
|
161
|
-
|
|
162
152
|
// Update Homekit
|
|
163
153
|
this.updateHomeKitCharacteristics();
|
|
164
154
|
|
|
@@ -180,7 +170,6 @@ export class Curtain {
|
|
|
180
170
|
this.refreshStatus();
|
|
181
171
|
});
|
|
182
172
|
|
|
183
|
-
|
|
184
173
|
// Watch for Curtain change events
|
|
185
174
|
// We put in a debounce of 100ms so we don't make duplicate calls
|
|
186
175
|
this.doCurtainUpdate
|
|
@@ -196,12 +185,10 @@ export class Curtain {
|
|
|
196
185
|
} catch (e: any) {
|
|
197
186
|
this.errorLog(`Curtain: ${this.accessory.displayName} failed pushChanges`);
|
|
198
187
|
if (this.deviceLogging === 'debug') {
|
|
199
|
-
this.errorLog(`Curtain: ${this.accessory.displayName} failed pushChanges,`
|
|
200
|
-
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
188
|
+
this.errorLog(`Curtain: ${this.accessory.displayName} failed pushChanges,` + ` Error Message: ${JSON.stringify(e.message)}`);
|
|
201
189
|
}
|
|
202
190
|
if (this.platform.debugMode) {
|
|
203
|
-
this.errorLog(`Curtain: ${this.accessory.displayName} failed pushChanges,`
|
|
204
|
-
+ ` Error: ${JSON.stringify(e)}`);
|
|
191
|
+
this.errorLog(`Curtain: ${this.accessory.displayName} failed pushChanges,` + ` Error: ${JSON.stringify(e)}`);
|
|
205
192
|
}
|
|
206
193
|
this.apiError(e);
|
|
207
194
|
}
|
|
@@ -251,8 +238,10 @@ export class Curtain {
|
|
|
251
238
|
this.debugLog(`Curtain: ${this.accessory.displayName} Stopped`);
|
|
252
239
|
}
|
|
253
240
|
}
|
|
254
|
-
this.debugLog(
|
|
255
|
-
|
|
241
|
+
this.debugLog(
|
|
242
|
+
`Curtain: ${this.accessory.displayName} CurrentPosition: ${this.CurrentPosition},` +
|
|
243
|
+
` TargetPosition: ${this.TargetPosition}, PositionState: ${this.PositionState},`,
|
|
244
|
+
);
|
|
256
245
|
|
|
257
246
|
if (!this.device.curtain?.hide_lightsensor) {
|
|
258
247
|
this.set_minLux = this.minLux();
|
|
@@ -266,36 +255,38 @@ export class Curtain {
|
|
|
266
255
|
this.debugLog(`Curtain: ${this.accessory.displayName} LightLevel: ${this.lightLevel}`);
|
|
267
256
|
break;
|
|
268
257
|
case 2:
|
|
269
|
-
this.CurrentAmbientLightLevel = (
|
|
270
|
-
this.debugLog(
|
|
271
|
-
|
|
258
|
+
this.CurrentAmbientLightLevel = (this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels;
|
|
259
|
+
this.debugLog(
|
|
260
|
+
`Curtain: ${this.accessory.displayName} LightLevel: ${this.lightLevel},` +
|
|
261
|
+
` Calculation: ${(this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels}`,
|
|
262
|
+
);
|
|
272
263
|
break;
|
|
273
264
|
case 3:
|
|
274
|
-
this.CurrentAmbientLightLevel = ((
|
|
265
|
+
this.CurrentAmbientLightLevel = ((this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels) * 2;
|
|
275
266
|
this.debugLog(`Curtain: ${this.accessory.displayName} LightLevel: ${this.lightLevel}`);
|
|
276
267
|
break;
|
|
277
268
|
case 4:
|
|
278
|
-
this.CurrentAmbientLightLevel = ((
|
|
269
|
+
this.CurrentAmbientLightLevel = ((this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels) * 3;
|
|
279
270
|
this.debugLog(`Curtain: ${this.accessory.displayName} LightLevel: ${this.lightLevel}`);
|
|
280
271
|
break;
|
|
281
272
|
case 5:
|
|
282
|
-
this.CurrentAmbientLightLevel = ((
|
|
273
|
+
this.CurrentAmbientLightLevel = ((this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels) * 4;
|
|
283
274
|
this.debugLog(`Curtain: ${this.accessory.displayName} LightLevel: ${this.lightLevel}`);
|
|
284
275
|
break;
|
|
285
276
|
case 6:
|
|
286
|
-
this.CurrentAmbientLightLevel = ((
|
|
277
|
+
this.CurrentAmbientLightLevel = ((this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels) * 5;
|
|
287
278
|
this.debugLog(`Curtain: ${this.accessory.displayName} LightLevel: ${this.lightLevel}`);
|
|
288
279
|
break;
|
|
289
280
|
case 7:
|
|
290
|
-
this.CurrentAmbientLightLevel = ((
|
|
281
|
+
this.CurrentAmbientLightLevel = ((this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels) * 6;
|
|
291
282
|
this.debugLog(`Curtain: ${this.accessory.displayName} LightLevel: ${this.lightLevel}`);
|
|
292
283
|
break;
|
|
293
284
|
case 8:
|
|
294
|
-
this.CurrentAmbientLightLevel = ((
|
|
285
|
+
this.CurrentAmbientLightLevel = ((this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels) * 7;
|
|
295
286
|
this.debugLog(`Curtain: ${this.accessory.displayName} LightLevel: ${this.lightLevel}`);
|
|
296
287
|
break;
|
|
297
288
|
case 9:
|
|
298
|
-
this.CurrentAmbientLightLevel = ((
|
|
289
|
+
this.CurrentAmbientLightLevel = ((this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels) * 8;
|
|
299
290
|
this.debugLog(`Curtain: ${this.accessory.displayName} LightLevel: ${this.lightLevel}`);
|
|
300
291
|
break;
|
|
301
292
|
case 10:
|
|
@@ -303,8 +294,9 @@ export class Curtain {
|
|
|
303
294
|
this.CurrentAmbientLightLevel = this.set_maxLux;
|
|
304
295
|
this.debugLog();
|
|
305
296
|
}
|
|
306
|
-
this.debugLog(
|
|
307
|
-
+ ` CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}
|
|
297
|
+
this.debugLog(
|
|
298
|
+
`Curtain: ${this.accessory.displayName} LightLevel: ${this.lightLevel},` + ` CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`,
|
|
299
|
+
);
|
|
308
300
|
}
|
|
309
301
|
// Battery
|
|
310
302
|
this.BatteryLevel = Number(this.battery);
|
|
@@ -351,8 +343,10 @@ export class Curtain {
|
|
|
351
343
|
this.debugLog(`Curtain: ${this.accessory.displayName} Stopped`);
|
|
352
344
|
}
|
|
353
345
|
}
|
|
354
|
-
this.debugLog(
|
|
355
|
-
|
|
346
|
+
this.debugLog(
|
|
347
|
+
`Curtain: ${this.accessory.displayName} CurrentPosition: ${this.CurrentPosition},` +
|
|
348
|
+
` TargetPosition: ${this.TargetPosition}, PositionState: ${this.PositionState},`,
|
|
349
|
+
);
|
|
356
350
|
|
|
357
351
|
if (!this.device.curtain?.hide_lightsensor) {
|
|
358
352
|
this.set_minLux = this.minLux();
|
|
@@ -383,66 +377,75 @@ export class Curtain {
|
|
|
383
377
|
this.debugLog(`Curtain: ${this.accessory.displayName} BLE refreshStatus`);
|
|
384
378
|
const switchbot = await this.platform.connectBLE();
|
|
385
379
|
// Convert to BLE Address
|
|
386
|
-
this.device.bleMac =
|
|
380
|
+
this.device.bleMac = this.device
|
|
381
|
+
.deviceId!.match(/.{1,2}/g)!
|
|
382
|
+
.join(':')
|
|
383
|
+
.toLowerCase();
|
|
387
384
|
this.debugLog(`Curtain: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
388
385
|
// Start to monitor advertisement packets
|
|
389
386
|
if (switchbot !== false) {
|
|
390
|
-
switchbot
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
387
|
+
switchbot
|
|
388
|
+
.startScan({
|
|
389
|
+
model: 'c',
|
|
390
|
+
id: this.device.bleMac,
|
|
391
|
+
})
|
|
392
|
+
.then(() => {
|
|
393
|
+
// Set an event hander
|
|
394
|
+
switchbot.onadvertisement = (ad: any) => {
|
|
395
|
+
this.serviceData = ad.serviceData;
|
|
396
|
+
this.calibration = ad.serviceData.calibration;
|
|
397
|
+
this.battery = ad.serviceData.battery;
|
|
398
|
+
this.position = ad.serviceData.position;
|
|
399
|
+
this.lightLevel = ad.serviceData.lightLevel;
|
|
400
|
+
this.debugLog(`Curtain: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
401
|
+
this.debugLog(
|
|
402
|
+
`Curtain: ${this.accessory.displayName} calibration: ${ad.serviceData.calibration}, ` +
|
|
403
|
+
`position: ${ad.serviceData.position}, lightLevel: ${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}`,
|
|
404
|
+
);
|
|
405
|
+
|
|
406
|
+
if (this.serviceData) {
|
|
407
|
+
this.connected = true;
|
|
408
|
+
this.debugLog(`Curtain: ${this.accessory.displayName} connected: ${this.connected}`);
|
|
409
|
+
} else {
|
|
410
|
+
this.connected = false;
|
|
411
|
+
this.debugLog(`Curtain: ${this.accessory.displayName} connected: ${this.connected}`);
|
|
412
|
+
}
|
|
413
|
+
};
|
|
414
|
+
// Wait 2 seconds
|
|
415
|
+
return switchbot.wait(this.scanDuration * 1000);
|
|
416
|
+
})
|
|
417
|
+
.then(async () => {
|
|
418
|
+
// Stop to monitor
|
|
419
|
+
switchbot.stopScan();
|
|
420
|
+
if (this.connected) {
|
|
421
|
+
this.parseStatus();
|
|
422
|
+
this.updateHomeKitCharacteristics();
|
|
408
423
|
} else {
|
|
409
|
-
this.
|
|
410
|
-
|
|
424
|
+
this.errorLog(`Curtain: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
|
|
425
|
+
if (this.platform.config.credentials?.openToken) {
|
|
426
|
+
this.warnLog(`Curtain: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
427
|
+
this.SwitchToOpenAPI = true;
|
|
428
|
+
await this.openAPIRefreshStatus();
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
})
|
|
432
|
+
.catch(async (e: any) => {
|
|
433
|
+
this.errorLog(`Curtain: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
|
|
434
|
+
if (this.deviceLogging === 'debug') {
|
|
435
|
+
this.errorLog(
|
|
436
|
+
`Curtain: ${this.accessory.displayName} failed refreshStatus with BLE Connection,` + ` Error Message: ${JSON.stringify(e.message)}`,
|
|
437
|
+
);
|
|
438
|
+
}
|
|
439
|
+
if (this.platform.debugMode) {
|
|
440
|
+
this.errorLog(`Curtain: ${this.accessory.displayName} failed refreshStatus with BLE Connection,` + ` Error: ${JSON.stringify(e)}`);
|
|
411
441
|
}
|
|
412
|
-
};
|
|
413
|
-
// Wait 2 seconds
|
|
414
|
-
return switchbot.wait(this.scanDuration * 1000);
|
|
415
|
-
}).then(async () => {
|
|
416
|
-
// Stop to monitor
|
|
417
|
-
switchbot.stopScan();
|
|
418
|
-
if (this.connected) {
|
|
419
|
-
this.parseStatus();
|
|
420
|
-
this.updateHomeKitCharacteristics();
|
|
421
|
-
} else {
|
|
422
|
-
this.errorLog(`Curtain: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
|
|
423
442
|
if (this.platform.config.credentials?.openToken) {
|
|
424
443
|
this.warnLog(`Curtain: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
425
444
|
this.SwitchToOpenAPI = true;
|
|
426
445
|
await this.openAPIRefreshStatus();
|
|
427
446
|
}
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
this.errorLog(`Curtain: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
|
|
431
|
-
if (this.deviceLogging === 'debug') {
|
|
432
|
-
this.errorLog(`Curtain: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
|
|
433
|
-
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
434
|
-
}
|
|
435
|
-
if (this.platform.debugMode) {
|
|
436
|
-
this.errorLog(`Curtain: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
|
|
437
|
-
+ ` Error: ${JSON.stringify(e)}`);
|
|
438
|
-
}
|
|
439
|
-
if (this.platform.config.credentials?.openToken) {
|
|
440
|
-
this.warnLog(`Curtain: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
441
|
-
this.SwitchToOpenAPI = true;
|
|
442
|
-
await this.openAPIRefreshStatus();
|
|
443
|
-
}
|
|
444
|
-
this.apiError(e);
|
|
445
|
-
});
|
|
447
|
+
this.apiError(e);
|
|
448
|
+
});
|
|
446
449
|
} else {
|
|
447
450
|
await this.BLEconnection(switchbot);
|
|
448
451
|
}
|
|
@@ -471,12 +474,12 @@ export class Curtain {
|
|
|
471
474
|
} catch (e: any) {
|
|
472
475
|
this.errorLog(`Curtain: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
|
|
473
476
|
if (this.deviceLogging === 'debug') {
|
|
474
|
-
this.errorLog(
|
|
475
|
-
+ ` Error Message: ${JSON.stringify(e.message)}
|
|
477
|
+
this.errorLog(
|
|
478
|
+
`Curtain: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,` + ` Error Message: ${JSON.stringify(e.message)}`,
|
|
479
|
+
);
|
|
476
480
|
}
|
|
477
481
|
if (this.platform.debugMode) {
|
|
478
|
-
this.errorLog(`Curtain: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
|
|
479
|
-
+ ` Error: ${JSON.stringify(e)}`);
|
|
482
|
+
this.errorLog(`Curtain: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,` + ` Error: ${JSON.stringify(e)}`);
|
|
480
483
|
}
|
|
481
484
|
this.apiError(e);
|
|
482
485
|
}
|
|
@@ -502,30 +505,37 @@ export class Curtain {
|
|
|
502
505
|
this.debugLog(`Curtain: ${this.accessory.displayName} BLE pushChanges`);
|
|
503
506
|
const switchbot = await this.platform.connectBLE();
|
|
504
507
|
// Convert to BLE Address
|
|
505
|
-
this.device.bleMac =
|
|
508
|
+
this.device.bleMac = this.device
|
|
509
|
+
.deviceId!.match(/.{1,2}/g)!
|
|
510
|
+
.join(':')
|
|
511
|
+
.toLowerCase();
|
|
506
512
|
this.debugLog(`Curtain: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
507
513
|
if (switchbot !== false) {
|
|
508
|
-
switchbot
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
514
|
+
switchbot
|
|
515
|
+
.discover({ model: 'c', quick: true, id: this.device.bleMac })
|
|
516
|
+
.then((device_list) => {
|
|
517
|
+
this.infoLog(`${this.accessory.displayName} Target Position: ${this.TargetPosition}`);
|
|
518
|
+
return device_list[0].runToPos(100 - Number(this.TargetPosition));
|
|
519
|
+
})
|
|
520
|
+
.then(() => {
|
|
521
|
+
this.debugLog(`Curtain: ${this.accessory.displayName} Done.`);
|
|
522
|
+
})
|
|
523
|
+
.catch(async (e: any) => {
|
|
524
|
+
this.errorLog(`Curtain: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
|
|
525
|
+
if (this.deviceLogging === 'debug') {
|
|
526
|
+
this.errorLog(
|
|
527
|
+
`Curtain: ${this.accessory.displayName} failed pushChanges with BLE Connection,` + ` Error Message: ${JSON.stringify(e.message)}`,
|
|
528
|
+
);
|
|
529
|
+
}
|
|
530
|
+
if (this.platform.debugMode) {
|
|
531
|
+
this.errorLog(`Curtain: ${this.accessory.displayName} failed pushChanges with BLE Connection,` + ` Error: ${JSON.stringify(e)}`);
|
|
532
|
+
}
|
|
533
|
+
if (this.platform.config.credentials?.openToken) {
|
|
534
|
+
this.warnLog(`Curtain: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
535
|
+
await this.openAPIpushChanges();
|
|
536
|
+
}
|
|
537
|
+
this.apiError(e);
|
|
538
|
+
});
|
|
529
539
|
} else {
|
|
530
540
|
this.errorLog(`Curtain: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
|
|
531
541
|
if (this.platform.config.credentials?.openToken) {
|
|
@@ -534,8 +544,10 @@ export class Curtain {
|
|
|
534
544
|
}
|
|
535
545
|
}
|
|
536
546
|
} else {
|
|
537
|
-
this.debugLog(
|
|
538
|
-
|
|
547
|
+
this.debugLog(
|
|
548
|
+
`Curtain: ${this.accessory.displayName} No BLE Changes, CurrentPosition & TargetPosition Are the Same.` +
|
|
549
|
+
` CurrentPosition: ${this.CurrentPosition}, TargetPosition ${this.TargetPosition}`,
|
|
550
|
+
);
|
|
539
551
|
}
|
|
540
552
|
}
|
|
541
553
|
|
|
@@ -552,26 +564,30 @@ export class Curtain {
|
|
|
552
564
|
parameter: `0,ff,${adjustedTargetPosition}`,
|
|
553
565
|
} as payload;
|
|
554
566
|
|
|
555
|
-
this.infoLog(
|
|
556
|
-
|
|
567
|
+
this.infoLog(
|
|
568
|
+
`Curtain: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},` +
|
|
569
|
+
` parameter: ${payload.parameter}, commandType: ${payload.commandType}`,
|
|
570
|
+
);
|
|
557
571
|
|
|
558
572
|
// Make the API request
|
|
559
|
-
const push: any =
|
|
573
|
+
const push: any = await this.platform.axios.post(`${DeviceURL}/${this.device.deviceId!}/commands`, payload);
|
|
560
574
|
this.debugLog(`Curtain: ${this.accessory.displayName} pushchanges: ${JSON.stringify(push.data)}`);
|
|
561
575
|
this.statusCode(push);
|
|
562
576
|
} else {
|
|
563
|
-
this.debugLog(
|
|
564
|
-
|
|
577
|
+
this.debugLog(
|
|
578
|
+
`Curtain: ${this.accessory.displayName} No OpenAPI Changes, CurrentPosition & TargetPosition Are the Same.` +
|
|
579
|
+
` CurrentPosition: ${this.CurrentPosition}, TargetPosition ${this.TargetPosition}`,
|
|
580
|
+
);
|
|
565
581
|
}
|
|
566
582
|
} catch (e: any) {
|
|
567
583
|
this.errorLog(`Curtain: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
|
|
568
584
|
if (this.deviceLogging === 'debug') {
|
|
569
|
-
this.errorLog(
|
|
570
|
-
+ ` Error Message: ${JSON.stringify(e.message)}
|
|
585
|
+
this.errorLog(
|
|
586
|
+
`Curtain: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,` + ` Error Message: ${JSON.stringify(e.message)}`,
|
|
587
|
+
);
|
|
571
588
|
}
|
|
572
589
|
if (this.platform.debugMode) {
|
|
573
|
-
this.errorLog(`Curtain: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
|
|
574
|
-
+ ` Error: ${JSON.stringify(e)}`);
|
|
590
|
+
this.errorLog(`Curtain: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,` + ` Error: ${JSON.stringify(e)}`);
|
|
575
591
|
}
|
|
576
592
|
this.apiError(e);
|
|
577
593
|
}
|
|
@@ -602,8 +618,7 @@ export class Curtain {
|
|
|
602
618
|
this.debugLog(`Curtain: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
|
|
603
619
|
} else {
|
|
604
620
|
this.lightSensorService?.updateCharacteristic(this.platform.Characteristic.CurrentAmbientLightLevel, this.CurrentAmbientLightLevel);
|
|
605
|
-
this.debugLog(`Curtain: ${this.accessory.displayName}`
|
|
606
|
-
+ ` updateCharacteristic CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
|
|
621
|
+
this.debugLog(`Curtain: ${this.accessory.displayName}` + ` updateCharacteristic CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
|
|
607
622
|
}
|
|
608
623
|
}
|
|
609
624
|
if (this.device.ble) {
|
|
@@ -636,7 +651,7 @@ export class Curtain {
|
|
|
636
651
|
//throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
|
|
637
652
|
}
|
|
638
653
|
|
|
639
|
-
private statusCode(push: AxiosResponse<{ statusCode: number
|
|
654
|
+
private statusCode(push: AxiosResponse<{ statusCode: number }>) {
|
|
640
655
|
switch (push.data.statusCode) {
|
|
641
656
|
case 151:
|
|
642
657
|
this.errorLog(`Curtain: ${this.accessory.displayName} Command not supported by this device type.`);
|
|
@@ -654,8 +669,10 @@ export class Curtain {
|
|
|
654
669
|
this.errorLog(`Curtain: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
|
|
655
670
|
break;
|
|
656
671
|
case 190:
|
|
657
|
-
this.errorLog(
|
|
658
|
-
|
|
672
|
+
this.errorLog(
|
|
673
|
+
`Curtain: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
|
|
674
|
+
` Or command: ${JSON.stringify(push.data)} format is invalid`,
|
|
675
|
+
);
|
|
659
676
|
break;
|
|
660
677
|
case 100:
|
|
661
678
|
this.debugLog(`Curtain: ${this.accessory.displayName} Command successfully sent.`);
|
|
@@ -691,9 +708,9 @@ export class Curtain {
|
|
|
691
708
|
this.service.getCharacteristic(this.platform.Characteristic.PositionState).updateValue(this.PositionState);
|
|
692
709
|
|
|
693
710
|
/**
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
711
|
+
* If Curtain movement time is short, the moving flag from backend is always false.
|
|
712
|
+
* The minimum time depends on the network control latency.
|
|
713
|
+
*/
|
|
697
714
|
clearTimeout(this.setNewTargetTimer);
|
|
698
715
|
if (this.setNewTarget) {
|
|
699
716
|
this.setNewTargetTimer = setTimeout(() => {
|
|
@@ -816,8 +833,8 @@ export class Curtain {
|
|
|
816
833
|
}
|
|
817
834
|
|
|
818
835
|
/**
|
|
819
|
-
|
|
820
|
-
|
|
836
|
+
* Logging for Device
|
|
837
|
+
*/
|
|
821
838
|
infoLog(...log: any[]) {
|
|
822
839
|
if (this.enablingDeviceLogging()) {
|
|
823
840
|
this.platform.log.info(String(...log));
|