@switchbot/homebridge-switchbot 3.5.0 → 3.5.1-beta.1

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 (137) hide show
  1. package/.github/npm-version-script.cjs +81 -0
  2. package/config.schema.json +4 -4
  3. package/dist/device/blindtilt.d.ts.map +1 -1
  4. package/dist/device/blindtilt.js +64 -189
  5. package/dist/device/blindtilt.js.map +1 -1
  6. package/dist/device/bot.d.ts.map +1 -1
  7. package/dist/device/bot.js +34 -68
  8. package/dist/device/bot.js.map +1 -1
  9. package/dist/device/ceilinglight.d.ts +1 -1
  10. package/dist/device/ceilinglight.d.ts.map +1 -1
  11. package/dist/device/ceilinglight.js +65 -158
  12. package/dist/device/ceilinglight.js.map +1 -1
  13. package/dist/device/colorbulb.d.ts +1 -1
  14. package/dist/device/colorbulb.d.ts.map +1 -1
  15. package/dist/device/colorbulb.js +47 -159
  16. package/dist/device/colorbulb.js.map +1 -1
  17. package/dist/device/contact.d.ts.map +1 -1
  18. package/dist/device/contact.js +42 -105
  19. package/dist/device/contact.js.map +1 -1
  20. package/dist/device/curtain.d.ts +1 -0
  21. package/dist/device/curtain.d.ts.map +1 -1
  22. package/dist/device/curtain.js +61 -182
  23. package/dist/device/curtain.js.map +1 -1
  24. package/dist/device/device.d.ts +41 -2
  25. package/dist/device/device.d.ts.map +1 -1
  26. package/dist/device/device.js +182 -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 +76 -145
  30. package/dist/device/fan.js.map +1 -1
  31. package/dist/device/hub.d.ts +7 -1
  32. package/dist/device/hub.d.ts.map +1 -1
  33. package/dist/device/hub.js +54 -17
  34. package/dist/device/hub.js.map +1 -1
  35. package/dist/device/humidifier.d.ts.map +1 -1
  36. package/dist/device/humidifier.js +55 -104
  37. package/dist/device/humidifier.js.map +1 -1
  38. package/dist/device/iosensor.d.ts.map +1 -1
  39. package/dist/device/iosensor.js +27 -46
  40. package/dist/device/iosensor.js.map +1 -1
  41. package/dist/device/lightstrip.d.ts +1 -1
  42. package/dist/device/lightstrip.d.ts.map +1 -1
  43. package/dist/device/lightstrip.js +56 -120
  44. package/dist/device/lightstrip.js.map +1 -1
  45. package/dist/device/lock.d.ts +1 -1
  46. package/dist/device/lock.d.ts.map +1 -1
  47. package/dist/device/lock.js +36 -66
  48. package/dist/device/lock.js.map +1 -1
  49. package/dist/device/meter.d.ts.map +1 -1
  50. package/dist/device/meter.js +27 -46
  51. package/dist/device/meter.js.map +1 -1
  52. package/dist/device/meterplus.d.ts.map +1 -1
  53. package/dist/device/meterplus.js +26 -45
  54. package/dist/device/meterplus.js.map +1 -1
  55. package/dist/device/motion.d.ts.map +1 -1
  56. package/dist/device/motion.js +24 -43
  57. package/dist/device/motion.js.map +1 -1
  58. package/dist/device/plug.d.ts.map +1 -1
  59. package/dist/device/plug.js +32 -66
  60. package/dist/device/plug.js.map +1 -1
  61. package/dist/device/robotvacuumcleaner.d.ts +1 -1
  62. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  63. package/dist/device/robotvacuumcleaner.js +43 -88
  64. package/dist/device/robotvacuumcleaner.js.map +1 -1
  65. package/dist/device/waterdetector.d.ts.map +1 -1
  66. package/dist/device/waterdetector.js +23 -43
  67. package/dist/device/waterdetector.js.map +1 -1
  68. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  69. package/dist/irdevice/airconditioner.js +10 -28
  70. package/dist/irdevice/airconditioner.js.map +1 -1
  71. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  72. package/dist/irdevice/airpurifier.js +8 -20
  73. package/dist/irdevice/airpurifier.js.map +1 -1
  74. package/dist/irdevice/camera.d.ts.map +1 -1
  75. package/dist/irdevice/camera.js +8 -20
  76. package/dist/irdevice/camera.js.map +1 -1
  77. package/dist/irdevice/fan.d.ts.map +1 -1
  78. package/dist/irdevice/fan.js +8 -20
  79. package/dist/irdevice/fan.js.map +1 -1
  80. package/dist/irdevice/irdevice.d.ts +7 -0
  81. package/dist/irdevice/irdevice.d.ts.map +1 -1
  82. package/dist/irdevice/irdevice.js +28 -0
  83. package/dist/irdevice/irdevice.js.map +1 -1
  84. package/dist/irdevice/light.d.ts.map +1 -1
  85. package/dist/irdevice/light.js +8 -20
  86. package/dist/irdevice/light.js.map +1 -1
  87. package/dist/irdevice/other.d.ts.map +1 -1
  88. package/dist/irdevice/other.js +8 -20
  89. package/dist/irdevice/other.js.map +1 -1
  90. package/dist/irdevice/tv.d.ts.map +1 -1
  91. package/dist/irdevice/tv.js +8 -20
  92. package/dist/irdevice/tv.js.map +1 -1
  93. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  94. package/dist/irdevice/vacuumcleaner.js +8 -20
  95. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  96. package/dist/irdevice/waterheater.d.ts.map +1 -1
  97. package/dist/irdevice/waterheater.js +8 -20
  98. package/dist/irdevice/waterheater.js.map +1 -1
  99. package/dist/settings.d.ts +4 -2
  100. package/dist/settings.d.ts.map +1 -1
  101. package/dist/settings.js.map +1 -1
  102. package/dist/utils.d.ts +19 -1
  103. package/dist/utils.d.ts.map +1 -1
  104. package/dist/utils.js +20 -1
  105. package/dist/utils.js.map +1 -1
  106. package/package.json +8 -8
  107. package/src/device/blindtilt.ts +71 -186
  108. package/src/device/bot.ts +32 -68
  109. package/src/device/ceilinglight.ts +67 -153
  110. package/src/device/colorbulb.ts +51 -155
  111. package/src/device/contact.ts +49 -104
  112. package/src/device/curtain.ts +70 -177
  113. package/src/device/device.ts +207 -8
  114. package/src/device/fan.ts +75 -147
  115. package/src/device/hub.ts +55 -19
  116. package/src/device/humidifier.ts +52 -104
  117. package/src/device/iosensor.ts +25 -46
  118. package/src/device/lightstrip.ts +54 -121
  119. package/src/device/lock.ts +35 -67
  120. package/src/device/meter.ts +25 -46
  121. package/src/device/meterplus.ts +24 -45
  122. package/src/device/motion.ts +22 -43
  123. package/src/device/plug.ts +30 -66
  124. package/src/device/robotvacuumcleaner.ts +40 -89
  125. package/src/device/waterdetector.ts +22 -43
  126. package/src/irdevice/airconditioner.ts +10 -26
  127. package/src/irdevice/airpurifier.ts +8 -20
  128. package/src/irdevice/camera.ts +8 -20
  129. package/src/irdevice/fan.ts +8 -20
  130. package/src/irdevice/irdevice.ts +29 -0
  131. package/src/irdevice/light.ts +8 -20
  132. package/src/irdevice/other.ts +8 -20
  133. package/src/irdevice/tv.ts +8 -20
  134. package/src/irdevice/vacuumcleaner.ts +8 -20
  135. package/src/irdevice/waterheater.ts +8 -20
  136. package/src/settings.ts +4 -2
  137. package/src/utils.ts +20 -1
