@switchbot/homebridge-switchbot 3.5.0-beta.22 → 3.5.0-beta.24

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 (150) hide show
  1. package/config.schema.json +23 -2
  2. package/dist/device/blindtilt.d.ts +21 -98
  3. package/dist/device/blindtilt.d.ts.map +1 -1
  4. package/dist/device/blindtilt.js +332 -785
  5. package/dist/device/blindtilt.js.map +1 -1
  6. package/dist/device/bot.d.ts +25 -75
  7. package/dist/device/bot.d.ts.map +1 -1
  8. package/dist/device/bot.js +566 -778
  9. package/dist/device/bot.js.map +1 -1
  10. package/dist/device/ceilinglight.d.ts +13 -71
  11. package/dist/device/ceilinglight.d.ts.map +1 -1
  12. package/dist/device/ceilinglight.js +168 -606
  13. package/dist/device/ceilinglight.js.map +1 -1
  14. package/dist/device/colorbulb.d.ts +13 -78
  15. package/dist/device/colorbulb.d.ts.map +1 -1
  16. package/dist/device/colorbulb.js +218 -655
  17. package/dist/device/colorbulb.js.map +1 -1
  18. package/dist/device/contact.d.ts +14 -69
  19. package/dist/device/contact.d.ts.map +1 -1
  20. package/dist/device/contact.js +196 -553
  21. package/dist/device/contact.js.map +1 -1
  22. package/dist/device/curtain.d.ts +17 -90
  23. package/dist/device/curtain.d.ts.map +1 -1
  24. package/dist/device/curtain.js +324 -810
  25. package/dist/device/curtain.js.map +1 -1
  26. package/dist/device/device.d.ts +58 -0
  27. package/dist/device/device.d.ts.map +1 -0
  28. package/dist/device/device.js +629 -0
  29. package/dist/device/device.js.map +1 -0
  30. package/dist/device/hub.d.ts +15 -63
  31. package/dist/device/hub.d.ts.map +1 -1
  32. package/dist/device/hub.js +223 -610
  33. package/dist/device/hub.js.map +1 -1
  34. package/dist/device/humidifier.d.ts +10 -69
  35. package/dist/device/humidifier.d.ts.map +1 -1
  36. package/dist/device/humidifier.js +192 -595
  37. package/dist/device/humidifier.js.map +1 -1
  38. package/dist/device/iosensor.d.ts +11 -68
  39. package/dist/device/iosensor.d.ts.map +1 -1
  40. package/dist/device/iosensor.js +156 -599
  41. package/dist/device/iosensor.js.map +1 -1
  42. package/dist/device/lightstrip.d.ts +10 -72
  43. package/dist/device/lightstrip.d.ts.map +1 -1
  44. package/dist/device/lightstrip.js +204 -591
  45. package/dist/device/lightstrip.js.map +1 -1
  46. package/dist/device/lock.d.ts +15 -66
  47. package/dist/device/lock.d.ts.map +1 -1
  48. package/dist/device/lock.js +199 -566
  49. package/dist/device/lock.js.map +1 -1
  50. package/dist/device/meter.d.ts +13 -67
  51. package/dist/device/meter.d.ts.map +1 -1
  52. package/dist/device/meter.js +152 -592
  53. package/dist/device/meter.js.map +1 -1
  54. package/dist/device/meterplus.d.ts +13 -68
  55. package/dist/device/meterplus.d.ts.map +1 -1
  56. package/dist/device/meterplus.js +151 -599
  57. package/dist/device/meterplus.js.map +1 -1
  58. package/dist/device/motion.d.ts +13 -73
  59. package/dist/device/motion.d.ts.map +1 -1
  60. package/dist/device/motion.js +129 -540
  61. package/dist/device/motion.js.map +1 -1
  62. package/dist/device/plug.d.ts +9 -61
  63. package/dist/device/plug.d.ts.map +1 -1
  64. package/dist/device/plug.js +71 -476
  65. package/dist/device/plug.js.map +1 -1
  66. package/dist/device/robotvacuumcleaner.d.ts +11 -66
  67. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  68. package/dist/device/robotvacuumcleaner.js +122 -471
  69. package/dist/device/robotvacuumcleaner.js.map +1 -1
  70. package/dist/device/waterdetector.d.ts +9 -66
  71. package/dist/device/waterdetector.d.ts.map +1 -1
  72. package/dist/device/waterdetector.js +105 -478
  73. package/dist/device/waterdetector.js.map +1 -1
  74. package/dist/irdevice/airconditioner.d.ts +5 -36
  75. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  76. package/dist/irdevice/airconditioner.js +228 -337
  77. package/dist/irdevice/airconditioner.js.map +1 -1
  78. package/dist/irdevice/airpurifier.d.ts +5 -34
  79. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  80. package/dist/irdevice/airpurifier.js +19 -267
  81. package/dist/irdevice/airpurifier.js.map +1 -1
  82. package/dist/irdevice/camera.d.ts +5 -34
  83. package/dist/irdevice/camera.d.ts.map +1 -1
  84. package/dist/irdevice/camera.js +13 -261
  85. package/dist/irdevice/camera.js.map +1 -1
  86. package/dist/irdevice/fan.d.ts +5 -34
  87. package/dist/irdevice/fan.d.ts.map +1 -1
  88. package/dist/irdevice/fan.js +13 -260
  89. package/dist/irdevice/fan.js.map +1 -1
  90. package/dist/irdevice/irdevice.d.ts +39 -0
  91. package/dist/irdevice/irdevice.d.ts.map +1 -0
  92. package/dist/irdevice/irdevice.js +502 -0
  93. package/dist/irdevice/irdevice.js.map +1 -0
  94. package/dist/irdevice/light.d.ts +5 -34
  95. package/dist/irdevice/light.d.ts.map +1 -1
  96. package/dist/irdevice/light.js +13 -267
  97. package/dist/irdevice/light.js.map +1 -1
  98. package/dist/irdevice/other.d.ts +18 -48
  99. package/dist/irdevice/other.d.ts.map +1 -1
  100. package/dist/irdevice/other.js +349 -551
  101. package/dist/irdevice/other.js.map +1 -1
  102. package/dist/irdevice/tv.d.ts +7 -40
  103. package/dist/irdevice/tv.d.ts.map +1 -1
  104. package/dist/irdevice/tv.js +79 -325
  105. package/dist/irdevice/tv.js.map +1 -1
  106. package/dist/irdevice/vacuumcleaner.d.ts +6 -36
  107. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  108. package/dist/irdevice/vacuumcleaner.js +38 -288
  109. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  110. package/dist/irdevice/waterheater.d.ts +6 -36
  111. package/dist/irdevice/waterheater.d.ts.map +1 -1
  112. package/dist/irdevice/waterheater.js +39 -291
  113. package/dist/irdevice/waterheater.js.map +1 -1
  114. package/dist/settings.d.ts +15 -2
  115. package/dist/settings.d.ts.map +1 -1
  116. package/dist/utils.d.ts +68 -0
  117. package/dist/utils.d.ts.map +1 -1
  118. package/dist/utils.js +71 -0
  119. package/dist/utils.js.map +1 -1
  120. package/package.json +8 -8
  121. package/src/device/blindtilt.ts +356 -809
  122. package/src/device/bot.ts +601 -865
  123. package/src/device/ceilinglight.ts +182 -628
  124. package/src/device/colorbulb.ts +235 -683
  125. package/src/device/contact.ts +220 -575
  126. package/src/device/curtain.ts +344 -836
  127. package/src/device/device.ts +655 -0
  128. package/src/device/hub.ts +246 -636
  129. package/src/device/humidifier.ts +224 -645
  130. package/src/device/iosensor.ts +181 -625
  131. package/src/device/lightstrip.ts +230 -622
  132. package/src/device/lock.ts +236 -607
  133. package/src/device/meter.ts +177 -616
  134. package/src/device/meterplus.ts +175 -624
  135. package/src/device/motion.ts +147 -563
  136. package/src/device/plug.ts +81 -505
  137. package/src/device/robotvacuumcleaner.ts +139 -494
  138. package/src/device/waterdetector.ts +126 -506
  139. package/src/irdevice/airconditioner.ts +228 -353
  140. package/src/irdevice/airpurifier.ts +23 -282
  141. package/src/irdevice/camera.ts +18 -277
  142. package/src/irdevice/fan.ts +18 -275
  143. package/src/irdevice/irdevice.ts +518 -0
  144. package/src/irdevice/light.ts +18 -283
  145. package/src/irdevice/other.ts +394 -621
  146. package/src/irdevice/tv.ts +97 -344
  147. package/src/irdevice/vacuumcleaner.ts +46 -310
  148. package/src/irdevice/waterheater.ts +47 -315
  149. package/src/settings.ts +20 -3
  150. package/src/utils.ts +71 -0
@@ -1,102 +1,31 @@
1
+ /* Copyright(C) 2021-2024, donavanbecker (https://github.com/donavanbecker). All rights reserved.
2
+ *
3
+ * blindtilt.ts: @switchbot/homebridge-switchbot.
4
+ */
1
5
  import { request } from 'undici';
2
- import { sleep } from '../utils.js';
6
+ import { BlindTiltMappingMode } from '../utils.js';
3
7
  import { interval, Subject } from 'rxjs';
4
- import asyncmqtt from 'async-mqtt';
8
+ import { deviceBase } from './device.js';
5
9
  import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
6
10
  import { Devices } from '../settings.js';
