@switchbot/homebridge-switchbot 3.5.0-beta.4 → 3.5.0-beta.40

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 (168) hide show
  1. package/CHANGELOG.md +4 -1
  2. package/config.schema.json +120 -17
  3. package/dist/device/blindtilt.d.ts +21 -94
  4. package/dist/device/blindtilt.d.ts.map +1 -1
  5. package/dist/device/blindtilt.js +349 -756
  6. package/dist/device/blindtilt.js.map +1 -1
  7. package/dist/device/bot.d.ts +25 -71
  8. package/dist/device/bot.d.ts.map +1 -1
  9. package/dist/device/bot.js +584 -759
  10. package/dist/device/bot.js.map +1 -1
  11. package/dist/device/ceilinglight.d.ts +13 -67
  12. package/dist/device/ceilinglight.d.ts.map +1 -1
  13. package/dist/device/ceilinglight.js +211 -587
  14. package/dist/device/ceilinglight.js.map +1 -1
  15. package/dist/device/colorbulb.d.ts +13 -74
  16. package/dist/device/colorbulb.d.ts.map +1 -1
  17. package/dist/device/colorbulb.js +252 -632
  18. package/dist/device/colorbulb.js.map +1 -1
  19. package/dist/device/contact.d.ts +14 -65
  20. package/dist/device/contact.d.ts.map +1 -1
  21. package/dist/device/contact.js +206 -531
  22. package/dist/device/contact.js.map +1 -1
  23. package/dist/device/curtain.d.ts +17 -86
  24. package/dist/device/curtain.d.ts.map +1 -1
  25. package/dist/device/curtain.js +337 -786
  26. package/dist/device/curtain.js.map +1 -1
  27. package/dist/device/device.d.ts +59 -0
  28. package/dist/device/device.d.ts.map +1 -0
  29. package/dist/device/device.js +671 -0
  30. package/dist/device/device.js.map +1 -0
  31. package/dist/device/fan.d.ts +51 -0
  32. package/dist/device/fan.d.ts.map +1 -0
  33. package/dist/device/fan.js +503 -0
  34. package/dist/device/fan.js.map +1 -0
  35. package/dist/device/hub.d.ts +15 -59
  36. package/dist/device/hub.d.ts.map +1 -1
  37. package/dist/device/hub.js +244 -596
  38. package/dist/device/hub.js.map +1 -1
  39. package/dist/device/humidifier.d.ts +10 -65
  40. package/dist/device/humidifier.d.ts.map +1 -1
  41. package/dist/device/humidifier.js +209 -571
  42. package/dist/device/humidifier.js.map +1 -1
  43. package/dist/device/iosensor.d.ts +11 -64
  44. package/dist/device/iosensor.d.ts.map +1 -1
  45. package/dist/device/iosensor.js +176 -584
  46. package/dist/device/iosensor.js.map +1 -1
  47. package/dist/device/lightstrip.d.ts +10 -68
  48. package/dist/device/lightstrip.d.ts.map +1 -1
  49. package/dist/device/lightstrip.js +254 -568
  50. package/dist/device/lightstrip.js.map +1 -1
  51. package/dist/device/lock.d.ts +15 -62
  52. package/dist/device/lock.d.ts.map +1 -1
  53. package/dist/device/lock.js +212 -542
  54. package/dist/device/lock.js.map +1 -1
  55. package/dist/device/meter.d.ts +13 -63
  56. package/dist/device/meter.d.ts.map +1 -1
  57. package/dist/device/meter.js +172 -577
  58. package/dist/device/meter.js.map +1 -1
  59. package/dist/device/meterplus.d.ts +13 -64
  60. package/dist/device/meterplus.d.ts.map +1 -1
  61. package/dist/device/meterplus.js +161 -577
  62. package/dist/device/meterplus.js.map +1 -1
  63. package/dist/device/motion.d.ts +13 -69
  64. package/dist/device/motion.d.ts.map +1 -1
  65. package/dist/device/motion.js +138 -518
  66. package/dist/device/motion.js.map +1 -1
  67. package/dist/device/plug.d.ts +9 -57
  68. package/dist/device/plug.d.ts.map +1 -1
  69. package/dist/device/plug.js +84 -452
  70. package/dist/device/plug.js.map +1 -1
  71. package/dist/device/robotvacuumcleaner.d.ts +11 -62
  72. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  73. package/dist/device/robotvacuumcleaner.js +142 -451
  74. package/dist/device/robotvacuumcleaner.js.map +1 -1
  75. package/dist/device/waterdetector.d.ts +9 -70
  76. package/dist/device/waterdetector.d.ts.map +1 -1
  77. package/dist/device/waterdetector.js +123 -664
  78. package/dist/device/waterdetector.js.map +1 -1
  79. package/dist/homebridge-ui/public/index.html +2 -2
  80. package/dist/index.js +1 -1
  81. package/dist/irdevice/airconditioner.d.ts +8 -48
  82. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  83. package/dist/irdevice/airconditioner.js +309 -419
  84. package/dist/irdevice/airconditioner.js.map +1 -1
  85. package/dist/irdevice/airpurifier.d.ts +7 -38
  86. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  87. package/dist/irdevice/airpurifier.js +72 -304
  88. package/dist/irdevice/airpurifier.js.map +1 -1
  89. package/dist/irdevice/camera.d.ts +6 -35
  90. package/dist/irdevice/camera.d.ts.map +1 -1
  91. package/dist/irdevice/camera.js +34 -278
  92. package/dist/irdevice/camera.js.map +1 -1
  93. package/dist/irdevice/fan.d.ts +6 -39
  94. package/dist/irdevice/fan.d.ts.map +1 -1
  95. package/dist/irdevice/fan.js +67 -311
  96. package/dist/irdevice/fan.js.map +1 -1
  97. package/dist/irdevice/irdevice.d.ts +40 -0
  98. package/dist/irdevice/irdevice.d.ts.map +1 -0
  99. package/dist/irdevice/irdevice.js +512 -0
  100. package/dist/irdevice/irdevice.js.map +1 -0
  101. package/dist/irdevice/light.d.ts +6 -34
  102. package/dist/irdevice/light.d.ts.map +1 -1
  103. package/dist/irdevice/light.js +23 -272
  104. package/dist/irdevice/light.js.map +1 -1
  105. package/dist/irdevice/other.d.ts +18 -47
  106. package/dist/irdevice/other.d.ts.map +1 -1
  107. package/dist/irdevice/other.js +352 -546
  108. package/dist/irdevice/other.js.map +1 -1
  109. package/dist/irdevice/tv.d.ts +7 -39
  110. package/dist/irdevice/tv.d.ts.map +1 -1
  111. package/dist/irdevice/tv.js +82 -321
  112. package/dist/irdevice/tv.js.map +1 -1
  113. package/dist/irdevice/vacuumcleaner.d.ts +6 -35
  114. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  115. package/dist/irdevice/vacuumcleaner.js +41 -284
  116. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  117. package/dist/irdevice/waterheater.d.ts +6 -35
  118. package/dist/irdevice/waterheater.d.ts.map +1 -1
  119. package/dist/irdevice/waterheater.js +42 -287
  120. package/dist/irdevice/waterheater.js.map +1 -1
  121. package/dist/platform.d.ts +14 -1
  122. package/dist/platform.d.ts.map +1 -1
  123. package/dist/platform.js +343 -198
  124. package/dist/platform.js.map +1 -1
  125. package/dist/settings.d.ts +41 -11
  126. package/dist/settings.d.ts.map +1 -1
  127. package/dist/settings.js +0 -552
  128. package/dist/settings.js.map +1 -1
  129. package/dist/utils.d.ts +77 -0
  130. package/dist/utils.d.ts.map +1 -1
  131. package/dist/utils.js +638 -1
  132. package/dist/utils.js.map +1 -1
  133. package/package.json +16 -15
  134. package/src/custom.d.ts +2 -1
  135. package/src/device/blindtilt.ts +375 -782
  136. package/src/device/bot.ts +618 -844
  137. package/src/device/ceilinglight.ts +224 -607
  138. package/src/device/colorbulb.ts +266 -656
  139. package/src/device/contact.ts +231 -553
  140. package/src/device/curtain.ts +357 -810
  141. package/src/device/device.ts +699 -0
  142. package/src/device/fan.ts +552 -0
  143. package/src/device/hub.ts +271 -624
  144. package/src/device/humidifier.ts +239 -618
  145. package/src/device/iosensor.ts +202 -610
  146. package/src/device/lightstrip.ts +275 -596
  147. package/src/device/lock.ts +249 -582
  148. package/src/device/meter.ts +198 -601
  149. package/src/device/meterplus.ts +186 -602
  150. package/src/device/motion.ts +158 -541
  151. package/src/device/plug.ts +94 -480
  152. package/src/device/robotvacuumcleaner.ts +163 -477
  153. package/src/device/waterdetector.ts +141 -689
  154. package/src/homebridge-ui/public/index.html +2 -2
  155. package/src/index.ts +1 -1
  156. package/src/irdevice/airconditioner.ts +330 -446
  157. package/src/irdevice/airpurifier.ts +89 -321
  158. package/src/irdevice/camera.ts +42 -293
  159. package/src/irdevice/fan.ts +78 -327
  160. package/src/irdevice/irdevice.ts +528 -0
  161. package/src/irdevice/light.ts +32 -287
  162. package/src/irdevice/other.ts +396 -615
  163. package/src/irdevice/tv.ts +100 -339
  164. package/src/irdevice/vacuumcleaner.ts +49 -305
  165. package/src/irdevice/waterheater.ts +50 -310
  166. package/src/platform.ts +353 -206
  167. package/src/settings.ts +57 -566
  168. package/src/utils.ts +642 -1
