@switchbot/homebridge-switchbot 3.6.1-beta.0 → 3.6.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 (106) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/config.schema.json +11 -0
  3. package/dist/device/blindtilt.d.ts.map +1 -1
  4. package/dist/device/blindtilt.js +36 -18
  5. package/dist/device/blindtilt.js.map +1 -1
  6. package/dist/device/bot.d.ts +1 -2
  7. package/dist/device/bot.d.ts.map +1 -1
  8. package/dist/device/bot.js +186 -396
  9. package/dist/device/bot.js.map +1 -1
  10. package/dist/device/ceilinglight.d.ts +3 -2
  11. package/dist/device/ceilinglight.d.ts.map +1 -1
  12. package/dist/device/ceilinglight.js +57 -32
  13. package/dist/device/ceilinglight.js.map +1 -1
  14. package/dist/device/colorbulb.d.ts +3 -2
  15. package/dist/device/colorbulb.d.ts.map +1 -1
  16. package/dist/device/colorbulb.js +58 -30
  17. package/dist/device/colorbulb.js.map +1 -1
  18. package/dist/device/contact.d.ts.map +1 -1
  19. package/dist/device/contact.js +36 -19
  20. package/dist/device/contact.js.map +1 -1
  21. package/dist/device/curtain.d.ts.map +1 -1
  22. package/dist/device/curtain.js +37 -18
  23. package/dist/device/curtain.js.map +1 -1
  24. package/dist/device/device.d.ts +9 -0
  25. package/dist/device/device.d.ts.map +1 -1
  26. package/dist/device/device.js +32 -5
  27. package/dist/device/device.js.map +1 -1
  28. package/dist/device/fan.d.ts.map +1 -1
  29. package/dist/device/fan.js +36 -19
  30. package/dist/device/fan.js.map +1 -1
  31. package/dist/device/hub.d.ts.map +1 -1
  32. package/dist/device/hub.js +38 -19
  33. package/dist/device/hub.js.map +1 -1
  34. package/dist/device/humidifier.d.ts.map +1 -1
  35. package/dist/device/humidifier.js +40 -21
  36. package/dist/device/humidifier.js.map +1 -1
  37. package/dist/device/iosensor.d.ts.map +1 -1
  38. package/dist/device/iosensor.js +38 -19
  39. package/dist/device/iosensor.js.map +1 -1
  40. package/dist/device/lightstrip.d.ts +4 -3
  41. package/dist/device/lightstrip.d.ts.map +1 -1
  42. package/dist/device/lightstrip.js +58 -32
  43. package/dist/device/lightstrip.js.map +1 -1
  44. package/dist/device/lock.d.ts.map +1 -1
  45. package/dist/device/lock.js +37 -18
  46. package/dist/device/lock.js.map +1 -1
  47. package/dist/device/meter.d.ts.map +1 -1
  48. package/dist/device/meter.js +38 -19
  49. package/dist/device/meter.js.map +1 -1
  50. package/dist/device/meterplus.d.ts.map +1 -1
  51. package/dist/device/meterplus.js +38 -19
  52. package/dist/device/meterplus.js.map +1 -1
  53. package/dist/device/motion.d.ts.map +1 -1
  54. package/dist/device/motion.js +36 -17
  55. package/dist/device/motion.js.map +1 -1
  56. package/dist/device/plug.d.ts.map +1 -1
  57. package/dist/device/plug.js +37 -18
  58. package/dist/device/plug.js.map +1 -1
  59. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  60. package/dist/device/robotvacuumcleaner.js +36 -17
  61. package/dist/device/robotvacuumcleaner.js.map +1 -1
  62. package/dist/device/waterdetector.d.ts.map +1 -1
  63. package/dist/device/waterdetector.js +36 -17
  64. package/dist/device/waterdetector.js.map +1 -1
  65. package/dist/irdevice/other.d.ts +1 -0
  66. package/dist/irdevice/other.d.ts.map +1 -1
  67. package/dist/irdevice/other.js +186 -228
  68. package/dist/irdevice/other.js.map +1 -1
  69. package/dist/platform.d.ts +4 -0
  70. package/dist/platform.d.ts.map +1 -1
  71. package/dist/platform.js +41 -0
  72. package/dist/platform.js.map +1 -1
  73. package/dist/settings.d.ts +1 -0
  74. package/dist/settings.d.ts.map +1 -1
  75. package/dist/settings.js.map +1 -1
  76. package/dist/types/bledevicestatus.d.ts +1 -0
  77. package/dist/types/bledevicestatus.d.ts.map +1 -1
  78. package/dist/utils.d.ts +8 -0
  79. package/dist/utils.d.ts.map +1 -1
  80. package/dist/utils.js +16 -0
  81. package/dist/utils.js.map +1 -1
  82. package/package.json +8 -8
  83. package/src/device/blindtilt.ts +33 -18
  84. package/src/device/bot.ts +179 -406
  85. package/src/device/ceilinglight.ts +54 -34
  86. package/src/device/colorbulb.ts +56 -32
  87. package/src/device/contact.ts +33 -20
  88. package/src/device/curtain.ts +34 -18
  89. package/src/device/device.ts +38 -5
  90. package/src/device/fan.ts +33 -20
  91. package/src/device/hub.ts +35 -19
  92. package/src/device/humidifier.ts +37 -21
  93. package/src/device/iosensor.ts +35 -19
  94. package/src/device/lightstrip.ts +57 -34
  95. package/src/device/lock.ts +34 -18
  96. package/src/device/meter.ts +35 -19
  97. package/src/device/meterplus.ts +35 -19
  98. package/src/device/motion.ts +33 -17
  99. package/src/device/plug.ts +34 -18
  100. package/src/device/robotvacuumcleaner.ts +33 -17
  101. package/src/device/waterdetector.ts +33 -17
  102. package/src/irdevice/other.ts +214 -283
  103. package/src/platform.ts +39 -0
  104. package/src/settings.ts +1 -0
  105. package/src/types/bledevicestatus.ts +1 -0
  106. package/src/utils.ts +15 -0
