@switchbot/homebridge-switchbot 3.5.0-beta.6 → 3.5.0-beta.60

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