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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (177) hide show
  1. package/.vscode/settings.json +4 -1
  2. package/CHANGELOG.md +4 -1
  3. package/config.schema.json +164 -22
  4. package/dist/device/blindtilt.d.ts +21 -96
  5. package/dist/device/blindtilt.d.ts.map +1 -1
  6. package/dist/device/blindtilt.js +457 -866
  7. package/dist/device/blindtilt.js.map +1 -1
  8. package/dist/device/bot.d.ts +27 -72
  9. package/dist/device/bot.d.ts.map +1 -1
  10. package/dist/device/bot.js +851 -913
  11. package/dist/device/bot.js.map +1 -1
  12. package/dist/device/ceilinglight.d.ts +15 -69
  13. package/dist/device/ceilinglight.d.ts.map +1 -1
  14. package/dist/device/ceilinglight.js +316 -742
  15. package/dist/device/ceilinglight.js.map +1 -1
  16. package/dist/device/colorbulb.d.ts +15 -76
  17. package/dist/device/colorbulb.d.ts.map +1 -1
  18. package/dist/device/colorbulb.js +380 -833
  19. package/dist/device/colorbulb.js.map +1 -1
  20. package/dist/device/contact.d.ts +14 -66
  21. package/dist/device/contact.d.ts.map +1 -1
  22. package/dist/device/contact.js +267 -602
  23. package/dist/device/contact.js.map +1 -1
  24. package/dist/device/curtain.d.ts +18 -89
  25. package/dist/device/curtain.d.ts.map +1 -1
  26. package/dist/device/curtain.js +469 -897
  27. package/dist/device/curtain.js.map +1 -1
  28. package/dist/device/device.d.ts +76 -0
  29. package/dist/device/device.d.ts.map +1 -0
  30. package/dist/device/device.js +846 -0
  31. package/dist/device/device.js.map +1 -0
  32. package/dist/device/fan.d.ts +63 -0
  33. package/dist/device/fan.d.ts.map +1 -0
  34. package/dist/device/fan.js +645 -0
  35. package/dist/device/fan.js.map +1 -0
  36. package/dist/device/hub.d.ts +19 -58
  37. package/dist/device/hub.d.ts.map +1 -1
  38. package/dist/device/hub.js +304 -640
  39. package/dist/device/hub.js.map +1 -1
  40. package/dist/device/humidifier.d.ts +11 -66
  41. package/dist/device/humidifier.d.ts.map +1 -1
  42. package/dist/device/humidifier.js +300 -710
  43. package/dist/device/humidifier.js.map +1 -1
  44. package/dist/device/iosensor.d.ts +12 -64
  45. package/dist/device/iosensor.d.ts.map +1 -1
  46. package/dist/device/iosensor.js +225 -643
  47. package/dist/device/iosensor.js.map +1 -1
  48. package/dist/device/lightstrip.d.ts +12 -70
  49. package/dist/device/lightstrip.d.ts.map +1 -1
  50. package/dist/device/lightstrip.js +371 -742
  51. package/dist/device/lightstrip.js.map +1 -1
  52. package/dist/device/lock.d.ts +17 -63
  53. package/dist/device/lock.d.ts.map +1 -1
  54. package/dist/device/lock.js +309 -642
  55. package/dist/device/lock.js.map +1 -1
  56. package/dist/device/meter.d.ts +15 -64
  57. package/dist/device/meter.d.ts.map +1 -1
  58. package/dist/device/meter.js +223 -638
  59. package/dist/device/meter.js.map +1 -1
  60. package/dist/device/meterplus.d.ts +15 -65
  61. package/dist/device/meterplus.d.ts.map +1 -1
  62. package/dist/device/meterplus.js +216 -644
  63. package/dist/device/meterplus.js.map +1 -1
  64. package/dist/device/motion.d.ts +12 -69
  65. package/dist/device/motion.d.ts.map +1 -1
  66. package/dist/device/motion.js +209 -588
  67. package/dist/device/motion.js.map +1 -1
  68. package/dist/device/plug.d.ts +10 -57
  69. package/dist/device/plug.d.ts.map +1 -1
  70. package/dist/device/plug.js +141 -532
  71. package/dist/device/plug.js.map +1 -1
  72. package/dist/device/robotvacuumcleaner.d.ts +13 -63
  73. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  74. package/dist/device/robotvacuumcleaner.js +252 -609
  75. package/dist/device/robotvacuumcleaner.js.map +1 -1
  76. package/dist/device/waterdetector.d.ts +11 -63
  77. package/dist/device/waterdetector.d.ts.map +1 -1
  78. package/dist/device/waterdetector.js +179 -521
  79. package/dist/device/waterdetector.js.map +1 -1
  80. package/dist/homebridge-ui/public/index.html +22 -16
  81. package/dist/homebridge-ui/server.js +1 -1
  82. package/dist/homebridge-ui/server.js.map +1 -1
  83. package/dist/index.d.ts +1 -1
  84. package/dist/index.d.ts.map +1 -1
  85. package/dist/index.js +2 -2
  86. package/dist/index.js.map +1 -1
  87. package/dist/irdevice/airconditioner.d.ts +10 -50
  88. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  89. package/dist/irdevice/airconditioner.js +337 -482
  90. package/dist/irdevice/airconditioner.js.map +1 -1
  91. package/dist/irdevice/airpurifier.d.ts +8 -39
  92. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  93. package/dist/irdevice/airpurifier.js +108 -333
  94. package/dist/irdevice/airpurifier.js.map +1 -1
  95. package/dist/irdevice/camera.d.ts +7 -36
  96. package/dist/irdevice/camera.d.ts.map +1 -1
  97. package/dist/irdevice/camera.js +47 -299
  98. package/dist/irdevice/camera.js.map +1 -1
  99. package/dist/irdevice/fan.d.ts +7 -43
  100. package/dist/irdevice/fan.d.ts.map +1 -1
  101. package/dist/irdevice/fan.js +95 -364
  102. package/dist/irdevice/fan.js.map +1 -1
  103. package/dist/irdevice/irdevice.d.ts +46 -0
  104. package/dist/irdevice/irdevice.d.ts.map +1 -0
  105. package/dist/irdevice/irdevice.js +393 -0
  106. package/dist/irdevice/irdevice.js.map +1 -0
  107. package/dist/irdevice/light.d.ts +12 -45
  108. package/dist/irdevice/light.d.ts.map +1 -1
  109. package/dist/irdevice/light.js +121 -376
  110. package/dist/irdevice/light.js.map +1 -1
  111. package/dist/irdevice/other.d.ts +19 -48
  112. package/dist/irdevice/other.d.ts.map +1 -1
  113. package/dist/irdevice/other.js +441 -584
  114. package/dist/irdevice/other.js.map +1 -1
  115. package/dist/irdevice/tv.d.ts +8 -40
  116. package/dist/irdevice/tv.d.ts.map +1 -1
  117. package/dist/irdevice/tv.js +94 -353
  118. package/dist/irdevice/tv.js.map +1 -1
  119. package/dist/irdevice/vacuumcleaner.d.ts +7 -36
  120. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  121. package/dist/irdevice/vacuumcleaner.js +48 -299
  122. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  123. package/dist/irdevice/waterheater.d.ts +7 -36
  124. package/dist/irdevice/waterheater.d.ts.map +1 -1
  125. package/dist/irdevice/waterheater.js +50 -305
  126. package/dist/irdevice/waterheater.js.map +1 -1
  127. package/dist/platform.d.ts +22 -8
  128. package/dist/platform.d.ts.map +1 -1
  129. package/dist/platform.js +342 -191
  130. package/dist/platform.js.map +1 -1
  131. package/dist/settings.d.ts +66 -18
  132. package/dist/settings.d.ts.map +1 -1
  133. package/dist/settings.js +16 -552
  134. package/dist/settings.js.map +1 -1
  135. package/dist/utils.d.ts +95 -0
  136. package/dist/utils.d.ts.map +1 -1
  137. package/dist/utils.js +657 -1
  138. package/dist/utils.js.map +1 -1
  139. package/eslint.config.js +96 -0
  140. package/package.json +20 -16
  141. package/src/custom.d.ts +2 -1
  142. package/src/device/blindtilt.ts +488 -937
  143. package/src/device/bot.ts +907 -1016
  144. package/src/device/ceilinglight.ts +330 -798
  145. package/src/device/colorbulb.ts +394 -904
  146. package/src/device/contact.ts +298 -637
  147. package/src/device/curtain.ts +496 -954
  148. package/src/device/device.ts +872 -0
  149. package/src/device/fan.ts +692 -0
  150. package/src/device/hub.ts +338 -671
  151. package/src/device/humidifier.ts +330 -777
  152. package/src/device/iosensor.ts +258 -671
  153. package/src/device/lightstrip.ts +394 -809
  154. package/src/device/lock.ts +345 -700
  155. package/src/device/meter.ts +256 -666
  156. package/src/device/meterplus.ts +248 -672
  157. package/src/device/motion.ts +234 -622
  158. package/src/device/plug.ts +152 -572
  159. package/src/device/robotvacuumcleaner.ts +273 -653
  160. package/src/device/waterdetector.ts +198 -549
  161. package/src/homebridge-ui/public/index.html +22 -16
  162. package/src/homebridge-ui/server.ts +1 -1
  163. package/src/index.ts +4 -3
  164. package/src/irdevice/airconditioner.ts +368 -540
  165. package/src/irdevice/airpurifier.ts +134 -369
  166. package/src/irdevice/camera.ts +60 -324
  167. package/src/irdevice/fan.ts +111 -392
  168. package/src/irdevice/irdevice.ts +406 -0
  169. package/src/irdevice/light.ts +159 -425
  170. package/src/irdevice/other.ts +509 -661
  171. package/src/irdevice/tv.ts +123 -388
  172. package/src/irdevice/vacuumcleaner.ts +61 -324
  173. package/src/irdevice/waterheater.ts +63 -333
  174. package/src/platform.ts +395 -259
  175. package/src/settings.ts +120 -610
  176. package/src/utils.ts +661 -1
  177. package/src/device/waterdetector.json +0 -11