7
- var BlindTiltMappingMode;
8
- (function (BlindTiltMappingMode) {
9
- BlindTiltMappingMode["OnlyUp"] = "only_up";
10
- BlindTiltMappingMode["OnlyDown"] = "only_down";
11
- BlindTiltMappingMode["DownAndUp"] = "down_and_up";
12
- BlindTiltMappingMode["UpAndDown"] = "up_and_down";
13
- BlindTiltMappingMode["UseTiltForDirection"] = "use_tilt_for_direction";
14
- })(BlindTiltMappingMode || (BlindTiltMappingMode = {}));
15
- export class BlindTilt {
11
+ export class BlindTilt extends deviceBase {
16
12
  platform;
17
- accessory;
18
- device;
19
- api;
20
- log;
21
- config;
22
- hap;
23
13
  // Services
24
- batteryService;
25
- lightSensorService;
26
- windowCoveringService;
27
- // Characteristic Values
28
- BatteryLevel;
29
- PositionState;
30
- TargetPosition;
31
- CurrentPosition;
32
- StatusLowBattery;
33
- FirmwareRevision;
34
- CurrentAmbientLightLevel;
35
- TargetHorizontalTiltAngle;
36
- CurrentHorizontalTiltAngle;
37
- // OpenAPI Status
38
- OpenAPI_InMotion;
39
- OpenAPI_BatteryLevel;
40
- OpenAPI_Direction;
41
- OpenAPI_Calibration;
42
- OpenAPI_CurrentPosition;
43
- OpenAPI_FirmwareRevision;
44
- OpenAPI_CurrentAmbientLightLevel;
14
+ WindowCovering;
15
+ Battery;
16
+ LightSensor;
45
17
  // OpenAPI Others
46
- Mode;
47
- setPositionMode;
48
18
  mappingMode = BlindTiltMappingMode.OnlyUp;
49
- // BLE Status
50
- BLE_InMotion;
51
- BLE_BatteryLevel;
52
- BLE_Calibration;
53
- BLE_CurrentPosition;
54
- BLE_CurrentAmbientLightLevel;
55
- // BLE Others
56
- BLE_IsConnected;
57
- spaceBetweenLevels;
58
19
  // Target
59
20
  setNewTarget;
60
21
  setNewTargetTimer;
61
- //MQTT stuff
62
- mqttClient = null;
63
- // Config
64
- set_minStep;
65
- updateRate;
66
- set_minLux;
67
- set_maxLux;
68
- scanDuration;
69
- deviceLogging;
70
- deviceRefreshRate;
71
- setCloseMode;
72
- setOpenMode;
73
- maxRetries;
74
- delayBetweenRetries;
75
22
  // Updates
76
23
  blindTiltUpdateInProgress;
77
24
  doBlindTiltUpdate;
78
- // Connection
79
- OpenAPI;
80
- BLE;
81
25
  constructor(platform, accessory, device) {
26
+ super(platform, accessory, device);
82
27
  this.platform = platform;
83
- this.accessory = accessory;
84
- this.device = device;
85
- this.api = this.platform.api;
86
- this.log = this.platform.log;
87
- this.config = this.platform.config;
88
- this.hap = this.api.hap;
89
- // Connection
90
- this.BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
91
- this.OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
92
28
  // default placeholders
93
- this.deviceLogs(device);
94
- this.refreshRate(device);
95
- this.scan(device);
96
- this.setupMqtt(device);
97
- this.deviceContext();
98
- this.deviceRetry(device);
99
- this.deviceConfig(device);
100
29
  this.mappingMode = device.blindTilt?.mode ?? BlindTiltMappingMode.OnlyUp;
101
30
  this.debugLog(`Mapping mode: ${this.mappingMode}`);
102
31
  // this is subject we use to track when we need to POST changes to the SwitchBot API
@@ -105,26 +34,37 @@ export class BlindTilt {
105
34
  this.setNewTarget = false;
106
35
  // Retrieve initial values and updateHomekit
107
36
  this.refreshStatus();
108
- // set accessory information
109
- accessory
110
- .getService(this.hap.Service.AccessoryInformation)
111
- .setCharacteristic(this.hap.Characteristic.Manufacturer, 'SwitchBot')
112
- .setCharacteristic(this.hap.Characteristic.Model, 'W2701600')
113
- .setCharacteristic(this.hap.Characteristic.SerialNumber, device.deviceId)
114
- .setCharacteristic(this.hap.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
37
+ // Initialize LightBulb property
38
+ this.WindowCovering = {
39
+ Service: this.accessory.addService(this.hap.Service.WindowCovering),
40
+ PositionState: this.accessory.context.PositionState || this.hap.Characteristic.PositionState.STOPPED,
41
+ TargetPosition: this.accessory.context.TargetPosition || 100,
42
+ CurrentPosition: this.accessory.context.CurrentPosition || 100,
43
+ TargetHorizontalTiltAngle: this.accessory.context.TargetHorizontalTiltAngle || 0,
44
+ CurrentHorizontalTiltAngle: this.accessory.context.CurrentHorizontalTiltAngle || 0,
45
+ };
46
+ // Initialize Battery property
47
+ this.Battery = {
48
+ Service: this.accessory.addService(this.hap.Service.Battery),
49
+ BatteryLevel: this.accessory.context.BatteryLevel || 100,
50
+ StatusLowBattery: this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL,
51
+ };
52
+ // Initialize LightSensor property
53
+ if (!this.device.blindTilt?.hide_lightsensor) {
54
+ this.LightSensor = {
55
+ Service: this.accessory.addService(this.hap.Service.LightSensor),
56
+ CurrentAmbientLightLevel: this.accessory.context.CurrentAmbientLightLevel || 0,
57
+ };
58
+ }
115
59
  // get the WindowCovering service if it exists, otherwise create a new WindowCovering service
116
60
  // you can create multiple services for each accessory
117
- const windowCoveringService = `${device.deviceName} ${device.deviceType}`;
118
- (this.windowCoveringService =
61
+ (this.WindowCovering.Service =
119
62
  accessory.getService(this.hap.Service.WindowCovering)
120
- || accessory.addService(this.hap.Service.WindowCovering)), windowCoveringService;
121
- this.windowCoveringService.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
122
- if (!this.windowCoveringService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
123
- this.windowCoveringService.addCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.displayName);
124
- }
63
+ || accessory.addService(this.hap.Service.WindowCovering)), accessory.displayName;
64
+ this.WindowCovering.Service.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
125
65
  // create handlers for required characteristics
126
- this.windowCoveringService.setCharacteristic(this.hap.Characteristic.PositionState, this.PositionState);
127
- this.windowCoveringService
66
+ this.WindowCovering.Service.setCharacteristic(this.hap.Characteristic.PositionState, this.WindowCovering.PositionState);
67
+ this.WindowCovering.Service
128
68
  .getCharacteristic(this.hap.Characteristic.CurrentPosition)
129
69
  .setProps({
130
70
  minStep: this.minStep(device),
@@ -133,9 +73,9 @@ export class BlindTilt {
133
73
  validValueRanges: [0, 100],
134
74
  })
135
75
  .onGet(() => {
136
- return this.CurrentPosition;
76
+ return this.WindowCovering?.CurrentPosition ?? 0;
137
77
  });
138
- this.windowCoveringService
78
+ this.WindowCovering.Service
139
79
  .getCharacteristic(this.hap.Characteristic.TargetPosition)
140
80
  .setProps({
141
81
  minStep: this.minStep(device),
@@ -144,8 +84,8 @@ export class BlindTilt {
144
84
  validValueRanges: [0, 100],
145
85
  })
146
86
  .onSet(this.TargetPositionSet.bind(this));
147
- this.CurrentHorizontalTiltAngle = 90;
148
- this.windowCoveringService
87
+ this.WindowCovering.CurrentHorizontalTiltAngle = 90;
88
+ this.WindowCovering.Service
149
89
  .getCharacteristic(this.hap.Characteristic.CurrentHorizontalTiltAngle)
150
90
  .setProps({
151
91
  minStep: 180,
@@ -154,11 +94,11 @@ export class BlindTilt {
154
94
  validValues: [-90, 90],
155
95
  })
156
96
  .onGet(() => {
157
- // this.debugLog(`requested CurrentHorizontalTiltAngle: ${this.CurrentHorizontalTiltAngle}`);
158
- return this.CurrentHorizontalTiltAngle;
97
+ // this.debugLog(`requested CurrentHorizontalTiltAngle: ${this.WindowCovering.CurrentHorizontalTiltAngle}`);
98
+ return this.WindowCovering.CurrentHorizontalTiltAngle ?? 0;
159
99
  });
160
- this.TargetHorizontalTiltAngle = 90;
161
- this.windowCoveringService
100
+ this.WindowCovering.TargetHorizontalTiltAngle = 90;
101
+ this.WindowCovering.Service
162
102
  .getCharacteristic(this.hap.Characteristic.TargetHorizontalTiltAngle)
163
103
  .setProps({
164
104
  minStep: 180,
@@ -169,12 +109,9 @@ export class BlindTilt {
169
109
  .onSet(this.TargetHorizontalTiltAngleSet.bind(this));
170
110
  // Battery Service
171
111
  const batteryService = `${accessory.displayName} Battery`;
172
- (this.batteryService = this.accessory.getService(this.hap.Service.Battery)
112
+ (this.Battery.Service = this.accessory.getService(this.hap.Service.Battery)
173
113
  || accessory.addService(this.hap.Service.Battery)), batteryService;
174
- this.batteryService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Battery`);
175
- if (!this.batteryService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
176
- this.batteryService.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
177
- }
114
+ this.Battery.Service.setCharacteristic(this.hap.Characteristic.Name, batteryService);
178
115
  // Update Homekit
179
116
  this.updateHomeKitCharacteristics();
180
117
  // Start an update interval
@@ -188,17 +125,17 @@ export class BlindTilt {
188
125
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} is listening webhook.`);
189
126
  this.platform.webhookEventHandler[this.device.deviceId] = async (context) => {
190
127
  try {
191
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
192
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Please Submit Logs: ` + 'https://tinyurl.com/SwitchBotBug');
193
- /*const { temperature, humidity } = context;
194
- const { CurrentTemperature, CurrentRelativeHumidity } = this;
195
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
196
- '(temperature, humidity) = ' +
197
- `Webhook:(${temperature}, ${humidity}), ` +
198
- `current:(${CurrentTemperature}, ${CurrentRelativeHumidity})`);
199
- this.CurrentRelativeHumidity = humidity;
200
- this.CurrentTemperature = temperature;
201
- this.updateHomeKitCharacteristics();*/
128
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
129
+ const { slidePosition, battery } = context;
130
+ const { CurrentPosition } = this.WindowCovering;
131
+ const { BatteryLevel } = this.Battery;
132
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
133
+ '(slidePosition, battery) = ' +
134
+ `Webhook:(${slidePosition}, ${battery}), ` +
135
+ `current:(${CurrentPosition}, ${BatteryLevel})`);
136
+ this.WindowCovering.CurrentPosition = slidePosition;
137
+ this.Battery.BatteryLevel = battery;
138
+ this.updateHomeKitCharacteristics();
202
139
  }
203
140
  catch (e) {
204
141
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
@@ -207,13 +144,14 @@ export class BlindTilt {
207
144
  };
208
145
  }
209
146
  // update slide progress
210
- interval(this.updateRate * 1000)
147
+ interval(this.deviceUpdateRate * 1000)
211
148
  //.pipe(skipWhile(() => this.blindTiltUpdateInProgress))
212
149
  .subscribe(async () => {
213
- if (this.PositionState === this.hap.Characteristic.PositionState.STOPPED) {
150
+ if (this.WindowCovering.PositionState === this.hap.Characteristic.PositionState.STOPPED) {
214
151
  return;
215
152
  }
216
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Refresh Status When Moving, PositionState: ${this.PositionState}`);
153
+ this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Refresh Status When Moving,`
154
+ + ` PositionState: ${this.WindowCovering.PositionState}`);
217
155
  await this.refreshStatus();
218
156
  });
219
157
  // Watch for BlindTilt change events
@@ -235,199 +173,199 @@ export class BlindTilt {
235
173
  });
236
174
  }
237
175
  /**
238
- * Parse the device status from the SwitchBot api
176
+ * Parse the device status from the SwitchBotBLE API
239
177
  */
240
- async parseStatus() {
241
- if (!this.device.enableCloudService && this.OpenAPI) {
242
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} parseStatus enableCloudService: ${this.device.enableCloudService}`);
243
- }
244
- else if (this.BLE) {
245
- await this.BLEparseStatus();
246
- }
247
- else if (this.OpenAPI && this.platform.config.credentials?.token) {
248
- await this.openAPIparseStatus();
249
- }
250
- else {
251
- await this.offlineOff();
252
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:` + ` ${this.device.connectionType}, parseStatus will not happen.`);
253
- }
254
- }
255
- async BLEparseStatus() {
178
+ async BLEparseStatus(serviceData) {
256
179
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
257
180
  // CurrentPosition
258
- this.CurrentPosition = 100 - Number(this.BLE_CurrentPosition);
181
+ this.WindowCovering.CurrentPosition = 100 - Number(serviceData.position);
259
182
  await this.setMinMax();
260
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentPosition ${this.CurrentPosition}`);
183
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentPosition ${this.WindowCovering.CurrentPosition}`);
261
184
  if (this.setNewTarget) {
262
185
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Checking Status ...`);
263
186
  }
264
- if (this.setNewTarget && this.BLE_InMotion) {
187
+ if (this.setNewTarget && serviceData.inMotion) {
265
188
  await this.setMinMax();
266
- if (Number(this.TargetPosition) > this.CurrentPosition) {
267
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Closing, CurrentPosition: ${this.CurrentPosition}`);
268
- this.PositionState = this.hap.Characteristic.PositionState.INCREASING;
269
- this.windowCoveringService.getCharacteristic(this.hap.Characteristic.PositionState).updateValue(this.PositionState);
270
- this.debugLog(`${this.device.deviceType}: ${this.CurrentPosition} INCREASING PositionState: ${this.PositionState}`);
271
- }
272
- else if (Number(this.TargetPosition) < this.CurrentPosition) {
273
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Opening, CurrentPosition: ${this.CurrentPosition}`);
274
- this.PositionState = this.hap.Characteristic.PositionState.DECREASING;
275
- this.windowCoveringService.getCharacteristic(this.hap.Characteristic.PositionState).updateValue(this.PositionState);
276
- this.debugLog(`${this.device.deviceType}: ${this.CurrentPosition} DECREASING PositionState: ${this.PositionState}`);
189
+ if (Number(this.WindowCovering.TargetPosition) > this.WindowCovering.CurrentPosition) {
190
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Closing, CurrentPosition: ${this.WindowCovering.CurrentPosition}`);
191
+ this.WindowCovering.PositionState = this.hap.Characteristic.PositionState.INCREASING;
192
+ this.WindowCovering.Service.getCharacteristic(this.hap.Characteristic.PositionState).updateValue(this.WindowCovering.PositionState);
193
+ this.debugLog(`${this.device.deviceType}: ${this.WindowCovering.CurrentPosition} INCREASING`
194
+ + ` PositionState: ${this.WindowCovering.PositionState}`);
195
+ }
196
+ else if (Number(this.WindowCovering.TargetPosition) < this.WindowCovering.CurrentPosition) {
197
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Opening, CurrentPosition: ${this.WindowCovering.CurrentPosition}`);
198
+ this.WindowCovering.PositionState = this.hap.Characteristic.PositionState.DECREASING;
199
+ this.WindowCovering.Service.getCharacteristic(this.hap.Characteristic.PositionState).updateValue(this.WindowCovering.PositionState);
200
+ this.debugLog(`${this.device.deviceType}: ${this.WindowCovering.CurrentPosition} DECREASING`
201
+ + ` PositionState: ${this.WindowCovering.PositionState}`);
277
202
  }
278
203
  else {
279
- this.debugLog(`${this.device.deviceType}: ${this.CurrentPosition} Standby2, CurrentPosition: ${this.CurrentPosition}`);
280
- this.PositionState = this.hap.Characteristic.PositionState.STOPPED;
281
- this.windowCoveringService.getCharacteristic(this.hap.Characteristic.PositionState).updateValue(this.PositionState);
282
- this.debugLog(`${this.device.deviceType}: ${this.CurrentPosition} STOPPED PositionState: ${this.PositionState}`);
204
+ this.debugLog(`${this.device.deviceType}: ${this.WindowCovering.CurrentPosition} Standby2,`
205
+ + ` CurrentPosition: ${this.WindowCovering.CurrentPosition}`);
206
+ this.WindowCovering.PositionState = this.hap.Characteristic.PositionState.STOPPED;
207
+ this.WindowCovering.Service.getCharacteristic(this.hap.Characteristic.PositionState).updateValue(this.WindowCovering.PositionState);
208
+ this.debugLog(`${this.device.deviceType}: ${this.WindowCovering.CurrentPosition} STOPPED`
209
+ + ` PositionState: ${this.WindowCovering.PositionState}`);
283
210
  }
284
211
  }
285
212
  else {
286
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Standby, CurrentPosition: ${this.CurrentPosition}`);
287
- this.TargetPosition = this.CurrentPosition;
288
- this.PositionState = this.hap.Characteristic.PositionState.STOPPED;
213
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Standby, CurrentPosition: ${this.WindowCovering.CurrentPosition}`);
214
+ this.WindowCovering.TargetPosition = this.WindowCovering.CurrentPosition;
215
+ this.WindowCovering.PositionState = this.hap.Characteristic.PositionState.STOPPED;
289
216
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Stopped`);
290
217
  }
291
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentPosition: ${this.CurrentPosition},` +
292
- ` TargetPosition: ${this.TargetPosition}, PositionState: ${this.PositionState},`);
218
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentPosition: ${this.WindowCovering.CurrentPosition},` +
219
+ ` TargetPosition: ${this.WindowCovering.TargetPosition}, PositionState: ${this.WindowCovering.PositionState},`);
293
220
  if (!this.device.blindTilt?.hide_lightsensor) {
294
- this.set_minLux = this.minLux();
295
- this.set_maxLux = this.maxLux();
296
- this.spaceBetweenLevels = 9;
221
+ const set_minLux = await this.minLux();
222
+ const set_maxLux = await this.maxLux();
223
+ const spaceBetweenLevels = 9;
297
224
  // Brightness
298
- switch (this.BLE_CurrentAmbientLightLevel) {
225
+ switch (serviceData.lightLevel) {
299
226
  case 1:
300
- this.CurrentAmbientLightLevel = this.set_minLux;
301
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${this.BLE_CurrentAmbientLightLevel}`);
227
+ this.LightSensor.CurrentAmbientLightLevel = set_minLux;
228
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${serviceData.lightLevel}`);
302
229
  break;
303
230
  case 2:
304
- this.CurrentAmbientLightLevel = (this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels;
305
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${this.BLE_CurrentAmbientLightLevel},` +
306
- ` Calculation: ${(this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels}`);
231
+ this.LightSensor.CurrentAmbientLightLevel = (set_maxLux - set_minLux) / spaceBetweenLevels;
232
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${serviceData.lightLevel},` +
233
+ ` Calculation: ${(set_maxLux - set_minLux) / spaceBetweenLevels}`);
307
234
  break;
308
235
  case 3:
309
- this.CurrentAmbientLightLevel = ((this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels) * 2;
310
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${this.BLE_CurrentAmbientLightLevel}`);
236
+ this.LightSensor.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 2;
237
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${serviceData.lightLevel}`);
311
238
  break;
312
239
  case 4:
313
- this.CurrentAmbientLightLevel = ((this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels) * 3;
314
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${this.BLE_CurrentAmbientLightLevel}`);
240
+ this.LightSensor.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 3;
241
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${serviceData.lightLevel}`);
315
242
  break;
316
243
  case 5:
317
- this.CurrentAmbientLightLevel = ((this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels) * 4;
318
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${this.BLE_CurrentAmbientLightLevel}`);
244
+ this.LightSensor.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 4;
245
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${serviceData.lightLevel}`);
319
246
  break;
320
247
  case 6:
321
- this.CurrentAmbientLightLevel = ((this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels) * 5;
322
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${this.BLE_CurrentAmbientLightLevel}`);
248
+ this.LightSensor.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 5;
249
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${serviceData.lightLevel}`);
323
250
  break;
324
251
  case 7:
325
- this.CurrentAmbientLightLevel = ((this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels) * 6;
326
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${this.BLE_CurrentAmbientLightLevel}`);
252
+ this.LightSensor.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 6;
253
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${serviceData.lightLevel}`);
327
254
  break;
328
255
  case 8:
329
- this.CurrentAmbientLightLevel = ((this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels) * 7;
330
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${this.BLE_CurrentAmbientLightLevel}`);
256
+ this.LightSensor.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 7;
257
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${serviceData.lightLevel}`);
331
258
  break;
332
259
  case 9:
333
- this.CurrentAmbientLightLevel = ((this.set_maxLux - this.set_minLux) / this.spaceBetweenLevels) * 8;
334
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${this.BLE_CurrentAmbientLightLevel}`);
260
+ this.LightSensor.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 8;
261
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${serviceData.lightLevel}`);
335
262
  break;
336
263
  case 10:
337
264
  default:
338
- this.CurrentAmbientLightLevel = this.set_maxLux;
265
+ this.LightSensor.CurrentAmbientLightLevel = set_maxLux;
339
266
  this.debugLog();
340
267
  }
341
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${this.BLE_CurrentAmbientLightLevel},` +
342
- ` CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
268
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${serviceData.lightLevel},` +
269
+ ` CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
343
270
  }
344
271
  // Battery
345
- this.BatteryLevel = Number(this.BLE_BatteryLevel);
346
- if (this.BatteryLevel < 10) {
347
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
272
+ this.Battery.BatteryLevel = Number(serviceData.battery);
273
+ if (this.Battery.BatteryLevel < 10) {
274
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
348
275
  }
349
276
  else {
350
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
277
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
351
278
  }
352
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel},` + ` StatusLowBattery: ${this.StatusLowBattery}`);
279
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel},`
280
+ + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
353
281
  }
354
- async openAPIparseStatus() {
282
+ /**
283
+ * Parse the device status from the SwitchBot OpenAPI
284
+ */
285
+ async openAPIparseStatus(deviceStatus) {
355
286
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
356
- const [homekitPosition, homekitTiltAngle] = this.mapDeviceValuesToHomekitValues(Number(this.OpenAPI_CurrentPosition), String(this.OpenAPI_Direction));
357
- this.debugLog(` device: ${this.OpenAPI_CurrentPosition} => HK: ${homekitPosition}`);
358
- this.CurrentPosition = homekitPosition;
287
+ const [homekitPosition, homekitTiltAngle] = this.mapDeviceValuesToHomekitValues(Number(deviceStatus.body.slidePosition), String(deviceStatus.body.direction));
288
+ this.debugLog(` device: ${deviceStatus.body.slidePosition} => HK: ${homekitPosition}`);
289
+ this.WindowCovering.CurrentPosition = homekitPosition;
359
290
  // CurrentPosition
360
291
  await this.setMinMax();
361
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentPosition: ${this.CurrentPosition}`);
292
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentPosition: ${this.WindowCovering.CurrentPosition}`);
362
293
  if (homekitTiltAngle) {
363
- this.CurrentHorizontalTiltAngle = homekitTiltAngle;
364
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentHorizontalTiltAngle: ${this.CurrentHorizontalTiltAngle}`);
294
+ this.WindowCovering.CurrentHorizontalTiltAngle = homekitTiltAngle;
295
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
296
+ + ` CurrentHorizontalTiltAngle: ${this.WindowCovering.CurrentHorizontalTiltAngle}`);
365
297
  }