@@ -0,0 +1,81 @@
1
+ #!/bin/env node
2
+
3
+ /**
4
+ * This scripts queries the npm registry to pull out the latest version for a given tag.
5
+ */
6
+
7
+ const fs = require("fs");
8
+ const semver = require("semver");
9
+ const child_process = require("child_process");
10
+ const assert = require("assert");
11
+
12
+ const BRANCH_VERSION_PATTERN = /^([A-Za-z]*)-(\d+.\d+.\d+)$/
13
+
14
+ // Load the contents of the package.json file
15
+ const packageJSON = JSON.parse(fs.readFileSync("package.json", "utf8"));
16
+
17
+ let refArgument = process.argv[2];
18
+ let tagArgument = process.argv[3] || "latest";
19
+
20
+ if (refArgument == null) {
21
+ console.error("ref argument is missing");
22
+ console.error("Usage: npm-version-script.cjs <ref> [tag]");
23
+ process.exit(1);
24
+ }
25
+
26
+ /**
27
+ * Queries the NPM registry for the latest version for the provided tag.
28
+ * @param tag The tag to query for.
29
+ * @returns {string} Returns the version.
30
+ */
31
+ function getTagVersionFromNpm(tag) {
32
+ try {
33
+ return child_process.execSync(`npm info ${packageJSON.name} version --tag="${tag}"`).toString("utf8").trim();
34
+ } catch (e) {
35
+ throw e;
36
+ }
37
+ }
38
+
39
+ function desiredTargetVersion(ref) {
40
+ // ref is a GitHub action ref string
41
+ if (ref.startsWith("refs/pull/")) {
42
+ throw Error("The version script was executed inside a PR!");
43
+ }
44
+
45
+ assert(ref.startsWith("refs/heads/"))
46
+ let branchName = ref.slice("refs/heads/".length);
47
+
48
+ let results = branchName.match(BRANCH_VERSION_PATTERN);
49
+ if (results != null) {
50
+ if (results[1] !== tagArgument) {
51
+ console.warn(`The base branch name (${results[1]}) differs from the tag name ${tagArgument}`);
52
+ }
53
+
54
+ return results[2];
55
+ }
56
+
57
+ // legacy mode were we use the `betaVersion` property in the package.json
58
+ if (branchName === "beta" && packageJSON.betaVersion) {
59
+ return packageJSON.betaVersion
60
+ }
61
+
62
+ throw new Error("Malformed branch name for ref: " + ref + ". Can't derive the base version. Use a branch name like: beta-x.x.x!");
63
+ }
64
+
65
+ // derive the base version from the branch ref
66
+ const baseVersion = desiredTargetVersion(refArgument);
67
+
68
+ // query the npm registry for the latest version of the provided tag name
69
+ const latestReleasedVersion = getTagVersionFromNpm(tagArgument); // e.g. 0.7.0-beta.12
70
+ const latestReleaseBase = semver.inc(latestReleasedVersion, "patch"); // will produce 0.7.0 (removing the preid, needed for the equality check below)
71
+
72
+ let publishTag;
73
+ if (semver.eq(baseVersion, latestReleaseBase)) { // check if we are releasing another version for the latest beta
74
+ publishTag = latestReleasedVersion; // set the current latest beta to be incremented
75
+ } else {
76
+ publishTag = baseVersion; // start of with a new beta version
77
+ }
78
+
79
+ // save the package.json
80
+ packageJSON.version = publishTag;
81
+ fs.writeFileSync("package.json", JSON.stringify(packageJSON, null, 2));
@@ -275,7 +275,7 @@
275
275
  ],