@@ -1,115 +1,83 @@
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
- import { Devices } from '../settings.js';
8
8
  /**
9
9
  * Platform Accessory
10
10
  * An instance of this class is created for each accessory your platform registers
11
11
  * Each accessory may expose multiple services of different service types.
12
12
  */
13
- export class WaterDetector {
13
+ export class WaterDetector extends deviceBase {
14
14
  platform;
15
- accessory;
16
- device;
17
- api;
18
- log;
19
- config;
20
- hap;
21
15
  // Services
22
- leakService;
23
- batteryService;
24
- // Characteristic Values
25
- StatusActive;
26
- LeakDetected;
27
- BatteryLevel;
28
- ChargingState;
29
- StatusLowBattery;
30
- FirmwareRevision;
31
- // OpenAPI Status
32
- OpenAPI_BatteryLevel;
33
- OpenAPI_FirmwareRevision;
34
- OpenAPI_LeakDetected;
35
- // BLE Status
36
- BLE_BatteryLevel;
37
- BLE_LeakDetected; //serviceData['status'];
38
- // BLE Others
39
- BLE_IsConnected;
40
- //MQTT stuff
41
- mqttClient = null;
42
- // EVE history service handler
43
- historyService;
44
- // Config
45
- scanDuration;
46
- deviceLogging;
47
- deviceRefreshRate;
16
+ Battery;
17
+ LeakSensor;
48
18
  // Updates
49
19
  WaterDetectorUpdateInProgress;
50
20
  doWaterDetectorUpdate;
51
- // Connection
52
- OpenAPI;
53
- BLE;
54
21
  constructor(platform, accessory, device) {
22
+ super(platform, accessory, device);
55
23
  this.platform = platform;
56
- this.accessory = accessory;
57
- this.device = device;
58
- this.api = this.platform.api;
59
- this.log = this.platform.log;
60
- this.config = this.platform.config;
61
- this.hap = this.api.hap;
62
- // Connection
63
- this.BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
64
- this.OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
65
- // default placeholders
66
- this.deviceLogs(device);
67
- this.scan(device);
68
- this.refreshRate(device);
69
- this.deviceContext();
70
- this.setupHistoryService(device);
71
- this.setupMqtt(device);
72
- this.deviceConfig(device);
73
24
  // this is subject we use to track when we need to POST changes to the SwitchBot API
74
25
  this.doWaterDetectorUpdate = new Subject();
75
26
  this.WaterDetectorUpdateInProgress = false;
76
- // Retrieve initial values and updateHomekit
77
- this.refreshStatus();
78
- // set accessory information
79
- accessory
80
- .getService(this.hap.Service.AccessoryInformation)
81
- .setCharacteristic(this.hap.Characteristic.Manufacturer, 'SwitchBot')
82
- .setCharacteristic(this.hap.Characteristic.Model, 'WoWaterDetector')
83
- .setCharacteristic(this.hap.Characteristic.SerialNumber, device.deviceId)
84
- .setCharacteristic(this.hap.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
85
- // Leak Sensor Service
27
+ // Initialize Battery Service
28
+ accessory.context.Battery = accessory.context.Battery ?? {};
29
+ this.Battery = {
30
+ Name: accessory.context.Battery.Name ?? `${accessory.displayName} Battery`,
31
+ Service: accessory.getService(this.hap.Service.Battery) ?? accessory.addService(this.hap.Service.Battery),
32
+ BatteryLevel: accessory.context.BatteryLevel ?? 100,
33
+ StatusLowBattery: accessory.context.StatusLowBattery ?? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL,
34
+ ChargingState: accessory.context.ChargingState ?? this.hap.Characteristic.ChargingState.NOT_CHARGEABLE,
35
+ };
36
+ accessory.context.Battery = this.Battery;
37
+ // Initialize Battery Characteristic
38
+ this.Battery.Service
39
+ .setCharacteristic(this.hap.Characteristic.Name, this.Battery.Name)
40
+ .setCharacteristic(this.hap.Characteristic.ChargingState, this.hap.Characteristic.ChargingState.NOT_CHARGEABLE)
41
+ .getCharacteristic(this.hap.Characteristic.BatteryLevel)
42
+ .onGet(() => {
43
+ return this.Battery.StatusLowBattery;
44
+ });
45
+ this.Battery.Service
46
+ .getCharacteristic(this.hap.Characteristic.StatusLowBattery)
47
+ .onGet(() => {
48
+ return this.Battery.StatusLowBattery;
49
+ });
50
+ // Initialize Leak Sensor Service
86
51
  if (device.waterdetector?.hide_leak) {
87
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leak Sensor Service`);
88
- this.leakService = this.accessory.getService(this.hap.Service.LeakSensor);
89
- accessory.removeService(this.leakService);
90
- }
91
- else if (!this.leakService) {
92
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Leak Sensor Service`);
93
- const leakService = `${accessory.displayName} Leak Sensor`;
94
- (this.leakService = this.accessory.getService(this.hap.Service.LeakSensor)
95
- || this.accessory.addService(this.hap.Service.LeakSensor)), leakService;
96
- this.leakService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Leak Sensor`);
97
- if (!this.leakService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
98
- this.leakService.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Leak Sensor`);
52
+ if (this.LeakSensor) {
53
+ this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leak Sensor Service`);
54
+ this.LeakSensor.Service = this.accessory.getService(this.hap.Service.LeakSensor);
55
+ accessory.removeService(this.LeakSensor.Service);
56
+ }
57
+ else {
58
+ this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Leak Sensor Service Not Found`);
99
59
  }
100
60
  }
101
61
  else {
102
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Leak Sensor Service Not Added`);
103
- }
104
- // Battery Service
105
- const batteryService = `${accessory.displayName} Battery`;
106
- (this.batteryService = this.accessory.getService(this.hap.Service.Battery)
107
- || accessory.addService(this.hap.Service.Battery)), batteryService;
108
- this.batteryService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Battery`);
109
- if (!this.batteryService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
110
- this.batteryService.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
62
+ accessory.context.LeakSensor = accessory.context.LeakSensor ?? {};
63
+ this.LeakSensor = {
64
+ Name: accessory.context.LeakSensor.Name ?? `${accessory.displayName} Leak Sensor`,
65
+ Service: accessory.getService(this.hap.Service.LeakSensor) ?? this.accessory.addService(this.hap.Service.LeakSensor),
66
+ StatusActive: accessory.context.StatusActive ?? false,
67
+ LeakDetected: accessory.context.LeakDetected ?? this.hap.Characteristic.LeakDetected.LEAK_NOT_DETECTED,
68
+ };
69
+ accessory.context.LeakSensor = this.LeakSensor;
70
+ // Initialize LeakSensor Characteristic
71
+ this.LeakSensor.Service
72
+ .setCharacteristic(this.hap.Characteristic.Name, this.LeakSensor.Name)
73
+ .setCharacteristic(this.hap.Characteristic.StatusActive, true)
74
+ .getCharacteristic(this.hap.Characteristic.LeakDetected)
75
+ .onGet(() => {
76
+ return this.LeakSensor.LeakDetected;
77
+ });
111
78
  }
112
- this.batteryService.setCharacteristic(this.hap.Characteristic.ChargingState, this.hap.Characteristic.ChargingState.NOT_CHARGEABLE);
79
+ // Retrieve initial values and updateHomekit
80
+ this.refreshStatus();
113
81
  // Retrieve initial values and updateHomekit
114
82
  this.updateHomeKitCharacteristics();
115
83
  // Start an update interval
@@ -119,86 +87,61 @@ export class WaterDetector {
119
87
  await this.refreshStatus();
120
88
  });
