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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (188) hide show
  1. package/README.md +16 -0
  2. package/dist/device/blindtilt.d.ts +15 -6
  3. package/dist/device/blindtilt.d.ts.map +1 -1
  4. package/dist/device/blindtilt.js +203 -255
  5. package/dist/device/blindtilt.js.map +1 -1
  6. package/dist/device/bot.d.ts +9 -4
  7. package/dist/device/bot.d.ts.map +1 -1
  8. package/dist/device/bot.js +196 -218
  9. package/dist/device/bot.js.map +1 -1
  10. package/dist/device/ceilinglight.d.ts +9 -4
  11. package/dist/device/ceilinglight.d.ts.map +1 -1
  12. package/dist/device/ceilinglight.js +202 -247
  13. package/dist/device/ceilinglight.js.map +1 -1
  14. package/dist/device/colorbulb.d.ts +10 -5
  15. package/dist/device/colorbulb.d.ts.map +1 -1
  16. package/dist/device/colorbulb.js +329 -378
  17. package/dist/device/colorbulb.js.map +1 -1
  18. package/dist/device/contact.d.ts +9 -4
  19. package/dist/device/contact.d.ts.map +1 -1
  20. package/dist/device/contact.js +99 -142
  21. package/dist/device/contact.js.map +1 -1
  22. package/dist/device/curtain.d.ts +19 -7
  23. package/dist/device/curtain.d.ts.map +1 -1
  24. package/dist/device/curtain.js +203 -264
  25. package/dist/device/curtain.js.map +1 -1
  26. package/dist/device/device.d.ts +28 -32
  27. package/dist/device/device.d.ts.map +1 -1
  28. package/dist/device/device.js +186 -225
  29. package/dist/device/device.js.map +1 -1
  30. package/dist/device/fan.d.ts +9 -4
  31. package/dist/device/fan.d.ts.map +1 -1
  32. package/dist/device/fan.js +198 -227
  33. package/dist/device/fan.js.map +1 -1
  34. package/dist/device/hub.d.ts +9 -5
  35. package/dist/device/hub.d.ts.map +1 -1
  36. package/dist/device/hub.js +106 -231
  37. package/dist/device/hub.js.map +1 -1
  38. package/dist/device/humidifier.d.ts +12 -4
  39. package/dist/device/humidifier.d.ts.map +1 -1
  40. package/dist/device/humidifier.js +192 -232
  41. package/dist/device/humidifier.js.map +1 -1
  42. package/dist/device/iosensor.d.ts +9 -4
  43. package/dist/device/iosensor.d.ts.map +1 -1
  44. package/dist/device/iosensor.js +91 -157
  45. package/dist/device/iosensor.js.map +1 -1
  46. package/dist/device/lightstrip.d.ts +10 -5
  47. package/dist/device/lightstrip.d.ts.map +1 -1
  48. package/dist/device/lightstrip.js +287 -367
  49. package/dist/device/lightstrip.js.map +1 -1
  50. package/dist/device/lock.d.ts +10 -5
  51. package/dist/device/lock.d.ts.map +1 -1
  52. package/dist/device/lock.js +171 -216
  53. package/dist/device/lock.js.map +1 -1
  54. package/dist/device/meter.d.ts +9 -4
  55. package/dist/device/meter.d.ts.map +1 -1
  56. package/dist/device/meter.js +93 -158
  57. package/dist/device/meter.js.map +1 -1
  58. package/dist/device/meterplus.d.ts +9 -4
  59. package/dist/device/meterplus.d.ts.map +1 -1
  60. package/dist/device/meterplus.js +94 -155
  61. package/dist/device/meterplus.js.map +1 -1
  62. package/dist/device/motion.d.ts +9 -4
  63. package/dist/device/motion.d.ts.map +1 -1
  64. package/dist/device/motion.js +84 -142
  65. package/dist/device/motion.js.map +1 -1
  66. package/dist/device/plug.d.ts +9 -4
  67. package/dist/device/plug.d.ts.map +1 -1
  68. package/dist/device/plug.js +99 -124
  69. package/dist/device/plug.js.map +1 -1
  70. package/dist/device/robotvacuumcleaner.d.ts +9 -6
  71. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  72. package/dist/device/robotvacuumcleaner.js +170 -253
  73. package/dist/device/robotvacuumcleaner.js.map +1 -1
  74. package/dist/device/waterdetector.d.ts +12 -4
  75. package/dist/device/waterdetector.d.ts.map +1 -1
  76. package/dist/device/waterdetector.js +105 -129
  77. package/dist/device/waterdetector.js.map +1 -1
  78. package/dist/irdevice/airconditioner.d.ts +2 -1
  79. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  80. package/dist/irdevice/airconditioner.js.map +1 -1
  81. package/dist/irdevice/airpurifier.d.ts +2 -1
  82. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  83. package/dist/irdevice/airpurifier.js.map +1 -1
  84. package/dist/irdevice/camera.d.ts +2 -1
  85. package/dist/irdevice/camera.d.ts.map +1 -1
  86. package/dist/irdevice/camera.js.map +1 -1
  87. package/dist/irdevice/fan.d.ts +2 -1
  88. package/dist/irdevice/fan.d.ts.map +1 -1
  89. package/dist/irdevice/fan.js.map +1 -1
  90. package/dist/irdevice/irdevice.d.ts +2 -1
  91. package/dist/irdevice/irdevice.d.ts.map +1 -1
  92. package/dist/irdevice/irdevice.js +1 -1
  93. package/dist/irdevice/irdevice.js.map +1 -1
  94. package/dist/irdevice/light.d.ts +2 -1
  95. package/dist/irdevice/light.d.ts.map +1 -1
  96. package/dist/irdevice/light.js.map +1 -1
  97. package/dist/irdevice/other.d.ts +2 -1
  98. package/dist/irdevice/other.d.ts.map +1 -1
  99. package/dist/irdevice/other.js.map +1 -1
  100. package/dist/irdevice/tv.d.ts +2 -1
  101. package/dist/irdevice/tv.d.ts.map +1 -1
  102. package/dist/irdevice/tv.js.map +1 -1
  103. package/dist/irdevice/vacuumcleaner.d.ts +2 -1
  104. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  105. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  106. package/dist/irdevice/waterheater.d.ts +2 -1
  107. package/dist/irdevice/waterheater.d.ts.map +1 -1
  108. package/dist/irdevice/waterheater.js.map +1 -1
  109. package/dist/platform.d.ts +14 -14
  110. package/dist/platform.d.ts.map +1 -1
  111. package/dist/platform.js +102 -86
  112. package/dist/platform.js.map +1 -1
  113. package/dist/settings.d.ts +49 -191
  114. package/dist/settings.d.ts.map +1 -1
  115. package/dist/settings.js +19 -0
  116. package/dist/settings.js.map +1 -1
  117. package/dist/types/bledevicestatus.d.ts +279 -0
  118. package/dist/types/bledevicestatus.d.ts.map +1 -0
  119. package/dist/types/bledevicestatus.js +2 -0
  120. package/dist/types/bledevicestatus.js.map +1 -0
  121. package/dist/types/devicelist.d.ts +89 -0
  122. package/dist/types/devicelist.d.ts.map +1 -0
  123. package/dist/types/devicelist.js +6 -0
  124. package/dist/types/devicelist.js.map +1 -0
  125. package/dist/types/deviceresponse.d.ts +13 -0
  126. package/dist/types/deviceresponse.d.ts.map +1 -0
  127. package/dist/types/deviceresponse.js +2 -0
  128. package/dist/types/deviceresponse.js.map +1 -0
  129. package/dist/types/devicestatus.d.ts +149 -0
  130. package/dist/types/devicestatus.d.ts.map +1 -0
  131. package/dist/types/devicestatus.js +3 -0
  132. package/dist/types/devicestatus.js.map +1 -0
  133. package/dist/types/devicewebhookstatus.d.ts +156 -0
  134. package/dist/types/devicewebhookstatus.d.ts.map +1 -0
  135. package/dist/types/devicewebhookstatus.js +2 -0
  136. package/dist/types/devicewebhookstatus.js.map +1 -0
  137. package/dist/types/irdevicelist.d.ts +10 -0
  138. package/dist/types/irdevicelist.d.ts.map +1 -0
  139. package/dist/types/irdevicelist.js +2 -0
  140. package/dist/types/irdevicelist.js.map +1 -0
  141. package/dist/types/pushbody.d.ts +6 -0
  142. package/dist/types/pushbody.d.ts.map +1 -0
  143. package/dist/types/pushbody.js +2 -0
  144. package/dist/types/pushbody.js.map +1 -0
  145. package/dist/utils.d.ts +25 -0
  146. package/dist/utils.d.ts.map +1 -1
  147. package/dist/utils.js +28 -0
  148. package/dist/utils.js.map +1 -1
  149. package/package.json +10 -10
  150. package/src/device/blindtilt.ts +224 -264
  151. package/src/device/bot.ts +208 -221
  152. package/src/device/ceilinglight.ts +210 -257
  153. package/src/device/colorbulb.ts +334 -397
  154. package/src/device/contact.ts +111 -146
  155. package/src/device/curtain.ts +220 -283
  156. package/src/device/device.ts +194 -245
  157. package/src/device/fan.ts +214 -230
  158. package/src/device/hub.ts +126 -246
  159. package/src/device/humidifier.ts +216 -243
  160. package/src/device/iosensor.ts +114 -166
  161. package/src/device/lightstrip.ts +302 -377
  162. package/src/device/lock.ts +187 -221
  163. package/src/device/meter.ts +116 -177
  164. package/src/device/meterplus.ts +117 -174
  165. package/src/device/motion.ts +99 -144
  166. package/src/device/plug.ts +110 -128
  167. package/src/device/robotvacuumcleaner.ts +189 -252
  168. package/src/device/waterdetector.ts +123 -139
  169. package/src/irdevice/airconditioner.ts +2 -1
  170. package/src/irdevice/airpurifier.ts +2 -1
  171. package/src/irdevice/camera.ts +2 -1
  172. package/src/irdevice/fan.ts +2 -1
  173. package/src/irdevice/irdevice.ts +3 -2
  174. package/src/irdevice/light.ts +2 -1
  175. package/src/irdevice/other.ts +2 -1
  176. package/src/irdevice/tv.ts +2 -1
  177. package/src/irdevice/vacuumcleaner.ts +2 -1
  178. package/src/irdevice/waterheater.ts +2 -1
  179. package/src/platform.ts +104 -87
  180. package/src/settings.ts +30 -239
  181. package/src/types/bledevicestatus.ts +305 -0
  182. package/src/types/devicelist.ts +124 -0
  183. package/src/types/deviceresponse.ts +15 -0
  184. package/src/types/devicestatus.ts +178 -0
  185. package/src/types/devicewebhookstatus.ts +196 -0
  186. package/src/types/irdevicelist.ts +15 -0
  187. package/src/types/pushbody.ts +9 -0
  188. package/src/utils.ts +33 -0
