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