121
89
  //regisiter webhook event handler
122
- if (this.device.webhook) {
123
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} is listening webhook.`);
124
- this.platform.webhookEventHandler[this.device.deviceId] = async (context) => {
125
- try {
126
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
127
- const { status } = context;
128
- const { LeakDetected } = this;
129
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
130
- '(status) = ' +
131
- `Webhook:(${status}), ` +
132
- `current:(${LeakDetected})`);
133
- this.LeakDetected = status;
134
- this.updateHomeKitCharacteristics();
135
- }
136
- catch (e) {
137
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
138
- + `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
139
- }
140
- };
141
- }
90
+ this.registerWebhook(accessory, device);
142
91
  }
143
- /**
144
- * Parse the device status from the SwitchBot api
145
- */
146
- async parseStatus() {
147
- if (!this.device.enableCloudService && this.OpenAPI) {
148
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} parseStatus enableCloudService: ${this.device.enableCloudService}`);
149
- }
150
- else if (this.BLE) {
151
- await this.BLEparseStatus();
152
- }
153
- else if (this.OpenAPI && this.platform.config.credentials?.token) {
154
- await this.openAPIparseStatus();
155
- }
156
- else {
157
- await this.offlineOff();
158
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:` + ` ${this.device.connectionType}, parseStatus will not happen.`);
159
- }
160
- }
161
- async BLEparseStatus() {
92
+ async BLEparseStatus(serviceData) {
162
93
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
163
94
  // Battery
164
- this.BatteryLevel = Number(this.BLE_BatteryLevel);
165
- if (this.BatteryLevel < 15) {
166
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
95
+ this.Battery.BatteryLevel = Number(serviceData.battery);
96
+ if (this.Battery.BatteryLevel < 15) {
97
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
167
98
  }
168
99
  else {
169
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
100
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
170
101
  }
171
- this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
102
+ this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}, StatusLowBattery: ${this.Battery.StatusLowBattery}`);
172
103
  // LeakDetected
173
- this.LeakDetected = this.BLE_LeakDetected;
174
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LeakDetected: ${this.LeakDetected}`);
104
+ if (this.device.waterdetector?.hide_leak) {
105
+ this.LeakSensor.LeakDetected = serviceData.status;
106
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LeakDetected: ${this.LeakSensor.LeakDetected}`);
107
+ }
175
108
  }
176
- async openAPIparseStatus() {
109
+ async openAPIparseStatus(deviceStatus) {
177
110
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
178
111
  // StatusLowBattery
179
- this.BatteryLevel = Number(this.OpenAPI_BatteryLevel);
180
- if (this.BatteryLevel < 10) {
181
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
112
+ this.Battery.BatteryLevel = Number(deviceStatus.body.battery);
113
+ if (this.Battery.BatteryLevel < 10) {
114
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
182
115
  }
183
116
  else {
184
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
117
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
185
118
  }
186
- this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
119
+ this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}, StatusLowBattery: ${this.Battery.StatusLowBattery}`);
187
120
  // BatteryLevel
188
- if (Number.isNaN(this.BatteryLevel)) {
189
- this.BatteryLevel = 100;
121
+ if (Number.isNaN(this.Battery.BatteryLevel)) {
122
+ this.Battery.BatteryLevel = 100;
190
123
  }
191
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
124
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}`);
192
125
  // LeakDetected
193
- this.LeakDetected = this.OpenAPI_LeakDetected;
194
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LeakDetected: ${this.LeakDetected}`);
195
- // FirmwareRevision
196
- this.FirmwareRevision = this.OpenAPI_FirmwareRevision;
197
- this.accessory.context.FirmwareRevision = this.FirmwareRevision;
126
+ if (!this.device.waterdetector?.hide_leak) {
127
+ this.LeakSensor.LeakDetected = deviceStatus.body.status;
128
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LeakDetected: ${this.LeakSensor.LeakDetected}`);
129
+ }
130
+ // Firmware Version
131
+ const version = deviceStatus.body.version?.toString();
132
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Firmware Version: ${version?.replace(/^V|-.*$/g, '')}`);
133
+ if (deviceStatus.body.version) {
134
+ const deviceVersion = version?.replace(/^V|-.*$/g, '') ?? '0.0.0';
135
+ this.accessory
136
+ .getService(this.hap.Service.AccessoryInformation)
137
+ .setCharacteristic(this.hap.Characteristic.HardwareRevision, deviceVersion)
138
+ .setCharacteristic(this.hap.Characteristic.FirmwareRevision, deviceVersion)
139
+ .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
140
+ .updateValue(deviceVersion);
141
+ this.accessory.context.deviceVersion = deviceVersion;
142
+ this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceVersion: ${this.accessory.context.deviceVersion}`);
143
+ }
198
144
  }
199
- /**
200
- * Asks the SwitchBot API for the latest device information
201
- */
202
145
  async refreshStatus() {
203
146
  if (!this.device.enableCloudService && this.OpenAPI) {
204
147
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus enableCloudService: ${this.device.enableCloudService}`);
@@ -211,77 +154,78 @@ export class WaterDetector {
211
154
  }
