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