@switchbot/homebridge-switchbot 1.4.1-beta.0 → 1.4.1-beta.4
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 +20 -0
- package/config.schema.json +83 -11
- package/dist/devices/bots.d.ts +12 -2
- package/dist/devices/bots.d.ts.map +1 -1
- package/dist/devices/bots.js +155 -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 +153 -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 +116 -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 +191 -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 +171 -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 +113 -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 +101 -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 +95 -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 +10 -10
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +290 -307
- 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 +10 -6
- package/src/devices/bots.ts +161 -89
- package/src/devices/colorbulb.ts +161 -86
- package/src/devices/contact.ts +122 -52
- package/src/devices/curtains.ts +199 -128
- package/src/devices/humidifiers.ts +177 -109
- package/src/devices/meters.ts +119 -49
- package/src/devices/motion.ts +106 -36
- package/src/devices/plugs.ts +99 -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 +285 -302
- 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,39 @@ 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
|
+
if (this.platform.debugMode) {
|
|
174
|
+
this.warnLog(`Using Device Config refreshRate: ${this.deviceRefreshRate}`);
|
|
175
|
+
}
|
|
176
|
+
} else if (this.platform.config.options!.refreshRate) {
|
|
177
|
+
this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options!.refreshRate;
|
|
178
|
+
if (this.platform.debugMode) {
|
|
179
|
+
this.warnLog(`Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
logs() {
|
|
185
|
+
if (this.device.logging) {
|
|
186
|
+
this.deviceLogging = this.accessory.context.logging = this.device.logging;
|
|
187
|
+
if (this.platform.debugMode) {
|
|
188
|
+
this.warnLog(`Using Device Config Logging: ${this.deviceLogging}`);
|
|
189
|
+
}
|
|
190
|
+
} else if (this.platform.config.options?.logging) {
|
|
191
|
+
this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
|
|
192
|
+
if (this.platform.debugMode) {
|
|
193
|
+
this.warnLog(`Using Platform Config Logging: ${this.deviceLogging}`);
|
|
194
|
+
}
|
|
195
|
+
} else {
|
|
196
|
+
this.deviceLogging = this.accessory.context.logging = 'standard';
|
|
197
|
+
if (this.platform.debugMode) {
|
|
198
|
+
this.warnLog('Using Device Standard Logging');
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
166
203
|
/**
|
|
167
204
|
* Parse the device status from the SwitchBot api
|
|
168
205
|
*/
|
|
@@ -175,13 +212,13 @@ export class Bot {
|
|
|
175
212
|
}
|
|
176
213
|
|
|
177
214
|
private async BLEparseStatus() {
|
|
178
|
-
this.
|
|
215
|
+
this.debugLog(`Bot: ${this.accessory.displayName} BLE parseStatus`);
|
|
179
216
|
// BLEmode (true if Switch Mode) | (false if Press Mode)
|
|
180
217
|
if (this.mode) {
|
|
181
218
|
this.On = Boolean(this.state);
|
|
182
|
-
this.
|
|
219
|
+
this.debugLog(`Bot: ${this.accessory.displayName} Switch Mode, mode: ${JSON.stringify(this.mode)}, On: ${JSON.stringify(this.On)}`);
|
|
183
220
|
} else {
|
|
184
|
-
this.
|
|
221
|
+
this.debugLog(`Bot: ${this.accessory.displayName} Press Mode, mode: ${JSON.stringify(this.mode)}, On: ${JSON.stringify(this.On)}`);
|
|
185
222
|
}
|
|
186
223
|
|
|
187
224
|
this.BatteryLevel = Number(this.battery);
|
|
@@ -193,12 +230,12 @@ export class Bot {
|
|
|
193
230
|
if (Number.isNaN(this.BatteryLevel)) {
|
|
194
231
|
this.BatteryLevel = 100;
|
|
195
232
|
}
|
|
196
|
-
this.
|
|
233
|
+
this.debugLog(`Bot: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
|
|
197
234
|
}
|
|
198
235
|
|
|
199
236
|
private async openAPIparseStatus() {
|
|
200
237
|
if (this.platform.config.credentials?.openToken) {
|
|
201
|
-
this.
|
|
238
|
+
this.debugLog(`Bot: ${this.accessory.displayName} OpenAPI parseStatus`);
|
|
202
239
|
if (this.device.bot?.mode === 'press') {
|
|
203
240
|
this.On = false;
|
|
204
241
|
} else {
|
|
@@ -208,7 +245,7 @@ export class Bot {
|
|
|
208
245
|
this.On = false;
|
|
209
246
|
}
|
|
210
247
|
}
|
|
211
|
-
this.
|
|
248
|
+
this.debugLog(`Bot ${this.accessory.displayName} On: ${this.On}`);
|
|
212
249
|
}
|
|
213
250
|
}
|
|
214
251
|
|
|
@@ -226,12 +263,12 @@ export class Bot {
|
|
|
226
263
|
private connectBLE() {
|
|
227
264
|
const switchbot = new Switchbot();
|
|
228
265
|
this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
|
|
229
|
-
this.
|
|
266
|
+
this.debugLog(`Bot: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
230
267
|
return switchbot;
|
|
231
268
|
}
|
|
232
269
|
|
|
233
270
|
private async BLERefreshStatus() {
|
|
234
|
-
this.
|
|
271
|
+
this.debugLog(`Bot: ${this.accessory.displayName} BLE refreshStatus`);
|
|
235
272
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
236
273
|
const switchbot = this.connectBLE();
|
|
237
274
|
// Start to monitor advertisement packets
|
|
@@ -245,16 +282,16 @@ export class Bot {
|
|
|
245
282
|
this.mode = ad.serviceData.mode;
|
|
246
283
|
this.state = ad.serviceData.state;
|
|
247
284
|
this.battery = ad.serviceData.battery;
|
|
248
|
-
this.
|
|
249
|
-
this.
|
|
285
|
+
this.debugLog(`Bot: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
286
|
+
this.debugLog(`Bot: ${this.accessory.displayName}, model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName},`
|
|
250
287
|
+ ` mode: ${ad.serviceData.mode}, state: ${ad.serviceData.state}, battery: ${ad.serviceData.battery}`);
|
|
251
288
|
|
|
252
289
|
if (this.serviceData) {
|
|
253
290
|
this.connected = true;
|
|
254
|
-
this.
|
|
291
|
+
this.debugLog(`Bot: ${this.accessory.displayName} connected: ${this.connected}`);
|
|
255
292
|
} else {
|
|
256
293
|
this.connected = false;
|
|
257
|
-
this.
|
|
294
|
+
this.debugLog(`Bot: ${this.accessory.displayName} connected: ${this.connected}`);
|
|
258
295
|
}
|
|
259
296
|
};
|
|
260
297
|
// Wait 10 seconds
|
|
@@ -266,24 +303,24 @@ export class Bot {
|
|
|
266
303
|
this.parseStatus();
|
|
267
304
|
this.updateHomeKitCharacteristics();
|
|
268
305
|
} else {
|
|
269
|
-
this.
|
|
306
|
+
this.errorLog(`Bot: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
|
|
270
307
|
if (this.platform.config.credentials?.openToken) {
|
|
271
|
-
this.
|
|
308
|
+
this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
272
309
|
await this.openAPIRefreshStatus();
|
|
273
310
|
}
|
|
274
311
|
}
|
|
275
312
|
}).catch(async (e: any) => {
|
|
276
|
-
this.
|
|
277
|
-
if (this.
|
|
278
|
-
this.
|
|
313
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
|
|
314
|
+
if (this.deviceLogging === 'debug') {
|
|
315
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
|
|
279
316
|
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
280
317
|
}
|
|
281
|
-
if (this.
|
|
282
|
-
this.
|
|
318
|
+
if (this.platform.debugMode) {
|
|
319
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
|
|
283
320
|
+ ` Error: ${JSON.stringify(e)}`);
|
|
284
321
|
}
|
|
285
322
|
if (this.platform.config.credentials?.openToken) {
|
|
286
|
-
this.
|
|
323
|
+
this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
287
324
|
await this.openAPIRefreshStatus();
|
|
288
325
|
}
|
|
289
326
|
this.apiError();
|
|
@@ -292,10 +329,10 @@ export class Bot {
|
|
|
292
329
|
|
|
293
330
|
private async openAPIRefreshStatus() {
|
|
294
331
|
if (this.platform.config.credentials?.openToken) {
|
|
295
|
-
this.
|
|
332
|
+
this.debugLog(`Bot: ${this.accessory.displayName} OpenAPI refreshStatus`);
|
|
296
333
|
try {
|
|
297
334
|
this.deviceStatus = (await this.platform.axios.get(`${DeviceURL}/${this.device.deviceId}/status`)).data;
|
|
298
|
-
this.
|
|
335
|
+
this.debugLog(`Bot: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(this.deviceStatus)}`);
|
|
299
336
|
/*this.deviceStatus = {
|
|
300
337
|
statusCode: 100,
|
|
301
338
|
body: {
|
|
@@ -309,13 +346,13 @@ export class Bot {
|
|
|
309
346
|
this.parseStatus();
|
|
310
347
|
this.updateHomeKitCharacteristics();
|
|
311
348
|
} catch (e: any) {
|
|
312
|
-
this.
|
|
313
|
-
if (this.
|
|
314
|
-
this.
|
|
349
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
|
|
350
|
+
if (this.deviceLogging === 'debug') {
|
|
351
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
|
|
315
352
|
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
316
353
|
}
|
|
317
|
-
if (this.
|
|
318
|
-
this.
|
|
354
|
+
if (this.platform.debugMode) {
|
|
355
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
|
|
319
356
|
+ ` Error: ${JSON.stringify(e)}`);
|
|
320
357
|
}
|
|
321
358
|
this.apiError();
|
|
@@ -346,57 +383,57 @@ export class Bot {
|
|
|
346
383
|
|
|
347
384
|
private async BLEpushChanges() {
|
|
348
385
|
if (this.On !== this.OnCached) {
|
|
349
|
-
this.
|
|
386
|
+
this.debugLog(`Bot: ${this.accessory.displayName} BLE pushChanges`);
|
|
350
387
|
const switchbot = this.connectBLE();
|
|
351
388
|
if (this.device.bot?.mode === 'press') {
|
|
352
|
-
this.
|
|
389
|
+
this.debugLog(`Bot: ${this.accessory.displayName} Press Mode: ${this.device.bot?.mode}`);
|
|
353
390
|
switchbot.discover({ model: 'H', quick: true, id: this.device.bleMac })
|
|
354
391
|
.then((device_list: { press: (arg0: { id: string | undefined; }) => any; }[]) => {
|
|
355
|
-
this.
|
|
392
|
+
this.infoLog(`Bot: ${this.accessory.displayName}, On: ${this.On}`);
|
|
356
393
|
return device_list[0].press({ id: this.device.bleMac });
|
|
357
394
|
}).then(() => {
|
|
358
|
-
this.
|
|
395
|
+
this.debugLog(`Bot: ${this.accessory.displayName} Done.`);
|
|
359
396
|
}).catch(async (e: any) => {
|
|
360
|
-
this.
|
|
361
|
-
if (this.
|
|
362
|
-
this.
|
|
397
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
|
|
398
|
+
if (this.deviceLogging === 'debug') {
|
|
399
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
|
|
363
400
|
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
364
401
|
}
|
|
365
|
-
if (this.
|
|
366
|
-
this.
|
|
402
|
+
if (this.platform.debugMode) {
|
|
403
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
|
|
367
404
|
+ ` Error: ${JSON.stringify(e)}`);
|
|
368
405
|
}
|
|
369
406
|
if (this.platform.config.credentials?.openToken) {
|
|
370
|
-
this.
|
|
407
|
+
this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
371
408
|
await this.openAPIpushChanges();
|
|
372
409
|
}
|
|
373
410
|
this.apiError();
|
|
374
411
|
});
|
|
375
412
|
} else if (this.device.bot?.mode === 'switch') {
|
|
376
|
-
this.
|
|
413
|
+
this.debugLog(`Bot: ${this.accessory.displayName} Press Mode: ${this.device.bot?.mode}`);
|
|
377
414
|
switchbot.discover({ model: 'H', quick: true, id: this.device.bleMac }).then((device_list: any) => {
|
|
378
|
-
this.
|
|
415
|
+
this.infoLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
|
|
379
416
|
return this.turnOnOff(device_list);
|
|
380
417
|
}).then(() => {
|
|
381
|
-
this.
|
|
418
|
+
this.debugLog(`Bot: ${this.accessory.displayName} Done.`);
|
|
382
419
|
}).catch(async (e: any) => {
|
|
383
|
-
this.
|
|
384
|
-
if (this.
|
|
385
|
-
this.
|
|
420
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
|
|
421
|
+
if (this.deviceLogging === 'debug') {
|
|
422
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
|
|
386
423
|
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
387
424
|
}
|
|
388
|
-
if (this.
|
|
389
|
-
this.
|
|
425
|
+
if (this.platform.debugMode) {
|
|
426
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
|
|
390
427
|
+ ` Error: ${JSON.stringify(e)}`);
|
|
391
428
|
}
|
|
392
429
|
if (this.platform.config.credentials?.openToken) {
|
|
393
|
-
this.
|
|
430
|
+
this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
394
431
|
await this.openAPIpushChanges();
|
|
395
432
|
}
|
|
396
433
|
this.apiError();
|
|
397
434
|
});
|
|
398
435
|
} else {
|
|
399
|
-
this.
|
|
436
|
+
this.errorLog(`Bot: ${this.accessory.displayName} Mode Not Set, mode: ${this.device.bot?.mode}`);
|
|
400
437
|
}
|
|
401
438
|
}
|
|
402
439
|
this.OnCached = this.On;
|
|
@@ -415,7 +452,7 @@ export class Bot {
|
|
|
415
452
|
if (this.platform.config.credentials?.openToken) {
|
|
416
453
|
try {
|
|
417
454
|
if (this.On !== this.OnCached) {
|
|
418
|
-
this.
|
|
455
|
+
this.debugLog(`Bot: ${this.accessory.displayName} OpenAPI pushChanges`);
|
|
419
456
|
const payload = {
|
|
420
457
|
commandType: 'command',
|
|
421
458
|
parameter: 'default',
|
|
@@ -424,37 +461,37 @@ export class Bot {
|
|
|
424
461
|
if (this.device.bot?.mode === 'switch' && this.On) {
|
|
425
462
|
payload.command = 'turnOn';
|
|
426
463
|
this.On = true;
|
|
427
|
-
this.
|
|
464
|
+
this.debugLog(`Bot: ${this.accessory.displayName} Switch Mode, Turning ${this.On}`);
|
|
428
465
|
} else if (this.device.bot?.mode === 'switch' && !this.On) {
|
|
429
466
|
payload.command = 'turnOff';
|
|
430
467
|
this.On = false;
|
|
431
|
-
this.
|
|
468
|
+
this.debugLog(`Bot: ${this.accessory.displayName} Switch Mode, Turning ${this.On}`);
|
|
432
469
|
} else if (this.device.bot?.mode === 'press') {
|
|
433
470
|
payload.command = 'press';
|
|
434
|
-
this.
|
|
471
|
+
this.debugLog(`Bot: ${this.accessory.displayName} Press Mode`);
|
|
435
472
|
this.On = false;
|
|
436
473
|
} else {
|
|
437
474
|
throw new Error(`Bot: ${this.accessory.displayName} Device Paramters not set for this Bot.`);
|
|
438
475
|
}
|
|
439
476
|
|
|
440
|
-
this.
|
|
477
|
+
this.infoLog(`Bot: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
|
|
441
478
|
+ ` parameter: ${payload.parameter}, commandType: ${payload.commandType}`);
|
|
442
479
|
|
|
443
480
|
// Make the API request
|
|
444
481
|
const push: any = (await this.platform.axios.post(`${DeviceURL}/${this.device.deviceId}/commands`, payload));
|
|
445
|
-
this.
|
|
482
|
+
this.debugLog(`Bot ${this.accessory.displayName} pushchanges: ${JSON.stringify(push.data)}`);
|
|
446
483
|
this.statusCode(push);
|
|
447
484
|
this.OnCached = this.On;
|
|
448
485
|
this.accessory.context.On = this.OnCached;
|
|
449
486
|
}
|
|
450
487
|
} catch (e: any) {
|
|
451
|
-
this.
|
|
452
|
-
if (this.
|
|
453
|
-
this.
|
|
488
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
|
|
489
|
+
if (this.deviceLogging === 'debug') {
|
|
490
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
|
|
454
491
|
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
455
492
|
}
|
|
456
|
-
if (this.
|
|
457
|
-
this.
|
|
493
|
+
if (this.platform.debugMode) {
|
|
494
|
+
this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
|
|
458
495
|
+ ` Error: ${JSON.stringify(e)}`);
|
|
459
496
|
}
|
|
460
497
|
this.apiError();
|
|
@@ -467,27 +504,27 @@ export class Bot {
|
|
|
467
504
|
*/
|
|
468
505
|
updateHomeKitCharacteristics() {
|
|
469
506
|
if (this.On === undefined) {
|
|
470
|
-
this.
|
|
507
|
+
this.debugLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
|
|
471
508
|
} else {
|
|
472
509
|
if (this.device.bot?.deviceType === 'switch') {
|
|
473
510
|
this.switchService?.updateCharacteristic(this.platform.Characteristic.On, this.On);
|
|
474
511
|
} else {
|
|
475
512
|
this.outletService?.updateCharacteristic(this.platform.Characteristic.On, this.On);
|
|
476
513
|
}
|
|
477
|
-
this.
|
|
514
|
+
this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
|
|
478
515
|
}
|
|
479
516
|
if (this.device.ble) {
|
|
480
517
|
if (this.BatteryLevel === undefined) {
|
|
481
|
-
this.
|
|
518
|
+
this.debugLog(`Bot: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
|
|
482
519
|
} else {
|
|
483
520
|
this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
|
|
484
|
-
this.
|
|
521
|
+
this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
|
|
485
522
|
}
|
|
486
523
|
if (this.StatusLowBattery === undefined) {
|
|
487
|
-
this.
|
|
524
|
+
this.debugLog(`Bot: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
|
|
488
525
|
} else {
|
|
489
526
|
this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
|
|
490
|
-
this.
|
|
527
|
+
this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
|
|
491
528
|
}
|
|
492
529
|
}
|
|
493
530
|
}
|
|
@@ -499,31 +536,31 @@ export class Bot {
|
|
|
499
536
|
private statusCode(push: AxiosResponse<{ statusCode: number; }>) {
|
|
500
537
|
switch (push.data.statusCode) {
|
|
501
538
|
case 151:
|
|
502
|
-
this.
|
|
539
|
+
this.errorLog(`Bot: ${this.accessory.displayName} Command not supported by this device type.`);
|
|
503
540
|
break;
|
|
504
541
|
case 152:
|
|
505
|
-
this.
|
|
542
|
+
this.errorLog(`Bot: ${this.accessory.displayName} Device not found.`);
|
|
506
543
|
break;
|
|
507
544
|
case 160:
|
|
508
|
-
this.
|
|
545
|
+
this.errorLog(`Bot: ${this.accessory.displayName} Command is not supported.`);
|
|
509
546
|
break;
|
|
510
547
|
case 161:
|
|
511
|
-
this.
|
|
548
|
+
this.errorLog(`Bot: ${this.accessory.displayName} Device is offline.`);
|
|
512
549
|
this.offlineOff();
|
|
513
550
|
break;
|
|
514
551
|
case 171:
|
|
515
|
-
this.
|
|
552
|
+
this.errorLog(`Bot: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
|
|
516
553
|
this.offlineOff();
|
|
517
554
|
break;
|
|
518
555
|
case 190:
|
|
519
|
-
this.
|
|
556
|
+
this.errorLog(`Bot: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,`
|
|
520
557
|
+ ` Or command: ${JSON.stringify(push.data)} format is invalid`);
|
|
521
558
|
break;
|
|
522
559
|
case 100:
|
|
523
|
-
this.
|
|
560
|
+
this.debugLog(`Bot: ${this.accessory.displayName} Command successfully sent.`);
|
|
524
561
|
break;
|
|
525
562
|
default:
|
|
526
|
-
this.
|
|
563
|
+
this.debugLog(`Bot: ${this.accessory.displayName} Unknown statusCode.`);
|
|
527
564
|
}
|
|
528
565
|
}
|
|
529
566
|
|
|
@@ -542,8 +579,43 @@ export class Bot {
|
|
|
542
579
|
* Handle requests to set the "On" characteristic
|
|
543
580
|
*/
|
|
544
581
|
private handleOnSet(value: CharacteristicValue) {
|
|
545
|
-
this.
|
|
582
|
+
this.debugLog(`Bot: ${this.accessory.displayName} On: ${value}`);
|
|
546
583
|
this.On = value;
|
|
547
584
|
this.doBotUpdate.next();
|
|
548
585
|
}
|
|
586
|
+
|
|
587
|
+
/**
|
|
588
|
+
* Logging for Device
|
|
589
|
+
*/
|
|
590
|
+
infoLog(...log: any[]) {
|
|
591
|
+
if (this.enablingDeviceLogging()) {
|
|
592
|
+
this.platform.log.info(String(...log));
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
warnLog(...log: any[]) {
|
|
597
|
+
if (this.enablingDeviceLogging()) {
|
|
598
|
+
this.platform.log.warn(String(...log));
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
errorLog(...log: any[]) {
|
|
603
|
+
if (this.enablingDeviceLogging()) {
|
|
604
|
+
this.platform.log.error(String(...log));
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
debugLog(...log: any[]) {
|
|
609
|
+
if (this.enablingDeviceLogging()) {
|
|
610
|
+
if (this.deviceLogging === 'debug') {
|
|
611
|
+
this.platform.log.info('[DEBUG]', String(...log));
|
|
612
|
+
} else {
|
|
613
|
+
this.platform.log.debug(String(...log));
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
enablingDeviceLogging(): boolean {
|
|
619
|
+
return this.deviceLogging === 'debug' || this.deviceLogging === 'standard';
|
|
620
|
+
}
|
|
549
621
|
}
|