366
298
  if (this.setNewTarget) {
367
299
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Checking Status ...`);
368
300
  }
369
- if (this.setNewTarget && this.OpenAPI_InMotion) {
301
+ if (this.setNewTarget && deviceStatus.body.moving) {
370
302
  await this.setMinMax();
371
- if (this.TargetPosition > this.CurrentPosition || (homekitTiltAngle && this.TargetHorizontalTiltAngle !== this.CurrentHorizontalTiltAngle)) {
372
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Closing, CurrentPosition: ${this.CurrentPosition} `);
373
- this.PositionState = this.hap.Characteristic.PositionState.INCREASING;
374
- this.windowCoveringService.getCharacteristic(this.hap.Characteristic.PositionState).updateValue(this.PositionState);
375
- this.debugLog(`${this.device.deviceType}: ${this.CurrentPosition} INCREASING PositionState: ${this.PositionState}`);
376
- }
377
- else if (this.TargetPosition < this.CurrentPosition) {
378
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Opening, CurrentPosition: ${this.CurrentPosition} `);
379
- this.PositionState = this.hap.Characteristic.PositionState.DECREASING;
380
- this.windowCoveringService.getCharacteristic(this.hap.Characteristic.PositionState).updateValue(this.PositionState);
381
- this.debugLog(`${this.device.deviceType}: ${this.CurrentPosition} DECREASING PositionState: ${this.PositionState}`);
303
+ if (this.WindowCovering.TargetPosition > this.WindowCovering.CurrentPosition
304
+ || (homekitTiltAngle && this.WindowCovering.TargetHorizontalTiltAngle !== this.WindowCovering.CurrentHorizontalTiltAngle)) {
305
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Closing, CurrentPosition: ${this.WindowCovering.CurrentPosition} `);
306
+ this.WindowCovering.PositionState = this.hap.Characteristic.PositionState.INCREASING;
307
+ this.WindowCovering.Service.getCharacteristic(this.hap.Characteristic.PositionState).updateValue(this.WindowCovering.PositionState);
308
+ this.debugLog(`${this.device.deviceType}: ${this.WindowCovering.CurrentPosition} INCREASING`
309
+ + ` PositionState: ${this.WindowCovering.PositionState}`);
310
+ }
311
+ else if (this.WindowCovering.TargetPosition < this.WindowCovering.CurrentPosition) {
312
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Opening, CurrentPosition: ${this.WindowCovering.CurrentPosition} `);
313
+ this.WindowCovering.PositionState = this.hap.Characteristic.PositionState.DECREASING;
314
+ this.WindowCovering.Service.getCharacteristic(this.hap.Characteristic.PositionState).updateValue(this.WindowCovering.PositionState);
315
+ this.debugLog(`${this.device.deviceType}: ${this.WindowCovering.CurrentPosition} DECREASING`
316
+ + ` PositionState: ${this.WindowCovering.PositionState}`);
382
317
  }
383
318
  else {
384
- this.debugLog(`${this.device.deviceType}: ${this.CurrentPosition} Standby because reached position,` + ` CurrentPosition: ${this.CurrentPosition}`);
385
- this.PositionState = this.hap.Characteristic.PositionState.STOPPED;
386
- this.windowCoveringService.getCharacteristic(this.hap.Characteristic.PositionState).updateValue(this.PositionState);
387
- this.debugLog(`${this.device.deviceType}: ${this.CurrentPosition} STOPPED PositionState: ${this.PositionState}`);
319
+ this.debugLog(`${this.device.deviceType}: ${this.WindowCovering.CurrentPosition} Standby because reached position,`
320
+ + ` CurrentPosition: ${this.WindowCovering.CurrentPosition}`);
321
+ this.WindowCovering.PositionState = this.hap.Characteristic.PositionState.STOPPED;
322
+ this.WindowCovering.Service.getCharacteristic(this.hap.Characteristic.PositionState).updateValue(this.WindowCovering.PositionState);
323
+ this.debugLog(`${this.device.deviceType}: ${this.WindowCovering.CurrentPosition} STOPPED`
324
+ + ` PositionState: ${this.WindowCovering.PositionState}`);
388
325
  }
389
326
  }
390
327
  else {
391
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Standby because device not moving,` + ` CurrentPosition: ${this.CurrentPosition}`);
392
- this.TargetPosition = this.CurrentPosition;
328
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Standby because device not moving,`
329
+ + ` CurrentPosition: ${this.WindowCovering.CurrentPosition}`);
330
+ this.WindowCovering.TargetPosition = this.WindowCovering.CurrentPosition;
393
331
  if (homekitTiltAngle) {
394
- this.TargetHorizontalTiltAngle = this.CurrentHorizontalTiltAngle;
332
+ this.WindowCovering.TargetHorizontalTiltAngle = this.WindowCovering.CurrentHorizontalTiltAngle;
395
333
  }
396
- this.PositionState = this.hap.Characteristic.PositionState.STOPPED;
334
+ this.WindowCovering.PositionState = this.hap.Characteristic.PositionState.STOPPED;
397
335
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Stopped`);
398
336
  }
