@switchbot/homebridge-switchbot 1.4.1-beta.1 → 1.4.1-beta.2
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 +14 -0
- package/config.schema.json +82 -10
- package/dist/devices/bots.d.ts +12 -2
- package/dist/devices/bots.d.ts.map +1 -1
- package/dist/devices/bots.js +140 -89
- package/dist/devices/bots.js.map +1 -1
- package/dist/devices/colorbulb.d.ts +12 -2
- package/dist/devices/colorbulb.d.ts.map +1 -1
- package/dist/devices/colorbulb.js +138 -84
- package/dist/devices/colorbulb.js.map +1 -1
- package/dist/devices/contact.d.ts +12 -2
- package/dist/devices/contact.d.ts.map +1 -1
- package/dist/devices/contact.js +101 -52
- package/dist/devices/contact.js.map +1 -1
- package/dist/devices/curtains.d.ts +13 -4
- package/dist/devices/curtains.d.ts.map +1 -1
- package/dist/devices/curtains.js +173 -124
- package/dist/devices/curtains.js.map +1 -1
- package/dist/devices/humidifiers.d.ts +12 -2
- package/dist/devices/humidifiers.d.ts.map +1 -1
- package/dist/devices/humidifiers.js +156 -108
- package/dist/devices/humidifiers.js.map +1 -1
- package/dist/devices/meters.d.ts +12 -2
- package/dist/devices/meters.d.ts.map +1 -1
- package/dist/devices/meters.js +98 -49
- package/dist/devices/meters.js.map +1 -1
- package/dist/devices/motion.d.ts +12 -2
- package/dist/devices/motion.d.ts.map +1 -1
- package/dist/devices/motion.js +86 -37
- package/dist/devices/motion.js.map +1 -1
- package/dist/devices/plugs.d.ts +12 -2
- package/dist/devices/plugs.d.ts.map +1 -1
- package/dist/devices/plugs.js +80 -31
- package/dist/devices/plugs.js.map +1 -1
- package/dist/irdevices/airconditioners.d.ts +10 -2
- package/dist/irdevices/airconditioners.d.ts.map +1 -1
- package/dist/irdevices/airconditioners.js +77 -37
- package/dist/irdevices/airconditioners.js.map +1 -1
- package/dist/irdevices/airpurifiers.d.ts +10 -2
- package/dist/irdevices/airpurifiers.d.ts.map +1 -1
- package/dist/irdevices/airpurifiers.js +65 -25
- package/dist/irdevices/airpurifiers.js.map +1 -1
- package/dist/irdevices/cameras.d.ts +10 -2
- package/dist/irdevices/cameras.d.ts.map +1 -1
- package/dist/irdevices/cameras.js +61 -21
- package/dist/irdevices/cameras.js.map +1 -1
- package/dist/irdevices/fans.d.ts +10 -2
- package/dist/irdevices/fans.d.ts.map +1 -1
- package/dist/irdevices/fans.js +71 -30
- package/dist/irdevices/fans.js.map +1 -1
- package/dist/irdevices/lights.d.ts +10 -2
- package/dist/irdevices/lights.d.ts.map +1 -1
- package/dist/irdevices/lights.js +62 -22
- package/dist/irdevices/lights.js.map +1 -1
- package/dist/irdevices/others.d.ts +10 -2
- package/dist/irdevices/others.d.ts.map +1 -1
- package/dist/irdevices/others.js +68 -28
- package/dist/irdevices/others.js.map +1 -1
- package/dist/irdevices/tvs.d.ts +10 -2
- package/dist/irdevices/tvs.d.ts.map +1 -1
- package/dist/irdevices/tvs.js +78 -38
- package/dist/irdevices/tvs.js.map +1 -1
- package/dist/irdevices/vacuumcleaners.d.ts +10 -2
- package/dist/irdevices/vacuumcleaners.d.ts.map +1 -1
- package/dist/irdevices/vacuumcleaners.js +61 -21
- package/dist/irdevices/vacuumcleaners.js.map +1 -1
- package/dist/irdevices/waterheaters.d.ts +10 -2
- package/dist/irdevices/waterheaters.d.ts.map +1 -1
- package/dist/irdevices/waterheaters.js +61 -21
- package/dist/irdevices/waterheaters.js.map +1 -1
- package/dist/platform.d.ts +9 -10
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +264 -285
- package/dist/platform.js.map +1 -1
- package/dist/settings.d.ts +4 -1
- package/dist/settings.d.ts.map +1 -1
- package/dist/settings.js.map +1 -1
- package/nodemon.json +1 -1
- package/package.json +1 -1
- package/src/devices/bots.ts +146 -89
- package/src/devices/colorbulb.ts +146 -86
- package/src/devices/contact.ts +107 -52
- package/src/devices/curtains.ts +182 -129
- package/src/devices/humidifiers.ts +162 -109
- package/src/devices/meters.ts +104 -49
- package/src/devices/motion.ts +91 -36
- package/src/devices/plugs.ts +84 -31
- package/src/irdevices/airconditioners.ts +81 -36
- package/src/irdevices/airpurifiers.ts +69 -24
- package/src/irdevices/cameras.ts +65 -20
- package/src/irdevices/fans.ts +75 -30
- package/src/irdevices/lights.ts +66 -21
- package/src/irdevices/others.ts +72 -27
- package/src/irdevices/tvs.ts +82 -37
- package/src/irdevices/vacuumcleaners.ts +65 -20
- package/src/irdevices/waterheaters.ts +65 -20
- package/src/platform.ts +267 -290
- package/src/settings.ts +4 -1
- package/dist/devices/indoorcam.d.ts +0 -64
- package/dist/devices/indoorcam.d.ts.map +0 -1
- package/dist/devices/indoorcam.js +0 -249
- package/dist/devices/indoorcam.js.map +0 -1
- package/src/devices/indoorcam.ts +0 -309
package/src/devices/bots.ts
CHANGED
|
@@ -36,8 +36,8 @@ export class Bot {
|
|
|
36
36
|
battery!: serviceData['battery'];
|
|
37
37
|
|
|
38
38
|
// Config
|
|
39
|
-
|
|
40
|
-
|
|
39
|
+
deviceLogging!: string;
|
|
40
|
+
deviceRefreshRate!: number;
|
|
41
41
|
|
|
42
42
|
// Updates
|
|
43
43
|
botUpdateInProgress!: boolean;
|
|
@@ -48,19 +48,21 @@ export class Bot {
|
|
|
48
48
|
private accessory: PlatformAccessory,
|
|
49
49
|
public device: device & devicesConfig,
|
|
50
50
|
) {
|
|
51
|
-
// Bot Config
|
|
52
|
-
this.platform.device(`Bot: ${this.accessory.displayName} Config: (ble: ${device.ble}, offline: ${device.offline}, mode: ${device.bot?.mode},`
|
|
53
|
-
+ ` deviceType: ${device.bot?.deviceType})`);
|
|
54
|
-
|
|
55
51
|
// default placeholders
|
|
56
52
|
if (this.On === undefined) {
|
|
57
53
|
this.On = false;
|
|
58
54
|
} else {
|
|
59
55
|
this.On = this.accessory.context.On;
|
|
60
56
|
}
|
|
57
|
+
this.refreshRate();
|
|
58
|
+
this.logs();
|
|
61
59
|
this.BatteryLevel = 100;
|
|
62
60
|
this.StatusLowBattery = 1;
|
|
63
61
|
|
|
62
|
+
// Bot Config
|
|
63
|
+
this.debugLog(`Bot: ${this.accessory.displayName} Config: (ble: ${device.ble}, offline: ${device.offline}, mode: ${device.bot?.mode},`
|
|
64
|
+
+ ` deviceType: ${device.bot?.deviceType})`);
|
|
65
|
+
|
|
64
66
|
// this is subject we use to track when we need to POST changes to the SwitchBot API
|
|
65
67
|
this.doBotUpdate = new Subject();
|
|
66
68
|
this.botUpdateInProgress = false;
|
|
@@ -80,7 +82,7 @@ export class Bot {
|
|
|
80
82
|
if (device.bot?.deviceType === 'switch') {
|
|
81
83
|
// If outletService still pressent, then remove first
|
|
82
84
|
if (this.outletService) {
|
|
83
|
-
this.
|
|
85
|
+
this.debugLog(`Bot: ${accessory.displayName} Removing Leftover outletService first`);
|
|
84
86
|
}
|
|
85
87
|
this.outletService = this.accessory.getService(this.platform.Service.Outlet);
|
|
86
88
|
accessory.removeService(this.outletService!);
|
|
@@ -89,13 +91,15 @@ export class Bot {
|
|
|
89
91
|
(this.switchService =
|
|
90
92
|
accessory.getService(this.platform.Service.Switch) ||
|
|
91
93
|
accessory.addService(this.platform.Service.Switch)), `${accessory.displayName} Switch`;
|
|
92
|
-
this.
|
|
94
|
+
if (this.deviceLogging !== 'none') {
|
|
95
|
+
this.infoLog(`Bot: ${accessory.displayName} Displaying as Switch`);
|
|
96
|
+
}
|
|
93
97
|
|
|
94
98
|
this.switchService?.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
|
|
95
99
|
} else {
|
|
96
100
|
// If switchService still pressent, then remove first
|
|
97
101
|
if (this.switchService) {
|
|
98
|
-
this.
|
|
102
|
+
this.debugLog(`Bot: ${accessory.displayName} Removing Leftover switchService first`);
|
|
99
103
|
}
|
|
100
104
|
this.switchService = this.accessory.getService(this.platform.Service.Switch);
|
|
101
105
|
accessory.removeService(this.switchService!);
|
|
@@ -104,7 +108,7 @@ export class Bot {
|
|
|
104
108
|
(this.outletService =
|
|
105
109
|
accessory.getService(this.platform.Service.Outlet) ||
|
|
106
110
|
accessory.addService(this.platform.Service.Outlet)), `${accessory.displayName} Outlet`;
|
|
107
|
-
this.
|
|
111
|
+
this.infoLog(`Bot: ${accessory.displayName} Displaying as Outlet`);
|
|
108
112
|
|
|
109
113
|
this.outletService?.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
|
|
110
114
|
}
|
|
@@ -130,7 +134,7 @@ export class Bot {
|
|
|
130
134
|
this.updateHomeKitCharacteristics();
|
|
131
135
|
|
|
132
136
|
// Start an update interval
|
|
133
|
-
interval(this.
|
|
137
|
+
interval(this.deviceRefreshRate * 1000)
|
|
134
138
|
.pipe(skipWhile(() => this.botUpdateInProgress))
|
|
135
139
|
.subscribe(() => {
|
|
136
140
|
this.refreshStatus();
|
|
@@ -149,13 +153,13 @@ export class Bot {
|
|
|
149
153
|
try {
|
|
150
154
|
await this.pushChanges();
|
|
151
155
|
} catch (e: any) {
|
|
152
|
-
this.
|
|
153
|
-
if (this.
|
|
154
|
-
this.
|
|
156
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges`);
|
|
157
|
+
if (this.deviceLogging === 'debug') {
|
|
158
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges,`
|
|
155
159
|
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
156
160
|
}
|
|
157
|
-
if (this.
|
|
158
|
-
this.
|
|
161
|
+
if (this.platform.debugMode) {
|
|
162
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges,`
|
|
159
163
|
+ ` Error: ${JSON.stringify(e)}`);
|
|
160
164
|
}
|
|
161
165
|
}
|
|
@@ -163,6 +167,24 @@ export class Bot {
|
|
|
163
167
|
});
|
|
164
168
|
}
|
|
165
169
|
|
|
170
|
+
refreshRate() {
|
|
171
|
+
if (this.device.refreshRate) {
|
|
172
|
+
this.deviceRefreshRate = this.accessory.context.refreshRate = this.device.refreshRate;
|
|
173
|
+
} else if (this.platform.config.options!.refreshRate) {
|
|
174
|
+
this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options!.refreshRate;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
logs() {
|
|
179
|
+
if (this.device.logging) {
|
|
180
|
+
this.deviceLogging = this.accessory.context.logging = this.device.logging;
|
|
181
|
+
} else if (this.platform.config.options?.logging) {
|
|
182
|
+
this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
|
|
183
|
+
} else {
|
|
184
|
+
this.deviceLogging = this.accessory.context.logging = 'standard';
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
166
188
|
/**
|
|
167
189
|
* Parse the device status from the SwitchBot api
|
|
168
190
|
*/
|
|
@@ -175,13 +197,13 @@ export class Bot {
|
|
|
175
197
|
}
|
|
176
198
|
|
|
177
199
|
private async BLEparseStatus() {
|
|
178
|
-
this.
|
|
200
|
+
this.debugLog(`Bot: ${this.accessory.displayName} BLE parseStatus`);
|
|
179
201
|
// BLEmode (true if Switch Mode) | (false if Press Mode)
|
|
180
202
|
if (this.mode) {
|
|
181
203
|
this.On = Boolean(this.state);
|
|
182
|
-
this.
|
|
204
|
+
this.debugLog(`Bot: ${this.accessory.displayName} Switch Mode, mode: ${JSON.stringify(this.mode)}, On: ${JSON.stringify(this.On)}`);
|
|
183
205
|
} else {
|
|
184
|
-
this.
|
|
206
|
+
this.debugLog(`Bot: ${this.accessory.displayName} Press Mode, mode: ${JSON.stringify(this.mode)}, On: ${JSON.stringify(this.On)}`);
|
|
185
207
|
}
|
|
186
208
|
|
|
187
209
|
this.BatteryLevel = Number(this.battery);
|
|
@@ -193,12 +215,12 @@ export class Bot {
|
|
|
193
215
|
if (Number.isNaN(this.BatteryLevel)) {
|
|
194
216
|
this.BatteryLevel = 100;
|
|
195
217
|
}
|
|
196
|
-
this.
|
|
218
|
+
this.debugLog(`Bot: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
|
|
197
219
|
}
|
|
198
220
|
|
|
199
221
|
private async openAPIparseStatus() {
|
|
200
222
|
if (this.platform.config.credentials?.openToken) {
|
|
201
|
-
this.
|
|
223
|
+
this.debugLog(`Bot: ${this.accessory.displayName} OpenAPI parseStatus`);
|
|
202
224
|
if (this.device.bot?.mode === 'press') {
|
|
203
225
|
this.On = false;
|
|
204
226
|
} else {
|
|
@@ -208,7 +230,7 @@ export class Bot {
|
|
|
208
230
|
this.On = false;
|
|
209
231
|
}
|
|
210
232
|
}
|
|
211
|
-
this.
|
|
233
|
+
this.debugLog(`Bot ${this.accessory.displayName} On: ${this.On}`);
|
|
212
234
|
}
|
|
213
235
|
}
|
|
214
236
|
|
|
@@ -226,12 +248,12 @@ export class Bot {
|
|
|
226
248
|
private connectBLE() {
|
|
227
249
|
const switchbot = new Switchbot();
|
|
228
250
|
this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
|
|
229
|
-
this.
|
|
251
|
+
this.debugLog(`Bot: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
230
252
|
return switchbot;
|
|
231
253
|
}
|
|
232
254
|
|
|
233
255
|
private async BLERefreshStatus() {
|
|
234
|
-
this.
|
|
256
|
+
this.debugLog(`Bot: ${this.accessory.displayName} BLE refreshStatus`);
|
|
235
257
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
236
258
|
const switchbot = this.connectBLE();
|
|
237
259
|
// Start to monitor advertisement packets
|
|
@@ -245,16 +267,16 @@ export class Bot {
|
|
|
245
267
|
this.mode = ad.serviceData.mode;
|
|
246
268
|
this.state = ad.serviceData.state;
|
|
247
269
|
this.battery = ad.serviceData.battery;
|
|
248
|
-
this.
|
|
249
|
-
this.
|
|
270
|
+
this.debugLog(`Bot: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
271
|
+
this.debugLog(`Bot: ${this.accessory.displayName}, model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName},`
|
|
250
272
|
+ ` mode: ${ad.serviceData.mode}, state: ${ad.serviceData.state}, battery: ${ad.serviceData.battery}`);
|
|
251
273
|
|
|
252
274
|
if (this.serviceData) {
|
|
253
275
|
this.connected = true;
|
|
254
|
-
this.
|
|
276
|
+
this.debugLog(`Bot: ${this.accessory.displayName} connected: ${this.connected}`);
|
|
255
277
|
} else {
|
|
256
278
|
this.connected = false;
|
|
257
|
-
this.
|
|
279
|
+
this.debugLog(`Bot: ${this.accessory.displayName} connected: ${this.connected}`);
|
|
258
280
|
}
|
|
259
281
|
};
|
|
260
282
|
// Wait 10 seconds
|
|
@@ -266,24 +288,24 @@ export class Bot {
|
|
|
266
288
|
this.parseStatus();
|
|
267
289
|
this.updateHomeKitCharacteristics();
|
|
268
290
|
} else {
|
|
269
|
-
this.
|
|
291
|
+
this.errorLog(`Bot: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
|
|
270
292
|
if (this.platform.config.credentials?.openToken) {
|
|
271
|
-
this.
|
|
293
|
+
this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
272
294
|
await this.openAPIRefreshStatus();
|
|
273
295
|
}
|
|
274
296
|
}
|
|
275
297
|
}).catch(async (e: any) => {
|
|
276
|
-
this.
|
|
277
|
-
if (this.
|
|
278
|
-
this.
|
|
298
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
|
|
299
|
+
if (this.deviceLogging === 'debug') {
|
|
300
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
|
|
279
301
|
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
280
302
|
}
|
|
281
|
-
if (this.
|
|
282
|
-
this.
|
|
303
|
+
if (this.platform.debugMode) {
|
|
304
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
|
|
283
305
|
+ ` Error: ${JSON.stringify(e)}`);
|
|
284
306
|
}
|
|
285
307
|
if (this.platform.config.credentials?.openToken) {
|
|
286
|
-
this.
|
|
308
|
+
this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
287
309
|
await this.openAPIRefreshStatus();
|
|
288
310
|
}
|
|
289
311
|
this.apiError();
|
|
@@ -292,10 +314,10 @@ export class Bot {
|
|
|
292
314
|
|
|
293
315
|
private async openAPIRefreshStatus() {
|
|
294
316
|
if (this.platform.config.credentials?.openToken) {
|
|
295
|
-
this.
|
|
317
|
+
this.debugLog(`Bot: ${this.accessory.displayName} OpenAPI refreshStatus`);
|
|
296
318
|
try {
|
|
297
319
|
this.deviceStatus = (await this.platform.axios.get(`${DeviceURL}/${this.device.deviceId}/status`)).data;
|
|
298
|
-
this.
|
|
320
|
+
this.debugLog(`Bot: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(this.deviceStatus)}`);
|
|
299
321
|
/*this.deviceStatus = {
|
|
300
322
|
statusCode: 100,
|
|
301
323
|
body: {
|
|
@@ -309,13 +331,13 @@ export class Bot {
|
|
|
309
331
|
this.parseStatus();
|
|
310
332
|
this.updateHomeKitCharacteristics();
|
|
311
333
|
} catch (e: any) {
|
|
312
|
-
this.
|
|
313
|
-
if (this.
|
|
314
|
-
this.
|
|
334
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
|
|
335
|
+
if (this.deviceLogging === 'debug') {
|
|
336
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
|
|
315
337
|
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
316
338
|
}
|
|
317
|
-
if (this.
|
|
318
|
-
this.
|
|
339
|
+
if (this.platform.debugMode) {
|
|
340
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
|
|
319
341
|
+ ` Error: ${JSON.stringify(e)}`);
|
|
320
342
|
}
|
|
321
343
|
this.apiError();
|
|
@@ -346,57 +368,57 @@ export class Bot {
|
|
|
346
368
|
|
|
347
369
|
private async BLEpushChanges() {
|
|
348
370
|
if (this.On !== this.OnCached) {
|
|
349
|
-
this.
|
|
371
|
+
this.debugLog(`Bot: ${this.accessory.displayName} BLE pushChanges`);
|
|
350
372
|
const switchbot = this.connectBLE();
|
|
351
373
|
if (this.device.bot?.mode === 'press') {
|
|
352
|
-
this.
|
|
374
|
+
this.debugLog(`Bot: ${this.accessory.displayName} Press Mode: ${this.device.bot?.mode}`);
|
|
353
375
|
switchbot.discover({ model: 'H', quick: true, id: this.device.bleMac })
|
|
354
376
|
.then((device_list: { press: (arg0: { id: string | undefined; }) => any; }[]) => {
|
|
355
|
-
this.
|
|
377
|
+
this.infoLog(`Bot: ${this.accessory.displayName}, On: ${this.On}`);
|
|
356
378
|
return device_list[0].press({ id: this.device.bleMac });
|
|
357
379
|
}).then(() => {
|
|
358
|
-
this.
|
|
380
|
+
this.debugLog(`Bot: ${this.accessory.displayName} Done.`);
|
|
359
381
|
}).catch(async (e: any) => {
|
|
360
|
-
this.
|
|
361
|
-
if (this.
|
|
362
|
-
this.
|
|
382
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
|
|
383
|
+
if (this.deviceLogging === 'debug') {
|
|
384
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
|
|
363
385
|
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
364
386
|
}
|
|
365
|
-
if (this.
|
|
366
|
-
this.
|
|
387
|
+
if (this.platform.debugMode) {
|
|
388
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
|
|
367
389
|
+ ` Error: ${JSON.stringify(e)}`);
|
|
368
390
|
}
|
|
369
391
|
if (this.platform.config.credentials?.openToken) {
|
|
370
|
-
this.
|
|
392
|
+
this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
371
393
|
await this.openAPIpushChanges();
|
|
372
394
|
}
|
|
373
395
|
this.apiError();
|
|
374
396
|
});
|
|
375
397
|
} else if (this.device.bot?.mode === 'switch') {
|
|
376
|
-
this.
|
|
398
|
+
this.debugLog(`Bot: ${this.accessory.displayName} Press Mode: ${this.device.bot?.mode}`);
|
|
377
399
|
switchbot.discover({ model: 'H', quick: true, id: this.device.bleMac }).then((device_list: any) => {
|
|
378
|
-
this.
|
|
400
|
+
this.infoLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
|
|
379
401
|
return this.turnOnOff(device_list);
|
|
380
402
|
}).then(() => {
|
|
381
|
-
this.
|
|
403
|
+
this.debugLog(`Bot: ${this.accessory.displayName} Done.`);
|
|
382
404
|
}).catch(async (e: any) => {
|
|
383
|
-
this.
|
|
384
|
-
if (this.
|
|
385
|
-
this.
|
|
405
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
|
|
406
|
+
if (this.deviceLogging === 'debug') {
|
|
407
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
|
|
386
408
|
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
387
409
|
}
|
|
388
|
-
if (this.
|
|
389
|
-
this.
|
|
410
|
+
if (this.platform.debugMode) {
|
|
411
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
|
|
390
412
|
+ ` Error: ${JSON.stringify(e)}`);
|
|
391
413
|
}
|
|
392
414
|
if (this.platform.config.credentials?.openToken) {
|
|
393
|
-
this.
|
|
415
|
+
this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
394
416
|
await this.openAPIpushChanges();
|
|
395
417
|
}
|
|
396
418
|
this.apiError();
|
|
397
419
|
});
|
|
398
420
|
} else {
|
|
399
|
-
this.
|
|
421
|
+
this.errorLog(`Bot: ${this.accessory.displayName} Mode Not Set, mode: ${this.device.bot?.mode}`);
|
|
400
422
|
}
|
|
401
423
|
}
|
|
402
424
|
this.OnCached = this.On;
|
|
@@ -415,7 +437,7 @@ export class Bot {
|
|
|
415
437
|
if (this.platform.config.credentials?.openToken) {
|
|
416
438
|
try {
|
|
417
439
|
if (this.On !== this.OnCached) {
|
|
418
|
-
this.
|
|
440
|
+
this.debugLog(`Bot: ${this.accessory.displayName} OpenAPI pushChanges`);
|
|
419
441
|
const payload = {
|
|
420
442
|
commandType: 'command',
|
|
421
443
|
parameter: 'default',
|
|
@@ -424,37 +446,37 @@ export class Bot {
|
|
|
424
446
|
if (this.device.bot?.mode === 'switch' && this.On) {
|
|
425
447
|
payload.command = 'turnOn';
|
|
426
448
|
this.On = true;
|
|
427
|
-
this.
|
|
449
|
+
this.debugLog(`Bot: ${this.accessory.displayName} Switch Mode, Turning ${this.On}`);
|
|
428
450
|
} else if (this.device.bot?.mode === 'switch' && !this.On) {
|
|
429
451
|
payload.command = 'turnOff';
|
|
430
452
|
this.On = false;
|
|
431
|
-
this.
|
|
453
|
+
this.debugLog(`Bot: ${this.accessory.displayName} Switch Mode, Turning ${this.On}`);
|
|
432
454
|
} else if (this.device.bot?.mode === 'press') {
|
|
433
455
|
payload.command = 'press';
|
|
434
|
-
this.
|
|
456
|
+
this.debugLog(`Bot: ${this.accessory.displayName} Press Mode`);
|
|
435
457
|
this.On = false;
|
|
436
458
|
} else {
|
|
437
459
|
throw new Error(`Bot: ${this.accessory.displayName} Device Paramters not set for this Bot.`);
|
|
438
460
|
}
|
|
439
461
|
|
|
440
|
-
this.
|
|
462
|
+
this.infoLog(`Bot: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
|
|
441
463
|
+ ` parameter: ${payload.parameter}, commandType: ${payload.commandType}`);
|
|
442
464
|
|
|
443
465
|
// Make the API request
|
|
444
466
|
const push: any = (await this.platform.axios.post(`${DeviceURL}/${this.device.deviceId}/commands`, payload));
|
|
445
|
-
this.
|
|
467
|
+
this.debugLog(`Bot ${this.accessory.displayName} pushchanges: ${JSON.stringify(push.data)}`);
|
|
446
468
|
this.statusCode(push);
|
|
447
469
|
this.OnCached = this.On;
|
|
448
470
|
this.accessory.context.On = this.OnCached;
|
|
449
471
|
}
|
|
450
472
|
} catch (e: any) {
|
|
451
|
-
this.
|
|
452
|
-
if (this.
|
|
453
|
-
this.
|
|
473
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
|
|
474
|
+
if (this.deviceLogging === 'debug') {
|
|
475
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
|
|
454
476
|
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
455
477
|
}
|
|
456
|
-
if (this.
|
|
457
|
-
this.
|
|
478
|
+
if (this.platform.debugMode) {
|
|
479
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
|
|
458
480
|
+ ` Error: ${JSON.stringify(e)}`);
|
|
459
481
|
}
|
|
460
482
|
this.apiError();
|
|
@@ -467,27 +489,27 @@ export class Bot {
|
|
|
467
489
|
*/
|
|
468
490
|
updateHomeKitCharacteristics() {
|
|
469
491
|
if (this.On === undefined) {
|
|
470
|
-
this.
|
|
492
|
+
this.debugLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
|
|
471
493
|
} else {
|
|
472
494
|
if (this.device.bot?.deviceType === 'switch') {
|
|
473
495
|
this.switchService?.updateCharacteristic(this.platform.Characteristic.On, this.On);
|
|
474
496
|
} else {
|
|
475
497
|
this.outletService?.updateCharacteristic(this.platform.Characteristic.On, this.On);
|
|
476
498
|
}
|
|
477
|
-
this.
|
|
499
|
+
this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
|
|
478
500
|
}
|
|
479
501
|
if (this.device.ble) {
|
|
480
502
|
if (this.BatteryLevel === undefined) {
|
|
481
|
-
this.
|
|
503
|
+
this.debugLog(`Bot: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
|
|
482
504
|
} else {
|
|
483
505
|
this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
|
|
484
|
-
this.
|
|
506
|
+
this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
|
|
485
507
|
}
|
|
486
508
|
if (this.StatusLowBattery === undefined) {
|
|
487
|
-
this.
|
|
509
|
+
this.debugLog(`Bot: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
|
|
488
510
|
} else {
|
|
489
511
|
this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
|
|
490
|
-
this.
|
|
512
|
+
this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
|
|
491
513
|
}
|
|
492
514
|
}
|
|
493
515
|
}
|
|
@@ -499,31 +521,31 @@ export class Bot {
|
|
|
499
521
|
private statusCode(push: AxiosResponse<{ statusCode: number; }>) {
|
|
500
522
|
switch (push.data.statusCode) {
|
|
501
523
|
case 151:
|
|
502
|
-
this.
|
|
524
|
+
this.errorLog(`Bot: ${this.accessory.displayName} Command not supported by this device type.`);
|
|
503
525
|
break;
|
|
504
526
|
case 152:
|
|
505
|
-
this.
|
|
527
|
+
this.errorLog(`Bot: ${this.accessory.displayName} Device not found.`);
|
|
506
528
|
break;
|
|
507
529
|
case 160:
|
|
508
|
-
this.
|
|
530
|
+
this.errorLog(`Bot: ${this.accessory.displayName} Command is not supported.`);
|
|
509
531
|
break;
|
|
510
532
|
case 161:
|
|
511
|
-
this.
|
|
533
|
+
this.errorLog(`Bot: ${this.accessory.displayName} Device is offline.`);
|
|
512
534
|
this.offlineOff();
|
|
513
535
|
break;
|
|
514
536
|
case 171:
|
|
515
|
-
this.
|
|
537
|
+
this.errorLog(`Bot: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
|
|
516
538
|
this.offlineOff();
|
|
517
539
|
break;
|
|
518
540
|
case 190:
|
|
519
|
-
this.
|
|
541
|
+
this.errorLog(`Bot: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,`
|
|
520
542
|
+ ` Or command: ${JSON.stringify(push.data)} format is invalid`);
|
|
521
543
|
break;
|
|
522
544
|
case 100:
|
|
523
|
-
this.
|
|
545
|
+
this.debugLog(`Bot: ${this.accessory.displayName} Command successfully sent.`);
|
|
524
546
|
break;
|
|
525
547
|
default:
|
|
526
|
-
this.
|
|
548
|
+
this.debugLog(`Bot: ${this.accessory.displayName} Unknown statusCode.`);
|
|
527
549
|
}
|
|
528
550
|
}
|
|
529
551
|
|
|
@@ -542,8 +564,43 @@ export class Bot {
|
|
|
542
564
|
* Handle requests to set the "On" characteristic
|
|
543
565
|
*/
|
|
544
566
|
private handleOnSet(value: CharacteristicValue) {
|
|
545
|
-
this.
|
|
567
|
+
this.debugLog(`Bot: ${this.accessory.displayName} On: ${value}`);
|
|
546
568
|
this.On = value;
|
|
547
569
|
this.doBotUpdate.next();
|
|
548
570
|
}
|
|
571
|
+
|
|
572
|
+
/**
|
|
573
|
+
* Logging for Device
|
|
574
|
+
*/
|
|
575
|
+
infoLog(...log: any[]) {
|
|
576
|
+
if (this.enablingDeviceLogging()) {
|
|
577
|
+
this.platform.log.info(String(...log));
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
warnLog(...log: any[]) {
|
|
582
|
+
if (this.enablingDeviceLogging()) {
|
|
583
|
+
this.platform.log.warn(String(...log));
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
errorLog(...log: any[]) {
|
|
588
|
+
if (this.enablingDeviceLogging()) {
|
|
589
|
+
this.platform.log.error(String(...log));
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
debugLog(...log: any[]) {
|
|
594
|
+
if (this.enablingDeviceLogging()) {
|
|
595
|
+
if (this.deviceLogging === 'debug') {
|
|
596
|
+
this.platform.log.info('[DEBUG]', String(...log));
|
|
597
|
+
} else {
|
|
598
|
+
this.platform.log.debug(String(...log));
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
enablingDeviceLogging(): boolean {
|
|
604
|
+
return this.deviceLogging === 'debug' || this.deviceLogging === 'standard';
|
|
605
|
+
}
|
|
549
606
|
}
|