@@ -3,12 +3,16 @@
3
3
  * waterdetector.ts: @switchbot/homebridge-switchbot.
4
4
  */
5
5
  import { deviceBase } from './device.js';
6
- import { interval, Subject } from 'rxjs';
7
- import { skipWhile } from 'rxjs/operators';
6
+ import { Subject, interval, skipWhile } from 'rxjs';
7
+ import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot';
8
8
 
9
+ import type { devicesConfig } from '../settings.js';
10
+ import type { device } from '../types/devicelist.js';
9
11
  import type { SwitchBotPlatform } from '../platform.js';
12
+ import type { waterLeakDetectorServiceData } from '../types/bledevicestatus.js';
10
13
  import type { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
11
- import type { device, devicesConfig, serviceData, deviceStatus } from '../settings.js';
14
+ import type { waterLeakDetectorStatus } from '../types/devicestatus.js';
15
+ import type { waterLeakDetectorWebhookContext } from '../types/devicewebhookstatus.js';
12
16
 
13
17
  /**
14
18
  * Platform Accessory
@@ -42,7 +46,6 @@ export class WaterDetector extends deviceBase {
42
46
  device: device & devicesConfig,
43
47
  ) {
44
48
  super(platform, accessory, device);
45
-
46
49
  // this is subject we use to track when we need to POST changes to the SwitchBot API
47
50
  this.doWaterDetectorUpdate = new Subject();
48
51
  this.WaterDetectorUpdateInProgress = false;
@@ -103,68 +106,65 @@ export class WaterDetector extends deviceBase {
103
106
  }
104
107
 
105
108
  // Retrieve initial values and updateHomekit
109
+ this.debugLog('retrieve initial values and Update Homekit');
106
110
  this.refreshStatus();
107
111
 
108
112
  // Retrieve initial values and updateHomekit
109
- this.updateHomeKitCharacteristics();
113
+ // this.updateHomeKitCharacteristics();
114
+
115
+ //regisiter webhook event handler
116
+ this.registerWebhook();
110
117
 
111
118
  // Start an update interval
112
119
  interval(this.deviceRefreshRate * 1000)
113
120
  .pipe(skipWhile(() => this.WaterDetectorUpdateInProgress))
114
121
  .subscribe(async () => {
122
+ await this.debugLog(`update interval: ${this.deviceRefreshRate * 1000} seconds`);
115
123
  await this.refreshStatus();
116
124
  });
117
-
118
- //regisiter webhook event handler
119
- this.registerWebhook(accessory, device);
120
125
  }
121
126
 
122
- async BLEparseStatus(serviceData: serviceData): Promise<void> {
123
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
124
- // Battery
125
- this.Battery.BatteryLevel = Number(serviceData.battery);
126
- if (this.Battery.BatteryLevel < 15) {
127
- this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
128
- } else {
129
- this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
130
- }
131
- this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}, StatusLowBattery: ${this.Battery.StatusLowBattery}`);
132
-
133
- // LeakDetected
134
- if (this.device.waterdetector?.hide_leak) {
135
- this.LeakSensor!.LeakDetected = serviceData.status!;
136
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LeakDetected: ${this.LeakSensor!.LeakDetected}`);
127
+ async BLEparseStatus(serviceData: waterLeakDetectorServiceData): Promise<void> {
128
+ await this.debugLog('BLEparseStatus');
129
+ if (this.device.waterdetector?.hide_leak && this.LeakSensor?.Service) {
130
+ // StatusActive
131
+ this.LeakSensor.StatusActive = serviceData.state;
132
+ await this.debugLog(`StatusActive: ${this.LeakSensor.StatusActive}`);
133
+ // LeakDetected
134
+ this.LeakSensor.LeakDetected = serviceData.status;
135
+ this.debugLog(`LeakDetected: ${this.LeakSensor.LeakDetected}`);
137
136
  }
137
+ // BatteryLevel
138
+ this.Battery.BatteryLevel = Number(serviceData.battery);
139
+ await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
140
+ // StatusLowBattery
141
+ this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
142
+ ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
143
+ await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
138
144
  }
139
145
 
140
- async openAPIparseStatus(deviceStatus: deviceStatus): Promise<void> {
141
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
142
- // StatusLowBattery
143
- this.Battery.BatteryLevel = Number(deviceStatus.body.battery);
144
- if (this.Battery.BatteryLevel < 10) {
145
- this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
146
- } else {
147
- this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
146
+ async openAPIparseStatus(deviceStatus: waterLeakDetectorStatus): Promise<void> {
147
+ await this.debugLog('openAPIparseStatus');
148
+ if (!this.device.waterdetector?.hide_leak && this.LeakSensor?.Service) {
149
+ // StatusActive
150
+ this.LeakSensor.StatusActive = deviceStatus.battery === 0 ? false : true;
151
+ await this.debugLog(`StatusActive: ${this.LeakSensor.StatusActive}`);
152
+ // LeakDetected
153
+ this.LeakSensor.LeakDetected = deviceStatus.status;
154
+ this.debugLog(`LeakDetected: ${this.LeakSensor.LeakDetected}`);
148
155
  }
149
- this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}, StatusLowBattery: ${this.Battery.StatusLowBattery}`);
150
-
151
156
  // BatteryLevel
152
- if (Number.isNaN(this.Battery.BatteryLevel)) {
153
- this.Battery.BatteryLevel = 100;
154
- }
155
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}`);
156
-
157
- // LeakDetected
158
- if (!this.device.waterdetector?.hide_leak) {
159
- this.LeakSensor!.LeakDetected = deviceStatus.body.status!;
160
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LeakDetected: ${this.LeakSensor!.LeakDetected}`);
161
- }
162
-
163
- // Firmware Version
164
- const version = deviceStatus.body.version?.toString();
165
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Firmware Version: ${version?.replace(/^V|-.*$/g, '')}`);
166
- if (deviceStatus.body.version) {
167
- const deviceVersion = version?.replace(/^V|-.*$/g, '') ?? '0.0.0';
157
+ this.Battery.BatteryLevel = Number(deviceStatus.battery);
158
+ await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
159
+ // StatusLowBattery
160
+ this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
161
+ ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
162
+ await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
163
+ // FirmwareVersion
164
+ const version = deviceStatus.version.toString();
165
+ await this.debugLog(`FirmwareVersion: ${version.replace(/^V|-.*$/g, '')}`);
166
+ if (deviceStatus.version) {
167
+ const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
168
168
  this.accessory
169
169
  .getService(this.hap.Service.AccessoryInformation)!
170
170
  .setCharacteristic(this.hap.Characteristic.HardwareRevision, deviceVersion)
@@ -172,41 +172,64 @@ export class WaterDetector extends deviceBase {
172
172
  .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
173
173
  .updateValue(deviceVersion);
174
174
  this.accessory.context.deviceVersion = deviceVersion;
175
- this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceVersion: ${this.accessory.context.deviceVersion}`);
175
+ await this.debugSuccessLog(`deviceVersion: ${this.accessory.context.deviceVersion}`);
176
176
  }
177
177
  }
178
178
 
179
+ async parseStatusWebhook(context: waterLeakDetectorWebhookContext): Promise<void> {
180
+ await this.debugLog('parseStatusWebhook');
181
+ await this.debugLog(`(detectionState, battery) = Webhook: (${context.detectionState}, ${context.battery}),`
182
+ + ` current:(${this.LeakSensor?.LeakDetected}, ${this.Battery.BatteryLevel})`);
183
+ if (!this.device.waterdetector?.hide_leak && this.LeakSensor?.Service) {
184
+ // StatusActive
185
+ this.LeakSensor.StatusActive = context.detectionState ? true : false;
186
+ await this.debugLog(`StatusActive: ${this.LeakSensor.StatusActive}`);
187
+ // LeakDetected
188
+ this.LeakSensor.LeakDetected = context.detectionState;
189
+ await this.debugLog(`LeakDetected: ${this.LeakSensor.LeakDetected}`);
190
+ }
191
+ // BatteryLevel
192
+ this.Battery.BatteryLevel = Number(context.battery);
193
+ await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
194
+ // StatusLowBattery
195
+ this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
196
+ ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
197
+ await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
198
+ }
199
+
200
+ /**
201
+ * Asks the SwitchBot API for the latest device information
202
+ */
179
203
  async refreshStatus(): Promise<void> {
180
204
  if (!this.device.enableCloudService && this.OpenAPI) {
181
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus enableCloudService: ${this.device.enableCloudService}`);
205
+ await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
182
206
  } else if (this.BLE) {
183
207
  await this.BLERefreshStatus();
184
208
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
185
209
  await this.openAPIRefreshStatus();
186
210
  } else {
187
211
  await this.offlineOff();
188
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
189
- + ` ${this.device.connectionType}, refreshStatus will not happen.`);
212
+ await this.debugWarnLog(`Connection Type: ${this.device.connectionType}, refreshStatus will not happen.`);
190
213
  }
191
214
  }
192
215
 
193
216
  async BLERefreshStatus(): Promise<void> {
194
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
217
+ await this.debugLog('BLERefreshStatus');
195
218
  const switchbot = await this.switchbotBLE();
196
219
 
197
220
  if (switchbot === undefined) {
198
221
  await this.BLERefreshConnection(switchbot);
199
222
  } else {
200
- // Start to monitor advertisement packets
201
- (async () => {
202
223
  // Start to monitor advertisement packets
203
- const serviceData: serviceData = await this.monitorAdvertisementPackets(switchbot);
224
+ (async () => {
225
+ // Start to monitor advertisement packets
226
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as unknown as waterLeakDetectorServiceData;
204
227
  // Update HomeKit
205
- if (serviceData.model !== '' && serviceData.modelName !== '') {
228
+ if (serviceData.model === SwitchBotBLEModel.Unknown && serviceData.modelName === SwitchBotBLEModelName.Unknown) {
206
229
  await this.BLEparseStatus(serviceData);
207
230
  await this.updateHomeKitCharacteristics();
208
231
  } else {
209
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed to get serviceData, serviceData: ${serviceData}`);
232
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
210
233
  await this.BLERefreshConnection(switchbot);
211
234
  }
212
235
  })();
