@switchbot/homebridge-switchbot 1.8.2 → 1.8.3-beta.0

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 (104) hide show
  1. package/.eslintrc +1 -1
  2. package/.vscode/extensions.json +5 -0
  3. package/.vscode/settings.json +15 -0
  4. package/dist/devices/bots.d.ts +34 -35
  5. package/dist/devices/bots.d.ts.map +1 -1
  6. package/dist/devices/bots.js +241 -184
  7. package/dist/devices/bots.js.map +1 -1
  8. package/dist/devices/colorbulb.d.ts +50 -50
  9. package/dist/devices/colorbulb.d.ts.map +1 -1
  10. package/dist/devices/colorbulb.js +145 -131
  11. package/dist/devices/colorbulb.js.map +1 -1
  12. package/dist/devices/contact.d.ts +27 -29
  13. package/dist/devices/contact.d.ts.map +1 -1
  14. package/dist/devices/contact.js +138 -115
  15. package/dist/devices/contact.js.map +1 -1
  16. package/dist/devices/curtains.d.ts +29 -31
  17. package/dist/devices/curtains.d.ts.map +1 -1
  18. package/dist/devices/curtains.js +211 -176
  19. package/dist/devices/curtains.js.map +1 -1
  20. package/dist/devices/humidifiers.d.ts +33 -33
  21. package/dist/devices/humidifiers.d.ts.map +1 -1
  22. package/dist/devices/humidifiers.js +214 -189
  23. package/dist/devices/humidifiers.js.map +1 -1
  24. package/dist/devices/meters.d.ts +30 -31
  25. package/dist/devices/meters.d.ts.map +1 -1
  26. package/dist/devices/meters.js +124 -100
  27. package/dist/devices/meters.js.map +1 -1
  28. package/dist/devices/motion.d.ts +25 -26
  29. package/dist/devices/motion.d.ts.map +1 -1
  30. package/dist/devices/motion.js +120 -102
  31. package/dist/devices/motion.js.map +1 -1
  32. package/dist/devices/plugs.d.ts +23 -23
  33. package/dist/devices/plugs.d.ts.map +1 -1
  34. package/dist/devices/plugs.js +81 -63
  35. package/dist/devices/plugs.js.map +1 -1
  36. package/dist/index.d.ts +2 -2
  37. package/dist/index.d.ts.map +1 -1
  38. package/dist/index.js +22 -3
  39. package/dist/index.js.map +1 -1
  40. package/dist/irdevices/airconditioners.d.ts +6 -6
  41. package/dist/irdevices/airconditioners.d.ts.map +1 -1
  42. package/dist/irdevices/airconditioners.js +105 -79
  43. package/dist/irdevices/airconditioners.js.map +1 -1
  44. package/dist/irdevices/airpurifiers.d.ts +5 -5
  45. package/dist/irdevices/airpurifiers.d.ts.map +1 -1
  46. package/dist/irdevices/airpurifiers.js +56 -42
  47. package/dist/irdevices/airpurifiers.js.map +1 -1
  48. package/dist/irdevices/cameras.d.ts +5 -5
  49. package/dist/irdevices/cameras.d.ts.map +1 -1
  50. package/dist/irdevices/cameras.js +31 -27
  51. package/dist/irdevices/cameras.js.map +1 -1
  52. package/dist/irdevices/fans.d.ts +5 -5
  53. package/dist/irdevices/fans.d.ts.map +1 -1
  54. package/dist/irdevices/fans.js +45 -38
  55. package/dist/irdevices/fans.js.map +1 -1
  56. package/dist/irdevices/lights.d.ts +5 -5
  57. package/dist/irdevices/lights.d.ts.map +1 -1
  58. package/dist/irdevices/lights.js +37 -33
  59. package/dist/irdevices/lights.js.map +1 -1
  60. package/dist/irdevices/others.d.ts +5 -5
  61. package/dist/irdevices/others.d.ts.map +1 -1
  62. package/dist/irdevices/others.js +34 -29
  63. package/dist/irdevices/others.js.map +1 -1
  64. package/dist/irdevices/tvs.d.ts +5 -5
  65. package/dist/irdevices/tvs.d.ts.map +1 -1
  66. package/dist/irdevices/tvs.js +91 -76
  67. package/dist/irdevices/tvs.js.map +1 -1
  68. package/dist/irdevices/vacuumcleaners.d.ts +5 -5
  69. package/dist/irdevices/vacuumcleaners.d.ts.map +1 -1
  70. package/dist/irdevices/vacuumcleaners.js +31 -27
  71. package/dist/irdevices/vacuumcleaners.js.map +1 -1
  72. package/dist/irdevices/waterheaters.d.ts +5 -5
  73. package/dist/irdevices/waterheaters.d.ts.map +1 -1
  74. package/dist/irdevices/waterheaters.js +31 -27
  75. package/dist/irdevices/waterheaters.js.map +1 -1
  76. package/dist/platform.d.ts +12 -12
  77. package/dist/platform.d.ts.map +1 -1
  78. package/dist/platform.js +241 -172
  79. package/dist/platform.js.map +1 -1
  80. package/dist/settings.d.ts +1 -1
  81. package/dist/settings.d.ts.map +1 -1
  82. package/dist/settings.js +4 -4
  83. package/dist/settings.js.map +1 -1
  84. package/package.json +72 -71
  85. package/src/devices/bots.ts +927 -442
  86. package/src/devices/colorbulb.ts +473 -246
  87. package/src/devices/contact.ts +381 -208
  88. package/src/devices/curtains.ts +613 -321
  89. package/src/devices/humidifiers.ts +594 -323
  90. package/src/devices/meters.ts +331 -186
  91. package/src/devices/motion.ts +327 -184
  92. package/src/devices/plugs.ts +175 -107
  93. package/src/index.ts +5 -5
  94. package/src/irdevices/airconditioners.ts +360 -149
  95. package/src/irdevices/airpurifiers.ts +178 -80
  96. package/src/irdevices/cameras.ts +109 -51
  97. package/src/irdevices/fans.ts +174 -77
  98. package/src/irdevices/lights.ts +110 -57
  99. package/src/irdevices/others.ts +137 -62
  100. package/src/irdevices/tvs.ts +249 -124
  101. package/src/irdevices/vacuumcleaners.ts +112 -52
  102. package/src/irdevices/waterheaters.ts +115 -53
  103. package/src/platform.ts +768 -357
  104. package/src/settings.ts +17 -23