package/src/device/hub.ts CHANGED
@@ -4,8 +4,8 @@
4
4
  */
5
5
  import { Units } from 'homebridge';
6
6
  import { deviceBase } from './device.js';
7
- import { convertUnits } from '../utils.js';
8
7
  import { Subject, interval, skipWhile} from 'rxjs';
8
+ import { validHumidity, convertUnits } from '../utils.js';
9
9
  import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot';
10
10
 
11
11
  import type { devicesConfig } from '../settings.js';
@@ -181,7 +181,7 @@ export class Hub extends deviceBase {
181
181
 
182
182
  // CurrentRelativeHumidity
183
183
  if (!this.device.hub?.hide_humidity && this.HumiditySensor?.Service) {
184
- this.HumiditySensor!.CurrentRelativeHumidity = this.serviceData.humidity;
184
+ this.HumiditySensor!.CurrentRelativeHumidity = validHumidity(this.serviceData.humidity, 0, 100);
185
185
  await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}%`);
186
186
  }
187
187
 
@@ -278,7 +278,7 @@ export class Hub extends deviceBase {
278
278
  async refreshStatus(): Promise<void> {
279
279
  if (!this.device.enableCloudService && this.OpenAPI) {
280
280
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
281
- } else if (this.BLE) {
281
+ } else if (this.BLE || this.config.options?.BLE) {
282
282
  await this.BLERefreshStatus();
283
283
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
284
284
  await this.openAPIRefreshStatus();
@@ -290,25 +290,41 @@ export class Hub extends deviceBase {
290
290
 
291
291
  async BLERefreshStatus(): Promise<void> {
292
292
  await this.debugLog('BLERefreshStatus');
293
- const switchbot = await this.switchbotBLE();
294
-
295
- if (switchbot === undefined) {
296
- await this.BLERefreshConnection(switchbot);
297
- } else {
298
- // Start to monitor advertisement packets
299
- (async () => {
300
- // Start to monitor advertisement packets
301
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as hub2ServiceData;
302
- // Update HomeKit
303
- if (serviceData.model === SwitchBotBLEModel.Hub2 && serviceData.modelName === SwitchBotBLEModelName.Hub2) {
304
- this.serviceData = serviceData;
293
+ if (this.config.options?.BLE) {
294
+ await this.debugLog('is listening to Platform BLE.');
295
+ this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
296
+ await this.debugLog(`bleMac: ${this.device.bleMac}`);
297
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: hub2ServiceData) => {
298
+ try {
299
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`);
300
+ this.serviceData = context;
305
301
  await this.BLEparseStatus();
