@switchbot/homebridge-switchbot 3.5.0-beta.9 → 3.5.0

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 (178) hide show
  1. package/.vscode/settings.json +4 -1
  2. package/CHANGELOG.md +4 -1
  3. package/config.schema.json +166 -24
  4. package/dist/device/blindtilt.d.ts +21 -96
  5. package/dist/device/blindtilt.d.ts.map +1 -1
  6. package/dist/device/blindtilt.js +439 -817
  7. package/dist/device/blindtilt.js.map +1 -1
  8. package/dist/device/bot.d.ts +27 -72
  9. package/dist/device/bot.d.ts.map +1 -1
  10. package/dist/device/bot.js +835 -865
  11. package/dist/device/bot.js.map +1 -1
  12. package/dist/device/ceilinglight.d.ts +15 -69
  13. package/dist/device/ceilinglight.d.ts.map +1 -1
  14. package/dist/device/ceilinglight.js +287 -653
  15. package/dist/device/ceilinglight.js.map +1 -1
  16. package/dist/device/colorbulb.d.ts +15 -76
  17. package/dist/device/colorbulb.d.ts.map +1 -1
  18. package/dist/device/colorbulb.js +354 -732
  19. package/dist/device/colorbulb.js.map +1 -1
  20. package/dist/device/contact.d.ts +14 -66
  21. package/dist/device/contact.d.ts.map +1 -1
  22. package/dist/device/contact.js +268 -576
  23. package/dist/device/contact.js.map +1 -1
  24. package/dist/device/curtain.d.ts +18 -89
  25. package/dist/device/curtain.d.ts.map +1 -1
  26. package/dist/device/curtain.js +431 -851
  27. package/dist/device/curtain.js.map +1 -1
  28. package/dist/device/device.d.ts +60 -0
  29. package/dist/device/device.d.ts.map +1 -0
  30. package/dist/device/device.js +739 -0
  31. package/dist/device/device.js.map +1 -0
  32. package/dist/device/fan.d.ts +63 -0
  33. package/dist/device/fan.d.ts.map +1 -0
  34. package/dist/device/fan.js +698 -0
  35. package/dist/device/fan.js.map +1 -0
  36. package/dist/device/hub.d.ts +15 -60
  37. package/dist/device/hub.d.ts.map +1 -1
  38. package/dist/device/hub.js +257 -631
  39. package/dist/device/hub.js.map +1 -1
  40. package/dist/device/humidifier.d.ts +11 -66
  41. package/dist/device/humidifier.d.ts.map +1 -1
  42. package/dist/device/humidifier.js +266 -629
  43. package/dist/device/humidifier.js.map +1 -1
  44. package/dist/device/iosensor.d.ts +12 -64
  45. package/dist/device/iosensor.d.ts.map +1 -1
  46. package/dist/device/iosensor.js +215 -615
  47. package/dist/device/iosensor.js.map +1 -1
  48. package/dist/device/lightstrip.d.ts +12 -70
  49. package/dist/device/lightstrip.d.ts.map +1 -1
  50. package/dist/device/lightstrip.js +336 -645
  51. package/dist/device/lightstrip.js.map +1 -1
  52. package/dist/device/lock.d.ts +17 -63
  53. package/dist/device/lock.d.ts.map +1 -1
  54. package/dist/device/lock.js +290 -595
  55. package/dist/device/lock.js.map +1 -1
  56. package/dist/device/meter.d.ts +15 -64
  57. package/dist/device/meter.d.ts.map +1 -1
  58. package/dist/device/meter.js +211 -608
  59. package/dist/device/meter.js.map +1 -1
  60. package/dist/device/meterplus.d.ts +15 -65
  61. package/dist/device/meterplus.d.ts.map +1 -1
  62. package/dist/device/meterplus.js +205 -615
  63. package/dist/device/meterplus.js.map +1 -1
  64. package/dist/device/motion.d.ts +12 -69
  65. package/dist/device/motion.d.ts.map +1 -1
  66. package/dist/device/motion.js +202 -563
  67. package/dist/device/motion.js.map +1 -1
  68. package/dist/device/plug.d.ts +10 -57
  69. package/dist/device/plug.d.ts.map +1 -1
  70. package/dist/device/plug.js +125 -484
  71. package/dist/device/plug.js.map +1 -1
  72. package/dist/device/robotvacuumcleaner.d.ts +13 -63
  73. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  74. package/dist/device/robotvacuumcleaner.js +233 -549
  75. package/dist/device/robotvacuumcleaner.js.map +1 -1
  76. package/dist/device/waterdetector.d.ts +11 -63
  77. package/dist/device/waterdetector.d.ts.map +1 -1
  78. package/dist/device/waterdetector.js +170 -493
  79. package/dist/device/waterdetector.js.map +1 -1
  80. package/dist/homebridge-ui/public/index.html +22 -16
  81. package/dist/homebridge-ui/server.js +1 -1
  82. package/dist/homebridge-ui/server.js.map +1 -1
  83. package/dist/index.d.ts +1 -1
  84. package/dist/index.d.ts.map +1 -1
  85. package/dist/index.js +2 -2
  86. package/dist/index.js.map +1 -1
  87. package/dist/irdevice/airconditioner.d.ts +10 -50
  88. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  89. package/dist/irdevice/airconditioner.js +340 -468
  90. package/dist/irdevice/airconditioner.js.map +1 -1
  91. package/dist/irdevice/airpurifier.d.ts +8 -39
  92. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  93. package/dist/irdevice/airpurifier.js +105 -319
  94. package/dist/irdevice/airpurifier.js.map +1 -1
  95. package/dist/irdevice/camera.d.ts +7 -36
  96. package/dist/irdevice/camera.d.ts.map +1 -1
  97. package/dist/irdevice/camera.js +44 -285
  98. package/dist/irdevice/camera.js.map +1 -1
  99. package/dist/irdevice/fan.d.ts +7 -43
  100. package/dist/irdevice/fan.d.ts.map +1 -1
  101. package/dist/irdevice/fan.js +92 -350
  102. package/dist/irdevice/fan.js.map +1 -1
  103. package/dist/irdevice/irdevice.d.ts +40 -0
  104. package/dist/irdevice/irdevice.d.ts.map +1 -0
  105. package/dist/irdevice/irdevice.js +369 -0
  106. package/dist/irdevice/irdevice.js.map +1 -0
  107. package/dist/irdevice/light.d.ts +12 -45
  108. package/dist/irdevice/light.d.ts.map +1 -1
  109. package/dist/irdevice/light.js +118 -362
  110. package/dist/irdevice/light.js.map +1 -1
  111. package/dist/irdevice/other.d.ts +19 -48
  112. package/dist/irdevice/other.d.ts.map +1 -1
  113. package/dist/irdevice/other.js +438 -570
  114. package/dist/irdevice/other.js.map +1 -1
  115. package/dist/irdevice/tv.d.ts +8 -40
  116. package/dist/irdevice/tv.d.ts.map +1 -1
  117. package/dist/irdevice/tv.js +91 -339
  118. package/dist/irdevice/tv.js.map +1 -1
  119. package/dist/irdevice/vacuumcleaner.d.ts +7 -36
  120. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  121. package/dist/irdevice/vacuumcleaner.js +45 -285
  122. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  123. package/dist/irdevice/waterheater.d.ts +7 -36
  124. package/dist/irdevice/waterheater.d.ts.map +1 -1
  125. package/dist/irdevice/waterheater.js +47 -291
  126. package/dist/irdevice/waterheater.js.map +1 -1
  127. package/dist/platform.d.ts +22 -8
  128. package/dist/platform.d.ts.map +1 -1
  129. package/dist/platform.js +342 -191
  130. package/dist/platform.js.map +1 -1
  131. package/dist/settings.d.ts +64 -17
  132. package/dist/settings.d.ts.map +1 -1
  133. package/dist/settings.js +16 -552
  134. package/dist/settings.js.map +1 -1
  135. package/dist/utils.d.ts +77 -0
  136. package/dist/utils.d.ts.map +1 -1
  137. package/dist/utils.js +638 -1
  138. package/dist/utils.js.map +1 -1
  139. package/eslint.config.js +96 -0
  140. package/package.json +20 -16
  141. package/src/custom.d.ts +2 -1
  142. package/src/device/blindtilt.ts +472 -888
  143. package/src/device/bot.ts +893 -968
  144. package/src/device/ceilinglight.ts +304 -709
  145. package/src/device/colorbulb.ts +370 -803
  146. package/src/device/contact.ts +301 -611
  147. package/src/device/curtain.ts +460 -908
  148. package/src/device/device.ts +755 -0
  149. package/src/device/fan.ts +749 -0
  150. package/src/device/hub.ts +294 -664
  151. package/src/device/humidifier.ts +299 -696
  152. package/src/device/iosensor.ts +251 -644
  153. package/src/device/lightstrip.ts +362 -712
  154. package/src/device/lock.ts +328 -653
  155. package/src/device/meter.ts +247 -637
  156. package/src/device/meterplus.ts +240 -644
  157. package/src/device/motion.ts +230 -598
  158. package/src/device/plug.ts +139 -525
  159. package/src/device/robotvacuumcleaner.ts +258 -593
  160. package/src/device/waterdetector.ts +191 -522
  161. package/src/homebridge-ui/public/index.html +22 -16
  162. package/src/homebridge-ui/server.ts +1 -1
  163. package/src/index.ts +4 -3
  164. package/src/irdevice/airconditioner.ts +370 -527
  165. package/src/irdevice/airpurifier.ts +132 -356
  166. package/src/irdevice/camera.ts +58 -311
  167. package/src/irdevice/fan.ts +109 -379
  168. package/src/irdevice/irdevice.ts +382 -0
  169. package/src/irdevice/light.ts +157 -412
  170. package/src/irdevice/other.ts +507 -648
  171. package/src/irdevice/tv.ts +121 -375
  172. package/src/irdevice/vacuumcleaner.ts +59 -311
  173. package/src/irdevice/waterheater.ts +61 -320
  174. package/src/platform.ts +395 -259
  175. package/src/settings.ts +118 -609
  176. package/src/utils.ts +642 -1
  177. package/.github/npm-version-script.cjs +0 -81
  178. package/src/device/waterdetector.json +0 -11