276
276
  "description": "Bluetooth (BLE) API is only available for the following Device Types: Humidifier, Meter, MeterPlus, Curtain, Bot, Motion Sensor, Contact Sensor, Plug Mini (US), Plug Mini (JP), & Color Bulb",
277
277
  "condition": {
278
- "functionBody": "return (model.options && model.options.devices && model.options.devices[arrayIndices].deviceId && !model.options.devices[arrayIndices].hide_device && model.options.devices[arrayIndices].configDeviceType !== 'Hub 2');"
278
+ "functionBody": "return (model.options && model.options.devices && model.options.devices[arrayIndices].deviceId && !model.options.devices[arrayIndices].hide_device);"
279
279
  }
280
280
  },
281
281
  "webhook": {
@@ -1887,15 +1887,15 @@
1887
1887
  {
1888
1888
  "key": "options.refreshRate",
1889
1889
  "description": "<em class='primary-text'>Specifies the interval, in seconds, for retrieving the latest device status from the SwitchBot API.</em>"
1890
- },
1890
+ },
1891
1891
  {
1892
1892
  "key": "options.updateRate",
1893
1893
  "description": "<em class='primary-text'>Specifies the interval, in seconds, at which devices will request updates from the SwitchBot API while in motion, for Curtain(s) and Blind Tilt(s) only.</em>"
1894
- },
1894
+ },
1895
1895
  {
1896
1896
  "key": "options.pushRate",
1897
1897
  "description": "<em class='primary-text'>Specifies the interval, in seconds, between pushes to the SwitchBot API.</em>"
1898
- },
1898
+ },
1899
1899
  "options.logging"