@@ -214,46 +237,39 @@ export class WaterDetector extends deviceBase {
214
237
  }
215
238
 
216
239
  async openAPIRefreshStatus(): Promise<void> {
217
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
240
+ await this.debugLog('openAPIRefreshStatus');
218
241
  try {
219
242
  const { body, statusCode } = await this.deviceRefreshStatus();
220
243
  const deviceStatus: any = await body.json();
221
- await this.refreshStatusCodes(statusCode, deviceStatus);
222
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
223
- await this.successfulRefreshStatus(statusCode, deviceStatus);
224
- await this.openAPIparseStatus(deviceStatus);
244
+ await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
245
+ if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
246
+ await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
247
+ await this.openAPIparseStatus(deviceStatus.body);
225
248
  await this.updateHomeKitCharacteristics();
226
249
  } else {
227
- await this.statusCodes(statusCode, deviceStatus);
250
+ await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
251
+ await this.debugWarnLog(statusCode, deviceStatus);
228
252
  }
229
253
  } catch (e: any) {
230
254
  await this.apiError(e);
231
- await this.openAPIRefreshError(e);
255
+ await this.errorLog(`failed openAPIRefreshStatus with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
232
256
  }
233
257
  }
234
258
 
235
- async registerWebhook(accessory: PlatformAccessory, device: device & devicesConfig) {
236
- if (device.webhook) {
237
- this.debugLog(`${device.deviceType}: ${accessory.displayName} is listening webhook.`);
238
- this.platform.webhookEventHandler[device.deviceId] = async (context) => {
259
+ async registerWebhook() {
260
+ if (this.device.webhook) {
261
+ await this.debugLog('is listening webhook.');
262
+ this.platform.webhookEventHandler[this.device.deviceId] = async (context: waterLeakDetectorWebhookContext) => {
239
263
  try {
240
- this.debugLog(`${device.deviceType}: ${accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
241
- const { detectionState, battery } = context;
242
- const { LeakDetected } = this.LeakSensor ? this.LeakSensor : { LeakDetected: undefined };
243
- const { BatteryLevel } = this.Battery ? this.Battery : { BatteryLevel: undefined };
244
- this.debugLog(`${device.deviceType}: ${accessory.displayName} (detectionState, battery) = Webhook: (${detectionState}, ${battery}), `
245
- + `current: (${LeakDetected}, ${BatteryLevel})`);
246
- if (!device.waterdetector?.hide_leak) {
247
- this.LeakSensor!.LeakDetected = detectionState;
248
- }
249
- this.Battery.BatteryLevel = battery;
250
- this.updateHomeKitCharacteristics();
264
+ await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
265
+ await this.parseStatusWebhook(context);
266
+ await this.updateHomeKitCharacteristics();
251
267
  } catch (e: any) {
252
- this.errorLog(`${device.deviceType}: ${accessory.displayName} failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
268
+ await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
253
269
  }
254
270
  };
255
271
  } else {
256
- this.debugLog(`${device.deviceType}: ${accessory.displayName} is not listening webhook.`);
272
+ await this.debugLog('is not listening webhook.');
257
273
  }
258
274
  }
259
275
 
@@ -261,75 +277,43 @@ export class WaterDetector extends deviceBase {
261
277
  * Updates the status for each of the HomeKit Characteristics
262
278
  */
263
279
  async updateHomeKitCharacteristics(): Promise<void> {
264
- const mqttmessage: string[] = [];
265
- const entry = { time: Math.round(new Date().valueOf() / 1000) };
266
- if (!this.device.waterdetector?.hide_leak) {
267
- if (this.LeakSensor!.LeakDetected === undefined) {
268
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LeakDetected: ${this.LeakSensor!.LeakDetected}`);
269
- } else {
270
- if (this.device.mqttURL) {
271
- mqttmessage.push(`"LeakDetected": ${this.LeakSensor!.LeakDetected}`);
272
- }
273
- if (this.device.history) {
274
- entry['leak'] = this.LeakSensor!.LeakDetected;
275
- }
276
- this.accessory.context.LeakDetected = this.LeakSensor!.LeakDetected;
277
- this.LeakSensor!.Service.updateCharacteristic(this.hap.Characteristic.LeakDetected, this.LeakSensor!.LeakDetected);
278
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic LeakDetected: ${this.LeakSensor!.LeakDetected}`);
279
- }
280
- }
281
- if (this.Battery.BatteryLevel === undefined) {
282
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}`);
283
- } else {
284
- if (this.device.mqttURL) {
285
- mqttmessage.push(`"battery": ${this.Battery.BatteryLevel}`);
286
- }
287
- this.accessory.context.BatteryLevel = this.Battery.BatteryLevel;
288
- this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.Battery.BatteryLevel);
289
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.Battery.BatteryLevel}`);
290
- }
291
- if (this.Battery.StatusLowBattery === undefined) {
292
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.Battery.StatusLowBattery}`);
293
- } else {
294
- if (this.device.mqttURL) {
295
- mqttmessage.push(`"lowBattery": ${this.Battery.StatusLowBattery}`);
296
- }
297
- this.accessory.context.StatusLowBattery = this.Battery.StatusLowBattery;
298
- this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.Battery.StatusLowBattery);
299
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
300
- + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
301
- }
302
- if (this.device.mqttURL) {
303
- this.mqttPublish(`{${mqttmessage.join(',')}}`);
304
- }
305
- if (!this.device.waterdetector?.hide_leak) {
306
- if (Number(this.LeakSensor!.LeakDetected) > 0) {
307
- // reject unreliable data
308
- if (this.device.history) {
309
- this.historyService?.addEntry(entry);
310
- }
311
- }
280
+ if (!this.device.waterdetector?.hide_leak && this.LeakSensor?.Service) {
281
+ // StatusActive
282
+ await this.updateCharacteristic(this.LeakSensor.Service, this.hap.Characteristic.StatusActive,
283
+ this.LeakSensor.StatusActive, 'StatusActive');
284
+ // LeakDetected
285
+ await this.updateCharacteristic(this.LeakSensor.Service, this.hap.Characteristic.LeakDetected,
286
+ this.LeakSensor.LeakDetected, 'LeakDetected');
312
287
  }
288
+ // BatteryLevel
289
+ await this.updateCharacteristic(this.Battery.Service, this.hap.Characteristic.BatteryLevel,
290
+ this.Battery.BatteryLevel, 'BatteryLevel');
291
+ // StatusLowBattery
292
+ await this.updateCharacteristic(this.Battery.Service, this.hap.Characteristic.StatusLowBattery,
293
+ this.Battery.StatusLowBattery, 'StatusLowBattery');
313
294
  }