@@ -1,298 +1,255 @@
1
- import { request } from 'undici';
2
- import { sleep } from '../utils.js';
1
+ /* Copyright(C) 2021-2024, donavanbecker (https://github.com/donavanbecker). All rights reserved.
2
+ *
3
+ * contact.ts: @switchbot/homebridge-switchbot.
4
+ */
5
+ import { deviceBase } from './device.js';
3
6
  import { interval, Subject } from 'rxjs';
4
- import { skipWhile } from 'rxjs/operators';
5
7
  import { Devices } from '../settings.js';
8
+ import { skipWhile } from 'rxjs/operators';
6
9
  /**
7
10
  * Platform Accessory
8
11
  * An instance of this class is created for each accessory your platform registers
9
12
  * Each accessory may expose multiple services of different service types.
10
13
  */
11
- export class Contact {
14
+ export class Contact extends deviceBase {
12
15
  platform;
13
- accessory;
14
- device;
15
- api;
16
- log;
17
- config;
18
- hap;
19
16
  // Services
20
- motionService;
21
- batteryService;
22
- lightSensorService;
23
- contactSensorservice;
24
- // Characteristic Values
25
- BatteryLevel;
26
- MotionDetected;
27
- StatusLowBattery;
28
- FirmwareRevision;
29
- ContactSensorState;
30
- CurrentAmbientLightLevel;
31
- // OpenAPI Status
32
- OpenAPI_BatteryLevel;
33
- OpenAPI_FirmwareRevision;
34
- OpenAPI_MotionDetected;
35
- OpenAPI_ContactSensorState;
36
- OpenAPI_CurrentAmbientLightLevel;
37
- // BLE Status
38
- BLE_BatteryLevel;
39
- BLE_MotionDetected;
40
- BLE_ContactSensorState;
41
- BLE_CurrentAmbientLightLevel;
42
- // BLE Others
43
- scanning;
44
- BLE_IsConnected;
45
- // Config
46
- set_minLux;
47
- set_maxLux;
48
- scanDuration;
49
- deviceLogging;
50
- deviceRefreshRate;
17
+ ContactSensor;
18
+ Battery;
19
+ MotionSensor;
20
+ LightSensor;
51
21
  // Updates
52
- contactUbpdateInProgress;
22
+ contactUpdateInProgress;
53
23
  doContactUpdate;
54
- // Connection
55
- OpenAPI;
56
- BLE;
57
24
  constructor(platform, accessory, device) {
25
+ super(platform, accessory, device);
58
26
  this.platform = platform;
59
- this.accessory = accessory;
60
- this.device = device;
61
- this.api = this.platform.api;
62
- this.log = this.platform.log;
63
- this.config = this.platform.config;
64
- this.hap = this.api.hap;
65
- // Connection
66
- this.BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
67
- this.OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
68
- // default placeholders
69
- this.deviceLogs(device);
70
- this.scan(device);
71
- this.refreshRate(device);
72
- this.deviceContext();
73
- this.deviceConfig(device);
74
27
  // this is subject we use to track when we need to POST changes to the SwitchBot API
75
28
  this.doContactUpdate = new Subject();
76
- this.contactUbpdateInProgress = false;
77
- // Retrieve initial values and updateHomekit
78
- this.refreshStatus();
79
- // set accessory information
80
- accessory
81
- .getService(this.hap.Service.AccessoryInformation)
82
- .setCharacteristic(this.hap.Characteristic.Manufacturer, 'SwitchBot')
83
- .setCharacteristic(this.hap.Characteristic.Model, 'W1201500')
84
- .setCharacteristic(this.hap.Characteristic.SerialNumber, device.deviceId)
85
- .setCharacteristic(this.hap.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
86
- // get the Contact service if it exists, otherwise create a new Contact service
87
- // you can create multiple services for each accessory
88
- const contactSensorservice = `${accessory.displayName} Contact Sensor`;
89
- (this.contactSensorservice = accessory.getService(this.hap.Service.ContactSensor)
90
- || accessory.addService(this.hap.Service.ContactSensor)), contactSensorservice;
91
- this.contactSensorservice.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
92
- if (!this.contactSensorservice.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
93
- this.contactSensorservice.addCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.displayName);
94
- }
95
- // Motion Sensor Service
96
- if (device.contact?.hide_motionsensor) {
97
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Motion Sensor Service`);
98
- this.motionService = this.accessory.getService(this.hap.Service.MotionSensor);
99
- accessory.removeService(this.motionService);
100
- }
101
- else if (!this.motionService) {
102
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Motion Sensor Service`);
103
- const motionService = `${accessory.displayName} Motion Sensor`;
104
- (this.motionService = this.accessory.getService(this.hap.Service.MotionSensor)
105
- || this.accessory.addService(this.hap.Service.MotionSensor)), motionService;
106
- this.motionService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Motion Sensor`);
107
- this.motionService.setCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Motion Sensor`);
29
+ this.contactUpdateInProgress = false;
30
+ // Initialize Contact Sensor Service
31
+ accessory.context.ContactSensor = accessory.context.ContactSensor ?? {};
32
+ this.ContactSensor = {
33
+ Name: accessory.context.ContactSensor.Name ?? accessory.displayName,
34
+ Service: accessory.getService(this.hap.Service.ContactSensor) ?? accessory.addService(this.hap.Service.ContactSensor),
35
+ ContactSensorState: accessory.context.ContactSensorState ?? this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED,
36
+ };
37
+ accessory.context.ContactSensor = this.ContactSensor;
38
+ // Initialize ContactSensor Characteristics
39
+ this.ContactSensor.Service
40
+ .setCharacteristic(this.hap.Characteristic.Name, this.ContactSensor.Name)
41
+ .setCharacteristic(this.hap.Characteristic.StatusActive, true)
42
+ .getCharacteristic(this.hap.Characteristic.ContactSensorState)
43
+ .onGet(() => {
44
+ return this.ContactSensor.ContactSensorState;
45
+ });
46
+ // Initialize Battery Service
47
+ accessory.context.Battery = accessory.context.Battery ?? {};
48
+ this.Battery = {
49
+ Name: accessory.context.Battery.Name ?? `${accessory.displayName} Battery`,
50
+ Service: accessory.getService(this.hap.Service.Battery) ?? accessory.addService(this.hap.Service.Battery),
51
+ BatteryLevel: accessory.context.BatteryLevel ?? 100,
52
+ StatusLowBattery: accessory.context.StatusLowBattery ?? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL,
53
+ };
54
+ accessory.context.Battery = this.Battery;
55
+ // Initialize Battery Characteristics
56
+ this.Battery.Service
57
+ .setCharacteristic(this.hap.Characteristic.Name, this.Battery.Name)
58
+ .setCharacteristic(this.hap.Characteristic.ChargingState, this.hap.Characteristic.ChargingState.NOT_CHARGEABLE)
59
+ .getCharacteristic(this.hap.Characteristic.BatteryLevel)
60
+ .onGet(() => {
61
+ return this.Battery.BatteryLevel;
62
+ });
63
+ this.Battery.Service
64
+ .setCharacteristic(this.hap.Characteristic.StatusLowBattery, this.Battery.StatusLowBattery)
65
+ .getCharacteristic(this.hap.Characteristic.StatusLowBattery)
66
+ .onGet(() => {
67
+ return this.Battery.StatusLowBattery;
68
+ });
69
+ // Initialize Motion Sensor Service
70
+ if (this.device.contact?.hide_motionsensor) {
71
+ if (this.MotionSensor) {
72
+ this.debugLog(`${device.deviceType}: ${accessory.displayName} Removing Motion Sensor Service`);
73
+ this.MotionSensor.Service = accessory.getService(this.hap.Service.MotionSensor);
74
+ accessory.removeService(this.MotionSensor.Service);
75
+ }
108
76
  }
109
77
  else {
110
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Motion Sensor Service Not Added`);
78
+ accessory.context.MotionSensor = accessory.context.MotionSensor ?? {};
79
+ this.MotionSensor = {
80
+ Name: accessory.context.MotionSensor.Name ?? `${accessory.displayName} Motion Sensor`,
81
+ Service: accessory.getService(this.hap.Service.MotionSensor) ?? accessory.addService(this.hap.Service.MotionSensor),
82
+ MotionDetected: accessory.context.MotionDetected ?? false,
83
+ };
84
+ accessory.context.MotionSensor = this.MotionSensor;
85
+ // Motion Sensor Characteristics
86
+ this.MotionSensor.Service
87
+ .setCharacteristic(this.hap.Characteristic.Name, this.MotionSensor.Name)
88
+ .setCharacteristic(this.hap.Characteristic.StatusActive, true)
89
+ .getCharacteristic(this.hap.Characteristic.MotionDetected)
90
+ .onGet(() => {
91
+ return this.MotionSensor.MotionDetected;
92
+ });
111
93
  }
112
- // Light Sensor Service
94
+ // Initialize Light Sensor Service
113
95
  if (device.contact?.hide_lightsensor) {
114
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Light Sensor Service`);
115
- this.lightSensorService = this.accessory.getService(this.hap.Service.LightSensor);
116
- accessory.removeService(this.lightSensorService);
117
- }
118
- else if (!this.lightSensorService) {
119
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Light Sensor Service`);
120
- const lightSensorService = `${accessory.displayName} Light Sensor`;
121
- (this.lightSensorService = this.accessory.getService(this.hap.Service.LightSensor)
122
- || this.accessory.addService(this.hap.Service.LightSensor)), lightSensorService;
123
- this.lightSensorService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Light Sensor`);
124
- this.lightSensorService.setCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Light Sensor`);
96
+ if (this.LightSensor) {
97
+ this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Light Sensor Service`);
98
+ this.LightSensor.Service = accessory.getService(this.hap.Service.LightSensor);
99
+ accessory.removeService(this.LightSensor.Service);
100
+ }
125
101
  }
