@switchbot/homebridge-switchbot 1.3.1-beta.9 → 1.4.1-beta.2

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