399
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentPosition: ${this.CurrentPosition},` +
400
- ` TargetPosition: ${this.TargetPosition}, PositionState: ${this.PositionState},`);
337
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentPosition: ${this.WindowCovering.CurrentPosition},` +
338
+ ` TargetPosition: ${this.WindowCovering.TargetPosition}, PositionState: ${this.WindowCovering.PositionState},`);
401
339
  if (!this.device.blindTilt?.hide_lightsensor) {
402
- this.set_minLux = this.minLux();
403
- this.set_maxLux = this.maxLux();
340
+ const set_minLux = await this.minLux();
341
+ const set_maxLux = await this.maxLux();
404
342
  // Brightness
405
- switch (this.OpenAPI_CurrentAmbientLightLevel) {
343
+ switch (deviceStatus.body.brightness) {
406
344
  case 'dim':
407
- this.CurrentAmbientLightLevel = this.set_minLux;
345
+ this.LightSensor.CurrentAmbientLightLevel = set_minLux;
408
346
  break;
409
347
  case 'bright':
410
348
  default:
411
- this.CurrentAmbientLightLevel = this.set_maxLux;
349
+ this.LightSensor.CurrentAmbientLightLevel = set_maxLux;
412
350
  }
413
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
351
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
352
+ + ` CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
414
353
  }
415
354
  // BatteryLevel
416
- this.BatteryLevel = Number(this.OpenAPI_BatteryLevel);
417
- if (this.BatteryLevel < 10) {
418
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
355
+ this.Battery.BatteryLevel = Number(deviceStatus.body.battery);
356
+ if (this.Battery.BatteryLevel < 10) {
357
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
419
358
  }
420
359
  else {
421
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
360
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
422
361
  }
423
- if (Number.isNaN(this.BatteryLevel)) {
424
- this.BatteryLevel = 100;
362
+ if (Number.isNaN(this.Battery.BatteryLevel)) {
363
+ this.Battery.BatteryLevel = 100;
425
364
  }
426
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel},`
427
- + ` StatusLowBattery: ${this.StatusLowBattery}`);
365
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel},`
366
+ + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
428
367
  // FirmwareRevision
429
- this.FirmwareRevision = this.OpenAPI_FirmwareRevision;
430
- this.accessory.context.FirmwareRevision = this.FirmwareRevision;
368
+ this.accessory.context.FirmwareRevision = deviceStatus.body.version;
431
369
  }
432
370
  async refreshStatus() {
433
371
  if (!this.device.enableCloudService && this.OpenAPI) {
@@ -458,21 +396,13 @@ export class BlindTilt {
458
396
  // Start to monitor advertisement packets
459
397
  (async () => {
460
398
  // Start to monitor advertisement packets
461
- await switchbot.startScan({
462
- model: 'x',
463
- id: this.device.bleMac,
464
- });
399
+ await switchbot.startScan({ model: this.device.bleModel, id: this.device.bleMac });
465
400
  // Set an event handler
466
401
  switchbot.onadvertisement = (ad) => {
467
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
468
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.model}`);
469
- if (this.device.bleMac === ad.address && ad.model === 'x') {
402
+ if (this.device.bleMac === ad.address && ad.model === this.device.bleModel) {
403
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
404
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.model}`);
470
405
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
471
- this.BLE_Calibration = ad.serviceData.calibration;
472
- this.BLE_BatteryLevel = ad.serviceData.battery;
473
- this.BLE_InMotion = ad.serviceData.inMotion;
474
- this.BLE_CurrentPosition = ad.serviceData.position;
475
- this.BLE_CurrentAmbientLightLevel = ad.serviceData.lightLevel;
476
406
  }
477
407
  else {
478
408
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
@@ -483,68 +413,17 @@ export class BlindTilt {
483
413
  // Stop to monitor
484
414
  await switchbot.stopScan();
485
415
  // Update HomeKit
486
- await this.BLEparseStatus();
416
+ await this.BLEparseStatus(switchbot.onadvertisement.serviceData);
487
417
  await this.updateHomeKitCharacteristics();
488
418
  })();
489
- /*if (switchbot !== false) {
490
- switchbot
491
- .startScan({
492
- model: 'c',
493
- id: this.device.bleMac,
494
- })
495
- .then(async () => {
496
- // Set an event handler
497
- switchbot.onadvertisement = async (ad: ad) => {
498
- this.debugLog(
499
- `${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},` +
500
- ` BLE Address Found: ${ad.address}`,
501
- );
502
- this.BLE_Calibration = ad.serviceData.calibration;
503
- this.BLE_BatteryLevel = ad.serviceData.battery;
504
- this.BLE_InMotion = ad.serviceData.inMotion;
505
- this.BLE_CurrentPosition = ad.serviceData.position;
506
- this.BLE_CurrentAmbientLightLevel = ad.serviceData.lightLevel;
507
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
508
- this.debugLog(
509
- `${this.device.deviceType}: ${this.accessory.displayName} calibration: ${ad.serviceData.calibration}, ` +
510
- `position: ${ad.serviceData.position}, lightLevel: ${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}, ` +
511
- `inMotion: ${ad.serviceData.inMotion}`,
512
- );
513
-
514
- if (ad.serviceData) {
515
- this.BLE_IsConnected = true;
516
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.BLE_IsConnected}`);
517
- await this.stopScanning(switchbot);
518
- } else {
519
- this.BLE_IsConnected = false;
520
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.BLE_IsConnected}`);
521
- }
522
- };
523
- // Wait
524
- return await sleep(this.scanDuration * 1000);
525
- })
526
- .then(async () => {
527
- // Stop to monitor
528
- await this.stopScanning(switchbot);
529
- })
530
- .catch(async (e: any) => {
531
- this.apiError(e);
532
- this.errorLog(
533
- `${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}` +
534
- ` Connection, Error Message: ${JSON.stringify(e.message)}`,
535
- );
536
- await this.BLERefreshConnection(switchbot);
537
- });
538
- } else {
539
- await this.BLERefreshConnection(switchbot);
540
- }*/
419
+ if (switchbot === undefined) {
420
+ await this.BLERefreshConnection(switchbot);
421
+ }
541
422
  }
542
423
  async openAPIRefreshStatus() {
543
424
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
544
425
  try {
545
- const { body, statusCode } = await this.platform.retryRequest(this.maxRetries, this.delayBetweenRetries, `${Devices}/${this.device.deviceId}/status`, {
546
- headers: this.platform.generateHeaders(),
547
- });
426
+ const { body, statusCode } = await this.platform.retryRequest(this.deviceMaxRetries, this.deviceDelayBetweenRetries, `${Devices}/${this.device.deviceId}/status`, { headers: this.platform.generateHeaders() });
548
427
  this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
549
428
  const deviceStatus = await body.json();
550
429
  this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
@@ -552,13 +431,7 @@ export class BlindTilt {
552
431
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
553
432
  this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
554
433
  + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
555
- this.OpenAPI_CurrentPosition = deviceStatus.body.slidePosition;
556
- this.OpenAPI_Direction = deviceStatus.body.direction;
557
- this.OpenAPI_InMotion = deviceStatus.body.moving;
558
- this.OpenAPI_CurrentAmbientLightLevel = deviceStatus.body.brightness;
559
- this.OpenAPI_BatteryLevel = deviceStatus.body.battery;
560
- this.OpenAPI_FirmwareRevision = deviceStatus.body.version;
561
- this.openAPIparseStatus();
434
+ this.openAPIparseStatus(deviceStatus);
562
435
  this.updateHomeKitCharacteristics();
563
436
  }
564
437
  else {
@@ -596,7 +469,7 @@ export class BlindTilt {
596
469
  }
597
470
  async BLEpushChanges() {
598
471
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`);
599
- if (this.TargetPosition !== this.CurrentPosition) {
472
+ if (this.WindowCovering.TargetPosition !== this.WindowCovering.CurrentPosition) {
600
473
  const switchbot = await this.platform.connectBLE();
601
474
  // Convert to BLE Address
602
475
  this.device.bleMac = this.device
@@ -604,28 +477,24 @@ export class BlindTilt {
604
477
  .join(':')
605
478
  .toLowerCase();
606
479
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
607
- this.SilentPerformance();
608
- const adjustedMode = this.setPositionMode || null;
609
- if (adjustedMode === null) {
610
- this.Mode = 'Default Mode';
611
- }
612
- this.debugLog(`${this.accessory.displayName} Mode: ${this.Mode}`);
480
+ const { setPositionMode, Mode } = await this.setPerformance();
481
+ this.debugLog(`${this.accessory.displayName} Mode: ${Mode}`);
613
482
  if (switchbot !== false) {
614
483
  switchbot
615
484
  .discover({ model: 'c', quick: true, id: this.device.bleMac })
616
485
  .then(async (device_list) => {
617
- this.infoLog(`${this.accessory.displayName} Target Position: ${this.TargetPosition}`);
618
- return await this.retry({
619
- max: this.maxRetry(),
486
+ this.infoLog(`${this.accessory.displayName} Target Position: ${this.WindowCovering.TargetPosition}`);
487
+ return await this.retryBLE({
488
+ max: await this.maxRetryBLE(),
620
489
  fn: async () => {
621
- return await device_list[0].runToPos(100 - Number(this.TargetPosition), adjustedMode);
490
+ return await device_list[0].runToPos(100 - Number(this.WindowCovering.TargetPosition), setPositionMode);
622
491
  },
623
492
  });
624
493
  })
625
494
  .then(() => {
626
495
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
627
496
  this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
628
- + `Target Position: ${this.TargetPosition} sent over BLE, sent successfully`);
497
+ + `Target Position: ${this.WindowCovering.TargetPosition} sent over BLE, sent successfully`);
629
498
  })
630
499
  .catch(async (e) => {
631
500
  this.apiError(e);
@@ -641,35 +510,20 @@ export class BlindTilt {
641
510
  }
642
511
  else {
643
512
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges, CurrentPosition & TargetPosition Are the Same.` +
644
- ` CurrentPosition: ${this.CurrentPosition}, TargetPosition ${this.TargetPosition}`);
645
- }
646
- }
647
- async retry({ max, fn }) {
648
- return fn().catch(async (e) => {
649
- if (max === 0) {
650
- throw e;
651
- }
652
- this.infoLog(e);
653
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Retrying`);
654
- await sleep(1000);
655
- return this.retry({ max: max - 1, fn });
656
- });
657
- }
658
- maxRetry() {
659
- if (this.device.maxRetry) {
660
- return this.device.maxRetry;
661
- }
662
- else {
663
- return 5;
513
+ ` CurrentPosition: ${this.WindowCovering.CurrentPosition}, TargetPosition ${this.WindowCovering.TargetPosition}`);
664
514
  }
665
515
  }
666
516
  async openAPIpushChanges() {
667
517
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
668
- const hasDifferentAndRelevantHorizontalTiltAngle = this.mappingMode === BlindTiltMappingMode.UseTiltForDirection && this.TargetHorizontalTiltAngle !== this.CurrentHorizontalTiltAngle;
669
- if (this.TargetPosition !== this.CurrentPosition || hasDifferentAndRelevantHorizontalTiltAngle || this.device.disableCaching) {
670
- const [direction, position] = this.mapHomekitValuesToDeviceValues(Number(this.TargetPosition), Number(this.TargetHorizontalTiltAngle));
671
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Pushing ${this.TargetPosition} (device = ${direction};${position})`);
672
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Mode: ${this.Mode}`);
518
+ const hasDifferentAndRelevantHorizontalTiltAngle = this.mappingMode === BlindTiltMappingMode.UseTiltForDirection
519
+ && this.WindowCovering.TargetHorizontalTiltAngle !== this.WindowCovering.CurrentHorizontalTiltAngle;
520
+ if (this.WindowCovering.TargetPosition !== this.WindowCovering.CurrentPosition
521
+ || hasDifferentAndRelevantHorizontalTiltAngle || this.device.disableCaching) {
522
+ const [direction, position] = this.mapHomekitValuesToDeviceValues(Number(this.WindowCovering.TargetPosition), Number(this.WindowCovering.TargetHorizontalTiltAngle));
523
+ const { Mode } = await this.setPerformance();
524
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
525
+ + ` Pushing ${this.WindowCovering.TargetPosition} (device = ${direction};${position})`);
526
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Mode: ${Mode}`);
673
527
  let bodyChange = '';
674
528
  if (position === 100) {
675
529
  bodyChange = JSON.stringify({
@@ -715,30 +569,31 @@ export class BlindTilt {
715
569
  }
716
570
  catch (e) {
717
571
  this.apiError(e);
718
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}` +
719
- ` Connection, Error Message: ${JSON.stringify(e.message)}`);
572
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
573
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
720
574
  }
721
575
  }