1900
1900
  ]
1901
1901
  }
@@ -1 +1 @@
1
- {"version":3,"file":"blindtilt.d.ts","sourceRoot":"","sources":["../../src/device/blindtilt.ts"],"names":[],"mappings":";AAMA,OAAO,EAAY,OAAO,EAAE,MAAM,MAAM,CAAC;AACzC,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAGnD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACxD,OAAO,KAAK,EAAW,iBAAiB,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAClF,OAAO,KAAK,EAAE,MAAM,EAAE,aAAa,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAGvF,qBAAa,SAAU,SAAQ,UAAU;IAsCrC,QAAQ,CAAC,QAAQ,EAAE,iBAAiB;IApCtC,OAAO,CAAC,cAAc,CAQpB;IAEF,OAAO,CAAC,OAAO,CAMb;IAEF,OAAO,CAAC,WAAW,CAAC,CAIlB;IAGF,WAAW,EAAE,oBAAoB,CAA+B;IAGhE,YAAY,EAAG,OAAO,CAAC;IACvB,iBAAiB,EAAG,MAAM,CAAC,OAAO,CAAC;IAGnC,yBAAyB,EAAG,OAAO,CAAC;I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/homebridge-switchbot.
4
4
  */
5
- import { request } from 'undici';
6
- import { Devices } from '../settings.js';
7
5
  import { interval, Subject } from 'rxjs';
8
6
  import { deviceBase } from './device.js';
9
7
  import { BlindTiltMappingMode } from '../utils.js';
@@ -218,13 +216,13 @@ export class BlindTilt extends deviceBase {
218
216
  }
219
217
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentPosition: ${this.WindowCovering.CurrentPosition},`
220
218
  + ` TargetPosition: ${this.WindowCovering.TargetPosition}, PositionState: ${this.WindowCovering.PositionState},`);
221
- if (!this.device.blindTilt?.hide_lightsensor) {
222
- const set_minLux = this.device.blindTilt?.set_minLux ?? 1;
223
- const set_maxLux = this.device.blindTilt?.set_maxLux ?? 6001;
224
- const spaceBetweenLevels = 9;
219
+ if (!this.device.blindTilt?.hide_lightsensor && this.LightSensor?.Service) {
225
220
  if (this.LightSensor?.CurrentAmbientLightLevel === 0) {
226
221
  this.LightSensor.CurrentAmbientLightLevel = 0.0001;
227
222
  }
223
+ const set_minLux = this.device.blindTilt?.set_minLux ?? 1;
224
+ const set_maxLux = this.device.blindTilt?.set_maxLux ?? 6001;
225
+ const spaceBetweenLevels = 9;
228
226
  // Brightness
229
227
  switch (serviceData.lightLevel) {
230
228
  case 1:
@@ -408,64 +406,46 @@ export class BlindTilt extends deviceBase {
408
406
  }
409
407
  async BLERefreshStatus() {
410
408
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
411
- const switchbot = await this.platform.connectBLE();
412
- // Convert to BLE Address
413
- this.device.bleMac = this.device
414
- .deviceId.match(/.{1,2}/g)
415
- .join(':')
416
- .toLowerCase();
417
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
418
- this.getCustomBLEAddress(switchbot);
419
- // Start to monitor advertisement packets
420
- (async () => {
409
+ const switchbot = await this.switchbotBLE();
410
+ if (switchbot === undefined) {
411
+ await this.BLERefreshConnection(switchbot);
412
+ }
413
+ else {
421
414
  // Start to monitor advertisement packets
422
- await switchbot.startScan({ model: this.device.bleModel, id: this.device.bleMac });
423
- // Set an event handler
424
- switchbot.onadvertisement = (ad) => {
425
- if (this.device.bleMac === ad.address && ad.model === this.device.bleModel) {
426
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
427
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.model}`);
428
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
415
+ (async () => {
416
+ // Start to monitor advertisement packets
417
+ const serviceData = await this.monitorAdvertisementPackets(switchbot);
418
+ // Update HomeKit
419
+ if (serviceData.model !== '' && serviceData.modelName !== '') {
420
+ await this.BLEparseStatus(serviceData);
421
+ await this.updateHomeKitCharacteristics();
429
422
  }
430
423
  else {
431
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
424
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed to get serviceData, serviceData: ${serviceData}`);
425
+ await this.BLERefreshConnection(switchbot);
432
426
  }
433
- };
434
- // Wait 10 seconds
435
- await switchbot.wait(this.scanDuration * 1000);
436
- // Stop to monitor
437
- await switchbot.stopScan();
438
- // Update HomeKit
439
- await this.BLEparseStatus(switchbot.onadvertisement.serviceData);
440
- await this.updateHomeKitCharacteristics();
441
- })();
442
- if (switchbot === undefined) {
443
- await this.BLERefreshConnection(switchbot);
427
+ })();
444
428
  }
445
429
  }
446
430
  async openAPIRefreshStatus() {
447
431
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
448
432
  try {
449
- const { body, statusCode } = await this.platform.retryRequest(this.deviceMaxRetries, this.deviceDelayBetweenRetries, `${Devices}/${this.device.deviceId}/status`, { headers: this.platform.generateHeaders() });
450
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
433
+ const { body, statusCode } = await this.deviceRefreshStatus();
451
434
  const deviceStatus = await body.json();
452
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
453
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
435
+ await this.refreshStatusCodes(statusCode, deviceStatus);
436
+ ;
454
437
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
455
- this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} `
456
- + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
457
- this.openAPIparseStatus(deviceStatus);
458
- this.updateHomeKitCharacteristics();
438
+ await this.successfulRefreshStatus(statusCode, deviceStatus);
439
+ await this.openAPIparseStatus(deviceStatus);
440
+ await this.updateHomeKitCharacteristics();
459
441
  }
460
442
  else {
461
- this.statusCode(statusCode);
462
- this.statusCode(deviceStatus.statusCode);
443
+ await this.statusCodes(statusCode, deviceStatus);
463
444
  }
464
445
  }
465
446
  catch (e) {
466
- this.apiError(e);
467
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
468
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
447
+ await this.apiError(e);
448
+ await this.openAPIRefreshError(e);
469
449
  }
470
450
  }
