@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.
Files changed (103) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/config.schema.json +83 -11
  3. package/dist/devices/bots.d.ts +13 -3
  4. package/dist/devices/bots.d.ts.map +1 -1
  5. package/dist/devices/bots.js +172 -106
  6. package/dist/devices/bots.js.map +1 -1
  7. package/dist/devices/colorbulb.d.ts +13 -3
  8. package/dist/devices/colorbulb.d.ts.map +1 -1
  9. package/dist/devices/colorbulb.js +166 -92
  10. package/dist/devices/colorbulb.js.map +1 -1
  11. package/dist/devices/contact.d.ts +13 -3
  12. package/dist/devices/contact.d.ts.map +1 -1
  13. package/dist/devices/contact.js +131 -56
  14. package/dist/devices/contact.js.map +1 -1
  15. package/dist/devices/curtains.d.ts +14 -5
  16. package/dist/devices/curtains.d.ts.map +1 -1
  17. package/dist/devices/curtains.js +208 -131
  18. package/dist/devices/curtains.js.map +1 -1
  19. package/dist/devices/humidifiers.d.ts +13 -3
  20. package/dist/devices/humidifiers.d.ts.map +1 -1
  21. package/dist/devices/humidifiers.js +191 -117
  22. package/dist/devices/humidifiers.js.map +1 -1
  23. package/dist/devices/meters.d.ts +13 -3
  24. package/dist/devices/meters.d.ts.map +1 -1
  25. package/dist/devices/meters.js +125 -53
  26. package/dist/devices/meters.js.map +1 -1
  27. package/dist/devices/motion.d.ts +13 -3
  28. package/dist/devices/motion.d.ts.map +1 -1
  29. package/dist/devices/motion.js +111 -41
  30. package/dist/devices/motion.js.map +1 -1
  31. package/dist/devices/plugs.d.ts +13 -3
  32. package/dist/devices/plugs.d.ts.map +1 -1
  33. package/dist/devices/plugs.js +100 -35
  34. package/dist/devices/plugs.js.map +1 -1
  35. package/dist/irdevices/airconditioners.d.ts +11 -6
  36. package/dist/irdevices/airconditioners.d.ts.map +1 -1
  37. package/dist/irdevices/airconditioners.js +91 -85
  38. package/dist/irdevices/airconditioners.js.map +1 -1
  39. package/dist/irdevices/airpurifiers.d.ts +11 -3
  40. package/dist/irdevices/airpurifiers.d.ts.map +1 -1
  41. package/dist/irdevices/airpurifiers.js +72 -28
  42. package/dist/irdevices/airpurifiers.js.map +1 -1
  43. package/dist/irdevices/cameras.d.ts +11 -3
  44. package/dist/irdevices/cameras.d.ts.map +1 -1
  45. package/dist/irdevices/cameras.js +65 -24
  46. package/dist/irdevices/cameras.js.map +1 -1
  47. package/dist/irdevices/fans.d.ts +11 -3
  48. package/dist/irdevices/fans.d.ts.map +1 -1
  49. package/dist/irdevices/fans.js +77 -33
  50. package/dist/irdevices/fans.js.map +1 -1
  51. package/dist/irdevices/lights.d.ts +11 -3
  52. package/dist/irdevices/lights.d.ts.map +1 -1
  53. package/dist/irdevices/lights.js +66 -25
  54. package/dist/irdevices/lights.js.map +1 -1
  55. package/dist/irdevices/others.d.ts +11 -3
  56. package/dist/irdevices/others.d.ts.map +1 -1
  57. package/dist/irdevices/others.js +72 -31
  58. package/dist/irdevices/others.js.map +1 -1
  59. package/dist/irdevices/tvs.d.ts +11 -3
  60. package/dist/irdevices/tvs.d.ts.map +1 -1
  61. package/dist/irdevices/tvs.js +83 -41
  62. package/dist/irdevices/tvs.js.map +1 -1
  63. package/dist/irdevices/vacuumcleaners.d.ts +11 -3
  64. package/dist/irdevices/vacuumcleaners.d.ts.map +1 -1
  65. package/dist/irdevices/vacuumcleaners.js +65 -24
  66. package/dist/irdevices/vacuumcleaners.js.map +1 -1
  67. package/dist/irdevices/waterheaters.d.ts +11 -3
  68. package/dist/irdevices/waterheaters.d.ts.map +1 -1
  69. package/dist/irdevices/waterheaters.js +65 -24
  70. package/dist/irdevices/waterheaters.js.map +1 -1
  71. package/dist/platform.d.ts +9 -10
  72. package/dist/platform.d.ts.map +1 -1
  73. package/dist/platform.js +264 -285
  74. package/dist/platform.js.map +1 -1
  75. package/dist/settings.d.ts +4 -1
  76. package/dist/settings.d.ts.map +1 -1
  77. package/dist/settings.js.map +1 -1
  78. package/nodemon.json +1 -1
  79. package/package.json +10 -6
  80. package/src/devices/bots.ts +178 -107
  81. package/src/devices/colorbulb.ts +175 -95
  82. package/src/devices/contact.ts +138 -57
  83. package/src/devices/curtains.ts +216 -135
  84. package/src/devices/humidifiers.ts +198 -119
  85. package/src/devices/meters.ts +132 -54
  86. package/src/devices/motion.ts +117 -41
  87. package/src/devices/plugs.ts +105 -36
  88. package/src/irdevices/airconditioners.ts +96 -89
  89. package/src/irdevices/airpurifiers.ts +77 -28
  90. package/src/irdevices/cameras.ts +70 -24
  91. package/src/irdevices/fans.ts +82 -34
  92. package/src/irdevices/lights.ts +71 -25
  93. package/src/irdevices/others.ts +76 -30
  94. package/src/irdevices/tvs.ts +88 -41
  95. package/src/irdevices/vacuumcleaners.ts +70 -24
  96. package/src/irdevices/waterheaters.ts +70 -24
  97. package/src/platform.ts +267 -290
  98. package/src/settings.ts +4 -1
  99. package/dist/devices/indoorcam.d.ts +0 -64
  100. package/dist/devices/indoorcam.d.ts.map +0 -1
  101. package/dist/devices/indoorcam.js +0 -249
  102. package/dist/devices/indoorcam.js.map +0 -1
  103. package/src/devices/indoorcam.ts +0 -309
