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