@switchbot/homebridge-switchbot 3.5.0-beta.9 → 3.5.1-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/settings.json +4 -1
- package/CHANGELOG.md +4 -1
- package/config.schema.json +164 -22
- package/dist/device/blindtilt.d.ts +21 -96
- package/dist/device/blindtilt.d.ts.map +1 -1
- package/dist/device/blindtilt.js +457 -866
- package/dist/device/blindtilt.js.map +1 -1
- package/dist/device/bot.d.ts +27 -72
- package/dist/device/bot.d.ts.map +1 -1
- package/dist/device/bot.js +851 -913
- package/dist/device/bot.js.map +1 -1
- package/dist/device/ceilinglight.d.ts +15 -69
- package/dist/device/ceilinglight.d.ts.map +1 -1
- package/dist/device/ceilinglight.js +316 -742
- package/dist/device/ceilinglight.js.map +1 -1
- package/dist/device/colorbulb.d.ts +15 -76
- package/dist/device/colorbulb.d.ts.map +1 -1
- package/dist/device/colorbulb.js +380 -833
- package/dist/device/colorbulb.js.map +1 -1
- package/dist/device/contact.d.ts +14 -66
- package/dist/device/contact.d.ts.map +1 -1
- package/dist/device/contact.js +267 -602
- package/dist/device/contact.js.map +1 -1
- package/dist/device/curtain.d.ts +18 -89
- package/dist/device/curtain.d.ts.map +1 -1
- package/dist/device/curtain.js +469 -897
- package/dist/device/curtain.js.map +1 -1
- package/dist/device/device.d.ts +76 -0
- package/dist/device/device.d.ts.map +1 -0
- package/dist/device/device.js +846 -0
- package/dist/device/device.js.map +1 -0
- package/dist/device/fan.d.ts +63 -0
- package/dist/device/fan.d.ts.map +1 -0
- package/dist/device/fan.js +645 -0
- package/dist/device/fan.js.map +1 -0
- package/dist/device/hub.d.ts +19 -58
- package/dist/device/hub.d.ts.map +1 -1
- package/dist/device/hub.js +304 -640
- package/dist/device/hub.js.map +1 -1
- package/dist/device/humidifier.d.ts +11 -66
- package/dist/device/humidifier.d.ts.map +1 -1
- package/dist/device/humidifier.js +300 -710
- package/dist/device/humidifier.js.map +1 -1
- package/dist/device/iosensor.d.ts +12 -64
- package/dist/device/iosensor.d.ts.map +1 -1
- package/dist/device/iosensor.js +225 -643
- package/dist/device/iosensor.js.map +1 -1
- package/dist/device/lightstrip.d.ts +12 -70
- package/dist/device/lightstrip.d.ts.map +1 -1
- package/dist/device/lightstrip.js +371 -742
- package/dist/device/lightstrip.js.map +1 -1
- package/dist/device/lock.d.ts +17 -63
- package/dist/device/lock.d.ts.map +1 -1
- package/dist/device/lock.js +309 -642
- package/dist/device/lock.js.map +1 -1
- package/dist/device/meter.d.ts +15 -64
- package/dist/device/meter.d.ts.map +1 -1
- package/dist/device/meter.js +223 -638
- package/dist/device/meter.js.map +1 -1
- package/dist/device/meterplus.d.ts +15 -65
- package/dist/device/meterplus.d.ts.map +1 -1
- package/dist/device/meterplus.js +216 -644
- package/dist/device/meterplus.js.map +1 -1
- package/dist/device/motion.d.ts +12 -69
- package/dist/device/motion.d.ts.map +1 -1
- package/dist/device/motion.js +209 -588
- package/dist/device/motion.js.map +1 -1
- package/dist/device/plug.d.ts +10 -57
- package/dist/device/plug.d.ts.map +1 -1
- package/dist/device/plug.js +141 -532
- package/dist/device/plug.js.map +1 -1
- package/dist/device/robotvacuumcleaner.d.ts +13 -63
- package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
- package/dist/device/robotvacuumcleaner.js +252 -609
- package/dist/device/robotvacuumcleaner.js.map +1 -1
- package/dist/device/waterdetector.d.ts +11 -63
- package/dist/device/waterdetector.d.ts.map +1 -1
- package/dist/device/waterdetector.js +179 -521
- package/dist/device/waterdetector.js.map +1 -1
- package/dist/homebridge-ui/public/index.html +22 -16
- package/dist/homebridge-ui/server.js +1 -1
- package/dist/homebridge-ui/server.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/irdevice/airconditioner.d.ts +10 -50
- package/dist/irdevice/airconditioner.d.ts.map +1 -1
- package/dist/irdevice/airconditioner.js +337 -482
- package/dist/irdevice/airconditioner.js.map +1 -1
- package/dist/irdevice/airpurifier.d.ts +8 -39
- package/dist/irdevice/airpurifier.d.ts.map +1 -1
- package/dist/irdevice/airpurifier.js +108 -333
- package/dist/irdevice/airpurifier.js.map +1 -1
- package/dist/irdevice/camera.d.ts +7 -36
- package/dist/irdevice/camera.d.ts.map +1 -1
- package/dist/irdevice/camera.js +47 -299
- package/dist/irdevice/camera.js.map +1 -1
- package/dist/irdevice/fan.d.ts +7 -43
- package/dist/irdevice/fan.d.ts.map +1 -1
- package/dist/irdevice/fan.js +95 -364
- package/dist/irdevice/fan.js.map +1 -1
- package/dist/irdevice/irdevice.d.ts +46 -0
- package/dist/irdevice/irdevice.d.ts.map +1 -0
- package/dist/irdevice/irdevice.js +393 -0
- package/dist/irdevice/irdevice.js.map +1 -0
- package/dist/irdevice/light.d.ts +12 -45
- package/dist/irdevice/light.d.ts.map +1 -1
- package/dist/irdevice/light.js +121 -376
- package/dist/irdevice/light.js.map +1 -1
- package/dist/irdevice/other.d.ts +19 -48
- package/dist/irdevice/other.d.ts.map +1 -1
- package/dist/irdevice/other.js +441 -584
- package/dist/irdevice/other.js.map +1 -1
- package/dist/irdevice/tv.d.ts +8 -40
- package/dist/irdevice/tv.d.ts.map +1 -1
- package/dist/irdevice/tv.js +94 -353
- package/dist/irdevice/tv.js.map +1 -1
- package/dist/irdevice/vacuumcleaner.d.ts +7 -36
- package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
- package/dist/irdevice/vacuumcleaner.js +48 -299
- package/dist/irdevice/vacuumcleaner.js.map +1 -1
- package/dist/irdevice/waterheater.d.ts +7 -36
- package/dist/irdevice/waterheater.d.ts.map +1 -1
- package/dist/irdevice/waterheater.js +50 -305
- package/dist/irdevice/waterheater.js.map +1 -1
- package/dist/platform.d.ts +22 -8
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +342 -191
- package/dist/platform.js.map +1 -1
- package/dist/settings.d.ts +66 -18
- package/dist/settings.d.ts.map +1 -1
- package/dist/settings.js +16 -552
- package/dist/settings.js.map +1 -1
- package/dist/utils.d.ts +95 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +657 -1
- package/dist/utils.js.map +1 -1
- package/eslint.config.js +96 -0
- package/package.json +20 -16
- package/src/custom.d.ts +2 -1
- package/src/device/blindtilt.ts +488 -937
- package/src/device/bot.ts +907 -1016
- package/src/device/ceilinglight.ts +330 -798
- package/src/device/colorbulb.ts +394 -904
- package/src/device/contact.ts +298 -637
- package/src/device/curtain.ts +496 -954
- package/src/device/device.ts +872 -0
- package/src/device/fan.ts +692 -0
- package/src/device/hub.ts +338 -671
- package/src/device/humidifier.ts +330 -777
- package/src/device/iosensor.ts +258 -671
- package/src/device/lightstrip.ts +394 -809
- package/src/device/lock.ts +345 -700
- package/src/device/meter.ts +256 -666
- package/src/device/meterplus.ts +248 -672
- package/src/device/motion.ts +234 -622
- package/src/device/plug.ts +152 -572
- package/src/device/robotvacuumcleaner.ts +273 -653
- package/src/device/waterdetector.ts +198 -549
- package/src/homebridge-ui/public/index.html +22 -16
- package/src/homebridge-ui/server.ts +1 -1
- package/src/index.ts +4 -3
- package/src/irdevice/airconditioner.ts +368 -540
- package/src/irdevice/airpurifier.ts +134 -369
- package/src/irdevice/camera.ts +60 -324
- package/src/irdevice/fan.ts +111 -392
- package/src/irdevice/irdevice.ts +406 -0
- package/src/irdevice/light.ts +159 -425
- package/src/irdevice/other.ts +509 -661
- package/src/irdevice/tv.ts +123 -388
- package/src/irdevice/vacuumcleaner.ts +61 -324
- package/src/irdevice/waterheater.ts +63 -333
- package/src/platform.ts +395 -259
- package/src/settings.ts +120 -610
- package/src/utils.ts +661 -1
- package/src/device/waterdetector.json +0 -11
package/src/device/lightstrip.ts
CHANGED
|
@@ -1,141 +1,108 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
} from '
|
|
9
|
-
|
|
1
|
+
/* Copyright(C) 2021-2024, donavanbecker (https://github.com/donavanbecker). All rights reserved.
|
|
2
|
+
*
|
|
3
|
+
* lightstrip.ts: @switchbot/homebridge-switchbot.
|
|
4
|
+
*/
|
|
5
|
+
import { deviceBase } from './device.js';
|
|
6
|
+
import { hs2rgb, rgb2hs, m2hs } from '../utils.js';
|
|
7
|
+
import { interval, skipWhile, Subject } from 'rxjs';
|
|
8
|
+
import { debounceTime, take, tap } from 'rxjs/operators';
|
|
9
|
+
|
|
10
|
+
import type { SwitchBotPlatform } from '../platform.js';
|
|
11
|
+
import type { device, devicesConfig, serviceData, deviceStatus } from '../settings.js';
|
|
12
|
+
import type { Service, PlatformAccessory, CharacteristicValue, ControllerConstructor, Controller, ControllerServiceMap } from 'homebridge';
|
|
10
13
|
|
|
11
14
|
/**
|
|
12
15
|
* Platform Accessory
|
|
13
16
|
* An instance of this class is created for each accessory your platform registers
|
|
14
17
|
* Each accessory may expose multiple services of different service types.
|
|
15
18
|
*/
|
|
16
|
-
export class StripLight {
|
|
17
|
-
public readonly api: API;
|
|
18
|
-
public readonly log: Logging;
|
|
19
|
-
public readonly config!: SwitchBotPlatformConfig;
|
|
20
|
-
protected readonly hap: HAP;
|
|
19
|
+
export class StripLight extends deviceBase {
|
|
21
20
|
// Services
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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;
|
|
21
|
+
private LightBulb: {
|
|
22
|
+
Name: CharacteristicValue;
|
|
23
|
+
Service: Service;
|
|
24
|
+
On: CharacteristicValue;
|
|
25
|
+
Hue: CharacteristicValue;
|
|
26
|
+
Saturation: CharacteristicValue;
|
|
27
|
+
Brightness: CharacteristicValue;
|
|
28
|
+
ColorTemperature?: CharacteristicValue;
|
|
29
|
+
};
|
|
51
30
|
|
|
52
31
|
// Adaptive Lighting
|
|
53
32
|
AdaptiveLightingController?: ControllerConstructor | Controller<ControllerServiceMap>;
|
|
54
|
-
|
|
55
|
-
maxKelvin!: number;
|
|
56
|
-
|
|
57
|
-
// Others
|
|
58
|
-
cacheKelvin!: number;
|
|
59
|
-
change!: string;
|
|
33
|
+
adaptiveLightingShift?: number;
|
|
60
34
|
|
|
61
35
|
// Updates
|
|
62
36
|
stripLightUpdateInProgress!: boolean;
|
|
63
37
|
doStripLightUpdate!: Subject<void>;
|
|
64
38
|
|
|
65
|
-
// Connection
|
|
66
|
-
private readonly OpenAPI: boolean;
|
|
67
|
-
private readonly BLE: boolean;
|
|
68
|
-
|
|
69
39
|
constructor(
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
40
|
+
readonly platform: SwitchBotPlatform,
|
|
41
|
+
accessory: PlatformAccessory,
|
|
42
|
+
device: device & devicesConfig,
|
|
73
43
|
) {
|
|
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
|
-
|
|
44
|
+
super(platform, accessory, device);
|
|
45
|
+
this.adaptiveLighting(device);
|
|
88
46
|
// this is subject we use to track when we need to POST changes to the SwitchBot API
|
|
89
47
|
this.doStripLightUpdate = new Subject();
|
|
90
48
|
this.stripLightUpdateInProgress = false;
|
|
91
49
|
|
|
92
|
-
//
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
.
|
|
98
|
-
.
|
|
99
|
-
.
|
|
100
|
-
.
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
(this.lightBulbService = accessory.getService(this.hap.Service.Lightbulb)
|
|
107
|
-
|| accessory.addService(this.hap.Service.Lightbulb)), lightBulbService;
|
|
108
|
-
|
|
50
|
+
// Initialize the LightBulb Service
|
|
51
|
+
accessory.context.LightBulb = accessory.context.LightBulb ?? {};
|
|
52
|
+
this.LightBulb = {
|
|
53
|
+
Name: accessory.context.LightBulb.Name ?? accessory.displayName,
|
|
54
|
+
Service: accessory.getService(this.hap.Service.Lightbulb) ?? accessory.addService(this.hap.Service.Lightbulb) as Service,
|
|
55
|
+
On: accessory.context.On ?? false,
|
|
56
|
+
Hue: accessory.context.Hue ?? 0,
|
|
57
|
+
Saturation: accessory.context.Saturation ?? 0,
|
|
58
|
+
Brightness: accessory.context.Brightness ?? 0,
|
|
59
|
+
ColorTemperature: accessory.context.ColorTemperature ?? 140,
|
|
60
|
+
};
|
|
61
|
+
accessory.context.LightBulb = this.LightBulb as object;
|
|
62
|
+
|
|
63
|
+
// Adaptive Lighting
|
|
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
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
70
|
+
if (this.adaptiveLightingShift !== -1) {
|
|
71
|
+
this.AdaptiveLightingController = new platform.api.hap.AdaptiveLightingController(this.LightBulb.Service, {
|
|
72
|
+
customTemperatureAdjustment: this.adaptiveLightingShift,
|
|
73
|
+
});
|
|
74
|
+
this.accessory.configureController(this.AdaptiveLightingController);
|
|
75
|
+
this.accessory.context.adaptiveLighting = true;
|
|
76
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLighting: ${this.accessory.context.adaptiveLighting},`
|
|
77
|
+
+ ` adaptiveLightingShift: ${this.adaptiveLightingShift}`);
|
|
119
78
|
}
|
|
120
|
-
|
|
121
|
-
|
|
79
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
|
|
80
|
+
|
|
81
|
+
// Initialize LightBulb Characteristics
|
|
82
|
+
this.LightBulb.Service
|
|
83
|
+
.setCharacteristic(this.hap.Characteristic.Name, this.LightBulb.Name)
|
|
84
|
+
.getCharacteristic(this.hap.Characteristic.On)
|
|
85
|
+
.onGet(() => {
|
|
86
|
+
return this.LightBulb.On;
|
|
87
|
+
})
|
|
88
|
+
.onSet(this.OnSet.bind(this));
|
|
122
89
|
|
|
123
|
-
//
|
|
124
|
-
this.
|
|
90
|
+
// Initialize LightBulb Brightness Characteristic
|
|
91
|
+
this.LightBulb.Service
|
|
125
92
|
.getCharacteristic(this.hap.Characteristic.Brightness)
|
|
126
93
|
.setProps({
|
|
127
|
-
minStep:
|
|
94
|
+
minStep: device.striplight?.set_minStep ?? 1,
|
|
128
95
|
minValue: 0,
|
|
129
96
|
maxValue: 100,
|
|
130
97
|
validValueRanges: [0, 100],
|
|
131
98
|
})
|
|
132
99
|
.onGet(() => {
|
|
133
|
-
return this.Brightness;
|
|
100
|
+
return this.LightBulb.Brightness;
|
|
134
101
|
})
|
|
135
102
|
.onSet(this.BrightnessSet.bind(this));
|
|
136
103
|
|
|
137
|
-
//
|
|
138
|
-
this.
|
|
104
|
+
// Initialize LightBulb ColorTemperature Characteristic
|
|
105
|
+
this.LightBulb.Service
|
|
139
106
|
.getCharacteristic(this.hap.Characteristic.ColorTemperature)
|
|
140
107
|
.setProps({
|
|
141
108
|
minValue: 140,
|
|
@@ -143,12 +110,12 @@ export class StripLight {
|
|
|
143
110
|
validValueRanges: [140, 500],
|
|
144
111
|
})
|
|
145
112
|
.onGet(() => {
|
|
146
|
-
return this.ColorTemperature!;
|
|
113
|
+
return this.LightBulb.ColorTemperature!;
|
|
147
114
|
})
|
|
148
115
|
.onSet(this.ColorTemperatureSet.bind(this));
|
|
149
116
|
|
|
150
|
-
//
|
|
151
|
-
this.
|
|
117
|
+
// Initialize LightBulb Hue Characteristic
|
|
118
|
+
this.LightBulb.Service
|
|
152
119
|
.getCharacteristic(this.hap.Characteristic.Hue)
|
|
153
120
|
.setProps({
|
|
154
121
|
minValue: 0,
|
|
@@ -156,12 +123,12 @@ export class StripLight {
|
|
|
156
123
|
validValueRanges: [0, 360],
|
|
157
124
|
})
|
|
158
125
|
.onGet(() => {
|
|
159
|
-
return this.Hue;
|
|
126
|
+
return this.LightBulb.Hue;
|
|
160
127
|
})
|
|
161
128
|
.onSet(this.HueSet.bind(this));
|
|
162
129
|
|
|
163
|
-
//
|
|
164
|
-
this.
|
|
130
|
+
// Initialize LightBulb Saturation Characteristic
|
|
131
|
+
this.LightBulb.Service
|
|
165
132
|
.getCharacteristic(this.hap.Characteristic.Saturation)
|
|
166
133
|
.setProps({
|
|
167
134
|
minValue: 0,
|
|
@@ -169,22 +136,12 @@ export class StripLight {
|
|
|
169
136
|
validValueRanges: [0, 100],
|
|
170
137
|
})
|
|
171
138
|
.onGet(() => {
|
|
172
|
-
return this.Saturation;
|
|
139
|
+
return this.LightBulb.Saturation;
|
|
173
140
|
})
|
|
174
141
|
.onSet(this.SaturationSet.bind(this));
|
|
175
142
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
this.AdaptiveLightingController = new platform.api.hap.AdaptiveLightingController(this.lightBulbService, {
|
|
179
|
-
customTemperatureAdjustment: this.adaptiveLightingShift,
|
|
180
|
-
});
|
|
181
|
-
this.accessory.configureController(this.AdaptiveLightingController);
|
|
182
|
-
this.accessory.context.adaptiveLighting = true;
|
|
183
|
-
this.debugLog(
|
|
184
|
-
`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLighting: ${this.accessory.context.adaptiveLighting},` +
|
|
185
|
-
` adaptiveLightingShift: ${this.adaptiveLightingShift}`,
|
|
186
|
-
);
|
|
187
|
-
}
|
|
143
|
+
// Retrieve initial values and updateHomekit
|
|
144
|
+
this.refreshStatus();
|
|
188
145
|
|
|
189
146
|
// Update Homekit
|
|
190
147
|
this.updateHomeKitCharacteristics();
|
|
@@ -197,46 +154,7 @@ export class StripLight {
|
|
|
197
154
|
});
|
|
198
155
|
|
|
199
156
|
//regisiter webhook event handler
|
|
200
|
-
|
|
201
|
-
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} is listening webhook.`);
|
|
202
|
-
this.platform.webhookEventHandler[this.device.deviceId] = async (context) => {
|
|
203
|
-
try {
|
|
204
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
|
|
205
|
-
const { powerState, brightness, color, colorTemperature } = context;
|
|
206
|
-
const { On, Brightness, Hue, Saturation, ColorTemperature } = this;
|
|
207
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
|
|
208
|
-
'(powerState, brightness, color, colorTemperature) = ' +
|
|
209
|
-
`Webhook:(${powerState}, ${brightness}, ${color}, ${colorTemperature}), ` +
|
|
210
|
-
`current:(${On}, ${Brightness}, ${Hue}, ${Saturation}, ${ColorTemperature})`);
|
|
211
|
-
this.On = powerState === 'ON' ? true : false;
|
|
212
|
-
this.Brightness = brightness;
|
|
213
|
-
|
|
214
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} color: ${JSON.stringify(color)}`);
|
|
215
|
-
const [red, green, blue] = color!.split(':');
|
|
216
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} red: ${JSON.stringify(red)}`);
|
|
217
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} green: ${JSON.stringify(green)}`);
|
|
218
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} blue: ${JSON.stringify(blue)}`);
|
|
219
|
-
|
|
220
|
-
const [hue, saturation] = rgb2hs(Number(red), Number(green), Number(blue));
|
|
221
|
-
this.debugLog(
|
|
222
|
-
`${this.device.deviceType}: ${this.accessory.displayName}` + ` hs: ${JSON.stringify(rgb2hs(Number(red), Number(green), Number(blue)))}`,
|
|
223
|
-
);
|
|
224
|
-
|
|
225
|
-
// Hue
|
|
226
|
-
this.Hue = hue;
|
|
227
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.Hue}`);
|
|
228
|
-
|
|
229
|
-
// Saturation
|
|
230
|
-
this.Saturation = saturation;
|
|
231
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.Saturation}`);
|
|
232
|
-
|
|
233
|
-
this.updateHomeKitCharacteristics();
|
|
234
|
-
} catch (e: any) {
|
|
235
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
236
|
-
+ `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
|
|
237
|
-
}
|
|
238
|
-
};
|
|
239
|
-
}
|
|
157
|
+
this.registerWebhook(accessory, device);
|
|
240
158
|
|
|
241
159
|
// Watch for Bulb change events
|
|
242
160
|
// We put in a debounce of 1000ms so we don't make duplicate calls
|
|
@@ -245,93 +163,114 @@ export class StripLight {
|
|
|
245
163
|
tap(() => {
|
|
246
164
|
this.stripLightUpdateInProgress = true;
|
|
247
165
|
}),
|
|
248
|
-
debounceTime(this.
|
|
166
|
+
debounceTime(this.devicePushRate * 1000),
|
|
249
167
|
)
|
|
250
168
|
.subscribe(async () => {
|
|
251
169
|
try {
|
|
252
170
|
await this.pushChanges();
|
|
253
171
|
} catch (e: any) {
|
|
254
172
|
this.apiError(e);
|
|
255
|
-
this.errorLog(
|
|
256
|
-
|
|
257
|
-
` Error Message: ${JSON.stringify(e.message)}`,
|
|
258
|
-
);
|
|
173
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with ${this.device.connectionType} Connection,`
|
|
174
|
+
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
259
175
|
}
|
|
260
176
|
this.stripLightUpdateInProgress = false;
|
|
261
177
|
});
|
|
262
178
|
}
|
|
263
179
|
|
|
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> {
|
|
180
|
+
async BLEparseStatus(serviceData: serviceData): Promise<void> {
|
|
283
181
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
|
|
284
182
|
// State
|
|
285
|
-
switch (
|
|
183
|
+
switch (serviceData.state) {
|
|
286
184
|
case 'on':
|
|
287
|
-
this.On = true;
|
|
185
|
+
this.LightBulb.On = true;
|
|
288
186
|
break;
|
|
289
187
|
default:
|
|
290
|
-
this.On = false;
|
|
188
|
+
this.LightBulb.On = false;
|
|
189
|
+
}
|
|
190
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
|
|
191
|
+
|
|
192
|
+
// Brightness
|
|
193
|
+
this.LightBulb.Brightness = Number(serviceData.brightness);
|
|
194
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
|
|
195
|
+
|
|
196
|
+
// Color, Hue & Brightness
|
|
197
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} red: ${serviceData.red}`);
|
|
198
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} green: ${serviceData.green}`);
|
|
199
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} blue: ${serviceData.blue}`);
|
|
200
|
+
|
|
201
|
+
const [hue, saturation] = rgb2hs(Number(serviceData.red), Number(serviceData.green), Number(serviceData.blue));
|
|
202
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
|
|
203
|
+
+ ` hs: ${JSON.stringify(rgb2hs(Number(serviceData.red), Number(serviceData.green), Number(serviceData.blue)))}`);
|
|
204
|
+
|
|
205
|
+
// Hue
|
|
206
|
+
this.LightBulb.Hue = hue;
|
|
207
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.LightBulb.Hue}`);
|
|
208
|
+
|
|
209
|
+
// Saturation
|
|
210
|
+
this.LightBulb.Saturation = saturation;
|
|
211
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.LightBulb.Saturation}`);
|
|
212
|
+
|
|
213
|
+
// ColorTemperature
|
|
214
|
+
if (serviceData.color_temperature) {
|
|
215
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${serviceData.color_temperature}`);
|
|
216
|
+
this.LightBulb.ColorTemperature = serviceData.color_temperature!;
|
|
217
|
+
|
|
218
|
+
this.LightBulb.ColorTemperature = Math.max(Math.min(this.LightBulb.ColorTemperature, 500), 140);
|
|
219
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${this.LightBulb.ColorTemperature}`);
|
|
291
220
|
}
|
|
292
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
|
|
293
221
|
}
|
|
294
222
|
|
|
295
|
-
async openAPIparseStatus(): Promise<void> {
|
|
223
|
+
async openAPIparseStatus(deviceStatus: deviceStatus): Promise<void> {
|
|
224
|
+
|
|
296
225
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
|
|
297
|
-
switch (
|
|
226
|
+
switch (deviceStatus.body.power) {
|
|
298
227
|
case 'on':
|
|
299
|
-
this.On = true;
|
|
228
|
+
this.LightBulb.On = true;
|
|
300
229
|
break;
|
|
301
230
|
default:
|
|
302
|
-
this.On = false;
|
|
231
|
+
this.LightBulb.On = false;
|
|
303
232
|
}
|
|
304
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
|
|
233
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
|
|
305
234
|
|
|
306
235
|
// Brightness
|
|
307
|
-
this.Brightness = Number(
|
|
308
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.Brightness}`);
|
|
236
|
+
this.LightBulb.Brightness = Number(deviceStatus.body.brightness);
|
|
237
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
|
|
309
238
|
|
|
310
239
|
// Color, Hue & Brightness
|
|
311
|
-
if (
|
|
312
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} color: ${JSON.stringify(
|
|
313
|
-
const [red, green, blue] =
|
|
240
|
+
if (deviceStatus.body.color) {
|
|
241
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} color: ${JSON.stringify(deviceStatus.body.color)}`);
|
|
242
|
+
const [red, green, blue] = deviceStatus.body.color!.split(':');
|
|
314
243
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} red: ${JSON.stringify(red)}`);
|
|
315
244
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} green: ${JSON.stringify(green)}`);
|
|
316
245
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} blue: ${JSON.stringify(blue)}`);
|
|
317
246
|
|
|
318
247
|
const [hue, saturation] = rgb2hs(Number(red), Number(green), Number(blue));
|
|
319
|
-
this.debugLog(
|
|
320
|
-
|
|
321
|
-
);
|
|
248
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
|
|
249
|
+
+ ` hs: ${JSON.stringify(rgb2hs(Number(red), Number(green), Number(blue)))}`);
|
|
322
250
|
|
|
323
251
|
// Hue
|
|
324
|
-
this.Hue = hue;
|
|
325
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.Hue}`);
|
|
252
|
+
this.LightBulb.Hue = hue;
|
|
253
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.LightBulb.Hue}`);
|
|
326
254
|
|
|
327
255
|
// Saturation
|
|
328
|
-
this.Saturation = saturation;
|
|
329
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.Saturation}`);
|
|
256
|
+
this.LightBulb.Saturation = saturation;
|
|
257
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.LightBulb.Saturation}`);
|
|
330
258
|
}
|
|
331
259
|
|
|
332
|
-
//
|
|
333
|
-
|
|
334
|
-
this.
|
|
260
|
+
// Firmware Version
|
|
261
|
+
const version = deviceStatus.body.version?.toString();
|
|
262
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Firmware Version: ${version?.replace(/^V|-.*$/g, '')}`);
|
|
263
|
+
if (deviceStatus.body.version) {
|
|
264
|
+
const deviceVersion = version?.replace(/^V|-.*$/g, '') ?? '0.0.0';
|
|
265
|
+
this.accessory
|
|
266
|
+
.getService(this.hap.Service.AccessoryInformation)!
|
|
267
|
+
.setCharacteristic(this.hap.Characteristic.HardwareRevision, deviceVersion)
|
|
268
|
+
.setCharacteristic(this.hap.Characteristic.FirmwareRevision, deviceVersion)
|
|
269
|
+
.getCharacteristic(this.hap.Characteristic.FirmwareRevision)
|
|
270
|
+
.updateValue(deviceVersion);
|
|
271
|
+
this.accessory.context.deviceVersion = deviceVersion;
|
|
272
|
+
this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceVersion: ${this.accessory.context.deviceVersion}`);
|
|
273
|
+
}
|
|
335
274
|
}
|
|
336
275
|
|
|
337
276
|
/**
|
|
@@ -346,127 +285,122 @@ export class StripLight {
|
|
|
346
285
|
await this.openAPIRefreshStatus();
|
|
347
286
|
} else {
|
|
348
287
|
await this.offlineOff();
|
|
349
|
-
this.debugWarnLog(
|
|
350
|
-
|
|
351
|
-
` ${this.device.connectionType}, refreshStatus will not happen.`,
|
|
352
|
-
);
|
|
288
|
+
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
|
|
289
|
+
+ ` ${this.device.connectionType}, refreshStatus will not happen.`);
|
|
353
290
|
}
|
|
354
291
|
}
|
|
355
292
|
|
|
356
293
|
async BLERefreshStatus(): Promise<void> {
|
|
357
294
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
|
|
358
|
-
const switchbot = await this.
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
.
|
|
362
|
-
|
|
363
|
-
.toLowerCase();
|
|
364
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
365
|
-
this.getCustomBLEAddress(switchbot);
|
|
295
|
+
const switchbot = await this.switchbotBLE();
|
|
296
|
+
|
|
297
|
+
if (switchbot === undefined) {
|
|
298
|
+
await this.BLERefreshConnection(switchbot);
|
|
299
|
+
} else {
|
|
366
300
|
// Start to monitor advertisement packets
|
|
367
|
-
|
|
301
|
+
(async () => {
|
|
368
302
|
// Start to monitor advertisement packets
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
switchbot.onadvertisement = (ad: any) => {
|
|
375
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
|
|
376
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.serviceData.model}`);
|
|
377
|
-
if (this.device.bleMac === ad.address && ad.serviceData.model === 'r') {
|
|
378
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
303
|
+
const serviceData: serviceData = await this.monitorAdvertisementPackets(switchbot);
|
|
304
|
+
// Update HomeKit
|
|
305
|
+
if (serviceData.model !== '' && serviceData.modelName !== '') {
|
|
306
|
+
await this.BLEparseStatus(serviceData);
|
|
307
|
+
await this.updateHomeKitCharacteristics();
|
|
379
308
|
} else {
|
|
380
|
-
this.
|
|
381
|
-
}
|
|
382
|
-
};
|
|
383
|
-
// Wait 1 seconds
|
|
384
|
-
await switchbot.wait(this.scanDuration * 1000);
|
|
385
|
-
// Stop to monitor
|
|
386
|
-
await switchbot.stopScan();
|
|
387
|
-
// Update HomeKit
|
|
388
|
-
await this.BLEparseStatus();
|
|
389
|
-
await this.updateHomeKitCharacteristics();
|
|
390
|
-
})();
|
|
391
|
-
/*if (switchbot !== false) {
|
|
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
|
-
);
|
|
309
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed to get serviceData, serviceData: ${serviceData}`);
|
|
434
310
|
await this.BLERefreshConnection(switchbot);
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
}*/
|
|
311
|
+
}
|
|
312
|
+
})();
|
|
313
|
+
}
|
|
439
314
|
}
|
|
440
315
|
|
|
441
316
|
async openAPIRefreshStatus(): Promise<void> {
|
|
442
317
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
|
|
443
318
|
try {
|
|
444
|
-
const { body, statusCode } = await
|
|
445
|
-
headers: this.platform.generateHeaders(),
|
|
446
|
-
});
|
|
447
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
|
|
319
|
+
const { body, statusCode } = await this.deviceRefreshStatus();
|
|
448
320
|
const deviceStatus: any = await body.json();
|
|
449
|
-
this.
|
|
450
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
|
|
321
|
+
await this.refreshStatusCodes(statusCode, deviceStatus);;
|
|
451
322
|
if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
|
|
452
|
-
this.
|
|
453
|
-
|
|
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();
|
|
459
|
-
this.updateHomeKitCharacteristics();
|
|
323
|
+
await this.successfulRefreshStatus(statusCode, deviceStatus);
|
|
324
|
+
await this.openAPIparseStatus(deviceStatus);
|
|
325
|
+
await this.updateHomeKitCharacteristics();
|
|
460
326
|
} else {
|
|
461
|
-
this.
|
|
462
|
-
this.statusCode(deviceStatus.statusCode);
|
|
327
|
+
await this.statusCodes(statusCode, deviceStatus);
|
|
463
328
|
}
|
|
464
329
|
} catch (e: any) {
|
|
465
330
|
this.apiError(e);
|
|
466
|
-
this.errorLog(
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
331
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
|
|
332
|
+
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
async registerWebhook(accessory: PlatformAccessory, device: device & devicesConfig) {
|
|
337
|
+
if (device.webhook) {
|
|
338
|
+
this.debugLog(`${device.deviceType}: ${accessory.displayName} is listening webhook.`);
|
|
339
|
+
this.platform.webhookEventHandler[device.deviceId] = async (context) => {
|
|
340
|
+
try {
|
|
341
|
+
this.debugLog(`${device.deviceType}: ${accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
|
|
342
|
+
const { powerState, brightness, color, colorTemperature } = context;
|
|
343
|
+
const { On, Brightness, Hue, Saturation, ColorTemperature } = this.LightBulb;
|
|
344
|
+
this.debugLog(`${device.deviceType}: ${accessory.displayName} (powerState, brightness, color, colorTemperature) = Webhook: (${powerState},`
|
|
345
|
+
+ ` ${brightness}, ${color}, ${colorTemperature}), current:(${On}, ${Brightness}, ${Hue}, ${Saturation}, ${ColorTemperature})`);
|
|
346
|
+
|
|
347
|
+
// On
|
|
348
|
+
this.LightBulb.On = powerState === 'ON' ? true : false;
|
|
349
|
+
if (accessory.context.Brightness !== this.LightBulb.On) {
|
|
350
|
+
this.infoLog(`${device.deviceType}: ${accessory.displayName} On: ${this.LightBulb.On}`);
|
|
351
|
+
} else {
|
|
352
|
+
this.debugLog(`${device.deviceType}: ${accessory.displayName} On: ${this.LightBulb.On}`);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
// Brightness
|
|
356
|
+
this.LightBulb.Brightness = brightness;
|
|
357
|
+
if (accessory.context.Brightness !== this.LightBulb.Brightness) {
|
|
358
|
+
this.infoLog(`${device.deviceType}: ${accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
|
|
359
|
+
} else {
|
|
360
|
+
this.debugLog(`${device.deviceType}: ${accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
this.debugLog(`${device.deviceType}: ${accessory.displayName} color: ${JSON.stringify(color)}`);
|
|
364
|
+
const [red, green, blue] = color!.split(':');
|
|
365
|
+
this.debugLog(`${device.deviceType}: ${accessory.displayName} red: ${JSON.stringify(red)}`);
|
|
366
|
+
this.debugLog(`${device.deviceType}: ${accessory.displayName} green: ${JSON.stringify(green)}`);
|
|
367
|
+
this.debugLog(`${device.deviceType}: ${accessory.displayName} blue: ${JSON.stringify(blue)}`);
|
|
368
|
+
|
|
369
|
+
const [hue, saturation] = rgb2hs(Number(red), Number(green), Number(blue));
|
|
370
|
+
this.debugLog(
|
|
371
|
+
`${device.deviceType}: ${accessory.displayName}`
|
|
372
|
+
+ ` hs: ${JSON.stringify(rgb2hs(Number(red), Number(green), Number(blue)))}`);
|
|
373
|
+
|
|
374
|
+
// Hue
|
|
375
|
+
this.LightBulb.Hue = hue;
|
|
376
|
+
if (accessory.context.Hue !== this.LightBulb.Hue) {
|
|
377
|
+
this.infoLog(`${device.deviceType}: ${accessory.displayName} Hue: ${this.LightBulb.Hue}`);
|
|
378
|
+
} else {
|
|
379
|
+
this.debugLog(`${device.deviceType}: ${accessory.displayName} Hue: ${this.LightBulb.Hue}`);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// Saturation
|
|
383
|
+
this.LightBulb.Saturation = saturation;
|
|
384
|
+
if (accessory.context.Saturation !== this.LightBulb.Saturation) {
|
|
385
|
+
this.infoLog(`${device.deviceType}: ${accessory.displayName} Saturation: ${this.LightBulb.Saturation}`);
|
|
386
|
+
} else {
|
|
387
|
+
this.debugLog(`${device.deviceType}: ${accessory.displayName} Saturation: ${this.LightBulb.Saturation}`);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// ColorTemperature
|
|
391
|
+
this.LightBulb.ColorTemperature = colorTemperature;
|
|
392
|
+
if (accessory.context.ColorTemperature !== this.LightBulb.ColorTemperature) {
|
|
393
|
+
this.infoLog(`${device.deviceType}: ${accessory.displayName} ColorTemperature: ${this.LightBulb.ColorTemperature}`);
|
|
394
|
+
} else {
|
|
395
|
+
this.debugLog(`${device.deviceType}: ${accessory.displayName} ColorTemperature: ${this.LightBulb.ColorTemperature}`);
|
|
396
|
+
}
|
|
397
|
+
this.updateHomeKitCharacteristics();
|
|
398
|
+
} catch (e: any) {
|
|
399
|
+
this.errorLog(`${device.deviceType}: ${accessory.displayName} failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
|
|
400
|
+
}
|
|
401
|
+
};
|
|
402
|
+
} else {
|
|
403
|
+
this.debugLog(`${device.deviceType}: ${accessory.displayName} is not listening webhook.`);
|
|
470
404
|
}
|
|
471
405
|
}
|
|
472
406
|
|
|
@@ -483,15 +417,14 @@ export class StripLight {
|
|
|
483
417
|
async pushChanges(): Promise<void> {
|
|
484
418
|
if (!this.device.enableCloudService && this.OpenAPI) {
|
|
485
419
|
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} pushChanges enableCloudService: ${this.device.enableCloudService}`);
|
|
486
|
-
|
|
487
|
-
|
|
420
|
+
} else if (this.BLE) {
|
|
421
|
+
await this.BLEpushChanges();
|
|
488
422
|
} else if (this.OpenAPI && this.platform.config.credentials?.token) {
|
|
489
423
|
await this.openAPIpushChanges();
|
|
490
424
|
} else {
|
|
491
425
|
await this.offlineOff();
|
|
492
|
-
this.debugWarnLog(
|
|
493
|
-
|
|
494
|
-
);
|
|
426
|
+
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
|
|
427
|
+
+ ` ${this.device.connectionType}, pushChanges will not happen.`);
|
|
495
428
|
}
|
|
496
429
|
// Refresh the status from the API
|
|
497
430
|
interval(15000)
|
|
@@ -504,26 +437,20 @@ export class StripLight {
|
|
|
504
437
|
|
|
505
438
|
async BLEpushChanges(): Promise<void> {
|
|
506
439
|
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
|
|
440
|
+
if (this.LightBulb.On !== this.accessory.context.On) {
|
|
441
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`
|
|
442
|
+
+ ` On: ${this.LightBulb.On} OnCached: ${this.accessory.context.On}`);
|
|
509
443
|
const switchbot = await this.platform.connectBLE();
|
|
510
444
|
// Convert to BLE Address
|
|
511
|
-
|
|
512
|
-
.deviceId!.match(/.{1,2}/g)!
|
|
513
|
-
.join(':')
|
|
514
|
-
.toLowerCase();
|
|
515
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
445
|
+
await this.convertBLEAddress();
|
|
516
446
|
switchbot
|
|
517
|
-
.discover({
|
|
518
|
-
model: 'r',
|
|
519
|
-
id: this.device.bleMac,
|
|
520
|
-
})
|
|
447
|
+
.discover({ model: this.device.bleModel, id: this.device.bleMac })
|
|
521
448
|
.then(async (device_list: any) => {
|
|
522
|
-
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
|
|
523
|
-
return await this.
|
|
524
|
-
max: this.
|
|
449
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
|
|
450
|
+
return await this.retryBLE({
|
|
451
|
+
max: await this.maxRetryBLE(),
|
|
525
452
|
fn: async () => {
|
|
526
|
-
if (this.On) {
|
|
453
|
+
if (this.LightBulb.On) {
|
|
527
454
|
return await device_list[0].turnOn({ id: this.device.bleMac });
|
|
528
455
|
} else {
|
|
529
456
|
return await device_list[0].turnOff({ id: this.device.bleMac });
|
|
@@ -533,121 +460,105 @@ export class StripLight {
|
|
|
533
460
|
})
|
|
534
461
|
.then(() => {
|
|
535
462
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
|
|
536
|
-
this.
|
|
463
|
+
this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
464
|
+
+ `On: ${this.LightBulb.On} sent over BLE, sent successfully`);
|
|
465
|
+
this.LightBulb.On = false;
|
|
537
466
|
})
|
|
538
467
|
.catch(async (e: any) => {
|
|
539
468
|
this.apiError(e);
|
|
540
|
-
this.errorLog(
|
|
541
|
-
|
|
542
|
-
` Connection, Error Message: ${JSON.stringify(e.message)}`,
|
|
543
|
-
);
|
|
469
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
|
|
470
|
+
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
544
471
|
await this.BLEPushConnection();
|
|
545
472
|
});
|
|
546
473
|
// Push Brightness Update
|
|
547
|
-
if (this.On) {
|
|
474
|
+
if (this.LightBulb.On) {
|
|
548
475
|
await this.BLEpushBrightnessChanges();
|
|
549
476
|
}
|
|
550
477
|
// Push Hue & Saturation Update
|
|
551
|
-
if (this.On) {
|
|
478
|
+
if (this.LightBulb.On) {
|
|
552
479
|
await this.BLEpushRGBChanges();
|
|
553
480
|
}
|
|
481
|
+
if (this.LightBulb.ColorTemperature !== this.accessory.context.ColorTemperature) {
|
|
482
|
+
const kelvin = Math.round(1000000 / Number(this.LightBulb.ColorTemperature));
|
|
483
|
+
this.accessory.context.kelvin = kelvin;
|
|
484
|
+
} else {
|
|
485
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushColorTemperatureChanges.`
|
|
486
|
+
+ `ColorTemperature: ${this.LightBulb.ColorTemperature}, ColorTemperatureCached: ${this.accessory.context.ColorTemperature}`);
|
|
487
|
+
}
|
|
554
488
|
} else {
|
|
555
|
-
this.debugLog(
|
|
556
|
-
|
|
557
|
-
);
|
|
489
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges,`
|
|
490
|
+
+ ` On: ${this.LightBulb.On}, OnCached: ${this.accessory.context.On}`);
|
|
558
491
|
}
|
|
559
492
|
}
|
|
560
493
|
|
|
561
494
|
async BLEpushBrightnessChanges(): Promise<void> {
|
|
562
495
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushBrightnessChanges`);
|
|
563
|
-
if (this.Brightness !== this.accessory.context.Brightness) {
|
|
496
|
+
if (this.LightBulb.Brightness !== this.accessory.context.Brightness) {
|
|
564
497
|
const switchbot = await this.platform.connectBLE();
|
|
565
498
|
// Convert to BLE Address
|
|
566
|
-
|
|
567
|
-
.deviceId!.match(/.{1,2}/g)!
|
|
568
|
-
.join(':')
|
|
569
|
-
.toLowerCase();
|
|
570
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
499
|
+
await this.convertBLEAddress();
|
|
571
500
|
switchbot
|
|
572
|
-
.discover({
|
|
573
|
-
model: 'u',
|
|
574
|
-
id: this.device.bleMac,
|
|
575
|
-
})
|
|
501
|
+
.discover({ model: this.device.bleModel, id: this.device.bleMac })
|
|
576
502
|
.then(async (device_list: any) => {
|
|
577
|
-
this.infoLog(`${this.accessory.displayName} Target Brightness: ${this.Brightness}`);
|
|
578
|
-
return await device_list[0].setBrightness(this.Brightness);
|
|
503
|
+
this.infoLog(`${this.accessory.displayName} Target Brightness: ${this.LightBulb.Brightness}`);
|
|
504
|
+
return await device_list[0].setBrightness(this.LightBulb.Brightness);
|
|
579
505
|
})
|
|
580
506
|
.then(() => {
|
|
581
507
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
|
|
582
|
-
this.On = false;
|
|
508
|
+
this.LightBulb.On = false;
|
|
583
509
|
})
|
|
584
510
|
.catch(async (e: any) => {
|
|
585
511
|
this.apiError(e);
|
|
586
|
-
this.errorLog(
|
|
587
|
-
|
|
588
|
-
` Connection, Error Message: ${JSON.stringify(e.message)}`,
|
|
589
|
-
);
|
|
512
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushBrightnessChanges with ${this.device.connectionType}`
|
|
513
|
+
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
590
514
|
await this.BLEPushConnection();
|
|
591
515
|
});
|
|
592
516
|
} else {
|
|
593
|
-
this.debugLog(
|
|
594
|
-
|
|
595
|
-
`
|
|
596
|
-
`BrightnessCached: ${this.accessory.context.Brightness}`,
|
|
597
|
-
);
|
|
517
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushBrightnessChanges.`
|
|
518
|
+
+ `Brightness: ${this.LightBulb.Brightness}, `
|
|
519
|
+
+ `BrightnessCached: ${this.accessory.context.Brightness}`);
|
|
598
520
|
}
|
|
599
521
|
}
|
|
600
522
|
|
|
601
523
|
async BLEpushRGBChanges(): Promise<void> {
|
|
602
524
|
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)}`);
|
|
525
|
+
if (this.LightBulb.Hue !== this.accessory.context.Hue || this.LightBulb.Saturation !== this.accessory.context.Saturation) {
|
|
526
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${JSON.stringify(this.LightBulb.Hue)}`);
|
|
527
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${JSON.stringify(this.LightBulb.Saturation)}`);
|
|
606
528
|
|
|
607
|
-
const [red, green, blue] = hs2rgb(Number(this.Hue), Number(this.Saturation));
|
|
529
|
+
const [red, green, blue] = hs2rgb(Number(this.LightBulb.Hue), Number(this.LightBulb.Saturation));
|
|
608
530
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} rgb: ${JSON.stringify([red, green, blue])}`);
|
|
609
531
|
|
|
610
532
|
const switchbot = await this.platform.connectBLE();
|
|
611
533
|
// Convert to BLE Address
|
|
612
|
-
|
|
613
|
-
.deviceId!.match(/.{1,2}/g)!
|
|
614
|
-
.join(':')
|
|
615
|
-
.toLowerCase();
|
|
616
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
534
|
+
await this.convertBLEAddress();
|
|
617
535
|
switchbot
|
|
618
|
-
.discover({
|
|
619
|
-
model: 'u',
|
|
620
|
-
id: this.device.bleMac,
|
|
621
|
-
})
|
|
536
|
+
.discover({ model: this.device.bleModel, id: this.device.bleMac })
|
|
622
537
|
.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);
|
|
538
|
+
this.infoLog(`${this.accessory.displayName} Target RGB: ${(this.LightBulb.Brightness, red, green, blue)}`);
|
|
539
|
+
return await device_list[0].setRGB(this.LightBulb.Brightness, red, green, blue);
|
|
625
540
|
})
|
|
626
541
|
.then(() => {
|
|
627
542
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
|
|
628
|
-
this.On = false;
|
|
543
|
+
this.LightBulb.On = false;
|
|
629
544
|
})
|
|
630
545
|
.catch(async (e: any) => {
|
|
631
546
|
this.apiError(e);
|
|
632
|
-
this.errorLog(
|
|
633
|
-
|
|
634
|
-
` Connection, Error Message: ${JSON.stringify(e.message)}`,
|
|
635
|
-
);
|
|
547
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushRGBChanges with ${this.device.connectionType}`
|
|
548
|
+
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
636
549
|
await this.BLEPushConnection();
|
|
637
550
|
});
|
|
638
551
|
} else {
|
|
639
|
-
this.debugLog(
|
|
640
|
-
`${this.
|
|
641
|
-
`HueCached: ${this.accessory.context.Hue}, Saturation: ${this.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`,
|
|
642
|
-
);
|
|
552
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushRGBChanges. Hue: ${this.LightBulb.Hue}, HueCached: `
|
|
553
|
+
+ `${this.accessory.context.Hue}, Saturation: ${this.LightBulb.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`);
|
|
643
554
|
}
|
|
644
555
|
}
|
|
645
556
|
|
|
646
557
|
async openAPIpushChanges() {
|
|
647
558
|
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) {
|
|
559
|
+
if (this.LightBulb.On !== this.accessory.context.On) {
|
|
560
|
+
const command = this.LightBulb.On ? 'turnOn' : 'turnOff';
|
|
561
|
+
/*if (this.LightBulb.On) {
|
|
651
562
|
command = 'turnOn';
|
|
652
563
|
} else {
|
|
653
564
|
command = 'turnOff';
|
|
@@ -659,53 +570,42 @@ export class StripLight {
|
|
|
659
570
|
});
|
|
660
571
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
|
|
661
572
|
try {
|
|
662
|
-
const { body, statusCode } = await
|
|
663
|
-
body: bodyChange,
|
|
664
|
-
method: 'POST',
|
|
665
|
-
headers: this.platform.generateHeaders(),
|
|
666
|
-
});
|
|
667
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
|
|
573
|
+
const { body, statusCode } = await this.pushChangeRequest(bodyChange);
|
|
668
574
|
const deviceStatus: any = await body.json();
|
|
669
|
-
this.
|
|
670
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
|
|
671
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
|
|
575
|
+
await this.pushStatusCodes(statusCode, deviceStatus);
|
|
672
576
|
if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
|
|
673
|
-
this.
|
|
674
|
-
|
|
577
|
+
await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
|
|
578
|
+
await this.updateHomeKitCharacteristics();
|
|
675
579
|
} else {
|
|
676
|
-
this.statusCode(statusCode);
|
|
677
|
-
this.statusCode(deviceStatus.statusCode);
|
|
580
|
+
await this.statusCode(statusCode);
|
|
581
|
+
await this.statusCode(deviceStatus.statusCode);
|
|
678
582
|
}
|
|
679
583
|
} catch (e: any) {
|
|
680
584
|
this.apiError(e);
|
|
681
|
-
this.errorLog(
|
|
682
|
-
|
|
683
|
-
` Connection, Error Message: ${JSON.stringify(e.message)}`,
|
|
684
|
-
);
|
|
585
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
|
|
586
|
+
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
685
587
|
}
|
|
686
588
|
} else {
|
|
687
|
-
this.debugLog(
|
|
688
|
-
|
|
689
|
-
`
|
|
690
|
-
`OnCached: ${this.accessory.context.On}`,
|
|
691
|
-
);
|
|
589
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.`
|
|
590
|
+
+ `On: ${this.LightBulb.On}, `
|
|
591
|
+
+ `OnCached: ${this.accessory.context.On}`);
|
|
692
592
|
}
|
|
693
593
|
// Push Hue & Saturation Update
|
|
694
|
-
if (this.On) {
|
|
594
|
+
if (this.LightBulb.On) {
|
|
695
595
|
await this.pushHueSaturationChanges();
|
|
696
596
|
}
|
|
697
597
|
// Push Brightness Update
|
|
698
|
-
if (this.On) {
|
|
598
|
+
if (this.LightBulb.On) {
|
|
699
599
|
await this.pushBrightnessChanges();
|
|
700
600
|
}
|
|
701
601
|
}
|
|
702
602
|
|
|
703
603
|
async pushHueSaturationChanges(): Promise<void> {
|
|
704
604
|
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));
|
|
605
|
+
if (this.LightBulb.Hue !== this.accessory.context.Hue || this.LightBulb.Saturation !== this.accessory.context.Saturation) {
|
|
606
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${JSON.stringify(this.LightBulb.Hue)}`);
|
|
607
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${JSON.stringify(this.LightBulb.Saturation)}`);
|
|
608
|
+
const [red, green, blue] = hs2rgb(Number(this.LightBulb.Hue), Number(this.LightBulb.Saturation));
|
|
709
609
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} rgb: ${JSON.stringify([red, green, blue])}`);
|
|
710
610
|
// Make Push On request to the API
|
|
711
611
|
const bodyChange = JSON.stringify({
|
|
@@ -715,78 +615,56 @@ export class StripLight {
|
|
|
715
615
|
});
|
|
716
616
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
|
|
717
617
|
try {
|
|
718
|
-
const { body, statusCode } = await
|
|
719
|
-
body: bodyChange,
|
|
720
|
-
method: 'POST',
|
|
721
|
-
headers: this.platform.generateHeaders(),
|
|
722
|
-
});
|
|
723
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
|
|
618
|
+
const { body, statusCode } = await this.pushChangeRequest(bodyChange);
|
|
724
619
|
const deviceStatus: any = await body.json();
|
|
725
|
-
this.
|
|
726
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
|
|
727
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
|
|
620
|
+
await this.pushStatusCodes(statusCode, deviceStatus);
|
|
728
621
|
if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
|
|
729
|
-
this.
|
|
730
|
-
|
|
622
|
+
await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
|
|
623
|
+
await this.updateHomeKitCharacteristics();
|
|
731
624
|
} else {
|
|
732
|
-
this.statusCode(statusCode);
|
|
733
|
-
this.statusCode(deviceStatus.statusCode);
|
|
625
|
+
await this.statusCode(statusCode);
|
|
626
|
+
await this.statusCode(deviceStatus.statusCode);
|
|
734
627
|
}
|
|
735
628
|
} catch (e: any) {
|
|
736
629
|
this.apiError(e);
|
|
737
|
-
this.errorLog(
|
|
738
|
-
|
|
739
|
-
` Connection, Error Message: ${JSON.stringify(e.message)}`,
|
|
740
|
-
);
|
|
630
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushHueSaturationChanges with ${this.device.connectionType}`
|
|
631
|
+
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
741
632
|
}
|
|
742
633
|
} else {
|
|
743
|
-
this.debugLog(
|
|
744
|
-
`${this.
|
|
745
|
-
`HueCached: ${this.accessory.context.Hue}, Saturation: ${this.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`,
|
|
746
|
-
);
|
|
634
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushHueSaturationChanges. Hue: ${this.LightBulb.Hue}, HueCached: `
|
|
635
|
+
+ `${this.accessory.context.Hue}, Saturation: ${this.LightBulb.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`);
|
|
747
636
|
}
|
|
748
637
|
}
|
|
749
638
|
|
|
750
639
|
async pushBrightnessChanges(): Promise<void> {
|
|
751
640
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushBrightnessChanges`);
|
|
752
|
-
if (this.Brightness !== this.accessory.context.Brightness) {
|
|
641
|
+
if (this.LightBulb.Brightness !== this.accessory.context.Brightness) {
|
|
753
642
|
const bodyChange = JSON.stringify({
|
|
754
643
|
command: 'setBrightness',
|
|
755
|
-
parameter: `${this.Brightness}`,
|
|
644
|
+
parameter: `${this.LightBulb.Brightness}`,
|
|
756
645
|
commandType: 'command',
|
|
757
646
|
});
|
|
758
647
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
|
|
759
648
|
try {
|
|
760
|
-
const { body, statusCode } = await
|
|
761
|
-
body: bodyChange,
|
|
762
|
-
method: 'POST',
|
|
763
|
-
headers: this.platform.generateHeaders(),
|
|
764
|
-
});
|
|
765
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
|
|
649
|
+
const { body, statusCode } = await this.pushChangeRequest(bodyChange);
|
|
766
650
|
const deviceStatus: any = await body.json();
|
|
767
|
-
this.
|
|
768
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
|
|
769
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
|
|
651
|
+
await this.pushStatusCodes(statusCode, deviceStatus);
|
|
770
652
|
if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
|
|
771
|
-
this.
|
|
772
|
-
|
|
653
|
+
await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
|
|
654
|
+
await this.updateHomeKitCharacteristics();
|
|
773
655
|
} else {
|
|
774
|
-
this.statusCode(statusCode);
|
|
775
|
-
this.statusCode(deviceStatus.statusCode);
|
|
656
|
+
await this.statusCode(statusCode);
|
|
657
|
+
await this.statusCode(deviceStatus.statusCode);
|
|
776
658
|
}
|
|
777
659
|
} catch (e: any) {
|
|
778
660
|
this.apiError(e);
|
|
779
|
-
this.errorLog(
|
|
780
|
-
|
|
781
|
-
` Connection, Error Message: ${JSON.stringify(e.message)}`,
|
|
782
|
-
);
|
|
661
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushBrightnessChanges with ${this.device.connectionType}`
|
|
662
|
+
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
783
663
|
}
|
|
784
664
|
} else {
|
|
785
|
-
this.debugLog(
|
|
786
|
-
|
|
787
|
-
`
|
|
788
|
-
`BrightnessCached: ${this.accessory.context.Brightness}`,
|
|
789
|
-
);
|
|
665
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushBrightnessChanges,`
|
|
666
|
+
+ `Brightness: ${this.LightBulb.Brightness}, `
|
|
667
|
+
+ `BrightnessCached: ${this.accessory.context.Brightness}`);
|
|
790
668
|
}
|
|
791
669
|
}
|
|
792
670
|
|
|
@@ -794,13 +672,13 @@ export class StripLight {
|
|
|
794
672
|
* Handle requests to set the value of the "On" characteristic
|
|
795
673
|
*/
|
|
796
674
|
async OnSet(value: CharacteristicValue): Promise<void> {
|
|
797
|
-
if (this.On === this.accessory.context.On) {
|
|
675
|
+
if (this.LightBulb.On === this.accessory.context.On) {
|
|
798
676
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set On: ${value}`);
|
|
799
677
|
} else {
|
|
800
678
|
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set On: ${value}`);
|
|
801
679
|
}
|
|
802
680
|
|
|
803
|
-
this.On = value;
|
|
681
|
+
this.LightBulb.On = value;
|
|
804
682
|
this.doStripLightUpdate.next();
|
|
805
683
|
}
|
|
806
684
|
|
|
@@ -808,15 +686,15 @@ export class StripLight {
|
|
|
808
686
|
* Handle requests to set the value of the "Brightness" characteristic
|
|
809
687
|
*/
|
|
810
688
|
async BrightnessSet(value: CharacteristicValue): Promise<void> {
|
|
811
|
-
if (this.Brightness === this.accessory.context.Brightness) {
|
|
689
|
+
if (this.LightBulb.Brightness === this.accessory.context.Brightness) {
|
|
812
690
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Brightness: ${value}`);
|
|
813
|
-
} else if (this.On) {
|
|
691
|
+
} else if (this.LightBulb.On) {
|
|
814
692
|
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Brightness: ${value}`);
|
|
815
693
|
} else {
|
|
816
694
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Brightness: ${value}`);
|
|
817
695
|
}
|
|
818
696
|
|
|
819
|
-
this.Brightness = value;
|
|
697
|
+
this.LightBulb.Brightness = value;
|
|
820
698
|
this.doStripLightUpdate.next();
|
|
821
699
|
}
|
|
822
700
|
|
|
@@ -824,30 +702,31 @@ export class StripLight {
|
|
|
824
702
|
* Handle requests to set the value of the "ColorTemperature" characteristic
|
|
825
703
|
*/
|
|
826
704
|
async ColorTemperatureSet(value: CharacteristicValue): Promise<void> {
|
|
827
|
-
if (this.ColorTemperature === this.accessory.context.ColorTemperature) {
|
|
705
|
+
if (this.LightBulb.ColorTemperature === this.accessory.context.ColorTemperature) {
|
|
828
706
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set ColorTemperature: ${value}`);
|
|
829
|
-
} else if (this.On) {
|
|
707
|
+
} else if (this.LightBulb.On) {
|
|
830
708
|
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set ColorTemperature: ${value}`);
|
|
831
709
|
} else {
|
|
832
710
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set ColorTemperature: ${value}`);
|
|
833
711
|
}
|
|
834
712
|
|
|
713
|
+
const minKelvin = 2000;
|
|
714
|
+
const maxKelvin = 9000;
|
|
835
715
|
// Convert mired to kelvin to nearest 100 (SwitchBot seems to need this)
|
|
836
716
|
const kelvin = Math.round(1000000 / Number(value) / 100) * 100;
|
|
837
|
-
|
|
838
717
|
// Check and increase/decrease kelvin to range of device
|
|
839
|
-
const k = Math.min(Math.max(kelvin,
|
|
718
|
+
const k = Math.min(Math.max(kelvin, minKelvin), maxKelvin);
|
|
840
719
|
|
|
841
|
-
if (!this.accessory.context.On || this.
|
|
720
|
+
if (!this.accessory.context.On || this.accessory.context.maxKelvin === k) {
|
|
842
721
|
return;
|
|
843
722
|
}
|
|
844
723
|
|
|
845
724
|
// Updating the hue/sat to the corresponding values mimics native adaptive lighting
|
|
846
725
|
const hs = m2hs(value);
|
|
847
|
-
this.
|
|
848
|
-
this.
|
|
726
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Hue, hs[0]);
|
|
727
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Saturation, hs[1]);
|
|
849
728
|
|
|
850
|
-
this.ColorTemperature = value;
|
|
729
|
+
this.LightBulb.ColorTemperature = value;
|
|
851
730
|
this.doStripLightUpdate.next();
|
|
852
731
|
}
|
|
853
732
|
|
|
@@ -855,17 +734,17 @@ export class StripLight {
|
|
|
855
734
|
* Handle requests to set the value of the "Hue" characteristic
|
|
856
735
|
*/
|
|
857
736
|
async HueSet(value: CharacteristicValue): Promise<void> {
|
|
858
|
-
if (this.Hue === this.accessory.context.Hue) {
|
|
737
|
+
if (this.LightBulb.Hue === this.accessory.context.Hue) {
|
|
859
738
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Hue: ${value}`);
|
|
860
|
-
} else if (this.On) {
|
|
739
|
+
} else if (this.LightBulb.On) {
|
|
861
740
|
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Hue: ${value}`);
|
|
862
741
|
} else {
|
|
863
742
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Hue: ${value}`);
|
|
864
743
|
}
|
|
865
744
|
|
|
866
|
-
this.
|
|
745
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, 140);
|
|
867
746
|
|
|
868
|
-
this.Hue = value;
|
|
747
|
+
this.LightBulb.Hue = value;
|
|
869
748
|
this.doStripLightUpdate.next();
|
|
870
749
|
}
|
|
871
750
|
|
|
@@ -873,89 +752,61 @@ export class StripLight {
|
|
|
873
752
|
* Handle requests to set the value of the "Saturation" characteristic
|
|
874
753
|
*/
|
|
875
754
|
async SaturationSet(value: CharacteristicValue): Promise<void> {
|
|
876
|
-
if (this.Saturation === this.accessory.context.Saturation) {
|
|
755
|
+
if (this.LightBulb.Saturation === this.accessory.context.Saturation) {
|
|
877
756
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Saturation: ${value}`);
|
|
878
|
-
} else if (this.On) {
|
|
757
|
+
} else if (this.LightBulb.On) {
|
|
879
758
|
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Saturation: ${value}`);
|
|
880
759
|
} else {
|
|
881
760
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Saturation: ${value}`);
|
|
882
761
|
}
|
|
883
762
|
|
|
884
|
-
this.
|
|
763
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, 140);
|
|
885
764
|
|
|
886
|
-
this.Saturation = value;
|
|
765
|
+
this.LightBulb.Saturation = value;
|
|
887
766
|
this.doStripLightUpdate.next();
|
|
888
767
|
}
|
|
889
768
|
|
|
890
769
|
async updateHomeKitCharacteristics(): Promise<void> {
|
|
891
770
|
// On
|
|
892
|
-
if (this.On === undefined) {
|
|
893
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
|
|
771
|
+
if (this.LightBulb.On === undefined) {
|
|
772
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
|
|
894
773
|
} else {
|
|
895
|
-
this.accessory.context.On = this.On;
|
|
896
|
-
this.
|
|
897
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
|
|
774
|
+
this.accessory.context.On = this.LightBulb.On;
|
|
775
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.On, this.LightBulb.On);
|
|
776
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.LightBulb.On}`);
|
|
898
777
|
}
|
|
899
778
|
// Brightness
|
|
900
|
-
if (this.Brightness === undefined) {
|
|
901
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.Brightness}`);
|
|
779
|
+
if (this.LightBulb.Brightness === undefined) {
|
|
780
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
|
|
902
781
|
} else {
|
|
903
|
-
this.accessory.context.Brightness = this.Brightness;
|
|
904
|
-
this.
|
|
905
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Brightness: ${this.Brightness}`);
|
|
782
|
+
this.accessory.context.Brightness = this.LightBulb.Brightness;
|
|
783
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Brightness, this.LightBulb.Brightness);
|
|
784
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Brightness: ${this.LightBulb.Brightness}`);
|
|
906
785
|
}
|
|
907
786
|
// ColorTemperature
|
|
908
|
-
if (this.ColorTemperature === undefined) {
|
|
909
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${this.ColorTemperature}`);
|
|
787
|
+
if (this.LightBulb.ColorTemperature === undefined) {
|
|
788
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${this.LightBulb.ColorTemperature}`);
|
|
910
789
|
} else {
|
|
911
|
-
this.accessory.context.ColorTemperature = this.ColorTemperature;
|
|
912
|
-
this.
|
|
913
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic
|
|
790
|
+
this.accessory.context.ColorTemperature = this.LightBulb.ColorTemperature;
|
|
791
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, this.LightBulb.ColorTemperature);
|
|
792
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
|
|
793
|
+
+ ` ColorTemperature: ${this.LightBulb.ColorTemperature}`);
|
|
914
794
|
}
|
|
915
795
|
// Hue
|
|
916
|
-
if (this.Hue === undefined) {
|
|
917
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.Hue}`);
|
|
796
|
+
if (this.LightBulb.Hue === undefined) {
|
|
797
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.LightBulb.Hue}`);
|
|
918
798
|
} else {
|
|
919
|
-
this.accessory.context.Hue = this.Hue;
|
|
920
|
-
this.
|
|
921
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Hue: ${this.Hue}`);
|
|
799
|
+
this.accessory.context.Hue = this.LightBulb.Hue;
|
|
800
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Hue, this.LightBulb.Hue);
|
|
801
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Hue: ${this.LightBulb.Hue}`);
|
|
922
802
|
}
|
|
923
803
|
// Saturation
|
|
924
|
-
if (this.Saturation === undefined) {
|
|
925
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.Saturation}`);
|
|
804
|
+
if (this.LightBulb.Saturation === undefined) {
|
|
805
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.LightBulb.Saturation}`);
|
|
926
806
|
} 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
|
-
})();
|
|
807
|
+
this.accessory.context.Saturation = this.LightBulb.Saturation;
|
|
808
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Saturation, this.LightBulb.Saturation);
|
|
809
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Saturation: ${this.LightBulb.Saturation}`);
|
|
959
810
|
}
|
|
960
811
|
}
|
|
961
812
|
|
|
@@ -975,35 +826,6 @@ export class StripLight {
|
|
|
975
826
|
}
|
|
976
827
|
}
|
|
977
828
|
|
|
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
|
-
minStep(device: device & devicesConfig): number {
|
|
999
|
-
if (device.striplight?.set_minStep) {
|
|
1000
|
-
this.set_minStep = device.striplight?.set_minStep;
|
|
1001
|
-
} else {
|
|
1002
|
-
this.set_minStep = 1;
|
|
1003
|
-
}
|
|
1004
|
-
return this.set_minStep;
|
|
1005
|
-
}
|
|
1006
|
-
|
|
1007
829
|
async adaptiveLighting(device: device & devicesConfig): Promise<void> {
|
|
1008
830
|
if (device.striplight?.adaptiveLightingShift) {
|
|
1009
831
|
this.adaptiveLightingShift = device.striplight.adaptiveLightingShift;
|
|
@@ -1014,254 +836,17 @@ export class StripLight {
|
|
|
1014
836
|
}
|
|
1015
837
|
}
|
|
1016
838
|
|
|
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
839
|
async offlineOff(): Promise<void> {
|
|
1110
840
|
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
|
-
}
|
|
841
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.On, false);
|
|
1261
842
|
}
|
|
1262
843
|
}
|
|
1263
844
|
|
|
1264
|
-
|
|
1265
|
-
|
|
845
|
+
async apiError(e: any): Promise<void> {
|
|
846
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.On, e);
|
|
847
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Hue, e);
|
|
848
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Brightness, e);
|
|
849
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Saturation, e);
|
|
850
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, e);
|
|
1266
851
|
}
|
|
1267
852
|
}
|