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