126
102
  else {
127
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Light Sensor Service Not Added`);
128
- }
129
- // Battery Service
130
- const batteryService = `${accessory.displayName} Battery`;
131
- (this.batteryService = this.accessory.getService(this.hap.Service.Battery)
132
- || accessory.addService(this.hap.Service.Battery)), batteryService;
133
- this.batteryService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Battery`);
134
- if (!this.batteryService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
135
- this.batteryService.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
103
+ accessory.context.LightSensor = accessory.context.LightSensor ?? {};
104
+ this.LightSensor = {
105
+ Name: accessory.context.LightSensor.Name ?? `${accessory.displayName} Light Sensor`,
106
+ Service: accessory.getService(this.hap.Service.LightSensor) ?? accessory.addService(this.hap.Service.LightSensor),
107
+ CurrentAmbientLightLevel: accessory.context.CurrentAmbientLightLevel ?? 0.0001,
108
+ };
109
+ accessory.context.LightSensor = this.LightSensor;
110
+ // Light Sensor Characteristics
111
+ this.LightSensor.Service
112
+ .setCharacteristic(this.hap.Characteristic.Name, this.LightSensor.Name)
113
+ .setCharacteristic(this.hap.Characteristic.StatusActive, true)
114
+ .getCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel)
115
+ .onGet(() => {
116
+ return this.LightSensor.CurrentAmbientLightLevel;
117
+ });
136
118
  }
