@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
@@ -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 } from 'homebridge';
7
+ import { Service, PlatformAccessory, CharacteristicValue, HAPStatus } 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,36 +303,36 @@ 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(e);
326
+ this.apiError();
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)}`);
335
+ this.debugLog(`Bot: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(this.deviceStatus)}`);
299
336
  /*this.deviceStatus = {
300
337
  statusCode: 100,
301
338
  body: {
@@ -309,16 +346,16 @@ export class Bot {
309
346
  this.parseStatus();
310
347
  this.updateHomeKitCharacteristics();
311
348
  } 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,`
349
+ this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
350
+ if (this.deviceLogging === 'debug') {
351
+ this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
315
352
  + ` Error Message: ${JSON.stringify(e.message)}`);
316
353
  }
317
- if (this.debugDebug) {
318
- this.platform.log.error(`Bot: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
354
+ if (this.platform.debugMode) {
355
+ this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
319
356
  + ` Error: ${JSON.stringify(e)}`);
320
357
  }
321
- this.apiError(e);
358
+ this.apiError();
322
359
  }
323
360
  }
324
361
  }
@@ -346,57 +383,57 @@ export class Bot {
346
383
 
347
384
  private async BLEpushChanges() {
348
385
  if (this.On !== this.OnCached) {
349
- this.platform.debug(`Bot: ${this.accessory.displayName} BLE pushChanges`);
386
+ this.debugLog(`Bot: ${this.accessory.displayName} BLE pushChanges`);
350
387
  const switchbot = this.connectBLE();
351
388
  if (this.device.bot?.mode === 'press') {
352
- this.platform.device(`Bot: ${this.accessory.displayName} Press Mode: ${this.device.bot?.mode}`);
389
+ this.debugLog(`Bot: ${this.accessory.displayName} Press Mode: ${this.device.bot?.mode}`);
353
390
  switchbot.discover({ model: 'H', quick: true, id: this.device.bleMac })
354
391
  .then((device_list: { press: (arg0: { id: string | undefined; }) => any; }[]) => {
355
- this.platform.log.info(`Bot: ${this.accessory.displayName}, On: ${this.On}`);
392
+ this.infoLog(`Bot: ${this.accessory.displayName}, On: ${this.On}`);
356
393
  return device_list[0].press({ id: this.device.bleMac });
357
394
  }).then(() => {
358
- this.platform.device(`Bot: ${this.accessory.displayName} Done.`);
395
+ this.debugLog(`Bot: ${this.accessory.displayName} Done.`);
359
396
  }).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,`
397
+ this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
398
+ if (this.deviceLogging === 'debug') {
399
+ this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
363
400
  + ` Error Message: ${JSON.stringify(e.message)}`);
364
401
  }
365
- if (this.debugDebug) {
366
- this.platform.log.error(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
402
+ if (this.platform.debugMode) {
403
+ this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
367
404
  + ` Error: ${JSON.stringify(e)}`);
368
405
  }
369
406
  if (this.platform.config.credentials?.openToken) {
370
- this.platform.log.warn(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
407
+ this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
371
408
  await this.openAPIpushChanges();
372
409
  }
373
- this.apiError(e);
410
+ this.apiError();
374
411
  });
375
412
  } else if (this.device.bot?.mode === 'switch') {
376
- this.platform.device(`Bot: ${this.accessory.displayName} Press Mode: ${this.device.bot?.mode}`);
413
+ this.debugLog(`Bot: ${this.accessory.displayName} Press Mode: ${this.device.bot?.mode}`);
377
414
  switchbot.discover({ model: 'H', quick: true, id: this.device.bleMac }).then((device_list: any) => {
378
- this.platform.log.info(`Bot: ${this.accessory.displayName} On: ${this.On}`);
415
+ this.infoLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
379
416
  return this.turnOnOff(device_list);
380
417
  }).then(() => {
381
- this.platform.device(`Bot: ${this.accessory.displayName} Done.`);
418
+ this.debugLog(`Bot: ${this.accessory.displayName} Done.`);
382
419
  }).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,`
420
+ this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
421
+ if (this.deviceLogging === 'debug') {
422
+ this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
386
423
  + ` Error Message: ${JSON.stringify(e.message)}`);
387
424
  }
388
- if (this.debugDebug) {
389
- this.platform.log.error(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
425
+ if (this.platform.debugMode) {
426
+ this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
390
427
  + ` Error: ${JSON.stringify(e)}`);
391
428
  }
392
429
  if (this.platform.config.credentials?.openToken) {
393
- this.platform.log.warn(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
430
+ this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
394
431
  await this.openAPIpushChanges();
395
432
  }
396
- this.apiError(e);
433
+ this.apiError();
397
434
  });
398
435
  } else {
399
- this.platform.log.error(`Bot: ${this.accessory.displayName} Mode Not Set, mode: ${this.device.bot?.mode}`);
436
+ this.errorLog(`Bot: ${this.accessory.displayName} Mode Not Set, mode: ${this.device.bot?.mode}`);
400
437
  }
401
438
  }
402
439
  this.OnCached = this.On;
@@ -415,7 +452,7 @@ export class Bot {
415
452
  if (this.platform.config.credentials?.openToken) {
416
453
  try {
417
454
  if (this.On !== this.OnCached) {
418
- this.platform.debug(`Bot: ${this.accessory.displayName} OpenAPI pushChanges`);
455
+ this.debugLog(`Bot: ${this.accessory.displayName} OpenAPI pushChanges`);
419
456
  const payload = {
420
457
  commandType: 'command',
421
458
  parameter: 'default',
@@ -424,40 +461,40 @@ export class Bot {
424
461
  if (this.device.bot?.mode === 'switch' && this.On) {
425
462
  payload.command = 'turnOn';
426
463
  this.On = true;
427
- this.platform.debug(`Bot: ${this.accessory.displayName} Switch Mode, Turning ${this.On}`);
464
+ this.debugLog(`Bot: ${this.accessory.displayName} Switch Mode, Turning ${this.On}`);
428
465
  } else if (this.device.bot?.mode === 'switch' && !this.On) {
429
466
  payload.command = 'turnOff';
430
467
  this.On = false;
431
- this.platform.debug(`Bot: ${this.accessory.displayName} Switch Mode, Turning ${this.On}`);
468
+ this.debugLog(`Bot: ${this.accessory.displayName} Switch Mode, Turning ${this.On}`);
432
469
  } else if (this.device.bot?.mode === 'press') {
433
470
  payload.command = 'press';
434
- this.platform.debug(`Bot: ${this.accessory.displayName} Press Mode`);
471
+ this.debugLog(`Bot: ${this.accessory.displayName} Press Mode`);
435
472
  this.On = false;
436
473
  } else {
437
474
  throw new Error(`Bot: ${this.accessory.displayName} Device Paramters not set for this Bot.`);
438
475
  }
439
476
 
440
- this.platform.log.info(`Bot: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
477
+ this.infoLog(`Bot: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
441
478
  + ` parameter: ${payload.parameter}, commandType: ${payload.commandType}`);
442
479
 
443
480
  // Make the API request
444
481
  const push: any = (await this.platform.axios.post(`${DeviceURL}/${this.device.deviceId}/commands`, payload));
445
- this.platform.debug(`Bot ${this.accessory.displayName} pushchanges: ${JSON.stringify(push.data)}`);
482
+ this.debugLog(`Bot ${this.accessory.displayName} pushchanges: ${JSON.stringify(push.data)}`);
446
483
  this.statusCode(push);
447
484
  this.OnCached = this.On;
448
485
  this.accessory.context.On = this.OnCached;
449
486
  }
450
487
  } catch (e: any) {
451
- this.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,`
488
+ this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
489
+ if (this.deviceLogging === 'debug') {
490
+ this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
454
491
  + ` Error Message: ${JSON.stringify(e.message)}`);
455
492
  }
456
- if (this.debugDebug) {
457
- this.platform.log.error(`Bot: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
493
+ if (this.platform.debugMode) {
494
+ this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
458
495
  + ` Error: ${JSON.stringify(e)}`);
459
496
  }
460
- this.apiError(e);
497
+ this.apiError();
461
498
  }
462
499
  }
463
500
  }
@@ -467,71 +504,63 @@ export class Bot {
467
504
  */
468
505
  updateHomeKitCharacteristics() {
469
506
  if (this.On === undefined) {
470
- this.platform.debug(`Bot: ${this.accessory.displayName} On: ${this.On}`);
507
+ this.debugLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
471
508
  } else {
472
509
  if (this.device.bot?.deviceType === 'switch') {
473
510
  this.switchService?.updateCharacteristic(this.platform.Characteristic.On, this.On);
474
511
  } else {
475
512
  this.outletService?.updateCharacteristic(this.platform.Characteristic.On, this.On);
476
513
  }
477
- this.platform.device(`Bot: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
514
+ this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
478
515
  }
479
516
  if (this.device.ble) {
480
517
  if (this.BatteryLevel === undefined) {
481
- this.platform.debug(`Bot: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
518
+ this.debugLog(`Bot: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
482
519
  } else {
483
520
  this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
484
- this.platform.device(`Bot: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
521
+ this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
485
522
  }
486
523
  if (this.StatusLowBattery === undefined) {
487
- this.platform.debug(`Bot: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
524
+ this.debugLog(`Bot: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
488
525
  } else {
489
526
  this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
490
- this.platform.device(`Bot: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
527
+ this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
491
528
  }
492
529
  }
493
530
  }
494
531
 
495
- public apiError(e: any) {
496
- if (this.device.bot?.deviceType === 'switch') {
497
- this.switchService?.updateCharacteristic(this.platform.Characteristic.On, e);
498
- } else {
499
- this.outletService?.updateCharacteristic(this.platform.Characteristic.On, e);
500
- }
501
- if (this.device.ble) {
502
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
503
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
504
- }
532
+ public apiError() {
533
+ throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
505
534
  }
506
535
 
507
536
  private statusCode(push: AxiosResponse<{ statusCode: number; }>) {
508
537
  switch (push.data.statusCode) {
509
538
  case 151:
510
- this.platform.log.error(`Bot: ${this.accessory.displayName} Command not supported by this device type.`);
539
+ this.errorLog(`Bot: ${this.accessory.displayName} Command not supported by this device type.`);
511
540
  break;
512
541
  case 152:
513
- this.platform.log.error(`Bot: ${this.accessory.displayName} Device not found.`);
542
+ this.errorLog(`Bot: ${this.accessory.displayName} Device not found.`);
514
543
  break;
515
544
  case 160:
516
- this.platform.log.error(`Bot: ${this.accessory.displayName} Command is not supported.`);
545
+ this.errorLog(`Bot: ${this.accessory.displayName} Command is not supported.`);
517
546
  break;
518
547
  case 161:
519
- this.platform.log.error(`Bot: ${this.accessory.displayName} Device is offline.`);
548
+ this.errorLog(`Bot: ${this.accessory.displayName} Device is offline.`);
520
549
  this.offlineOff();
521
550
  break;
522
551
  case 171:
523
- this.platform.log.error(`Bot: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
552
+ this.errorLog(`Bot: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
524
553
  this.offlineOff();
525
554
  break;
526
555
  case 190:
527
- this.platform.log.error(`Bot: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,`
556
+ this.errorLog(`Bot: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,`
528
557
  + ` Or command: ${JSON.stringify(push.data)} format is invalid`);
529
558
  break;
530
559
  case 100:
531
- this.platform.debug(`Bot: ${this.accessory.displayName} Command successfully sent.`);
560
+ this.debugLog(`Bot: ${this.accessory.displayName} Command successfully sent.`);
532
561
  break;
533
562
  default:
534
- this.platform.debug(`Bot: ${this.accessory.displayName} Unknown statusCode.`);
563
+ this.debugLog(`Bot: ${this.accessory.displayName} Unknown statusCode.`);
535
564
  }
536
565
  }
537
566
 
@@ -550,8 +579,43 @@ export class Bot {
550
579
  * Handle requests to set the "On" characteristic
551
580
  */
552
581
  private handleOnSet(value: CharacteristicValue) {
553
- this.platform.debug(`Bot: ${this.accessory.displayName} On: ${value}`);
582
+ this.debugLog(`Bot: ${this.accessory.displayName} On: ${value}`);
554
583
  this.On = value;
555
584
  this.doBotUpdate.next();
556
585
  }
586
+
587
+ /**
588
+ * Logging for Device
589
+ */
590
+ infoLog(...log: any[]) {
591
+ if (this.enablingDeviceLogging()) {
592
+ this.platform.log.info(String(...log));
593
+ }
594
+ }
595
+
596
+ warnLog(...log: any[]) {
597
+ if (this.enablingDeviceLogging()) {
598
+ this.platform.log.warn(String(...log));
599
+ }
600
+ }
601
+
602
+ errorLog(...log: any[]) {
603
+ if (this.enablingDeviceLogging()) {
604
+ this.platform.log.error(String(...log));
605
+ }
606
+ }
607
+
608
+ debugLog(...log: any[]) {
609
+ if (this.enablingDeviceLogging()) {
610
+ if (this.deviceLogging === 'debug') {
611
+ this.platform.log.info('[DEBUG]', String(...log));
612
+ } else {
613
+ this.platform.log.debug(String(...log));
614
+ }
615
+ }
616
+ }
617
+
618
+ enablingDeviceLogging(): boolean {
619
+ return this.deviceLogging === 'debug' || this.deviceLogging === 'standard';
620
+ }
557
621
  }