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