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