@@ -1,8 +1,8 @@
1
- import { interval, Subject } from 'rxjs';
2
- import { skipWhile } from 'rxjs/operators';
3
- import { SwitchBotPlatform } from '../platform';
4
- import { Service, PlatformAccessory, Units, CharacteristicValue } from 'homebridge';
5
- import { DeviceURL, device, devicesConfig, serviceData, ad, switchbot, deviceStatusResponse, temperature, deviceStatus } from '../settings';
1
+ import * as rxjs from "rxjs";
2
+ import * as operators from "rxjs/operators";
3
+ import * as platform_1 from "../platform";
4
+ import * as homebridge from "homebridge";
5
+ import * as settings from "../settings";
6
6
 
7
7
  /**
8
8
  * Platform Accessory
@@ -11,34 +11,34 @@ import { DeviceURL, device, devicesConfig, serviceData, ad, switchbot, deviceSta
11
11
  */
12
12
  export class Meter {
13
13
  // Services
14
- private batteryService?: Service;
15
- private temperatureservice?: Service;
16
- private humidityservice?: Service;
14
+ private batteryService?: homebridge.Service;
15
+ private temperatureservice?: homebridge.Service;
16
+ private humidityservice?: homebridge.Service;
17
17
 
18
18
  // Characteristic Values
19
- CurrentRelativeHumidity!: CharacteristicValue;
20
- CurrentRelativeHumidityCached!: CharacteristicValue;
21
- CurrentTemperature?: CharacteristicValue;
22
- CurrentTemperatureCached?: CharacteristicValue;
23
- BatteryLevel?: CharacteristicValue;
24
- ChargingState?: CharacteristicValue;
25
- StatusLowBattery?: CharacteristicValue;
26
- Active!: CharacteristicValue;
27
- WaterLevel!: CharacteristicValue;
19
+ CurrentRelativeHumidity!: homebridge.CharacteristicValue;
20
+ CurrentRelativeHumidityCached!: homebridge.CharacteristicValue;
21
+ CurrentTemperature?: homebridge.CharacteristicValue;
22
+ CurrentTemperatureCached?: homebridge.CharacteristicValue;
23
+ BatteryLevel?: homebridge.CharacteristicValue;
24
+ ChargingState?: homebridge.CharacteristicValue;
25
+ StatusLowBattery?: homebridge.CharacteristicValue;
26
+ Active!: homebridge.CharacteristicValue;
27
+ WaterLevel!: homebridge.CharacteristicValue;
28
28
 
29
29
  // OpenAPI Others
30
- Temperature: deviceStatus['temperature'];
31
- Humidity: deviceStatus['humidity'];
32
- deviceStatus!: deviceStatusResponse;
30
+ Temperature: settings.deviceStatus["temperature"];
31
+ Humidity: settings.deviceStatus["humidity"];
32
+ deviceStatus!: settings.deviceStatusResponse;
33
33
 
34
34
  // BLE Others
35
35
  connected?: boolean;
36
- switchbot!: switchbot;
36
+ switchbot!: settings.switchbot;
37
37
  SwitchToOpenAPI?: boolean;
38
- serviceData!: serviceData;
39
- temperature!: temperature['c'];
40
- battery!: serviceData['battery'];
41
- humidity!: serviceData['humidity'];
38
+ serviceData!: settings.serviceData;
39
+ temperature!: settings.temperature["c"];
40
+ battery!: settings.serviceData["battery"];
41
+ humidity!: settings.serviceData["humidity"];
42
42
 
43
43
  // Config
44
44
  scanDuration!: number;
@@ -47,12 +47,12 @@ export class Meter {
47
47
 
48
48
  // Updates
49
49
  meterUpdateInProgress!: boolean;
50
- doMeterUpdate: Subject<void>;
50
+ doMeterUpdate: rxjs.Subject<void>;
51
51
 
52
52
  constructor(
53
- private readonly platform: SwitchBotPlatform,
54
- private accessory: PlatformAccessory,
55
- public device: device & devicesConfig,
53
+ private readonly platform: platform_1.SwitchBotPlatform,
54
+ private accessory: homebridge.PlatformAccessory,
55
+ public device: settings.device & settings.devicesConfig,
56
56
  ) {
57
57
  // default placeholders
58
58
  this.logs(device);
@@ -62,7 +62,8 @@ export class Meter {
62
62
  if (this.CurrentRelativeHumidity === undefined) {
63
63
  this.CurrentRelativeHumidity = 0;
64
64
  } else {
65
- this.CurrentRelativeHumidity = this.accessory.context.CurrentRelativeHumidity;
65
+ this.CurrentRelativeHumidity =
66
+ this.accessory.context.CurrentRelativeHumidity;
66
67
  }
67
68
  if (this.CurrentTemperature === undefined) {
68
69
  this.CurrentTemperature = 0;
@@ -70,12 +71,8 @@ export class Meter {
70
71
  this.CurrentTemperature = this.accessory.context.CurrentTemperature;
71
72
  }
72
73
 
73
- // Meter Config
74
- this.debugLog(`Meter: ${this.accessory.displayName} Config: (ble: ${device.ble}, hide_temperature: ${device.meter?.hide_temperature},`
75
- + ` hide_humidity: ${device.meter?.hide_humidity})`);
76
-
77
74
  // this is subject we use to track when we need to POST changes to the SwitchBot API
78
- this.doMeterUpdate = new Subject();
75
+ this.doMeterUpdate = new rxjs.Subject();
79
76
  this.meterUpdateInProgress = false;
80
77
 
81
78
  // Retrieve initial values and updateHomekit
@@ -84,27 +81,43 @@ export class Meter {
84
81
  // set accessory information
85
82
  accessory
86
83
  .getService(this.platform.Service.AccessoryInformation)!
87
- .setCharacteristic(this.platform.Characteristic.Manufacturer, 'SwitchBot')
88
- .setCharacteristic(this.platform.Characteristic.Model, 'SWITCHBOT-METERTH-S1')
89
- .setCharacteristic(this.platform.Characteristic.SerialNumber, device.deviceId!);
84
+ .setCharacteristic(this.platform.Characteristic.Manufacturer, "SwitchBot")
85
+ .setCharacteristic(
86
+ this.platform.Characteristic.Model,
87
+ "SWITCHBOT-METERTH-S1",
88
+ )
89
+ .setCharacteristic(
90
+ this.platform.Characteristic.SerialNumber,
91
+ device.deviceId!,
92
+ );
90
93
 
91
94
  // Temperature Sensor Service
92
95
  if (device.meter?.hide_temperature) {
93
- this.debugLog(`Meter: ${accessory.displayName} Removing Temperature Sensor Service`);
94
- this.temperatureservice = this.accessory.getService(this.platform.Service.TemperatureSensor);
96
+ this.debugLog(
97
+ `Meter: ${accessory.displayName} Removing Temperature Sensor Service`,
98
+ );
99
+ this.temperatureservice = this.accessory.getService(
100
+ this.platform.Service.TemperatureSensor,
101
+ );
95
102
  accessory.removeService(this.temperatureservice!);
96
103
  } else if (!this.temperatureservice) {
97
- this.debugLog(`Meter: ${accessory.displayName} Add Temperature Sensor Service`);
104
+ this.debugLog(
105
+ `Meter: ${accessory.displayName} Add Temperature Sensor Service`,
106
+ );
98
107
  (this.temperatureservice =
99
108
  this.accessory.getService(this.platform.Service.TemperatureSensor) ||
100
- this.accessory.addService(this.platform.Service.TemperatureSensor)), `${accessory.displayName} Temperature Sensor`;
109
+ this.accessory.addService(this.platform.Service.TemperatureSensor)),
110
+ `${accessory.displayName} Temperature Sensor`;
101
111
 
102
- this.temperatureservice.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Temperature Sensor`);
112
+ this.temperatureservice.setCharacteristic(
113
+ this.platform.Characteristic.Name,
114
+ `${accessory.displayName} Temperature Sensor`,
115
+ );
103
116
 
104
117
  this.temperatureservice
105
118
  .getCharacteristic(this.platform.Characteristic.CurrentTemperature)
106
119
  .setProps({
107
- unit: Units['CELSIUS'],
120
+ unit: homebridge.Units["CELSIUS"],
108
121
  validValueRanges: [-273.15, 100],
109
122
  minValue: -273.15,
110
123
  maxValue: 100,
@@ -114,21 +127,33 @@ export class Meter {
114
127
  return this.CurrentTemperature!;
115
128
  });
116
129
  } else {
117
- this.debugLog(`Meter: ${accessory.displayName} Temperature Sensor Service Not Added`);
130
+ this.debugLog(
131
+ `Meter: ${accessory.displayName} Temperature Sensor Service Not Added`,
132
+ );
118
133
  }
119
134
 
120
135
  // Humidity Sensor Service
121
136
  if (device.meter?.hide_humidity) {
122
- this.debugLog(`Meter: ${accessory.displayName} Removing Humidity Sensor Service`);
123
- this.humidityservice = this.accessory.getService(this.platform.Service.HumiditySensor);
137
+ this.debugLog(
138
+ `Meter: ${accessory.displayName} Removing Humidity Sensor Service`,
139
+ );
140
+ this.humidityservice = this.accessory.getService(
141
+ this.platform.Service.HumiditySensor,
142
+ );
124
143
  accessory.removeService(this.humidityservice!);
125
144
  } else if (!this.humidityservice) {
126
- this.debugLog(`Meter: ${accessory.displayName} Add Humidity Sensor Service`);
145
+ this.debugLog(
146
+ `Meter: ${accessory.displayName} Add Humidity Sensor Service`,
147
+ );
127
148
  (this.humidityservice =
128
149
  this.accessory.getService(this.platform.Service.HumiditySensor) ||
129
- this.accessory.addService(this.platform.Service.HumiditySensor)), `${accessory.displayName} Humidity Sensor`;
150
+ this.accessory.addService(this.platform.Service.HumiditySensor)),
151
+ `${accessory.displayName} Humidity Sensor`;
130
152
 
131
- this.humidityservice.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Humidity Sensor`);
153
+ this.humidityservice.setCharacteristic(
154
+ this.platform.Characteristic.Name,
155
+ `${accessory.displayName} Humidity Sensor`,
156
+ );
132
157
 
133
158
  this.humidityservice
134
159
  .getCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity)
@@ -139,36 +164,47 @@ export class Meter {
139
164
  return this.CurrentRelativeHumidity;
140
165
  });
141
166
  } else {
142
- this.debugLog(`Meter: ${accessory.displayName} Humidity Sensor Service Not Added`);
167
+ this.debugLog(
168
+ `Meter: ${accessory.displayName} Humidity Sensor Service Not Added`,
169
+ );
143
170
  }
144
171
 
145
172
  // Battery Service
146
173
  if (!device.ble) {
147
174
  this.debugLog(`Meter: ${accessory.displayName} Removing Battery Service`);
148
- this.batteryService = this.accessory.getService(this.platform.Service.Battery);
175
+ this.batteryService = this.accessory.getService(
176
+ this.platform.Service.Battery,
177
+ );
149
178
  accessory.removeService(this.batteryService!);
150
179
  } else if (device.ble && !this.batteryService) {
151
180
  this.debugLog(`Meter: ${accessory.displayName} Add Battery Service`);
152
181
  (this.batteryService =
153
182
  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
-
183
+ this.accessory.addService(this.platform.Service.Battery)),
184
+ `${accessory.displayName} Battery`;
185
+
186
+ this.batteryService.setCharacteristic(
187
+ this.platform.Characteristic.Name,
188
+ `${accessory.displayName} Battery`,
189
+ );
190
+
191
+ this.batteryService.setCharacteristic(
192
+ this.platform.Characteristic.ChargingState,
193
+ this.platform.Characteristic.ChargingState.NOT_CHARGEABLE,
194
+ );
160
195
  } else {
161
- this.debugLog(`Meter: ${accessory.displayName} Battery Service Not Added`);
196
+ this.debugLog(
197
+ `Meter: ${accessory.displayName} Battery Service Not Added`,
198
+ );
162
199
  }
163
200
 
164
-
165
-
166
201
  // Retrieve initial values and updateHomekit
167
202
  this.updateHomeKitCharacteristics();
168
203
 
169
204
  // Start an update interval
170
- interval(this.deviceRefreshRate * 1000)
171
- .pipe(skipWhile(() => this.meterUpdateInProgress))
205
+ rxjs
206
+ .interval(this.deviceRefreshRate * 1000)
207
+ .pipe(operators.skipWhile(() => this.meterUpdateInProgress))
172
208
  .subscribe(() => {
173
209
  this.refreshStatus();
174
210
  });
@@ -191,23 +227,35 @@ export class Meter {
191
227
  // Battery
192
228
  this.BatteryLevel = Number(this.battery);
193
229
  if (this.BatteryLevel < 15) {
194
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
230
+ this.StatusLowBattery =
231
+ this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
195
232
  } else {
196
- this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
233
+ this.StatusLowBattery =
234
+ this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
197
235
  }
198
- this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
236
+ this.debugLog(
237
+ `${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`,
238
+ );
199
239
 
200
240
  // Humidity
201
241
  if (!this.device.meter?.hide_humidity) {
202
242
  this.CurrentRelativeHumidity = this.humidity!;
203
- this.debugLog(`Meter: ${this.accessory.displayName} Humidity: ${this.CurrentRelativeHumidity}%`);
243
+ this.debugLog(
244
+ `Meter: ${this.accessory.displayName} Humidity: ${this.CurrentRelativeHumidity}%`,
245
+ );
204
246
  }
205
247
 
206
248
  // Current Temperature
207
249
  if (!this.device.meter?.hide_temperature) {
208
- this.temperature < 0 ? 0 : this.temperature > 100 ? 100 : this.temperature;
250
+ this.temperature < 0
251
+ ? 0
252
+ : this.temperature > 100
253
+ ? 100
254
+ : this.temperature;
209
255
  this.CurrentTemperature = this.temperature;
210
- this.debugLog(`Meter: ${this.accessory.displayName} Temperature: ${this.CurrentTemperature}°c`);
256
+ this.debugLog(
257
+ `Meter: ${this.accessory.displayName} Temperature: ${this.CurrentTemperature}°c`,
258
+ );
211
259
  }
212
260
  }
213
261
 
@@ -220,13 +268,17 @@ export class Meter {
220
268
  // Current Relative Humidity
221
269
  if (!this.device.meter?.hide_humidity) {
222
270
  this.CurrentRelativeHumidity = this.Humidity!;
223
- this.debugLog(`Meter: ${this.accessory.displayName} Humidity: ${this.CurrentRelativeHumidity}%`);
271
+ this.debugLog(
272
+ `Meter: ${this.accessory.displayName} Humidity: ${this.CurrentRelativeHumidity}%`,
273
+ );
224
274
  }
225
275
 
226
276
  // Current Temperature
227
277
  if (!this.device.meter?.hide_temperature) {
228
278
  this.CurrentTemperature = this.Temperature!;
229
- this.debugLog(`Meter: ${this.accessory.displayName} Temperature: ${this.CurrentTemperature}°c`);
279
+ this.debugLog(
280
+ `Meter: ${this.accessory.displayName} Temperature: ${this.CurrentTemperature}°c`,
281
+ );
230
282
  }
231
283
  }
232
284
  }
@@ -246,94 +298,123 @@ export class Meter {
246
298
  this.debugLog(`Meter: ${this.accessory.displayName} BLE RefreshStatus`);
247
299
  const switchbot = await this.platform.connectBLE();
248
300
  // Convert to BLE Address
249
- this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
250
- this.debugLog(`Curtain: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
251
- // Start to monitor advertisement packets
252
- if (switchbot !== false) {
253
- switchbot.startScan({
254
- model: 'T',
301
+ this.device.bleMac = this.device
302
+ .deviceId!.match(/.{1,2}/g)!
303
+ .join(":")
304
+ .toLowerCase();
305
+ this.debugLog(
306
+ `Meter: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`,
307
+ );
308
+ try {
309
+ await switchbot.discover({
310
+ model: "T",
311
+ quick: false,
312
+ });
313
+ // Start to monitor advertisement packets
314
+ await switchbot.startScan({
315
+ model: "T",
255
316
  id: this.device.bleMac,
256
- }).then(() => {
257
- // Set an event hander
258
- switchbot.onadvertisement = (ad: ad) => {
259
- this.serviceData = ad.serviceData;
260
- this.temperature = ad.serviceData.temperature!.c;
261
- this.humidity = ad.serviceData.humidity;
262
- this.battery = ad.serviceData.battery;
263
- this.debugLog(`Meter: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
264
- this.debugLog(`Meter: ${this.accessory.displayName} model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName}, `
265
- + `temperature: ${JSON.stringify(ad.serviceData.temperature?.c)}, humidity: ${ad.serviceData.humidity}, `
266
- + `battery: ${ad.serviceData.battery}`);
267
-
268
- if (this.serviceData) {
269
- this.connected = true;
270
- this.debugLog(`Meter: ${this.accessory.displayName} connected: ${this.connected}`);
271
- } else {
272
- this.connected = false;
273
- this.debugLog(`Meter: ${this.accessory.displayName} connected: ${this.connected}`);
274
- }
275
- };
276
- // Wait 10 seconds
277
- return switchbot.wait(this.scanDuration * 1000);
278
- }).then(async () => {
279
- // Stop to monitor
280
- switchbot.stopScan();
281
- if (this.connected) {
282
- this.parseStatus();
283
- this.updateHomeKitCharacteristics();
317
+ });
318
+ // Set an event hander
319
+ switchbot.onadvertisement = (ad: settings.ad) => {
320
+ this.serviceData = ad.serviceData;
321
+ this.temperature = ad.serviceData.temperature!.c;
322
+ this.humidity = ad.serviceData.humidity;
323
+ this.battery = ad.serviceData.battery;
324
+ this.debugLog(
325
+ `Meter: ${this.accessory.displayName} serviceData: ${JSON.stringify(
326
+ ad.serviceData,
327
+ )}`,
328
+ );
329
+ this.debugLog(
330
+ `Meter: ${this.accessory.displayName} model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName}, ` +
331
+ `temperature: ${JSON.stringify(
332
+ ad.serviceData.temperature?.c,
333
+ )}, humidity: ${ad.serviceData.humidity}, ` +
334
+ `battery: ${ad.serviceData.battery}`,
335
+ );
336
+
337
+ if (this.serviceData) {
338
+ this.connected = true;
339
+ this.debugLog(
340
+ `Meter: ${this.accessory.displayName} connected: ${this.connected}`,
341
+ );
284
342
  } else {
285
- await this.BLEconnection(switchbot);
286
- }
287
- }).catch(async (e: any) => {
288
- this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
289
- if (this.deviceLogging === 'debug') {
290
- this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
291
- + ` Error Message: ${JSON.stringify(e.message)}`);
292
- }
293
- if (this.platform.debugMode) {
294
- this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
295
- + ` Error: ${JSON.stringify(e)}`);
343
+ this.connected = false;
344
+ this.debugLog(
345
+ `Meter: ${this.accessory.displayName} connected: ${this.connected}`,
346
+ );
296
347
  }