212
155
  else {
213
156
  await this.offlineOff();
214
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:` +
215
- ` ${this.device.connectionType}, refreshStatus will not happen.`);
157
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
158
+ + ` ${this.device.connectionType}, refreshStatus will not happen.`);
216
159
  }
217
160
  }
218
161
  async BLERefreshStatus() {
219
162
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
220
- const switchbot = await this.platform.connectBLE();
221
- // Convert to BLE Address
222
- this.device.bleMac = this.device
223
- .deviceId.match(/.{1,2}/g)
224
- .join(':')
225
- .toLowerCase();
226
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
227
- this.getCustomBLEAddress(switchbot);
228
- // Start to monitor advertisement packets
229
- (async () => {
163
+ const switchbot = await this.switchbotBLE();
164
+ if (switchbot === undefined) {
165
+ await this.BLERefreshConnection(switchbot);
166
+ }
167
+ else {
230
168
  // Start to monitor advertisement packets
231
- await switchbot.startScan({
232
- model: 'l',
233
- id: this.device.bleMac,
234
- });
235
- // Set an event handler
236
- switchbot.onadvertisement = (ad) => {
237
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
238
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.serviceData.model}`);
239
- if (this.device.bleMac === ad.address && ad.serviceData.model === 'l') {
240
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
241
- this.BLE_LeakDetected = ad.serviceData.status;
242
- this.BLE_BatteryLevel = ad.serviceData.battery;
169
+ (async () => {
170
+ // Start to monitor advertisement packets
171
+ const serviceData = await this.monitorAdvertisementPackets(switchbot);
172
+ // Update HomeKit
173
+ if (serviceData.model !== '' && serviceData.modelName !== '') {
174
+ await this.BLEparseStatus(serviceData);
175
+ await this.updateHomeKitCharacteristics();
243
176
  }
244
177
  else {
245
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
178
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed to get serviceData, serviceData: ${serviceData}`);
179
+ await this.BLERefreshConnection(switchbot);
246
180
  }
247
- };
248
- // Wait 1 seconds
249
- await switchbot.wait(this.scanDuration * 1000);
250
- // Stop to monitor
251
- await switchbot.stopScan();
252
- // Update HomeKit
253
- await this.BLEparseStatus();
254
- await this.updateHomeKitCharacteristics();
255
- })();
181
+ })();
182
+ }
256
183
  }
257
184
  async openAPIRefreshStatus() {
258
185
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
259
186
  try {
260
- const { body, statusCode } = await request(`${Devices}/${this.device.deviceId}/status`, {
261
- headers: this.platform.generateHeaders(),
262
- });
263
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
187
+ const { body, statusCode } = await this.deviceRefreshStatus();
264
188
  const deviceStatus = await body.json();
265
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
266
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
189
+ await this.refreshStatusCodes(statusCode, deviceStatus);
267
190
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
268
- this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
269
- + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
270
- this.OpenAPI_LeakDetected = deviceStatus.body.status;
271
- this.OpenAPI_BatteryLevel = deviceStatus.body.battery;
272
- this.OpenAPI_FirmwareRevision = deviceStatus.body.version;
273
- this.openAPIparseStatus();
274
- this.updateHomeKitCharacteristics();
191
+ await this.successfulRefreshStatus(statusCode, deviceStatus);
192
+ await this.openAPIparseStatus(deviceStatus);
193
+ await this.updateHomeKitCharacteristics();
275
194
  }
276
195
  else {
277
- this.statusCode(statusCode);
278
- this.statusCode(deviceStatus.statusCode);
196
+ await this.statusCodes(statusCode, deviceStatus);
279
197
  }
280
198
  }
281
199
  catch (e) {
282
200
  this.apiError(e);
283
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}` +
284
- ` Connection, Error Message: ${JSON.stringify(e.message)}`);
201
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
202
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
203
+ }
204
+ }
205
+ async registerWebhook(accessory, device) {
206
+ if (device.webhook) {
207
+ this.debugLog(`${device.deviceType}: ${accessory.displayName} is listening webhook.`);
208
+ this.platform.webhookEventHandler[device.deviceId] = async (context) => {
209
+ try {
210
+ this.debugLog(`${device.deviceType}: ${accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
211
+ const { detectionState, battery } = context;
212
+ const { LeakDetected } = this.LeakSensor ? this.LeakSensor : { LeakDetected: undefined };
213
+ const { BatteryLevel } = this.Battery ? this.Battery : { BatteryLevel: undefined };
214
+ this.debugLog(`${device.deviceType}: ${accessory.displayName} (detectionState, battery) = Webhook: (${detectionState}, ${battery}), `
215
+ + `current: (${LeakDetected}, ${BatteryLevel})`);
216
+ if (!device.waterdetector?.hide_leak) {
217
+ this.LeakSensor.LeakDetected = detectionState;
218
+ }
219
+ this.Battery.BatteryLevel = battery;
220
+ this.updateHomeKitCharacteristics();
221
+ }
222
+ catch (e) {
223
+ this.errorLog(`${device.deviceType}: ${accessory.displayName} failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
224
+ }
225
+ };
226
+ }
227
+ else {
228
+ this.debugLog(`${device.deviceType}: ${accessory.displayName} is not listening webhook.`);
285
229
  }
286
230
  }
287
231
  /**
@@ -291,128 +235,56 @@ export class WaterDetector {
291
235
  const mqttmessage = [];
292
236
  const entry = { time: Math.round(new Date().valueOf() / 1000) };
293
237
  if (!this.device.waterdetector?.hide_leak) {
294
- if (this.LeakDetected === undefined) {
295
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LeakDetected: ${this.LeakDetected}`);
238
+ if (this.LeakSensor.LeakDetected === undefined) {
239
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LeakDetected: ${this.LeakSensor.LeakDetected}`);
296
240
  }
297
241
  else {
298
242
  if (this.device.mqttURL) {
299
- mqttmessage.push(`"LeakDetected": ${this.LeakDetected}`);
243
+ mqttmessage.push(`"LeakDetected": ${this.LeakSensor.LeakDetected}`);
300
244
  }
301
245
  if (this.device.history) {
302
- entry['leak'] = this.LeakDetected;
246
+ entry['leak'] = this.LeakSensor.LeakDetected;
303
247
  }
304
- this.accessory.context.LeakDetected = this.LeakDetected;
305
- this.leakService?.updateCharacteristic(this.hap.Characteristic.LeakDetected, this.LeakDetected);
306
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic LeakDetected: ${this.LeakDetected}`);
248
+ this.accessory.context.LeakDetected = this.LeakSensor.LeakDetected;
249
+ this.LeakSensor.Service.updateCharacteristic(this.hap.Characteristic.LeakDetected, this.LeakSensor.LeakDetected);
250
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic LeakDetected: ${this.LeakSensor.LeakDetected}`);
307
251
  }
308
252
  }
309
- if (this.BatteryLevel === undefined) {
310
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
253
+ if (this.Battery.BatteryLevel === undefined) {
254
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}`);
311
255
  }
312
256
  else {
313
257
  if (this.device.mqttURL) {
314
- mqttmessage.push(`"battery": ${this.BatteryLevel}`);
258
+ mqttmessage.push(`"battery": ${this.Battery.BatteryLevel}`);
315
259
  }
316
- this.accessory.context.BatteryLevel = this.BatteryLevel;
317
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.BatteryLevel);
318
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
260
+ this.accessory.context.BatteryLevel = this.Battery.BatteryLevel;
261
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.Battery.BatteryLevel);
262
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.Battery.BatteryLevel}`);
319
263
  }
320
- if (this.StatusLowBattery === undefined) {
321
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
264
+ if (this.Battery.StatusLowBattery === undefined) {
265
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.Battery.StatusLowBattery}`);
322
266
  }
323
267
  else {
324
268
  if (this.device.mqttURL) {
325
- mqttmessage.push(`"lowBattery": ${this.StatusLowBattery}`);
269
+ mqttmessage.push(`"lowBattery": ${this.Battery.StatusLowBattery}`);
326
270
  }
327
- this.accessory.context.StatusLowBattery = this.StatusLowBattery;
328
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.StatusLowBattery);
329
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
271
+ this.accessory.context.StatusLowBattery = this.Battery.StatusLowBattery;
272
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.Battery.StatusLowBattery);
273
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
274
+ + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
330
275
  }
331
276
  if (this.device.mqttURL) {
332
277
  this.mqttPublish(`{${mqttmessage.join(',')}}`);
333
278
  }
334
- if (Number(this.LeakDetected) > 0) {
335
- // reject unreliable data
336
- if (this.device.history) {
337
- this.historyService?.addEntry(entry);
338
- }
339
- }
340
- }
341
- /*
342
- * Publish MQTT message for topics of
343
- * 'homebridge-switchbot/meter/xx:xx:xx:xx:xx:xx'
344
- */
345
- mqttPublish(message) {
346
- const mac = this.device.deviceId
347
- ?.toLowerCase()
348
- .match(/[\s\S]{1,2}/g)
349
- ?.join(':');
350
- const options = this.device.mqttPubOptions || {};
351
- this.mqttClient?.publish(`homebridge-switchbot/meter/${mac}`, `${message}`, options);
352
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MQTT message: ${message} options:${JSON.stringify(options)}`);
353
- }
354
- /*
355
- * Setup MQTT hadler if URL is specified.
356
- */
357
- async setupMqtt(device) {
358
- if (device.mqttURL) {
359
- try {
360
- const { connectAsync } = asyncmqtt;
361
- this.mqttClient = await connectAsync(device.mqttURL, device.mqttOptions || {});
362
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MQTT connection has been established successfully.`);
363
- this.mqttClient.on('error', (e) => {
364
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Failed to publish MQTT messages. ${e}`);
365
- });
366
- }
367
- catch (e) {
368
- this.mqttClient = null;
369
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Failed to establish MQTT connection. ${e}`);
279
+ if (!this.device.waterdetector?.hide_leak) {
280
+ if (Number(this.LeakSensor.LeakDetected) > 0) {
281
+ // reject unreliable data
282
+ if (this.device.history) {
283
+ this.historyService?.addEntry(entry);
284
+ }
370
285
  }
371
286
  }
372
287
  }
373
- /*
374
- * Setup EVE history graph feature if enabled.
375
- */
376
- async setupHistoryService(device) {
377
- const mac = this.device
378
- .deviceId.match(/.{1,2}/g)
379
- .join(':')
380
- .toLowerCase();
381
- this.historyService = device.history
382
- ? new this.platform.fakegatoAPI('room', this.accessory, {
383
- log: this.platform.log,
384
- storage: 'fs',
385
- filename: `${hostname().split('.')[0]}_${mac}_persist.json`,
386
- })
387
- : null;
388
- }
389
- async stopScanning(switchbot) {
390
- switchbot.stopScan();
391
- if (this.BLE_IsConnected) {
392
- await this.BLEparseStatus();
393
- await this.updateHomeKitCharacteristics();
394
- }
395
- else {
396
- await this.BLERefreshConnection(switchbot);
397
- }
398
- }
399
- async getCustomBLEAddress(switchbot) {
400
- if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
401
- (async () => {
402
- // Start to monitor advertisement packets
403
- await switchbot.startScan({
404
- model: 'i',
405
- });
406
- // Set an event handler
407
- switchbot.onadvertisement = (ad) => {
408
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
409
- };
410
- await sleep(10000);
411
- // Stop to monitor
412
- switchbot.stopScan();
413
- })();
414
- }
415
- }
416
288
  async BLERefreshConnection(switchbot) {
417
289
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot:`
418
290
  + ` ${JSON.stringify(switchbot)}`);
@@ -421,231 +293,17 @@ export class WaterDetector {
421
293
  await this.openAPIRefreshStatus();
422
294
  }
423
295
  }
424
- async scan(device) {
425
- if (device.scanDuration) {
426
- this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
427
- if (this.BLE) {
428
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
429
- }
430
- }
431
- else {
432
- this.scanDuration = this.accessory.context.scanDuration = 1;
433
- if (this.BLE) {
434
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
435
- }
436
- }
437
- }
438
- async statusCode(statusCode) {
439
- switch (statusCode) {
440
- case 151:
441
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this deviceType, statusCode: ${statusCode}`);
442
- break;
443
- case 152:
444
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found, statusCode: ${statusCode}`);
445
- break;
446
- case 160:
447
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported, statusCode: ${statusCode}`);
448
- break;
449
- case 161:
450
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline, statusCode: ${statusCode}`);
451
- this.offlineOff();
452
- break;
453
- case 171:
454
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. ` +
455
- `Hub: ${this.device.hubDeviceId}`);
456
- this.offlineOff();
457
- break;
458
- case 190:
459
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
460
- ` Or command format is invalid, statusCode: ${statusCode}`);
461
- break;
462
- case 100:
463
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent, statusCode: ${statusCode}`);
464
- break;
465
- case 200:
466
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Request successful, statusCode: ${statusCode}`);
467
- break;
468
- case 400:
469
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Bad Request, The client has issued an invalid request. `
470
- + `This is commonly used to specify validation errors in a request payload, statusCode: ${statusCode}`);
471
- break;
472
- case 401:
473
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unauthorized, Authorization for the API is required, `
474
- + `but the request has not been authenticated, statusCode: ${statusCode}`);
475
- break;
476
- case 403:
477
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Forbidden, The request has been authenticated but does not `
478
- + `have appropriate permissions, or a requested resource is not found, statusCode: ${statusCode}`);
479
- break;
480
- case 404:
481
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Found, Specifies the requested path does not exist, `
482
- + `statusCode: ${statusCode}`);
483
- break;
484
- case 406:
485
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Acceptable, The client has requested a MIME type via `
486
- + `the Accept header for a value not supported by the server, statusCode: ${statusCode}`);
487
- break;
488
- case 415:
489
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unsupported Media Type, The client has defined a contentType `
490
- + `header that is not supported by the server, statusCode: ${statusCode}`);
491
- break;
492
- case 422:
493
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unprocessable Entity, The client has made a valid request, `
494
- + `but the server cannot process it. This is often used for APIs for which certain limits have been exceeded, statusCode: ${statusCode}`);
495
- break;
496
- case 429:
497
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Too Many Requests, The client has exceeded the number of `
498
- + `requests allowed for a given time window, statusCode: ${statusCode}`);
499
- break;
500
- case 500:
501
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Internal Server Error, An unexpected error on the SmartThings `
502
- + `servers has occurred. These errors should be rare, statusCode: ${statusCode}`);
503
- break;
504
- default:
505
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: ` +
506
- `${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`);
507
- }
508
- }
509
296
  async offlineOff() {
510
- if (this.device.offline) {
511
- await this.deviceContext();
512
- await this.updateHomeKitCharacteristics();
297
+ if (this.device.offline && !this.device.waterdetector?.hide_leak) {
298
+ this.LeakSensor.Service.updateCharacteristic(this.hap.Characteristic.LeakDetected, this.hap.Characteristic.LeakDetected.LEAK_NOT_DETECTED);
513
299
  }
514
300
  }
515
301
  async apiError(e) {
516
302
  if (!this.device.waterdetector?.hide_leak) {
517
- this.leakService?.updateCharacteristic(this.hap.Characteristic.LeakDetected, e);
518
- }
519
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e);
520
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e);
521
- }
522
- async deviceContext() {
523
- if (this.LeakDetected === undefined) {
524
- this.LeakDetected = 0;
525
- }
526
- else {
527
- this.LeakDetected = this.accessory.context.LeakDetected;
528
- }
529
- if (this.BatteryLevel === undefined) {
530
- this.BatteryLevel = 100;
531
- }
532
- else {
533
- this.BatteryLevel = this.accessory.context.BatteryLevel;
534
- }
535
- if (this.StatusLowBattery === undefined) {
536
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
537
- this.accessory.context.StatusLowBattery = this.StatusLowBattery;
538
- }
539
- else {
540
- this.StatusLowBattery = this.accessory.context.StatusLowBattery;
541
- }
542
- if (this.FirmwareRevision === undefined) {
543
- this.FirmwareRevision = this.platform.version;
544
- this.accessory.context.FirmwareRevision = this.FirmwareRevision;
545
- }
546
- }
547
- async refreshRate(device) {
548
- if (device.refreshRate) {
549
- this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
550
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
551
- }
552
- else if (this.platform.config.options.refreshRate) {
553
- this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options.refreshRate;
554
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
555
- }
556
- }
557
- async deviceConfig(device) {
558
- let config = {};
559
- if (device.waterdetector) {
560
- config = device.waterdetector;
303
+ this.LeakSensor.Service.updateCharacteristic(this.hap.Characteristic.LeakDetected, e);
561
304
  }
562
- if (device.connectionType !== undefined) {
563
- config['connectionType'] = device.connectionType;
564
- }
565
- if (device.external !== undefined) {
566
- config['external'] = device.external;
567
- }
568
- if (device.mqttURL !== undefined) {
569
- config['mqttURL'] = device.mqttURL;
570
- }
571
- if (device.logging !== undefined) {
572
- config['logging'] = device.logging;
573
- }
574
- if (device.refreshRate !== undefined) {
575
- config['refreshRate'] = device.refreshRate;
576
- }
577
- if (device.scanDuration !== undefined) {
578
- config['scanDuration'] = device.scanDuration;
579
- }
580
- if (device.webhook !== undefined) {
581
- config['webhook'] = device.webhook;
582
- }
583
- if (Object.entries(config).length !== 0) {
584
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
585
- }
586
- }
587
- async deviceLogs(device) {
588
- if (this.platform.debugMode) {
589
- this.deviceLogging = this.accessory.context.logging = 'debugMode';
590
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
591
- }
592
- else if (device.logging) {
593
- this.deviceLogging = this.accessory.context.logging = device.logging;
594
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
595
- }
596
- else if (this.platform.config.options?.logging) {
597
- this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
598
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
599
- }
600
- else {
601
- this.deviceLogging = this.accessory.context.logging = 'standard';
602
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
603
- }
604
- }
605
- /**
606
- * Logging for Device
607
- */
608
- infoLog(...log) {
609
- if (this.enablingDeviceLogging()) {
610
- this.platform.log.info(String(...log));
611
- }
612
- }
613
- warnLog(...log) {
614
- if (this.enablingDeviceLogging()) {
615
- this.platform.log.warn(String(...log));
616
- }
617
- }
618
- debugWarnLog(...log) {
619
- if (this.enablingDeviceLogging()) {
620
- if (this.deviceLogging?.includes('debug')) {
621
- this.platform.log.warn('[DEBUG]', String(...log));
622
- }
623
- }
624
- }
625
- errorLog(...log) {
626
- if (this.enablingDeviceLogging()) {
627
- this.platform.log.error(String(...log));
628
- }
629
- }
630
- debugErrorLog(...log) {
631
- if (this.enablingDeviceLogging()) {
632
- if (this.deviceLogging?.includes('debug')) {
633
- this.platform.log.error('[DEBUG]', String(...log));
634
- }
635
- }
636
- }
637
- debugLog(...log) {
638
- if (this.enablingDeviceLogging()) {
639
- if (this.deviceLogging === 'debug') {
640
- this.platform.log.info('[DEBUG]', String(...log));
641
- }
642
- else {
643
- this.platform.log.debug(String(...log));
644
- }
645
- }
646
- }
647
- enablingDeviceLogging() {
648
- return this.deviceLogging.includes('debug') || this.deviceLogging === 'standard';
305
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e);
306
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e);
649
307
  }
650
308
  }
651
309
  //# sourceMappingURL=waterdetector.js.map