@switchbot/homebridge-switchbot 3.7.1-beta.0 → 3.7.1-beta.10

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 (110) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/config.schema.json +7 -1
  3. package/dist/device/blindtilt.d.ts +3 -1
  4. package/dist/device/blindtilt.d.ts.map +1 -1
  5. package/dist/device/blindtilt.js +61 -28
  6. package/dist/device/blindtilt.js.map +1 -1
  7. package/dist/device/bot.d.ts +1 -0
  8. package/dist/device/bot.d.ts.map +1 -1
  9. package/dist/device/bot.js +102 -80
  10. package/dist/device/bot.js.map +1 -1
  11. package/dist/device/ceilinglight.d.ts +1 -0
  12. package/dist/device/ceilinglight.d.ts.map +1 -1
  13. package/dist/device/ceilinglight.js +53 -28
  14. package/dist/device/ceilinglight.js.map +1 -1
  15. package/dist/device/colorbulb.d.ts +1 -0
  16. package/dist/device/colorbulb.d.ts.map +1 -1
  17. package/dist/device/colorbulb.js +53 -28
  18. package/dist/device/colorbulb.js.map +1 -1
  19. package/dist/device/contact.d.ts +1 -0
  20. package/dist/device/contact.d.ts.map +1 -1
  21. package/dist/device/contact.js +53 -28
  22. package/dist/device/contact.js.map +1 -1
  23. package/dist/device/curtain.d.ts +2 -0
  24. package/dist/device/curtain.d.ts.map +1 -1
  25. package/dist/device/curtain.js +74 -43
  26. package/dist/device/curtain.js.map +1 -1
  27. package/dist/device/device.d.ts.map +1 -1
  28. package/dist/device/device.js +247 -394
  29. package/dist/device/device.js.map +1 -1
  30. package/dist/device/fan.d.ts +1 -0
  31. package/dist/device/fan.d.ts.map +1 -1
  32. package/dist/device/fan.js +53 -28
  33. package/dist/device/fan.js.map +1 -1
  34. package/dist/device/hub.d.ts +1 -0
  35. package/dist/device/hub.d.ts.map +1 -1
  36. package/dist/device/hub.js +53 -28
  37. package/dist/device/hub.js.map +1 -1
  38. package/dist/device/humidifier.d.ts +1 -0
  39. package/dist/device/humidifier.d.ts.map +1 -1
  40. package/dist/device/humidifier.js +53 -28
  41. package/dist/device/humidifier.js.map +1 -1
  42. package/dist/device/iosensor.d.ts +1 -0
  43. package/dist/device/iosensor.d.ts.map +1 -1
  44. package/dist/device/iosensor.js +53 -28
  45. package/dist/device/iosensor.js.map +1 -1
  46. package/dist/device/lightstrip.d.ts +1 -0
  47. package/dist/device/lightstrip.d.ts.map +1 -1
  48. package/dist/device/lightstrip.js +53 -28
  49. package/dist/device/lightstrip.js.map +1 -1
  50. package/dist/device/lock.d.ts +1 -0
  51. package/dist/device/lock.d.ts.map +1 -1
  52. package/dist/device/lock.js +54 -29
  53. package/dist/device/lock.js.map +1 -1
  54. package/dist/device/meter.d.ts +1 -0
  55. package/dist/device/meter.d.ts.map +1 -1
  56. package/dist/device/meter.js +53 -28
  57. package/dist/device/meter.js.map +1 -1
  58. package/dist/device/meterplus.d.ts +1 -0
  59. package/dist/device/meterplus.d.ts.map +1 -1
  60. package/dist/device/meterplus.js +53 -28
  61. package/dist/device/meterplus.js.map +1 -1
  62. package/dist/device/motion.d.ts +1 -0
  63. package/dist/device/motion.d.ts.map +1 -1
  64. package/dist/device/motion.js +53 -28
  65. package/dist/device/motion.js.map +1 -1
  66. package/dist/device/plug.d.ts +1 -0
  67. package/dist/device/plug.d.ts.map +1 -1
  68. package/dist/device/plug.js +54 -29
  69. package/dist/device/plug.js.map +1 -1
  70. package/dist/device/robotvacuumcleaner.d.ts +1 -0
  71. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  72. package/dist/device/robotvacuumcleaner.js +53 -28
  73. package/dist/device/robotvacuumcleaner.js.map +1 -1
  74. package/dist/device/waterdetector.d.ts +1 -0
  75. package/dist/device/waterdetector.d.ts.map +1 -1
  76. package/dist/device/waterdetector.js +53 -28
  77. package/dist/device/waterdetector.js.map +1 -1
  78. package/dist/irdevice/irdevice.d.ts.map +1 -1
  79. package/dist/irdevice/irdevice.js +30 -133
  80. package/dist/irdevice/irdevice.js.map +1 -1
  81. package/dist/platform.d.ts +19 -18
  82. package/dist/platform.d.ts.map +1 -1
  83. package/dist/platform.js +369 -524
  84. package/dist/platform.js.map +1 -1
  85. package/dist/settings.d.ts +1 -0
  86. package/dist/settings.d.ts.map +1 -1
  87. package/dist/settings.js.map +1 -1
  88. package/package.json +13 -13
  89. package/src/device/blindtilt.ts +59 -27
  90. package/src/device/bot.ts +98 -77
  91. package/src/device/ceilinglight.ts +51 -27
  92. package/src/device/colorbulb.ts +50 -26
  93. package/src/device/contact.ts +50 -26
  94. package/src/device/curtain.ts +71 -40
  95. package/src/device/device.ts +283 -395
  96. package/src/device/fan.ts +50 -26
  97. package/src/device/hub.ts +50 -26
  98. package/src/device/humidifier.ts +50 -26
  99. package/src/device/iosensor.ts +50 -26
  100. package/src/device/lightstrip.ts +50 -26
  101. package/src/device/lock.ts +51 -27
  102. package/src/device/meter.ts +50 -26
  103. package/src/device/meterplus.ts +50 -26
  104. package/src/device/motion.ts +50 -26
  105. package/src/device/plug.ts +51 -27
  106. package/src/device/robotvacuumcleaner.ts +50 -26
  107. package/src/device/waterdetector.ts +50 -26
  108. package/src/irdevice/irdevice.ts +54 -134
  109. package/src/platform.ts +379 -529
  110. package/src/settings.ts +1 -0