306
302
  await this.updateHomeKitCharacteristics();
307
- } else {
308
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
309
- await this.BLERefreshConnection(switchbot);
303
+ } catch (e: any) {
304
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`);
310
305
  }
311
- })();
306
+ };
307
+ } else {
308
+ await this.debugLog('is using Device BLE Scanning.');
309
+ const switchbot = await this.switchbotBLE();
310
+ if (switchbot === undefined) {
311
+ await this.BLERefreshConnection(switchbot);
312
+ } else {
313
+ // Start to monitor advertisement packets
314
+ (async () => {
315
+ // Start to monitor advertisement packets
316
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as hub2ServiceData;
317
+ // Update HomeKit
318
+ if (serviceData.model === SwitchBotBLEModel.Hub2 && serviceData.modelName === SwitchBotBLEModelName.Hub2) {
319
+ this.serviceData = serviceData;
320
+ await this.BLEparseStatus();
321
+ await this.updateHomeKitCharacteristics();
322
+ } else {
323
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
324
+ await this.BLERefreshConnection(switchbot);
325
+ }
326
+ })();
327
+ }
312
328
  }
313
329
  }
314
330
 
@@ -3,7 +3,7 @@
3
3
  * humidifier.ts: @switchbot/homebridge-switchbot.
4
4
  */
5
5
  import { deviceBase } from './device.js';
6
- import { convertUnits } from '../utils.js';
6
+ import { convertUnits, validHumidity } from '../utils.js';
7
7
  import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot';
8
8
  import { Subject, debounceTime, interval, skipWhile, take, tap } from 'rxjs';
9
9
 
@@ -197,7 +197,7 @@ export class Humidifier extends deviceBase {
197
197
  await this.debugLog(`Active: ${this.HumidifierDehumidifier.Active}`);
198
198
 
199
199
  // Current Relative Humidity
200
- this.HumidifierDehumidifier.CurrentRelativeHumidity = this.serviceData.percentage;
200
+ this.HumidifierDehumidifier.CurrentRelativeHumidity = validHumidity(this.serviceData.humidity);
201
201
  await this.debugLog(`CurrentRelativeHumidity: ${this.HumidifierDehumidifier.CurrentRelativeHumidity}`);
202
202
 
203
203
  // Target Humidifier Dehumidifier State
@@ -238,7 +238,7 @@ export class Humidifier extends deviceBase {
238
238
  await this.debugLog(`Active: ${this.HumidifierDehumidifier.Active}`);
239
239
 
240
240
  // Current Relative Humidity
241
- this.HumidifierDehumidifier.CurrentRelativeHumidity = this.deviceStatus.temperature!;
241
+ this.HumidifierDehumidifier.CurrentRelativeHumidity = validHumidity(this.deviceStatus.humidity);
242
242
  await this.debugLog(`CurrentRelativeHumidity: ${this.HumidifierDehumidifier.CurrentRelativeHumidity}`);
243
243
 
244
244
  // Current Temperature
@@ -301,7 +301,7 @@ export class Humidifier extends deviceBase {
301
301
  + ` ${this.HumidifierDehumidifier.CurrentRelativeHumidity})`);
302
302
 
303
303
  // CurrentRelativeHumidity
304
- this.HumidifierDehumidifier.CurrentRelativeHumidity = this.webhookContext.humidity;
304
+ this.HumidifierDehumidifier.CurrentRelativeHumidity = validHumidity(this.webhookContext.humidity);
305
305
  await this.debugLog(`CurrentRelativeHumidity: ${this.HumidifierDehumidifier.CurrentRelativeHumidity}`);
306
306
 
307
307
  // CurrentTemperature
@@ -318,7 +318,7 @@ export class Humidifier extends deviceBase {
318
318
  async refreshStatus(): Promise<void> {
319
319
  if (!this.device.enableCloudService && this.OpenAPI) {
320
320
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
321
- } else if (this.BLE) {
321
+ } else if (this.BLE || this.config.options?.BLE) {
322
322
  await this.BLERefreshStatus();
323
323
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
324
324
  await this.openAPIRefreshStatus();
@@ -330,25 +330,41 @@ export class Humidifier extends deviceBase {
330
330
 
331
331
  async BLERefreshStatus(): Promise<void> {
332
332
  await this.debugLog('BLERefreshStatus');
333
- const switchbot = await this.switchbotBLE();
334
-
335
- if (switchbot === undefined) {
336
- await this.BLERefreshConnection(switchbot);
337
- } else {
338
- // Start to monitor advertisement packets
339
- (async () => {
340
- // Start to monitor advertisement packets
341
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as humidifierServiceData;
342
- // Update HomeKit
343
- if (serviceData.model === SwitchBotBLEModel.Humidifier && serviceData.modelName === SwitchBotBLEModelName.Humidifier) {
344
- this.serviceData = serviceData;
333
+ if (this.config.options?.BLE) {
334
+ await this.debugLog('is listening to Platform BLE.');
335
+ this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
336
+ await this.debugLog(`bleMac: ${this.device.bleMac}`);
337
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: humidifierServiceData) => {
338
+ try {
339
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`);
340
+ this.serviceData = context;
345
341
  await this.BLEparseStatus();
