@switchbot/homebridge-switchbot 1.4.0 → 1.4.1-beta.3

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 +18 -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 +162 -105
  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 +161 -96
  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 +117 -63
  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 +198 -140
  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 +180 -127
  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 +121 -63
  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 +104 -45
  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 +99 -35
  34. package/dist/devices/plugs.js.map +1 -1
  35. package/dist/irdevices/airconditioners.d.ts +11 -3
  36. package/dist/irdevices/airconditioners.d.ts.map +1 -1
  37. package/dist/irdevices/airconditioners.js +80 -45
  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 +68 -31
  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 +64 -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 +74 -35
  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 +65 -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 +71 -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 +81 -42
  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 +64 -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 +64 -24
  70. package/dist/irdevices/waterheaters.js.map +1 -1
  71. package/dist/platform.d.ts +10 -10
  72. package/dist/platform.d.ts.map +1 -1
  73. package/dist/platform.js +290 -307
  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 +70 -66
  80. package/src/devices/bots.ts +169 -105
  81. package/src/devices/colorbulb.ts +170 -99
  82. package/src/devices/contact.ts +124 -64
  83. package/src/devices/curtains.ts +207 -145
  84. package/src/devices/humidifiers.ts +187 -129
  85. package/src/devices/meters.ts +128 -64
  86. package/src/devices/motion.ts +110 -45
  87. package/src/devices/plugs.ts +104 -36
  88. package/src/irdevices/airconditioners.ts +85 -45
  89. package/src/irdevices/airpurifiers.ts +73 -31
  90. package/src/irdevices/cameras.ts +69 -24
  91. package/src/irdevices/fans.ts +79 -36
  92. package/src/irdevices/lights.ts +70 -25
  93. package/src/irdevices/others.ts +76 -31
  94. package/src/irdevices/tvs.ts +86 -42
  95. package/src/irdevices/vacuumcleaners.ts +69 -24
  96. package/src/irdevices/waterheaters.ts +69 -24
  97. package/src/platform.ts +285 -302
  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,35 +259,35 @@ 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(e);
282
+ this.apiError();
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)}`);
290
+ this.debugLog(`Bot: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(this.deviceStatus)}`);
256
291
  /*this.deviceStatus = {
257
292
  statusCode: 100,
258
293
  body: {
@@ -267,16 +302,16 @@ class Bot {
267
302
  this.updateHomeKitCharacteristics();
268
303
  }
269
304
  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,`
305
+ this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
306
+ if (this.deviceLogging === 'debug') {
307
+ this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
273
308
  + ` Error Message: ${JSON.stringify(e.message)}`);
274
309
  }
275
- if (this.debugDebug) {
276
- this.platform.log.error(`Bot: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
310
+ if (this.platform.debugMode) {
311
+ this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
277
312
  + ` Error: ${JSON.stringify(e)}`);
278
313
  }
279
- this.apiError(e);
314
+ this.apiError();
280
315
  }
281
316
  }
282
317
  }
@@ -303,59 +338,59 @@ class Bot {
303
338
  }
304
339
  async BLEpushChanges() {
305
340
  if (this.On !== this.OnCached) {
306
- this.platform.debug(`Bot: ${this.accessory.displayName} BLE pushChanges`);
341
+ this.debugLog(`Bot: ${this.accessory.displayName} BLE pushChanges`);
307
342
  const switchbot = this.connectBLE();
308
343
  if (this.device.bot?.mode === 'press') {
309
- this.platform.device(`Bot: ${this.accessory.displayName} Press Mode: ${this.device.bot?.mode}`);
344
+ this.debugLog(`Bot: ${this.accessory.displayName} Press Mode: ${this.device.bot?.mode}`);
310
345
  switchbot.discover({ model: 'H', quick: true, id: this.device.bleMac })
311
346
  .then((device_list) => {
312
- this.platform.log.info(`Bot: ${this.accessory.displayName}, On: ${this.On}`);
347
+ this.infoLog(`Bot: ${this.accessory.displayName}, On: ${this.On}`);
313
348
  return device_list[0].press({ id: this.device.bleMac });
314
349
  }).then(() => {
315
- this.platform.device(`Bot: ${this.accessory.displayName} Done.`);
350
+ this.debugLog(`Bot: ${this.accessory.displayName} Done.`);
316
351
  }).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,`
352
+ this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
353
+ if (this.deviceLogging === 'debug') {
354
+ this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
320
355
  + ` Error Message: ${JSON.stringify(e.message)}`);
321
356
  }
322
- if (this.debugDebug) {
323
- this.platform.log.error(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
357
+ if (this.platform.debugMode) {
358
+ this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
324
359
  + ` Error: ${JSON.stringify(e)}`);
325
360
  }
326
361
  if (this.platform.config.credentials?.openToken) {
327
- this.platform.log.warn(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
362
+ this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
328
363
  await this.openAPIpushChanges();
329
364
  }
330
- this.apiError(e);
365
+ this.apiError();
331
366
  });
332
367
  }