314
295
 
315
296
  async BLERefreshConnection(switchbot: any): Promise<void> {
316
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot:`
317
- + ` ${JSON.stringify(switchbot)}`);
297
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${JSON.stringify(switchbot)}`);
318
298
  if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
319
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Refresh Status`);
299
+ await this.warnLog('Using OpenAPI Connection to Refresh Status');
320
300
  await this.openAPIRefreshStatus();
321
301
  }
322
302
  }
323
303
 
324
304
  async offlineOff(): Promise<void> {
325
- if (this.device.offline && !this.device.waterdetector?.hide_leak) {
326
- this.LeakSensor!.Service.updateCharacteristic(this.hap.Characteristic.LeakDetected, this.hap.Characteristic.LeakDetected.LEAK_NOT_DETECTED);
305
+ if (this.device.offline) {
306
+ if (!this.device.waterdetector?.hide_leak && this.LeakSensor?.Service) {
307
+ this.LeakSensor.Service.updateCharacteristic(this.hap.Characteristic.StatusActive, false);
308
+ this.LeakSensor.Service.updateCharacteristic(this.hap.Characteristic.LeakDetected, this.hap.Characteristic.LeakDetected.LEAK_NOT_DETECTED);
309
+ }
327
310
  }
328
311
  }
329
312
 
330
- async apiError(e: any): Promise<void> {
331
- if (!this.device.waterdetector?.hide_leak) {
332
- this.LeakSensor!.Service.updateCharacteristic(this.hap.Characteristic.LeakDetected, e);
313
+ async apiError(e: any): Promise < void> {
314
+ if(!this.device.waterdetector?.hide_leak && this.LeakSensor?.Service) {
315
+ this.LeakSensor.Service.updateCharacteristic(this.hap.Characteristic.StatusActive, e);
316
+ this.LeakSensor.Service.updateCharacteristic(this.hap.Characteristic.LeakDetected, e);
333
317
  }
334
318
  this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e);
335
319
  this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e);
@@ -5,7 +5,8 @@
5
5
  import { irdeviceBase } from './irdevice.js';
6
6
 
7
7
  import type { SwitchBotPlatform } from '../platform.js';
8
- import type { irDevicesConfig, irdevice } from '../settings.js';
8
+ import type { irDevicesConfig } from '../settings.js';
9
+ import type { irdevice } from '../types/irdevicelist.js';
9
10
  import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';
10
11
 
11
12
  /**
@@ -5,7 +5,8 @@
5
5
  import { irdeviceBase } from './irdevice.js';
6
6
 
7
7
  import type { SwitchBotPlatform } from '../platform.js';
8
- import type { irDevicesConfig, irdevice } from '../settings.js';
8
+ import type { irDevicesConfig } from '../settings.js';
9
+ import type { irdevice } from '../types/irdevicelist.js';
9
10
  import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';
10
11
 
11
12
  /**
@@ -5,7 +5,8 @@
5
5
  import { irdeviceBase } from './irdevice.js';
6
6
 
7
7
  import type { SwitchBotPlatform } from '../platform.js';
8
- import type { irDevicesConfig, irdevice } from '../settings.js';
8
+ import type { irDevicesConfig } from '../settings.js';
9
+ import type { irdevice } from '../types/irdevicelist.js';
9
10
  import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';
10
11
 
11
12
  /**
@@ -5,7 +5,8 @@
5
5
  import { irdeviceBase } from './irdevice.js';
6
6
 
7
7
  import type { SwitchBotPlatform } from '../platform.js';
8
- import type { irDevicesConfig, irdevice } from '../settings.js';
8
+ import type { irDevicesConfig } from '../settings.js';
9
+ import type { irdevice } from '../types/irdevicelist.js';
9
10
  import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';
10
11
 
11
12
  /**
@@ -7,7 +7,8 @@ import { Devices } from '../settings.js';
7
7
 
8
8
  import type { SwitchBotPlatform } from '../platform.js';
9
9
  import type { API, HAP, Logging, PlatformAccessory } from 'homebridge';
10
- import type { SwitchBotPlatformConfig, irDevicesConfig, irdevice } from '../settings.js';
10
+ import type { SwitchBotPlatformConfig, irDevicesConfig } from '../settings.js';
11
+ import type { irdevice } from '../types/irdevicelist.js';
11
12
 
12
13
  export abstract class irdeviceBase {
13
14
  public readonly api: API;
@@ -179,7 +180,7 @@ export abstract class irdeviceBase {
179
180
  const validVersion = match?.join('.');
180
181
  deviceVersion = validVersion ?? '0.0.0';
181
182
  } else {
182
- deviceVersion = version?.replace(/^V|-.*$/g, '') ?? '0.0.0';
183
+ deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
183
184
  }
184
185
  accessory
185
186
  .getService(this.hap.Service.AccessoryInformation)!
@@ -5,7 +5,8 @@
5
5
  import { irdeviceBase } from './irdevice.js';
6
6
 
7
7
  import type { SwitchBotPlatform } from '../platform.js';
8
- import type { irDevicesConfig, irdevice } from '../settings.js';
8
+ import type { irDevicesConfig } from '../settings.js';
9
+ import type { irdevice } from '../types/irdevicelist.js';
9
10
  import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';
10
11
 
11
12
  /**
@@ -5,7 +5,8 @@
5
5
  import { irdeviceBase } from './irdevice.js';
6
6
 
7
7
  import type { SwitchBotPlatform } from '../platform.js';
8
- import type { irDevicesConfig, irdevice } from '../settings.js';
8
+ import type { irDevicesConfig } from '../settings.js';
9
+ import type { irdevice } from '../types/irdevicelist.js';
9
10
  import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';
10
11
 
11
12
  /**
@@ -5,7 +5,8 @@
5
5
  import { irdeviceBase } from './irdevice.js';
6
6
 
7
7
  import type { SwitchBotPlatform } from '../platform.js';
8
- import type { irDevicesConfig, irdevice } from '../settings.js';
8
+ import type { irDevicesConfig } from '../settings.js';
9
+ import type { irdevice } from '../types/irdevicelist.js';
9
10
  import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';
10
11
 
11
12
  /**
@@ -5,7 +5,8 @@
5
5
  import { irdeviceBase } from './irdevice.js';
6
6
 
7
7
  import type { SwitchBotPlatform } from '../platform.js';
8
- import type { irDevicesConfig, irdevice } from '../settings.js';
8
+ import type { irDevicesConfig } from '../settings.js';
9
+ import type { irdevice } from '../types/irdevicelist.js';
9
10
  import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';
10
11
 
11
12
  /**
@@ -5,7 +5,8 @@
5
5
  import { irdeviceBase } from './irdevice.js';
6
6
 
7
7
  import type { SwitchBotPlatform } from '../platform.js';
8
- import type { irDevicesConfig, irdevice } from '../settings.js';
8
+ import type { irDevicesConfig } from '../settings.js';
9
+ import type { irdevice } from '../types/irdevicelist.js';
9
10
  import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';
10
11
 
11
12
  /**