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