722
576
  else {
723
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No OpenAPI Changes, CurrentPosition & TargetPosition Are the Same.` +
724
- ` CurrentPosition: ${this.CurrentPosition}, TargetPosition ${this.TargetPosition}` +
725
- ` CurrentHorizontalTiltAngle: ${this.CurrentHorizontalTiltAngle}, TargetPosition ${this.TargetHorizontalTiltAngle}`);
577
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No OpenAPI Changes, CurrentPosition & TargetPosition Are the Same.`
578
+ + ` CurrentPosition: ${this.WindowCovering.CurrentPosition}, TargetPosition ${this.WindowCovering.TargetPosition}`
579
+ + ` CurrentHorizontalTiltAngle: ${this.WindowCovering.CurrentHorizontalTiltAngle},`
580
+ + ` TargetPosition ${this.WindowCovering.TargetHorizontalTiltAngle}`);
726
581
  }
727
582
  }
728
583
  /**
729
584
  * Handle requests to set the value of the "Target Horizontal Tilt" characteristic
730
585
  */
731
586
  async TargetHorizontalTiltAngleSet(value) {
732
- if (this.TargetHorizontalTiltAngle === this.accessory.context.TargetHorizontalTiltAngle) {
587
+ if (this.WindowCovering.TargetHorizontalTiltAngle === this.accessory.context.TargetHorizontalTiltAngle) {
733
588
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set TargetHorizontalTiltAngle: ${value}`);
734
589
  }
735
590
  else {
736
591
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set TargetHorizontalTiltAngle: ${value}`);
737
592
  }
738
593
  //value = value < 0 ? -90 : 90;
739
- this.TargetHorizontalTiltAngle = value;
594
+ this.WindowCovering.TargetHorizontalTiltAngle = value;
740
595
  if (this.device.mqttURL) {
741
- this.mqttPublish('TargetHorizontalTiltAngle', this.TargetHorizontalTiltAngle);
596
+ this.mqttPublish('TargetHorizontalTiltAngle', this.WindowCovering.TargetHorizontalTiltAngle.toString());
742
597
  }
743
598
  this.startUpdatingBlindTiltIfNeeded();
744
599
  }