@@ -1,7 +1,8 @@
1
- import { hostname } from 'os';
2
- import { request } from 'undici';
3
- import { sleep } from '../utils.js';
4
- import asyncmqtt from 'async-mqtt';
1
+ /* Copyright(C) 2021-2024, donavanbecker (https://github.com/donavanbecker). All rights reserved.
2
+ *
3
+ * waterdetector.ts: @switchbot/homebridge-switchbot.
4
+ */
5
+ import { deviceBase } from './device.js';
5
6
  import { interval, Subject } from 'rxjs';
6
7
  import { skipWhile } from 'rxjs/operators';
7
8
  import { Devices } from '../settings.js';
@@ -10,172 +11,61 @@ import { Devices } from '../settings.js';
10
11
  * An instance of this class is created for each accessory your platform registers
11
12
  * Each accessory may expose multiple services of different service types.
12
13
  */
13
- export class WaterDetector {
14
+ export class WaterDetector extends deviceBase {
14
15
  platform;
15
- accessory;
16
- device;
17
- api;
18
- log;
19
- config;
20
- hap;
21
16
  // Services
22
- leakService;
23
- batteryService;
24
- humidityservice;
25
- temperatureservice;
26
- // Characteristic Values
27
- StatusActive;
28
- LeakDetected;
29
- BatteryLevel;
30
- ChargingState;
31
- StatusLowBattery;
32
- FirmwareRevision;
33
- CurrentTemperature;
34
- CurrentRelativeHumidity;
35
- // OpenAPI Status
36
- OpenAPI_BatteryLevel;
37
- OpenAPI_FirmwareRevision;
38
- OpenAPI_CurrentTemperature;
39
- OpenAPI_CurrentRelativeHumidity;
40
- // BLE Status
41
- BLE_Celsius;
42
- BLE_Fahrenheit;
43
- BLE_BatteryLevel;
44
- BLE_CurrentTemperature;
45
- BLE_CurrentRelativeHumidity;
46
- // BLE Others
47
- BLE_IsConnected;
48
- //MQTT stuff
49
- mqttClient = null;
50
- // EVE history service handler
51
- historyService;
52
- // Config
53
- scanDuration;
54
- deviceLogging;
55
- deviceRefreshRate;
17
+ Battery;
18
+ LeakSensor;
56
19
  // Updates
57
20
  WaterDetectorUpdateInProgress;
58
21
  doWaterDetectorUpdate;
59
- // Connection
60
- OpenAPI;
61
- BLE;
62
22
  constructor(platform, accessory, device) {
23
+ super(platform, accessory, device);
63
24
  this.platform = platform;
64
- this.accessory = accessory;
65
- this.device = device;
66
- this.api = this.platform.api;
67
- this.log = this.platform.log;
68
- this.config = this.platform.config;
69
- this.hap = this.api.hap;
70
- // Connection
71
- this.BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
72
- this.OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
73
25
  // default placeholders
74
- this.deviceLogs(device);
75
- this.scan(device);
76
- this.refreshRate(device);
77
- this.deviceContext();
78
26
  this.setupHistoryService(device);
79
- this.setupMqtt(device);
80
- this.deviceConfig(device);
81
27
  // this is subject we use to track when we need to POST changes to the SwitchBot API
82
28
  this.doWaterDetectorUpdate = new Subject();
83
29
  this.WaterDetectorUpdateInProgress = false;
30
+ // Initialize Battery property
31
+ this.Battery = {
32
+ Service: accessory.getService(this.hap.Service.Battery),
33
+ BatteryLevel: accessory.context.BatteryLevel || 100,
34
+ StatusLowBattery: accessory.context.StatusLowBattery || this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL,
35
+ ChargingState: accessory.context.ChargingState || this.hap.Characteristic.ChargingState.NOT_CHARGEABLE,
36
+ };
37
+ // Initialize Leak Sensor property
38
+ if (!this.device.waterdetector?.hide_leak) {
39
+ this.LeakSensor = {
40
+ Service: accessory.getService(this.hap.Service.LeakSensor),
41
+ StatusActive: accessory.context.StatusActive || false,
42
+ LeakDetected: accessory.context.LeakDetected || this.hap.Characteristic.LeakDetected.LEAK_NOT_DETECTED,
43
+ };
44
+ }
84
45
  // Retrieve initial values and updateHomekit
85
46
  this.refreshStatus();
86
- // set accessory information
87
- accessory
88
- .getService(this.hap.Service.AccessoryInformation)
89
- .setCharacteristic(this.hap.Characteristic.Manufacturer, 'SwitchBot')
90
- .setCharacteristic(this.hap.Characteristic.Model, 'WoWaterDetector')
91
- .setCharacteristic(this.hap.Characteristic.SerialNumber, device.deviceId)
92
- .setCharacteristic(this.hap.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
93
47
  // Leak Sensor Service
94
48
  if (device.waterdetector?.hide_leak) {
95
49
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leak Sensor Service`);
96
- this.leakService = this.accessory.getService(this.hap.Service.LeakSensor);
97
- accessory.removeService(this.leakService);
50
+ this.LeakSensor.Service = this.accessory.getService(this.hap.Service.LeakSensor);
51
+ accessory.removeService(this.LeakSensor.Service);
98
52
  }
99
- else if (!this.leakService) {
53
+ else if (!this.LeakSensor?.Service) {
100
54
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Leak Sensor Service`);
101
- const leakService = `${accessory.displayName} Leak Sensor`;
102
- (this.leakService = this.accessory.getService(this.hap.Service.LeakSensor)
103
- || this.accessory.addService(this.hap.Service.LeakSensor)), leakService;
104
- this.leakService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Leak Sensor`);
105
- if (!this.leakService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
106
- this.leakService.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Leak Sensor`);
107
- }
55
+ const LeakSensorService = `${accessory.displayName} Leak Sensor`;
56
+ (this.LeakSensor.Service = this.accessory.getService(this.hap.Service.LeakSensor)
57
+ || this.accessory.addService(this.hap.Service.LeakSensor)), LeakSensorService;
58
+ this.LeakSensor.Service.setCharacteristic(this.hap.Characteristic.Name, LeakSensorService);
108
59
  }
109
60
  else {
110
61
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Leak Sensor Service Not Added`);
111
62
  }
112
- // Temperature Sensor Service
113
- if (device.waterdetector?.hide_temperature) {
114
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Temperature Sensor Service`);
115
- this.temperatureservice = this.accessory.getService(this.hap.Service.TemperatureSensor);
116
- accessory.removeService(this.temperatureservice);
117
- }
118
- else if (!this.temperatureservice) {
119
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Temperature Sensor Service`);
120
- const temperatureservice = `${accessory.displayName} Temperature Sensor`;
121
- (this.temperatureservice = this.accessory.getService(this.hap.Service.TemperatureSensor)
122
- || this.accessory.addService(this.hap.Service.TemperatureSensor)), temperatureservice;
123
- this.temperatureservice.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Temperature Sensor`);
124
- if (!this.temperatureservice.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
125
- this.temperatureservice.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Temperature Sensor`);
126
- }
127
- this.temperatureservice
128
- .getCharacteristic(this.hap.Characteristic.CurrentTemperature)
129
- .setProps({
130
- unit: "celsius" /* Units['CELSIUS'] */,
131
- validValueRanges: [-273.15, 100],
132
- minValue: -273.15,
133
- maxValue: 100,
134
- minStep: 0.1,
135
- })
136
- .onGet(() => {
137
- return this.CurrentTemperature;
138
- });
139
- }
140
- else {
141
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Temperature Sensor Service Not Added`);
142
- }
143
- // Humidity Sensor Service
144
- if (device.waterdetector?.hide_humidity) {
145
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Humidity Sensor Service`);
146
- this.humidityservice = this.accessory.getService(this.hap.Service.HumiditySensor);
147
- accessory.removeService(this.humidityservice);
148
- }
149
- else if (!this.humidityservice) {
150
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Humidity Sensor Service`);
151
- const humidityservice = `${accessory.displayName} Humidity Sensor`;
152
- (this.humidityservice = this.accessory.getService(this.hap.Service.HumiditySensor)
153
- || this.accessory.addService(this.hap.Service.HumiditySensor)), humidityservice;
154
- this.humidityservice.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Humidity Sensor`);
155
- if (!this.humidityservice.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
156
- this.humidityservice.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Humidity Sensor`);
157
- }
158
- this.humidityservice
159
- .getCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity)
160
- .setProps({
161
- minStep: 0.1,
162
- })
163
- .onGet(() => {
164
- return this.CurrentRelativeHumidity;
165
- });
166
- }
167
- else {
168
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Humidity Sensor Service Not Added`);
169
- }
170
63
  // Battery Service
171
- const batteryService = `${accessory.displayName} Battery`;
172
- (this.batteryService = this.accessory.getService(this.hap.Service.Battery)
173
- || accessory.addService(this.hap.Service.Battery)), batteryService;
174
- this.batteryService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Battery`);
175
- if (!this.batteryService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
176
- this.batteryService.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
177
- }
178
- this.batteryService.setCharacteristic(this.hap.Characteristic.ChargingState, this.hap.Characteristic.ChargingState.NOT_CHARGEABLE);
64
+ const BatteryService = `${accessory.displayName} Battery`;
65
+ (this.Battery.Service = this.accessory.getService(this.hap.Service.Battery)
66
+ || accessory.addService(this.hap.Service.Battery)), BatteryService;
67
+ this.Battery.Service.setCharacteristic(this.hap.Characteristic.Name, BatteryService);
68
+ this.Battery.Service.setCharacteristic(this.hap.Characteristic.ChargingState, this.hap.Characteristic.ChargingState.NOT_CHARGEABLE);
179
69
  // Retrieve initial values and updateHomekit