@@ -4,6 +4,10 @@
4
4
  */
5
5
  import { deviceBase } from './device.js';
6
6
  import { hs2rgb, m2hs } from '../utils.js';
7
+ /*
8
+ * For Testing Locally:
9
+ * import { SwitchBotBLEModel, SwitchBotBLEModelName } from '/Users/Shared/GitHub/OpenWonderLabs/node-switchbot/dist/index.js';
10
+ */
7
11
  import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot';
8
12
  import { Subject, debounceTime, interval, skipWhile, take, tap } from 'rxjs';
9
13
 
@@ -161,12 +165,28 @@ export class CeilingLight extends deviceBase {
161
165
  .onSet(this.SaturationSet.bind(this));
162
166
 
163
167
  // Retrieve initial values and updateHomekit
164
- this.debugLog('Retrieve initial values and update Homekit');
165
- this.refreshStatus();
168
+ try {
169
+ this.debugLog('Retrieve initial values and update Homekit');
170
+ this.refreshStatus();
171
+ } catch (e: any) {
172
+ this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e}`);
173
+ }
174
+
175
+ //regisiter webhook event handler if enabled
176
+ try {
177
+ this.debugLog('Registering Webhook Event Handler');
178
+ this.registerWebhook();
179
+ } catch (e: any) {
180
+ this.errorLog(`failed to registerWebhook, Error: ${e}`);
181
+ }
166
182
 
167
- //regisiter webhook event handler
168
- this.debugLog('Registering Webhook Event Handler');
169
- this.registerWebhook();
183
+ //regisiter platform BLE event handler if enabled
184
+ try {
185
+ this.debugLog('Registering Platform BLE Event Handler');
186
+ this.registerPlatformBLE();
187
+ } catch (e: any) {
188
+ this.errorLog(`failed to registerPlatformBLE, Error: ${e}`);
189
+ }
170
190
 
171
191
  // Start an update interval
172
192
  interval(this.deviceRefreshRate * 1000)
@@ -275,7 +295,7 @@ export class CeilingLight extends deviceBase {
275
295
  async refreshStatus(): Promise<void> {
276
296
  if (!this.device.enableCloudService && this.OpenAPI) {
277
297
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
278
- } else if (this.BLE || this.config.options?.BLE) {
298
+ } else if (this.BLE) {
279
299
  await this.BLERefreshStatus();
280
300
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
281
301
  await this.openAPIRefreshStatus();
@@ -287,11 +307,34 @@ export class CeilingLight extends deviceBase {
287
307
 
288
308
  async BLERefreshStatus(): Promise<void> {
289
309
  await this.debugLog('BLERefreshStatus');
310
+ const switchbot = await this.switchbotBLE();
311
+ if (switchbot === undefined) {
312
+ await this.BLERefreshConnection(switchbot);
313
+ } else {
314
+ (async () => {
315
+ // Start to monitor advertisement packets
316
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as unknown as ceilingLightServiceData;
317
+ // Update HomeKit
318
+ if ((serviceData.model === SwitchBotBLEModel.CeilingLight || SwitchBotBLEModel.CeilingLightPro)
319
+ && serviceData.modelName === SwitchBotBLEModelName.CeilingLight || SwitchBotBLEModelName.CeilingLightPro) {
320
+ this.serviceData = serviceData;
321
+ await this.BLEparseStatus();
322
+ await this.updateHomeKitCharacteristics();
323
+ } else {
324
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
325
+ await this.BLERefreshConnection(switchbot);
326
+ }
327
+ })();
328
+ }
329
+ }
330
+
331
+ async registerPlatformBLE(): Promise<void> {
332
+ await this.debugLog('registerPlatformBLE');
290
333
  if (this.config.options?.BLE) {
291
334
  await this.debugLog('is listening to Platform BLE.');
292
335
  this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
293
336
  await this.debugLog(`bleMac: ${this.device.bleMac}`);
294
- this.platform.bleEventHandler[this.device.bleMac] = async (context: ceilingLightServiceData) => {
337
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: ceilingLightServiceData | ceilingLightProServiceData) => {
295
338
  try {
296
339
  await this.debugLog(`received BLE: ${JSON.stringify(context)}`);
297
340
  this.serviceData = context;
@@ -302,26 +345,7 @@ export class CeilingLight extends deviceBase {
302
345
  }
303
346
  };
304
347
  } else {
305
- await this.debugLog('is using Device BLE Scanning.');
306
- const switchbot = await this.switchbotBLE();
307
- if (switchbot === undefined) {
308
- await this.BLERefreshConnection(switchbot);
309
- } else {
310
- (async () => {
311
- // Start to monitor advertisement packets
312
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as unknown as ceilingLightServiceData;
313
- // Update HomeKit
314
- if ((serviceData.model === SwitchBotBLEModel.CeilingLight || SwitchBotBLEModel.CeilingLightPro)
315
- && serviceData.modelName === SwitchBotBLEModelName.CeilingLight || SwitchBotBLEModelName.CeilingLightPro) {
316
- this.serviceData = serviceData;
317
- await this.BLEparseStatus();
318
- await this.updateHomeKitCharacteristics();
319
- } else {
320
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
321
- await this.BLERefreshConnection(switchbot);
322
- }
323
- })();
324
- }
348
+ await this.debugLog('is not listening to Platform BLE');
325
349
  }
326
350
  }
327
351
 
@@ -4,6 +4,10 @@
4
4
  */
5
5
  import { deviceBase } from './device.js';
6
6
  import { hs2rgb, rgb2hs, m2hs } from '../utils.js';
7
+ /*
8
+ * For Testing Locally:
9
+ * import { SwitchBotBLEModel, SwitchBotBLEModelName } from '/Users/Shared/GitHub/OpenWonderLabs/node-switchbot/dist/index.js';
10
+ */
7
11
  import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot';
8
12
  import { Subject, debounceTime, interval, skipWhile, take, tap } from 'rxjs';
9
13
 
@@ -157,12 +161,28 @@ export class ColorBulb extends deviceBase {
157
161
  .onSet(this.SaturationSet.bind(this));
158
162
 
159
163
  // Retrieve initial values and updateHomekit
160
- this.debugLog('Retrieve initial values and update Homekit');
161
- this.refreshStatus();
164
+ try {
165
+ this.debugLog('Retrieve initial values and update Homekit');
166
+ this.refreshStatus();
167
+ } catch (e: any) {
168
+ this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e}`);
169
+ }
170
+
171
+ //regisiter webhook event handler if enabled
172
+ try {
173
+ this.debugLog('Registering Webhook Event Handler');
174
+ this.registerWebhook();
175
+ } catch (e: any) {
176
+ this.errorLog(`failed to registerWebhook, Error: ${e}`);
177
+ }
162
178
 
