@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
@@ -3,7 +3,7 @@ import { interval, Subject } from 'rxjs';
3
3
  import { SwitchBotPlatform } from '../platform';
4
4
  import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
5
5
  import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
6
- import { DeviceURL, device, devicesConfig, serviceData, ad, deviceStatusResponse, payload } from '../settings';
6
+ import { DeviceURL, device, devicesConfig, serviceData, ad, deviceStatusResponse, payload, deviceStatus } from '../settings';
7
7
 
8
8
  /**
9
9
  * Platform Accessory
@@ -25,6 +25,12 @@ export class Humidifier {
25
25
  WaterLevel!: CharacteristicValue;
26
26
 
27
27
  // OpenAPI
28
+ auto: deviceStatus['auto'];
29
+ power: deviceStatus['power'];
30
+ humidity: deviceStatus['humidity'];
31
+ lackWater: deviceStatus['lackWater'];
32
+ temperature: deviceStatus['temperature'];
33
+ nebulizationEfficiency: deviceStatus['nebulizationEfficiency'];
28
34
  deviceStatus!: deviceStatusResponse;
29
35
 
30
36
  // BLE Others
@@ -53,6 +59,7 @@ export class Humidifier {
53
59
  this.logs();
54
60
  this.scan();
55
61
  this.refreshRate();
62
+ this.config(device);
56
63
  this.CurrentRelativeHumidity = 0;
57
64
  this.TargetHumidifierDehumidifierState = this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER;
58
65
  this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.INACTIVE;
@@ -147,7 +154,7 @@ export class Humidifier {
147
154
  minStep: 0.1,
148
155
  })
149
156
  .onGet(() => {
150
- return Number.isNaN(this.CurrentTemperature);
157
+ return this.CurrentTemperature;
151
158
  });
152
159
  } else {
153
160
  this.debugLog(`Humidifier: ${accessory.displayName} Temperature Sensor Service Not Added`);
@@ -191,6 +198,25 @@ export class Humidifier {
191
198
  });
192
199
  }
193
200
 
201
+ config(device: device & devicesConfig) {
202
+ const config: any = device.humidifier;
203
+ if (device.ble !== undefined) {
204
+ config['ble'] = device.ble;
205
+ }
206
+ if (device.logging !== undefined) {
207
+ config['logging'] = device.logging;
208
+ }
209
+ if (device.refreshRate !== undefined) {
210
+ config['refreshRate'] = device.refreshRate;
211
+ }
212
+ if (device.scanDuration !== undefined) {
213
+ config['scanDuration'] = device.scanDuration;
214
+ }
215
+ if (config !== undefined) {
216
+ this.warnLog(`Humidifier: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
217
+ }
218
+ }
219
+
194
220
  refreshRate() {
195
221
  if (this.device.refreshRate) {
196
222
  this.deviceRefreshRate = this.accessory.context.refreshRate = this.device.refreshRate;
@@ -276,15 +302,15 @@ export class Humidifier {
276
302
  if (this.platform.config.credentials?.openToken) {
277
303
  this.debugLog(`Humidifier: ${this.accessory.displayName} OpenAPI parseStatus`);
278
304
  // Current Relative Humidity
279
- this.CurrentRelativeHumidity = this.deviceStatus.body.humidity!;
305
+ this.CurrentRelativeHumidity = this.humidity!;
280
306
  this.debugLog(`Humidifier: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
281
307
  // Current Temperature
282
308
  if (!this.device.humidifier?.hide_temperature) {
283
- this.CurrentTemperature = Number(this.deviceStatus.body.temperature);
309
+ this.CurrentTemperature = this.temperature!;
284
310
  this.debugLog(`Humidifier: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
285
311
  }
286
312
  // Target Humidifier Dehumidifier State
287
- switch (this.deviceStatus.body.auto) {
313
+ switch (this.auto) {
288
314
  case true:
289
315
  this.TargetHumidifierDehumidifierState = this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER_OR_DEHUMIDIFIER;
290
316
  this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.HUMIDIFYING;
@@ -292,10 +318,10 @@ export class Humidifier {
292
318
  break;
293
319
  default:
294
320
  this.TargetHumidifierDehumidifierState = this.platform.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER;
295
- if (this.deviceStatus.body.nebulizationEfficiency! > 100) {
321
+ if (this.nebulizationEfficiency! > 100) {
296
322
  this.RelativeHumidityHumidifierThreshold = 100;
297
323
  } else {
298
- this.RelativeHumidityHumidifierThreshold = this.deviceStatus.body.nebulizationEfficiency!;
324
+ this.RelativeHumidityHumidifierThreshold = this.nebulizationEfficiency!;
299
325
  }
300
326
  if (this.CurrentRelativeHumidity > this.RelativeHumidityHumidifierThreshold) {
301
327
  this.CurrentHumidifierDehumidifierState = this.platform.Characteristic.CurrentHumidifierDehumidifierState.IDLE;
@@ -310,7 +336,7 @@ export class Humidifier {
310
336
  + ` RelativeHumidityHumidifierThreshold: ${this.RelativeHumidityHumidifierThreshold}`);
311
337
  this.debugLog(`Humidifier: ${this.accessory.displayName} CurrentHumidifierDehumidifierState: ${this.CurrentHumidifierDehumidifierState}`);
312
338
  // Active
313
- switch (this.deviceStatus.body.power) {
339
+ switch (this.power) {
314
340
  case 'on':
315
341
  this.Active = this.platform.Characteristic.Active.ACTIVE;
316
342
  break;
@@ -319,7 +345,7 @@ export class Humidifier {
319
345
  }
320
346
  this.debugLog(`Humidifier: ${this.accessory.displayName} Active: ${this.Active}`);
321
347
  // Water Level
322
- if (this.deviceStatus.body.lackWater) {
348
+ if (this.lackWater) {
323
349
  this.WaterLevel = 0;
324
350
  } else {
325
351
  this.WaterLevel = 100;
@@ -339,34 +365,12 @@ export class Humidifier {
339
365
  }
340
366
  }
341
367
 
342
- public async connectBLE() {
343
- let switchbot: any;
344
- try {
345
- // eslint-disable-next-line @typescript-eslint/no-var-requires
346
- const Switchbot = require('node-switchbot');
347
- switchbot = new Switchbot();
348
- // Convert to BLE Address
349
- this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
350
- this.debugLog(`Humidifier: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
351
- } catch (e: any) {
352
- switchbot = false;
353
- this.errorLog(`Humidifier: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot}`);
354
- if (this.deviceLogging === 'debug') {
355
- this.errorLog(`Humidifier: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
356
- + ` Error Message: ${JSON.stringify(e.message)}`);
357
- }
358
- if (this.platform.debugMode) {
359
- this.errorLog(`Humidifier: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
360
- + ` Error: ${JSON.stringify(e)}`);
361
- }
362
- }
363
- return switchbot;
364
- }
365
-
366
368
  private async BLERefreshStatus() {
367
369
  this.debugLog(`Humidifier: ${this.accessory.displayName} BLE refreshStatus`);
368
- // eslint-disable-next-line @typescript-eslint/no-var-requires
369
- const switchbot = await this.connectBLE();
370
+ const switchbot = await this.platform.connectBLE();
371
+ // Convert to BLE Address
372
+ this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
373
+ this.debugLog(`Curtain: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
370
374
  // Start to monitor advertisement packets
371
375
  if (switchbot !== false) {
372
376
  switchbot.startScan({
@@ -438,10 +442,16 @@ export class Humidifier {
438
442
  this.deviceStatus = (await this.platform.axios.get(`${DeviceURL}/${this.device.deviceId}/status`)).data;
439
443
  if (this.deviceStatus.message === 'success') {
440
444
  this.debugLog(`Humidifier: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(this.deviceStatus)}`);
445
+ this.auto = this.deviceStatus.body.auto;
446
+ this.power = this.deviceStatus.body.power;
447
+ this.lackWater = this.deviceStatus.body.lackWater;
448
+ this.humidity = this.deviceStatus.body.humidity;
449
+ this.temperature = this.deviceStatus.body.temperature;
450
+ this.nebulizationEfficiency = this.deviceStatus.body.nebulizationEfficiency;
441
451
  this.parseStatus();
442
452
  this.updateHomeKitCharacteristics();
443
453
  } else {
444
- this.debugLog(this.deviceStatus);
454
+ this.errorLog(`Humidifier: ${this.accessory.displayName} message: ${JSON.stringify(this.deviceStatus.message)}`);
445
455
  }
446
456
  } catch (e: any) {
447
457
  this.errorLog(`Humidifier: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
@@ -477,7 +487,10 @@ export class Humidifier {
477
487
 
478
488
  private async BLEpushChanges() {
479
489
  this.debugLog(`Humidifier: ${this.accessory.displayName} BLE pushChanges`);
480
- const switchbot = await this.connectBLE();
490
+ const switchbot = await this.platform.connectBLE();
491
+ // Convert to BLE Address
492
+ this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
493
+ this.debugLog(`Curtain: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
481
494
  if (switchbot !== false) {
482
495
  switchbot.discover({ model: 'e', quick: true, id: this.device.bleMac }).then((device_list) => {
483
496
  this.infoLog(`${this.accessory.displayName} Target Position: ${this.Active}`);
@@ -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, Units, CharacteristicValue } from 'homebridge';
5
- import { DeviceURL, device, devicesConfig, serviceData, ad, switchbot, deviceStatusResponse, temperature } from '../settings';
5
+ import { DeviceURL, device, devicesConfig, serviceData, ad, switchbot, deviceStatusResponse, temperature, deviceStatus } from '../settings';
6
6
 
7
7
  /**
8
8
  * Platform Accessory
@@ -11,28 +11,30 @@ import { DeviceURL, device, devicesConfig, serviceData, ad, switchbot, deviceSta
11
11
  */