180
70
  this.updateHomeKitCharacteristics();
181
71
  // Start an update interval
@@ -185,22 +75,21 @@ export class WaterDetector {
185
75
  await this.refreshStatus();
186
76
  });
187
77
  //regisiter webhook event handler
188
- if (this.device.webhook) {
189
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} is listening webhook.`);
78
+ if (device.webhook && !this.device.waterdetector?.hide_leak) {
79
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} is listening webhook.`);
190
80
  this.platform.webhookEventHandler[this.device.deviceId] = async (context) => {
191
81
  try {
192
82
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
193
- if (context.scale === 'CELSIUS') {
194
- const { temperature, humidity } = context;
195
- const { CurrentTemperature, CurrentRelativeHumidity } = this;
196
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
197
- '(temperature, humidity) = ' +
198
- `Webhook:(${temperature}, ${humidity}), ` +
199
- `current:(${CurrentTemperature}, ${CurrentRelativeHumidity})`);
200
- this.CurrentRelativeHumidity = humidity;
201
- this.CurrentTemperature = temperature;
202
- this.updateHomeKitCharacteristics();
203
- }
83
+ const { detectionState, battery } = context;
84
+ const { LeakDetected } = this.LeakSensor;
85
+ const { BatteryLevel } = this.Battery;
86
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
87
+ '(detectionState, battery) = ' +
88
+ `Webhook:(${detectionState}, ${battery}), ` +
89
+ `current:(${LeakDetected}, ${BatteryLevel})`);
90
+ this.LeakSensor.LeakDetected = detectionState;
91
+ this.Battery.BatteryLevel = battery;
92
+ this.updateHomeKitCharacteristics();
204
93
  }
