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