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