@@ -746,49 +601,53 @@ export class BlindTilt {
746
601
  * Handle requests to set the value of the "Target Position" characteristic
747
602
  */
748
603
  async TargetPositionSet(value) {
749
- if (this.TargetPosition === this.accessory.context.TargetPosition) {
604
+ if (this.WindowCovering.TargetPosition === this.accessory.context.TargetPosition) {
750
605
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set TargetPosition: ${value}`);
751
606
  }
752
607
  else {
753
608
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set TargetPosition: ${value}`);
754
609
  }
755
- this.TargetPosition = value;
610
+ this.WindowCovering.TargetPosition = value;
756
611
  if (this.device.mqttURL) {
757
- this.mqttPublish('TargetPosition', this.TargetPosition);
612
+ this.mqttPublish('TargetPosition', this.WindowCovering.TargetPosition.toString());
758
613
  }
759
614
  this.startUpdatingBlindTiltIfNeeded();
760
615
  }
761
616
  async startUpdatingBlindTiltIfNeeded() {
762
617
  await this.setMinMax();
763
618
  this.debugLog('setMinMax');
764
- if (this.TargetPosition > this.CurrentPosition || this.TargetHorizontalTiltAngle !== this.CurrentHorizontalTiltAngle) {
765
- this.PositionState = this.hap.Characteristic.PositionState.INCREASING;
619
+ if (this.WindowCovering.TargetPosition > this.WindowCovering.CurrentPosition
620
+ || this.WindowCovering.TargetHorizontalTiltAngle !== this.WindowCovering.CurrentHorizontalTiltAngle) {
621
+ this.WindowCovering.PositionState = this.hap.Characteristic.PositionState.INCREASING;
766
622
  this.setNewTarget = true;
767
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} value: ${this.CurrentPosition},` + ` CurrentPosition: ${this.CurrentPosition}`);
623
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} value: ${this.WindowCovering.CurrentPosition},`
624
+ + ` CurrentPosition: ${this.WindowCovering.CurrentPosition}`);
768
625
  }
769
- else if (this.TargetPosition < this.CurrentPosition) {
770
- this.PositionState = this.hap.Characteristic.PositionState.DECREASING;
626
+ else if (this.WindowCovering.TargetPosition < this.WindowCovering.CurrentPosition) {
627
+ this.WindowCovering.PositionState = this.hap.Characteristic.PositionState.DECREASING;
771
628
  this.setNewTarget = true;
772
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} value: ${this.CurrentPosition},` + ` CurrentPosition: ${this.CurrentPosition}`);
629
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} value: ${this.WindowCovering.CurrentPosition},`
630
+ + ` CurrentPosition: ${this.WindowCovering.CurrentPosition}`);
773
631
  }
774
632
  else {
775
- this.PositionState = this.hap.Characteristic.PositionState.STOPPED;
633
+ this.WindowCovering.PositionState = this.hap.Characteristic.PositionState.STOPPED;
776
634
  this.setNewTarget = false;
777
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} value: ${this.CurrentPosition},` + ` CurrentPosition: ${this.CurrentPosition}`);
635
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} value: ${this.WindowCovering.CurrentPosition},`
636
+ + ` CurrentPosition: ${this.WindowCovering.CurrentPosition}`);
778
637
  }
779
- this.windowCoveringService.setCharacteristic(this.hap.Characteristic.PositionState, this.PositionState);
780
- this.windowCoveringService.getCharacteristic(this.hap.Characteristic.PositionState).updateValue(this.PositionState);
638
+ this.WindowCovering.Service.setCharacteristic(this.hap.Characteristic.PositionState, this.WindowCovering.PositionState);
639
+ this.WindowCovering.Service.getCharacteristic(this.hap.Characteristic.PositionState).updateValue(this.WindowCovering.PositionState);
781
640
  /**
782
641
  * If Blind Tilt movement time is short, the moving flag from backend is always false.
783
642
  * The minimum time depends on the network control latency.
784
643
  */
785
644
  clearTimeout(this.setNewTargetTimer);
786
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateRate: ${this.updateRate}`);
645
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateRate: ${this.deviceUpdateRate}`);
787
646
  if (this.setNewTarget) {
788
647
  this.setNewTargetTimer = setTimeout(() => {
789
648
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} setNewTarget ${this.setNewTarget} timeout`);
790
649
  this.setNewTarget = false;
791
- }, this.updateRate * 1000);
650
+ }, this.deviceUpdateRate * 1000);
792
651
  }
793
652
  this.doBlindTiltUpdate.next();
794
653
  }
@@ -796,152 +655,99 @@ export class BlindTilt {
796
655
  await this.setMinMax();
797
656
  // CurrentHorizontalTiltAngle
798
657
  if (this.mappingMode === BlindTiltMappingMode.UseTiltForDirection) {
799
- if (this.CurrentHorizontalTiltAngle === undefined || Number.isNaN(this.CurrentHorizontalTiltAngle)) {
800
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentHorizontalTiltAngle: ${this.CurrentHorizontalTiltAngle}`);
658
+ if (this.WindowCovering.CurrentHorizontalTiltAngle === undefined || Number.isNaN(this.WindowCovering.CurrentHorizontalTiltAngle)) {
659
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
660
+ + ` CurrentHorizontalTiltAngle: ${this.WindowCovering.CurrentHorizontalTiltAngle}`);
801
661
  }
802
662
  else {
803
663
  if (this.device.mqttURL) {
804
- this.mqttPublish('CurrentHorizontalTiltAngle', this.CurrentHorizontalTiltAngle);
664
+ this.mqttPublish('CurrentHorizontalTiltAngle', this.WindowCovering.CurrentHorizontalTiltAngle.toString());
805
665
  }
806
- this.accessory.context.CurrentHorizontalTiltAngle = this.CurrentHorizontalTiltAngle;
807
- this.windowCoveringService.updateCharacteristic(this.hap.Characteristic.CurrentHorizontalTiltAngle, Number(this.CurrentHorizontalTiltAngle));
666
+ this.accessory.context.CurrentHorizontalTiltAngle = this.WindowCovering.CurrentHorizontalTiltAngle;
667
+ this.WindowCovering.Service.updateCharacteristic(this.hap.Characteristic.CurrentHorizontalTiltAngle, Number(this.WindowCovering.CurrentHorizontalTiltAngle));
808
668
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}
809
- updateCharacteristic CurrentHorizontalTiltAngle: ${this.CurrentHorizontalTiltAngle}`);
669
+ updateCharacteristic CurrentHorizontalTiltAngle: ${this.WindowCovering.CurrentHorizontalTiltAngle}`);
810
670
  }
811
671
  }
812
672
  // CurrentPosition
813
- if (this.CurrentPosition === undefined || Number.isNaN(this.CurrentPosition)) {
814
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentPosition: ${this.CurrentPosition}`);
673
+ if (this.WindowCovering.CurrentPosition === undefined || Number.isNaN(this.WindowCovering.CurrentPosition)) {
674
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentPosition: ${this.WindowCovering.CurrentPosition}`);
815
675
  }
816
676
  else {
817
677
  if (this.device.mqttURL) {
818
- this.mqttPublish('CurrentPosition', this.CurrentPosition);
678
+ this.mqttPublish('CurrentPosition', this.WindowCovering.CurrentPosition.toString());
819
679
  }
820
- this.accessory.context.CurrentPosition = this.CurrentPosition;
821
- this.windowCoveringService.updateCharacteristic(this.hap.Characteristic.CurrentPosition, Number(this.CurrentPosition));
822
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic CurrentPosition: ${this.CurrentPosition}`);
680
+ this.accessory.context.CurrentPosition = this.WindowCovering.CurrentPosition;
681
+ this.WindowCovering.Service.updateCharacteristic(this.hap.Characteristic.CurrentPosition, Number(this.WindowCovering.CurrentPosition));
682
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
683
+ + ` updateCharacteristic CurrentPosition: ${this.WindowCovering.CurrentPosition}`);
823
684
  }
824
685
  // PositionState
825
- if (this.PositionState === undefined) {
826
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} PositionState: ${this.PositionState}`);
686
+ if (this.WindowCovering.PositionState === undefined) {
687
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} PositionState: ${this.WindowCovering.PositionState}`);
827
688
  }
828
689
  else {
829
690
  if (this.device.mqttURL) {
830
- this.mqttPublish('PositionState', this.PositionState);
691
+ this.mqttPublish('PositionState', this.WindowCovering.PositionState.toString());
831
692
  }
832
- this.accessory.context.PositionState = this.PositionState;
833
- this.windowCoveringService.updateCharacteristic(this.hap.Characteristic.PositionState, Number(this.PositionState));
834
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic PositionState: ${this.PositionState}`);
693
+ this.accessory.context.PositionState = this.WindowCovering.PositionState;
694
+ this.WindowCovering.Service.updateCharacteristic(this.hap.Characteristic.PositionState, Number(this.WindowCovering.PositionState));
695
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
696
+ + ` PositionState: ${this.WindowCovering.PositionState}`);
835
697
  }
836
698
  // TargetPosition
837
- if (this.TargetPosition === undefined || Number.isNaN(this.TargetPosition)) {
838
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} TargetPosition: ${this.TargetPosition}`);
699
+ if (this.WindowCovering.TargetPosition === undefined || Number.isNaN(this.WindowCovering.TargetPosition)) {
700
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} TargetPosition: ${this.WindowCovering.TargetPosition}`);
839
701
  }
840
702
  else {
841
703
  if (this.device.mqttURL) {
842
- this.mqttPublish('TargetPosition', this.TargetPosition);
704
+ this.mqttPublish('TargetPosition', this.WindowCovering.TargetPosition.toString());
843
705
  }
844
- this.accessory.context.TargetPosition = this.TargetPosition;
845
- this.windowCoveringService.updateCharacteristic(this.hap.Characteristic.TargetPosition, Number(this.TargetPosition));
846
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic TargetPosition: ${this.TargetPosition}`);
706
+ this.accessory.context.TargetPosition = this.WindowCovering.TargetPosition;
707
+ this.WindowCovering.Service.updateCharacteristic(this.hap.Characteristic.TargetPosition, Number(this.WindowCovering.TargetPosition));
708
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
709
+ + ` TargetPosition: ${this.WindowCovering.TargetPosition}`);
847
710
  }
848
711
  // CurrentAmbientLightLevel
849
712
  if (!this.device.blindTilt?.hide_lightsensor) {
850
- if (this.CurrentAmbientLightLevel === undefined || Number.isNaN(this.CurrentAmbientLightLevel)) {
851
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
713
+ if (this.LightSensor.CurrentAmbientLightLevel === undefined || Number.isNaN(this.LightSensor.CurrentAmbientLightLevel)) {
714
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
715
+ + ` CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
852
716
  }
