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