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