137
- this.batteryService.setCharacteristic(this.hap.Characteristic.ChargingState, this.hap.Characteristic.ChargingState.NOT_CHARGEABLE);
119
+ // Retrieve initial values and updateHomekit
120
+ this.refreshStatus();
138
121
  // Retrieve initial values and updateHomekit
139
122
  this.updateHomeKitCharacteristics();
140
123
  // Start an update interval
141
124
  interval(this.deviceRefreshRate * 1000)
142
- .pipe(skipWhile(() => this.contactUbpdateInProgress))
125
+ .pipe(skipWhile(() => this.contactUpdateInProgress))
143
126
  .subscribe(async () => {
144
127
  await this.refreshStatus();
145
128
  });
146
129
  //regisiter webhook event handler
147
- if (this.device.webhook) {
148
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} is listening webhook.`);
149
- this.platform.webhookEventHandler[this.device.deviceId] = async (context) => {
150
- try {
151
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
152
- const { detectionState, brightness, openState } = context;
153
- const { MotionDetected, CurrentAmbientLightLevel, ContactSensorState } = this;
154
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
155
- '(detectionState, brightness, openState) = ' +
156
- `Webhook:(${detectionState}, ${brightness}, ${openState}), ` +
157
- `current:(${MotionDetected}, ${CurrentAmbientLightLevel}, ${ContactSensorState})`);
158
- this.set_minLux = this.minLux();
159
- this.set_maxLux = this.maxLux();
160
- this.MotionDetected = detectionState === 'DETECTED' ? true : false;
161
- this.CurrentAmbientLightLevel = brightness === 'bright' ? this.set_maxLux : this.set_minLux;
162
- this.ContactSensorState = openState === 'open' ? 1 : 0;
163
- this.updateHomeKitCharacteristics();
164
- }
165
- catch (e) {
166
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
167
- + `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
168
- }
169
- };
170
- }
171
- }
172
- /**
173
- * Parse the device status from the SwitchBot api
174
- */
175
- async parseStatus() {
176
- if (!this.device.enableCloudService && this.OpenAPI) {
177
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} parseStatus enableCloudService: ${this.device.enableCloudService}`);
178
- }
179
- else if (this.BLE) {
180
- await this.BLEparseStatus();
181
- }
182
- else if (this.OpenAPI && this.platform.config.credentials?.token) {
183
- await this.openAPIparseStatus();
184
- }
185
- else {
186
- await this.offlineOff();
187
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:` + ` ${this.device.connectionType}, parseStatus will not happen.`);
188
- }
130
+ this.registerWebhook(accessory, device);
189
131
  }
190
- async BLEparseStatus() {
132
+ async BLEparseStatus(serviceData) {
191
133
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
192
134
  // Door State
193
- switch (this.BLE_ContactSensorState) {
135
+ switch (serviceData.doorState) {
194
136
  case 'open':
195
137
  case 1:
196
- this.ContactSensorState = this.hap.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED;
138
+ this.ContactSensor.ContactSensorState = this.hap.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED;
197
139
  break;
198
140
  case 'close':
199
141
  case 0:
200
- this.ContactSensorState = this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED;
142
+ this.ContactSensor.ContactSensorState = this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED;
201
143
  break;
202
144
  default:
203
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} timeout no closed, doorstate: ${this.BLE_ContactSensorState}`);
145
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} timeout no closed, doorstate: ${serviceData.doorState}`);
204
146
  }
205
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensorState}`);
206
- if (this.ContactSensorState !== this.accessory.context.ContactSensorState &&
147
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensor.ContactSensorState}`);
148
+ if (this.ContactSensor.ContactSensorState !== this.accessory.context.ContactSensorState &&
207
149
  this.hap.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED) {
208
150
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Opened`);
209
151
  }
210
152
  // Movement
211
153
  if (!this.device.contact?.hide_motionsensor) {
212
- this.MotionDetected = Boolean(this.BLE_MotionDetected);
213
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
214
- if (this.MotionDetected !== this.accessory.context.MotionDetected && this.MotionDetected) {
154
+ this.MotionSensor.MotionDetected = Boolean(serviceData.movement);
155
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MotionDetected: ${this.MotionSensor.MotionDetected}`);
156
+ if (this.MotionSensor.MotionDetected !== this.accessory.context.MotionDetected && this.MotionSensor.MotionDetected) {
215
157
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Detected Motion`);
216
158
  }
217
159
  }
218
160
  // Light Level
219
161
  if (!this.device.contact?.hide_lightsensor) {
220
- this.set_minLux = this.minLux();
221
- this.set_maxLux = this.maxLux();
222
- switch (this.BLE_CurrentAmbientLightLevel) {
162
+ const set_minLux = this.device.contact?.set_minLux ?? 1;
163
+ const set_maxLux = this.device.contact?.set_maxLux ?? 6001;
164
+ switch (serviceData.lightLevel) {
223
165
  case true:
224
- this.CurrentAmbientLightLevel = this.set_minLux;
166
+ this.LightSensor.CurrentAmbientLightLevel = set_minLux;
225
167
  break;
226
168
  default:
227
- this.CurrentAmbientLightLevel = this.set_maxLux;
169
+ this.LightSensor.CurrentAmbientLightLevel = set_maxLux;
228
170
  }
229
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${this.BLE_CurrentAmbientLightLevel},` +
230
- ` CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
231
- if (this.CurrentAmbientLightLevel !== this.accessory.context.CurrentAmbientLightLevel) {
232
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
171
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${serviceData.lightLevel},`
172
+ + ` CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
173
+ if (this.LightSensor.CurrentAmbientLightLevel !== this.accessory.context.CurrentAmbientLightLevel) {
174
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName}`
175
+ + ` CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
233
176
  }
234
177
  }
235
178
  // Battery
236
- if (this.BLE_BatteryLevel === undefined) {
237
- this.BLE_BatteryLevel === 100;
179
+ if (serviceData.battery === undefined) {
180
+ serviceData.battery === 100;
238
181
  }
239
- this.BatteryLevel = Number(this.BLE_BatteryLevel);
240
- if (this.BatteryLevel < 10) {
241
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
182
+ this.Battery.BatteryLevel = Number(serviceData.battery);
183
+ if (this.Battery.BatteryLevel < 10) {
184
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
242
185
  }
243
186
  else {
244
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
187
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
245
188
  }
246
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, ` + `StatusLowBattery: ${this.StatusLowBattery}`);
189
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}, `
190
+ + `StatusLowBattery: ${this.Battery.StatusLowBattery}`);
247
191
  }
248
- async openAPIparseStatus() {
192
+ async openAPIparseStatus(deviceStatus) {
249
193
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
250
194
  // Contact State
251
- if (this.OpenAPI_ContactSensorState === 'open') {
252
- this.ContactSensorState = this.hap.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED;
253
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensorState}`);
195
+ if (deviceStatus.body.openState === 'open') {
196
+ this.ContactSensor.ContactSensorState = this.hap.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED;
197
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensor.ContactSensorState}`);
254
198
  }
255
- else if (this.OpenAPI_ContactSensorState === 'close') {
256
- this.ContactSensorState = this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED;
257
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensorState}`);
199
+ else if (deviceStatus.body.openState === 'close') {
200
+ this.ContactSensor.ContactSensorState = this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED;
201
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensor.ContactSensorState}`);
258
202
  }
259
203
  else {
260
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openState: ${this.OpenAPI_ContactSensorState}`);
204
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openState: ${deviceStatus.body.openState}`);
261
205
  }
262
206
  // Motion State
263
207
  if (!this.device.contact?.hide_motionsensor) {
264
- this.MotionDetected = this.OpenAPI_MotionDetected;
265
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
208
+ this.MotionSensor.MotionDetected = deviceStatus.body.moveDetected;
209
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MotionDetected: ${this.MotionSensor.MotionDetected}`);
266
210
  }