205
94
  catch (e) {
206
95
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
@@ -209,88 +98,54 @@ export class WaterDetector {
209
98
  };
210
99
  }
211
100
  }
212
- /**
213
- * Parse the device status from the SwitchBot api
214
- */
215
- async parseStatus() {
216
- if (!this.device.enableCloudService && this.OpenAPI) {
217
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} parseStatus enableCloudService: ${this.device.enableCloudService}`);
218
- }
219
- else if (this.BLE) {
220
- await this.BLEparseStatus();
221
- }
222
- else if (this.OpenAPI && this.platform.config.credentials?.token) {
223
- await this.openAPIparseStatus();
224
- }
225
- else {
226
- await this.offlineOff();
227
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:` + ` ${this.device.connectionType}, parseStatus will not happen.`);
228
- }
229
- }
230
- async BLEparseStatus() {
101
+ async BLEparseStatus(serviceData) {
231
102
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
232
103
  // Battery
233
- this.BatteryLevel = Number(this.BLE_BatteryLevel);
234
- if (this.BatteryLevel < 15) {
235
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
104
+ this.Battery.BatteryLevel = Number(serviceData.battery);
105
+ if (this.Battery.BatteryLevel < 15) {
106
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
236
107
  }
237
108
  else {
238
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
109
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
239
110
  }
240
- this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
241
- // Humidity
242
- if (!this.device.waterdetector?.hide_humidity) {
243
- this.CurrentRelativeHumidity = this.BLE_CurrentRelativeHumidity;
244
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Humidity: ${this.CurrentRelativeHumidity}%`);
245
- }
246
- // Current Temperature
247
- if (!this.device.waterdetector?.hide_temperature) {
248
- this.BLE_Celsius < 0 ? 0 : this.BLE_Celsius > 100 ? 100 : this.BLE_Celsius;
249
- this.CurrentTemperature = this.BLE_Celsius;
250
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Temperature: ${this.CurrentTemperature}°c`);
111
+ this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}, StatusLowBattery: ${this.Battery.StatusLowBattery}`);
112
+ // LeakDetected
113
+ if (this.device.waterdetector?.hide_leak) {
114
+ this.LeakSensor.LeakDetected = serviceData.status;
115
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LeakDetected: ${this.LeakSensor.LeakDetected}`);
251
116
  }
252
117
  }
