@switchbot/homebridge-switchbot 1.7.1-beta.1 → 1.7.1-beta.13

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 (97) hide show
  1. package/CHANGELOG.md +9 -2
  2. package/config.schema.json +49 -36
  3. package/dist/devices/bots.d.ts +7 -5
  4. package/dist/devices/bots.d.ts.map +1 -1
  5. package/dist/devices/bots.js +72 -66
  6. package/dist/devices/bots.js.map +1 -1
  7. package/dist/devices/colorbulb.d.ts +9 -4
  8. package/dist/devices/colorbulb.d.ts.map +1 -1
  9. package/dist/devices/colorbulb.js +50 -37
  10. package/dist/devices/colorbulb.js.map +1 -1
  11. package/dist/devices/contact.d.ts +9 -10
  12. package/dist/devices/contact.d.ts.map +1 -1
  13. package/dist/devices/contact.js +105 -137
  14. package/dist/devices/contact.js.map +1 -1
  15. package/dist/devices/curtains.d.ts +11 -4
  16. package/dist/devices/curtains.d.ts.map +1 -1
  17. package/dist/devices/curtains.js +70 -44
  18. package/dist/devices/curtains.js.map +1 -1
  19. package/dist/devices/humidifiers.d.ts +8 -2
  20. package/dist/devices/humidifiers.d.ts.map +1 -1
  21. package/dist/devices/humidifiers.js +42 -36
  22. package/dist/devices/humidifiers.js.map +1 -1
  23. package/dist/devices/meters.d.ts +17 -15
  24. package/dist/devices/meters.d.ts.map +1 -1
  25. package/dist/devices/meters.js +99 -133
  26. package/dist/devices/meters.js.map +1 -1
  27. package/dist/devices/motion.d.ts +8 -9
  28. package/dist/devices/motion.d.ts.map +1 -1
  29. package/dist/devices/motion.js +97 -120
  30. package/dist/devices/motion.js.map +1 -1
  31. package/dist/devices/plugs.d.ts +6 -4
  32. package/dist/devices/plugs.d.ts.map +1 -1
  33. package/dist/devices/plugs.js +44 -31
  34. package/dist/devices/plugs.js.map +1 -1
  35. package/dist/irdevices/airconditioners.d.ts +2 -1
  36. package/dist/irdevices/airconditioners.d.ts.map +1 -1
  37. package/dist/irdevices/airconditioners.js +19 -12
  38. package/dist/irdevices/airconditioners.js.map +1 -1
  39. package/dist/irdevices/airpurifiers.d.ts +4 -3
  40. package/dist/irdevices/airpurifiers.d.ts.map +1 -1
  41. package/dist/irdevices/airpurifiers.js +23 -16
  42. package/dist/irdevices/airpurifiers.js.map +1 -1
  43. package/dist/irdevices/cameras.d.ts +2 -1
  44. package/dist/irdevices/cameras.d.ts.map +1 -1
  45. package/dist/irdevices/cameras.js +19 -12
  46. package/dist/irdevices/cameras.js.map +1 -1
  47. package/dist/irdevices/fans.d.ts +2 -1
  48. package/dist/irdevices/fans.d.ts.map +1 -1
  49. package/dist/irdevices/fans.js +19 -12
  50. package/dist/irdevices/fans.js.map +1 -1
  51. package/dist/irdevices/lights.d.ts +2 -1
  52. package/dist/irdevices/lights.d.ts.map +1 -1
  53. package/dist/irdevices/lights.js +19 -12
  54. package/dist/irdevices/lights.js.map +1 -1
  55. package/dist/irdevices/others.d.ts +3 -2
  56. package/dist/irdevices/others.d.ts.map +1 -1
  57. package/dist/irdevices/others.js +23 -16
  58. package/dist/irdevices/others.js.map +1 -1
  59. package/dist/irdevices/tvs.d.ts +2 -1
  60. package/dist/irdevices/tvs.d.ts.map +1 -1
  61. package/dist/irdevices/tvs.js +19 -12
  62. package/dist/irdevices/tvs.js.map +1 -1
  63. package/dist/irdevices/vacuumcleaners.d.ts +2 -1
  64. package/dist/irdevices/vacuumcleaners.d.ts.map +1 -1
  65. package/dist/irdevices/vacuumcleaners.js +19 -12
  66. package/dist/irdevices/vacuumcleaners.js.map +1 -1
  67. package/dist/irdevices/waterheaters.d.ts +2 -1
  68. package/dist/irdevices/waterheaters.d.ts.map +1 -1
  69. package/dist/irdevices/waterheaters.js +19 -12
  70. package/dist/irdevices/waterheaters.js.map +1 -1
  71. package/dist/platform.d.ts +2 -1
  72. package/dist/platform.d.ts.map +1 -1
  73. package/dist/platform.js +14 -1
  74. package/dist/platform.js.map +1 -1
  75. package/dist/settings.d.ts +7 -1
  76. package/dist/settings.d.ts.map +1 -1
  77. package/dist/settings.js.map +1 -1
  78. package/package.json +5 -5
  79. package/src/devices/bots.ts +75 -70
  80. package/src/devices/colorbulb.ts +56 -40
  81. package/src/devices/contact.ts +108 -138
  82. package/src/devices/curtains.ts +79 -48
  83. package/src/devices/humidifiers.ts +50 -37
  84. package/src/devices/meters.ts +115 -143
  85. package/src/devices/motion.ts +102 -121
  86. package/src/devices/plugs.ts +47 -32
  87. package/src/irdevices/airconditioners.ts +20 -12
  88. package/src/irdevices/airpurifiers.ts +24 -16
  89. package/src/irdevices/cameras.ts +20 -12
  90. package/src/irdevices/fans.ts +20 -12
  91. package/src/irdevices/lights.ts +20 -12
  92. package/src/irdevices/others.ts +24 -16
  93. package/src/irdevices/tvs.ts +20 -12
  94. package/src/irdevices/vacuumcleaners.ts +20 -12
  95. package/src/irdevices/waterheaters.ts +20 -12
  96. package/src/platform.ts +14 -1
  97. package/src/settings.ts +7 -1