267
211
  // Light Level
268
212
  if (!this.device.contact?.hide_lightsensor) {
269
- this.set_minLux = this.minLux();
270
- this.set_maxLux = this.maxLux();
271
- switch (this.OpenAPI_CurrentAmbientLightLevel) {
213
+ const set_minLux = this.device.contact?.set_minLux ?? 1;
214
+ const set_maxLux = this.device.contact?.set_maxLux ?? 6001;
215
+ switch (deviceStatus.body.brightness) {
272
216
  case 'dim':
273
- this.CurrentAmbientLightLevel = this.set_minLux;
217
+ this.LightSensor.CurrentAmbientLightLevel = set_minLux;
274
218
  break;
275
219
  case 'bright':
276
220
  default:
277
- this.CurrentAmbientLightLevel = this.set_maxLux;
221
+ this.LightSensor.CurrentAmbientLightLevel = set_maxLux;
278
222
  }
279
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
223
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
224
+ + ` CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
280
225
  }
281
226
  // Battery
282
- if (this.OpenAPI_BatteryLevel === undefined) {
283
- this.OpenAPI_BatteryLevel === 100;
227
+ if (deviceStatus.body.battery === undefined) {
228
+ deviceStatus.body.battery === 100;
284
229
  }
285
- this.BatteryLevel = Number(this.OpenAPI_BatteryLevel);
286
- if (this.BatteryLevel < 10) {
287
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
230
+ this.Battery.BatteryLevel = Number(deviceStatus.body.battery);
231
+ if (this.Battery.BatteryLevel < 10) {
232
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
288
233
  }
289
234
  else {
290
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
235
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
236
+ }
237
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}, `
238
+ + `StatusLowBattery: ${this.Battery.StatusLowBattery}`);
239
+ // Firmware Version
240
+ const version = deviceStatus.body.version?.toString();
241
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Firmware Version: ${version?.replace(/^V|-.*$/g, '')}`);
242
+ if (deviceStatus.body.version) {
243
+ const deviceVersion = version?.replace(/^V|-.*$/g, '') ?? '0.0.0';
244
+ this.accessory
245
+ .getService(this.hap.Service.AccessoryInformation)
246
+ .setCharacteristic(this.hap.Characteristic.HardwareRevision, deviceVersion)
247
+ .setCharacteristic(this.hap.Characteristic.FirmwareRevision, deviceVersion)
248
+ .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
249
+ .updateValue(deviceVersion);
250
+ this.accessory.context.deviceVersion = deviceVersion;
251
+ this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceVersion: ${this.accessory.context.deviceVersion}`);
291
252
  }
292
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, ` + `StatusLowBattery: ${this.StatusLowBattery}`);
293
- // FirmwareRevision
294
- this.FirmwareRevision = this.OpenAPI_FirmwareRevision;
295
- this.accessory.context.FirmwareRevision = this.FirmwareRevision;
296
253
  }
297
254
  /**
298
255
  * Asks the SwitchBot API for the latest device information
@@ -309,211 +266,159 @@ export class Contact {
309
266
  }
310
267
  else {
311
268
  await this.offlineOff();
312
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:` +
313
- ` ${this.device.connectionType}, refreshStatus will not happen.`);
269
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
270
+ + ` ${this.device.connectionType}, refreshStatus will not happen.`);
314
271
  }
315
272
  }
316
273
  async BLERefreshStatus() {
317
274
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
318
275
  const switchbot = await this.platform.connectBLE();
319
276
  // Convert to BLE Address
320
- this.device.bleMac =
321
- this.device.customBLEaddress ||
322
- this.device
323
- .deviceId.match(/.{1,2}/g)
324
- .join(':')
325
- .toLowerCase();
277
+ this.device.bleMac = this.device
278
+ .deviceId.match(/.{1,2}/g)
279
+ .join(':')
280
+ .toLowerCase();
326
281
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
327
282
  this.getCustomBLEAddress(switchbot);
328
283
  // Start to monitor advertisement packets
329
284
  (async () => {
330
- await switchbot.startScan({
331
- model: 'd',
332
- id: this.device.bleMac,
333
- });
285
+ // Start to monitor advertisement packets
286
+ await switchbot.startScan({ model: this.device.bleModel, id: this.device.bleMac });
334
287
  // Set an event handler
335
288
  switchbot.onadvertisement = (ad) => {
336
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
337
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.serviceData.model}`);
338
- if (this.device.bleMac === ad.address && ad.serviceData.model === 'd') {
289
+ if (this.device.bleMac === ad.address && ad.model === this.device.bleModel) {
290
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
291
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.model}`);
339
292
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
340
- this.BLE_MotionDetected = ad.serviceData.movement;
341
- this.BLE_BatteryLevel = ad.serviceData.battery;
342
- this.BLE_CurrentAmbientLightLevel = ad.serviceData.lightLevel;
343
293
  }
344
294
  else {
345
295
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
346
296
  }
347
297
  };