471
451
  async registerWebhook(accessory, device) {
@@ -485,7 +465,7 @@ export class BlindTilt extends deviceBase {
485
465
  if (!device.blindTilt?.hide_lightsensor) {
486
466
  this.LightSensor.CurrentAmbientLightLevel = lightLevel;
487
467
  }
488
- this.updateHomeKitCharacteristics();
468
+ await this.updateHomeKitCharacteristics();
489
469
  }
490
470
  catch (e) {
491
471
  this.errorLog(`${device.deviceType}: ${accessory.displayName} failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
@@ -508,8 +488,7 @@ export class BlindTilt extends deviceBase {
508
488
  }
509
489
  else {
510
490
  await this.offlineOff();
511
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
512
- + ` ${this.device.connectionType}, pushChanges will not happen.`);
491
+ await this.pushChangeDisabled();
513
492
  }
514
493
  // Refresh the status from the API
515
494
  interval(15000)
@@ -524,16 +503,12 @@ export class BlindTilt extends deviceBase {
524
503
  if (this.WindowCovering.TargetPosition !== this.WindowCovering.CurrentPosition) {
525
504
  const switchbot = await this.platform.connectBLE();
526
505
  // Convert to BLE Address
527
- this.device.bleMac = this.device
528
- .deviceId.match(/.{1,2}/g)
529
- .join(':')
530
- .toLowerCase();
531
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
506
+ await this.convertBLEAddress();
532
507
  const { setPositionMode, Mode } = await this.setPerformance();
533
508
  this.debugLog(`${this.accessory.displayName} Mode: ${Mode}`);
534
509
  if (switchbot !== false) {
535
510
  switchbot
536
- .discover({ model: 'c', quick: true, id: this.device.bleMac })
511
+ .discover({ model: this.device.bleModel, quick: true, id: this.device.bleMac })
537
512
  .then(async (device_list) => {
538
513
  this.infoLog(`${this.accessory.displayName} Target Position: ${this.WindowCovering.TargetPosition}`);
539
514
  return await this.retryBLE({
@@ -549,14 +524,13 @@ export class BlindTilt extends deviceBase {
549
524
  + `Target Position: ${this.WindowCovering.TargetPosition} sent over BLE, sent successfully`);
550
525
  })
551
526
  .catch(async (e) => {
552
- this.apiError(e);
553
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
554
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
527
+ await this.apiError(e);
528
+ await this.failedBLEChanges(e);
555
529
  await this.BLEPushConnection();
556
530
  });
557
531
  }
558
532
  else {
559
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
533
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection,`);
560
534
  await this.BLEPushConnection();
561
535
  }
562
536
  }