@@ -2,7 +2,7 @@ import { interval, Subject } from 'rxjs';
2
2
  import { skipWhile } from 'rxjs/operators';
3
3
  import { SwitchBotPlatform } from '../platform';
4
4
  import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
5
- import { DeviceURL, device, devicesConfig, serviceData, switchbot, deviceStatusResponse } from '../settings';
5
+ import { DeviceURL, device, devicesConfig, serviceData, switchbot, deviceStatusResponse, deviceStatus } from '../settings';
6
6
 
7
7
  /**
8
8
  * Platform Accessory
@@ -23,10 +23,13 @@ export class Motion {
23
23
 
24
24
  // OpenAPI Others
25
25
  deviceStatus!: deviceStatusResponse;
26
+ moveDetected: deviceStatus['moveDetected'];
27
+ brightness: deviceStatus['brightness'];
26
28
 
27
29
  // BLE Others
28
30
  connected?: boolean;
29
31
  switchbot!: switchbot;
32
+ SwitchToOpenAPI?: boolean;
30
33
  serviceData!: serviceData;
31
34
  battery!: serviceData['battery'];
32
35
  movement!: serviceData['movement'];
@@ -49,9 +52,10 @@ export class Motion {
49
52
  public device: device & devicesConfig,
50
53
  ) {
51
54
  // default placeholders
52
- this.logs();
53
- this.scan();
54
- this.refreshRate();
55
+ this.logs(device);
56
+ this.scan(device);
57
+ this.refreshRate(device);
58
+ this.config(device);
55
59
  this.MotionDetected = false;
56
60
 
57
61
  // Motion Config
@@ -108,12 +112,20 @@ export class Motion {
108
112
  }
109
113
 
110
114
  // Battery Service
111
- if (device.ble) {
115
+ if (!device.ble) {
116
+ this.debugLog(`Motion Sensor: ${accessory.displayName} Removing Battery Service`);
117
+ this.batteryService = this.accessory.getService(this.platform.Service.Battery);
118
+ accessory.removeService(this.batteryService!);
119
+ } else if (device.ble && !this.batteryService) {
120
+ this.debugLog(`Motion Sensor: ${accessory.displayName} Add Battery Service`);
112
121
  (this.batteryService =
113
- accessory.getService(this.platform.Service.Battery) ||
114
- accessory.addService(this.platform.Service.Battery)), `${accessory.displayName} Battery`;
122
+ this.accessory.getService(this.platform.Service.Battery) ||
123
+ this.accessory.addService(this.platform.Service.Battery)), `${accessory.displayName} Battery`;
115
124
 
116
125
  this.batteryService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Battery`);
126
+
127
+ } else {
128
+ this.debugLog(`Motion Sensor: ${accessory.displayName} Battery Service Not Added`);
117
129
  }
118
130
 
119
131
  // Retrieve initial values and updateHomekit
@@ -127,50 +139,62 @@ export class Motion {
127
139
  });
128
140
  }
129
141
 
130
- refreshRate() {
131
- if (this.device.refreshRate) {
132
- this.deviceRefreshRate = this.accessory.context.refreshRate = this.device.refreshRate;
133
- if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
134
- this.warnLog(`Motion Sensor: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
135
- }
142
+ config(device: device & devicesConfig) {
143
+ const config: any = device.motion;
144
+ if (device.ble !== undefined) {
145
+ config['ble'] = device.ble;
146
+ }
147
+ if (device.logging !== undefined) {
148
+ config['logging'] = device.logging;
149
+ }
150
+ if (device.refreshRate !== undefined) {
151
+ config['refreshRate'] = device.refreshRate;
152
+ }
153
+ if (device.scanDuration !== undefined) {
154
+ config['scanDuration'] = device.scanDuration;
155
+ }
156
+ if (config !== undefined) {
157
+ this.warnLog(`Motion Sensor: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
158
+ }
159
+ }
160
+
161
+ refreshRate(device: device & devicesConfig) {
162
+ if (device.refreshRate) {
163
+ this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
164
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
136
165
  } else if (this.platform.config.options!.refreshRate) {
137
166
  this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options!.refreshRate;
138
- if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
139
- this.warnLog(`Motion Sensor: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
140
- }
167
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
141
168
  }
142
169
  }
143
170
 
144
- scan() {
145
- if (this.device.scanDuration) {
146
- this.scanDuration = this.accessory.context.scanDuration = this.device.scanDuration;
147
- if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
148
- this.warnLog(`Motion Sensor: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
171
+ scan(device: device & devicesConfig) {
172
+ if (device.scanDuration) {
173
+ this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
174
+ if (device.ble) {
175
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
149
176
  }
150
177
  } else {
151
178
  this.scanDuration = this.accessory.context.scanDuration = 1;
152
- if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
153
- this.warnLog(`Motion Sensor: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
179
+ if (this.device.ble) {
180
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
154
181
  }
155
182
  }
156
183
  }
157
184
 
158
- logs() {
185
+ logs(device: device & devicesConfig) {
159
186
  if (this.platform.debugMode) {
160
- this.deviceLogging = this.accessory.context.logging = 'debug';
161
- this.warnLog(`Motion Sensor: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
162
- } else if (this.device.logging) {
163
- this.deviceLogging = this.accessory.context.logging = this.device.logging;
164
- if (this.deviceLogging === 'debug' || this.deviceLogging === 'standard') {
165
- this.warnLog(`Motion Sensor: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
166
- }
187
+ this.deviceLogging = this.accessory.context.logging = 'debugMode';
188
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
189
+ } else if (device.logging) {
190
+ this.deviceLogging = this.accessory.context.logging = device.logging;
191
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
167
192
  } else if (this.platform.config.options?.logging) {
168
193
  this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
169
- if (this.deviceLogging === 'debug' || this.deviceLogging === 'standard') {
170
- this.warnLog(`Motion Sensor: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
171
- }
194
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
172
195
  } else {
173
196
  this.deviceLogging = this.accessory.context.logging = 'standard';
197
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
174
198
  }
175
199
  }
176
200
 
@@ -196,42 +220,38 @@ export class Motion {
196
220
  * Parse the device status from the SwitchBot api
197
221
  */