348
+ };
349
+ // Wait 10 seconds
350
+ await switchbot.wait(this.scanDuration * 1000);
351
+ // Stop to monitor
352
+ switchbot.stopScan();
353
+ if (this.connected) {
354
+ this.parseStatus();
355
+ this.updateHomeKitCharacteristics();
356
+ } else {
357
+ this.errorLog(
358
+ `Meter: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`,
359
+ );
297
360
  if (this.platform.config.credentials?.openToken) {
298
- this.warnLog(`Meter: ${this.accessory.displayName} Using OpenAPI Connection`);
361
+ this.warnLog(
362
+ `Meter: ${this.accessory.displayName} Using OpenAPI Connection`,
363
+ );
299
364
  this.SwitchToOpenAPI = true;
300
- this.openAPIRefreshStatus();
365
+ await this.openAPIRefreshStatus();
301
366
  }
302
- this.apiError(e);
303
- });
304
- } else {
305
- await this.BLEconnection(switchbot);
306
- }
307
- }
308
-
309
- private async BLEconnection(switchbot: any) {
310
- this.errorLog(`Meter: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
311
- if (this.platform.config.credentials?.openToken) {
312
- this.warnLog(`Meter: ${this.accessory.displayName} Using OpenAPI Connection`);
313
- this.SwitchToOpenAPI = true;
314
- await this.openAPIRefreshStatus();
367
+ }
368
+ } catch (e: any) {
369
+ this.errorLog(
370
+ `Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection`,
371
+ );
372
+ if (this.deviceLogging.includes("debug")) {
373
+ this.errorLog(
374
+ `Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection,` +
375
+ ` Error Message: ${JSON.stringify(e.message)}`,
376
+ );
377
+ }
378
+ if (this.platform.config.credentials?.openToken) {
379
+ this.warnLog(
380
+ `Meter: ${this.accessory.displayName} Using OpenAPI Connection`,
381
+ );
382
+ this.SwitchToOpenAPI = true;
383
+ this.openAPIRefreshStatus();
384
+ }
385
+ this.apiError(e);
315
386
  }
316
387
  }
317
388
 
318
389
  private async openAPIRefreshStatus() {
319
390
  if (this.platform.config.credentials?.openToken) {
320
- this.debugLog(`Meter: ${this.accessory.displayName} OpenAPI RefreshStatus`);
391
+ this.debugLog(
392
+ `Meter: ${this.accessory.displayName} OpenAPI RefreshStatus`,
393
+ );
321
394
  try {
322
- this.deviceStatus = (await this.platform.axios.get(`${DeviceURL}/${this.device.deviceId}/status`)).data;
323
- this.debugLog(`Meter: ${this.accessory.displayName} openAPIRefreshStatus: ${JSON.stringify(this.deviceStatus)}`);
395
+ this.deviceStatus = (
396
+ await this.platform.axios.get(
397
+ `${settings.DeviceURL}/${this.device.deviceId}/status`,
398
+ )
399
+ ).data;
400
+ this.debugLog(
401
+ `Meter: ${
402
+ this.accessory.displayName
403
+ } openAPIRefreshStatus: ${JSON.stringify(this.deviceStatus)}`,
404
+ );
324
405
  this.Humidity = this.deviceStatus.body.humidity!;
325
406
  this.Temperature = this.deviceStatus.body.temperature!;
326
407
  this.parseStatus();
327
408
  this.updateHomeKitCharacteristics();
328
409
  } catch (e: any) {
329
- this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
330
- if (this.deviceLogging === 'debug') {
331
- this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
332
- + ` Error Message: ${JSON.stringify(e.message)}`);
333
- }
334
- if (this.platform.debugMode) {
335
- this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
336
- + ` Error: ${JSON.stringify(e)}`);
410
+ this.errorLog(
411
+ `Meter: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`,
412
+ );
413
+ if (this.deviceLogging.includes("debug")) {
414
+ this.errorLog(
415
+ `Meter: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,` +
416
+ ` Error Message: ${JSON.stringify(e.message)}`,
417
+ );
337
418
  }
338
419
  this.apiError(e);
339
420
  }
@@ -346,115 +427,177 @@ export class Meter {
346
427
  updateHomeKitCharacteristics() {
347
428
  if (!this.device.meter?.hide_humidity) {
348
429
  if (this.CurrentRelativeHumidity === undefined) {
349
- this.debugLog(`Meter: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
430
+ this.debugLog(
431
+ `Meter: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`,
432
+ );
350
433
  } else {
351
- this.humidityservice?.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.CurrentRelativeHumidity);
352
- this.debugLog(`Meter: ${this.accessory.displayName} updateCharacteristic CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
434
+ this.humidityservice?.updateCharacteristic(
435
+ this.platform.Characteristic.CurrentRelativeHumidity,
436
+ this.CurrentRelativeHumidity,
437
+ );
438
+ this.debugLog(
439
+ `Meter: ${this.accessory.displayName} updateCharacteristic CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`,
440
+ );
353
441
  }
354
442
  }
355
443
  if (!this.device.meter?.hide_temperature) {
356
444
  if (this.CurrentTemperature === undefined) {
357
- this.debugLog(`Meter: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
445
+ this.debugLog(
446
+ `Meter: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`,
447
+ );
358
448
  } else {
359
- this.temperatureservice?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.CurrentTemperature);
360
- this.debugLog(`Meter: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`);
449
+ this.temperatureservice?.updateCharacteristic(
450
+ this.platform.Characteristic.CurrentTemperature,
451
+ this.CurrentTemperature,
452
+ );
453
+ this.debugLog(
454
+ `Meter: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`,
455
+ );
361
456
  }
