@switchbot/homebridge-switchbot 3.5.0-beta.23 → 3.5.0-beta.24

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