@@ -600,25 +574,16 @@ export class BlindTilt extends deviceBase {
600
574
  }
601
575
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
602
576
  try {
603
- const { body, statusCode } = await request(`${Devices}/${this.device.deviceId}/commands`, {
604
- body: bodyChange,
605
- method: 'POST',
606
- headers: this.platform.generateHeaders(),
607
- });
608
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
577
+ const { body, statusCode } = await this.pushChangeRequest(bodyChange);
609
578
  const deviceStatus = await body.json();
610
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
611
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
612
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
579
+ await this.pushStatusCodes(statusCode, deviceStatus);
613
580
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
614
- this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} `
615
- + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
616
- this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
617
- + `request to SwitchBot API, body: ${JSON.stringify(JSON.parse(bodyChange))} sent successfully`);
581
+ await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
582
+ await this.updateHomeKitCharacteristics();
618
583
  }
619
584
  else {
620
- this.statusCode(statusCode);
621
- this.statusCode(deviceStatus.statusCode);
585
+ await this.statusCode(statusCode);
586
+ await this.statusCode(deviceStatus.statusCode);
622
587
  }
623
588
  }
624
589
  catch (e) {
@@ -638,34 +603,30 @@ export class BlindTilt extends deviceBase {
638
603
  * Handle requests to set the value of the "Target Horizontal Tilt" characteristic
639
604
  */
640
605
  async TargetHorizontalTiltAngleSet(value) {
641
- if (this.WindowCovering.TargetHorizontalTiltAngle === this.accessory.context.TargetHorizontalTiltAngle) {
642
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set TargetHorizontalTiltAngle: ${value}`);
606
+ if (this.WindowCovering.TargetHorizontalTiltAngle !== this.accessory.context.TargetHorizontalTiltAngle) {
607
+ await this.changeSet(value, 'TargetHorizontalTiltAngle');
643
608
  }
644
609
  else {
645
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set TargetHorizontalTiltAngle: ${value}`);
610
+ await this.noChangeSet(value, 'TargetHorizontalTiltAngle');
646
611
  }
647
612
  //value = value < 0 ? -90 : 90;
648
613
  this.WindowCovering.TargetHorizontalTiltAngle = value;
649
- if (this.device.mqttURL) {
650
- this.mqttPublish('TargetHorizontalTiltAngle', this.WindowCovering.TargetHorizontalTiltAngle.toString());
651
- }
652
- this.startUpdatingBlindTiltIfNeeded();
614
+ await this.mqtt('TargetHorizontalTiltAngle', this.WindowCovering.TargetHorizontalTiltAngle);
615
+ await this.startUpdatingBlindTiltIfNeeded();
653
616
  }
654
617
  /**
655
618
  * Handle requests to set the value of the "Target Position" characteristic
656
619
  */
657
620
  async TargetPositionSet(value) {
658
- if (this.WindowCovering.TargetPosition === this.accessory.context.TargetPosition) {
659
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set TargetPosition: ${value}`);
621
+ if (this.WindowCovering.TargetPosition !== this.accessory.context.TargetPosition) {
622
+ await this.changeSet(value, 'TargetPosition');
660
623
  }