362
457
  }
363
458
  if (this.device.ble) {
364
459
  if (this.BatteryLevel === undefined) {
365
- this.debugLog(`Meter: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
460
+ this.debugLog(
461
+ `Meter: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`,
462
+ );
366
463
  } else {
367
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
368
- this.debugLog(`Meter: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
464
+ this.batteryService?.updateCharacteristic(
465
+ this.platform.Characteristic.BatteryLevel,
466
+ this.BatteryLevel,
467
+ );
468
+ this.debugLog(
469
+ `Meter: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`,
470
+ );
369
471
  }
370
472
  if (this.StatusLowBattery === undefined) {
371
- this.debugLog(`Meter: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
473
+ this.debugLog(
474
+ `Meter: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`,
475
+ );
372
476
  } else {
373
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
374
- this.debugLog(`Meter: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
477
+ this.batteryService?.updateCharacteristic(
478
+ this.platform.Characteristic.StatusLowBattery,
479
+ this.StatusLowBattery,
480
+ );
481
+ this.debugLog(
482
+ `Meter: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`,
483
+ );
375
484
  }
376
485
  }
377
486
  }
378
487
 
379
488
  public apiError(e: any) {
380
489
  if (!this.device.meter?.hide_humidity) {
381
- this.humidityservice?.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, e);
490
+ this.humidityservice?.updateCharacteristic(
491
+ this.platform.Characteristic.CurrentRelativeHumidity,
492
+ e,
493
+ );
382
494
  }
383
495
  if (!this.device.meter?.hide_temperature) {
384
- this.temperatureservice?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, e);
496
+ this.temperatureservice?.updateCharacteristic(
497
+ this.platform.Characteristic.CurrentTemperature,
498
+ e,
499
+ );
385
500
  }