253
- async openAPIparseStatus() {
118
+ async openAPIparseStatus(deviceStatus) {
254
119
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
255
- // Battery
256
- this.BatteryLevel = Number(this.OpenAPI_BatteryLevel);
257
- if (this.BatteryLevel < 15) {
258
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
120
+ // StatusLowBattery
121
+ this.Battery.BatteryLevel = Number(deviceStatus.body.battery);
122
+ if (this.Battery.BatteryLevel < 10) {
123
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
259
124
  }
260
125
  else {
261
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
262
- }
263
- this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
264
- // Current Relative Humidity
265
- if (!this.device.waterdetector?.hide_humidity) {
266
- this.CurrentRelativeHumidity = this.OpenAPI_CurrentRelativeHumidity;
267
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Humidity: ${this.CurrentRelativeHumidity}%`);
268
- }
269
- // Current Temperature
270
- if (!this.device.waterdetector?.hide_temperature) {
271
- this.CurrentTemperature = this.OpenAPI_CurrentTemperature;
272
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Temperature: ${this.CurrentTemperature}°c`);
126
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
273
127
  }
128
+ this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}, StatusLowBattery: ${this.Battery.StatusLowBattery}`);
274
129
  // BatteryLevel
275
- this.BatteryLevel = Number(this.OpenAPI_BatteryLevel);
276
- if (this.BatteryLevel < 10) {
277
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
130
+ if (Number.isNaN(this.Battery.BatteryLevel)) {
131
+ this.Battery.BatteryLevel = 100;
278
132
  }
279
- else {
280
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
281
- }
282
- if (Number.isNaN(this.BatteryLevel)) {
283
- this.BatteryLevel = 100;
133
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}`);
134
+ // LeakDetected
135
+ if (!this.device.waterdetector?.hide_leak) {
136
+ this.LeakSensor.LeakDetected = deviceStatus.body.status;
137
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LeakDetected: ${this.LeakSensor.LeakDetected}`);
284
138
  }
285
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel},`
286
- + ` StatusLowBattery: ${this.StatusLowBattery}`);
287
139
  // FirmwareRevision
288
- this.FirmwareRevision = this.OpenAPI_FirmwareRevision;
289
- this.accessory.context.FirmwareRevision = this.FirmwareRevision;
140
+ if (deviceStatus.body.version) {
141
+ this.accessory.context.version = deviceStatus.body.version;
142
+ this.accessory
143
+ .getService(this.hap.Service.AccessoryInformation)
144
+ .setCharacteristic(this.hap.Characteristic.FirmwareRevision, this.accessory.context.version)
145
+ .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
146
+ .updateValue(this.accessory.context.version);
147
+ }
290
148
  }
291
- /**
292
- * Asks the SwitchBot API for the latest device information
293
- */
294
149
  async refreshStatus() {
295
150
  if (!this.device.enableCloudService && this.OpenAPI) {
296
151
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus enableCloudService: ${this.device.enableCloudService}`);
@@ -320,107 +175,42 @@ export class WaterDetector {
320
175
  // Start to monitor advertisement packets
321
176
  (async () => {
322
177
  // Start to monitor advertisement packets
323
- await switchbot.startScan({
324
- model: 'w',
325
- id: this.device.bleMac,
326
- });
178
+ await switchbot.startScan({ model: this.device.bleModel, id: this.device.bleMac });
327
179
  // Set an event handler
328
180
  switchbot.onadvertisement = (ad) => {
329
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
330
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.serviceData.model}`);
331
- if (this.device.bleMac === ad.address && ad.serviceData.model === 'w') {
181
+ if (this.device.bleMac === ad.address && ad.model === this.device.bleModel) {
182
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
183
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.model}`);
332
184
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
333
- this.BLE_CurrentTemperature = ad.serviceData.temperature;
334
- this.BLE_Celsius = ad.serviceData.temperature.c;
335
- this.BLE_Fahrenheit = ad.serviceData.temperature.f;
336
- this.BLE_BatteryLevel = ad.serviceData.battery;
337
185
  }
338
186
  else {
339
187
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
340
188
  }
341
189
  };
342
- // Wait 1 seconds
190
+ // Wait 10 seconds
343
191
  await switchbot.wait(this.scanDuration * 1000);
344
192
  // Stop to monitor
345
193
  await switchbot.stopScan();
346
194
  // Update HomeKit
347
- await this.BLEparseStatus();
195
+ await this.BLEparseStatus(switchbot.onadvertisement.serviceData);
348
196
  await this.updateHomeKitCharacteristics();
349
197
  })();
350
- /*if (switchbot !== false) {
351
- switchbot
352
- .startScan({
353
- model: 'w',
354
- id: this.device.bleMac,
355
- })
356
- .then(async () => {
357
- // Set an event handler
358
- switchbot.onadvertisement = async (ad: ad) => {
359
- this.debugLog(
360
- `${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},` +
361
- ` BLE Address Found: ${ad.address}`,
362
- );
363
- if (ad.serviceData.humidity! > 0) {
364
- // reject unreliable data
365
- this.BLE_CurrentRelativeHumidity = ad.serviceData.humidity;
366
- }
367
- this.BLE_CurrentTemperature = ad.serviceData.temperature;
368
- this.BLE_Celsius = ad.serviceData.temperature!.c;
369
- this.BLE_Fahrenheit = ad.serviceData.temperature!.f;
370
- this.BLE_BatteryLevel = ad.serviceData.battery;
371
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
372
- this.debugLog(
373
- `${this.device.deviceType}: ${this.accessory.displayName} model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName}, ` +
374
- `temperature: ${JSON.stringify(ad.serviceData.temperature?.c)}, humidity: ${ad.serviceData.humidity}, ` +
375
- `battery: ${ad.serviceData.battery}`,
376
- );
377
-
378
- if (ad.serviceData) {
379
- this.BLE_IsConnected = true;
380
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.BLE_IsConnected}`);
381
- await this.stopScanning(switchbot);
382
- } else {
383
- this.BLE_IsConnected = false;
384
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.BLE_IsConnected}`);
385
- }
386
- };
387
- // Wait
388
- return await sleep(this.scanDuration * 1000);
389
- })
390
- .then(async () => {
391
- // Stop to monitor
392
- await this.stopScanning(switchbot);
393
- })
394
- .catch(async (e: any) => {
395
- this.apiError(e);
396
- this.errorLog(
397
- `${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}` +
398
- ` Connection, Error Message: ${JSON.stringify(e.message)}`,
399
- );
400
- await this.BLERefreshConnection(switchbot);
401
- });
402
- } else {
403
- await this.BLERefreshConnection(switchbot);
404
- }*/
198
+ if (switchbot === undefined) {
199
+ await this.BLERefreshConnection(switchbot);
200
+ }
405
201
  }
406
202
  async openAPIRefreshStatus() {
407
203
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
408
204
  try {
409
- const { body, statusCode } = await request(`${Devices}/${this.device.deviceId}/status`, {
410
- headers: this.platform.generateHeaders(),
411
- });
205
+ const { body, statusCode } = await this.platform.retryRequest(this.deviceMaxRetries, this.deviceDelayBetweenRetries, `${Devices}/${this.device.deviceId}/status`, { headers: this.platform.generateHeaders() });
412
206
  this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
413
207
  const deviceStatus = await body.json();
414
208
  this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
415
209
  this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
416
210
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
417
- this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
211
+ this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} `
418
212
  + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
