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