386
501
  if (this.device.ble) {
387
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
388
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
502
+ this.batteryService?.updateCharacteristic(
503
+ this.platform.Characteristic.BatteryLevel,
504
+ e,
505
+ );
506
+ this.batteryService?.updateCharacteristic(
507
+ this.platform.Characteristic.StatusLowBattery,
508
+ e,
509
+ );
389
510
  }
390
511
  //throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
391
512
  }
392
513
 
393
- config(device: device & devicesConfig) {
514
+ config(device: settings.device & settings.devicesConfig) {
394
515
  let config = {};
395
516
  if (device.meter) {
396
517
  config = device.meter;
397
518
  }
398
519
  if (device.ble !== undefined) {
399
- config['ble'] = device.ble;
520
+ config["ble"] = device.ble;
400
521
  }
401
522
  if (device.logging !== undefined) {
402
- config['logging'] = device.logging;
523
+ config["logging"] = device.logging;
403
524
  }
404
525
  if (device.refreshRate !== undefined) {
405
- config['refreshRate'] = device.refreshRate;
526
+ config["refreshRate"] = device.refreshRate;
406
527
  }
407
528
  if (device.scanDuration !== undefined) {
408
- config['scanDuration'] = device.scanDuration;
529
+ config["scanDuration"] = device.scanDuration;
409
530
  }
410
531
  if (Object.entries(config).length !== 0) {
411
- this.warnLog(`Meter: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
532
+ this.warnLog(
533
+ `Meter: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`,
534
+ );
412
535
  }
413
536
  }
414
537
 
415
- refreshRate(device: device & devicesConfig) {
538
+ refreshRate(device: settings.device & settings.devicesConfig) {
416
539
  if (device.refreshRate) {
417
- this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
418
- this.debugLog(`Meter: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
540
+ this.deviceRefreshRate = this.accessory.context.refreshRate =
541
+ device.refreshRate;
542
+ this.debugLog(
543
+ `Meter: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`,
544
+ );
419
545
  } else if (this.platform.config.options!.refreshRate) {
420
- this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options!.refreshRate;
421
- this.debugLog(`Meter: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
546
+ this.deviceRefreshRate = this.accessory.context.refreshRate =
547
+ this.platform.config.options!.refreshRate;
548
+ this.debugLog(
549
+ `Meter: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`,
550
+ );
422
551
  }
423
552
  }
424
553
 
425
- scan(device: device & devicesConfig) {
554
+ scan(device: settings.device & settings.devicesConfig) {
426
555
  if (device.scanDuration) {
427
- this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
556
+ this.scanDuration = this.accessory.context.scanDuration =
557
+ device.scanDuration;
428
558
  if (device.ble) {
429
- this.debugLog(`Meter: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
559
+ this.debugLog(
560
+ `Meter: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`,
561
+ );
430
562
  }
431
563
  } else {
432
564
  this.scanDuration = this.accessory.context.scanDuration = 1;
433
565
  if (this.device.ble) {
434
- this.debugLog(`Meter: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
566
+ this.debugLog(
567
+ `Meter: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`,
568
+ );
435
569
  }
436
570
  }
437
571
  }
438
572
 
439
- logs(device: device & devicesConfig) {
573
+ logs(device: settings.device & settings.devicesConfig) {
440
574
  if (this.platform.debugMode) {
441
- this.deviceLogging = this.accessory.context.logging = 'debugMode';
442
- this.debugLog(`Meter: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
575
+ this.deviceLogging = this.accessory.context.logging = "debugMode";
576
+ this.debugLog(
577
+ `Meter: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`,
578
+ );
443
579
  } else if (device.logging) {
444
580
  this.deviceLogging = this.accessory.context.logging = device.logging;
445
- this.debugLog(`Meter: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
581
+ this.debugLog(
582
+ `Meter: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`,
583
+ );
446
584
  } else if (this.platform.config.options?.logging) {
447
- this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
448
- this.debugLog(`Meter: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
585
+ this.deviceLogging = this.accessory.context.logging =
586
+ this.platform.config.options?.logging;
587
+ this.debugLog(
588
+ `Meter: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`,
589
+ );
449
590
  } else {
450
- this.deviceLogging = this.accessory.context.logging = 'standard';
451
- this.debugLog(`Meter: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
591
+ this.deviceLogging = this.accessory.context.logging = "standard";
592
+ this.debugLog(
593
+ `Meter: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`,
594
+ );
452
595
  }
453
596
  }
454
597
 
455
598
  /**
456
- * Logging for Device
457
- */
599
+ * Logging for Device
600
+ */
458
601
  infoLog(...log: any[]) {
459
602
  if (this.enablingDeviceLogging()) {
460
603
  this.platform.log.info(String(...log));
@@ -475,8 +618,8 @@ export class Meter {
475
618
 
476
619
  debugLog(...log: any[]) {
477
620
  if (this.enablingDeviceLogging()) {
478
- if (this.deviceLogging === 'debug') {
479
- this.platform.log.info('[DEBUG]', String(...log));
621
+ if (this.deviceLogging === "debug") {
622
+ this.platform.log.info("[DEBUG]", String(...log));
480
623
  } else {
481
624
  this.platform.log.debug(String(...log));
482
625
  }
@@ -484,6 +627,8 @@ export class Meter {
484
627
  }
485
628
 
486
629
  enablingDeviceLogging(): boolean {
487
- return this.deviceLogging === 'debug' || this.deviceLogging === 'standard';
630
+ return (
631
+ this.deviceLogging.includes("debug") || this.deviceLogging === "standard"
632
+ );
488
633
  }
489
634
  }