198
222
  async parseStatus() {
199
- if (this.device.ble) {
200
- await this.BLEparseStatus();
201
- } else {
223
+ if (this.SwitchToOpenAPI || !this.device.ble) {
202
224
  await this.openAPIparseStatus();
225
+ } else {
226
+ await this.BLEparseStatus();
203
227
  }
204
228
  }
205
229
 
206
230
  private async BLEparseStatus() {
207
231
  this.debugLog(`Motion Sensor: ${this.accessory.displayName} BLE parseStatus`);
208
232
  // Movement
209
- if (this.movement === undefined && this.platform.config.credentials?.openToken) {
210
- this.openAPImotion();
211
- } else if (this.movement === undefined) {
212
- this.movement = false;
213
- this.BLEmotion();
214
- } else {
215
- this.BLEmotion();
216
- }
233
+ this.MotionDetected = this.movement!;
234
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
217
235
  // Light Level
218
236
  if (!this.device.motion?.hide_lightsensor) {
219
237
  this.set_minLux = this.minLux();
220
238
  this.set_maxLux = this.maxLux();
221
- if (this.lightLevel === undefined && this.platform.config.credentials?.openToken) {
222
- this.openAPIlux();
223
- } else if (this.lightLevel === undefined) {
224
- this.lightLevel = 100;
225
- this.BLElux();
226
- } else {
227
- this.BLElux();
239
+ switch (this.lightLevel) {
240
+ case 'dark':
241
+ case 1:
242
+ this.CurrentAmbientLightLevel = this.set_minLux;
243
+ break;
244
+ default:
245
+ this.CurrentAmbientLightLevel = this.set_maxLux;
228
246
  }
247
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} LightLevel: ${this.lightLevel},`
248
+ + ` CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
229
249
  }
230
250
  // Battery
231
251
  if (this.battery === undefined) {
232
252
  this.battery = 100;
233
253
  }
234
- this.BatteryLevel = Number(this.battery);
254
+ this.BatteryLevel = this.battery!;
235
255
  if (this.BatteryLevel < 10) {
236
256
  this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
237
257
  } else {
@@ -240,55 +260,31 @@ export class Motion {
240
260
  this.debugLog(`Motion Sensor: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
241
261
  }
242
262
 
243
- private BLEmotion() {
244
- this.MotionDetected = Boolean(this.movement);
245
- this.debugLog(`Motion Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
246
- }
247
-
248
- private BLElux() {
249
- switch (this.lightLevel) {
250
- case 'dark':
251
- case 1:
252
- this.CurrentAmbientLightLevel = this.set_minLux;
253
- break;
254
- default:
255
- this.CurrentAmbientLightLevel = this.set_maxLux;
256
- }
257
- this.debugLog(`Motion Sensor: ${this.accessory.displayName} LightLevel: ${this.lightLevel},`
258
- + ` CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
259
- }
260
-
261
263
  private async openAPIparseStatus() {
264
+ if (this.device.ble) {
265
+ this.SwitchToOpenAPI = false;
266
+ }
262
267
  if (this.platform.config.credentials?.openToken) {
263
268
  this.debugLog(`Motion Sensor: ${this.accessory.displayName} OpenAPI parseStatus`);
264
269
  // Motion State
265
- this.openAPImotion();
270
+ this.MotionDetected = this.moveDetected!;
271
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
266
272
  // Light Level
267
273
  if (!this.device.motion?.hide_lightsensor) {
268
- this.openAPIlux();
269
- }
270
- }
271
- }
272
-
273
- private openAPImotion() {
274
- if (typeof this.deviceStatus.body.moveDetected === 'boolean') {
275
- this.MotionDetected = this.deviceStatus.body.moveDetected;
276
- }
277
- this.debugLog(`Motion Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
278
- }
274
+ this.set_minLux = this.minLux();
275
+ this.set_maxLux = this.maxLux();
276
+ switch (this.brightness) {
277
+ case 'dim':
278
+ this.CurrentAmbientLightLevel = this.set_minLux;
279
+ break;
280
+ case 'bright':
281
+ default:
282
+ this.CurrentAmbientLightLevel = this.set_maxLux;
283
+ }
284
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
279
285
 
280
- private openAPIlux() {
281
- this.set_minLux = this.minLux();
282
- this.set_maxLux = this.maxLux();
283
- switch (this.deviceStatus.body.brightness) {
284
- case 'dim':
285
- this.CurrentAmbientLightLevel = this.set_minLux;
286
- break;
287
- case 'bright':
288
- default:
289
- this.CurrentAmbientLightLevel = this.set_maxLux;
286
+ }
290
287
  }
291
- this.debugLog(`Motion Sensor: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
292
288
  }
293
289
 
294
290
  /**
@@ -302,38 +298,16 @@ export class Motion {
302
298
  }
303
299
  }
304
300
 
305
- public async connectBLE() {
306
- let switchbot: any;
307
- try {
308
- // eslint-disable-next-line @typescript-eslint/no-var-requires
309
- const Switchbot = require('node-switchbot');
310
- switchbot = new Switchbot();
311
- // Convert to BLE Address
312
- this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
313
- this.debugLog(`Motion Sensor: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
314
- } catch (e: any) {
315
- switchbot = false;
316
- this.errorLog(`Motion Sensor: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot}`);
317
- if (this.deviceLogging === 'debug') {
318
- this.errorLog(`Motion Sensor: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
319
- + ` Error Message: ${JSON.stringify(e.message)}`);
320
- }
321
- if (this.platform.debugMode) {
322
- this.errorLog(`Motion Sensor: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
323
- + ` Error: ${JSON.stringify(e)}`);
324
- }
325
- }
326
- return switchbot;
327
- }
328
-
329
301
  private async BLERefreshStatus() {
330
302
  this.debugLog(`Motion Sensor: ${this.accessory.displayName} BLE RefreshStatus`);
331
- // eslint-disable-next-line @typescript-eslint/no-var-requires
332
- const switchbot = await this.connectBLE();
303
+ const switchbot = await this.platform.connectBLE();
304
+ // Convert to BLE Address
305
+ this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
306
+ this.debugLog(`Curtain: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
333
307
  // Start to monitor advertisement packets
334
308
  if (switchbot !== false) {
335
309
  switchbot.startScan({
336
- //model: 's',
310
+ model: 's',
337
311
  id: this.device.bleMac,
338
312
  }).then(() => {
339
313
  // Set an event hander
@@ -377,6 +351,7 @@ export class Motion {
377
351
  }
378
352
  if (this.platform.config.credentials?.openToken) {
379
353
  this.warnLog(`Motion Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
354
+ this.SwitchToOpenAPI = true;
380
355
  await this.openAPIRefreshStatus();
381
356
  }
382
357
  this.apiError(e);
@@ -390,6 +365,7 @@ export class Motion {
390
365
  this.errorLog(`Motion Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
391
366
  if (this.platform.config.credentials?.openToken) {
392
367
  this.warnLog(`Motion Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
368
+ this.SwitchToOpenAPI = true;
393
369
  await this.openAPIRefreshStatus();
394
370
  }
395
371
  }
@@ -400,6 +376,11 @@ export class Motion {
400
376
  try {
401
377
  this.deviceStatus = (await this.platform.axios.get(`${DeviceURL}/${this.device.deviceId}/status`)).data;
402
378
  this.debugLog(`Motion Sensor: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(this.deviceStatus)}`);
379
+ if (this.deviceStatus.body.moveDetected === undefined) {
380
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(this.deviceStatus)}`);
381
+ }
382
+ this.moveDetected = this.deviceStatus.body.moveDetected;
383
+ this.brightness = this.deviceStatus.body.brightness;
403
384
  this.parseStatus();
404
385
  this.updateHomeKitCharacteristics();
405
386
  } catch (e: any) {
@@ -4,7 +4,7 @@ import { interval, Subject } from 'rxjs';
4
4
  import { SwitchBotPlatform } from '../platform';
5
5
  import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
6
6
  import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
7
- import { DeviceURL, device, devicesConfig, deviceStatusResponse, payload } from '../settings';
7
+ import { DeviceURL, device, devicesConfig, deviceStatusResponse, payload, deviceStatus } from '../settings';
8
8
 
9
9
  export class Plug {
10
10
  // Services
@@ -15,6 +15,7 @@ export class Plug {
15
15
  OnCached!: CharacteristicValue;
16
16
 
17
17
  // OpenAPI Others
18
+ power: deviceStatus['power'];
18
19
  deviceStatus!: deviceStatusResponse;
19
20
 
20
21
  // Config
@@ -32,9 +33,10 @@ export class Plug {
32
33
  public device: device & devicesConfig,
33
34
  ) {
34
35
  // default placeholders
35
- this.logs();
36
- this.scan();
37
- this.refreshRate();
36
+ this.logs(device);
37
+ this.scan(device);
38
+ this.refreshRate(device);
39
+ this.config(device);
38
40
  if (this.On === undefined) {
39
41
  this.On = false;
40
42
  } else {
@@ -116,55 +118,67 @@ export class Plug {
116
118
  });
117
119
  }
118
120
 
119
- refreshRate() {
120
- if (this.device.refreshRate) {
121
- this.deviceRefreshRate = this.accessory.context.refreshRate = this.device.refreshRate;
122
- if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
123
- this.warnLog(`Plug: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
124
- }
121
+ config(device: device & devicesConfig) {
122
+ const config: any = device.plug;
123
+ if (device.ble !== undefined) {
124
+ config['ble'] = device.ble;
125
+ }
126
+ if (device.logging !== undefined) {
127
+ config['logging'] = device.logging;
128
+ }
129
+ if (device.refreshRate !== undefined) {
130
+ config['refreshRate'] = device.refreshRate;
131
+ }
132
+ if (device.scanDuration !== undefined) {
133
+ config['scanDuration'] = device.scanDuration;
134
+ }
135
+ if (config !== undefined) {
136
+ this.warnLog(`Plug: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
137
+ }
138
+ }
139
+
140
+ refreshRate(device: device & devicesConfig) {
141
+ if (device.refreshRate) {
142
+ this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
143
+ this.debugLog(`Plug: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
125
144
  } else if (this.platform.config.options!.refreshRate) {
126
145
  this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options!.refreshRate;
127
- if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
128
- this.warnLog(`Plug: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
129
- }
146
+ this.debugLog(`Plug: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
130
147
  }
131
148
  }
132
149
 
133
- scan() {
134
- if (this.device.scanDuration) {
135
- this.scanDuration = this.accessory.context.scanDuration = this.device.scanDuration;
136
- if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
137
- this.warnLog(`Plug: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
150
+ scan(device: device & devicesConfig) {
151
+ if (device.scanDuration) {
152
+ this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
153
+ if (device.ble) {
154
+ this.debugLog(`Plug: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
138
155
  }
139
156
  } else {
140
157
  this.scanDuration = this.accessory.context.scanDuration = 1;
141
- if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
142
- this.warnLog(`Plug: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
158
+ if (this.device.ble) {
159
+ this.debugLog(`Plug: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
143
160
  }
144
161
  }
145
162
  }
146
163
 
147
- logs() {
164
+ logs(device: device & devicesConfig) {
148
165
  if (this.platform.debugMode) {
149
- this.deviceLogging = this.accessory.context.logging = 'debug';
150
- this.warnLog(`Plug: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
151
- } else if (this.device.logging) {
152
- this.deviceLogging = this.accessory.context.logging = this.device.logging;
153
- if (this.deviceLogging === 'debug' || this.deviceLogging === 'standard') {
154
- this.warnLog(`Plug: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
155
- }
166
+ this.deviceLogging = this.accessory.context.logging = 'debugMode';
167
+ this.debugLog(`Plug: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
168
+ } else if (device.logging) {
169
+ this.deviceLogging = this.accessory.context.logging = device.logging;
170
+ this.debugLog(`Plug: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
156
171
  } else if (this.platform.config.options?.logging) {
157
172
  this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
158
- if (this.deviceLogging === 'debug' || this.deviceLogging === 'standard') {
159
- this.warnLog(`Plug: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
160
- }
173
+ this.debugLog(`Plug: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
161
174
  } else {
162
175
  this.deviceLogging = this.accessory.context.logging = 'standard';
176
+ this.debugLog(`Plug: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
163
177
  }
164
178
  }
165
179
 
166
180
  parseStatus() {
167
- switch (this.deviceStatus.body.power) {
181
+ switch (this.power) {
168
182
  case 'on':
169
183
  this.On = true;
170
184
  break;
@@ -178,6 +192,7 @@ export class Plug {
178
192
  try {
179
193
  this.deviceStatus = (await this.platform.axios.get(`${DeviceURL}/${this.device.deviceId}/status`)).data;
180
194
  this.debugLog(`Plug: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(this.deviceStatus)}`);
195
+ this.power = this.deviceStatus.body.power;
181
196
  this.parseStatus();
182
197
  this.updateHomeKitCharacteristics();
183
198
  } catch (e: any) {
@@ -49,7 +49,8 @@ export class AirConditioner {
49
49
  public device: irdevice & irDevicesConfig,
50
50
  ) {
51
51
  // default placeholders
52
- this.logs();
52
+ this.logs(device);
53
+ this.config(device);
53
54
  if (this.Active === undefined) {
54
55
  this.Active = this.platform.Characteristic.Active.INACTIVE;
55
56
  } else {
@@ -166,22 +167,29 @@ export class AirConditioner {
166
167
  .onSet(this.RotationSpeedSet.bind(this));
167
168
  }
168
169
 
169
- logs() {
170
+ config(device: irdevice & irDevicesConfig) {
171
+ const config: any = device.irair;
172
+ if (device.logging !== undefined) {
173
+ config['logging'] = device.logging;
174
+ }
175
+ if (config !== undefined) {
176
+ this.warnLog(`Air Conditioner: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
177
+ }
178
+ }
179
+
180
+ logs(device: irdevice & irDevicesConfig) {
170
181
  if (this.platform.debugMode) {
171
- this.deviceLogging = this.accessory.context.logging = 'debug';
172
- this.warnLog(`Air Conditioner: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
173
- } else if (this.device.logging) {
174
- this.deviceLogging = this.accessory.context.logging = this.device.logging;
175
- if (this.deviceLogging === 'debug' || this.deviceLogging === 'standard') {
176
- this.warnLog(`Air Conditioner: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
177
- }
182
+ this.deviceLogging = this.accessory.context.logging = 'debugMode';
183
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
184
+ } else if (device.logging) {
185
+ this.deviceLogging = this.accessory.context.logging = device.logging;
186
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
178
187
  } else if (this.platform.config.options?.logging) {
179
188
  this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
180
- if (this.deviceLogging === 'debug' || this.deviceLogging === 'standard') {
181
- this.warnLog(`Air Conditioner: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
182
- }
189
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
183
190
  } else {
184
191
  this.deviceLogging = this.accessory.context.logging = 'standard';
192
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
185
193
  }
186
194
  }
187
195
 
@@ -44,7 +44,8 @@ export class AirPurifier {
44
44
  public device: irdevice & irDevicesConfig,
45
45
  ) {
46
46
  // default placeholders
47
- this.logs();
47
+ this.logs(device);
48
+ this.config(device);
48
49
  if (this.Active === undefined) {
49
50
  this.Active = this.platform.Characteristic.Active.INACTIVE;
50
51
  } else {
@@ -87,31 +88,38 @@ export class AirPurifier {
87
88
  this.service.getCharacteristic(this.platform.Characteristic.TargetAirPurifierState).onSet(this.TargetAirPurifierStateSet.bind(this));
88
89
  }
89
90
 
90
- logs() {
91
+ config(device: irdevice & irDevicesConfig) {
92
+ const config: any = device.irpur;
93
+ if (device.logging !== undefined) {
94
+ config['logging'] = device.logging;
95
+ }
96
+ if (config !== undefined) {
97
+ this.warnLog(`Air Purifier: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
98
+ }
99
+ }
100
+
101
+ logs(device: irdevice & irDevicesConfig) {
91
102
  if (this.platform.debugMode) {
92
- this.deviceLogging = this.accessory.context.logging = 'debug';
93
- this.warnLog(`Air Purifier: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
94
- } else if (this.device.logging) {
95
- this.deviceLogging = this.accessory.context.logging = this.device.logging;
96
- if (this.deviceLogging === 'debug' || this.deviceLogging === 'standard') {
97
- this.warnLog(`Air Purifier: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
98
- }
103
+ this.deviceLogging = this.accessory.context.logging = 'debugMode';
104
+ this.debugLog(`Air Purifier: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
105
+ } else if (device.logging) {
106
+ this.deviceLogging = this.accessory.context.logging = device.logging;
107
+ this.debugLog(`Air Purifier: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
99
108
  } else if (this.platform.config.options?.logging) {
100
109
  this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
101
- if (this.deviceLogging === 'debug' || this.deviceLogging === 'standard') {
102
- this.warnLog(`Air Purifier: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
103
- }
110
+ this.debugLog(`Air Purifier: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
104
111
  } else {
105
112
  this.deviceLogging = this.accessory.context.logging = 'standard';
113
+ this.debugLog(`Air Purifier: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
106
114
  }
107
115
  }
108
116
 
109
117
  private ActiveSet(value: CharacteristicValue) {
110
118
  this.debugLog(`Air Purifier: ${this.accessory.displayName} Set Active: ${value}`);
111
119
  if (value === this.platform.Characteristic.Active.INACTIVE) {
112
- this.pushAirConditionerOffChanges();
120
+ this.pushAirPurifierOffChanges();
113
121
  } else {
114
- this.pushAirConditionerOnChanges();
122
+ this.pushAirPurifierOnChanges();
115
123
  }
116
124
  this.Active = value;
117
125
  this.ActiveCached = this.Active;
@@ -177,7 +185,7 @@ export class AirPurifier {
177
185
  * AirPurifier: "command" "middleSpeed" "default" = fan speed to medium
178
186
  * AirPurifier: "command" "highSpeed" "default" = fan speed to high
179
187
  */
180
- async pushAirConditionerOnChanges() {
188
+ async pushAirPurifierOnChanges() {
181
189
  if (this.Active !== 1) {
182
190
  const payload = {
183
191
  commandType: 'command',
@@ -188,7 +196,7 @@ export class AirPurifier {
188
196
  }
189
197
  }
190
198
 
191
- async pushAirConditionerOffChanges() {
199
+ async pushAirPurifierOffChanges() {
192
200
  if (this.Active !== 0) {
193
201
  const payload = {
194
202
  commandType: 'command',