333
368
  else if (this.device.bot?.mode === 'switch') {
334
- this.platform.device(`Bot: ${this.accessory.displayName} Press Mode: ${this.device.bot?.mode}`);
369
+ this.debugLog(`Bot: ${this.accessory.displayName} Press Mode: ${this.device.bot?.mode}`);
335
370
  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}`);
371
+ this.infoLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
337
372
  return this.turnOnOff(device_list);
338
373
  }).then(() => {
339
- this.platform.device(`Bot: ${this.accessory.displayName} Done.`);
374
+ this.debugLog(`Bot: ${this.accessory.displayName} Done.`);
340
375
  }).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,`
376
+ this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
377
+ if (this.deviceLogging === 'debug') {
378
+ this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
344
379
  + ` Error Message: ${JSON.stringify(e.message)}`);
345
380
  }
346
- if (this.debugDebug) {
347
- this.platform.log.error(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
381
+ if (this.platform.debugMode) {
382
+ this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
348
383
  + ` Error: ${JSON.stringify(e)}`);
349
384
  }
350
385
  if (this.platform.config.credentials?.openToken) {
351
- this.platform.log.warn(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
386
+ this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
352
387
  await this.openAPIpushChanges();
353
388
  }
354
- this.apiError(e);
389
+ this.apiError();
355
390
  });
356
391
  }
357
392
  else {
358
- this.platform.log.error(`Bot: ${this.accessory.displayName} Mode Not Set, mode: ${this.device.bot?.mode}`);
393
+ this.errorLog(`Bot: ${this.accessory.displayName} Mode Not Set, mode: ${this.device.bot?.mode}`);
359
394
  }
360
395
  }
361
396
  this.OnCached = this.On;
