@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,39 +1,40 @@
1
- import * as axios from "axios";
2
- import * as rxjs from "rxjs";
3
- import * as platform from "../platform";
4
- import * as operators from "rxjs/operators";
5
- import * as homebridge from "homebridge";
6
- import * as settings from "../settings";
1
+ import { AxiosResponse } from 'axios';
2
+ import { interval, Subject } from 'rxjs';
3
+ import { SwitchBotPlatform } from '../platform';
4
+ import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
5
+ import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
6
+ import { DeviceURL, device, devicesConfig, serviceData, switchbot, deviceStatusResponse, payload, deviceStatus } from '../settings';
7
7
 
8
8
  export class Curtain {
9
9
  // Services
10
- private service: homebridge.Service;
11
- private lightSensorService?: homebridge.Service;
12
- private batteryService?: homebridge.Service;
10
+ private service: Service;
11
+ private lightSensorService?: Service;
12
+ private batteryService?: Service;
13
13
 
14
14
  // Characteristic Values
15
- CurrentPosition!: homebridge.CharacteristicValue;
16
- PositionState!: homebridge.CharacteristicValue;
17
- TargetPosition!: homebridge.CharacteristicValue;
18
- CurrentAmbientLightLevel?: homebridge.CharacteristicValue;
19
- BatteryLevel?: homebridge.CharacteristicValue;
20
- StatusLowBattery?: homebridge.CharacteristicValue;
15
+ CurrentPosition!: CharacteristicValue;
16
+ PositionState!: CharacteristicValue;
17
+ TargetPosition!: CharacteristicValue;
18
+ CurrentAmbientLightLevel?: CharacteristicValue;
19
+ BatteryLevel?: CharacteristicValue;
20
+ StatusLowBattery?: CharacteristicValue;
21
21
 
22
22
  // OpenAPI Others
23
- deviceStatus!: settings.deviceStatusResponse;
24
- slidePosition: settings.deviceStatus["slidePosition"];
25
- moving: settings.deviceStatus["moving"];
26
- brightness: settings.deviceStatus["brightness"];
23
+ deviceStatus!: deviceStatusResponse;
24
+ slidePosition: deviceStatus['slidePosition'];
25
+ moving: deviceStatus['moving'];
26
+ brightness: deviceStatus['brightness'];
27
27
 
28
28
  // BLE Others
29
29
  connected?: boolean;
30
+ switchbot!: switchbot;
30
31
  SwitchToOpenAPI?: boolean;
31
- serviceData!: settings.serviceData;
32
+ serviceData!: serviceData;
32
33
  spaceBetweenLevels!: number;
33
- calibration: settings.serviceData["calibration"];
34
- battery: settings.serviceData["battery"];
35
- position: settings.serviceData["position"];
36
- lightLevel: settings.serviceData["lightLevel"];
34
+ calibration: serviceData['calibration'];
35
+ battery: serviceData['battery'];
36
+ position: serviceData['position'];
37
+ lightLevel: serviceData['lightLevel'];
37
38
 
38
39
  // Target
39
40
  setNewTarget!: boolean;
@@ -50,13 +51,9 @@ export class Curtain {
50
51
 
51
52
  // Updates
52
53
  curtainUpdateInProgress!: boolean;
53
- doCurtainUpdate!: rxjs.Subject<void>;
54
+ doCurtainUpdate!: Subject<void>;
54
55
 
55
- constructor(
56
- private readonly platform: platform.SwitchBotPlatform,
57
- private accessory: homebridge.PlatformAccessory,
58
- public device: settings.device & settings.devicesConfig,
59
- ) {
56
+ constructor(private readonly platform: SwitchBotPlatform, private accessory: PlatformAccessory, public device: device & devicesConfig) {
60
57
  // default placeholders
61
58
  this.logs(device);
62
59
  this.scan(device);
@@ -68,7 +65,7 @@ export class Curtain {
68
65
  this.PositionState = this.platform.Characteristic.PositionState.STOPPED;
69
66
 
70
67
  // this is subject we use to track when we need to POST changes to the SwitchBot API
71
- this.doCurtainUpdate = new rxjs.Subject();
68
+ this.doCurtainUpdate = new Subject();
72
69
  this.curtainUpdateInProgress = false;
73
70
  this.setNewTarget = false;
74
71
 
@@ -78,21 +75,13 @@ export class Curtain {
78
75
  // set accessory information
79
76
  accessory
80
77
  .getService(this.platform.Service.AccessoryInformation)!
81
- .setCharacteristic(this.platform.Characteristic.Manufacturer, "SwitchBot")
82
- .setCharacteristic(
83
- this.platform.Characteristic.Model,
84
- "SWITCHBOT-CURTAIN-W0701600",
85
- )
86
- .setCharacteristic(
87
- this.platform.Characteristic.SerialNumber,
88
- device.deviceId,
89
- );
78
+ .setCharacteristic(this.platform.Characteristic.Manufacturer, 'SwitchBot')
79
+ .setCharacteristic(this.platform.Characteristic.Model, 'SWITCHBOT-CURTAIN-W0701600')
80
+ .setCharacteristic(this.platform.Characteristic.SerialNumber, device.deviceId);
90
81
 
91
82
  // get the WindowCovering service if it exists, otherwise create a new WindowCovering service
92
83
  // you can create multiple services for each accessory
93
- (this.service =
94
- accessory.getService(this.platform.Service.WindowCovering) ||
95
- accessory.addService(this.platform.Service.WindowCovering)),
84
+ (this.service = accessory.getService(this.platform.Service.WindowCovering) || accessory.addService(this.platform.Service.WindowCovering)),
96
85
  `${device.deviceName} ${device.deviceType}`;
97
86
 
98
87
  // To avoid "Cannot add a Service with the same UUID another Service without also defining a unique 'subtype' property." error,
@@ -101,19 +90,13 @@ export class Curtain {
101
90
 
102
91
  // set the service name, this is what is displayed as the default name on the Home app
103
92
  // in this example we are using the name we stored in the `accessory.context` in the `discoverDevices` method.
104
- this.service.setCharacteristic(
105
- this.platform.Characteristic.Name,
106
- accessory.displayName,
107
- );
93
+ this.service.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
108
94
 
109
95
  // each service must implement at-minimum the "required characteristics" for the given service type
110
96
  // see https://developers.homebridge.io/#/service/WindowCovering
111
97
 
112
98
  // create handlers for required characteristics
113
- this.service.setCharacteristic(
114
- this.platform.Characteristic.PositionState,
115
- this.PositionState,
116
- );
99
+ this.service.setCharacteristic(this.platform.Characteristic.PositionState, this.PositionState);
117
100
 
118
101
  this.service
119
102
  .getCharacteristic(this.platform.Characteristic.CurrentPosition)
@@ -137,83 +120,53 @@ export class Curtain {
137
120
 
138
121
  // Light Sensor Service
139
122
  if (device.curtain?.hide_lightsensor) {
140
- this.debugLog(
141
- `Curtain: ${accessory.displayName} Removing Light Sensor Service`,
142
- );
143
- this.lightSensorService = this.accessory.getService(
144
- this.platform.Service.LightSensor,
145
- );
123
+ this.debugLog(`Curtain: ${accessory.displayName} Removing Light Sensor Service`);
124
+ this.lightSensorService = this.accessory.getService(this.platform.Service.LightSensor);
146
125
  accessory.removeService(this.lightSensorService!);
147
126
  } else if (!this.lightSensorService) {
148
- this.debugLog(
149
- `Curtain: ${accessory.displayName} Add Light Sensor Service`,
150
- );
127
+ this.debugLog(`Curtain: ${accessory.displayName} Add Light Sensor Service`);
151
128
  (this.lightSensorService =
152
- this.accessory.getService(this.platform.Service.LightSensor) ||
153
- this.accessory.addService(this.platform.Service.LightSensor)),
129
+ this.accessory.getService(this.platform.Service.LightSensor) || this.accessory.addService(this.platform.Service.LightSensor)),
154
130
  `${accessory.displayName} Light Sensor`;
155
131
 
156
- this.lightSensorService.setCharacteristic(
157
- this.platform.Characteristic.Name,
158
- `${accessory.displayName} Light Sensor`,
159
- );
132
+ this.lightSensorService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Light Sensor`);
160
133
  } else {
161
- this.debugLog(
162
- `Curtain: ${accessory.displayName} Light Sensor Service Not Added`,
163
- );
134
+ this.debugLog(`Curtain: ${accessory.displayName} Light Sensor Service Not Added`);
164
135
  }
165
136
 
166
137
  // Battery Service
167
138
  if (!device.ble) {
168
- this.debugLog(
169
- `Curtain: ${accessory.displayName} Removing Battery Service`,
170
- );
171
- this.batteryService = this.accessory.getService(
172
- this.platform.Service.Battery,
173
- );
139
+ this.debugLog(`Curtain: ${accessory.displayName} Removing Battery Service`);
140
+ this.batteryService = this.accessory.getService(this.platform.Service.Battery);
174
141
  accessory.removeService(this.batteryService!);
175
142
  } else if (device.ble && !this.batteryService) {
176
143
  this.debugLog(`Curtain: ${accessory.displayName} Add Battery Service`);
177
- (this.batteryService =
178
- this.accessory.getService(this.platform.Service.Battery) ||
179
- this.accessory.addService(this.platform.Service.Battery)),
144
+ (this.batteryService = this.accessory.getService(this.platform.Service.Battery) || this.accessory.addService(this.platform.Service.Battery)),
180
145
  `${accessory.displayName} Battery`;
181
146
 
182
- this.batteryService.setCharacteristic(
183
- this.platform.Characteristic.Name,
184
- `${accessory.displayName} Battery`,
185
- );
147
+ this.batteryService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Battery`);
186
148
  } else {
187
- this.debugLog(
188
- `Curtain: ${accessory.displayName} Battery Service Not Added`,
189
- );
149
+ this.debugLog(`Curtain: ${accessory.displayName} Battery Service Not Added`);
190
150
  }
191
151
 
192
152
  // Update Homekit
193
153
  this.updateHomeKitCharacteristics();
194
154
 
195
155
  // Start an update interval
196
- rxjs
197
- .interval(this.deviceRefreshRate * 1000)
198
- .pipe(operators.skipWhile(() => this.curtainUpdateInProgress))
156
+ interval(this.deviceRefreshRate * 1000)
157
+ .pipe(skipWhile(() => this.curtainUpdateInProgress))
199
158
  .subscribe(() => {
200
159
  this.refreshStatus();
201
160
  });
202
161
 
203
162
  // update slide progress
204
- rxjs
205
- .interval(this.updateRate * 1000)
206
- .pipe(operators.skipWhile(() => this.curtainUpdateInProgress))
163
+ interval(this.updateRate * 1000)
164
+ .pipe(skipWhile(() => this.curtainUpdateInProgress))
207
165
  .subscribe(() => {
208
- if (
209
- this.PositionState ===
210
- this.platform.Characteristic.PositionState.STOPPED
211
- ) {
166
+ if (this.PositionState === this.platform.Characteristic.PositionState.STOPPED) {
212
167
  return;
213
168
  }
214
- this.debugLog(
215
- `Curtain: ${accessory.displayName} Refresh Status When Moving, PositionState: ${this.PositionState}`,
216
- );
169
+ this.debugLog(`Curtain: ${accessory.displayName} Refresh Status When Moving, PositionState: ${this.PositionState}`);
217
170
  this.refreshStatus();
218
171
  });
219
172
 
@@ -221,23 +174,21 @@ export class Curtain {
221
174
  // We put in a debounce of 100ms so we don't make duplicate calls
222
175
  this.doCurtainUpdate
223
176
  .pipe(
224
- operators.tap(() => {
177
+ tap(() => {
225
178
  this.curtainUpdateInProgress = true;
226
179
  }),
227
- operators.debounceTime(this.platform.config.options!.pushRate! * 1000),
180
+ debounceTime(this.platform.config.options!.pushRate! * 1000),
228
181
  )
229
182
  .subscribe(async () => {
230
183
  try {
231
184
  await this.pushChanges();
232
185
  } catch (e: any) {
233
- this.errorLog(
234
- `Curtain: ${this.accessory.displayName} failed pushChanges`,
235
- );
236
- if (this.deviceLogging.includes("debug")) {
237
- this.errorLog(
238
- `Curtain: ${this.accessory.displayName} failed pushChanges,` +
239
- ` Error Message: ${JSON.stringify(e.message)}`,
240
- );
186
+ this.errorLog(`Curtain: ${this.accessory.displayName} failed pushChanges`);
187
+ if (this.deviceLogging === 'debug') {
188
+ this.errorLog(`Curtain: ${this.accessory.displayName} failed pushChanges,` + ` Error Message: ${JSON.stringify(e.message)}`);
189
+ }
190
+ if (this.platform.debugMode) {
191
+ this.errorLog(`Curtain: ${this.accessory.displayName} failed pushChanges,` + ` Error: ${JSON.stringify(e)}`);
241
192
  }
242
193
  this.apiError(e);
243
194
  }
@@ -261,39 +212,25 @@ export class Curtain {
261
212
  // CurrentPosition
262
213
  this.setMinMax();
263
214
  this.CurrentPosition = 100 - Number(this.position);
264
- this.debugLog(
265
- `Curtain: ${this.accessory.displayName} CurrentPosition ${this.CurrentPosition}`,
266
- );
215
+ this.debugLog(`Curtain: ${this.accessory.displayName} CurrentPosition ${this.CurrentPosition}`);
267
216
  if (this.setNewTarget) {
268
- this.infoLog(
269
- `Curtain: ${this.accessory.displayName} Checking Status ...`,
270
- );
217
+ this.infoLog(`Curtain: ${this.accessory.displayName} Checking Status ...`);
271
218
  }
272
219
 
273
220
  if (this.setNewTarget) {
274
221
  this.setMinMax();
275
222
  if (this.TargetPosition > this.CurrentPosition) {
276
- this.debugLog(
277
- `Curtain: ${this.accessory.displayName} Closing, CurrentPosition: ${this.CurrentPosition}`,
278
- );
279
- this.PositionState =
280
- this.platform.Characteristic.PositionState.INCREASING;
223
+ this.debugLog(`Curtain: ${this.accessory.displayName} Closing, CurrentPosition: ${this.CurrentPosition}`);
224
+ this.PositionState = this.platform.Characteristic.PositionState.INCREASING;
281
225
  } else if (this.TargetPosition < this.CurrentPosition) {
282
- this.debugLog(
283
- `Curtain: ${this.accessory.displayName} Opening, CurrentPosition: ${this.CurrentPosition}`,
284
- );
285
- this.PositionState =
286
- this.platform.Characteristic.PositionState.DECREASING;
226
+ this.debugLog(`Curtain: ${this.accessory.displayName} Opening, CurrentPosition: ${this.CurrentPosition}`);
227
+ this.PositionState = this.platform.Characteristic.PositionState.DECREASING;
287
228
  } else {
288
- this.debugLog(
289
- `Curtain: ${this.CurrentPosition} Standby, CurrentPosition: ${this.CurrentPosition}`,
290
- );
229
+ this.debugLog(`Curtain: ${this.CurrentPosition} Standby, CurrentPosition: ${this.CurrentPosition}`);
291
230
  this.PositionState = this.platform.Characteristic.PositionState.STOPPED;
292
231
  }
293
232
  } else {
294
- this.debugLog(
295
- `Curtain: ${this.accessory.displayName} Standby, CurrentPosition: ${this.CurrentPosition}`,
296
- );
233
+ this.debugLog(`Curtain: ${this.accessory.displayName} Standby, CurrentPosition: ${this.CurrentPosition}`);
297
234
  if (!this.setNewTarget) {
298
235
  // If Curtain calibration distance is short, there will be an error between the current percentage and the target percentage.
299
236
  this.TargetPosition = this.CurrentPosition;
@@ -315,68 +252,42 @@ export class Curtain {
315
252
  switch (this.lightLevel) {
316
253
  case 1:
317
254
  this.CurrentAmbientLightLevel = this.set_minLux;
318
- this.debugLog(
319
- `Curtain: ${this.accessory.displayName} LightLevel: ${this.lightLevel}`,
320
- );
255
+ this.debugLog(`Curtain: ${this.accessory.displayName} LightLevel: ${this.lightLevel}`);
321
256
  break;
322
257
  case 2:
323
- this.CurrentAmbientLightLevel =
324
- (this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels;
258
+ this.CurrentAmbientLightLevel = (this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels;
325
259
  this.debugLog(
326
260
  `Curtain: ${this.accessory.displayName} LightLevel: ${this.lightLevel},` +
327
- ` Calculation: ${
328
- (this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels
329
- }`,
261
+ ` Calculation: ${(this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels}`,
330
262
  );
331
263
  break;
332
264
  case 3:
333
- this.CurrentAmbientLightLevel =
334
- ((this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels) * 2;
335
- this.debugLog(
336
- `Curtain: ${this.accessory.displayName} LightLevel: ${this.lightLevel}`,
337
- );
265
+ this.CurrentAmbientLightLevel = ((this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels) * 2;
266
+ this.debugLog(`Curtain: ${this.accessory.displayName} LightLevel: ${this.lightLevel}`);
338
267
  break;
339
268
  case 4:
340
- this.CurrentAmbientLightLevel =
341
- ((this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels) * 3;
342
- this.debugLog(
343
- `Curtain: ${this.accessory.displayName} LightLevel: ${this.lightLevel}`,
344
- );
269
+ this.CurrentAmbientLightLevel = ((this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels) * 3;
270
+ this.debugLog(`Curtain: ${this.accessory.displayName} LightLevel: ${this.lightLevel}`);
345
271
  break;
346
272
  case 5:
347
- this.CurrentAmbientLightLevel =
348
- ((this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels) * 4;
349
- this.debugLog(
350
- `Curtain: ${this.accessory.displayName} LightLevel: ${this.lightLevel}`,
351
- );
273
+ this.CurrentAmbientLightLevel = ((this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels) * 4;
274
+ this.debugLog(`Curtain: ${this.accessory.displayName} LightLevel: ${this.lightLevel}`);
352
275
  break;
353
276
  case 6:
354
- this.CurrentAmbientLightLevel =
355
- ((this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels) * 5;
356
- this.debugLog(
357
- `Curtain: ${this.accessory.displayName} LightLevel: ${this.lightLevel}`,
358
- );
277
+ this.CurrentAmbientLightLevel = ((this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels) * 5;
278
+ this.debugLog(`Curtain: ${this.accessory.displayName} LightLevel: ${this.lightLevel}`);
359
279
  break;
360
280
  case 7:
361
- this.CurrentAmbientLightLevel =
362
- ((this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels) * 6;
363
- this.debugLog(
364
- `Curtain: ${this.accessory.displayName} LightLevel: ${this.lightLevel}`,
365
- );
281
+ this.CurrentAmbientLightLevel = ((this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels) * 6;
282
+ this.debugLog(`Curtain: ${this.accessory.displayName} LightLevel: ${this.lightLevel}`);
366
283
  break;
367
284
  case 8:
368
- this.CurrentAmbientLightLevel =
369
- ((this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels) * 7;
370
- this.debugLog(
371
- `Curtain: ${this.accessory.displayName} LightLevel: ${this.lightLevel}`,
372
- );
285
+ this.CurrentAmbientLightLevel = ((this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels) * 7;
286
+ this.debugLog(`Curtain: ${this.accessory.displayName} LightLevel: ${this.lightLevel}`);
373
287
  break;
374
288
  case 9:
375
- this.CurrentAmbientLightLevel =
376
- ((this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels) * 8;
377
- this.debugLog(
378
- `Curtain: ${this.accessory.displayName} LightLevel: ${this.lightLevel}`,
379
- );
289
+ this.CurrentAmbientLightLevel = ((this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels) * 8;
290
+ this.debugLog(`Curtain: ${this.accessory.displayName} LightLevel: ${this.lightLevel}`);
380
291
  break;
381
292
  case 10:
382
293
  default:
@@ -384,22 +295,17 @@ export class Curtain {
384
295
  this.debugLog();
385
296
  }
386
297
  this.debugLog(
387
- `Curtain: ${this.accessory.displayName} LightLevel: ${this.lightLevel},` +
388
- ` CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`,
298
+ `Curtain: ${this.accessory.displayName} LightLevel: ${this.lightLevel},` + ` CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`,
389
299
  );
390
300
  }
391
301
  // Battery
392
302
  this.BatteryLevel = Number(this.battery);
393
303
  if (this.BatteryLevel < 10) {
394
- this.StatusLowBattery =
395
- this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
304
+ this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
396
305
  } else {
397
- this.StatusLowBattery =
398
- this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
306
+ this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
399
307
  }
400
- this.debugLog(
401
- `Curtain: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`,
402
- );
308
+ this.debugLog(`Curtain: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
403
309
  }
404
310
 
405
311
  private async openAPIparseStatus() {
@@ -407,51 +313,33 @@ export class Curtain {
407
313
  this.SwitchToOpenAPI = false;
408
314
  }
409
315
  if (this.platform.config.credentials?.openToken) {
410
- this.debugLog(
411
- `Curtain: ${this.accessory.displayName} OpenAPI parseStatus`,
412
- );
316
+ this.debugLog(`Curtain: ${this.accessory.displayName} OpenAPI parseStatus`);
413
317
  // CurrentPosition
414
318
  this.setMinMax();
415
319
  this.CurrentPosition = 100 - Number(this.slidePosition);
416
- this.debugLog(
417
- `Curtain ${this.accessory.displayName} CurrentPosition: ${this.CurrentPosition}`,
418
- );
320
+ this.debugLog(`Curtain ${this.accessory.displayName} CurrentPosition: ${this.CurrentPosition}`);
419
321
  if (this.setNewTarget) {
420
- this.infoLog(
421
- `Curtain: ${this.accessory.displayName} Checking Status ...`,
422
- );
322
+ this.infoLog(`Curtain: ${this.accessory.displayName} Checking Status ...`);
423
323
  }
424
324
 
425
325
  if (this.setNewTarget && this.moving) {
426
326
  this.setMinMax();
427
327
  if (this.TargetPosition > this.CurrentPosition) {
428
- this.debugLog(
429
- `Curtain: ${this.accessory.displayName} Closing, CurrentPosition: ${this.CurrentPosition} `,
430
- );
431
- this.PositionState =
432
- this.platform.Characteristic.PositionState.INCREASING;
328
+ this.debugLog(`Curtain: ${this.accessory.displayName} Closing, CurrentPosition: ${this.CurrentPosition} `);
329
+ this.PositionState = this.platform.Characteristic.PositionState.INCREASING;
433
330
  } else if (this.TargetPosition < this.CurrentPosition) {
434
- this.debugLog(
435
- `Curtain: ${this.accessory.displayName} Opening, CurrentPosition: ${this.CurrentPosition} `,
436
- );
437
- this.PositionState =
438
- this.platform.Characteristic.PositionState.DECREASING;
331
+ this.debugLog(`Curtain: ${this.accessory.displayName} Opening, CurrentPosition: ${this.CurrentPosition} `);
332
+ this.PositionState = this.platform.Characteristic.PositionState.DECREASING;
439
333
  } else {
440
- this.debugLog(
441
- `Curtain: ${this.CurrentPosition} Standby, CurrentPosition: ${this.CurrentPosition}`,
442
- );
443
- this.PositionState =
444
- this.platform.Characteristic.PositionState.STOPPED;
334
+ this.debugLog(`Curtain: ${this.CurrentPosition} Standby, CurrentPosition: ${this.CurrentPosition}`);
335
+ this.PositionState = this.platform.Characteristic.PositionState.STOPPED;
445
336
  }
446
337
  } else {
447
- this.debugLog(
448
- `Curtain: ${this.accessory.displayName} Standby, CurrentPosition: ${this.CurrentPosition}`,
449
- );
338
+ this.debugLog(`Curtain: ${this.accessory.displayName} Standby, CurrentPosition: ${this.CurrentPosition}`);
450
339
  if (!this.setNewTarget) {
451
340
  /*If Curtain calibration distance is short, there will be an error between the current percentage and the target percentage.*/
452
341
  this.TargetPosition = this.CurrentPosition;
453
- this.PositionState =
454
- this.platform.Characteristic.PositionState.STOPPED;
342
+ this.PositionState = this.platform.Characteristic.PositionState.STOPPED;
455
343
  this.debugLog(`Curtain: ${this.accessory.displayName} Stopped`);
456
344
  }
457
345
  }
@@ -465,16 +353,14 @@ export class Curtain {
465
353
  this.set_maxLux = this.maxLux();
466
354
  // Brightness
467
355
  switch (this.brightness) {
468
- case "dim":
356
+ case 'dim':
469
357
  this.CurrentAmbientLightLevel = this.set_minLux;
470
358
  break;
471
- case "bright":
359
+ case 'bright':
472
360
  default:
473
361
  this.CurrentAmbientLightLevel = this.set_maxLux;
474
362
  }
475
- this.debugLog(
476
- `Curtain: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`,
477
- );
363
+ this.debugLog(`Curtain: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
478
364
  }
479
365
  }
480
366
  }
@@ -493,121 +379,108 @@ export class Curtain {
493
379
  // Convert to BLE Address
494
380
  this.device.bleMac = this.device
495
381
  .deviceId!.match(/.{1,2}/g)!
496
- .join(":")
382
+ .join(':')
497
383
  .toLowerCase();
498
- this.debugLog(
499
- `Curtain: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`,
500
- );
501
- try {
502
- await switchbot.discover({
503
- model: "c",
504
- quick: false,
505
- });
506
- // Start to monitor advertisement packets
507
- await switchbot.startScan({
508
- model: "c",
509
- id: this.device.bleMac,
510
- });
511
- // Set an event hander
512
- switchbot.onadvertisement = (ad: settings.ad) => {
513
- this.serviceData = ad.serviceData;
514
- this.calibration = ad.serviceData.calibration;
515
- this.battery = ad.serviceData.battery;
516
- this.position = ad.serviceData.position;
517
- this.lightLevel = ad.serviceData.lightLevel;
518
- this.debugLog(
519
- `Curtain: ${this.accessory.displayName} serviceData: ${JSON.stringify(
520
- ad.serviceData,
521
- )}`,
522
- );
523
- this.debugLog(
524
- `Curtain: ${this.accessory.displayName} calibration: ${ad.serviceData.calibration}, ` +
525
- `position: ${ad.serviceData.position}, lightLevel: ${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}`,
526
- );
384
+ this.debugLog(`Curtain: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
385
+ // Start to monitor advertisement packets
386
+ if (switchbot !== false) {
387
+ switchbot
388
+ .startScan({
389
+ model: 'c',
390
+ id: this.device.bleMac,
391
+ })
392
+ .then(() => {
393
+ // Set an event hander
394
+ switchbot.onadvertisement = (ad: any) => {
395
+ this.serviceData = ad.serviceData;
396
+ this.calibration = ad.serviceData.calibration;
397
+ this.battery = ad.serviceData.battery;
398
+ this.position = ad.serviceData.position;
399
+ this.lightLevel = ad.serviceData.lightLevel;
400
+ this.debugLog(`Curtain: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
401
+ this.debugLog(
402
+ `Curtain: ${this.accessory.displayName} calibration: ${ad.serviceData.calibration}, ` +
403
+ `position: ${ad.serviceData.position}, lightLevel: ${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}`,
404
+ );
527
405
 
528
- if (this.serviceData) {
529
- this.connected = true;
530
- this.debugLog(
531
- `Curtain: ${this.accessory.displayName} connected: ${this.connected}`,
532
- );
533
- } else {
534
- this.connected = false;
535
- this.debugLog(
536
- `Curtain: ${this.accessory.displayName} connected: ${this.connected}`,
537
- );
538
- }
539
- };
540
- // Wait 10 seconds
541
- await switchbot.wait(this.scanDuration * 1000);
542
- // Stop to monitor
543
- switchbot.stopScan();
544
- if (this.connected) {
545
- this.parseStatus();
546
- this.updateHomeKitCharacteristics();
547
- } else {
548
- this.errorLog(
549
- `Curtain: ${this.accessory.displayName} wasn't able to establish BLE Connection`,
550
- );
551
- if (this.platform.config.credentials?.openToken) {
552
- this.warnLog(
553
- `Curtain: ${this.accessory.displayName} Using OpenAPI Connection`,
554
- );
555
- this.SwitchToOpenAPI = true;
556
- await this.openAPIRefreshStatus();
557
- }
558
- }
559
- } catch (e: any) {
560
- this.errorLog(
561
- `Curtain: ${this.accessory.displayName} failed refreshStatus with BLE Connection`,
562
- );
563
- if (this.deviceLogging.includes("debug")) {
564
- this.errorLog(
565
- `Curtain: ${this.accessory.displayName} failed refreshStatus with BLE Connection,` +
566
- ` Error Message: ${JSON.stringify(e.message)}`,
567
- );
568
- }
569
- if (this.platform.config.credentials?.openToken) {
570
- this.warnLog(
571
- `Curtain: ${this.accessory.displayName} Using OpenAPI Connection`,
572
- );
573
- this.SwitchToOpenAPI = true;
574
- await this.openAPIRefreshStatus();
575
- }
576
- this.apiError(e);
406
+ if (this.serviceData) {
407
+ this.connected = true;
408
+ this.debugLog(`Curtain: ${this.accessory.displayName} connected: ${this.connected}`);
409
+ } else {
410
+ this.connected = false;
411
+ this.debugLog(`Curtain: ${this.accessory.displayName} connected: ${this.connected}`);
412
+ }
413
+ };
414
+ // Wait 2 seconds
415
+ return switchbot.wait(this.scanDuration * 1000);
416
+ })
417
+ .then(async () => {
418
+ // Stop to monitor
419
+ switchbot.stopScan();
420
+ if (this.connected) {
421
+ this.parseStatus();
422
+ this.updateHomeKitCharacteristics();
423
+ } else {
424
+ this.errorLog(`Curtain: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
425
+ if (this.platform.config.credentials?.openToken) {
426
+ this.warnLog(`Curtain: ${this.accessory.displayName} Using OpenAPI Connection`);
427
+ this.SwitchToOpenAPI = true;
428
+ await this.openAPIRefreshStatus();
429
+ }
430
+ }
431
+ })
432
+ .catch(async (e: any) => {
433
+ this.errorLog(`Curtain: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
434
+ if (this.deviceLogging === 'debug') {
435
+ this.errorLog(
436
+ `Curtain: ${this.accessory.displayName} failed refreshStatus with BLE Connection,` + ` Error Message: ${JSON.stringify(e.message)}`,
437
+ );
438
+ }
439
+ if (this.platform.debugMode) {
440
+ this.errorLog(`Curtain: ${this.accessory.displayName} failed refreshStatus with BLE Connection,` + ` Error: ${JSON.stringify(e)}`);
441
+ }
442
+ if (this.platform.config.credentials?.openToken) {
443
+ this.warnLog(`Curtain: ${this.accessory.displayName} Using OpenAPI Connection`);
444
+ this.SwitchToOpenAPI = true;
445
+ await this.openAPIRefreshStatus();
446
+ }
447
+ this.apiError(e);
448
+ });
449
+ } else {
450
+ await this.BLEconnection(switchbot);
451
+ }
452
+ }
453
+
454
+ public async BLEconnection(switchbot: any) {
455
+ this.errorLog(`Curtain: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
456
+ if (this.platform.config.credentials?.openToken) {
457
+ this.warnLog(`Curtain: ${this.accessory.displayName} Using OpenAPI Connection`);
458
+ this.SwitchToOpenAPI = true;
459
+ await this.openAPIRefreshStatus();
577
460
  }
578
461
  }
579
462
 
580
463
  private async openAPIRefreshStatus() {
581
464
  if (this.platform.config.credentials?.openToken) {
582
- this.debugLog(
583
- `Curtain: ${this.accessory.displayName} OpenAPI refreshStatus`,
584
- );
465
+ this.debugLog(`Curtain: ${this.accessory.displayName} OpenAPI refreshStatus`);
585
466
  try {
586
- this.deviceStatus = (
587
- await this.platform.axios.get(
588
- `${settings.DeviceURL}/${this.device.deviceId}/status`,
589
- )
590
- ).data;
591
- this.debugLog(
592
- `Curtain: ${
593
- this.accessory.displayName
594
- } refreshStatus: ${JSON.stringify(this.deviceStatus)}`,
595
- );
467
+ this.deviceStatus = (await this.platform.axios.get(`${DeviceURL}/${this.device.deviceId}/status`)).data;
468
+ this.debugLog(`Curtain: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(this.deviceStatus)}`);
596
469
  this.slidePosition = this.deviceStatus.body.slidePosition;
597
470
  this.moving = this.deviceStatus.body.moving;
598
471
  this.brightness = this.deviceStatus.body.brightness;
599
472
  this.parseStatus();
600
473
  this.updateHomeKitCharacteristics();
601
474
  } catch (e: any) {
602
- this.errorLog(
603
- `Curtain: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`,
604
- );
605
- if (this.deviceLogging.includes("debug")) {
475
+ this.errorLog(`Curtain: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
476
+ if (this.deviceLogging === 'debug') {
606
477
  this.errorLog(
607
- `Curtain: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,` +
608
- ` Error Message: ${JSON.stringify(e.message)}`,
478
+ `Curtain: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,` + ` Error Message: ${JSON.stringify(e.message)}`,
609
479
  );
610
480
  }
481
+ if (this.platform.debugMode) {
482
+ this.errorLog(`Curtain: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,` + ` Error: ${JSON.stringify(e)}`);
483
+ }
611
484
  this.apiError(e);
612
485
  }
613
486
  }
@@ -619,10 +492,9 @@ export class Curtain {
619
492
  } else {
620
493
  await this.openAPIpushChanges();
621
494
  }
622
- rxjs
623
- .interval(5000)
624
- .pipe(operators.skipWhile(() => this.curtainUpdateInProgress))
625
- .pipe(operators.take(1))
495
+ interval(5000)
496
+ .pipe(skipWhile(() => this.curtainUpdateInProgress))
497
+ .pipe(take(1))
626
498
  .subscribe(() => {
627
499
  this.refreshStatus();
628
500
  });
@@ -635,49 +507,39 @@ export class Curtain {
635
507
  // Convert to BLE Address
636
508
  this.device.bleMac = this.device
637
509
  .deviceId!.match(/.{1,2}/g)!
638
- .join(":")
510
+ .join(':')
639
511
  .toLowerCase();
640
- this.debugLog(
641
- `Curtain: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`,
642
- );
512
+ this.debugLog(`Curtain: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
643
513
  if (switchbot !== false) {
644
514
  switchbot
645
- .discover({ model: "c", quick: true, id: this.device.bleMac })
515
+ .discover({ model: 'c', quick: true, id: this.device.bleMac })
646
516
  .then((device_list) => {
647
- this.infoLog(
648
- `${this.accessory.displayName} Target Position: ${this.TargetPosition}`,
649
- );
517
+ this.infoLog(`${this.accessory.displayName} Target Position: ${this.TargetPosition}`);
650
518
  return device_list[0].runToPos(100 - Number(this.TargetPosition));
651
519
  })
652
520
  .then(() => {
653
521
  this.debugLog(`Curtain: ${this.accessory.displayName} Done.`);
654
522
  })
655
523
  .catch(async (e: any) => {
656
- this.errorLog(
657
- `Curtain: ${this.accessory.displayName} failed pushChanges with BLE Connection`,
658
- );
659
- if (this.deviceLogging.includes("debug")) {
524
+ this.errorLog(`Curtain: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
525
+ if (this.deviceLogging === 'debug') {
660
526
  this.errorLog(
661
- `Curtain: ${this.accessory.displayName} failed pushChanges with BLE Connection,` +
662
- ` Error Message: ${JSON.stringify(e.message)}`,
527
+ `Curtain: ${this.accessory.displayName} failed pushChanges with BLE Connection,` + ` Error Message: ${JSON.stringify(e.message)}`,
663
528
  );
664
529
  }
530
+ if (this.platform.debugMode) {
531
+ this.errorLog(`Curtain: ${this.accessory.displayName} failed pushChanges with BLE Connection,` + ` Error: ${JSON.stringify(e)}`);
532
+ }
665
533
  if (this.platform.config.credentials?.openToken) {
666
- this.warnLog(
667
- `Curtain: ${this.accessory.displayName} Using OpenAPI Connection`,
668
- );
534
+ this.warnLog(`Curtain: ${this.accessory.displayName} Using OpenAPI Connection`);
669
535
  await this.openAPIpushChanges();
670
536
  }
671
537
  this.apiError(e);
672
538
  });
673
539
  } else {
674
- this.errorLog(
675
- `Curtain: ${this.accessory.displayName} wasn't able to establish BLE Connection`,
676
- );
540
+ this.errorLog(`Curtain: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
677
541
  if (this.platform.config.credentials?.openToken) {
678
- this.warnLog(
679
- `Curtain: ${this.accessory.displayName} Using OpenAPI Connection`,
680
- );
542
+ this.warnLog(`Curtain: ${this.accessory.displayName} Using OpenAPI Connection`);
681
543
  await this.openAPIpushChanges();
682
544
  }
683
545
  }
@@ -692,17 +554,15 @@ export class Curtain {
692
554
  private async openAPIpushChanges() {
693
555
  if (this.platform.config.credentials?.openToken) {
694
556
  try {
695
- this.debugLog(
696
- `Curtain: ${this.accessory.displayName} OpenAPI pushChanges`,
697
- );
557
+ this.debugLog(`Curtain: ${this.accessory.displayName} OpenAPI pushChanges`);
698
558
  if (this.TargetPosition !== this.CurrentPosition) {
699
559
  this.debugLog(`Pushing ${this.TargetPosition}`);
700
560
  const adjustedTargetPosition = 100 - Number(this.TargetPosition);
701
561
  const payload = {
702
- commandType: "command",
703
- command: "setPosition",
562
+ commandType: 'command',
563
+ command: 'setPosition',
704
564
  parameter: `0,ff,${adjustedTargetPosition}`,
705
- } as settings.payload;
565
+ } as payload;
706
566
 
707
567
  this.infoLog(
708
568
  `Curtain: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},` +
@@ -710,15 +570,8 @@ export class Curtain {
710
570
  );
711
571
 
712
572
  // Make the API request
713
- const push: any = await this.platform.axios.post(
714
- `${settings.DeviceURL}/${this.device.deviceId!}/commands`,
715
- payload,
716
- );
717
- this.debugLog(
718
- `Curtain: ${
719
- this.accessory.displayName
720
- } pushchanges: ${JSON.stringify(push.data)}`,
721
- );
573
+ const push: any = await this.platform.axios.post(`${DeviceURL}/${this.device.deviceId!}/commands`, payload);
574
+ this.debugLog(`Curtain: ${this.accessory.displayName} pushchanges: ${JSON.stringify(push.data)}`);
722
575
  this.statusCode(push);
723
576
  } else {
724
577
  this.debugLog(
@@ -727,15 +580,15 @@ export class Curtain {
727
580
  );
728
581
  }
729
582
  } catch (e: any) {
730
- this.errorLog(
731
- `Curtain: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`,
732
- );
733
- if (this.deviceLogging.includes("debug")) {
583
+ this.errorLog(`Curtain: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
584
+ if (this.deviceLogging === 'debug') {
734
585
  this.errorLog(
735
- `Curtain: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,` +
736
- ` Error Message: ${JSON.stringify(e.message)}`,
586
+ `Curtain: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,` + ` Error Message: ${JSON.stringify(e.message)}`,
737
587
  );
738
588
  }
589
+ if (this.platform.debugMode) {
590
+ this.errorLog(`Curtain: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,` + ` Error: ${JSON.stringify(e)}`);
591
+ }
739
592
  this.apiError(e);
740
593
  }
741
594
  }
@@ -743,154 +596,77 @@ export class Curtain {
743
596
 
744
597
  updateHomeKitCharacteristics() {
745
598
  this.setMinMax();
746
- if (
747
- this.CurrentPosition === undefined ||
748
- Number.isNaN(this.CurrentPosition)
749
- ) {
750
- this.debugLog(
751
- `Curtain: ${this.accessory.displayName} CurrentPosition: ${this.CurrentPosition}`,
752
- );
599
+ if (this.CurrentPosition === undefined || Number.isNaN(this.CurrentPosition)) {
600
+ this.debugLog(`Curtain: ${this.accessory.displayName} CurrentPosition: ${this.CurrentPosition}`);
753
601
  } else {
754
- this.service.updateCharacteristic(
755
- this.platform.Characteristic.CurrentPosition,
756
- Number(this.CurrentPosition),
757
- );
758
- this.debugLog(
759
- `Curtain: ${this.accessory.displayName} updateCharacteristic CurrentPosition: ${this.CurrentPosition}`,
760
- );
602
+ this.service.updateCharacteristic(this.platform.Characteristic.CurrentPosition, Number(this.CurrentPosition));
603
+ this.debugLog(`Curtain: ${this.accessory.displayName} updateCharacteristic CurrentPosition: ${this.CurrentPosition}`);
761
604
  }
762
605
  if (this.PositionState === undefined) {
763
- this.debugLog(
764
- `Curtain: ${this.accessory.displayName} PositionState: ${this.PositionState}`,
765
- );
606
+ this.debugLog(`Curtain: ${this.accessory.displayName} PositionState: ${this.PositionState}`);
766
607
  } else {
767
- this.debugLog(
768
- `Curtain: ${this.accessory.displayName} updateCharacteristic PositionState: ${this.PositionState}`,
769
- );
608
+ this.debugLog(`Curtain: ${this.accessory.displayName} updateCharacteristic PositionState: ${this.PositionState}`);
770
609
  }
771
- if (
772
- this.TargetPosition === undefined ||
773
- Number.isNaN(this.TargetPosition)
774
- ) {
775
- this.debugLog(
776
- `Curtain: ${this.accessory.displayName} TargetPosition: ${this.TargetPosition}`,
777
- );
610
+ if (this.TargetPosition === undefined || Number.isNaN(this.TargetPosition)) {
611
+ this.debugLog(`Curtain: ${this.accessory.displayName} TargetPosition: ${this.TargetPosition}`);
778
612
  } else {
779
- this.service.updateCharacteristic(
780
- this.platform.Characteristic.TargetPosition,
781
- Number(this.TargetPosition),
782
- );
783
- this.debugLog(
784
- `Curtain: ${this.accessory.displayName} updateCharacteristic TargetPosition: ${this.TargetPosition}`,
785
- );
613
+ this.service.updateCharacteristic(this.platform.Characteristic.TargetPosition, Number(this.TargetPosition));
614
+ this.debugLog(`Curtain: ${this.accessory.displayName} updateCharacteristic TargetPosition: ${this.TargetPosition}`);
786
615
  }
787
616
  if (!this.device.curtain?.hide_lightsensor) {
788
- if (
789
- this.CurrentAmbientLightLevel === undefined ||
790
- Number.isNaN(this.CurrentAmbientLightLevel)
791
- ) {
792
- this.debugLog(
793
- `Curtain: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`,
794
- );
617
+ if (this.CurrentAmbientLightLevel === undefined || Number.isNaN(this.CurrentAmbientLightLevel)) {
618
+ this.debugLog(`Curtain: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
795
619
  } else {
796
- this.lightSensorService?.updateCharacteristic(
797
- this.platform.Characteristic.CurrentAmbientLightLevel,
798
- this.CurrentAmbientLightLevel,
799
- );
800
- this.debugLog(
801
- `Curtain: ${this.accessory.displayName}` +
802
- ` updateCharacteristic CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`,
803
- );
620
+ this.lightSensorService?.updateCharacteristic(this.platform.Characteristic.CurrentAmbientLightLevel, this.CurrentAmbientLightLevel);
621
+ this.debugLog(`Curtain: ${this.accessory.displayName}` + ` updateCharacteristic CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
804
622
  }
805
623
  }
806
624
  if (this.device.ble) {
807
625
  if (this.BatteryLevel === undefined) {
808
- this.debugLog(
809
- `Curtain: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`,
810
- );
626
+ this.debugLog(`Curtain: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
811
627
  } else {
812
- this.batteryService?.updateCharacteristic(
813
- this.platform.Characteristic.BatteryLevel,
814
- this.BatteryLevel,
815
- );
816
- this.debugLog(
817
- `Curtain: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`,
818
- );
628
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
629
+ this.debugLog(`Curtain: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
819
630
  }
820
631
  if (this.StatusLowBattery === undefined) {
821
- this.debugLog(
822
- `Curtain: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`,
823
- );
632
+ this.debugLog(`Curtain: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
824
633
  } else {
825
- this.batteryService?.updateCharacteristic(
826
- this.platform.Characteristic.StatusLowBattery,
827
- this.StatusLowBattery,
828
- );
829
- this.debugLog(
830
- `Curtain: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`,
831
- );
634
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
635
+ this.debugLog(`Curtain: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
832
636
  }
833
637
  }
834
638
  }
835
639
 
836
640
  public apiError(e: any) {
837
- this.service.updateCharacteristic(
838
- this.platform.Characteristic.CurrentPosition,
839
- e,
840
- );
841
- this.service.updateCharacteristic(
842
- this.platform.Characteristic.PositionState,
843
- e,
844
- );
845
- this.service.updateCharacteristic(
846
- this.platform.Characteristic.TargetPosition,
847
- e,
848
- );
641
+ this.service.updateCharacteristic(this.platform.Characteristic.CurrentPosition, e);
642
+ this.service.updateCharacteristic(this.platform.Characteristic.PositionState, e);
643
+ this.service.updateCharacteristic(this.platform.Characteristic.TargetPosition, e);
849
644
  if (!this.device.curtain?.hide_lightsensor) {
850
- this.lightSensorService?.updateCharacteristic(
851
- this.platform.Characteristic.CurrentAmbientLightLevel,
852
- e,
853
- );
645
+ this.lightSensorService?.updateCharacteristic(this.platform.Characteristic.CurrentAmbientLightLevel, e);
854
646
  }
855
647
  if (this.device.ble) {
856
- this.batteryService?.updateCharacteristic(
857
- this.platform.Characteristic.BatteryLevel,
858
- e,
859
- );
860
- this.batteryService?.updateCharacteristic(
861
- this.platform.Characteristic.StatusLowBattery,
862
- e,
863
- );
648
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
649
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
864
650
  }
865
651
  //throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
866
652
  }
867
653
 
868
- private statusCode(push: axios.AxiosResponse<{ statusCode: number }>) {
654
+ private statusCode(push: AxiosResponse<{ statusCode: number }>) {
869
655
  switch (push.data.statusCode) {
870
656
  case 151:
871
- this.errorLog(
872
- `Curtain: ${this.accessory.displayName} Command not supported by this device type.`,
873
- );
657
+ this.errorLog(`Curtain: ${this.accessory.displayName} Command not supported by this device type.`);
874
658
  break;
875
659
  case 152:
876
- this.errorLog(
877
- `Curtain: ${this.accessory.displayName} Device not found.`,
878
- );
660
+ this.errorLog(`Curtain: ${this.accessory.displayName} Device not found.`);
879
661
  break;
880
662
  case 160:
881
- this.errorLog(
882
- `Curtain: ${this.accessory.displayName} Command is not supported.`,
883
- );
663
+ this.errorLog(`Curtain: ${this.accessory.displayName} Command is not supported.`);
884
664
  break;
885
665
  case 161:
886
- this.errorLog(
887
- `Curtain: ${this.accessory.displayName} Device is offline.`,
888
- );
666
+ this.errorLog(`Curtain: ${this.accessory.displayName} Device is offline.`);
889
667
  break;
890
668
  case 171:
891
- this.errorLog(
892
- `Curtain: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`,
893
- );
669
+ this.errorLog(`Curtain: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
894
670
  break;
895
671
  case 190:
896
672
  this.errorLog(
@@ -899,56 +675,37 @@ export class Curtain {
899
675
  );
900
676
  break;
901
677
  case 100:
902
- this.debugLog(
903
- `Curtain: ${this.accessory.displayName} Command successfully sent.`,
904
- );
678
+ this.debugLog(`Curtain: ${this.accessory.displayName} Command successfully sent.`);
905
679
  break;
906
680
  default:
907
- this.debugLog(
908
- `Curtain: ${this.accessory.displayName} Unknown statusCode.`,
909
- );
681
+ this.debugLog(`Curtain: ${this.accessory.displayName} Unknown statusCode.`);
910
682
  }
911
683
  }
912
684
 
913
685
  /**
914
686
  * Handle requests to set the value of the "Target Position" characteristic
915
687
  */
916
- TargetPositionSet(value: homebridge.CharacteristicValue) {
917
- this.debugLog(
918
- `Curtain: ${this.accessory.displayName} TargetPosition: ${value}`,
919
- );
688
+ TargetPositionSet(value: CharacteristicValue) {
689
+ this.debugLog(`Curtain: ${this.accessory.displayName} TargetPosition: ${value}`);
920
690
 
921
691
  this.TargetPosition = value;
922
692
 
923
693
  this.setMinMax();
924
694
  if (value > this.CurrentPosition) {
925
- this.PositionState =
926
- this.platform.Characteristic.PositionState.INCREASING;
695
+ this.PositionState = this.platform.Characteristic.PositionState.INCREASING;
927
696
  this.setNewTarget = true;
928
- this.debugLog(
929
- `Curtain: ${this.accessory.displayName} value: ${value}, CurrentPosition: ${this.CurrentPosition}`,
930
- );
697
+ this.debugLog(`Curtain: ${this.accessory.displayName} value: ${value}, CurrentPosition: ${this.CurrentPosition}`);
931
698
  } else if (value < this.CurrentPosition) {
932
- this.PositionState =
933
- this.platform.Characteristic.PositionState.DECREASING;
699
+ this.PositionState = this.platform.Characteristic.PositionState.DECREASING;
934
700
  this.setNewTarget = true;
935
- this.debugLog(
936
- `Curtain: ${this.accessory.displayName} value: ${value}, CurrentPosition: ${this.CurrentPosition}`,
937
- );
701
+ this.debugLog(`Curtain: ${this.accessory.displayName} value: ${value}, CurrentPosition: ${this.CurrentPosition}`);
938
702
  } else {
939
703
  this.PositionState = this.platform.Characteristic.PositionState.STOPPED;
940
704
  this.setNewTarget = false;
941
- this.debugLog(
942
- `Curtain: ${this.accessory.displayName} value: ${value}, CurrentPosition: ${this.CurrentPosition}`,
943
- );
705
+ this.debugLog(`Curtain: ${this.accessory.displayName} value: ${value}, CurrentPosition: ${this.CurrentPosition}`);
944
706
  }
945
- this.service.setCharacteristic(
946
- this.platform.Characteristic.PositionState,
947
- this.PositionState,
948
- );
949
- this.service
950
- .getCharacteristic(this.platform.Characteristic.PositionState)
951
- .updateValue(this.PositionState);
707
+ this.service.setCharacteristic(this.platform.Characteristic.PositionState, this.PositionState);
708
+ this.service.getCharacteristic(this.platform.Characteristic.PositionState).updateValue(this.PositionState);
952
709
 
953
710
  /**
954
711
  * If Curtain movement time is short, the moving flag from backend is always false.
@@ -957,9 +714,7 @@ export class Curtain {
957
714
  clearTimeout(this.setNewTargetTimer);
958
715
  if (this.setNewTarget) {
959
716
  this.setNewTargetTimer = setTimeout(() => {
960
- this.debugLog(
961
- `Curtain: ${this.accessory.displayName} setNewTarget ${this.setNewTarget} timeout`,
962
- );
717
+ this.debugLog(`Curtain: ${this.accessory.displayName} setNewTarget ${this.setNewTarget} timeout`);
963
718
  this.setNewTarget = false;
964
719
  }, 10000);
965
720
  }
@@ -979,106 +734,78 @@ export class Curtain {
979
734
  }
980
735
  }
981
736
 
982
- config(device: settings.device & settings.devicesConfig) {
737
+ config(device: device & devicesConfig) {
983
738
  let config = {};
984
739
  if (device.curtain) {
985
740
  config = device.curtain;
986
741
  }
987
742
  if (device.ble) {
988
- config["ble"] = device.ble;
743
+ config['ble'] = device.ble;
989
744
  }
990
745
  if (device.logging !== undefined) {
991
- config["logging"] = device.logging;
746
+ config['logging'] = device.logging;
992
747
  }
993
748
  if (device.refreshRate !== undefined) {
994
- config["refreshRate"] = device.refreshRate;
749
+ config['refreshRate'] = device.refreshRate;
995
750
  }
996
751
  if (device.scanDuration !== undefined) {
997
- config["scanDuration"] = device.scanDuration;
752
+ config['scanDuration'] = device.scanDuration;
998
753
  }
999
754
  if (Object.entries(config).length !== 0) {
1000
- this.warnLog(
1001
- `Curtain: ${this.accessory.displayName} Config: ${JSON.stringify(
1002
- config,
1003
- )}`,
1004
- );
755
+ this.warnLog(`Curtain: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
1005
756
  }
1006
757
  }
1007
758
 
1008
- refreshRate(device: settings.device & settings.devicesConfig) {
759
+ refreshRate(device: device & devicesConfig) {
1009
760
  // refreshRate
1010
761
  if (device.refreshRate) {
1011
- this.deviceRefreshRate = this.accessory.context.refreshRate =
1012
- device.refreshRate;
1013
- this.debugLog(
1014
- `Curtain: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`,
1015
- );
762
+ this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
763
+ this.debugLog(`Curtain: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
1016
764
  } else if (this.platform.config.options!.refreshRate) {
1017
- this.deviceRefreshRate = this.accessory.context.refreshRate =
1018
- this.platform.config.options!.refreshRate;
1019
- this.debugLog(
1020
- `Curtain: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`,
1021
- );
765
+ this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options!.refreshRate;
766
+ this.debugLog(`Curtain: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
1022
767
  }
1023
768
  // updateRate
1024
769
  if (device?.curtain?.updateRate) {
1025
770
  this.updateRate = device?.curtain?.updateRate;
1026
- this.debugLog(
1027
- `Curtain: ${this.accessory.displayName} Using Device Config Curtain refreshRate: ${this.deviceRefreshRate}`,
1028
- );
771
+ this.debugLog(`Curtain: ${this.accessory.displayName} Using Device Config Curtain refreshRate: ${this.deviceRefreshRate}`);
1029
772
  } else {
1030
773
  this.updateRate = 5;
1031
- this.debugLog(
1032
- `Curtain: ${this.accessory.displayName} Using Default Curtain Refresh Rate.`,
1033
- );
774
+ this.debugLog(`Curtain: ${this.accessory.displayName} Using Default Curtain Refresh Rate.`);
1034
775
  }
1035
776
  }
1036
777
 
1037
- scan(device: settings.device & settings.devicesConfig) {
778
+ scan(device: device & devicesConfig) {
1038
779
  if (device.scanDuration) {
1039
- this.scanDuration = this.accessory.context.scanDuration =
1040
- device.scanDuration;
780
+ this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
1041
781
  if (device.ble) {
1042
- this.debugLog(
1043
- `Curtain: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`,
1044
- );
782
+ this.debugLog(`Curtain: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
1045
783
  }
1046
784
  } else {
1047
785
  this.scanDuration = this.accessory.context.scanDuration = 1;
1048
786
  if (this.device.ble) {
1049
- this.debugLog(
1050
- `Curtain: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`,
1051
- );
787
+ this.debugLog(`Curtain: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
1052
788
  }
1053
789
  }
1054
790
  }
1055
791
 
1056
- logs(device: settings.device & settings.devicesConfig) {
792
+ logs(device: device & devicesConfig) {
1057
793
  if (this.platform.debugMode) {
1058
- this.deviceLogging = this.accessory.context.logging = "debugMode";
1059
- this.debugLog(
1060
- `Curtain: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`,
1061
- );
794
+ this.deviceLogging = this.accessory.context.logging = 'debugMode';
795
+ this.debugLog(`Curtain: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
1062
796
  } else if (device.logging) {
1063
797
  this.deviceLogging = this.accessory.context.logging = device.logging;
1064
- this.debugLog(
1065
- `Curtain: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`,
1066
- );
798
+ this.debugLog(`Curtain: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
1067
799
  } else if (this.platform.config.options?.logging) {
1068
- this.deviceLogging = this.accessory.context.logging =
1069
- this.platform.config.options?.logging;
1070
- this.debugLog(
1071
- `Curtain: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`,
1072
- );
800
+ this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
801
+ this.debugLog(`Curtain: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
1073
802
  } else {
1074
- this.deviceLogging = this.accessory.context.logging = "standard";
1075
- this.debugLog(
1076
- `Curtain: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`,
1077
- );
803
+ this.deviceLogging = this.accessory.context.logging = 'standard';
804
+ this.debugLog(`Curtain: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
1078
805
  }
1079
806
  }
1080
807
 
1081
- private minStep(device: settings.device & settings.devicesConfig): number {
808
+ private minStep(device: device & devicesConfig): number {
1082
809
  if (device.curtain?.set_minStep) {
1083
810
  this.set_minStep = device.curtain?.set_minStep;
1084
811
  } else {
@@ -1128,8 +855,8 @@ export class Curtain {
1128
855
 
1129
856
  debugLog(...log: any[]) {
1130
857
  if (this.enablingDeviceLogging()) {
1131
- if (this.deviceLogging === "debug") {
1132
- this.platform.log.info("[DEBUG]", String(...log));
858
+ if (this.deviceLogging === 'debug') {
859
+ this.platform.log.info('[DEBUG]', String(...log));
1133
860
  } else {
1134
861
  this.platform.log.debug(String(...log));
1135
862
  }
@@ -1137,8 +864,6 @@ export class Curtain {
1137
864
  }
1138
865
 
1139
866
  enablingDeviceLogging(): boolean {
1140
- return (
1141
- this.deviceLogging.includes("debug") || this.deviceLogging === "standard"
1142
- );
867
+ return this.deviceLogging === 'debug' || this.deviceLogging === 'standard';
1143
868
  }
1144
869
  }