348
- // Wait 1 seconds
298
+ // Wait 10 seconds
349
299
  await switchbot.wait(this.scanDuration * 1000);
350
300
  // Stop to monitor
351
301
  await switchbot.stopScan();
352
302
  // Update HomeKit
353
- await this.BLEparseStatus();
303
+ await this.BLEparseStatus(switchbot.onadvertisement.serviceData);
354
304
  await this.updateHomeKitCharacteristics();
355
305
  })();
356
- /*if (switchbot !== false) {
357
- await switchbot
358
- .startScan({
359
- model: 'd',
360
- id: this.device.bleMac,
361
- })
362
- .then(async () => {
363
- // Set an event handler
364
- this.scanning = true;
365
- switchbot.onadvertisement = async (ad: ad) => {
366
- this.debugLog(
367
- `${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},` +
368
- ` BLE Address Found: ${ad.address}`,
369
- );
370
- this.BLE_MotionDetected = ad.serviceData.movement;
371
- this.BLE_BatteryLevel = ad.serviceData.battery;
372
- this.BLE_ContactSensorState = ad.serviceData.doorState;
373
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
374
- this.debugLog(
375
- `${this.device.deviceType}: ${this.accessory.displayName} movement: ${ad.serviceData.movement}, doorState: ` +
376
- `${ad.serviceData.doorState}, battery: ${ad.serviceData.battery}`,
377
- );
378
-
379
- if (ad.serviceData) {
380
- this.BLE_IsConnected = true;
381
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.BLE_IsConnected}`);
382
- await this.stopScanning(switchbot);
383
- this.scanning = false;
384
- } else {
385
- this.BLE_IsConnected = false;
386
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.BLE_IsConnected}`);
387
- }
388
- };
389
- // Wait
390
- return await sleep(this.scanDuration * 1000);
391
- })
392
- .then(async () => {
393
- // Stop to monitor
394
- if (this.scanning) {
395
- await this.stopScanning(switchbot);
396
- }
397
- })
398
- .catch(async (e: any) => {
399
- this.apiError(e);
400
- this.errorLog(
401
- `${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}` +
402
- ` Connection, Error Message: ${JSON.stringify(e.message)}`,
403
- );
404
- await this.BLERefreshConnection(switchbot);
405
- });
406
- } else {
407
- await this.BLERefreshConnection(switchbot);
408
- }*/
306
+ if (switchbot === undefined) {
307
+ await this.BLERefreshConnection(switchbot);
308
+ }
409
309
  }
410
310
  async openAPIRefreshStatus() {
411
311
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
412
312
  try {
413
- const { body, statusCode } = await request(`${Devices}/${this.device.deviceId}/status`, {
414
- headers: this.platform.generateHeaders(),
415
- });
313
+ const { body, statusCode } = await this.platform.retryRequest(this.deviceMaxRetries, this.deviceDelayBetweenRetries, `${Devices}/${this.device.deviceId}/status`, { headers: this.platform.generateHeaders() });
416
314
  this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
417
315
  const deviceStatus = await body.json();
418
316
  this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
419
317
  this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
420
318
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
421
- this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
319
+ this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} `
422
320
  + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
423
- this.OpenAPI_ContactSensorState = deviceStatus.body.openState;
424
- this.OpenAPI_MotionDetected = deviceStatus.body.moveDetected;
425
- this.OpenAPI_CurrentAmbientLightLevel = deviceStatus.body.brightness;
426
- this.OpenAPI_BatteryLevel = deviceStatus.body.battery;
427
- this.OpenAPI_FirmwareRevision = deviceStatus.body.version;
428
- this.openAPIparseStatus();
321
+ this.openAPIparseStatus(deviceStatus);
429
322
  this.updateHomeKitCharacteristics();
430
323
  }
431
324
  else {
432
325
  this.statusCode(statusCode);
433
326
  this.statusCode(deviceStatus.statusCode);
327
+ if (deviceStatus.statusCode === 161 || deviceStatus.statusCode === 171) {
328
+ this.offlineOff();
329
+ }
434
330
  }
435
331
  }
436
332
  catch (e) {
437
333
  this.apiError(e);
438
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}` +
439
- ` Connection, Error Message: ${JSON.stringify(e.message)}`);
334
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
335
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
336
+ }
337
+ }
338
+ async registerWebhook(accessory, device) {
339
+ if (device.webhook) {
340
+ this.debugLog(`${device.deviceType}: ${accessory.displayName} is listening webhook.`);
341
+ this.platform.webhookEventHandler[device.deviceId] = async (context) => {
342
+ try {
343
+ this.debugLog(`${device.deviceType}: ${accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
344
+ const { detectionState, brightness, openState } = context;
345
+ const { ContactSensorState } = this.ContactSensor;
346
+ const { CurrentAmbientLightLevel } = this.LightSensor ?? {};
347
+ const { MotionDetected } = this.MotionSensor ?? {};
348
+ this.debugLog(`${device.deviceType}: ${accessory.displayName} (detectionState, brightness, openState) = Webhook:(${detectionState}, `
349
+ + `${brightness}, ${openState}), current:(${MotionDetected}, ${CurrentAmbientLightLevel}, ${ContactSensorState})`);
350
+ const set_minLux = this.device.contact?.set_minLux ?? 1;
351
+ const set_maxLux = this.device.contact?.set_maxLux ?? 6001;
352
+ this.ContactSensor.ContactSensorState = openState === 'open' ? 1 : 0;
353
+ if (!device.contact?.hide_motionsensor) {
354
+ this.MotionSensor.MotionDetected = detectionState === 'DETECTED' ? true : false;
355
+ }
356
+ if (!device.contact?.hide_lightsensor) {
357
+ this.LightSensor.CurrentAmbientLightLevel = brightness === 'bright' ? set_maxLux : set_minLux;
358
+ }
359
+ this.updateHomeKitCharacteristics();
360
+ }
361
+ catch (e) {
362
+ this.errorLog(`${device.deviceType}: ${accessory.displayName} failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
363
+ }
364
+ };
365
+ }
366
+ else {
367
+ this.debugLog(`${device.deviceType}: ${accessory.displayName} is not listening webhook.`);
440
368
  }
441
369
  }
442
370
  /**
443
371
  * Updates the status for each of the HomeKit Characteristics
444
372
  */
445
373
  async updateHomeKitCharacteristics() {
446
- if (this.ContactSensorState === undefined) {
447
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensorState}`);
374
+ if (this.ContactSensor.ContactSensorState === undefined) {
375
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensor.ContactSensorState}`);
448
376
  }