12
12
  export class Meter {
13
13
  // Services
14
- private service: Service;
15
- temperatureservice?: Service;
16
- humidityservice?: Service;
14
+ private batteryService?: Service;
15
+ private temperatureservice?: Service;
16
+ private humidityservice?: Service;
17
17
 
18
18
  // Characteristic Values
19
19
  CurrentRelativeHumidity!: CharacteristicValue;
20
20
  CurrentRelativeHumidityCached!: CharacteristicValue;
21
- CurrentTemperature!: CharacteristicValue;
22
- CurrentTemperatureCached!: CharacteristicValue;
23
- BatteryLevel!: CharacteristicValue;
24
- BatteryLevelCached!: CharacteristicValue;
25
- ChargingState!: CharacteristicValue;
26
- StatusLowBattery!: CharacteristicValue;
21
+ CurrentTemperature?: CharacteristicValue;
22
+ CurrentTemperatureCached?: CharacteristicValue;
23
+ BatteryLevel?: CharacteristicValue;
24
+ ChargingState?: CharacteristicValue;
25
+ StatusLowBattery?: CharacteristicValue;
27
26
  Active!: CharacteristicValue;
28
27
  WaterLevel!: CharacteristicValue;
29
28
 
30
29
  // OpenAPI Others
30
+ Temperature: deviceStatus['temperature'];
31
+ Humidity: deviceStatus['humidity'];
31
32
  deviceStatus!: deviceStatusResponse;
32
33
 
33
34
  // BLE Others
34
35
  connected?: boolean;
35
36
  switchbot!: switchbot;
37
+ SwitchToOpenAPI?: boolean;
36
38
  serviceData!: serviceData;
37
39
  temperature!: temperature['c'];
38
40
  battery!: serviceData['battery'];
@@ -53,20 +55,10 @@ export class Meter {
53
55
  public device: device & devicesConfig,
54
56
  ) {
55
57
  // default placeholders
56
- this.logs();
57
- this.scan();
58
- this.refreshRate();
59
- if (this.BatteryLevel === undefined) {
60
- this.BatteryLevel = 100;
61
- } else {
62
- this.BatteryLevel = this.accessory.context.BatteryLevel;
63
- }
64
- if (this.BatteryLevel < 15) {
65
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
66
- } else {
67
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
68
- }
69
- this.ChargingState = this.platform.Characteristic.ChargingState.NOT_CHARGEABLE;
58
+ this.logs(device);
59
+ this.scan(device);
60
+ this.refreshRate(device);
61
+ this.config(device);
70
62
  if (this.CurrentRelativeHumidity === undefined) {
71
63
  this.CurrentRelativeHumidity = 0;
72
64
  } else {
@@ -96,26 +88,6 @@ export class Meter {
96
88
  .setCharacteristic(this.platform.Characteristic.Model, 'SWITCHBOT-METERTH-S1')
97
89
  .setCharacteristic(this.platform.Characteristic.SerialNumber, device.deviceId!);
98
90
 
99
- // get the Battery service if it exists, otherwise create a new Battery service
100
- // you can create multiple services for each accessory
101
- (this.service =
102
- accessory.getService(this.platform.Service.Battery) ||
103
- accessory.addService(this.platform.Service.Battery)), `${accessory.displayName} Battery`;
104
-
105
- // To avoid "Cannot add a Service with the same UUID another Service without also defining a unique 'subtype' property." error,
106
- // when creating multiple services of the same type, you need to use the following syntax to specify a name and subtype id:
107
- // accessory.getService('NAME') ?? accessory.addService(this.platform.Service.Battery, 'NAME', 'USER_DEFINED_SUBTYPE');
108
-
109
- // set the service name, this is what is displayed as the default name on the Home app
110
- // in this example we are using the name we stored in the `accessory.context` in the `discoverDevices` method.
111
- this.service.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
112
-
113
- // each service must implement at-minimum the "required characteristics" for the given service type
114
- // see https://developers.homebridge.io/#/service/Battery
115
-
116
- // create handlers for required characteristics
117
- this.service.setCharacteristic(this.platform.Characteristic.ChargingState, 2);
118
-
119
91
  // Temperature Sensor Service
120
92
  if (device.meter?.hide_temperature) {
121
93
  this.debugLog(`Meter: ${accessory.displayName} Removing Temperature Sensor Service`);
@@ -127,7 +99,7 @@ export class Meter {
127
99
  this.accessory.getService(this.platform.Service.TemperatureSensor) ||
128
100
  this.accessory.addService(this.platform.Service.TemperatureSensor)), `${accessory.displayName} Temperature Sensor`;
129
101
 
130
- this.service.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Temperature Sensor`);
102
+ this.temperatureservice.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Temperature Sensor`);
131
103
 
132
104
  this.temperatureservice
133
105
  .getCharacteristic(this.platform.Characteristic.CurrentTemperature)
@@ -139,7 +111,7 @@ export class Meter {
139
111
  minStep: 0.1,
140
112
  })
141
113
  .onGet(() => {
142
- return this.CurrentTemperature;
114
+ return this.CurrentTemperature!;
143
115
  });
144
116
  } else {
145
117
  this.debugLog(`Meter: ${accessory.displayName} Temperature Sensor Service Not Added`);
@@ -156,7 +128,7 @@ export class Meter {
156
128
  this.accessory.getService(this.platform.Service.HumiditySensor) ||
157
129
  this.accessory.addService(this.platform.Service.HumiditySensor)), `${accessory.displayName} Humidity Sensor`;
158
130
 
159
- this.service.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Humidity Sensor`);
131
+ this.humidityservice.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Humidity Sensor`);
160
132
 
161
133
  this.humidityservice
162
134
  .getCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity)
@@ -170,6 +142,27 @@ export class Meter {
170
142
  this.debugLog(`Meter: ${accessory.displayName} Humidity Sensor Service Not Added`);
171
143
  }
172
144
 
145
+ // Battery Service
146
+ if (!device.ble) {
147
+ this.debugLog(`Meter: ${accessory.displayName} Removing Battery Service`);
148
+ this.batteryService = this.accessory.getService(this.platform.Service.Battery);
149
+ accessory.removeService(this.batteryService!);
150
+ } else if (device.ble && !this.batteryService) {
151
+ this.debugLog(`Meter: ${accessory.displayName} Add Battery Service`);
152
+ (this.batteryService =
153
+ this.accessory.getService(this.platform.Service.Battery) ||
154
+ this.accessory.addService(this.platform.Service.Battery)), `${accessory.displayName} Battery`;
155
+
156
+ this.batteryService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Battery`);
157
+
158
+ this.batteryService.setCharacteristic(this.platform.Characteristic.ChargingState, this.platform.Characteristic.ChargingState.NOT_CHARGEABLE);
159
+
160
+ } else {
161
+ this.debugLog(`Meter: ${accessory.displayName} Battery Service Not Added`);
162
+ }
163
+
164
+
165
+
173
166
  // Retrieve initial values and updateHomekit
174
167
  this.updateHomeKitCharacteristics();
175
168
 
@@ -181,50 +174,62 @@ export class Meter {
181
174
  });
182
175
  }
183
176
 
184
- refreshRate() {
185
- if (this.device.refreshRate) {
186
- this.deviceRefreshRate = this.accessory.context.refreshRate = this.device.refreshRate;
187
- if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
188
- this.warnLog(`Meter: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
189
- }
177
+ config(device: device & devicesConfig) {
178
+ const config: any = device.meter;
179
+ if (device.ble !== undefined) {
180
+ config['ble'] = device.ble;
181
+ }
182
+ if (device.logging !== undefined) {
183
+ config['logging'] = device.logging;
184
+ }
185
+ if (device.refreshRate !== undefined) {
186
+ config['refreshRate'] = device.refreshRate;
187
+ }
188
+ if (device.scanDuration !== undefined) {
189
+ config['scanDuration'] = device.scanDuration;
190
+ }
191
+ if (config !== undefined) {
192
+ this.warnLog(`Meter: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
193
+ }
194
+ }
195
+
196
+ refreshRate(device: device & devicesConfig) {
197
+ if (device.refreshRate) {
198
+ this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
199
+ this.debugLog(`Meter: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
190
200
  } else if (this.platform.config.options!.refreshRate) {
191
201
  this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options!.refreshRate;
192
- if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
193
- this.warnLog(`Meter: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
194
- }
202
+ this.debugLog(`Meter: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
195
203
  }
196
204
  }
197
205
 
198
- scan() {
199
- if (this.device.scanDuration) {
200
- this.scanDuration = this.accessory.context.scanDuration = this.device.scanDuration;
201
- if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
202
- this.warnLog(`Meter: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
206
+ scan(device: device & devicesConfig) {
207
+ if (device.scanDuration) {
208
+ this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
209
+ if (device.ble) {
210
+ this.debugLog(`Meter: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
203
211
  }
204
212
  } else {
205
213
  this.scanDuration = this.accessory.context.scanDuration = 1;
206
- if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
207
- this.warnLog(`Meter: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
214
+ if (this.device.ble) {
215
+ this.debugLog(`Meter: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
208
216
  }
209
217
  }
210
218
  }
211
219
 
212
- logs() {
220
+ logs(device: device & devicesConfig) {
213
221
  if (this.platform.debugMode) {
214
- this.deviceLogging = this.accessory.context.logging = 'debug';
215
- this.warnLog(`Meter: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
216
- } else if (this.device.logging) {
217
- this.deviceLogging = this.accessory.context.logging = this.device.logging;
218
- if (this.deviceLogging === 'debug' || this.deviceLogging === 'standard') {
219
- this.warnLog(`Meter: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
220
- }
222
+ this.deviceLogging = this.accessory.context.logging = 'debugMode';
223
+ this.debugLog(`Meter: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
224
+ } else if (device.logging) {
225
+ this.deviceLogging = this.accessory.context.logging = device.logging;
226
+ this.debugLog(`Meter: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
221
227
  } else if (this.platform.config.options?.logging) {
222
228
  this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
223
- if (this.deviceLogging === 'debug' || this.deviceLogging === 'standard') {
224
- this.warnLog(`Meter: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
225
- }
229
+ this.debugLog(`Meter: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
226
230
  } else {
227
231
  this.deviceLogging = this.accessory.context.logging = 'standard';
232
+ this.debugLog(`Meter: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
228
233
  }
229
234
  }
230
235
 
@@ -232,10 +237,10 @@ export class Meter {
232
237
  * Parse the device status from the SwitchBot api
233
238
  */
234
239
  async parseStatus() {
235
- if (this.device.ble) {
236
- await this.BLEparseStatus();
237
- } else {
240
+ if (this.SwitchToOpenAPI || !this.device.ble) {
238
241
  await this.openAPIparseStatus();
242
+ } else {
243
+ await this.BLEparseStatus();
239
244
  }
240
245
  }
241
246
 
@@ -251,9 +256,9 @@ export class Meter {
251
256
  }
252
257
  this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
253
258
 
259
+ // Humidity
254
260
  if (!this.device.meter?.hide_humidity) {
255
- // Humidity
256
- this.CurrentRelativeHumidity = Number(this.humidity);
261
+ this.CurrentRelativeHumidity = this.humidity!;
257
262
  this.debugLog(`Meter: ${this.accessory.displayName} Humidity: ${this.CurrentRelativeHumidity}%`);
258
263
  }
259
264
 
@@ -266,27 +271,20 @@ export class Meter {
266
271
  }
267
272
 
268
273
  private async openAPIparseStatus() {
274
+ if (this.device.ble) {
275
+ this.SwitchToOpenAPI = false;
276
+ }
269
277
  if (this.platform.config.credentials?.openToken) {
270
278
  this.debugLog(`Meter: ${this.accessory.displayName} OpenAPI parseStatus`);
271
- if (this.deviceStatus.body) {
272
- this.BatteryLevel = 100;
273
- } else {
274
- this.BatteryLevel = 10;
275
- }
276
- if (this.BatteryLevel < 15) {
277
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
278
- } else {
279
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
280
- }
281
279
  // Current Relative Humidity
282
280
  if (!this.device.meter?.hide_humidity) {
283
- this.CurrentRelativeHumidity = this.deviceStatus.body.humidity!;
281
+ this.CurrentRelativeHumidity = this.Humidity!;
284
282
  this.debugLog(`Meter: ${this.accessory.displayName} Humidity: ${this.CurrentRelativeHumidity}%`);
285
283
  }
286
284
 
287
285
  // Current Temperature
288
286
  if (!this.device.meter?.hide_temperature) {
289
- this.CurrentTemperature = Number(this.deviceStatus.body.temperature);
287
+ this.CurrentTemperature = this.Temperature!;
290
288
  this.debugLog(`Meter: ${this.accessory.displayName} Temperature: ${this.CurrentTemperature}°c`);
291
289
  }
292
290
  }
@@ -303,34 +301,12 @@ export class Meter {
303
301
  }
304
302
  }
305
303
 
306
- private async connectBLE() {
307
- let switchbot: any;
308
- try {
309
- // eslint-disable-next-line @typescript-eslint/no-var-requires
310
- const Switchbot = require('node-switchbot');
311
- switchbot = new Switchbot();
312
- // Convert to BLE Address
313
- this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
314
- this.debugLog(`Meter: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
315
- } catch (e: any) {
316
- switchbot = false;
317
- this.errorLog(`Meter: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot}`);
318
- if (this.deviceLogging === 'debug') {
319
- this.errorLog(`Meter: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
320
- + ` Error Message: ${JSON.stringify(e.message)}`);
321
- }
322
- if (this.platform.debugMode) {
323
- this.errorLog(`Meter: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
324
- + ` Error: ${JSON.stringify(e)}`);
325
- }
326
- }
327
- return switchbot;
328
- }
329
-
330
304
  private async BLErefreshStatus() {
331
305
  this.debugLog(`Meter: ${this.accessory.displayName} BLE RefreshStatus`);
332
- // eslint-disable-next-line @typescript-eslint/no-var-requires
333
- const switchbot = await this.connectBLE();
306
+ const switchbot = await this.platform.connectBLE();
307
+ // Convert to BLE Address
308
+ this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
309
+ this.debugLog(`Curtain: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
334
310
  // Start to monitor advertisement packets
335
311
  if (switchbot !== false) {
336
312
  switchbot.startScan({
@@ -362,12 +338,6 @@ export class Meter {
362
338
  // Stop to monitor
363
339
  switchbot.stopScan();
364
340
  if (this.connected) {
365
- this.CurrentTemperatureCached = this.temperature;
366
- this.accessory.context.CurrentTemperature = this.CurrentTemperatureCached;
367
- this.CurrentRelativeHumidityCached = this.humidity!;
368
- this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidityCached;
369
- this.BatteryLevelCached = this.battery!;
370
- this.accessory.context.BatteryLevel = this.BatteryLevelCached;
371
341
  this.parseStatus();
372
342
  this.updateHomeKitCharacteristics();
373
343
  } else {
@@ -385,6 +355,7 @@ export class Meter {
385
355
  }
386
356
  if (this.platform.config.credentials?.openToken) {
387
357
  this.warnLog(`Meter: ${this.accessory.displayName} Using OpenAPI Connection`);
358
+ this.SwitchToOpenAPI = true;
388
359
  this.openAPIRefreshStatus();
389
360
  }
390
361
  this.apiError(e);
@@ -398,6 +369,7 @@ export class Meter {
398
369
  this.errorLog(`Meter: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
399
370
  if (this.platform.config.credentials?.openToken) {
400
371
  this.warnLog(`Meter: ${this.accessory.displayName} Using OpenAPI Connection`);
372
+ this.SwitchToOpenAPI = true;
401
373
  await this.openAPIRefreshStatus();
402
374
  }
403
375
  }
@@ -408,12 +380,8 @@ export class Meter {
408
380
  try {
409
381
  this.deviceStatus = (await this.platform.axios.get(`${DeviceURL}/${this.device.deviceId}/status`)).data;
410
382
  this.debugLog(`Meter: ${this.accessory.displayName} openAPIRefreshStatus: ${JSON.stringify(this.deviceStatus)}`);
411
- this.CurrentTemperatureCached = this.deviceStatus.body.humidity!;
412
- this.accessory.context.CurrentTemperature = this.CurrentTemperatureCached;
413
- this.CurrentRelativeHumidityCached = this.deviceStatus.body.humidity!;
414
- this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidityCached;
415
- this.BatteryLevelCached = 100;
416
- this.accessory.context.BatteryLevel = this.BatteryLevelCached;
383
+ this.Humidity = this.deviceStatus.body.humidity!;
384
+ this.Temperature = this.deviceStatus.body.temperature!;
417
385
  this.parseStatus();
418
386
  this.updateHomeKitCharacteristics();
419
387
  } catch (e: any) {
@@ -435,18 +403,6 @@ export class Meter {
435
403
  * Updates the status for each of the HomeKit Characteristics
436
404
  */
437
405
  updateHomeKitCharacteristics() {
438
- if (this.StatusLowBattery === undefined) {
439
- this.debugLog(`Meter: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
440
- } else {
441
- this.service.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
442
- this.debugLog(`Meter: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
443
- }
444
- if (this.BatteryLevel === undefined) {
445
- this.debugLog(`Meter: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
446
- } else {
447
- this.service.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
448
- this.debugLog(`Meter: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
449
- }
450
406
  if (!this.device.meter?.hide_humidity) {
451
407
  if (this.CurrentRelativeHumidity === undefined) {
452
408
  this.debugLog(`Meter: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
@@ -463,17 +419,33 @@ export class Meter {
463
419
  this.debugLog(`Meter: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`);
464
420
  }
465
421
  }
422
+ if (this.device.ble) {
423
+ if (this.BatteryLevel === undefined) {
424
+ this.debugLog(`Meter: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
425
+ } else {
426
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
427
+ this.debugLog(`Meter: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
428
+ }
429
+ if (this.StatusLowBattery === undefined) {
430
+ this.debugLog(`Meter: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
431
+ } else {
432
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
433
+ this.debugLog(`Meter: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
434
+ }
435
+ }
466
436
  }
467
437
 
468
438
  public apiError(e: any) {
469
- this.service.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
470
- this.service.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
471
439
  if (!this.device.meter?.hide_humidity) {
472
440
  this.humidityservice?.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, e);
473
441
  }
474
442
  if (!this.device.meter?.hide_temperature) {
475
443
  this.temperatureservice?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, e);
476
444
  }
445
+ if (this.device.ble) {
446
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
447
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
448
+ }
477
449
  //throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
478
450
  }
479
451