@@ -373,7 +408,7 @@ class Bot {
373
408
  if (this.platform.config.credentials?.openToken) {
374
409
  try {
375
410
  if (this.On !== this.OnCached) {
376
- this.platform.debug(`Bot: ${this.accessory.displayName} OpenAPI pushChanges`);
411
+ this.debugLog(`Bot: ${this.accessory.displayName} OpenAPI pushChanges`);
377
412
  const payload = {
378
413
  commandType: 'command',
379
414
  parameter: 'default',
@@ -381,42 +416,42 @@ class Bot {
381
416
  if (this.device.bot?.mode === 'switch' && this.On) {
382
417
  payload.command = 'turnOn';
383
418
  this.On = true;
384
- this.platform.debug(`Bot: ${this.accessory.displayName} Switch Mode, Turning ${this.On}`);
419
+ this.debugLog(`Bot: ${this.accessory.displayName} Switch Mode, Turning ${this.On}`);
385
420
  }
386
421
  else if (this.device.bot?.mode === 'switch' && !this.On) {
387
422
  payload.command = 'turnOff';
388
423
  this.On = false;
389
- this.platform.debug(`Bot: ${this.accessory.displayName} Switch Mode, Turning ${this.On}`);
424
+ this.debugLog(`Bot: ${this.accessory.displayName} Switch Mode, Turning ${this.On}`);
390
425
  }
391
426
  else if (this.device.bot?.mode === 'press') {
392
427
  payload.command = 'press';
393
- this.platform.debug(`Bot: ${this.accessory.displayName} Press Mode`);
428
+ this.debugLog(`Bot: ${this.accessory.displayName} Press Mode`);
394
429
  this.On = false;
395
430
  }
396
431
  else {
397
432
  throw new Error(`Bot: ${this.accessory.displayName} Device Paramters not set for this Bot.`);
398
433
  }
399
- this.platform.log.info(`Bot: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
434
+ this.infoLog(`Bot: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
400
435
  + ` parameter: ${payload.parameter}, commandType: ${payload.commandType}`);
401
436
  // Make the API request
402
437
  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)}`);
438
+ this.debugLog(`Bot ${this.accessory.displayName} pushchanges: ${JSON.stringify(push.data)}`);
404
439
  this.statusCode(push);
405
440
  this.OnCached = this.On;
406
441
  this.accessory.context.On = this.OnCached;
407
442
  }
408
443
  }
409
444
  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,`
445
+ this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
446
+ if (this.deviceLogging === 'debug') {
447
+ this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
413
448
  + ` Error Message: ${JSON.stringify(e.message)}`);
414
449
  }
415
- if (this.debugDebug) {
416
- this.platform.log.error(`Bot: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
450
+ if (this.platform.debugMode) {
451
+ this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
417
452
  + ` Error: ${JSON.stringify(e)}`);
418
453
  }
419
- this.apiError(e);
454
+ this.apiError();
420
455
  }
421
456
  }
422
457
  }
@@ -425,7 +460,7 @@ class Bot {
425
460
  */
426
461
  updateHomeKitCharacteristics() {
427
462
  if (this.On === undefined) {
428
- this.platform.debug(`Bot: ${this.accessory.displayName} On: ${this.On}`);
463
+ this.debugLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
429
464
  }
430
465
  else {
431
466
  if (this.device.bot?.deviceType === 'switch') {
@@ -434,65 +469,56 @@ class Bot {
434
469
  else {
435
470
  this.outletService?.updateCharacteristic(this.platform.Characteristic.On, this.On);
436
471
  }
437
- this.platform.device(`Bot: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
472
+ this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
438
473
  }
439
474
  if (this.device.ble) {
440
475
  if (this.BatteryLevel === undefined) {
441
- this.platform.debug(`Bot: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
476
+ this.debugLog(`Bot: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
442
477
  }
443
478
  else {
444
479
  this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
445
- this.platform.device(`Bot: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
480
+ this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
446
481
  }
447
482
  if (this.StatusLowBattery === undefined) {
448
- this.platform.debug(`Bot: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
483
+ this.debugLog(`Bot: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
449
484
  }
450
485
  else {
451
486
  this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
452
- this.platform.device(`Bot: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
487
+ this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
453
488
  }
454
489
  }
455
490
  }
456
- apiError(e) {
457
- if (this.device.bot?.deviceType === 'switch') {
458
- this.switchService?.updateCharacteristic(this.platform.Characteristic.On, e);
459
- }
460
- else {
461
- this.outletService?.updateCharacteristic(this.platform.Characteristic.On, e);
462
- }
463
- if (this.device.ble) {
464
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
465
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
466
- }
491
+ apiError() {
492
+ throw new this.platform.api.hap.HapStatusError(-70402 /* SERVICE_COMMUNICATION_FAILURE */);
467
493
  }
468
494
  statusCode(push) {
469
495
  switch (push.data.statusCode) {
470
496
  case 151:
471
- 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.`);
472
498
  break;
473
499
  case 152:
474
- this.platform.log.error(`Bot: ${this.accessory.displayName} Device not found.`);
500
+ this.errorLog(`Bot: ${this.accessory.displayName} Device not found.`);
475
501
  break;
476
502
  case 160:
477
- this.platform.log.error(`Bot: ${this.accessory.displayName} Command is not supported.`);
503
+ this.errorLog(`Bot: ${this.accessory.displayName} Command is not supported.`);
478
504
  break;
479
505
  case 161:
480
- this.platform.log.error(`Bot: ${this.accessory.displayName} Device is offline.`);
506
+ this.errorLog(`Bot: ${this.accessory.displayName} Device is offline.`);
481
507
  this.offlineOff();
482
508
  break;
483
509
  case 171:
484
- 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}`);
485
511
  this.offlineOff();
486
512
  break;
487
513
  case 190:
488
- 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,`
489
515
  + ` Or command: ${JSON.stringify(push.data)} format is invalid`);
490
516
  break;
491
517
  case 100:
492
- this.platform.debug(`Bot: ${this.accessory.displayName} Command successfully sent.`);
518
+ this.debugLog(`Bot: ${this.accessory.displayName} Command successfully sent.`);
493
519
  break;
494
520
  default:
495
- this.platform.debug(`Bot: ${this.accessory.displayName} Unknown statusCode.`);
521
+ this.debugLog(`Bot: ${this.accessory.displayName} Unknown statusCode.`);
496
522
  }
497
523
  }
498
524
  offlineOff() {
@@ -510,10 +536,41 @@ class Bot {
510
536
  * Handle requests to set the "On" characteristic
511
537
  */
512
538
  handleOnSet(value) {
513
- this.platform.debug(`Bot: ${this.accessory.displayName} On: ${value}`);
539
+ this.debugLog(`Bot: ${this.accessory.displayName} On: ${value}`);
514
540
  this.On = value;
515
541
  this.doBotUpdate.next();
516
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
+ }
517
574
  }
518
575
  exports.Bot = Bot;
519
576
  //# sourceMappingURL=bots.js.map