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