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