449
377
  else {
450
- this.accessory.context.ContactSensorState = this.ContactSensorState;
451
- this.contactSensorservice.updateCharacteristic(this.hap.Characteristic.ContactSensorState, this.ContactSensorState);
452
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic ContactSensorState: ${this.ContactSensorState}`);
378
+ this.accessory.context.ContactSensorState = this.ContactSensor.ContactSensorState;
379
+ this.ContactSensor.Service.updateCharacteristic(this.hap.Characteristic.ContactSensorState, this.ContactSensor.ContactSensorState);
380
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
381
+ + ` ContactSensorState: ${this.ContactSensor.ContactSensorState}`);
453
382
  }
454
383
  if (!this.device.contact?.hide_motionsensor) {
455
- if (this.MotionDetected === undefined) {
456
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
384
+ if (this.MotionSensor.MotionDetected === undefined) {
385
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MotionDetected: ${this.MotionSensor.MotionDetected}`);
457
386
  }
458
387
  else {
459
- this.accessory.context.MotionDetected = this.MotionDetected;
460
- this.motionService?.updateCharacteristic(this.hap.Characteristic.MotionDetected, this.MotionDetected);
461
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic MotionDetected: ${this.MotionDetected}`);
388
+ this.accessory.context.MotionDetected = this.MotionSensor.MotionDetected;
389
+ this.MotionSensor.Service.updateCharacteristic(this.hap.Characteristic.MotionDetected, this.MotionSensor.MotionDetected);
390
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
391
+ + ` MotionDetected: ${this.MotionSensor.MotionDetected}`);
462
392
  }
463
393
  }
464
394
  if (!this.device.contact?.hide_lightsensor) {
465
- if (this.CurrentAmbientLightLevel === undefined) {
466
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
395
+ if (this.LightSensor?.CurrentAmbientLightLevel === undefined) {
396
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
397
+ + ` CurrentAmbientLightLevel: ${this.LightSensor?.CurrentAmbientLightLevel}`);
467
398
  }
468
399
  else {
469
- this.accessory.context.CurrentAmbientLightLevel = this.CurrentAmbientLightLevel;
470
- this.lightSensorService?.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, this.CurrentAmbientLightLevel);
471
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}` +
472
- ` updateCharacteristic CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
400
+ this.accessory.context.CurrentAmbientLightLevel = this.LightSensor.CurrentAmbientLightLevel;
401
+ this.LightSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, this.LightSensor.CurrentAmbientLightLevel);
402
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
403
+ + ` updateCharacteristic CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
473
404
  }
474
405
  }
475
- if (this.BatteryLevel === undefined) {
476
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
477
- }
478
- else {
479
- this.accessory.context.BatteryLevel = this.BatteryLevel;
480
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.BatteryLevel);
481
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
482
- }
483
- if (this.StatusLowBattery === undefined) {
484
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
406
+ if (this.Battery.BatteryLevel === undefined) {
407
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}`);
485
408
  }
486
409
  else {
487
- this.accessory.context.StatusLowBattery = this.StatusLowBattery;
488
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.StatusLowBattery);
489
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
410
+ this.accessory.context.BatteryLevel = this.Battery.BatteryLevel;
411
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.Battery.BatteryLevel);
412
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.Battery.BatteryLevel}`);
490
413
  }
491
- }
492
- async stopScanning(switchbot) {
493
- switchbot.stopScan();
494
- if (this.BLE_IsConnected) {
495
- await this.BLEparseStatus();
496
- await this.updateHomeKitCharacteristics();
414
+ if (this.Battery.StatusLowBattery === undefined) {
415
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.Battery.StatusLowBattery}`);
497
416
  }
498
417
  else {
499
- await this.BLERefreshConnection(switchbot);
500
- }
501
- }
502
- async getCustomBLEAddress(switchbot) {
503
- if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
504
- (async () => {
505
- // Start to monitor advertisement packets
506
- await switchbot.startScan({
507
- model: 'd',
508
- });
509
- // Set an event handler
510
- switchbot.onadvertisement = (ad) => {
511
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
512
- };
513
- await sleep(10000);
514
- // Stop to monitor
515
- switchbot.stopScan();
516
- })();
418
+ this.accessory.context.StatusLowBattery = this.Battery.StatusLowBattery;
419
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.Battery.StatusLowBattery);
420
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
421
+ + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
517
422
  }
518
423
  }
519
424
  async BLERefreshConnection(switchbot) {
@@ -524,243 +429,30 @@ export class Contact {
524
429
  await this.openAPIRefreshStatus();
525
430
  }
526
431
  }
527
- minLux() {
528
- if (this.device.contact?.set_minLux) {
529
- this.set_minLux = this.device.contact.set_minLux;
530
- }
531
- else {
532
- this.set_minLux = 1;
533
- }
534
- return this.set_minLux;
535
- }
536
- maxLux() {
537
- if (this.device.contact?.set_maxLux) {
538
- this.set_maxLux = this.device.contact.set_maxLux;
539
- }
540
- else {
541
- this.set_maxLux = 6001;
542
- }
543
- return this.set_maxLux;
544
- }
545
- async scan(device) {
546
- if (device.scanDuration) {
547
- this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
548
- if (this.BLE) {
549
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
550
- }
551
- }
552
- else {
553
- this.scanDuration = this.accessory.context.scanDuration = 1;
554
- if (this.BLE) {
555
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
556
- }
557
- }
558
- }
559
- async statusCode(statusCode) {
560
- switch (statusCode) {
561
- case 151:
562
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this deviceType, statusCode: ${statusCode}`);
563
- break;
564
- case 152:
565
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found, statusCode: ${statusCode}`);
566
- break;
567
- case 160:
568
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported, statusCode: ${statusCode}`);
569
- break;
570
- case 161:
571
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline, statusCode: ${statusCode}`);
572
- this.offlineOff();
573
- break;
574
- case 171:
575
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. ` +
576
- `Hub: ${this.device.hubDeviceId}`);
577
- this.offlineOff();
578
- break;
579
- case 190:
580
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
581
- ` Or command format is invalid, statusCode: ${statusCode}`);
582
- break;
583
- case 100:
584
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent, statusCode: ${statusCode}`);
585
- break;
586
- case 200:
587
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Request successful, statusCode: ${statusCode}`);
588
- break;
589
- case 400:
590
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Bad Request, The client has issued an invalid request. `
591
- + `This is commonly used to specify validation errors in a request payload, statusCode: ${statusCode}`);
592
- break;
593
- case 401:
594
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unauthorized, Authorization for the API is required, `
595
- + `but the request has not been authenticated, statusCode: ${statusCode}`);
596
- break;
597
- case 403:
598
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Forbidden, The request has been authenticated but does not `
599
- + `have appropriate permissions, or a requested resource is not found, statusCode: ${statusCode}`);
600
- break;
601
- case 404:
602
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Found, Specifies the requested path does not exist, `
603
- + `statusCode: ${statusCode}`);
604
- break;
605
- case 406:
606
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Acceptable, The client has requested a MIME type via `
607
- + `the Accept header for a value not supported by the server, statusCode: ${statusCode}`);
608
- break;
609
- case 415:
610
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unsupported Media Type, The client has defined a contentType `
611
- + `header that is not supported by the server, statusCode: ${statusCode}`);
612
- break;
613
- case 422:
614
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unprocessable Entity, The client has made a valid request, `
615
- + `but the server cannot process it. This is often used for APIs for which certain limits have been exceeded, statusCode: ${statusCode}`);
616
- break;
617
- case 429:
618
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Too Many Requests, The client has exceeded the number of `
619
- + `requests allowed for a given time window, statusCode: ${statusCode}`);
620
- break;
621
- case 500:
622
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Internal Server Error, An unexpected error on the SmartThings `
623
- + `servers has occurred. These errors should be rare, statusCode: ${statusCode}`);
624
- break;
625
- default:
626
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: ` +
627
- `${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`);
628
- }
629
- }
630
432
  async offlineOff() {
631
433
  if (this.device.offline) {
632
- await this.deviceContext();
633
- await this.updateHomeKitCharacteristics();
434
+ this.ContactSensor.Service.updateCharacteristic(this.hap.Characteristic.ContactSensorState, this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED);
435
+ if (!this.device.contact?.hide_motionsensor) {
436
+ this.MotionSensor.Service.updateCharacteristic(this.hap.Characteristic.MotionDetected, false);
437
+ }
438
+ if (!this.device.contact?.hide_lightsensor) {
439
+ this.LightSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, 100);
440
+ }
634
441
  }