419
- this.OpenAPI_CurrentRelativeHumidity = deviceStatus.body.humidity;
420
- this.OpenAPI_CurrentTemperature = deviceStatus.body.temperature;
421
- this.OpenAPI_BatteryLevel = deviceStatus.body.battery;
422
- this.OpenAPI_FirmwareRevision = deviceStatus.body.version;
423
- this.openAPIparseStatus();
213
+ this.openAPIparseStatus(deviceStatus);
424
214
  this.updateHomeKitCharacteristics();
425
215
  }
426
216
  else {
@@ -441,161 +231,56 @@ export class WaterDetector {
441
231
  const mqttmessage = [];
442
232
  const entry = { time: Math.round(new Date().valueOf() / 1000) };
443
233
  if (!this.device.waterdetector?.hide_leak) {
444
- if (this.LeakDetected === undefined) {
445
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LeakDetected: ${this.LeakDetected}`);
446
- }
447
- else {
448
- if (this.device.mqttURL) {
449
- mqttmessage.push(`"LeakDetected": ${this.LeakDetected}`);
450
- }
451
- if (this.device.history) {
452
- entry['leak'] = this.LeakDetected;
453
- }
454
- this.accessory.context.LeakDetected = this.LeakDetected;
455
- this.leakService?.updateCharacteristic(this.hap.Characteristic.LeakDetected, this.LeakDetected);
456
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic LeakDetected: ${this.LeakDetected}`);
457
- }
458
- }
459
- if (!this.device.waterdetector?.hide_humidity) {
460
- if (this.CurrentRelativeHumidity === undefined) {
461
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
462
- }
463
- else {
464
- this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidity;
465
- this.humidityservice?.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, this.CurrentRelativeHumidity);
466
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}` +
467
- ` updateCharacteristic CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
468
- if (this.device.mqttURL) {
469
- mqttmessage.push(`"humidity": ${this.CurrentRelativeHumidity}`);
470
- }
471
- if (this.device.history) {
472
- entry['humidity'] = this.CurrentRelativeHumidity;
473
- }
474
- }
475
- }
476
- if (!this.device.waterdetector?.hide_temperature) {
477
- if (this.CurrentTemperature === undefined) {
478
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
234
+ if (this.LeakSensor.LeakDetected === undefined) {
235
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LeakDetected: ${this.LeakSensor.LeakDetected}`);
479
236
  }
480
237
  else {
481
238
  if (this.device.mqttURL) {
482
- mqttmessage.push(`"temperature": ${this.CurrentTemperature}`);
239
+ mqttmessage.push(`"LeakDetected": ${this.LeakSensor.LeakDetected}`);
483
240
  }
484
241
  if (this.device.history) {
485
- entry['temp'] = this.CurrentTemperature;
242
+ entry['leak'] = this.LeakSensor.LeakDetected;
486
243
  }
487
- this.accessory.context.CurrentTemperature = this.CurrentTemperature;
488
- this.temperatureservice?.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, this.CurrentTemperature);
489
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`);
244
+ this.accessory.context.LeakDetected = this.LeakSensor.LeakDetected;
245
+ this.LeakSensor.Service.updateCharacteristic(this.hap.Characteristic.LeakDetected, this.LeakSensor.LeakDetected);
246
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic LeakDetected: ${this.LeakSensor.LeakDetected}`);
490
247
  }
491
248
  }
492
- if (this.BatteryLevel === undefined) {
493
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
249
+ if (this.Battery.BatteryLevel === undefined) {
250
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}`);
494
251
  }
495
252
  else {
496
253
  if (this.device.mqttURL) {
497
- mqttmessage.push(`"battery": ${this.BatteryLevel}`);
254
+ mqttmessage.push(`"battery": ${this.Battery.BatteryLevel}`);
498
255
  }
499
- this.accessory.context.BatteryLevel = this.BatteryLevel;
500
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.BatteryLevel);
501
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
256
+ this.accessory.context.BatteryLevel = this.Battery.BatteryLevel;
257
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.Battery.BatteryLevel);
258
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.Battery.BatteryLevel}`);
502
259
  }
503
- if (this.StatusLowBattery === undefined) {
504
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
260
+ if (this.Battery.StatusLowBattery === undefined) {
261
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.Battery.StatusLowBattery}`);
505
262
  }
506
263
  else {
507
264
  if (this.device.mqttURL) {
508
- mqttmessage.push(`"lowBattery": ${this.StatusLowBattery}`);
265
+ mqttmessage.push(`"lowBattery": ${this.Battery.StatusLowBattery}`);
509
266
  }
510
- this.accessory.context.StatusLowBattery = this.StatusLowBattery;
511
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.StatusLowBattery);
512
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
267
+ this.accessory.context.StatusLowBattery = this.Battery.StatusLowBattery;
268
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.Battery.StatusLowBattery);
269
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
270
+ + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
513
271
  }
