@switchbot/homebridge-switchbot 1.8.2 → 1.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc +3 -1
- 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 +5 -0
- package/.vscode/settings.json +27 -0
- package/CHANGELOG.md +16 -0
- package/README.md +100 -99
- package/SECURITY.md +2 -2
- package/config.schema.json +17 -0
- package/dist/devices/bots.d.ts +4 -2
- package/dist/devices/bots.d.ts.map +1 -1
- package/dist/devices/bots.js +101 -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 +2 -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 +18 -12
- package/src/devices/bots.ts +228 -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 -18
- package/.github/PULL_REQUEST_TEMPLATE/pull_requests.md +0 -27
package/src/devices/colorbulb.ts
CHANGED
|
@@ -52,11 +52,7 @@ export class ColorBulb {
|
|
|
52
52
|
doColorBulbUpdate!: Subject<void>;
|
|
53
53
|
cacheKelvin!: number;
|
|
54
54
|
|
|
55
|
-
constructor(
|
|
56
|
-
private readonly platform: SwitchBotPlatform,
|
|
57
|
-
private accessory: PlatformAccessory,
|
|
58
|
-
public device: device & devicesConfig,
|
|
59
|
-
) {
|
|
55
|
+
constructor(private readonly platform: SwitchBotPlatform, private accessory: PlatformAccessory, public device: device & devicesConfig) {
|
|
60
56
|
// default placeholders
|
|
61
57
|
this.logs(device);
|
|
62
58
|
this.refreshRate(device);
|
|
@@ -77,7 +73,6 @@ export class ColorBulb {
|
|
|
77
73
|
this.doColorBulbUpdate = new Subject();
|
|
78
74
|
this.colorBulbUpdateInProgress = false;
|
|
79
75
|
|
|
80
|
-
|
|
81
76
|
// Retrieve initial values and updateHomekit
|
|
82
77
|
this.refreshStatus();
|
|
83
78
|
|
|
@@ -90,10 +85,8 @@ export class ColorBulb {
|
|
|
90
85
|
|
|
91
86
|
// get the Lightbulb service if it exists, otherwise create a new Lightbulb service
|
|
92
87
|
// you can create multiple services for each accessory
|
|
93
|
-
(this.service =
|
|
94
|
-
|
|
95
|
-
accessory.addService(this.platform.Service.Lightbulb)), `${accessory.displayName} Light Bulb`;
|
|
96
|
-
|
|
88
|
+
(this.service = accessory.getService(this.platform.Service.Lightbulb) || accessory.addService(this.platform.Service.Lightbulb)),
|
|
89
|
+
`${accessory.displayName} Light Bulb`;
|
|
97
90
|
|
|
98
91
|
if (this.adaptiveLightingShift === -1 && this.accessory.context.adaptiveLighting) {
|
|
99
92
|
this.accessory.removeService(this.service);
|
|
@@ -111,11 +104,11 @@ export class ColorBulb {
|
|
|
111
104
|
this.service.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
|
|
112
105
|
|
|
113
106
|
// handle on / off events using the On characteristic
|
|
114
|
-
this.service.getCharacteristic(this.platform.Characteristic.On)
|
|
115
|
-
.onSet(this.OnSet.bind(this));
|
|
107
|
+
this.service.getCharacteristic(this.platform.Characteristic.On).onSet(this.OnSet.bind(this));
|
|
116
108
|
|
|
117
109
|
// handle Brightness events using the Brightness characteristic
|
|
118
|
-
this.service
|
|
110
|
+
this.service
|
|
111
|
+
.getCharacteristic(this.platform.Characteristic.Brightness)
|
|
119
112
|
.setProps({
|
|
120
113
|
minStep: this.minStep(device),
|
|
121
114
|
minValue: 0,
|
|
@@ -128,7 +121,8 @@ export class ColorBulb {
|
|
|
128
121
|
.onSet(this.BrightnessSet.bind(this));
|
|
129
122
|
|
|
130
123
|
// handle ColorTemperature events using the ColorTemperature characteristic
|
|
131
|
-
this.service
|
|
124
|
+
this.service
|
|
125
|
+
.getCharacteristic(this.platform.Characteristic.ColorTemperature)
|
|
132
126
|
.setProps({
|
|
133
127
|
minValue: 140,
|
|
134
128
|
maxValue: 500,
|
|
@@ -139,9 +133,9 @@ export class ColorBulb {
|
|
|
139
133
|
})
|
|
140
134
|
.onSet(this.ColorTemperatureSet.bind(this));
|
|
141
135
|
|
|
142
|
-
|
|
143
136
|
// handle Hue events using the Hue characteristic
|
|
144
|
-
this.service
|
|
137
|
+
this.service
|
|
138
|
+
.getCharacteristic(this.platform.Characteristic.Hue)
|
|
145
139
|
.setProps({
|
|
146
140
|
minValue: 0,
|
|
147
141
|
maxValue: 360,
|
|
@@ -153,7 +147,8 @@ export class ColorBulb {
|
|
|
153
147
|
.onSet(this.HueSet.bind(this));
|
|
154
148
|
|
|
155
149
|
// handle Hue events using the Hue characteristic
|
|
156
|
-
this.service
|
|
150
|
+
this.service
|
|
151
|
+
.getCharacteristic(this.platform.Characteristic.Saturation)
|
|
157
152
|
.setProps({
|
|
158
153
|
minValue: 0,
|
|
159
154
|
maxValue: 100,
|
|
@@ -164,7 +159,6 @@ export class ColorBulb {
|
|
|
164
159
|
})
|
|
165
160
|
.onSet(this.SaturationSet.bind(this));
|
|
166
161
|
|
|
167
|
-
|
|
168
162
|
this.debugLog(`Color Bulb: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
|
|
169
163
|
if (this.adaptiveLightingShift !== -1) {
|
|
170
164
|
this.AdaptiveLightingController = new platform.api.hap.AdaptiveLightingController(this.service, {
|
|
@@ -172,8 +166,10 @@ export class ColorBulb {
|
|
|
172
166
|
});
|
|
173
167
|
this.accessory.configureController(this.AdaptiveLightingController);
|
|
174
168
|
this.accessory.context.adaptiveLighting = true;
|
|
175
|
-
this.debugLog(
|
|
176
|
-
|
|
169
|
+
this.debugLog(
|
|
170
|
+
`Color Bulb: ${this.accessory.displayName} adaptiveLighting: ${this.accessory.context.adaptiveLighting},` +
|
|
171
|
+
` adaptiveLightingShift: ${this.adaptiveLightingShift}`,
|
|
172
|
+
);
|
|
177
173
|
}
|
|
178
174
|
|
|
179
175
|
// Update Homekit
|
|
@@ -201,12 +197,10 @@ export class ColorBulb {
|
|
|
201
197
|
} catch (e: any) {
|
|
202
198
|
this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges`);
|
|
203
199
|
if (this.deviceLogging === 'debug') {
|
|
204
|
-
this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges,`
|
|
205
|
-
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
200
|
+
this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges,` + ` Error Message: ${JSON.stringify(e.message)}`);
|
|
206
201
|
}
|
|
207
202
|
if (this.platform.debugMode) {
|
|
208
|
-
this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges,`
|
|
209
|
-
+ ` Error: ${JSON.stringify(e)}`);
|
|
203
|
+
this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges,` + ` Error: ${JSON.stringify(e)}`);
|
|
210
204
|
}
|
|
211
205
|
this.apiError(e);
|
|
212
206
|
}
|
|
@@ -273,27 +267,27 @@ export class ColorBulb {
|
|
|
273
267
|
} catch (e: any) {
|
|
274
268
|
this.errorLog(`Color Bulb: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
|
|
275
269
|
if (this.deviceLogging === 'debug') {
|
|
276
|
-
this.errorLog(
|
|
277
|
-
+ ` Error Message: ${JSON.stringify(e.message)}
|
|
270
|
+
this.errorLog(
|
|
271
|
+
`Color Bulb: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,` + ` Error Message: ${JSON.stringify(e.message)}`,
|
|
272
|
+
);
|
|
278
273
|
}
|
|
279
274
|
if (this.platform.debugMode) {
|
|
280
|
-
this.errorLog(`Color Bulb: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
|
|
281
|
-
+ ` Error: ${JSON.stringify(e)}`);
|
|
275
|
+
this.errorLog(`Color Bulb: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,` + ` Error: ${JSON.stringify(e)}`);
|
|
282
276
|
}
|
|
283
277
|
this.apiError(e);
|
|
284
278
|
}
|
|
285
279
|
}
|
|
286
280
|
|
|
287
281
|
/**
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
282
|
+
* Pushes the requested changes to the SwitchBot API
|
|
283
|
+
* deviceType commandType Command command parameter Description
|
|
284
|
+
* Color Bulb - "command" "turnOff" "default" = set to OFF state
|
|
285
|
+
* Color Bulb - "command" "turnOn" "default" = set to ON state
|
|
286
|
+
* Color Bulb - "command" "toggle" "default" = toggle state
|
|
287
|
+
* Color Bulb - "command" "setBrightness" "{1-100}" = set brightness
|
|
288
|
+
* Color Bulb - "command" "setColor" "{0-255}:{0-255}:{0-255}" = set RGB color value
|
|
289
|
+
* Color Bulb - "command" "setColorTemperature" "{2700-6500}" = set color temperature
|
|
290
|
+
*/
|
|
297
291
|
async pushChanges() {
|
|
298
292
|
try {
|
|
299
293
|
if (this.On !== this.OnCached) {
|
|
@@ -309,11 +303,13 @@ export class ColorBulb {
|
|
|
309
303
|
payload.command = 'turnOff';
|
|
310
304
|
}
|
|
311
305
|
|
|
312
|
-
this.infoLog(
|
|
313
|
-
|
|
306
|
+
this.infoLog(
|
|
307
|
+
`Color Bulb: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},` +
|
|
308
|
+
` parameter: ${payload.parameter}, commandType: ${payload.commandType}`,
|
|
309
|
+
);
|
|
314
310
|
|
|
315
311
|
// Make the API request
|
|
316
|
-
const push: any =
|
|
312
|
+
const push: any = await this.platform.axios.post(`${DeviceURL}/${this.device.deviceId}/commands`, payload);
|
|
317
313
|
this.debugLog(`Color Bulb: ${this.accessory.displayName} pushChanges: ${JSON.stringify(push.data)}`);
|
|
318
314
|
this.statusCode(push);
|
|
319
315
|
this.OnCached = this.On;
|
|
@@ -333,16 +329,15 @@ export class ColorBulb {
|
|
|
333
329
|
if (this.On) {
|
|
334
330
|
await this.pushHueSaturationChanges();
|
|
335
331
|
}
|
|
336
|
-
|
|
337
332
|
} catch (e: any) {
|
|
338
333
|
this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
|
|
339
334
|
if (this.deviceLogging === 'debug') {
|
|
340
|
-
this.errorLog(
|
|
341
|
-
+ ` Error Message: ${JSON.stringify(e.message)}
|
|
335
|
+
this.errorLog(
|
|
336
|
+
`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,` + ` Error Message: ${JSON.stringify(e.message)}`,
|
|
337
|
+
);
|
|
342
338
|
}
|
|
343
339
|
if (this.platform.debugMode) {
|
|
344
|
-
this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
|
|
345
|
-
+ ` Error: ${JSON.stringify(e)}`);
|
|
340
|
+
this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,` + ` Error: ${JSON.stringify(e)}`);
|
|
346
341
|
}
|
|
347
342
|
this.apiError(e);
|
|
348
343
|
}
|
|
@@ -350,7 +345,7 @@ export class ColorBulb {
|
|
|
350
345
|
|
|
351
346
|
private async pushHueSaturationChanges() {
|
|
352
347
|
try {
|
|
353
|
-
if (
|
|
348
|
+
if (this.Hue !== this.HueCached || this.Saturation !== this.SaturationCached) {
|
|
354
349
|
this.debugLog(`Color Bulb: ${this.accessory.displayName} Hue: ${JSON.stringify(this.Hue)}`);
|
|
355
350
|
this.debugLog(`Color Bulb: ${this.accessory.displayName} Saturation: ${JSON.stringify(this.Saturation)}`);
|
|
356
351
|
|
|
@@ -363,28 +358,32 @@ export class ColorBulb {
|
|
|
363
358
|
parameter: `${red}:${green}:${blue}`,
|
|
364
359
|
} as payload;
|
|
365
360
|
|
|
366
|
-
this.infoLog(
|
|
367
|
-
|
|
361
|
+
this.infoLog(
|
|
362
|
+
`Color Bulb: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},` +
|
|
363
|
+
` parameter: ${payload.parameter}, commandType: ${payload.commandType}`,
|
|
364
|
+
);
|
|
368
365
|
|
|
369
366
|
// Make the API request
|
|
370
|
-
const push: any =
|
|
367
|
+
const push: any = await this.platform.axios.post(`${DeviceURL}/${this.device.deviceId}/commands`, payload);
|
|
371
368
|
this.debugLog(`Color Bulb: ${this.accessory.displayName} pushChanges: ${JSON.stringify(push.data)}`);
|
|
372
369
|
this.statusCode(push);
|
|
373
370
|
this.HueCached = this.Hue;
|
|
374
371
|
this.SaturationCached = this.Saturation;
|
|
375
372
|
} else {
|
|
376
|
-
this.debugLog(
|
|
377
|
-
|
|
373
|
+
this.debugLog(
|
|
374
|
+
`Color Bulb: ${this.accessory.displayName} No Changes.` +
|
|
375
|
+
`Hue: ${this.Hue}, HueCached: ${this.HueCached}, Saturation: ${this.Saturation}, SaturationCached: ${this.SaturationCached}`,
|
|
376
|
+
);
|
|
378
377
|
}
|
|
379
378
|
} catch (e: any) {
|
|
380
379
|
this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
|
|
381
380
|
if (this.deviceLogging === 'debug') {
|
|
382
|
-
this.errorLog(
|
|
383
|
-
+ ` Error Message: ${JSON.stringify(e.message)}
|
|
381
|
+
this.errorLog(
|
|
382
|
+
`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,` + ` Error Message: ${JSON.stringify(e.message)}`,
|
|
383
|
+
);
|
|
384
384
|
}
|
|
385
385
|
if (this.platform.debugMode) {
|
|
386
|
-
this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
|
|
387
|
-
+ ` Error: ${JSON.stringify(e)}`);
|
|
386
|
+
this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,` + ` Error: ${JSON.stringify(e)}`);
|
|
388
387
|
}
|
|
389
388
|
this.apiError(e);
|
|
390
389
|
}
|
|
@@ -402,27 +401,31 @@ export class ColorBulb {
|
|
|
402
401
|
parameter: `${kelvin}`,
|
|
403
402
|
} as payload;
|
|
404
403
|
|
|
405
|
-
this.infoLog(
|
|
406
|
-
|
|
404
|
+
this.infoLog(
|
|
405
|
+
`Color Bulb: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},` +
|
|
406
|
+
` parameter: ${payload.parameter}, commandType: ${payload.commandType}`,
|
|
407
|
+
);
|
|
407
408
|
|
|
408
409
|
// Make the API request
|
|
409
|
-
const push: any =
|
|
410
|
+
const push: any = await this.platform.axios.post(`${DeviceURL}/${this.device.deviceId}/commands`, payload);
|
|
410
411
|
this.debugLog(`Color Bulb: ${this.accessory.displayName} pushChanges: ${JSON.stringify(push.data)}`);
|
|
411
412
|
this.statusCode(push);
|
|
412
413
|
this.ColorTemperatureCached = this.ColorTemperature;
|
|
413
414
|
} else {
|
|
414
|
-
this.debugLog(
|
|
415
|
-
|
|
415
|
+
this.debugLog(
|
|
416
|
+
`Color Bulb: ${this.accessory.displayName} No Changes.` +
|
|
417
|
+
`ColorTemperature: ${this.ColorTemperature}, ColorTemperatureCached: ${this.ColorTemperatureCached}`,
|
|
418
|
+
);
|
|
416
419
|
}
|
|
417
420
|
} catch (e: any) {
|
|
418
421
|
this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
|
|
419
422
|
if (this.deviceLogging === 'debug') {
|
|
420
|
-
this.errorLog(
|
|
421
|
-
+ ` Error Message: ${JSON.stringify(e.message)}
|
|
423
|
+
this.errorLog(
|
|
424
|
+
`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,` + ` Error Message: ${JSON.stringify(e.message)}`,
|
|
425
|
+
);
|
|
422
426
|
}
|
|
423
427
|
if (this.platform.debugMode) {
|
|
424
|
-
this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
|
|
425
|
-
+ ` Error: ${JSON.stringify(e)}`);
|
|
428
|
+
this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,` + ` Error: ${JSON.stringify(e)}`);
|
|
426
429
|
}
|
|
427
430
|
this.apiError(e);
|
|
428
431
|
}
|
|
@@ -437,27 +440,30 @@ export class ColorBulb {
|
|
|
437
440
|
parameter: `${this.Brightness}`,
|
|
438
441
|
} as payload;
|
|
439
442
|
|
|
440
|
-
this.infoLog(
|
|
441
|
-
|
|
443
|
+
this.infoLog(
|
|
444
|
+
`Color Bulb: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},` +
|
|
445
|
+
` parameter: ${payload.parameter}, commandType: ${payload.commandType}`,
|
|
446
|
+
);
|
|
442
447
|
|
|
443
448
|
// Make the API request
|
|
444
|
-
const push: any =
|
|
449
|
+
const push: any = await this.platform.axios.post(`${DeviceURL}/${this.device.deviceId}/commands`, payload);
|
|
445
450
|
this.debugLog(`Color Bulb: ${this.accessory.displayName} pushChanges: ${JSON.stringify(push.data)}`);
|
|
446
451
|
this.statusCode(push);
|
|
447
452
|
this.BrightnessCached = this.Brightness;
|
|
448
453
|
} else {
|
|
449
|
-
this.debugLog(
|
|
450
|
-
+ `Brightness: ${this.Brightness}, BrightnessCached: ${this.BrightnessCached}
|
|
454
|
+
this.debugLog(
|
|
455
|
+
`Color Bulb: ${this.accessory.displayName} No Changes.` + `Brightness: ${this.Brightness}, BrightnessCached: ${this.BrightnessCached}`,
|
|
456
|
+
);
|
|
451
457
|
}
|
|
452
458
|
} catch (e: any) {
|
|
453
459
|
this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
|
|
454
460
|
if (this.deviceLogging === 'debug') {
|
|
455
|
-
this.errorLog(
|
|
456
|
-
+ ` Error Message: ${JSON.stringify(e.message)}
|
|
461
|
+
this.errorLog(
|
|
462
|
+
`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,` + ` Error Message: ${JSON.stringify(e.message)}`,
|
|
463
|
+
);
|
|
457
464
|
}
|
|
458
465
|
if (this.platform.debugMode) {
|
|
459
|
-
this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
|
|
460
|
-
+ ` Error: ${JSON.stringify(e)}`);
|
|
466
|
+
this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,` + ` Error: ${JSON.stringify(e)}`);
|
|
461
467
|
}
|
|
462
468
|
this.apiError(e);
|
|
463
469
|
}
|
|
@@ -505,7 +511,7 @@ export class ColorBulb {
|
|
|
505
511
|
//throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
|
|
506
512
|
}
|
|
507
513
|
|
|
508
|
-
private statusCode(push: AxiosResponse<{ statusCode: number
|
|
514
|
+
private statusCode(push: AxiosResponse<{ statusCode: number }>) {
|
|
509
515
|
switch (push.data.statusCode) {
|
|
510
516
|
case 151:
|
|
511
517
|
this.errorLog(`Color Bulb: ${this.accessory.displayName} Command not supported by this device type.`);
|
|
@@ -525,8 +531,10 @@ export class ColorBulb {
|
|
|
525
531
|
this.offlineOff();
|
|
526
532
|
break;
|
|
527
533
|
case 190:
|
|
528
|
-
this.errorLog(
|
|
529
|
-
|
|
534
|
+
this.errorLog(
|
|
535
|
+
`Color Bulb: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
|
|
536
|
+
` Or command: ${JSON.stringify(push.data)} format is invalid`,
|
|
537
|
+
);
|
|
530
538
|
break;
|
|
531
539
|
case 100:
|
|
532
540
|
if (this.platform.debugMode) {
|
|
@@ -573,7 +581,6 @@ export class ColorBulb {
|
|
|
573
581
|
ColorTemperatureSet(value: CharacteristicValue) {
|
|
574
582
|
this.debugLog(`Color Bulb: ${this.accessory.displayName} ColorTemperature: ${value}`);
|
|
575
583
|
|
|
576
|
-
|
|
577
584
|
// Convert mired to kelvin to nearest 100 (SwitchBot seems to need this)
|
|
578
585
|
const kelvin = Math.round(1000000 / Number(value) / 100) * 100;
|
|
579
586
|
|
|
@@ -594,8 +601,8 @@ export class ColorBulb {
|
|
|
594
601
|
}
|
|
595
602
|
|
|
596
603
|
/**
|
|
597
|
-
|
|
598
|
-
|
|
604
|
+
* Handle requests to set the value of the "Hue" characteristic
|
|
605
|
+
*/
|
|
599
606
|
HueSet(value: CharacteristicValue) {
|
|
600
607
|
this.debugLog(`Color Bulb: ${this.accessory.displayName} Hue: ${value}`);
|
|
601
608
|
|
|
@@ -606,8 +613,8 @@ export class ColorBulb {
|
|
|
606
613
|
}
|
|
607
614
|
|
|
608
615
|
/**
|
|
609
|
-
|
|
610
|
-
|
|
616
|
+
* Handle requests to set the value of the "Saturation" characteristic
|
|
617
|
+
*/
|
|
611
618
|
SaturationSet(value: CharacteristicValue) {
|
|
612
619
|
this.debugLog(`Color Bulb: ${this.accessory.displayName} Saturation: ${value}`);
|
|
613
620
|
|
|
@@ -685,8 +692,8 @@ export class ColorBulb {
|
|
|
685
692
|
}
|
|
686
693
|
|
|
687
694
|
/**
|
|
688
|
-
|
|
689
|
-
|
|
695
|
+
* Logging for Device
|
|
696
|
+
*/
|
|
690
697
|
infoLog(...log: any[]) {
|
|
691
698
|
if (this.enablingDeviceLogging()) {
|
|
692
699
|
this.platform.log.info(String(...log));
|
|
@@ -719,4 +726,3 @@ export class ColorBulb {
|
|
|
719
726
|
return this.deviceLogging === 'debug' || this.deviceLogging === 'standard';
|
|
720
727
|
}
|
|
721
728
|
}
|
|
722
|
-
|
package/src/devices/contact.ts
CHANGED
|
@@ -50,11 +50,7 @@ export class Contact {
|
|
|
50
50
|
contactUbpdateInProgress!: boolean;
|
|
51
51
|
doContactUpdate!: Subject<void>;
|
|
52
52
|
|
|
53
|
-
constructor(
|
|
54
|
-
private readonly platform: SwitchBotPlatform,
|
|
55
|
-
private accessory: PlatformAccessory,
|
|
56
|
-
public device: device & devicesConfig,
|
|
57
|
-
) {
|
|
53
|
+
constructor(private readonly platform: SwitchBotPlatform, private accessory: PlatformAccessory, public device: device & devicesConfig) {
|
|
58
54
|
// default placeholders
|
|
59
55
|
this.logs(device);
|
|
60
56
|
this.scan(device);
|
|
@@ -78,9 +74,8 @@ export class Contact {
|
|
|
78
74
|
|
|
79
75
|
// get the Contact service if it exists, otherwise create a new Contact service
|
|
80
76
|
// you can create multiple services for each accessory
|
|
81
|
-
(this.service =
|
|
82
|
-
|
|
83
|
-
accessory.addService(this.platform.Service.ContactSensor)), `${accessory.displayName} Contact Sensor`;
|
|
77
|
+
(this.service = accessory.getService(this.platform.Service.ContactSensor) || accessory.addService(this.platform.Service.ContactSensor)),
|
|
78
|
+
`${accessory.displayName} Contact Sensor`;
|
|
84
79
|
|
|
85
80
|
// To avoid "Cannot add a Service with the same UUID another Service without also defining a unique 'subtype' property." error,
|
|
86
81
|
// when creating multiple services of the same type, you need to use the following syntax to specify a name and subtype id:
|
|
@@ -101,11 +96,10 @@ export class Contact {
|
|
|
101
96
|
} else if (!this.motionService) {
|
|
102
97
|
this.debugLog(`Contact Sensor: ${accessory.displayName} Add Motion Sensor Service`);
|
|
103
98
|
(this.motionService =
|
|
104
|
-
this.accessory.getService(this.platform.Service.MotionSensor) ||
|
|
105
|
-
|
|
99
|
+
this.accessory.getService(this.platform.Service.MotionSensor) || this.accessory.addService(this.platform.Service.MotionSensor)),
|
|
100
|
+
`${accessory.displayName} Motion Sensor`;
|
|
106
101
|
|
|
107
102
|
this.motionService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Motion Sensor`);
|
|
108
|
-
|
|
109
103
|
} else {
|
|
110
104
|
this.debugLog(`Contact Sensor: ${accessory.displayName} Motion Sensor Service Not Added`);
|
|
111
105
|
}
|
|
@@ -118,11 +112,10 @@ export class Contact {
|
|
|
118
112
|
} else if (!this.lightSensorService) {
|
|
119
113
|
this.debugLog(`Contact Sensor: ${accessory.displayName} Add Light Sensor Service`);
|
|
120
114
|
(this.lightSensorService =
|
|
121
|
-
this.accessory.getService(this.platform.Service.LightSensor) ||
|
|
122
|
-
|
|
115
|
+
this.accessory.getService(this.platform.Service.LightSensor) || this.accessory.addService(this.platform.Service.LightSensor)),
|
|
116
|
+
`${accessory.displayName} Light Sensor`;
|
|
123
117
|
|
|
124
118
|
this.lightSensorService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Light Sensor`);
|
|
125
|
-
|
|
126
119
|
} else {
|
|
127
120
|
this.debugLog(`Contact Sensor: ${accessory.displayName} Light Sensor Service Not Added`);
|
|
128
121
|
}
|
|
@@ -134,12 +127,10 @@ export class Contact {
|
|
|
134
127
|
accessory.removeService(this.batteryService!);
|
|
135
128
|
} else if (device.ble && !this.batteryService) {
|
|
136
129
|
this.debugLog(`Contact Sensor: ${accessory.displayName} Add Battery Service`);
|
|
137
|
-
(this.batteryService =
|
|
138
|
-
|
|
139
|
-
this.accessory.addService(this.platform.Service.Battery)), `${accessory.displayName} Battery`;
|
|
130
|
+
(this.batteryService = this.accessory.getService(this.platform.Service.Battery) || this.accessory.addService(this.platform.Service.Battery)),
|
|
131
|
+
`${accessory.displayName} Battery`;
|
|
140
132
|
|
|
141
133
|
this.batteryService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Battery`);
|
|
142
|
-
|
|
143
134
|
} else {
|
|
144
135
|
this.debugLog(`Contact Sensor: ${accessory.displayName} Battery Service Not Added`);
|
|
145
136
|
}
|
|
@@ -198,8 +189,10 @@ export class Contact {
|
|
|
198
189
|
default:
|
|
199
190
|
this.CurrentAmbientLightLevel = this.set_maxLux;
|
|
200
191
|
}
|
|
201
|
-
this.debugLog(
|
|
202
|
-
|
|
192
|
+
this.debugLog(
|
|
193
|
+
`Contact Sensor: ${this.accessory.displayName} LightLevel: ${this.lightLevel},` +
|
|
194
|
+
` CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`,
|
|
195
|
+
);
|
|
203
196
|
}
|
|
204
197
|
// Battery
|
|
205
198
|
if (this.battery === undefined) {
|
|
@@ -267,65 +260,75 @@ export class Contact {
|
|
|
267
260
|
this.debugLog(`Contact Sensor: ${this.accessory.displayName} BLE refreshStatus`);
|
|
268
261
|
const switchbot = await this.platform.connectBLE();
|
|
269
262
|
// Convert to BLE Address
|
|
270
|
-
this.device.bleMac =
|
|
263
|
+
this.device.bleMac = this.device
|
|
264
|
+
.deviceId!.match(/.{1,2}/g)!
|
|
265
|
+
.join(':')
|
|
266
|
+
.toLowerCase();
|
|
271
267
|
this.debugLog(`Curtain: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
272
268
|
// Start to monitor advertisement packets
|
|
273
269
|
if (switchbot !== false) {
|
|
274
|
-
switchbot
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
270
|
+
switchbot
|
|
271
|
+
.startScan({
|
|
272
|
+
model: 'd',
|
|
273
|
+
id: this.device.bleMac,
|
|
274
|
+
})
|
|
275
|
+
.then(() => {
|
|
276
|
+
// Set an event hander
|
|
277
|
+
switchbot.onadvertisement = (ad: any) => {
|
|
278
|
+
this.serviceData = ad.serviceData;
|
|
279
|
+
this.movement = ad.serviceData.movement;
|
|
280
|
+
this.doorState = ad.serviceData.doorState;
|
|
281
|
+
this.lightLevel = ad.serviceData.lightLevel;
|
|
282
|
+
this.battery = ad.serviceData.battery;
|
|
283
|
+
this.debugLog(`Contact Sensor: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
284
|
+
this.debugLog(
|
|
285
|
+
`Contact Sensor: ${this.accessory.displayName} movement: ${ad.serviceData.movement}, doorState: ` +
|
|
286
|
+
`${ad.serviceData.doorState}, lightLevel: ${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}`,
|
|
287
|
+
);
|
|
288
|
+
|
|
289
|
+
if (this.serviceData) {
|
|
290
|
+
this.connected = true;
|
|
291
|
+
this.debugLog(`Contact Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
|
|
292
|
+
} else {
|
|
293
|
+
this.connected = false;
|
|
294
|
+
this.debugLog(`Contact Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
// Wait 2 seconds
|
|
298
|
+
return switchbot.wait(this.scanDuration * 1000);
|
|
299
|
+
})
|
|
300
|
+
.then(async () => {
|
|
301
|
+
// Stop to monitor
|
|
302
|
+
switchbot.stopScan();
|
|
303
|
+
if (this.connected) {
|
|
304
|
+
this.parseStatus();
|
|
305
|
+
this.updateHomeKitCharacteristics();
|
|
292
306
|
} else {
|
|
293
|
-
this.
|
|
294
|
-
|
|
307
|
+
this.errorLog(`Contact Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
|
|
308
|
+
if (this.platform.config.credentials?.openToken) {
|
|
309
|
+
this.warnLog(`Contact Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
310
|
+
await this.openAPIRefreshStatus();
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
})
|
|
314
|
+
.catch(async (e: any) => {
|
|
315
|
+
this.errorLog(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
|
|
316
|
+
if (this.deviceLogging === 'debug') {
|
|
317
|
+
this.errorLog(
|
|
318
|
+
`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,` +
|
|
319
|
+
` Error Message: ${JSON.stringify(e.message)}`,
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
if (this.platform.debugMode) {
|
|
323
|
+
this.errorLog(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,` + ` Error: ${JSON.stringify(e)}`);
|
|
295
324
|
}
|
|
296
|
-
};
|
|
297
|
-
// Wait 2 seconds
|
|
298
|
-
return switchbot.wait(this.scanDuration * 1000);
|
|
299
|
-
}).then(async () => {
|
|
300
|
-
// Stop to monitor
|
|
301
|
-
switchbot.stopScan();
|
|
302
|
-
if (this.connected) {
|
|
303
|
-
this.parseStatus();
|
|
304
|
-
this.updateHomeKitCharacteristics();
|
|
305
|
-
} else {
|
|
306
|
-
this.errorLog(`Contact Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
|
|
307
325
|
if (this.platform.config.credentials?.openToken) {
|
|
308
326
|
this.warnLog(`Contact Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
327
|
+
this.SwitchToOpenAPI = true;
|
|
309
328
|
await this.openAPIRefreshStatus();
|
|
310
329
|
}
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
this.errorLog(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
|
|
314
|
-
if (this.deviceLogging === 'debug') {
|
|
315
|
-
this.errorLog(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
|
|
316
|
-
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
317
|
-
}
|
|
318
|
-
if (this.platform.debugMode) {
|
|
319
|
-
this.errorLog(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
|
|
320
|
-
+ ` Error: ${JSON.stringify(e)}`);
|
|
321
|
-
}
|
|
322
|
-
if (this.platform.config.credentials?.openToken) {
|
|
323
|
-
this.warnLog(`Contact Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
324
|
-
this.SwitchToOpenAPI = true;
|
|
325
|
-
await this.openAPIRefreshStatus();
|
|
326
|
-
}
|
|
327
|
-
this.apiError(e);
|
|
328
|
-
});
|
|
330
|
+
this.apiError(e);
|
|
331
|
+
});
|
|
329
332
|
} else {
|
|
330
333
|
await this.BLEconnection(switchbot);
|
|
331
334
|
}
|
|
@@ -354,12 +357,15 @@ export class Contact {
|
|
|
354
357
|
} catch (e: any) {
|
|
355
358
|
this.errorLog(`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
|
|
356
359
|
if (this.deviceLogging === 'debug') {
|
|
357
|
-
this.errorLog(
|
|
358
|
-
|
|
360
|
+
this.errorLog(
|
|
361
|
+
`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,` +
|
|
362
|
+
` Error Message: ${JSON.stringify(e.message)}`,
|
|
363
|
+
);
|
|
359
364
|
}
|
|
360
365
|
if (this.platform.debugMode) {
|
|
361
|
-
this.errorLog(
|
|
362
|
-
+ ` Error: ${JSON.stringify(e)}
|
|
366
|
+
this.errorLog(
|
|
367
|
+
`Contact Sensor: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,` + ` Error: ${JSON.stringify(e)}`,
|
|
368
|
+
);
|
|
363
369
|
}
|
|
364
370
|
this.apiError(e);
|
|
365
371
|
}
|
|
@@ -389,8 +395,9 @@ export class Contact {
|
|
|
389
395
|
this.debugLog(`Contact Sensor: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
|
|
390
396
|
} else {
|
|
391
397
|
this.lightSensorService?.updateCharacteristic(this.platform.Characteristic.CurrentAmbientLightLevel, this.CurrentAmbientLightLevel);
|
|
392
|
-
this.debugLog(
|
|
393
|
-
+ ` updateCharacteristic CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}
|
|
398
|
+
this.debugLog(
|
|
399
|
+
`Contact Sensor: ${this.accessory.displayName}` + ` updateCharacteristic CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`,
|
|
400
|
+
);
|
|
394
401
|
}
|
|
395
402
|
}
|
|
396
403
|
if (this.device.ble) {
|
|
@@ -505,8 +512,8 @@ export class Contact {
|
|
|
505
512
|
}
|
|
506
513
|
|
|
507
514
|
/**
|
|
508
|
-
|
|
509
|
-
|
|
515
|
+
* Logging for Device
|
|
516
|
+
*/
|
|
510
517
|
infoLog(...log: any[]) {
|
|
511
518
|
if (this.enablingDeviceLogging()) {
|
|
512
519
|
this.platform.log.info(String(...log));
|