661
624
  else {
662
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set TargetPosition: ${value}`);
625
+ await this.noChangeSet(value, 'TargetPosition');
663
626
  }
664
627
  this.WindowCovering.TargetPosition = value;
665
- if (this.device.mqttURL) {
666
- this.mqttPublish('TargetPosition', this.WindowCovering.TargetPosition.toString());
667
- }
668
- this.startUpdatingBlindTiltIfNeeded();
628
+ await this.mqtt('TargetPosition', this.WindowCovering.TargetPosition);
629
+ await this.startUpdatingBlindTiltIfNeeded();
669
630
  }
670
631
  async startUpdatingBlindTiltIfNeeded() {
671
632
  await this.setMinMax();
@@ -709,112 +670,25 @@ export class BlindTilt extends deviceBase {
709
670
  await this.setMinMax();
710
671
  // CurrentHorizontalTiltAngle
711
672
  if (this.mappingMode === BlindTiltMappingMode.UseTiltForDirection) {
712
- if (this.WindowCovering.CurrentHorizontalTiltAngle === undefined || Number.isNaN(this.WindowCovering.CurrentHorizontalTiltAngle)) {
713
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
714
- + ` CurrentHorizontalTiltAngle: ${this.WindowCovering.CurrentHorizontalTiltAngle}`);
715
- }
716
- else {
717
- if (this.device.mqttURL) {
718
- this.mqttPublish('CurrentHorizontalTiltAngle', this.WindowCovering.CurrentHorizontalTiltAngle.toString());
719
- }
720
- this.accessory.context.CurrentHorizontalTiltAngle = this.WindowCovering.CurrentHorizontalTiltAngle;
721
- this.WindowCovering.Service.updateCharacteristic(this.hap.Characteristic.CurrentHorizontalTiltAngle, Number(this.WindowCovering.CurrentHorizontalTiltAngle));
722
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}
723
- updateCharacteristic CurrentHorizontalTiltAngle: ${this.WindowCovering.CurrentHorizontalTiltAngle}`);
724
- }
673
+ await this.updateCharacteristic(this.WindowCovering.Service, this.hap.Characteristic.CurrentHorizontalTiltAngle, this.WindowCovering.CurrentHorizontalTiltAngle, 'CurrentHorizontalTiltAngle');
725
674
  }
726
675
  // CurrentPosition
727
- if (this.WindowCovering.CurrentPosition === undefined || Number.isNaN(this.WindowCovering.CurrentPosition)) {
728
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentPosition: ${this.WindowCovering.CurrentPosition}`);
729
- }
730
- else {
731
- if (this.device.mqttURL) {
732
- this.mqttPublish('CurrentPosition', this.WindowCovering.CurrentPosition.toString());
733
- }
734
- this.accessory.context.CurrentPosition = this.WindowCovering.CurrentPosition;
735
- this.WindowCovering.Service.updateCharacteristic(this.hap.Characteristic.CurrentPosition, Number(this.WindowCovering.CurrentPosition));
736
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
737
- + ` updateCharacteristic CurrentPosition: ${this.WindowCovering.CurrentPosition}`);
738
- }
676
+ await this.updateCharacteristic(this.WindowCovering.Service, this.hap.Characteristic.CurrentPosition, this.WindowCovering.CurrentPosition, 'CurrentPosition');
739
677
  // PositionState
740
- if (this.WindowCovering.PositionState === undefined) {
741
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} PositionState: ${this.WindowCovering.PositionState}`);
742
- }
743
- else {
744
- if (this.device.mqttURL) {
745
- this.mqttPublish('PositionState', this.WindowCovering.PositionState.toString());
746
- }
747
- this.accessory.context.PositionState = this.WindowCovering.PositionState;
748
- this.WindowCovering.Service.updateCharacteristic(this.hap.Characteristic.PositionState, Number(this.WindowCovering.PositionState));
749
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
750
- + ` PositionState: ${this.WindowCovering.PositionState}`);
751
- }
678
+ await this.updateCharacteristic(this.WindowCovering.Service, this.hap.Characteristic.PositionState, this.WindowCovering.PositionState, 'PositionState');
752
679
  // TargetPosition