514
272
  if (this.device.mqttURL) {
515
273
  this.mqttPublish(`{${mqttmessage.join(',')}}`);
516
274
  }
517
- if (Number(this.CurrentRelativeHumidity) > 0) {
518
- // reject unreliable data
519
- if (this.device.history) {
520
- this.historyService?.addEntry(entry);
521
- }
522
- }
523
- }
524
- /*
525
- * Publish MQTT message for topics of
526
- * 'homebridge-switchbot/meter/xx:xx:xx:xx:xx:xx'
527
- */
528
- mqttPublish(message) {
529
- const mac = this.device.deviceId
530
- ?.toLowerCase()
531
- .match(/[\s\S]{1,2}/g)
532
- ?.join(':');
533
- const options = this.device.mqttPubOptions || {};
534
- this.mqttClient?.publish(`homebridge-switchbot/meter/${mac}`, `${message}`, options);
535
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MQTT message: ${message} options:${JSON.stringify(options)}`);
536
- }
537
- /*
538
- * Setup MQTT hadler if URL is specified.
539
- */
540
- async setupMqtt(device) {
541
- if (device.mqttURL) {
542
- try {
543
- const { connectAsync } = asyncmqtt;
544
- this.mqttClient = await connectAsync(device.mqttURL, device.mqttOptions || {});
545
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MQTT connection has been established successfully.`);
546
- this.mqttClient.on('error', (e) => {
547
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Failed to publish MQTT messages. ${e}`);
548
- });
549
- }
550
- catch (e) {
551
- this.mqttClient = null;
552
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Failed to establish MQTT connection. ${e}`);
275
+ if (!this.device.waterdetector?.hide_leak) {
276
+ if (Number(this.LeakSensor.LeakDetected) > 0) {
277
+ // reject unreliable data
278
+ if (this.device.history) {
279
+ this.historyService?.addEntry(entry);
280
+ }
553
281
  }
554
282
  }
555
283
  }