853
717
  else {
854
718
  if (this.device.mqttURL) {
855
- this.mqttPublish('CurrentAmbientLightLevel', this.CurrentAmbientLightLevel);
719
+ this.mqttPublish('CurrentAmbientLightLevel', this.LightSensor.CurrentAmbientLightLevel.toString());
856
720
  }
857
- this.accessory.context.CurrentAmbientLightLevel = this.CurrentAmbientLightLevel;
858
- this.lightSensorService?.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, this.CurrentAmbientLightLevel);
721
+ this.accessory.context.CurrentAmbientLightLevel = this.LightSensor.CurrentAmbientLightLevel;
722
+ this.LightSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, this.LightSensor.CurrentAmbientLightLevel);
859
723
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}` +
860
- ` updateCharacteristic CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
724
+ ` updateCharacteristic CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
861
725
  }
862
726
  }
863
727
  // BatteryLevel
864
- if (this.BatteryLevel === undefined) {
865
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
728
+ if (this.Battery.BatteryLevel === undefined) {
729
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}`);
866
730
  }
867
731
  else {
868
732
  if (this.device.mqttURL) {
869
- this.mqttPublish('BatteryLevel', this.BatteryLevel);
733
+ this.mqttPublish('BatteryLevel', this.Battery.BatteryLevel.toString());
870
734
  }
871
- this.accessory.context.BatteryLevel = this.BatteryLevel;
872
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.BatteryLevel);
873
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
735
+ this.accessory.context.BatteryLevel = this.Battery.BatteryLevel;
736
+ this.Battery?.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.Battery.BatteryLevel);
737
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.Battery.BatteryLevel}`);
874
738
  }
875
739
  // StatusLowBattery
876
- if (this.StatusLowBattery === undefined) {
877
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
740
+ if (this.Battery.StatusLowBattery === undefined) {
741
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.Battery.StatusLowBattery}`);
878
742
  }
879
743
  else {
880
744
  if (this.device.mqttURL) {
881
- this.mqttPublish('StatusLowBattery', this.StatusLowBattery);
882
- }
883
- this.accessory.context.StatusLowBattery = this.StatusLowBattery;
884
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.StatusLowBattery);
885
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
886
- }
887
- }
888
- /*
889
- * Publish MQTT message for topics of
890
- * 'homebridge-switchbot/blindtilt/xx:xx:xx:xx:xx:xx'
891
- */
892
- mqttPublish(topic, message) {
893
- const mac = this.device.deviceId
894
- ?.toLowerCase()
895
- .match(/[\s\S]{1,2}/g)
896
- ?.join(':');
897
- const options = this.device.mqttPubOptions || {};
898
- this.mqttClient?.publish(`homebridge-switchbot/blindtilt/${mac}/${topic}`, `${message}`, options);
899
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MQTT message: ${topic}/${message} options:${JSON.stringify(options)}`);
900
- }
901
- /*
902
- * Setup MQTT hadler if URL is specified.
903
- */
904
- async setupMqtt(device) {
905
- if (device.mqttURL) {
906
- try {
907
- const { connectAsync } = asyncmqtt;
908
- this.mqttClient = await connectAsync(device.mqttURL, device.mqttOptions || {});
909
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MQTT connection has been established successfully.`);
910
- this.mqttClient.on('error', (e) => {
911
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Failed to publish MQTT messages. ${e}`);
912
- });
913
- }
914
- catch (e) {
915
- this.mqttClient = null;
916
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Failed to establish MQTT connection. ${e}`);
745
+ this.mqttPublish('StatusLowBattery', this.Battery.StatusLowBattery.toString());
917
746
  }
918
- }
919
- }
920
- async stopScanning(switchbot) {
921
- switchbot.stopScan();
922
- if (this.BLE_IsConnected) {
923
- await this.BLEparseStatus();
924
- await this.updateHomeKitCharacteristics();
925
- }
926
- else {
927
- await this.BLERefreshConnection(switchbot);
928
- }
929
- }
930
- async getCustomBLEAddress(switchbot) {
931
- if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
932
- (async () => {
933
- // Start to monitor advertisement packets
934
- await switchbot.startScan({
935
- model: 'c',
936
- });
937
- // Set an event handler
938
- switchbot.onadvertisement = (ad) => {
939
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
940
- };
941
- await sleep(10000);
942
- // Stop to monitor
943
- switchbot.stopScan();
944
- })();
747
+ this.accessory.context.StatusLowBattery = this.Battery.StatusLowBattery;
748
+ this.Battery?.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.Battery.StatusLowBattery);
749
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
750
+ + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
945
751
  }
946
752
  }
947
753
  async BLEPushConnection() {
@@ -958,212 +764,104 @@ export class BlindTilt {
958
764
  await this.openAPIRefreshStatus();
959
765
  }
960
766
  }