163
- //regisiter webhook event handler
164
- this.debugLog('Registering Webhook Event Handler');
165
- this.registerWebhook();
179
+ //regisiter platform BLE event handler if enabled
180
+ try {
181
+ this.debugLog('Registering Platform BLE Event Handler');
182
+ this.registerPlatformBLE();
183
+ } catch (e: any) {
184
+ this.errorLog(`failed to registerPlatformBLE, Error: ${e}`);
185
+ }
166
186
 
167
187
  // Start an update interval
168
188
  interval(this.deviceRefreshRate * 1000)
@@ -296,7 +316,7 @@ export class ColorBulb extends deviceBase {
296
316
  async refreshStatus(): Promise<void> {
297
317
  if (!this.device.enableCloudService && this.OpenAPI) {
298
318
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
299
- } else if (this.BLE || this.config.options?.BLE) {
319
+ } else if (this.BLE) {
300
320
  await this.BLERefreshStatus();
301
321
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
302
322
  await this.openAPIRefreshStatus();
@@ -308,6 +328,29 @@ export class ColorBulb extends deviceBase {
308
328
 
309
329
  async BLERefreshStatus(): Promise<void> {
310
330
  await this.debugLog('BLERefreshStatus');
331
+ const switchbot = await this.switchbotBLE();
332
+ if (switchbot === undefined) {
333
+ await this.BLERefreshConnection(switchbot);
334
+ } else {
335
+ // Start to monitor advertisement packets
336
+ (async () => {
337
+ // Start to monitor advertisement packets
338
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as colorBulbServiceData;
339
+ // Update HomeKit
340
+ if (serviceData.model === SwitchBotBLEModel.ColorBulb && serviceData.modelName === SwitchBotBLEModelName.ColorBulb) {
341
+ this.serviceData = serviceData;
342
+ await this.BLEparseStatus();
343
+ await this.updateHomeKitCharacteristics();
344
+ } else {
345
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
346
+ await this.BLERefreshConnection(switchbot);
347
+ }
348
+ })();
349
+ }
350
+ }
351
+
352
+ async registerPlatformBLE(): Promise<void> {
353
+ await this.debugLog('registerPlatformBLE');
311
354
  if (this.config.options?.BLE) {
312
355
  await this.debugLog('is listening to Platform BLE.');
313
356
  this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
@@ -323,26 +366,7 @@ export class ColorBulb extends deviceBase {
323
366
  }
324
367
  };
325
368
  } else {
326
- await this.debugLog('is using Device BLE Scanning.');
327
- const switchbot = await this.switchbotBLE();
328
- if (switchbot === undefined) {
329
- await this.BLERefreshConnection(switchbot);
330
- } else {
331
- // Start to monitor advertisement packets
332
- (async () => {
333
- // Start to monitor advertisement packets
334
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as colorBulbServiceData;
335
- // Update HomeKit
336
- if (serviceData.model === SwitchBotBLEModel.ColorBulb && serviceData.modelName === SwitchBotBLEModelName.ColorBulb) {
337
- this.serviceData = serviceData;
338
- await this.BLEparseStatus();
339
- await this.updateHomeKitCharacteristics();
340
- } else {
341
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
342
- await this.BLERefreshConnection(switchbot);
343
- }
344
- })();
345
- }
369
+ await this.debugLog('is not listening to Platform BLE');
346
370
  }
347
371
  }
348
372
 
@@ -4,6 +4,10 @@
4
4
  */
5
5
  import { deviceBase } from './device.js';
6
6
  import { Subject, interval, skipWhile } from 'rxjs';
7
+ /*
8
+ * For Testing Locally:
9
+ * import { SwitchBotBLEModel, SwitchBotBLEModelName } from '/Users/Shared/GitHub/OpenWonderLabs/node-switchbot/dist/index.js';
10
+ */
7
11
  import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot';
8
12
 
9
13
  import type { devicesConfig } from '../settings.js';
@@ -169,12 +173,28 @@ export class Contact extends deviceBase {
169
173
  }
170
174
 
171
175
  // Retrieve initial values and updateHomekit
172
- this.debugLog('Retrieve initial values and update Homekit');
173
- this.refreshStatus();
176
+ try {
177
+ this.debugLog('Retrieve initial values and update Homekit');
178
+ this.refreshStatus();
179
+ } catch (e: any) {
180
+ this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e}`);
181
+ }
174
182
 
175
- //regisiter webhook event handler
176
- this.debugLog('Registering Webhook Event Handler');
177
- this.registerWebhook();
183
+ //regisiter webhook event handler if enabled
184
+ try {
185
+ this.debugLog('Registering Webhook Event Handler');
186
+ this.registerWebhook();
187
+ } catch (e: any) {
188
+ this.errorLog(`failed to registerWebhook, Error: ${e}`);
189
+ }
190
+
191
+ //regisiter platform BLE event handler if enabled
192
+ try {
193
+ this.debugLog('Registering Platform BLE Event Handler');
194
+ this.registerPlatformBLE();
195
+ } catch (e: any) {
196
+ this.errorLog(`failed to registerPlatformBLE, Error: ${e}`);
197
+ }
178
198
 
179
199
  // Start an update interval
180
200
  interval(this.deviceRefreshRate * 1000)
@@ -284,7 +304,7 @@ export class Contact extends deviceBase {
284
304
  async refreshStatus(): Promise<void> {
285
305
  if (!this.device.enableCloudService && this.OpenAPI) {
286
306
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
287
- } else if (this.BLE || this.config.options?.BLE) {
307
+ } else if (this.BLE) {
288
308
  await this.BLERefreshStatus();
289
309
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
290
310
  await this.openAPIRefreshStatus();
@@ -296,6 +316,29 @@ export class Contact extends deviceBase {
296
316
 
297
317
  async BLERefreshStatus(): Promise<void> {
298
318
  await this.debugLog('BLERefreshStatus');
319
+ const switchbot = await this.switchbotBLE();
320
+ if (switchbot === undefined) {
321
+ await this.BLERefreshConnection(switchbot);
322
+ } else {
323
+ // Start to monitor advertisement packets
324
+ (async () => {
325
+ // Start to monitor advertisement packets
326
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as contactSensorServiceData;
327
+ // Update HomeKit
328
+ if (serviceData.model === SwitchBotBLEModel.ContactSensor && serviceData.modelName === SwitchBotBLEModelName.ContactSensor) {
329
+ this.serviceData = serviceData;
330
+ await this.BLEparseStatus();
331
+ await this.updateHomeKitCharacteristics();
332
+ } else {
333
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
334
+ await this.BLERefreshConnection(switchbot);
335
+ }
336
+ })();
337
+ }
338
+ }
339
+
340
+ async registerPlatformBLE(): Promise<void> {
341
+ await this.debugLog('registerPlatformBLE');
299
342
  if (this.config.options?.BLE) {
300
343
  await this.debugLog('is listening to Platform BLE.');
301
344
  this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
@@ -311,26 +354,7 @@ export class Contact extends deviceBase {
311
354
  }
312
355
  };
313
356
  } else {
314
- await this.debugLog('is using Device BLE Scanning.');
315
- const switchbot = await this.switchbotBLE();
316
- if (switchbot === undefined) {
317
- await this.BLERefreshConnection(switchbot);
318
- } else {
319
- // Start to monitor advertisement packets
320
- (async () => {
321
- // Start to monitor advertisement packets
322
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as contactSensorServiceData;
323
- // Update HomeKit
324
- if (serviceData.model === SwitchBotBLEModel.ContactSensor && serviceData.modelName === SwitchBotBLEModelName.ContactSensor) {
325
- this.serviceData = serviceData;
326
- await this.BLEparseStatus();
327
- await this.updateHomeKitCharacteristics();
328
- } else {
329
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
330
- await this.BLERefreshConnection(switchbot);
331
- }
332
- })();
333
- }
357
+ await this.debugLog('is not listening to Platform BLE');
334
358
  }
335
359
  }
336
360
 
@@ -4,7 +4,12 @@
4
4
  */
5
5
  import { hostname } from 'os';
6
6
  import { deviceBase } from './device.js';
7
+ /*
8
+ * For Testing Locally:
9
+ * import { SwitchBotBLEModel, SwitchBotBLEModelName } from '/Users/Shared/GitHub/OpenWonderLabs/node-switchbot/dist/index.js';
10
+ */
7
11
  import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot';
12
+
8
13
  import { Subject, debounceTime, interval, skipWhile, take, tap } from 'rxjs';
9
14
 
10
15
  import type { devicesConfig } from '../settings.js';
@@ -54,6 +59,7 @@ export class Curtain extends deviceBase {
54
59
  setNewTargetTimer!: NodeJS.Timeout;
55
60
 
56
61
  // Updates
62
+ curtainMoving!: boolean;
57
63
  curtainUpdateInProgress!: boolean;
58
64
  doCurtainUpdate!: Subject<void>;
59
65
 
@@ -68,6 +74,7 @@ export class Curtain extends deviceBase {
68
74
 
69
75
  // this is subject we use to track when we need to POST changes to the SwitchBot API
70
76
  this.doCurtainUpdate = new Subject();
77
+ this.curtainMoving = false;
71
78
  this.curtainUpdateInProgress = false;
72
79
  this.setNewTarget = false;
73
80
 
@@ -186,12 +193,28 @@ export class Curtain extends deviceBase {
186
193
  }
187
194
 
188
195
  // Retrieve initial values and updateHomekit
189
- this.debugLog('Retrieve initial values and update Homekit');
190
- this.refreshStatus();
196
+ try {
197
+ this.debugLog('Retrieve initial values and update Homekit');
198
+ this.refreshStatus();
199
+ } catch (e: any) {
200
+ this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e}`);
201
+ }
191
202
 
192
- //regisiter webhook event handler
193
- this.debugLog('Registering Webhook Event Handler');
194
- this.registerWebhook();
203
+ //regisiter webhook event handler if enabled
204
+ try {
205
+ this.debugLog('Registering Webhook Event Handler');
206
+ this.registerWebhook();
207
+ } catch (e: any) {
208
+ this.errorLog(`failed to registerWebhook, Error: ${e}`);
209
+ }
210
+
211
+ //regisiter platform BLE event handler if enabled
212
+ try {
213
+ this.debugLog('Registering Platform BLE Event Handler');
214
+ this.registerPlatformBLE();
215
+ } catch (e: any) {
216
+ this.errorLog(`failed to registerPlatformBLE, Error: ${e}`);
217
+ }
195
218
 
196
219
  // History
197
220
  this.history();
@@ -205,7 +228,7 @@ export class Curtain extends deviceBase {
205
228
 
206
229
  // update slide progress
207
230
  interval(this.deviceUpdateRate * 1000)
208
- //.pipe(skipWhile(() => this.curtainUpdateInProgress))
231
+ .pipe(skipWhile(() => !this.curtainMoving))
209
232
  .subscribe(async () => {
210
233
  if (this.WindowCovering.PositionState === this.hap.Characteristic.PositionState.STOPPED) {
211
234
  return;
@@ -340,6 +363,7 @@ export class Curtain extends deviceBase {
340
363
  await this.infoLog('Checking Status ...');
341
364
  }
342
365
  if (this.setNewTarget && this.deviceStatus.moving) {
366
+ this.curtainMoving = true;
343
367
  await this.setMinMax();
344
368
  if (Number(this.WindowCovering.TargetPosition) > this.WindowCovering.CurrentPosition) {
345
369
  await this.debugLog(`Closing, CurrentPosition: ${this.WindowCovering.CurrentPosition}`);
@@ -358,6 +382,7 @@ export class Curtain extends deviceBase {
358
382
  await this.debugLog(`Stopped, PositionState: ${this.WindowCovering.PositionState}`);
359
383
  }
360
384
  } else {
385
+ this.curtainMoving = false;
361
386
  await this.debugLog(`Standby, CurrentPosition: ${this.WindowCovering.CurrentPosition}`);
362
387
  this.WindowCovering.TargetPosition = this.WindowCovering.CurrentPosition;
363
388
  this.WindowCovering.PositionState = this.hap.Characteristic.PositionState.STOPPED;
@@ -425,7 +450,7 @@ export class Curtain extends deviceBase {
425
450
  async refreshStatus(): Promise<void> {
426
451
  if (!this.device.enableCloudService && this.OpenAPI) {
427
452
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
428
- } else if (this.BLE || this.config.options?.BLE) {
453
+ } else if (this.BLE) {
429
454
  await this.BLERefreshStatus();
430
455
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
431
456
  await this.openAPIRefreshStatus();
@@ -437,42 +462,25 @@ export class Curtain extends deviceBase {
437
462
 
438
463
  async BLERefreshStatus(): Promise<void> {
439
464
  await this.debugLog('BLERefreshStatus');
440
- if (this.config.options?.BLE) {
441
- await this.debugLog('is listening to Platform BLE.');
442
- this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
443
- await this.debugLog(`bleMac: ${this.device.bleMac}`);
444
- this.platform.bleEventHandler[this.device.bleMac] = async (context: curtainServiceData | curtain3ServiceData) => {
445
- try {
446
- await this.debugLog(`received BLE: ${JSON.stringify(context)}`);
447
- this.serviceData = context;
448
- await this.BLEparseStatus();
449
- await this.updateHomeKitCharacteristics();
450
- } catch (e: any) {
451
- await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`);
452
- }
453
- };
465
+ const switchbot = await this.switchbotBLE();
466
+ if (switchbot === undefined) {
467
+ await this.BLERefreshConnection(switchbot);
454
468
  } else {
455
- await this.debugLog('is using Device BLE Scanning.');
456
- const switchbot = await this.switchbotBLE();
457
- if (switchbot === undefined) {
458
- await this.BLERefreshConnection(switchbot);
459
- } else {
469
+ // Start to monitor advertisement packets
470
+ (async () => {
460
471
  // Start to monitor advertisement packets
461
- (async () => {
462
- // Start to monitor advertisement packets
463
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as curtainServiceData | curtain3ServiceData;
464
- // Update HomeKit
465
- if ((serviceData.model === SwitchBotBLEModel.Curtain || SwitchBotBLEModel.Curtain3)
472
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as curtainServiceData | curtain3ServiceData;
473
+ // Update HomeKit
474
+ if ((serviceData.model === SwitchBotBLEModel.Curtain || SwitchBotBLEModel.Curtain3)
466
475
  && (serviceData.modelName === SwitchBotBLEModelName.Curtain || SwitchBotBLEModelName.Curtain3)) {
467
- this.serviceData = serviceData;
468
- await this.BLEparseStatus();
469
- await this.updateHomeKitCharacteristics();
470
- } else {
471
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
472
- await this.BLERefreshConnection(switchbot);
473
- }
474
- })();
475
- }
476
+ this.serviceData = serviceData;
477
+ await this.BLEparseStatus();
478
+ await this.updateHomeKitCharacteristics();
479
+ } else {
480
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
481
+ await this.BLERefreshConnection(switchbot);
482
+ }
483
+ })();
476
484
  }
477
485
  }
478
486
 
@@ -515,6 +523,27 @@ export class Curtain extends deviceBase {
515
523
  }
516
524
  }
517
525
 
526
+ async registerPlatformBLE(): Promise<void> {
527
+ await this.debugLog('registerPlatformBLE');
528
+ if (this.config.options?.BLE) {
529
+ await this.debugLog('is listening to Platform BLE.');
530
+ this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
531
+ await this.debugLog(`bleMac: ${this.device.bleMac}`);
532
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: curtainServiceData | curtain3ServiceData) => {
533
+ try {
534
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`);
535
+ this.serviceData = context;
536
+ await this.BLEparseStatus();
537
+ await this.updateHomeKitCharacteristics();
538
+ } catch (e: any) {
539
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`);
540
+ }
541
+ };
542
+ } else {
543
+ await this.debugLog('is not listening to Platform BLE');
544
+ }
545
+ }
546
+
518
547
  /**
519
548
  * Pushes the requested changes to the SwitchBot API
520
549
  */
@@ -761,6 +790,7 @@ export class Curtain extends deviceBase {
761
790
  await this.setMinMax();
762
791
  await this.debugLog(`CurrentPosition ${this.WindowCovering.CurrentPosition}`);
763
792
  if (this.setNewTarget) {
793
+ this.curtainMoving = true;
764
794
  this.infoLog('Checking Status ...');
765
795
  await this.setMinMax();
766
796
  if (this.WindowCovering.TargetPosition > this.WindowCovering.CurrentPosition) {
@@ -780,6 +810,7 @@ export class Curtain extends deviceBase {
780
810
  await this.debugLog(`Stopped, PositionState: ${this.WindowCovering.PositionState}`);
781
811
  }
782
812
  } else {
813
+ this.curtainMoving = false;
783
814
  await this.debugLog(`Standby, CurrentPosition: ${this.WindowCovering.CurrentPosition}`);
784
815
  this.WindowCovering.TargetPosition = this.WindowCovering.CurrentPosition;
785
816
  this.WindowCovering.PositionState = this.hap.Characteristic.PositionState.STOPPED;