635
442
  }
636
443
  async apiError(e) {
637
- this.contactSensorservice.updateCharacteristic(this.hap.Characteristic.ContactSensorState, e);
444
+ this.ContactSensor.Service.updateCharacteristic(this.hap.Characteristic.ContactSensorState, e);
445
+ this.ContactSensor.Service.updateCharacteristic(this.hap.Characteristic.StatusActive, e);
638
446
  if (!this.device.contact?.hide_motionsensor) {
639
- this.motionService?.updateCharacteristic(this.hap.Characteristic.MotionDetected, e);
447
+ this.MotionSensor.Service.updateCharacteristic(this.hap.Characteristic.MotionDetected, e);
448
+ this.MotionSensor.Service.updateCharacteristic(this.hap.Characteristic.StatusActive, e);
640
449
  }
641
450
  if (!this.device.contact?.hide_lightsensor) {
642
- this.lightSensorService?.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, e);
643
- }
644
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e);
645
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e);
646
- }
647
- async deviceContext() {
648
- if (this.MotionDetected === undefined) {
649
- this.MotionDetected = false;
650
- }
651
- else {
652
- this.MotionDetected = this.accessory.context.MotionDetected;
653
- }
654
- if (this.ContactSensorState === undefined) {
655
- this.ContactSensorState = this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED;
656
- }
657
- else {
658
- this.ContactSensorState = this.accessory.context.ContactSensorState;
659
- }
660
- if (this.FirmwareRevision === undefined) {
661
- this.FirmwareRevision = this.platform.version;
662
- this.accessory.context.FirmwareRevision = this.FirmwareRevision;
663
- }
664
- }
665
- async refreshRate(device) {
666
- if (device.refreshRate) {
667
- this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
668
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
451
+ this.LightSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, e);
452
+ this.LightSensor.Service.updateCharacteristic(this.hap.Characteristic.StatusActive, e);
669
453
  }
670
- else if (this.platform.config.options.refreshRate) {
671
- this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options.refreshRate;
672
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
673
- }
674
- }
675
- async deviceConfig(device) {
676
- let config = {};
677
- if (device.contact) {
678
- config = device.contact;
679
- }
680
- if (device.connectionType !== undefined) {
681
- config['connectionType'] = device.connectionType;
682
- }
683
- if (device.external !== undefined) {
684
- config['external'] = device.external;
685
- }
686
- if (device.logging !== undefined) {
687
- config['logging'] = device.logging;
688
- }
689
- if (device.refreshRate !== undefined) {
690
- config['refreshRate'] = device.refreshRate;
691
- }
692
- if (device.scanDuration !== undefined) {
693
- config['scanDuration'] = device.scanDuration;
694
- }
695
- if (device.webhook !== undefined) {
696
- config['webhook'] = device.webhook;
697
- }
698
- if (Object.entries(config).length !== 0) {
699
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
700
- }
701
- }
702
- async deviceLogs(device) {
703
- if (this.platform.debugMode) {
704
- this.deviceLogging = this.accessory.context.logging = 'debugMode';
705
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
706
- }
707
- else if (device.logging) {
708
- this.deviceLogging = this.accessory.context.logging = device.logging;
709
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
710
- }
711
- else if (this.platform.config.options?.logging) {
712
- this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
713
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
714
- }
715
- else {
716
- this.deviceLogging = this.accessory.context.logging = 'standard';
717
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
718
- }
719
- }
720
- /**
721
- * Logging for Device
722
- */
723
- infoLog(...log) {
724
- if (this.enablingDeviceLogging()) {
725
- this.platform.log.info(String(...log));
726
- }
727
- }
728
- warnLog(...log) {
729
- if (this.enablingDeviceLogging()) {
730
- this.platform.log.warn(String(...log));
731
- }
732
- }
733
- debugWarnLog(...log) {
734
- if (this.enablingDeviceLogging()) {
735
- if (this.deviceLogging?.includes('debug')) {
736
- this.platform.log.warn('[DEBUG]', String(...log));
737
- }
738
- }
739
- }
740
- errorLog(...log) {
741
- if (this.enablingDeviceLogging()) {
742
- this.platform.log.error(String(...log));
743
- }
744
- }
745
- debugErrorLog(...log) {
746
- if (this.enablingDeviceLogging()) {
747
- if (this.deviceLogging?.includes('debug')) {
748
- this.platform.log.error('[DEBUG]', String(...log));
749
- }
750
- }
751
- }
752
- debugLog(...log) {
753
- if (this.enablingDeviceLogging()) {
754
- if (this.deviceLogging === 'debug') {
755
- this.platform.log.info('[DEBUG]', String(...log));
756
- }
757
- else {
758
- this.platform.log.debug(String(...log));
759
- }
760
- }
761
- }
762
- enablingDeviceLogging() {
763
- return this.deviceLogging.includes('debug') || this.deviceLogging === 'standard';
454
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e);
455
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e);
764
456
  }
765
457
  }
766
458
  //# sourceMappingURL=contact.js.map