961
- async SilentPerformance() {
962
- if (Number(this.TargetPosition) > 50) {
767
+ async setPerformance() {
768
+ let setPositionMode;
769
+ let Mode;
770
+ if (Number(this.WindowCovering.TargetPosition) > 50) {
963
771
  if (this.device.blindTilt?.setOpenMode === '1') {
964
- this.setPositionMode = 1;
965
- this.Mode = 'Silent Mode';
772
+ setPositionMode = 1;
773
+ Mode = 'Silent Mode';
774
+ }
775
+ else if (this.device.blindTilt?.setOpenMode === '0') {
776
+ setPositionMode = 0;
777
+ Mode = 'Performance Mode';
966
778
  }
967
779
  else {
968
- this.setPositionMode = 0;
969
- this.Mode = 'Performance Mode';
780
+ setPositionMode = 0;
781
+ Mode = 'Default Mode';
970
782
  }
971
783
  }
972
784
  else {
973
785
  if (this.device.blindTilt?.setCloseMode === '1') {
974
- this.setPositionMode = 1;
975
- this.Mode = 'Silent Mode';
786
+ setPositionMode = 1;
787
+ Mode = 'Silent Mode';
788
+ }
789
+ else if (this.device.blindTilt?.setOpenMode === '0') {
790
+ setPositionMode = 0;
791
+ Mode = 'Performance Mode';
976
792
  }
977
793
  else {
978
- this.setPositionMode = 0;
979
- this.Mode = 'Performance Mode';
794
+ setPositionMode = 0;
795
+ Mode = 'Default Mode';
980
796
  }
981
797
  }
798
+ return { setPositionMode, Mode };
982
799
  }
983
800
  async setMinMax() {
984
801
  if (this.device.blindTilt?.set_min) {
985
- if (Number(this.CurrentPosition) <= this.device.blindTilt?.set_min) {
986
- this.CurrentPosition = 0;
802
+ if (Number(this.WindowCovering.CurrentPosition) <= this.device.blindTilt?.set_min) {
803
+ this.WindowCovering.CurrentPosition = 0;
987
804
  }
988
805
  }
989
806
  if (this.device.blindTilt?.set_max) {
990
- if (Number(this.CurrentPosition) >= this.device.blindTilt?.set_max) {
991
- this.CurrentPosition = 100;
807
+ if (Number(this.WindowCovering.CurrentPosition) >= this.device.blindTilt?.set_max) {
808
+ this.WindowCovering.CurrentPosition = 100;
992
809
  }
993
810
  }
994
811
  if (this.mappingMode === BlindTiltMappingMode.UseTiltForDirection) {
995
- this.CurrentHorizontalTiltAngle = Number(this.CurrentHorizontalTiltAngle) < 0 ? -90 : 90;
812
+ this.WindowCovering.CurrentHorizontalTiltAngle = Number(this.WindowCovering.CurrentHorizontalTiltAngle) < 0 ? -90 : 90;
996
813
  }
997
814
  }
998
815
  minStep(device) {
816
+ let set_minStep;
999
817
  if (device.blindTilt?.set_minStep) {
1000
- this.set_minStep = device.blindTilt?.set_minStep;
818
+ set_minStep = device.blindTilt?.set_minStep;
1001
819
  }
1002
820
  else {
1003
- this.set_minStep = 1;
821
+ set_minStep = 1;
1004
822
  }
1005
- return this.set_minStep;
823
+ return set_minStep;
1006
824
  }
1007
- minLux() {
825
+ async minLux() {
826
+ let set_minLux;
1008
827
  if (this.device.blindTilt?.set_minLux) {
1009
- this.set_minLux = this.device.blindTilt?.set_minLux;
828
+ set_minLux = this.device.blindTilt?.set_minLux;
1010
829
  }
1011
830
  else {
1012
- this.set_minLux = 1;
831
+ set_minLux = 1;
1013
832
  }
1014
- return this.set_minLux;
833
+ return set_minLux;
1015
834
  }
1016
- maxLux() {
835
+ async maxLux() {
836
+ let set_maxLux;
1017
837
  if (this.device.blindTilt?.set_maxLux) {
1018
- this.set_maxLux = this.device.blindTilt?.set_maxLux;
838
+ set_maxLux = this.device.blindTilt?.set_maxLux;
1019
839
  }
1020
840
  else {
1021
- this.set_maxLux = 6001;
1022
- }
1023
- return this.set_maxLux;
1024
- }
1025
- async scan(device) {
1026
- if (device.scanDuration) {
1027
- if (this.updateRate > device.scanDuration) {
1028
- this.scanDuration = this.updateRate;
1029
- if (this.BLE) {
1030
- this.warnLog(`${this.device.deviceType}: ` +
1031
- `${this.accessory.displayName} scanDuration is less than updateRate, overriding scanDuration with updateRate`);
1032
- }
1033
- }
1034
- else {
1035
- this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
1036
- }
1037
- if (this.BLE) {
1038
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
1039
- }
1040
- }
1041
- else {
1042
- if (this.updateRate > 1) {
1043
- this.scanDuration = this.updateRate;
1044
- }
1045
- else {
1046
- this.scanDuration = this.accessory.context.scanDuration = 1;
1047
- }
1048
- if (this.BLE) {
1049
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
1050
- }
1051
- }
1052
- }
1053
- async statusCode(statusCode) {
1054
- switch (statusCode) {
1055
- case 151:
1056
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this deviceType, statusCode: ${statusCode}`);
1057
- break;
1058
- case 152:
1059
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found, statusCode: ${statusCode}`);
1060
- break;
1061
- case 160:
1062
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported, statusCode: ${statusCode}`);
1063
- break;
1064
- case 161:
1065
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline, statusCode: ${statusCode}`);
1066
- this.offlineOff();
1067
- break;
1068
- case 171:
1069
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. ` +
1070
- `Hub: ${this.device.hubDeviceId}`);
1071
- this.offlineOff();
1072
- break;
1073
- case 190:
1074
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
1075
- ` Or command format is invalid, statusCode: ${statusCode}`);
1076
- break;
1077
- case 100:
1078
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent, statusCode: ${statusCode}`);
1079
- break;
1080
- case 200:
1081
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Request successful, statusCode: ${statusCode}`);
1082
- break;
1083
- case 400:
1084
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Bad Request, The client has issued an invalid request. `
1085
- + `This is commonly used to specify validation errors in a request payload, statusCode: ${statusCode}`);
1086
- break;
1087
- case 401:
1088
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unauthorized, Authorization for the API is required, `
1089
- + `but the request has not been authenticated, statusCode: ${statusCode}`);
1090
- break;
1091
- case 403:
1092
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Forbidden, The request has been authenticated but does not `
1093
- + `have appropriate permissions, or a requested resource is not found, statusCode: ${statusCode}`);
1094
- break;
1095
- case 404:
1096
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Found, Specifies the requested path does not exist, `
1097
- + `statusCode: ${statusCode}`);
1098
- break;
1099
- case 406:
1100
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Acceptable, The client has requested a MIME type via `
1101
- + `the Accept header for a value not supported by the server, statusCode: ${statusCode}`);
1102
- break;
1103
- case 415:
1104
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unsupported Media Type, The client has defined a contentType `
1105
- + `header that is not supported by the server, statusCode: ${statusCode}`);
1106
- break;
1107
- case 422:
1108
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unprocessable Entity, The client has made a valid request, `
1109
- + `but the server cannot process it. This is often used for APIs for which certain limits have been exceeded, statusCode: ${statusCode}`);
1110
- break;
1111
- case 429:
1112
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Too Many Requests, The client has exceeded the number of `
1113
- + `requests allowed for a given time window, statusCode: ${statusCode}`);
1114
- break;
1115
- case 500:
1116
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Internal Server Error, An unexpected error on the SmartThings `
1117
- + `servers has occurred. These errors should be rare, statusCode: ${statusCode}`);
1118
- break;
1119
- default:
1120
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: ` +
1121
- `${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`);
841
+ set_maxLux = 6001;
1122
842
  }
843
+ return set_maxLux;
1123
844
  }
1124
845
  async offlineOff() {
1125
846
  if (this.device.offline) {
1126
- await this.deviceContext();
1127
- await this.updateHomeKitCharacteristics();
847
+ this.WindowCovering.Service.updateCharacteristic(this.hap.Characteristic.CurrentPosition, 100);
848
+ this.WindowCovering.Service.updateCharacteristic(this.hap.Characteristic.PositionState, this.hap.Characteristic.PositionState.STOPPED);
849
+ this.WindowCovering.Service.updateCharacteristic(this.hap.Characteristic.TargetPosition, 100);
1128
850
  }
1129
851
  }
1130
852
  async apiError(e) {
1131
- this.windowCoveringService.updateCharacteristic(this.hap.Characteristic.CurrentPosition, e);
1132
- this.windowCoveringService.updateCharacteristic(this.hap.Characteristic.PositionState, e);
1133
- this.windowCoveringService.updateCharacteristic(this.hap.Characteristic.TargetPosition, e);
853
+ this.WindowCovering.Service.updateCharacteristic(this.hap.Characteristic.CurrentPosition, e);
854
+ this.WindowCovering.Service.updateCharacteristic(this.hap.Characteristic.PositionState, e);
855
+ this.WindowCovering.Service.updateCharacteristic(this.hap.Characteristic.TargetPosition, e);
1134
856
  if (!this.device.curtain?.hide_lightsensor) {
1135
- this.lightSensorService?.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, e);
857
+ this.LightSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, e);
1136
858
  }
1137
859
  if (this.BLE) {
1138
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e);
1139
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e);
860
+ this.Battery?.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e);
861
+ this.Battery?.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e);
1140
862
  }
1141
863
  //throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
1142
864
  }
1143
- async deviceContext() {
1144
- if (this.CurrentPosition === undefined) {
1145
- this.CurrentPosition = 0;
1146
- }
1147
- else {
1148
- this.CurrentPosition = this.accessory.context.CurrentPosition;
1149
- }
1150
- if (this.TargetPosition === undefined) {
1151
- this.TargetPosition = 0;
1152
- }
1153
- else {
1154
- this.TargetPosition = this.accessory.context.TargetPosition;
1155
- }
1156
- if (this.PositionState === undefined) {
1157
- this.PositionState = this.hap.Characteristic.PositionState.STOPPED;
1158
- }
1159
- else {
1160
- this.PositionState = this.accessory.context.PositionState;
1161
- }
1162
- if (this.FirmwareRevision === undefined) {
1163
- this.FirmwareRevision = this.platform.version;
1164
- this.accessory.context.FirmwareRevision = this.FirmwareRevision;
1165
- }
1166
- }
1167
865
  /**
1168
866
  * Maps device values to homekit values
1169
867
  *
@@ -1278,156 +976,5 @@ export class BlindTilt {
1278
976
  }
1279
977
  }
1280
978
  }
1281
- async refreshRate(device) {
1282
- // refreshRate
1283
- if (device.refreshRate) {
1284
- this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
1285
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
1286
- }
1287
- else if (this.platform.config.options.refreshRate) {
1288
- this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options.refreshRate;
1289
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
1290
- }
1291
- // updateRate
1292
- if (device?.blindTilt?.updateRate) {
1293
- this.updateRate = device?.blindTilt?.updateRate;
1294
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config Blind Tilt updateRate: ${this.updateRate}`);
1295
- }
1296
- else {
1297
- this.updateRate = 2;
1298
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default Blind Tilt updateRate: ${this.updateRate}`);
1299
- }
1300
- }
1301
- async deviceRetry(device) {
1302
- if (!device.maxRetries) {
1303
- this.maxRetries = 5; // Maximum number of retries
1304
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Max Retries Not Set, Using: ${this.maxRetries}`);
1305
- }
1306
- else {
1307
- this.maxRetries = device.maxRetries;
1308
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Max Retries: ${this.maxRetries}`);
1309
- }
1310
- if (!device.delayBetweenRetries) {
1311
- this.delayBetweenRetries = 3000; // Delay between retries in milliseconds
1312
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Delay Between Retries Not Set, Using: ${this.delayBetweenRetries}`);
1313
- }
1314
- else {
1315
- this.delayBetweenRetries = device.delayBetweenRetries * 1000;
1316
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Delay Between Retries: ${this.delayBetweenRetries}`);
1317
- }
1318
- }
1319
- async deviceConfig(device) {
1320
- let config = {};
1321
- if (device.blindTilt) {
1322
- config = device.blindTilt;
1323
- }
1324
- if (device.connectionType !== undefined) {
1325
- config['connectionType'] = device.connectionType;
1326
- }
1327
- if (device.external !== undefined) {
1328
- config['external'] = device.external;
1329
- }
1330
- if (device.mqttURL !== undefined) {
1331
- config['mqttURL'] = device.mqttURL;
1332
- }
1333
- if (device.logging !== undefined) {
1334
- config['logging'] = device.logging;
1335
- }
1336
- if (device.refreshRate !== undefined) {
1337
- config['refreshRate'] = device.refreshRate;
1338
- }
1339
- if (device.scanDuration !== undefined) {
1340
- config['scanDuration'] = device.scanDuration;
1341
- }
1342
- if (device.maxRetry !== undefined) {
1343
- config['maxRetry'] = device.maxRetry;
1344
- }
1345
- if (device.webhook !== undefined) {
1346
- config['webhook'] = device.webhook;
1347
- }
1348
- if (device.maxRetries !== undefined) {
1349
- config['maxRetries'] = device.maxRetries;
1350
- }
1351
- if (device.delayBetweenRetries !== undefined) {
1352
- config['delayBetweenRetries'] = device.delayBetweenRetries;
1353
- }
1354
- if (device.blindTilt?.mode === undefined) {
1355
- config['mode'] = BlindTiltMappingMode.OnlyUp;
1356
- }
1357
- else {
1358
- config['mode'] = device.blindTilt?.mode;
1359
- }
1360
- if (Object.entries(config).length !== 0) {
1361
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
1362
- }
1363
- }
1364
- async deviceLogs(device) {
1365
- if (this.platform.debugMode) {
1366
- this.deviceLogging = this.accessory.context.logging = 'debugMode';
1367
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
1368
- }
1369
- else if (device.logging) {
1370
- this.deviceLogging = this.accessory.context.logging = device.logging;
1371
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
1372
- }
1373
- else if (this.platform.config.options?.logging) {
1374
- this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
1375
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
1376
- }
1377
- else {
1378
- this.deviceLogging = this.accessory.context.logging = 'standard';
1379
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
1380
- }
1381
- }
1382
- /**
1383
- * Logging for Device
1384
- */
1385
- successLog(...log) {
1386
- if (this.enablingDeviceLogging()) {
1387
- this.platform.log.success(String(...log));
1388
- }
1389
- }
1390
- infoLog(...log) {
1391
- if (this.enablingDeviceLogging()) {
1392
- this.platform.log.info(String(...log));
1393
- }
1394
- }
1395
- warnLog(...log) {
1396
- if (this.enablingDeviceLogging()) {
1397
- this.platform.log.warn(String(...log));
1398
- }
1399
- }
1400
- debugWarnLog(...log) {
1401
- if (this.enablingDeviceLogging()) {
1402
- if (this.deviceLogging?.includes('debug')) {
1403
- this.platform.log.warn('[DEBUG]', String(...log));
1404
- }
1405
- }
1406
- }
1407
- errorLog(...log) {
1408
- if (this.enablingDeviceLogging()) {
1409
- this.platform.log.error(String(...log));
1410
- }
1411
- }
1412
- debugErrorLog(...log) {
1413
- if (this.enablingDeviceLogging()) {
1414
- if (this.deviceLogging?.includes('debug')) {
1415
- this.platform.log.error('[DEBUG]', String(...log));
1416
- }
1417
- }
1418
- }
1419
- debugLog(...log) {
1420
- if (this.enablingDeviceLogging()) {
1421
- if (this.deviceLogging === 'debug') {
1422
- this.platform.log.info('[DEBUG]', String(...log));
1423
- }
1424
- else {
1425
- this.platform.log.debug(String(...log));
1426
- }
1427
- }
1428
- }
1429
- enablingDeviceLogging() {
1430
- return this.deviceLogging.includes('debug') || this.deviceLogging === 'standard';
1431
- }
1432
979
  }
1433
980
  //# sourceMappingURL=blindtilt.js.map