@switchbot/homebridge-switchbot 3.5.0-beta.23 → 3.5.0-beta.25
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 +9 -9
- 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/lightstrip.ts
CHANGED
|
@@ -1,93 +1,43 @@
|
|
|
1
1
|
import { request } from 'undici';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { interval, skipWhile, Subject } from 'rxjs';
|
|
3
|
+
import { deviceBase } from './device.js';
|
|
4
4
|
import { SwitchBotPlatform } from '../platform.js';
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
} from 'homebridge';
|
|
9
|
-
import { device, devicesConfig, deviceStatus, serviceData, Devices, SwitchBotPlatformConfig } from '../settings.js';
|
|
5
|
+
import { hs2rgb, rgb2hs, m2hs } from '../utils.js';
|
|
6
|
+
import { debounceTime, take, tap } from 'rxjs/operators';
|
|
7
|
+
import { device, devicesConfig, deviceStatus, serviceData, Devices } from '../settings.js';
|
|
8
|
+
import { Service, PlatformAccessory, CharacteristicValue, ControllerConstructor, Controller, ControllerServiceMap } from 'homebridge';
|
|
10
9
|
|
|
11
10
|
/**
|
|
12
11
|
* Platform Accessory
|
|
13
12
|
* An instance of this class is created for each accessory your platform registers
|
|
14
13
|
* Each accessory may expose multiple services of different service types.
|
|
15
14
|
*/
|
|
16
|
-
export class StripLight {
|
|
17
|
-
public readonly api: API;
|
|
18
|
-
public readonly log: Logging;
|
|
19
|
-
public readonly config!: SwitchBotPlatformConfig;
|
|
20
|
-
protected readonly hap: HAP;
|
|
15
|
+
export class StripLight extends deviceBase {
|
|
21
16
|
// Services
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
ColorTemperature?: CharacteristicValue;
|
|
31
|
-
|
|
32
|
-
// OpenAPI Status
|
|
33
|
-
OpenAPI_On: deviceStatus['power'];
|
|
34
|
-
OpenAPI_RGB: deviceStatus['color'];
|
|
35
|
-
OpenAPI_Brightness: deviceStatus['brightness'];
|
|
36
|
-
OpenAPI_FirmwareRevision: deviceStatus['version'];
|
|
37
|
-
|
|
38
|
-
// BLE Status
|
|
39
|
-
BLE_On: serviceData['state'];
|
|
40
|
-
BLE_Brightness: serviceData['brightness'];
|
|
41
|
-
|
|
42
|
-
// BLE Others
|
|
43
|
-
BLE_IsConnected?: boolean;
|
|
44
|
-
|
|
45
|
-
// Config
|
|
46
|
-
set_minStep?: number;
|
|
47
|
-
scanDuration!: number;
|
|
48
|
-
deviceLogging!: string;
|
|
49
|
-
deviceRefreshRate!: number;
|
|
50
|
-
adaptiveLightingShift?: number;
|
|
51
|
-
maxRetries!: number;
|
|
52
|
-
delayBetweenRetries!: number;
|
|
17
|
+
private LightBulb: {
|
|
18
|
+
Service: Service;
|
|
19
|
+
On: CharacteristicValue;
|
|
20
|
+
Hue: CharacteristicValue;
|
|
21
|
+
Saturation: CharacteristicValue;
|
|
22
|
+
Brightness: CharacteristicValue;
|
|
23
|
+
ColorTemperature?: CharacteristicValue;
|
|
24
|
+
};
|
|
53
25
|
|
|
54
26
|
// Adaptive Lighting
|
|
55
27
|
AdaptiveLightingController?: ControllerConstructor | Controller<ControllerServiceMap>;
|
|
56
|
-
|
|
57
|
-
maxKelvin!: number;
|
|
58
|
-
|
|
59
|
-
// Others
|
|
60
|
-
cacheKelvin!: number;
|
|
61
|
-
change!: string;
|
|
28
|
+
adaptiveLightingShift?: number;
|
|
62
29
|
|
|
63
30
|
// Updates
|
|
64
31
|
stripLightUpdateInProgress!: boolean;
|
|
65
32
|
doStripLightUpdate!: Subject<void>;
|
|
66
33
|
|
|
67
|
-
// Connection
|
|
68
|
-
private readonly OpenAPI: boolean;
|
|
69
|
-
private readonly BLE: boolean;
|
|
70
|
-
|
|
71
34
|
constructor(
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
35
|
+
readonly platform: SwitchBotPlatform,
|
|
36
|
+
accessory: PlatformAccessory,
|
|
37
|
+
device: device & devicesConfig,
|
|
75
38
|
) {
|
|
76
|
-
|
|
77
|
-
this.
|
|
78
|
-
this.config = this.platform.config;
|
|
79
|
-
this.hap = this.api.hap;
|
|
80
|
-
// Connection
|
|
81
|
-
this.BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
|
|
82
|
-
this.OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
|
|
83
|
-
// default placeholders
|
|
84
|
-
this.deviceLogs(device);
|
|
85
|
-
this.scan(device);
|
|
86
|
-
this.refreshRate(device);
|
|
87
|
-
this.deviceContext();
|
|
88
|
-
this.deviceRetry(device);
|
|
89
|
-
this.deviceConfig(device);
|
|
90
|
-
|
|
39
|
+
super(platform, accessory, device);
|
|
40
|
+
this.adaptiveLighting(device);
|
|
91
41
|
// this is subject we use to track when we need to POST changes to the SwitchBot API
|
|
92
42
|
this.doStripLightUpdate = new Subject();
|
|
93
43
|
this.stripLightUpdateInProgress = false;
|
|
@@ -95,36 +45,35 @@ export class StripLight {
|
|
|
95
45
|
// Retrieve initial values and updateHomekit
|
|
96
46
|
this.refreshStatus();
|
|
97
47
|
|
|
98
|
-
//
|
|
99
|
-
|
|
100
|
-
.
|
|
101
|
-
.
|
|
102
|
-
.
|
|
103
|
-
.
|
|
104
|
-
|
|
48
|
+
// Initialize the LightBulb property
|
|
49
|
+
this.LightBulb = {
|
|
50
|
+
Service: this.accessory.addService(this.hap.Service.Lightbulb),
|
|
51
|
+
On: accessory.context.On || false,
|
|
52
|
+
Hue: accessory.context.Hue || 0,
|
|
53
|
+
Saturation: accessory.context.Saturation || 0,
|
|
54
|
+
Brightness: accessory.context.Brightness || 0,
|
|
55
|
+
ColorTemperature: accessory.context.ColorTemperature || 140,
|
|
56
|
+
};
|
|
105
57
|
|
|
106
58
|
// get the Lightbulb service if it exists, otherwise create a new Lightbulb service
|
|
107
59
|
// you can create multiple services for each accessory
|
|
108
|
-
const
|
|
109
|
-
(this.
|
|
110
|
-
|| accessory.addService(this.hap.Service.Lightbulb)),
|
|
60
|
+
const LightBulbService = `${accessory.displayName} ${device.deviceType}`;
|
|
61
|
+
(this.LightBulb.Service = accessory.getService(this.hap.Service.Lightbulb)
|
|
62
|
+
|| accessory.addService(this.hap.Service.Lightbulb)), LightBulbService;
|
|
111
63
|
|
|
112
64
|
if (this.adaptiveLightingShift === -1 && this.accessory.context.adaptiveLighting) {
|
|
113
|
-
this.accessory.removeService(this.
|
|
114
|
-
this.
|
|
65
|
+
this.accessory.removeService(this.LightBulb.Service);
|
|
66
|
+
this.LightBulb.Service = this.accessory.addService(this.hap.Service.Lightbulb);
|
|
115
67
|
this.accessory.context.adaptiveLighting = false;
|
|
116
68
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLighting: ${this.accessory.context.adaptiveLighting}`);
|
|
117
69
|
}
|
|
118
70
|
|
|
119
|
-
this.
|
|
120
|
-
if (!this.lightBulbService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
|
|
121
|
-
this.lightBulbService.addCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.displayName);
|
|
122
|
-
}
|
|
71
|
+
this.LightBulb.Service.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
|
|
123
72
|
// handle on / off events using the On characteristic
|
|
124
|
-
this.
|
|
73
|
+
this.LightBulb.Service.getCharacteristic(this.hap.Characteristic.On).onSet(this.OnSet.bind(this));
|
|
125
74
|
|
|
126
75
|
// handle Brightness events using the Brightness characteristic
|
|
127
|
-
this.
|
|
76
|
+
this.LightBulb.Service
|
|
128
77
|
.getCharacteristic(this.hap.Characteristic.Brightness)
|
|
129
78
|
.setProps({
|
|
130
79
|
minStep: this.minStep(device),
|
|
@@ -133,12 +82,12 @@ export class StripLight {
|
|
|
133
82
|
validValueRanges: [0, 100],
|
|
134
83
|
})
|
|
135
84
|
.onGet(() => {
|
|
136
|
-
return this.Brightness;
|
|
85
|
+
return this.LightBulb.Brightness;
|
|
137
86
|
})
|
|
138
87
|
.onSet(this.BrightnessSet.bind(this));
|
|
139
88
|
|
|
140
89
|
// handle ColorTemperature events using the ColorTemperature characteristic
|
|
141
|
-
this.
|
|
90
|
+
this.LightBulb.Service
|
|
142
91
|
.getCharacteristic(this.hap.Characteristic.ColorTemperature)
|
|
143
92
|
.setProps({
|
|
144
93
|
minValue: 140,
|
|
@@ -146,12 +95,12 @@ export class StripLight {
|
|
|
146
95
|
validValueRanges: [140, 500],
|
|
147
96
|
})
|
|
148
97
|
.onGet(() => {
|
|
149
|
-
return this.ColorTemperature!;
|
|
98
|
+
return this.LightBulb.ColorTemperature!;
|
|
150
99
|
})
|
|
151
100
|
.onSet(this.ColorTemperatureSet.bind(this));
|
|
152
101
|
|
|
153
102
|
// handle Hue events using the Hue characteristic
|
|
154
|
-
this.
|
|
103
|
+
this.LightBulb.Service
|
|
155
104
|
.getCharacteristic(this.hap.Characteristic.Hue)
|
|
156
105
|
.setProps({
|
|
157
106
|
minValue: 0,
|
|
@@ -159,12 +108,12 @@ export class StripLight {
|
|
|
159
108
|
validValueRanges: [0, 360],
|
|
160
109
|
})
|
|
161
110
|
.onGet(() => {
|
|
162
|
-
return this.Hue;
|
|
111
|
+
return this.LightBulb.Hue;
|
|
163
112
|
})
|
|
164
113
|
.onSet(this.HueSet.bind(this));
|
|
165
114
|
|
|
166
115
|
// handle Hue events using the Hue characteristic
|
|
167
|
-
this.
|
|
116
|
+
this.LightBulb.Service
|
|
168
117
|
.getCharacteristic(this.hap.Characteristic.Saturation)
|
|
169
118
|
.setProps({
|
|
170
119
|
minValue: 0,
|
|
@@ -172,13 +121,13 @@ export class StripLight {
|
|
|
172
121
|
validValueRanges: [0, 100],
|
|
173
122
|
})
|
|
174
123
|
.onGet(() => {
|
|
175
|
-
return this.Saturation;
|
|
124
|
+
return this.LightBulb.Saturation;
|
|
176
125
|
})
|
|
177
126
|
.onSet(this.SaturationSet.bind(this));
|
|
178
127
|
|
|
179
128
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
|
|
180
129
|
if (this.adaptiveLightingShift !== -1) {
|
|
181
|
-
this.AdaptiveLightingController = new platform.api.hap.AdaptiveLightingController(this.
|
|
130
|
+
this.AdaptiveLightingController = new platform.api.hap.AdaptiveLightingController(this.LightBulb.Service, {
|
|
182
131
|
customTemperatureAdjustment: this.adaptiveLightingShift,
|
|
183
132
|
});
|
|
184
133
|
this.accessory.configureController(this.AdaptiveLightingController);
|
|
@@ -206,13 +155,13 @@ export class StripLight {
|
|
|
206
155
|
try {
|
|
207
156
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
|
|
208
157
|
const { powerState, brightness, color, colorTemperature } = context;
|
|
209
|
-
const { On, Brightness, Hue, Saturation, ColorTemperature } = this;
|
|
158
|
+
const { On, Brightness, Hue, Saturation, ColorTemperature } = this.LightBulb;
|
|
210
159
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
|
|
211
160
|
'(powerState, brightness, color, colorTemperature) = ' +
|
|
212
161
|
`Webhook:(${powerState}, ${brightness}, ${color}, ${colorTemperature}), ` +
|
|
213
162
|
`current:(${On}, ${Brightness}, ${Hue}, ${Saturation}, ${ColorTemperature})`);
|
|
214
|
-
this.On = powerState === 'ON' ? true : false;
|
|
215
|
-
this.Brightness = brightness;
|
|
163
|
+
this.LightBulb.On = powerState === 'ON' ? true : false;
|
|
164
|
+
this.LightBulb.Brightness = brightness;
|
|
216
165
|
|
|
217
166
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} color: ${JSON.stringify(color)}`);
|
|
218
167
|
const [red, green, blue] = color!.split(':');
|
|
@@ -226,12 +175,12 @@ export class StripLight {
|
|
|
226
175
|
);
|
|
227
176
|
|
|
228
177
|
// Hue
|
|
229
|
-
this.Hue = hue;
|
|
230
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.Hue}`);
|
|
178
|
+
this.LightBulb.Hue = hue;
|
|
179
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.LightBulb.Hue}`);
|
|
231
180
|
|
|
232
181
|
// Saturation
|
|
233
|
-
this.Saturation = saturation;
|
|
234
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.Saturation}`);
|
|
182
|
+
this.LightBulb.Saturation = saturation;
|
|
183
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.LightBulb.Saturation}`);
|
|
235
184
|
|
|
236
185
|
this.updateHomeKitCharacteristics();
|
|
237
186
|
} catch (e: any) {
|
|
@@ -264,56 +213,71 @@ export class StripLight {
|
|
|
264
213
|
});
|
|
265
214
|
}
|
|
266
215
|
|
|
267
|
-
|
|
268
|
-
* Parse the device status from the SwitchBot api
|
|
269
|
-
*/
|
|
270
|
-
async parseStatus(): Promise<void> {
|
|
271
|
-
if (!this.device.enableCloudService && this.OpenAPI) {
|
|
272
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} parseStatus enableCloudService: ${this.device.enableCloudService}`);
|
|
273
|
-
} else if (this.BLE) {
|
|
274
|
-
await this.BLEparseStatus();
|
|
275
|
-
} else if (this.OpenAPI && this.platform.config.credentials?.token) {
|
|
276
|
-
await this.openAPIparseStatus();
|
|
277
|
-
} else {
|
|
278
|
-
await this.offlineOff();
|
|
279
|
-
this.debugWarnLog(
|
|
280
|
-
`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:` + ` ${this.device.connectionType}, parseStatus will not happen.`,
|
|
281
|
-
);
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
async BLEparseStatus(): Promise<void> {
|
|
216
|
+
async BLEparseStatus(serviceData: serviceData): Promise<void> {
|
|
286
217
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
|
|
287
218
|
// State
|
|
288
|
-
switch (
|
|
219
|
+
switch (serviceData.state) {
|
|
289
220
|
case 'on':
|
|
290
|
-
this.On = true;
|
|
221
|
+
this.LightBulb.On = true;
|
|
291
222
|
break;
|
|
292
223
|
default:
|
|
293
|
-
this.On = false;
|
|
224
|
+
this.LightBulb.On = false;
|
|
225
|
+
}
|
|
226
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
|
|
227
|
+
|
|
228
|
+
// Brightness
|
|
229
|
+
this.LightBulb.Brightness = Number(serviceData.brightness);
|
|
230
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
|
|
231
|
+
|
|
232
|
+
// Color, Hue & Brightness
|
|
233
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} red: ${serviceData.red}`);
|
|
234
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} green: ${serviceData.green}`);
|
|
235
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} blue: ${serviceData.blue}`);
|
|
236
|
+
|
|
237
|
+
const [hue, saturation] = rgb2hs(Number(serviceData.red), Number(serviceData.green), Number(serviceData.blue));
|
|
238
|
+
this.debugLog(
|
|
239
|
+
`${this.device.deviceType}: ${this.accessory.displayName}` +
|
|
240
|
+
` hs: ${JSON.stringify(rgb2hs(Number(serviceData.red), Number(serviceData.green), Number(serviceData.blue)))}`,
|
|
241
|
+
);
|
|
242
|
+
|
|
243
|
+
// Hue
|
|
244
|
+
this.LightBulb.Hue = hue;
|
|
245
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.LightBulb.Hue}`);
|
|
246
|
+
|
|
247
|
+
// Saturation
|
|
248
|
+
this.LightBulb.Saturation = saturation;
|
|
249
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.LightBulb.Saturation}`);
|
|
250
|
+
|
|
251
|
+
// ColorTemperature
|
|
252
|
+
if (serviceData.color_temperature) {
|
|
253
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${serviceData.color_temperature}`);
|
|
254
|
+
this.LightBulb.ColorTemperature = serviceData.color_temperature!;
|
|
255
|
+
|
|
256
|
+
this.LightBulb.ColorTemperature = Math.max(Math.min(this.LightBulb.ColorTemperature, 500), 140);
|
|
257
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${this.LightBulb.ColorTemperature}`);
|
|
294
258
|
}
|
|
295
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
|
|
296
259
|
}
|
|
297
260
|
|
|
298
|
-
async openAPIparseStatus(): Promise<void> {
|
|
261
|
+
async openAPIparseStatus(deviceStatus: deviceStatus): Promise<void> {
|
|
262
|
+
|
|
299
263
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
|
|
300
|
-
switch (
|
|
264
|
+
switch (deviceStatus.body.power) {
|
|
301
265
|
case 'on':
|
|
302
|
-
this.On = true;
|
|
266
|
+
this.LightBulb.On = true;
|
|
303
267
|
break;
|
|
304
268
|
default:
|
|
305
|
-
this.On = false;
|
|
269
|
+
this.LightBulb.On = false;
|
|
306
270
|
}
|
|
307
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
|
|
271
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
|
|
308
272
|
|
|
309
273
|
// Brightness
|
|
310
|
-
this.Brightness = Number(
|
|
311
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.Brightness}`);
|
|
274
|
+
this.LightBulb.Brightness = Number(deviceStatus.body.brightness);
|
|
275
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
|
|
312
276
|
|
|
313
277
|
// Color, Hue & Brightness
|
|
314
|
-
if (
|
|
315
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} color: ${JSON.stringify(
|
|
316
|
-
const [red, green, blue] =
|
|
278
|
+
if (deviceStatus.body.color) {
|
|
279
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} color: ${JSON.stringify(deviceStatus.body.color)}`);
|
|
280
|
+
const [red, green, blue] = deviceStatus.body.color!.split(':');
|
|
317
281
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} red: ${JSON.stringify(red)}`);
|
|
318
282
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} green: ${JSON.stringify(green)}`);
|
|
319
283
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} blue: ${JSON.stringify(blue)}`);
|
|
@@ -324,17 +288,16 @@ export class StripLight {
|
|
|
324
288
|
);
|
|
325
289
|
|
|
326
290
|
// Hue
|
|
327
|
-
this.Hue = hue;
|
|
328
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.Hue}`);
|
|
291
|
+
this.LightBulb.Hue = hue;
|
|
292
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.LightBulb.Hue}`);
|
|
329
293
|
|
|
330
294
|
// Saturation
|
|
331
|
-
this.Saturation = saturation;
|
|
332
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.Saturation}`);
|
|
295
|
+
this.LightBulb.Saturation = saturation;
|
|
296
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.LightBulb.Saturation}`);
|
|
333
297
|
}
|
|
334
298
|
|
|
335
299
|
// FirmwareRevision
|
|
336
|
-
this.FirmwareRevision =
|
|
337
|
-
this.accessory.context.FirmwareRevision = this.FirmwareRevision;
|
|
300
|
+
this.accessory.context.FirmwareRevision = deviceStatus.body.version;
|
|
338
301
|
}
|
|
339
302
|
|
|
340
303
|
/**
|
|
@@ -369,85 +332,35 @@ export class StripLight {
|
|
|
369
332
|
// Start to monitor advertisement packets
|
|
370
333
|
(async () => {
|
|
371
334
|
// Start to monitor advertisement packets
|
|
372
|
-
await switchbot.startScan({
|
|
373
|
-
model: 'r',
|
|
374
|
-
id: this.device.bleMac,
|
|
375
|
-
});
|
|
335
|
+
await switchbot.startScan({ model: this.device.bleModel, id: this.device.bleMac });
|
|
376
336
|
// Set an event handler
|
|
377
337
|
switchbot.onadvertisement = (ad: any) => {
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
338
|
+
if (this.device.bleMac === ad.address && ad.model === this.device.bleModel) {
|
|
339
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
|
|
340
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.model}`);
|
|
381
341
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
382
342
|
} else {
|
|
383
343
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
384
344
|
}
|
|
385
345
|
};
|
|
386
|
-
// Wait
|
|
346
|
+
// Wait 10 seconds
|
|
387
347
|
await switchbot.wait(this.scanDuration * 1000);
|
|
388
348
|
// Stop to monitor
|
|
389
349
|
await switchbot.stopScan();
|
|
390
350
|
// Update HomeKit
|
|
391
|
-
await this.BLEparseStatus();
|
|
351
|
+
await this.BLEparseStatus(switchbot.onadvertisement.serviceData);
|
|
392
352
|
await this.updateHomeKitCharacteristics();
|
|
393
353
|
})();
|
|
394
|
-
|
|
395
|
-
switchbot
|
|
396
|
-
.startScan({
|
|
397
|
-
model: 'r',
|
|
398
|
-
id: this.device.bleMac,
|
|
399
|
-
})
|
|
400
|
-
.then(async () => {
|
|
401
|
-
// Set an event handler
|
|
402
|
-
switchbot.onadvertisement = async (ad: ad) => {
|
|
403
|
-
this.debugLog(
|
|
404
|
-
`${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},` +
|
|
405
|
-
` BLE Address Found: ${ad.address}`,
|
|
406
|
-
);
|
|
407
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
408
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
409
|
-
this.debugLog(
|
|
410
|
-
`${this.device.deviceType}: ${this.accessory.displayName} state: ${ad.serviceData.state}, ` +
|
|
411
|
-
`delay: ${ad.serviceData.delay}, timer: ${ad.serviceData.timer}, syncUtcTime: ${ad.serviceData.syncUtcTime} ` +
|
|
412
|
-
`wifiRssi: ${ad.serviceData.wifiRssi}, overload: ${ad.serviceData.overload}, currentPower: ${ad.serviceData.currentPower}`,
|
|
413
|
-
);
|
|
414
|
-
|
|
415
|
-
if (ad.serviceData) {
|
|
416
|
-
this.BLE_IsConnected = true;
|
|
417
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.BLE_IsConnected}`);
|
|
418
|
-
await this.stopScanning(switchbot);
|
|
419
|
-
} else {
|
|
420
|
-
this.BLE_IsConnected = false;
|
|
421
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.BLE_IsConnected}`);
|
|
422
|
-
}
|
|
423
|
-
};
|
|
424
|
-
// Wait
|
|
425
|
-
return await sleep(this.scanDuration * 1000);
|
|
426
|
-
})
|
|
427
|
-
.then(async () => {
|
|
428
|
-
// Stop to monitor
|
|
429
|
-
await this.stopScanning(switchbot);
|
|
430
|
-
})
|
|
431
|
-
.catch(async (e: any) => {
|
|
432
|
-
this.apiError(e);
|
|
433
|
-
this.errorLog(
|
|
434
|
-
`${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}` +
|
|
435
|
-
` Connection, Error Message: ${JSON.stringify(e.message)}`,
|
|
436
|
-
);
|
|
437
|
-
await this.BLERefreshConnection(switchbot);
|
|
438
|
-
});
|
|
439
|
-
} else {
|
|
354
|
+
if (switchbot === undefined) {
|
|
440
355
|
await this.BLERefreshConnection(switchbot);
|
|
441
|
-
}
|
|
356
|
+
}
|
|
442
357
|
}
|
|
443
358
|
|
|
444
359
|
async openAPIRefreshStatus(): Promise<void> {
|
|
445
360
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
|
|
446
361
|
try {
|
|
447
|
-
const { body, statusCode } = await this.platform.retryRequest(this.
|
|
448
|
-
`${Devices}/${this.device.deviceId}/status`, {
|
|
449
|
-
headers: this.platform.generateHeaders(),
|
|
450
|
-
});
|
|
362
|
+
const { body, statusCode } = await this.platform.retryRequest(this.deviceMaxRetries, this.deviceDelayBetweenRetries,
|
|
363
|
+
`${Devices}/${this.device.deviceId}/status`, { headers: this.platform.generateHeaders() });
|
|
451
364
|
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
|
|
452
365
|
const deviceStatus: any = await body.json();
|
|
453
366
|
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
|
|
@@ -455,11 +368,7 @@ export class StripLight {
|
|
|
455
368
|
if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
|
|
456
369
|
this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
457
370
|
+ `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
|
|
458
|
-
this.
|
|
459
|
-
this.OpenAPI_RGB = deviceStatus.body.color;
|
|
460
|
-
this.OpenAPI_Brightness = deviceStatus.body.brightness;
|
|
461
|
-
this.OpenAPI_FirmwareRevision = deviceStatus.body.version;
|
|
462
|
-
this.openAPIparseStatus();
|
|
371
|
+
this.openAPIparseStatus(deviceStatus);
|
|
463
372
|
this.updateHomeKitCharacteristics();
|
|
464
373
|
} else {
|
|
465
374
|
this.statusCode(statusCode);
|
|
@@ -508,8 +417,9 @@ export class StripLight {
|
|
|
508
417
|
|
|
509
418
|
async BLEpushChanges(): Promise<void> {
|
|
510
419
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`);
|
|
511
|
-
if (this.On !== this.accessory.context.On) {
|
|
512
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges
|
|
420
|
+
if (this.LightBulb.On !== this.accessory.context.On) {
|
|
421
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`
|
|
422
|
+
+ ` On: ${this.LightBulb.On} OnCached: ${this.accessory.context.On}`);
|
|
513
423
|
const switchbot = await this.platform.connectBLE();
|
|
514
424
|
// Convert to BLE Address
|
|
515
425
|
this.device.bleMac = this.device
|
|
@@ -523,11 +433,11 @@ export class StripLight {
|
|
|
523
433
|
id: this.device.bleMac,
|
|
524
434
|
})
|
|
525
435
|
.then(async (device_list: any) => {
|
|
526
|
-
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
|
|
527
|
-
return await this.
|
|
528
|
-
max: this.
|
|
436
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
|
|
437
|
+
return await this.retryBLE({
|
|
438
|
+
max: await this.maxRetryBLE(),
|
|
529
439
|
fn: async () => {
|
|
530
|
-
if (this.On) {
|
|
440
|
+
if (this.LightBulb.On) {
|
|
531
441
|
return await device_list[0].turnOn({ id: this.device.bleMac });
|
|
532
442
|
} else {
|
|
533
443
|
return await device_list[0].turnOff({ id: this.device.bleMac });
|
|
@@ -538,35 +448,39 @@ export class StripLight {
|
|
|
538
448
|
.then(() => {
|
|
539
449
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
|
|
540
450
|
this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
541
|
-
|
|
542
|
-
this.On = false;
|
|
451
|
+
+ `On: ${this.LightBulb.On} sent over BLE, sent successfully`);
|
|
452
|
+
this.LightBulb.On = false;
|
|
543
453
|
})
|
|
544
454
|
.catch(async (e: any) => {
|
|
545
455
|
this.apiError(e);
|
|
546
|
-
this.errorLog(
|
|
547
|
-
|
|
548
|
-
` Connection, Error Message: ${JSON.stringify(e.message)}`,
|
|
549
|
-
);
|
|
456
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}` +
|
|
457
|
+
` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
550
458
|
await this.BLEPushConnection();
|
|
551
459
|
});
|
|
552
460
|
// Push Brightness Update
|
|
553
|
-
if (this.On) {
|
|
461
|
+
if (this.LightBulb.On) {
|
|
554
462
|
await this.BLEpushBrightnessChanges();
|
|
555
463
|
}
|
|
556
464
|
// Push Hue & Saturation Update
|
|
557
|
-
if (this.On) {
|
|
465
|
+
if (this.LightBulb.On) {
|
|
558
466
|
await this.BLEpushRGBChanges();
|
|
559
467
|
}
|
|
468
|
+
if (this.LightBulb.ColorTemperature !== this.accessory.context.ColorTemperature) {
|
|
469
|
+
const kelvin = Math.round(1000000 / Number(this.LightBulb.ColorTemperature));
|
|
470
|
+
this.accessory.context.kelvin = kelvin;
|
|
471
|
+
} else {
|
|
472
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushColorTemperatureChanges.`
|
|
473
|
+
+ `ColorTemperature: ${this.LightBulb.ColorTemperature}, ColorTemperatureCached: ${this.accessory.context.ColorTemperature}`);
|
|
474
|
+
}
|
|
560
475
|
} else {
|
|
561
|
-
this.debugLog(
|
|
562
|
-
|
|
563
|
-
);
|
|
476
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges,`
|
|
477
|
+
+ ` On: ${this.LightBulb.On}, OnCached: ${this.accessory.context.On}`);
|
|
564
478
|
}
|
|
565
479
|
}
|
|
566
480
|
|
|
567
481
|
async BLEpushBrightnessChanges(): Promise<void> {
|
|
568
482
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushBrightnessChanges`);
|
|
569
|
-
if (this.Brightness !== this.accessory.context.Brightness) {
|
|
483
|
+
if (this.LightBulb.Brightness !== this.accessory.context.Brightness) {
|
|
570
484
|
const switchbot = await this.platform.connectBLE();
|
|
571
485
|
// Convert to BLE Address
|
|
572
486
|
this.device.bleMac = this.device
|
|
@@ -580,12 +494,12 @@ export class StripLight {
|
|
|
580
494
|
id: this.device.bleMac,
|
|
581
495
|
})
|
|
582
496
|
.then(async (device_list: any) => {
|
|
583
|
-
this.infoLog(`${this.accessory.displayName} Target Brightness: ${this.Brightness}`);
|
|
584
|
-
return await device_list[0].setBrightness(this.Brightness);
|
|
497
|
+
this.infoLog(`${this.accessory.displayName} Target Brightness: ${this.LightBulb.Brightness}`);
|
|
498
|
+
return await device_list[0].setBrightness(this.LightBulb.Brightness);
|
|
585
499
|
})
|
|
586
500
|
.then(() => {
|
|
587
501
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
|
|
588
|
-
this.On = false;
|
|
502
|
+
this.LightBulb.On = false;
|
|
589
503
|
})
|
|
590
504
|
.catch(async (e: any) => {
|
|
591
505
|
this.apiError(e);
|
|
@@ -598,7 +512,7 @@ export class StripLight {
|
|
|
598
512
|
} else {
|
|
599
513
|
this.debugLog(
|
|
600
514
|
`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushBrightnessChanges.` +
|
|
601
|
-
`Brightness: ${this.Brightness}, ` +
|
|
515
|
+
`Brightness: ${this.LightBulb.Brightness}, ` +
|
|
602
516
|
`BrightnessCached: ${this.accessory.context.Brightness}`,
|
|
603
517
|
);
|
|
604
518
|
}
|
|
@@ -606,11 +520,11 @@ export class StripLight {
|
|
|
606
520
|
|
|
607
521
|
async BLEpushRGBChanges(): Promise<void> {
|
|
608
522
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushRGBChanges`);
|
|
609
|
-
if (this.Hue !== this.accessory.context.Hue || this.Saturation !== this.accessory.context.Saturation) {
|
|
610
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${JSON.stringify(this.Hue)}`);
|
|
611
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${JSON.stringify(this.Saturation)}`);
|
|
523
|
+
if (this.LightBulb.Hue !== this.accessory.context.Hue || this.LightBulb.Saturation !== this.accessory.context.Saturation) {
|
|
524
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${JSON.stringify(this.LightBulb.Hue)}`);
|
|
525
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${JSON.stringify(this.LightBulb.Saturation)}`);
|
|
612
526
|
|
|
613
|
-
const [red, green, blue] = hs2rgb(Number(this.Hue), Number(this.Saturation));
|
|
527
|
+
const [red, green, blue] = hs2rgb(Number(this.LightBulb.Hue), Number(this.LightBulb.Saturation));
|
|
614
528
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} rgb: ${JSON.stringify([red, green, blue])}`);
|
|
615
529
|
|
|
616
530
|
const switchbot = await this.platform.connectBLE();
|
|
@@ -626,12 +540,12 @@ export class StripLight {
|
|
|
626
540
|
id: this.device.bleMac,
|
|
627
541
|
})
|
|
628
542
|
.then(async (device_list: any) => {
|
|
629
|
-
this.infoLog(`${this.accessory.displayName} Target RGB: ${(this.Brightness, red, green, blue)}`);
|
|
630
|
-
return await device_list[0].setRGB(this.Brightness, red, green, blue);
|
|
543
|
+
this.infoLog(`${this.accessory.displayName} Target RGB: ${(this.LightBulb.Brightness, red, green, blue)}`);
|
|
544
|
+
return await device_list[0].setRGB(this.LightBulb.Brightness, red, green, blue);
|
|
631
545
|
})
|
|
632
546
|
.then(() => {
|
|
633
547
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
|
|
634
|
-
this.On = false;
|
|
548
|
+
this.LightBulb.On = false;
|
|
635
549
|
})
|
|
636
550
|
.catch(async (e: any) => {
|
|
637
551
|
this.apiError(e);
|
|
@@ -643,17 +557,17 @@ export class StripLight {
|
|
|
643
557
|
});
|
|
644
558
|
} else {
|
|
645
559
|
this.debugLog(
|
|
646
|
-
`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushRGBChanges. Hue: ${this.Hue}, ` +
|
|
647
|
-
`HueCached: ${this.accessory.context.Hue}, Saturation: ${this.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`,
|
|
560
|
+
`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushRGBChanges. Hue: ${this.LightBulb.Hue}, ` +
|
|
561
|
+
`HueCached: ${this.accessory.context.Hue}, Saturation: ${this.LightBulb.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`,
|
|
648
562
|
);
|
|
649
563
|
}
|
|
650
564
|
}
|
|
651
565
|
|
|
652
566
|
async openAPIpushChanges() {
|
|
653
567
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
|
|
654
|
-
if (this.On !== this.accessory.context.On) {
|
|
655
|
-
const command = this.On ? 'turnOn' : 'turnOff';
|
|
656
|
-
/*if (this.On) {
|
|
568
|
+
if (this.LightBulb.On !== this.accessory.context.On) {
|
|
569
|
+
const command = this.LightBulb.On ? 'turnOn' : 'turnOff';
|
|
570
|
+
/*if (this.LightBulb.On) {
|
|
657
571
|
command = 'turnOn';
|
|
658
572
|
} else {
|
|
659
573
|
command = 'turnOff';
|
|
@@ -679,7 +593,7 @@ export class StripLight {
|
|
|
679
593
|
this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
680
594
|
+ `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
|
|
681
595
|
this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
682
|
-
|
|
596
|
+
+ `request to SwitchBot API, body: ${deviceStatus} sent successfully`);
|
|
683
597
|
} else {
|
|
684
598
|
this.statusCode(statusCode);
|
|
685
599
|
this.statusCode(deviceStatus.statusCode);
|
|
@@ -694,26 +608,26 @@ export class StripLight {
|
|
|
694
608
|
} else {
|
|
695
609
|
this.debugLog(
|
|
696
610
|
`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.` +
|
|
697
|
-
`On: ${this.On}, ` +
|
|
611
|
+
`On: ${this.LightBulb.On}, ` +
|
|
698
612
|
`OnCached: ${this.accessory.context.On}`,
|
|
699
613
|
);
|
|
700
614
|
}
|
|
701
615
|
// Push Hue & Saturation Update
|
|
702
|
-
if (this.On) {
|
|
616
|
+
if (this.LightBulb.On) {
|
|
703
617
|
await this.pushHueSaturationChanges();
|
|
704
618
|
}
|
|
705
619
|
// Push Brightness Update
|
|
706
|
-
if (this.On) {
|
|
620
|
+
if (this.LightBulb.On) {
|
|
707
621
|
await this.pushBrightnessChanges();
|
|
708
622
|
}
|
|
709
623
|
}
|
|
710
624
|
|
|
711
625
|
async pushHueSaturationChanges(): Promise<void> {
|
|
712
626
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushHueSaturationChanges`);
|
|
713
|
-
if (this.Hue !== this.accessory.context.Hue || this.Saturation !== this.accessory.context.Saturation) {
|
|
714
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${JSON.stringify(this.Hue)}`);
|
|
715
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${JSON.stringify(this.Saturation)}`);
|
|
716
|
-
const [red, green, blue] = hs2rgb(Number(this.Hue), Number(this.Saturation));
|
|
627
|
+
if (this.LightBulb.Hue !== this.accessory.context.Hue || this.LightBulb.Saturation !== this.accessory.context.Saturation) {
|
|
628
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${JSON.stringify(this.LightBulb.Hue)}`);
|
|
629
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${JSON.stringify(this.LightBulb.Saturation)}`);
|
|
630
|
+
const [red, green, blue] = hs2rgb(Number(this.LightBulb.Hue), Number(this.LightBulb.Saturation));
|
|
717
631
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} rgb: ${JSON.stringify([red, green, blue])}`);
|
|
718
632
|
// Make Push On request to the API
|
|
719
633
|
const bodyChange = JSON.stringify({
|
|
@@ -737,7 +651,7 @@ export class StripLight {
|
|
|
737
651
|
this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
738
652
|
+ `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
|
|
739
653
|
this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
740
|
-
|
|
654
|
+
+ `request to SwitchBot API, body: ${deviceStatus} sent successfully`);
|
|
741
655
|
} else {
|
|
742
656
|
this.statusCode(statusCode);
|
|
743
657
|
this.statusCode(deviceStatus.statusCode);
|
|
@@ -751,18 +665,18 @@ export class StripLight {
|
|
|
751
665
|
}
|
|
752
666
|
} else {
|
|
753
667
|
this.debugLog(
|
|
754
|
-
`${this.device.deviceType}: ${this.accessory.displayName} No pushHueSaturationChanges. Hue: ${this.Hue}, ` +
|
|
755
|
-
`HueCached: ${this.accessory.context.Hue}, Saturation: ${this.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`,
|
|
668
|
+
`${this.device.deviceType}: ${this.accessory.displayName} No pushHueSaturationChanges. Hue: ${this.LightBulb.Hue}, ` +
|
|
669
|
+
`HueCached: ${this.accessory.context.Hue}, Saturation: ${this.LightBulb.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`,
|
|
756
670
|
);
|
|
757
671
|
}
|
|
758
672
|
}
|
|
759
673
|
|
|
760
674
|
async pushBrightnessChanges(): Promise<void> {
|
|
761
675
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushBrightnessChanges`);
|
|
762
|
-
if (this.Brightness !== this.accessory.context.Brightness) {
|
|
676
|
+
if (this.LightBulb.Brightness !== this.accessory.context.Brightness) {
|
|
763
677
|
const bodyChange = JSON.stringify({
|
|
764
678
|
command: 'setBrightness',
|
|
765
|
-
parameter: `${this.Brightness}`,
|
|
679
|
+
parameter: `${this.LightBulb.Brightness}`,
|
|
766
680
|
commandType: 'command',
|
|
767
681
|
});
|
|
768
682
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
|
|
@@ -781,7 +695,7 @@ export class StripLight {
|
|
|
781
695
|
this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
782
696
|
+ `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
|
|
783
697
|
this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
784
|
-
|
|
698
|
+
+ `request to SwitchBot API, body: ${deviceStatus} sent successfully`);
|
|
785
699
|
} else {
|
|
786
700
|
this.statusCode(statusCode);
|
|
787
701
|
this.statusCode(deviceStatus.statusCode);
|
|
@@ -796,7 +710,7 @@ export class StripLight {
|
|
|
796
710
|
} else {
|
|
797
711
|
this.debugLog(
|
|
798
712
|
`${this.device.deviceType}: ${this.accessory.displayName} No pushBrightnessChanges,` +
|
|
799
|
-
`Brightness: ${this.Brightness}, ` +
|
|
713
|
+
`Brightness: ${this.LightBulb.Brightness}, ` +
|
|
800
714
|
`BrightnessCached: ${this.accessory.context.Brightness}`,
|
|
801
715
|
);
|
|
802
716
|
}
|
|
@@ -806,13 +720,13 @@ export class StripLight {
|
|
|
806
720
|
* Handle requests to set the value of the "On" characteristic
|
|
807
721
|
*/
|
|
808
722
|
async OnSet(value: CharacteristicValue): Promise<void> {
|
|
809
|
-
if (this.On === this.accessory.context.On) {
|
|
723
|
+
if (this.LightBulb.On === this.accessory.context.On) {
|
|
810
724
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set On: ${value}`);
|
|
811
725
|
} else {
|
|
812
726
|
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set On: ${value}`);
|
|
813
727
|
}
|
|
814
728
|
|
|
815
|
-
this.On = value;
|
|
729
|
+
this.LightBulb.On = value;
|
|
816
730
|
this.doStripLightUpdate.next();
|
|
817
731
|
}
|
|
818
732
|
|
|
@@ -820,15 +734,15 @@ export class StripLight {
|
|
|
820
734
|
* Handle requests to set the value of the "Brightness" characteristic
|
|
821
735
|
*/
|
|
822
736
|
async BrightnessSet(value: CharacteristicValue): Promise<void> {
|
|
823
|
-
if (this.Brightness === this.accessory.context.Brightness) {
|
|
737
|
+
if (this.LightBulb.Brightness === this.accessory.context.Brightness) {
|
|
824
738
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Brightness: ${value}`);
|
|
825
|
-
} else if (this.On) {
|
|
739
|
+
} else if (this.LightBulb.On) {
|
|
826
740
|
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Brightness: ${value}`);
|
|
827
741
|
} else {
|
|
828
742
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Brightness: ${value}`);
|
|
829
743
|
}
|
|
830
744
|
|
|
831
|
-
this.Brightness = value;
|
|
745
|
+
this.LightBulb.Brightness = value;
|
|
832
746
|
this.doStripLightUpdate.next();
|
|
833
747
|
}
|
|
834
748
|
|
|
@@ -836,30 +750,31 @@ export class StripLight {
|
|
|
836
750
|
* Handle requests to set the value of the "ColorTemperature" characteristic
|
|
837
751
|
*/
|
|
838
752
|
async ColorTemperatureSet(value: CharacteristicValue): Promise<void> {
|
|
839
|
-
if (this.ColorTemperature === this.accessory.context.ColorTemperature) {
|
|
753
|
+
if (this.LightBulb.ColorTemperature === this.accessory.context.ColorTemperature) {
|
|
840
754
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set ColorTemperature: ${value}`);
|
|
841
|
-
} else if (this.On) {
|
|
755
|
+
} else if (this.LightBulb.On) {
|
|
842
756
|
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set ColorTemperature: ${value}`);
|
|
843
757
|
} else {
|
|
844
758
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set ColorTemperature: ${value}`);
|
|
845
759
|
}
|
|
846
760
|
|
|
761
|
+
const minKelvin = 2000;
|
|
762
|
+
const maxKelvin = 9000;
|
|
847
763
|
// Convert mired to kelvin to nearest 100 (SwitchBot seems to need this)
|
|
848
764
|
const kelvin = Math.round(1000000 / Number(value) / 100) * 100;
|
|
849
|
-
|
|
850
765
|
// Check and increase/decrease kelvin to range of device
|
|
851
|
-
const k = Math.min(Math.max(kelvin,
|
|
766
|
+
const k = Math.min(Math.max(kelvin, minKelvin), maxKelvin);
|
|
852
767
|
|
|
853
|
-
if (!this.accessory.context.On || this.
|
|
768
|
+
if (!this.accessory.context.On || this.accessory.context.maxKelvin === k) {
|
|
854
769
|
return;
|
|
855
770
|
}
|
|
856
771
|
|
|
857
772
|
// Updating the hue/sat to the corresponding values mimics native adaptive lighting
|
|
858
773
|
const hs = m2hs(value);
|
|
859
|
-
this.
|
|
860
|
-
this.
|
|
774
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Hue, hs[0]);
|
|
775
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Saturation, hs[1]);
|
|
861
776
|
|
|
862
|
-
this.ColorTemperature = value;
|
|
777
|
+
this.LightBulb.ColorTemperature = value;
|
|
863
778
|
this.doStripLightUpdate.next();
|
|
864
779
|
}
|
|
865
780
|
|
|
@@ -867,17 +782,17 @@ export class StripLight {
|
|
|
867
782
|
* Handle requests to set the value of the "Hue" characteristic
|
|
868
783
|
*/
|
|
869
784
|
async HueSet(value: CharacteristicValue): Promise<void> {
|
|
870
|
-
if (this.Hue === this.accessory.context.Hue) {
|
|
785
|
+
if (this.LightBulb.Hue === this.accessory.context.Hue) {
|
|
871
786
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Hue: ${value}`);
|
|
872
|
-
} else if (this.On) {
|
|
787
|
+
} else if (this.LightBulb.On) {
|
|
873
788
|
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Hue: ${value}`);
|
|
874
789
|
} else {
|
|
875
790
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Hue: ${value}`);
|
|
876
791
|
}
|
|
877
792
|
|
|
878
|
-
this.
|
|
793
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, 140);
|
|
879
794
|
|
|
880
|
-
this.Hue = value;
|
|
795
|
+
this.LightBulb.Hue = value;
|
|
881
796
|
this.doStripLightUpdate.next();
|
|
882
797
|
}
|
|
883
798
|
|
|
@@ -885,89 +800,61 @@ export class StripLight {
|
|
|
885
800
|
* Handle requests to set the value of the "Saturation" characteristic
|
|
886
801
|
*/
|
|
887
802
|
async SaturationSet(value: CharacteristicValue): Promise<void> {
|
|
888
|
-
if (this.Saturation === this.accessory.context.Saturation) {
|
|
803
|
+
if (this.LightBulb.Saturation === this.accessory.context.Saturation) {
|
|
889
804
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Saturation: ${value}`);
|
|
890
|
-
} else if (this.On) {
|
|
805
|
+
} else if (this.LightBulb.On) {
|
|
891
806
|
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Saturation: ${value}`);
|
|
892
807
|
} else {
|
|
893
808
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Saturation: ${value}`);
|
|
894
809
|
}
|
|
895
810
|
|
|
896
|
-
this.
|
|
811
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, 140);
|
|
897
812
|
|
|
898
|
-
this.Saturation = value;
|
|
813
|
+
this.LightBulb.Saturation = value;
|
|
899
814
|
this.doStripLightUpdate.next();
|
|
900
815
|
}
|
|
901
816
|
|
|
902
817
|
async updateHomeKitCharacteristics(): Promise<void> {
|
|
903
818
|
// On
|
|
904
|
-
if (this.On === undefined) {
|
|
905
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
|
|
819
|
+
if (this.LightBulb.On === undefined) {
|
|
820
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
|
|
906
821
|
} else {
|
|
907
|
-
this.accessory.context.On = this.On;
|
|
908
|
-
this.
|
|
909
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
|
|
822
|
+
this.accessory.context.On = this.LightBulb.On;
|
|
823
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.On, this.LightBulb.On);
|
|
824
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.LightBulb.On}`);
|
|
910
825
|
}
|
|
911
826
|
// Brightness
|
|
912
|
-
if (this.Brightness === undefined) {
|
|
913
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.Brightness}`);
|
|
827
|
+
if (this.LightBulb.Brightness === undefined) {
|
|
828
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
|
|
914
829
|
} else {
|
|
915
|
-
this.accessory.context.Brightness = this.Brightness;
|
|
916
|
-
this.
|
|
917
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Brightness: ${this.Brightness}`);
|
|
830
|
+
this.accessory.context.Brightness = this.LightBulb.Brightness;
|
|
831
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Brightness, this.LightBulb.Brightness);
|
|
832
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Brightness: ${this.LightBulb.Brightness}`);
|
|
918
833
|
}
|
|
919
834
|
// ColorTemperature
|
|
920
|
-
if (this.ColorTemperature === undefined) {
|
|
921
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${this.ColorTemperature}`);
|
|
835
|
+
if (this.LightBulb.ColorTemperature === undefined) {
|
|
836
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${this.LightBulb.ColorTemperature}`);
|
|
922
837
|
} else {
|
|
923
|
-
this.accessory.context.ColorTemperature = this.ColorTemperature;
|
|
924
|
-
this.
|
|
925
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic
|
|
838
|
+
this.accessory.context.ColorTemperature = this.LightBulb.ColorTemperature;
|
|
839
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, this.LightBulb.ColorTemperature);
|
|
840
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
|
|
841
|
+
+ ` ColorTemperature: ${this.LightBulb.ColorTemperature}`);
|
|
926
842
|
}
|
|
927
843
|
// Hue
|
|
928
|
-
if (this.Hue === undefined) {
|
|
929
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.Hue}`);
|
|
844
|
+
if (this.LightBulb.Hue === undefined) {
|
|
845
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.LightBulb.Hue}`);
|
|
930
846
|
} else {
|
|
931
|
-
this.accessory.context.Hue = this.Hue;
|
|
932
|
-
this.
|
|
933
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Hue: ${this.Hue}`);
|
|
847
|
+
this.accessory.context.Hue = this.LightBulb.Hue;
|
|
848
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Hue, this.LightBulb.Hue);
|
|
849
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Hue: ${this.LightBulb.Hue}`);
|
|
934
850
|
}
|
|
935
851
|
// Saturation
|
|
936
|
-
if (this.Saturation === undefined) {
|
|
937
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.Saturation}`);
|
|
852
|
+
if (this.LightBulb.Saturation === undefined) {
|
|
853
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.LightBulb.Saturation}`);
|
|
938
854
|
} else {
|
|
939
|
-
this.accessory.context.Saturation = this.Saturation;
|
|
940
|
-
this.
|
|
941
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Saturation: ${this.Saturation}`);
|
|
942
|
-
}
|
|
943
|
-
}
|
|
944
|
-
|
|
945
|
-
async stopScanning(switchbot: any) {
|
|
946
|
-
switchbot.stopScan();
|
|
947
|
-
if (this.BLE_IsConnected) {
|
|
948
|
-
await this.BLEparseStatus();
|
|
949
|
-
await this.updateHomeKitCharacteristics();
|
|
950
|
-
} else {
|
|
951
|
-
await this.BLERefreshConnection(switchbot);
|
|
952
|
-
}
|
|
953
|
-
}
|
|
954
|
-
|
|
955
|
-
async getCustomBLEAddress(switchbot: any) {
|
|
956
|
-
if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
|
|
957
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} customBLEaddress: ${this.device.customBLEaddress}`);
|
|
958
|
-
(async () => {
|
|
959
|
-
// Start to monitor advertisement packets
|
|
960
|
-
await switchbot.startScan({
|
|
961
|
-
model: 'r',
|
|
962
|
-
});
|
|
963
|
-
// Set an event handler
|
|
964
|
-
switchbot.onadvertisement = (ad: any) => {
|
|
965
|
-
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
|
|
966
|
-
};
|
|
967
|
-
await sleep(10000);
|
|
968
|
-
// Stop to monitor
|
|
969
|
-
switchbot.stopScan();
|
|
970
|
-
})();
|
|
855
|
+
this.accessory.context.Saturation = this.LightBulb.Saturation;
|
|
856
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Saturation, this.LightBulb.Saturation);
|
|
857
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Saturation: ${this.LightBulb.Saturation}`);
|
|
971
858
|
}
|
|
972
859
|
}
|
|
973
860
|
|
|
@@ -987,33 +874,14 @@ export class StripLight {
|
|
|
987
874
|
}
|
|
988
875
|
}
|
|
989
876
|
|
|
990
|
-
async retry({ max, fn }: { max: number; fn: { (): any; (): Promise<any> } }): Promise<null> {
|
|
991
|
-
return fn().catch(async (e: any) => {
|
|
992
|
-
if (max === 0) {
|
|
993
|
-
throw e;
|
|
994
|
-
}
|
|
995
|
-
this.infoLog(e);
|
|
996
|
-
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Retrying`);
|
|
997
|
-
await sleep(1000);
|
|
998
|
-
return this.retry({ max: max - 1, fn });
|
|
999
|
-
});
|
|
1000
|
-
}
|
|
1001
|
-
|
|
1002
|
-
maxRetry(): number {
|
|
1003
|
-
if (this.device.maxRetry) {
|
|
1004
|
-
return this.device.maxRetry;
|
|
1005
|
-
} else {
|
|
1006
|
-
return 5;
|
|
1007
|
-
}
|
|
1008
|
-
}
|
|
1009
|
-
|
|
1010
877
|
minStep(device: device & devicesConfig): number {
|
|
878
|
+
let set_minStep: number;
|
|
1011
879
|
if (device.striplight?.set_minStep) {
|
|
1012
|
-
|
|
880
|
+
set_minStep = device.striplight?.set_minStep;
|
|
1013
881
|
} else {
|
|
1014
|
-
|
|
882
|
+
set_minStep = 1;
|
|
1015
883
|
}
|
|
1016
|
-
return
|
|
884
|
+
return set_minStep;
|
|
1017
885
|
}
|
|
1018
886
|
|
|
1019
887
|
async adaptiveLighting(device: device & devicesConfig): Promise<void> {
|
|
@@ -1026,277 +894,17 @@ export class StripLight {
|
|
|
1026
894
|
}
|
|
1027
895
|
}
|
|
1028
896
|
|
|
1029
|
-
async scan(device: device & devicesConfig): Promise<void> {
|
|
1030
|
-
if (device.scanDuration) {
|
|
1031
|
-
this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
|
|
1032
|
-
if (this.BLE) {
|
|
1033
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
|
|
1034
|
-
}
|
|
1035
|
-
} else {
|
|
1036
|
-
this.scanDuration = this.accessory.context.scanDuration = 1;
|
|
1037
|
-
if (this.BLE) {
|
|
1038
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
|
|
1039
|
-
}
|
|
1040
|
-
}
|
|
1041
|
-
}
|
|
1042
|
-
|
|
1043
|
-
async statusCode(statusCode: number): Promise<void> {
|
|
1044
|
-
switch (statusCode) {
|
|
1045
|
-
case 151:
|
|
1046
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this deviceType, statusCode: ${statusCode}`);
|
|
1047
|
-
break;
|
|
1048
|
-
case 152:
|
|
1049
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found, statusCode: ${statusCode}`);
|
|
1050
|
-
break;
|
|
1051
|
-
case 160:
|
|
1052
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported, statusCode: ${statusCode}`);
|
|
1053
|
-
break;
|
|
1054
|
-
case 161:
|
|
1055
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline, statusCode: ${statusCode}`);
|
|
1056
|
-
this.offlineOff();
|
|
1057
|
-
break;
|
|
1058
|
-
case 171:
|
|
1059
|
-
this.errorLog(
|
|
1060
|
-
`${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. ` +
|
|
1061
|
-
`Hub: ${this.device.hubDeviceId}`,
|
|
1062
|
-
);
|
|
1063
|
-
this.offlineOff();
|
|
1064
|
-
break;
|
|
1065
|
-
case 190:
|
|
1066
|
-
this.errorLog(
|
|
1067
|
-
`${this.device.deviceType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
|
|
1068
|
-
` Or command format is invalid, statusCode: ${statusCode}`,
|
|
1069
|
-
);
|
|
1070
|
-
break;
|
|
1071
|
-
case 100:
|
|
1072
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent, statusCode: ${statusCode}`);
|
|
1073
|
-
break;
|
|
1074
|
-
case 200:
|
|
1075
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Request successful, statusCode: ${statusCode}`);
|
|
1076
|
-
break;
|
|
1077
|
-
case 400:
|
|
1078
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Bad Request, The client has issued an invalid request. `
|
|
1079
|
-
+ `This is commonly used to specify validation errors in a request payload, statusCode: ${statusCode}`);
|
|
1080
|
-
break;
|
|
1081
|
-
case 401:
|
|
1082
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unauthorized, Authorization for the API is required, `
|
|
1083
|
-
+ `but the request has not been authenticated, statusCode: ${statusCode}`);
|
|
1084
|
-
break;
|
|
1085
|
-
case 403:
|
|
1086
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Forbidden, The request has been authenticated but does not `
|
|
1087
|
-
+ `have appropriate permissions, or a requested resource is not found, statusCode: ${statusCode}`);
|
|
1088
|
-
break;
|
|
1089
|
-
case 404:
|
|
1090
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Found, Specifies the requested path does not exist, `
|
|
1091
|
-
+ `statusCode: ${statusCode}`);
|
|
1092
|
-
break;
|
|
1093
|
-
case 406:
|
|
1094
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Acceptable, The client has requested a MIME type via `
|
|
1095
|
-
+ `the Accept header for a value not supported by the server, statusCode: ${statusCode}`);
|
|
1096
|
-
break;
|
|
1097
|
-
case 415:
|
|
1098
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unsupported Media Type, The client has defined a contentType `
|
|
1099
|
-
+ `header that is not supported by the server, statusCode: ${statusCode}`);
|
|
1100
|
-
break;
|
|
1101
|
-
case 422:
|
|
1102
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unprocessable Entity, The client has made a valid request, `
|
|
1103
|
-
+ `but the server cannot process it. This is often used for APIs for which certain limits have been exceeded, statusCode: ${statusCode}`);
|
|
1104
|
-
break;
|
|
1105
|
-
case 429:
|
|
1106
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Too Many Requests, The client has exceeded the number of `
|
|
1107
|
-
+ `requests allowed for a given time window, statusCode: ${statusCode}`);
|
|
1108
|
-
break;
|
|
1109
|
-
case 500:
|
|
1110
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Internal Server Error, An unexpected error on the SmartThings `
|
|
1111
|
-
+ `servers has occurred. These errors should be rare, statusCode: ${statusCode}`);
|
|
1112
|
-
break;
|
|
1113
|
-
default:
|
|
1114
|
-
this.infoLog(
|
|
1115
|
-
`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: ` +
|
|
1116
|
-
`${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`,
|
|
1117
|
-
);
|
|
1118
|
-
}
|
|
1119
|
-
}
|
|
1120
|
-
|
|
1121
897
|
async offlineOff(): Promise<void> {
|
|
1122
898
|
if (this.device.offline) {
|
|
1123
|
-
|
|
1124
|
-
await this.updateHomeKitCharacteristics();
|
|
1125
|
-
}
|
|
1126
|
-
}
|
|
1127
|
-
|
|
1128
|
-
apiError(e: any): void {
|
|
1129
|
-
this.lightBulbService.updateCharacteristic(this.hap.Characteristic.On, e);
|
|
1130
|
-
this.lightBulbService.updateCharacteristic(this.hap.Characteristic.Hue, e);
|
|
1131
|
-
this.lightBulbService.updateCharacteristic(this.hap.Characteristic.Brightness, e);
|
|
1132
|
-
this.lightBulbService.updateCharacteristic(this.hap.Characteristic.Saturation, e);
|
|
1133
|
-
}
|
|
1134
|
-
|
|
1135
|
-
async deviceContext() {
|
|
1136
|
-
if (this.On === undefined) {
|
|
1137
|
-
this.On = false;
|
|
1138
|
-
} else {
|
|
1139
|
-
this.On = this.accessory.context.On;
|
|
1140
|
-
}
|
|
1141
|
-
if (this.Hue === undefined) {
|
|
1142
|
-
this.Hue = 0;
|
|
1143
|
-
} else {
|
|
1144
|
-
this.Hue = this.accessory.context.Hue;
|
|
1145
|
-
}
|
|
1146
|
-
if (this.Brightness === undefined) {
|
|
1147
|
-
this.Brightness = 0;
|
|
1148
|
-
} else {
|
|
1149
|
-
this.Brightness = this.accessory.context.Brightness;
|
|
1150
|
-
}
|
|
1151
|
-
if (this.Saturation === undefined) {
|
|
1152
|
-
this.Saturation = 0;
|
|
1153
|
-
} else {
|
|
1154
|
-
this.Saturation = this.accessory.context.Saturation;
|
|
1155
|
-
}
|
|
1156
|
-
if (this.ColorTemperature === undefined) {
|
|
1157
|
-
this.ColorTemperature = 140;
|
|
1158
|
-
} else {
|
|
1159
|
-
this.ColorTemperature = this.accessory.context.ColorTemperature;
|
|
1160
|
-
}
|
|
1161
|
-
this.minKelvin = 2000;
|
|
1162
|
-
this.maxKelvin = 9000;
|
|
1163
|
-
if (this.FirmwareRevision === undefined) {
|
|
1164
|
-
this.FirmwareRevision = this.platform.version;
|
|
1165
|
-
this.accessory.context.FirmwareRevision = this.FirmwareRevision;
|
|
1166
|
-
}
|
|
1167
|
-
}
|
|
1168
|
-
|
|
1169
|
-
async refreshRate(device: device & devicesConfig): Promise<void> {
|
|
1170
|
-
if (device.refreshRate) {
|
|
1171
|
-
this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
|
|
1172
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
|
|
1173
|
-
} else if (this.platform.config.options!.refreshRate) {
|
|
1174
|
-
this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options!.refreshRate;
|
|
1175
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
|
|
1176
|
-
}
|
|
1177
|
-
}
|
|
1178
|
-
|
|
1179
|
-
async deviceRetry(device: device & devicesConfig): Promise<void> {
|
|
1180
|
-
if (!device.maxRetries) {
|
|
1181
|
-
this.maxRetries = 5; // Maximum number of retries
|
|
1182
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Max Retries Not Set, Using: ${this.maxRetries}`);
|
|
1183
|
-
} else {
|
|
1184
|
-
this.maxRetries = device.maxRetries;
|
|
1185
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Max Retries: ${this.maxRetries}`);
|
|
1186
|
-
}
|
|
1187
|
-
if (!device.delayBetweenRetries) {
|
|
1188
|
-
this.delayBetweenRetries = 3000; // Delay between retries in milliseconds
|
|
1189
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Delay Between Retries Not Set, Using: ${this.delayBetweenRetries}`);
|
|
1190
|
-
} else {
|
|
1191
|
-
this.delayBetweenRetries = device.delayBetweenRetries * 1000;
|
|
1192
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Delay Between Retries: ${this.delayBetweenRetries}`);
|
|
1193
|
-
}
|
|
1194
|
-
}
|
|
1195
|
-
|
|
1196
|
-
async deviceConfig(device: device & devicesConfig): Promise<void> {
|
|
1197
|
-
let config = {};
|
|
1198
|
-
if (device.striplight) {
|
|
1199
|
-
config = device.striplight;
|
|
1200
|
-
}
|
|
1201
|
-
if (device.connectionType !== undefined) {
|
|
1202
|
-
config['connectionType'] = device.connectionType;
|
|
1203
|
-
}
|
|
1204
|
-
if (device.external !== undefined) {
|
|
1205
|
-
config['external'] = device.external;
|
|
1206
|
-
}
|
|
1207
|
-
if (device.logging !== undefined) {
|
|
1208
|
-
config['logging'] = device.logging;
|
|
1209
|
-
}
|
|
1210
|
-
if (device.refreshRate !== undefined) {
|
|
1211
|
-
config['refreshRate'] = device.refreshRate;
|
|
1212
|
-
}
|
|
1213
|
-
if (device.scanDuration !== undefined) {
|
|
1214
|
-
config['scanDuration'] = device.scanDuration;
|
|
1215
|
-
}
|
|
1216
|
-
if (device.offline !== undefined) {
|
|
1217
|
-
config['offline'] = device.offline;
|
|
1218
|
-
}
|
|
1219
|
-
if (device.maxRetry !== undefined) {
|
|
1220
|
-
config['maxRetry'] = device.maxRetry;
|
|
1221
|
-
}
|
|
1222
|
-
if (device.webhook !== undefined) {
|
|
1223
|
-
config['webhook'] = device.webhook;
|
|
1224
|
-
}
|
|
1225
|
-
if (Object.entries(config).length !== 0) {
|
|
1226
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
|
|
1227
|
-
}
|
|
1228
|
-
}
|
|
1229
|
-
|
|
1230
|
-
async deviceLogs(device: device & devicesConfig): Promise<void> {
|
|
1231
|
-
if (this.platform.debugMode) {
|
|
1232
|
-
this.deviceLogging = this.accessory.context.logging = 'debugMode';
|
|
1233
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
|
|
1234
|
-
} else if (device.logging) {
|
|
1235
|
-
this.deviceLogging = this.accessory.context.logging = device.logging;
|
|
1236
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
|
|
1237
|
-
} else if (this.platform.config.options?.logging) {
|
|
1238
|
-
this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
|
|
1239
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
|
|
1240
|
-
} else {
|
|
1241
|
-
this.deviceLogging = this.accessory.context.logging = 'standard';
|
|
1242
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
|
|
1243
|
-
}
|
|
1244
|
-
}
|
|
1245
|
-
|
|
1246
|
-
/**
|
|
1247
|
-
* Logging for Device
|
|
1248
|
-
*/
|
|
1249
|
-
successLog(...log: any[]): void {
|
|
1250
|
-
if (this.enablingDeviceLogging()) {
|
|
1251
|
-
this.platform.log.success(String(...log));
|
|
1252
|
-
}
|
|
1253
|
-
}
|
|
1254
|
-
|
|
1255
|
-
infoLog(...log: any[]): void {
|
|
1256
|
-
if (this.enablingDeviceLogging()) {
|
|
1257
|
-
this.platform.log.info(String(...log));
|
|
1258
|
-
}
|
|
1259
|
-
}
|
|
1260
|
-
|
|
1261
|
-
warnLog(...log: any[]): void {
|
|
1262
|
-
if (this.enablingDeviceLogging()) {
|
|
1263
|
-
this.platform.log.warn(String(...log));
|
|
1264
|
-
}
|
|
1265
|
-
}
|
|
1266
|
-
|
|
1267
|
-
debugWarnLog(...log: any[]): void {
|
|
1268
|
-
if (this.enablingDeviceLogging()) {
|
|
1269
|
-
if (this.deviceLogging?.includes('debug')) {
|
|
1270
|
-
this.platform.log.warn('[DEBUG]', String(...log));
|
|
1271
|
-
}
|
|
1272
|
-
}
|
|
1273
|
-
}
|
|
1274
|
-
|
|
1275
|
-
errorLog(...log: any[]): void {
|
|
1276
|
-
if (this.enablingDeviceLogging()) {
|
|
1277
|
-
this.platform.log.error(String(...log));
|
|
1278
|
-
}
|
|
1279
|
-
}
|
|
1280
|
-
|
|
1281
|
-
debugErrorLog(...log: any[]): void {
|
|
1282
|
-
if (this.enablingDeviceLogging()) {
|
|
1283
|
-
if (this.deviceLogging?.includes('debug')) {
|
|
1284
|
-
this.platform.log.error('[DEBUG]', String(...log));
|
|
1285
|
-
}
|
|
1286
|
-
}
|
|
1287
|
-
}
|
|
1288
|
-
|
|
1289
|
-
debugLog(...log: any[]): void {
|
|
1290
|
-
if (this.enablingDeviceLogging()) {
|
|
1291
|
-
if (this.deviceLogging === 'debug') {
|
|
1292
|
-
this.platform.log.info('[DEBUG]', String(...log));
|
|
1293
|
-
} else {
|
|
1294
|
-
this.platform.log.debug(String(...log));
|
|
1295
|
-
}
|
|
899
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.On, false);
|
|
1296
900
|
}
|
|
1297
901
|
}
|
|
1298
902
|
|
|
1299
|
-
|
|
1300
|
-
|
|
903
|
+
async apiError(e: any): Promise<void> {
|
|
904
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.On, e);
|
|
905
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Hue, e);
|
|
906
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Brightness, e);
|
|
907
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Saturation, e);
|
|
908
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, e);
|
|
1301
909
|
}
|
|
1302
910
|
}
|