346
342
  await this.updateHomeKitCharacteristics();
347
- } else {
348
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
349
- await this.BLERefreshConnection(switchbot);
343
+ } catch (e: any) {
344
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`);
350
345
  }
351
- })();
346
+ };
347
+ } else {
348
+ await this.debugLog('is using Device BLE Scanning.');
349
+ const switchbot = await this.switchbotBLE();
350
+ if (switchbot === undefined) {
351
+ await this.BLERefreshConnection(switchbot);
352
+ } else {
353
+ // Start to monitor advertisement packets
354
+ (async () => {
355
+ // Start to monitor advertisement packets
356
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as humidifierServiceData;
357
+ // Update HomeKit
358
+ if (serviceData.model === SwitchBotBLEModel.Humidifier && serviceData.modelName === SwitchBotBLEModelName.Humidifier) {
359
+ this.serviceData = serviceData;
360
+ await this.BLEparseStatus();
361
+ await this.updateHomeKitCharacteristics();
362
+ } else {
363
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
364
+ await this.BLERefreshConnection(switchbot);
365
+ }
366
+ })();
367
+ }
352
368
  }
353
369
  }
354
370
 
@@ -4,8 +4,8 @@
4
4
  */
5
5
  import { Units } from 'homebridge';
6
6
  import { deviceBase } from './device.js';
7
- import { convertUnits } from '../utils.js';
8
7
  import { Subject, interval, skipWhile } from 'rxjs';
8
+ import { convertUnits, validHumidity } from '../utils.js';
9
9
  import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot';
10
10
 
11
11
  import type { devicesConfig } from '../settings.js';
@@ -187,7 +187,7 @@ export class IOSensor extends deviceBase {
187
187
 
188
188
  // CurrentRelativeHumidity
189
189
  if (!this.device.iosensor?.hide_humidity && this.HumiditySensor?.Service) {
190
- this.HumiditySensor.CurrentRelativeHumidity = this.serviceData.humidity;
190
+ this.HumiditySensor.CurrentRelativeHumidity = validHumidity(this.serviceData.humidity, 0, 100);
191
191
  await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}%`);
192
192
  }
193
193
 