@@ -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.platform.device(`Bot: ${accessory.displayName} Removing Leftover outletService first`);
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.platform.log.info(`Bot: ${accessory.displayName} Displaying as Switch`);
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.platform.device(`Bot: ${accessory.displayName} Removing Leftover switchService first`);
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.platform.log.info(`Bot: ${accessory.displayName} Displaying as Outlet`);
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.platform.config.options.refreshRate * 1000)
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.platform.log.error(`Bot: ${this.accessory.displayName} failed pushChanges`);
111
- if (this.deviceDebug) {
112
- this.platform.log.error(`Bot: ${this.accessory.displayName} failed pushChanges,`
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.debugDebug) {
116
- this.platform.log.error(`Bot: ${this.accessory.displayName} failed pushChanges,`
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.platform.debug(`Bot: ${this.accessory.displayName} BLE parseStatus`);
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.platform.device(`Bot: ${this.accessory.displayName} Switch Mode, mode: ${JSON.stringify(this.mode)}, On: ${JSON.stringify(this.On)}`);
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.platform.device(`Bot: ${this.accessory.displayName} Press Mode, mode: ${JSON.stringify(this.mode)}, On: ${JSON.stringify(this.On)}`);
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.platform.debug(`Bot: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
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.platform.debug(`Bot: ${this.accessory.displayName} OpenAPI parseStatus`);
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.platform.debug(`Bot ${this.accessory.displayName} On: ${this.On}`);
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.platform.device(`Bot: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
222
+ this.debugLog(`Bot: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
188
223
  return switchbot;
189
224
  }
190
225
  async BLERefreshStatus() {
191
- this.platform.debug(`Bot: ${this.accessory.displayName} BLE refreshStatus`);
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.platform.device(`Bot: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
206
- this.platform.device(`Bot: ${this.accessory.displayName}, model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName},`
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.platform.device(`Bot: ${this.accessory.displayName} connected: ${this.connected}`);
245
+ this.debugLog(`Bot: ${this.accessory.displayName} connected: ${this.connected}`);
211
246
  }
212
247
  else {
213
248
  this.connected = false;
214
- this.platform.device(`Bot: ${this.accessory.displayName} connected: ${this.connected}`);
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.platform.log.error(`Bot: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
262
+ this.errorLog(`Bot: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
228
263
  if (this.platform.config.credentials?.openToken) {
229
- this.platform.log.warn(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
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.platform.log.error(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
235
- if (this.deviceDebug) {
236
- this.platform.log.error(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
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.debugDebug) {
240
- this.platform.log.error(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
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.platform.log.warn(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
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.platform.debug(`Bot: ${this.accessory.displayName} OpenAPI refreshStatus`);
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.platform.device(`Bot: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(this.deviceStatus)}`);
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.platform.log.error(`Bot: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
271
- if (this.deviceDebug) {
272
- this.platform.log.error(`Bot: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
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.debugDebug) {
276
- this.platform.log.error(`Bot: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
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.platform.debug(`Bot: ${this.accessory.displayName} BLE pushChanges`);
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.platform.device(`Bot: ${this.accessory.displayName} Press Mode: ${this.device.bot?.mode}`);
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.platform.log.info(`Bot: ${this.accessory.displayName}, On: ${this.On}`);
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.platform.device(`Bot: ${this.accessory.displayName} Done.`);
340
+ this.debugLog(`Bot: ${this.accessory.displayName} Done.`);
316
341
  }).catch(async (e) => {
317
- this.platform.log.error(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
318
- if (this.deviceDebug) {
319
- this.platform.log.error(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
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.debugDebug) {
323
- this.platform.log.error(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
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.platform.log.warn(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
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.platform.device(`Bot: ${this.accessory.displayName} Press Mode: ${this.device.bot?.mode}`);
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.platform.log.info(`Bot: ${this.accessory.displayName} On: ${this.On}`);
361
+ this.infoLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
337
362
  return this.turnOnOff(device_list);
338
363
  }).then(() => {
339
- this.platform.device(`Bot: ${this.accessory.displayName} Done.`);
364
+ this.debugLog(`Bot: ${this.accessory.displayName} Done.`);
340
365
  }).catch(async (e) => {
341
- this.platform.log.error(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
342
- if (this.deviceDebug) {
343
- this.platform.log.error(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
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.debugDebug) {
347
- this.platform.log.error(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
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.platform.log.warn(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
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.platform.log.error(`Bot: ${this.accessory.displayName} Mode Not Set, mode: ${this.device.bot?.mode}`);
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.platform.debug(`Bot: ${this.accessory.displayName} OpenAPI pushChanges`);
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.platform.debug(`Bot: ${this.accessory.displayName} Switch Mode, Turning ${this.On}`);
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.platform.debug(`Bot: ${this.accessory.displayName} Switch Mode, Turning ${this.On}`);
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.platform.debug(`Bot: ${this.accessory.displayName} Press Mode`);
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.platform.log.info(`Bot: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
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.platform.debug(`Bot ${this.accessory.displayName} pushchanges: ${JSON.stringify(push.data)}`);
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.platform.log.error(`Bot: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
411
- if (this.deviceDebug) {
412
- this.platform.log.error(`Bot: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
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.debugDebug) {
416
- this.platform.log.error(`Bot: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
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.platform.debug(`Bot: ${this.accessory.displayName} On: ${this.On}`);
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.platform.device(`Bot: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
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.platform.debug(`Bot: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
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.platform.device(`Bot: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
470
+ this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
446
471
  }
447
472
  if (this.StatusLowBattery === undefined) {
448
- this.platform.debug(`Bot: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
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.platform.device(`Bot: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
477
+ this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
453
478
  }
454
479
  }
455
480
  }
456
- apiError() {
457
- throw new this.platform.api.hap.HapStatusError(-70402 /* SERVICE_COMMUNICATION_FAILURE */);
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.platform.log.error(`Bot: ${this.accessory.displayName} Command not supported by this device type.`);
497
+ this.errorLog(`Bot: ${this.accessory.displayName} Command not supported by this device type.`);
463
498
  break;
464
499
  case 152:
465
- this.platform.log.error(`Bot: ${this.accessory.displayName} Device not found.`);
500
+ this.errorLog(`Bot: ${this.accessory.displayName} Device not found.`);
466
501
  break;
467
502
  case 160:
468
- this.platform.log.error(`Bot: ${this.accessory.displayName} Command is not supported.`);
503
+ this.errorLog(`Bot: ${this.accessory.displayName} Command is not supported.`);
469
504
  break;
470
505
  case 161:
471
- this.platform.log.error(`Bot: ${this.accessory.displayName} Device is offline.`);
506
+ this.errorLog(`Bot: ${this.accessory.displayName} Device is offline.`);
472
507
  this.offlineOff();
473
508
  break;
474
509
  case 171:
475
- this.platform.log.error(`Bot: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
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.platform.log.error(`Bot: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,`
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.platform.debug(`Bot: ${this.accessory.displayName} Command successfully sent.`);
518
+ this.debugLog(`Bot: ${this.accessory.displayName} Command successfully sent.`);
484
519
  break;
485
520
  default:
486
- this.platform.debug(`Bot: ${this.accessory.displayName} Unknown statusCode.`);
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.platform.debug(`Bot: ${this.accessory.displayName} On: ${value}`);
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