556
- /*
557
- * Setup EVE history graph feature if enabled.
558
- */
559
- async setupHistoryService(device) {
560
- const mac = this.device
561
- .deviceId.match(/.{1,2}/g)
562
- .join(':')
563
- .toLowerCase();
564
- this.historyService = device.history
565
- ? new this.platform.fakegatoAPI('room', this.accessory, {
566
- log: this.platform.log,
567
- storage: 'fs',
568
- filename: `${hostname().split('.')[0]}_${mac}_persist.json`,
569
- })
570
- : null;
571
- }
572
- async stopScanning(switchbot) {
573
- switchbot.stopScan();
574
- if (this.BLE_IsConnected) {
575
- await this.BLEparseStatus();
576
- await this.updateHomeKitCharacteristics();
577
- }
578
- else {
579
- await this.BLERefreshConnection(switchbot);
580
- }
581
- }
582
- async getCustomBLEAddress(switchbot) {
583
- if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
584
- (async () => {
585
- // Start to monitor advertisement packets
586
- await switchbot.startScan({
587
- model: 'i',
588
- });
589
- // Set an event handler
590
- switchbot.onadvertisement = (ad) => {
591
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
592
- };
593
- await sleep(10000);
594
- // Stop to monitor
595
- switchbot.stopScan();
596
- })();
597
- }
598
- }
599
284
  async BLERefreshConnection(switchbot) {
600
285
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot:`
601
286
  + ` ${JSON.stringify(switchbot)}`);
@@ -604,243 +289,17 @@ export class WaterDetector {
604
289
  await this.openAPIRefreshStatus();
605
290
  }
606
291
  }
607
- async scan(device) {
608
- if (device.scanDuration) {
609
- this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
610
- if (this.BLE) {
611
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
612
- }
613
- }
614
- else {
615
- this.scanDuration = this.accessory.context.scanDuration = 1;
616
- if (this.BLE) {
617
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
618
- }
619
- }
620
- }
621
- async statusCode(statusCode) {
622
- switch (statusCode) {
623
- case 151:
624
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this deviceType, statusCode: ${statusCode}`);
625
- break;
626
- case 152:
627
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found, statusCode: ${statusCode}`);
628
- break;
629
- case 160:
630
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported, statusCode: ${statusCode}`);
631
- break;
632
- case 161:
633
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline, statusCode: ${statusCode}`);
634
- this.offlineOff();
635
- break;
636
- case 171:
637
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. ` +
638
- `Hub: ${this.device.hubDeviceId}`);
639
- this.offlineOff();
640
- break;
641
- case 190:
642
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
643
- ` Or command format is invalid, statusCode: ${statusCode}`);
644
- break;
645
- case 100:
646
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent, statusCode: ${statusCode}`);
647
- break;
648
- case 200:
649
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Request successful, statusCode: ${statusCode}`);
650
- break;
651
- case 400:
652
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Bad Request, The client has issued an invalid request. `
653
- + `This is commonly used to specify validation errors in a request payload, statusCode: ${statusCode}`);
654
- break;
655
- case 401:
656
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unauthorized, Authorization for the API is required, `
657
- + `but the request has not been authenticated, statusCode: ${statusCode}`);
658
- break;
659
- case 403:
660
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Forbidden, The request has been authenticated but does not `
661
- + `have appropriate permissions, or a requested resource is not found, statusCode: ${statusCode}`);
662
- break;
663
- case 404:
664
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Found, Specifies the requested path does not exist, `
665
- + `statusCode: ${statusCode}`);
666
- break;
667
- case 406:
668
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Acceptable, The client has requested a MIME type via `
669
- + `the Accept header for a value not supported by the server, statusCode: ${statusCode}`);
670
- break;
671
- case 415:
672
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unsupported Media Type, The client has defined a contentType `
673
- + `header that is not supported by the server, statusCode: ${statusCode}`);
674
- break;
675
- case 422:
676
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unprocessable Entity, The client has made a valid request, `
677
- + `but the server cannot process it. This is often used for APIs for which certain limits have been exceeded, statusCode: ${statusCode}`);
678
- break;
679
- case 429:
680
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Too Many Requests, The client has exceeded the number of `
681
- + `requests allowed for a given time window, statusCode: ${statusCode}`);
682
- break;
683
- case 500:
684
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Internal Server Error, An unexpected error on the SmartThings `
685
- + `servers has occurred. These errors should be rare, statusCode: ${statusCode}`);
686
- break;
687
- default:
688
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: ` +
689
- `${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`);
690
- }
691
- }
692
292
  async offlineOff() {
693
- if (this.device.offline) {
694
- await this.deviceContext();
695
- await this.updateHomeKitCharacteristics();
293
+ if (this.device.offline && !this.device.waterdetector?.hide_leak) {
294
+ this.LeakSensor.Service.updateCharacteristic(this.hap.Characteristic.LeakDetected, this.hap.Characteristic.LeakDetected.LEAK_NOT_DETECTED);
696
295
  }
697
296
  }
698
297
  async apiError(e) {
699
298
  if (!this.device.waterdetector?.hide_leak) {
700
- this.leakService?.updateCharacteristic(this.hap.Characteristic.LeakDetected, e);
701
- }
702
- if (!this.device.waterdetector?.hide_humidity) {
703
- this.humidityservice?.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, e);
704
- }
705
- if (!this.device.waterdetector?.hide_temperature) {
706
- this.temperatureservice?.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, e);
707
- }
708
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e);
709
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e);
710
- }
711
- async deviceContext() {
712
- if (this.CurrentRelativeHumidity === undefined) {
713
- this.CurrentRelativeHumidity = 0;
714
- }
715
- else {
716
- this.CurrentRelativeHumidity = this.accessory.context.CurrentRelativeHumidity;
717
- }
718
- if (this.CurrentTemperature === undefined) {
719
- this.CurrentTemperature = 0;
720
- }
721
- else {
722
- this.CurrentTemperature = this.accessory.context.CurrentTemperature;
723
- }
724
- if (this.BatteryLevel === undefined) {
725
- this.BatteryLevel = 100;
726
- }
727
- else {
728
- this.BatteryLevel = this.accessory.context.BatteryLevel;
729
- }
730
- if (this.StatusLowBattery === undefined) {
731
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
732
- this.accessory.context.StatusLowBattery = this.StatusLowBattery;
733
- }
734
- else {
735
- this.StatusLowBattery = this.accessory.context.StatusLowBattery;
736
- }
737
- if (this.FirmwareRevision === undefined) {
738
- this.FirmwareRevision = this.platform.version;
739
- this.accessory.context.FirmwareRevision = this.FirmwareRevision;
740
- }
741
- }
742
- async refreshRate(device) {
743
- if (device.refreshRate) {
744
- this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
745
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
746
- }
747
- else if (this.platform.config.options.refreshRate) {
748
- this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options.refreshRate;
749
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
299
+ this.LeakSensor.Service.updateCharacteristic(this.hap.Characteristic.LeakDetected, e);
750
300
  }
751
- }
752
- async deviceConfig(device) {
753
- let config = {};
754
- if (device.waterdetector) {
755
- config = device.waterdetector;
756
- }
757
- if (device.connectionType !== undefined) {
758
- config['connectionType'] = device.connectionType;
759
- }
760
- if (device.external !== undefined) {
761
- config['external'] = device.external;
762
- }
763
- if (device.mqttURL !== undefined) {
764
- config['mqttURL'] = device.mqttURL;
765
- }
766
- if (device.logging !== undefined) {
767
- config['logging'] = device.logging;
768
- }
769
- if (device.refreshRate !== undefined) {
770
- config['refreshRate'] = device.refreshRate;
771
- }
772
- if (device.scanDuration !== undefined) {
773
- config['scanDuration'] = device.scanDuration;
774
- }
775
- if (device.webhook !== undefined) {
776
- config['webhook'] = device.webhook;
777
- }
778
- if (Object.entries(config).length !== 0) {
779
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
780
- }
781
- }
782
- async deviceLogs(device) {
783
- if (this.platform.debugMode) {
784
- this.deviceLogging = this.accessory.context.logging = 'debugMode';
785
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
786
- }
787
- else if (device.logging) {
788
- this.deviceLogging = this.accessory.context.logging = device.logging;
789
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
790
- }
791
- else if (this.platform.config.options?.logging) {
792
- this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
793
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
794
- }
795
- else {
796
- this.deviceLogging = this.accessory.context.logging = 'standard';
797
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
798
- }
799
- }
800
- /**
801
- * Logging for Device
802
- */
803
- infoLog(...log) {
804
- if (this.enablingDeviceLogging()) {
805
- this.platform.log.info(String(...log));
806
- }
807
- }
808
- warnLog(...log) {
809
- if (this.enablingDeviceLogging()) {
810
- this.platform.log.warn(String(...log));
811
- }
812
- }
813
- debugWarnLog(...log) {
814
- if (this.enablingDeviceLogging()) {
815
- if (this.deviceLogging?.includes('debug')) {
816
- this.platform.log.warn('[DEBUG]', String(...log));
817
- }
818
- }
819
- }
820
- errorLog(...log) {
821
- if (this.enablingDeviceLogging()) {
822
- this.platform.log.error(String(...log));
823
- }
824
- }
825
- debugErrorLog(...log) {
826
- if (this.enablingDeviceLogging()) {
827
- if (this.deviceLogging?.includes('debug')) {
828
- this.platform.log.error('[DEBUG]', String(...log));
829
- }
830
- }
831
- }
832
- debugLog(...log) {
833
- if (this.enablingDeviceLogging()) {
834
- if (this.deviceLogging === 'debug') {
835
- this.platform.log.info('[DEBUG]', String(...log));
836
- }
837
- else {
838
- this.platform.log.debug(String(...log));
839
- }
840
- }
841
- }
842
- enablingDeviceLogging() {
843
- return this.deviceLogging.includes('debug') || this.deviceLogging === 'standard';
301
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e);
302
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e);
844
303
  }
845
304
  }
846
305
  //# sourceMappingURL=waterdetector.js.map