@@ -271,7 +271,7 @@ export class IOSensor extends deviceBase {
271
271
  async refreshStatus(): Promise<void> {
272
272
  if (!this.device.enableCloudService && this.OpenAPI) {
273
273
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
274
- } else if (this.BLE) {
274
+ } else if (this.BLE || this.config.options?.BLE) {
275
275
  await this.BLERefreshStatus();
276
276
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
277
277
  await this.openAPIRefreshStatus();
@@ -283,25 +283,41 @@ export class IOSensor extends deviceBase {
283
283
 
284
284
  async BLERefreshStatus(): Promise<void> {
285
285
  await this.debugLog('BLERefreshStatus');
286
- const switchbot = await this.switchbotBLE();
287
-
288
- if (switchbot === undefined) {
289
- await this.BLERefreshConnection(switchbot);
290
- } else {
291
- // Start to monitor advertisement packets
292
- (async () => {
293
- // Start to monitor advertisement packets
294
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as outdoorMeterServiceData;
295
- // Update HomeKit
296
- if (serviceData.model === SwitchBotBLEModel.OutdoorMeter && serviceData.modelName === SwitchBotBLEModelName.OutdoorMeter) {
297
- this.serviceData = serviceData;
286
+ if (this.config.options?.BLE) {
287
+ await this.debugLog('is listening to Platform BLE.');
288
+ this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
289
+ await this.debugLog(`bleMac: ${this.device.bleMac}`);
290
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: outdoorMeterServiceData) => {
291
+ try {
292
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`);
293
+ this.serviceData = context;
298
294
  await this.BLEparseStatus();
299
295
  await this.updateHomeKitCharacteristics();
300
- } else {
301
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
302
- await this.BLERefreshConnection(switchbot);
296
+ } catch (e: any) {
297
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`);
303
298
  }
304
- })();
299
+ };
300
+ } else {
301
+ await this.debugLog('is using Device BLE Scanning.');
302
+ const switchbot = await this.switchbotBLE();
303
+ if (switchbot === undefined) {
304
+ await this.BLERefreshConnection(switchbot);
305
+ } else {
306
+ // Start to monitor advertisement packets
307
+ (async () => {
308
+ // Start to monitor advertisement packets
309
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as outdoorMeterServiceData;
310
+ // Update HomeKit
311
+ if (serviceData.model === SwitchBotBLEModel.OutdoorMeter && serviceData.modelName === SwitchBotBLEModelName.OutdoorMeter) {
312
+ this.serviceData = serviceData;
313
+ await this.BLEparseStatus();
314
+ await this.updateHomeKitCharacteristics();
315
+ } else {
316
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
317
+ await this.BLERefreshConnection(switchbot);
318
+ }
319
+ })();
320
+ }
305
321
  }
306
322
  }
307
323
 
@@ -10,8 +10,8 @@ import { Subject, debounceTime, interval, skipWhile, take, tap } from 'rxjs';
10
10
  import type { devicesConfig } from '../settings.js';
11
11
  import type { device } from '../types/devicelist.js';
12
12
  import type { SwitchBotPlatform } from '../platform.js';
13
- import type { stripLightServiceData } from '../types/bledevicestatus.js';
14
13
  import type { stripLightStatus } from '../types/devicestatus.js';
14
+ import type { stripLightServiceData } from '../types/bledevicestatus.js';
15
15
  import type { stripLightWebhookContext } from '../types/devicewebhookstatus.js';
16
16
  import type { Service, PlatformAccessory, CharacteristicValue, ControllerConstructor, Controller, ControllerServiceMap } from 'homebridge';
17
17
 
@@ -42,8 +42,9 @@ export class StripLight extends deviceBase {
42
42
  serviceData!: stripLightServiceData;
43
43
 
44
44
  // Adaptive Lighting
45
+ adaptiveLighting!: boolean;
46
+ adaptiveLightingShift!: number;
45
47
  AdaptiveLightingController?: ControllerConstructor | Controller<ControllerServiceMap>;
46
- adaptiveLightingShift?: number;
47
48
 
48
49
  // Updates
49
50
  stripLightUpdateInProgress!: boolean;
@@ -59,7 +60,8 @@ export class StripLight extends deviceBase {
59
60
  accessory.category = this.hap.Categories.LIGHTBULB;
60
61
 
61
62
  // Adaptive Lighting
62
- this.adaptiveLighting(device);
63
+ this.getAdaptiveLightingSettings(accessory, device);
64
+
63
65
  // this is subject we use to track when we need to POST changes to the SwitchBot API
64
66
  this.doStripLightUpdate = new Subject();
65
67
  this.stripLightUpdateInProgress = false;
@@ -77,23 +79,24 @@ export class StripLight extends deviceBase {
77
79
  };
78
80
  accessory.context.LightBulb = this.LightBulb as object;
79
81
 
80
- // Adaptive Lighting
81
- if (this.adaptiveLightingShift === -1 && this.accessory.context.adaptiveLighting) {
82
- this.accessory.removeService(this.LightBulb.Service);
83
- this.LightBulb.Service = this.accessory.addService(this.hap.Service.Lightbulb);
84
- this.accessory.context.adaptiveLighting = false;
85
- this.debugLog(`adaptiveLighting: ${this.accessory.context.adaptiveLighting}`);
86
- }
87
- if (this.adaptiveLightingShift !== -1) {
82
+ if (this.adaptiveLighting && this.adaptiveLightingShift === -1 && this.LightBulb) {
83
+ accessory.removeService(this.LightBulb.Service);
84
+ this.LightBulb.Service = accessory.addService(this.hap.Service.Lightbulb);
85
+ accessory.context.adaptiveLighting = false;
86
+ this.debugLog(`adaptiveLighting: ${this.adaptiveLighting}`);
87
+ } else if (this.adaptiveLighting && this.adaptiveLightingShift >= 0 && this.LightBulb) {
88
88
  this.AdaptiveLightingController = new platform.api.hap.AdaptiveLightingController(this.LightBulb.Service, {
89
+ controllerMode: this.hap.AdaptiveLightingControllerMode.AUTOMATIC,
89
90
  customTemperatureAdjustment: this.adaptiveLightingShift,
90
91
  });
91
- this.accessory.configureController(this.AdaptiveLightingController);
92
- this.accessory.context.adaptiveLighting = true;
93
- this.debugLog(`adaptiveLighting: ${this.accessory.context.adaptiveLighting},`
94
- + ` adaptiveLightingShift: ${this.adaptiveLightingShift}`);
92
+ accessory.configureController(this.AdaptiveLightingController);
93
+ accessory.context.adaptiveLighting = true;
94
+ this.debugLog(`adaptiveLighting: ${this.adaptiveLighting}, adaptiveLightingShift: ${this.adaptiveLightingShift}`,
95
+ );
96
+ } else {
97
+ accessory.context.adaptiveLighting = false;
98
+ this.debugLog(`adaptiveLighting: ${accessory.context.adaptiveLighting}`);
95
99
  }
96
- this.debugLog(`adaptiveLightingShift: ${this.adaptiveLightingShift}`);
97
100
 
98
101
  // Initialize LightBulb Characteristics
99
102
  this.LightBulb.Service
@@ -301,7 +304,7 @@ export class StripLight extends deviceBase {
301
304
  async refreshStatus(): Promise<void> {
302
305
  if (!this.device.enableCloudService && this.OpenAPI) {
303
306
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
304
- } else if (this.BLE) {
307
+ } else if (this.BLE || this.config.options?.BLE) {
305
308
  await this.BLERefreshStatus();
306
309
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
307
310
  await this.openAPIRefreshStatus();
@@ -313,25 +316,41 @@ export class StripLight extends deviceBase {
313
316
 
314
317
  async BLERefreshStatus(): Promise<void> {
315
318
  await this.debugLog('BLERefreshStatus');
316
- const switchbot = await this.switchbotBLE();
317
-
318
- if (switchbot === undefined) {
319
- await this.BLERefreshConnection(switchbot);
320
- } else {
321
- // Start to monitor advertisement packets
322
- (async () => {
323
- // Start to monitor advertisement packets
324
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as stripLightServiceData;
325
- // Update HomeKit
326
- if (serviceData.model === SwitchBotBLEModel.StripLight && serviceData.modelName === SwitchBotBLEModelName.StripLight) {
327
- this.serviceData = serviceData;
319
+ if (this.config.options?.BLE) {
320
+ await this.debugLog('is listening to Platform BLE.');
321
+ this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
322
+ await this.debugLog(`bleMac: ${this.device.bleMac}`);
323
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: stripLightServiceData) => {
324
+ try {
325
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`);
326
+ this.serviceData = context;
328
327
  await this.BLEparseStatus();
329
328
  await this.updateHomeKitCharacteristics();
330
- } else {
331
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
332
- await this.BLERefreshConnection(switchbot);
329
+ } catch (e: any) {
330
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`);
333
331
  }
334
- })();
332
+ };
333
+ } else {
334
+ await this.debugLog('is using Device BLE Scanning.');
335
+ const switchbot = await this.switchbotBLE();
336
+ if (switchbot === undefined) {
337
+ await this.BLERefreshConnection(switchbot);
338
+ } else {
339
+ // Start to monitor advertisement packets
340
+ (async () => {
341
+ // Start to monitor advertisement packets
342
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as stripLightServiceData;
343
+ // Update HomeKit
344
+ if (serviceData.model === SwitchBotBLEModel.StripLight && serviceData.modelName === SwitchBotBLEModelName.StripLight) {
345
+ this.serviceData = serviceData;
346
+ await this.BLEparseStatus();
347
+ await this.updateHomeKitCharacteristics();
348
+ } else {
349
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
350
+ await this.BLERefreshConnection(switchbot);
351
+ }
352
+ })();
353
+ }
335
354
  }
336
355
  }
337
356
 
@@ -757,7 +776,11 @@ export class StripLight extends deviceBase {
757
776
  this.LightBulb.Saturation, 'Saturation');
758
777
  }
759
778
 
760
- async adaptiveLighting(device: device & devicesConfig): Promise<void> {
779
+ async getAdaptiveLightingSettings(accessory: PlatformAccessory, device: device & devicesConfig): Promise<void> {
780
+ // Adaptive Lighting
781
+ this.adaptiveLighting = accessory.context.adaptiveLighting ?? true;
782
+ await this.debugLog(`adaptiveLighting: ${this.adaptiveLighting}`);
783
+ // Adaptive Lighting Shift
761
784
  if (device.striplight?.adaptiveLightingShift) {
762
785
  this.adaptiveLightingShift = device.striplight.adaptiveLightingShift;
763
786
  this.debugLog(`adaptiveLightingShift: ${this.adaptiveLightingShift}`);
@@ -297,7 +297,7 @@ export class Lock extends deviceBase {
297
297
  async refreshStatus(): Promise<void> {
298
298
  if (!this.device.enableCloudService && this.OpenAPI) {
299
299
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
300
- } else if (this.BLE) {
300
+ } else if (this.BLE || this.config.options?.BLE) {
301
301
  await this.BLERefreshStatus();
302
302
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
303
303
  await this.openAPIRefreshStatus();
@@ -309,26 +309,42 @@ export class Lock extends deviceBase {
309
309
 
310
310
  async BLERefreshStatus(): Promise<void> {
311
311
  await this.debugLog('BLERefreshStatus');
312
- const switchbot = await this.switchbotBLE();
313
-
314
- if (switchbot === undefined) {
315
- await this.BLERefreshConnection(switchbot);
316
- } else {
317
- // Start to monitor advertisement packets
318
- (async () => {
319
- // Start to monitor advertisement packets
320
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as lockServiceData | lockProServiceData;
321
- // Update HomeKit
322
- if ((serviceData.model === SwitchBotBLEModel.Lock || SwitchBotBLEModel.LockPro)
323
- && (serviceData.modelName === SwitchBotBLEModelName.Lock || SwitchBotBLEModelName.LockPro)) {
324
- this.serviceData = serviceData;
312
+ if (this.config.options?.BLE) {
313
+ await this.debugLog('is listening to Platform BLE.');
314
+ this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
315
+ await this.debugLog(`bleMac: ${this.device.bleMac}`);
316
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: lockServiceData | lockProServiceData) => {
317
+ try {
318
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`);
319
+ this.serviceData = context;
325
320
  await this.BLEparseStatus();
326
321
  await this.updateHomeKitCharacteristics();
327
- } else {
328
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
329
- await this.BLERefreshConnection(switchbot);
322
+ } catch (e: any) {
323
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`);
330
324
  }
331
- })();
325
+ };
326
+ } else {
327
+ await this.debugLog('is using Device BLE Scanning.');
328
+ const switchbot = await this.switchbotBLE();
329
+ if (switchbot === undefined) {
330
+ await this.BLERefreshConnection(switchbot);
331
+ } else {
332
+ // Start to monitor advertisement packets
333
+ (async () => {
334
+ // Start to monitor advertisement packets
335
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as lockServiceData | lockProServiceData;
336
+ // Update HomeKit
337
+ if ((serviceData.model === SwitchBotBLEModel.Lock || SwitchBotBLEModel.LockPro)
338
+ && (serviceData.modelName === SwitchBotBLEModelName.Lock || SwitchBotBLEModelName.LockPro)) {
339
+ this.serviceData = serviceData;
340
+ await this.BLEparseStatus();
341
+ await this.updateHomeKitCharacteristics();
342
+ } else {
343
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
344
+ await this.BLERefreshConnection(switchbot);
345
+ }
346
+ })();
347
+ }
332
348
  }
333
349
  }
334
350
 
@@ -4,8 +4,8 @@
4
4
  */
5
5
  import { Units } from 'homebridge';
6
6
  import { deviceBase } from './device.js';
7
- import { convertUnits } from '../utils.js';
8
7
  import { Subject, interval, skipWhile } from 'rxjs';
8
+ import { convertUnits, validHumidity } from '../utils.js';
9
9
  import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot';
10
10
 
11
11
  import type { devicesConfig } from '../settings.js';
@@ -170,7 +170,7 @@ export class Meter extends deviceBase {
170
170
 
171
171
  // CurrentRelativeHumidity
172
172
  if (!this.device.iosensor?.hide_humidity && this.HumiditySensor?.Service) {
173
- this.HumiditySensor.CurrentRelativeHumidity = this.serviceData.humidity;
173
+ this.HumiditySensor.CurrentRelativeHumidity = validHumidity(this.serviceData.humidity, 0, 100);
174
174
  await this.debugLog(`CurrentRelativeHumidity: ${this.HumiditySensor.CurrentRelativeHumidity}%`);
175
175
  }
176
176
 
@@ -265,7 +265,7 @@ export class Meter extends deviceBase {
265
265
  async refreshStatus(): Promise<void> {
266
266
  if (!this.device.enableCloudService && this.OpenAPI) {
267
267
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
268
- } else if (this.BLE) {
268
+ } else if (this.BLE || this.config.options?.BLE) {
269
269
  await this.BLERefreshStatus();
270
270
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
271
271
  await this.openAPIRefreshStatus();
@@ -277,25 +277,41 @@ export class Meter extends deviceBase {
277
277
 
278
278
  async BLERefreshStatus(): Promise<void> {
279
279
  await this.debugLog('BLERefreshStatus');
280
- const switchbot = await this.switchbotBLE();
281
-
282
- if (switchbot === undefined) {
283
- await this.BLERefreshConnection(switchbot);
284
- } else {
285
- // Start to monitor advertisement packets
286
- (async () => {
287
- // Start to monitor advertisement packets
288
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as meterServiceData;
289
- // Update HomeKit
290
- if (serviceData.model === SwitchBotBLEModel.Meter && serviceData.modelName === SwitchBotBLEModelName.Meter) {
291
- this.serviceData = serviceData;
280
+ if (this.config.options?.BLE) {
281
+ await this.debugLog('is listening to Platform BLE.');
282
+ this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
283
+ await this.debugLog(`bleMac: ${this.device.bleMac}`);
284
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: meterServiceData) => {
285
+ try {
286
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`);
287
+ this.serviceData = context;
292
288
  await this.BLEparseStatus();
293
289
  await this.updateHomeKitCharacteristics();
294
- } else {
295
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
296
- await this.BLERefreshConnection(switchbot);
290
+ } catch (e: any) {
291
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`);
297
292
  }
298
- })();
293
+ };
294
+ } else {
295
+ await this.debugLog('is using Device BLE Scanning.');
296
+ const switchbot = await this.switchbotBLE();
297
+ if (switchbot === undefined) {
298
+ await this.BLERefreshConnection(switchbot);
299
+ } else {
300
+ // Start to monitor advertisement packets
301
+ (async () => {
302
+ // Start to monitor advertisement packets
303
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as meterServiceData;
304
+ // Update HomeKit
305
+ if (serviceData.model === SwitchBotBLEModel.Meter && serviceData.modelName === SwitchBotBLEModelName.Meter) {
306
+ this.serviceData = serviceData;
307
+ await this.BLEparseStatus();
308
+ await this.updateHomeKitCharacteristics();
309
+ } else {
310
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
311
+ await this.BLERefreshConnection(switchbot);
312
+ }
313
+ })();
314
+ }
299
315
  }
300
316
  }
301
317