753
- if (this.WindowCovering.TargetPosition === undefined || Number.isNaN(this.WindowCovering.TargetPosition)) {
754
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} TargetPosition: ${this.WindowCovering.TargetPosition}`);
755
- }
756
- else {
757
- if (this.device.mqttURL) {
758
- this.mqttPublish('TargetPosition', this.WindowCovering.TargetPosition.toString());
759
- }
760
- this.accessory.context.TargetPosition = this.WindowCovering.TargetPosition;
761
- this.WindowCovering.Service.updateCharacteristic(this.hap.Characteristic.TargetPosition, Number(this.WindowCovering.TargetPosition));
762
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
763
- + ` TargetPosition: ${this.WindowCovering.TargetPosition}`);
764
- }
680
+ await this.updateCharacteristic(this.WindowCovering.Service, this.hap.Characteristic.TargetPosition, this.WindowCovering.TargetPosition, 'TargetPosition');
765
681
  // CurrentAmbientLightLevel
766
- if (!this.device.blindTilt?.hide_lightsensor) {
767
- if (this.LightSensor.CurrentAmbientLightLevel === undefined || Number.isNaN(this.LightSensor.CurrentAmbientLightLevel)) {
768
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
769
- + ` CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
770
- }
771
- else {
772
- if (this.device.mqttURL) {
773
- this.mqttPublish('CurrentAmbientLightLevel', this.LightSensor.CurrentAmbientLightLevel.toString());
774
- }
775
- this.accessory.context.CurrentAmbientLightLevel = this.LightSensor.CurrentAmbientLightLevel;
776
- this.LightSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, this.LightSensor.CurrentAmbientLightLevel);
777
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
778
- + ` updateCharacteristic CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
779
- }
682
+ if (!this.device.blindTilt?.hide_lightsensor && this.LightSensor?.Service) {
683
+ const history = { time: Math.round(new Date().valueOf() / 1000), lux: this.LightSensor.CurrentAmbientLightLevel };
684
+ await this.updateCharacteristic(this.LightSensor?.Service, this.hap.Characteristic.CurrentAmbientLightLevel, this.LightSensor?.CurrentAmbientLightLevel, 'CurrentAmbientLightLevel', history);
780
685
  }
781
686
  // BatteryLevel
782
- if (this.Battery.BatteryLevel === undefined) {
783
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}`);
784
- }
785
- else {
786
- if (this.device.mqttURL) {
787
- this.mqttPublish('BatteryLevel', this.Battery.BatteryLevel.toString());
788
- }
789
- this.accessory.context.BatteryLevel = this.Battery.BatteryLevel;
790
- this.Battery?.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.Battery.BatteryLevel);
791
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.Battery.BatteryLevel}`);
792
- }
687
+ await this.updateCharacteristic(this.Battery.Service, this.hap.Characteristic.BatteryLevel, this.Battery.BatteryLevel, 'BatteryLevel');
793
688
  // StatusLowBattery
794
- if (this.Battery.StatusLowBattery === undefined) {
795
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.Battery.StatusLowBattery}`);
796
- }
797
- else {
798
- if (this.device.mqttURL) {
799
- this.mqttPublish('StatusLowBattery', this.Battery.StatusLowBattery.toString());
800
- }
801
- this.accessory.context.StatusLowBattery = this.Battery.StatusLowBattery;
802
- this.Battery?.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.Battery.StatusLowBattery);
803
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
804
- + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
805
- }
806
- if (this.Battery.ChargingState === undefined) {
807
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ChargingState: ${this.Battery.ChargingState}`);
808
- }
809
- else {
810
- if (this.device.mqttURL) {
811
- this.mqttPublish('ChargingState', this.Battery.ChargingState.toString());
812
- }
813
- this.accessory.context.ChargingState = this.Battery.ChargingState;
814
- this.Battery.Service.updateCharacteristic(this.hap.Characteristic.ChargingState, this.Battery.ChargingState);
815
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
816
- + ` ChargingState: ${this.Battery.ChargingState}`);
817
- }
689
+ await this.updateCharacteristic(this.Battery.Service, this.hap.Characteristic.StatusLowBattery, this.Battery.StatusLowBattery, 'StatusLowBattery');
690
+ // ChargingState
691
+ await this.updateCharacteristic(this.Battery.Service, this.hap.Characteristic.ChargingState, this.Battery.ChargingState, 'ChargingState');
818
692
  }
819
693
  async BLEPushConnection() {
820
694
  if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
@@ -914,7 +788,8 @@ export class BlindTilt extends deviceBase {
914
788
  // device position 100 => closed up
915
789
  // homekit position 0 => closed
916
790
  // homekit position 100 => open
917
- deviceDirection;
791
+ const direction = deviceDirection === 'up' ? 'up' : 'down';
792
+ this.debugLog(`Mapping device values to homekit values, devicePostion: ${devicePosition}, deviceDirection: ${direction}`);
918
793
  switch (this.mappingMode) {
919
794
  case BlindTiltMappingMode.OnlyUp:
920
795
  // we only close upwards, so we see anything that is tilted downwards(<50) as open