@switchbot/homebridge-switchbot 3.5.0-beta.6 → 3.5.0-beta.61
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/settings.json +4 -1
- package/CHANGELOG.md +4 -1
- package/config.schema.json +120 -17
- package/dist/device/blindtilt.d.ts +23 -96
- package/dist/device/blindtilt.d.ts.map +1 -1
- package/dist/device/blindtilt.js +408 -779
- package/dist/device/blindtilt.js.map +1 -1
- package/dist/device/bot.d.ts +26 -72
- package/dist/device/bot.d.ts.map +1 -1
- package/dist/device/bot.js +696 -822
- package/dist/device/bot.js.map +1 -1
- package/dist/device/ceilinglight.d.ts +14 -68
- package/dist/device/ceilinglight.d.ts.map +1 -1
- package/dist/device/ceilinglight.js +237 -606
- package/dist/device/ceilinglight.js.map +1 -1
- package/dist/device/colorbulb.d.ts +14 -75
- package/dist/device/colorbulb.d.ts.map +1 -1
- package/dist/device/colorbulb.js +280 -658
- package/dist/device/colorbulb.js.map +1 -1
- package/dist/device/contact.d.ts +15 -66
- package/dist/device/contact.d.ts.map +1 -1
- package/dist/device/contact.js +242 -544
- package/dist/device/contact.js.map +1 -1
- package/dist/device/curtain.d.ts +19 -88
- package/dist/device/curtain.d.ts.map +1 -1
- package/dist/device/curtain.js +402 -805
- 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 +684 -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 +535 -0
- package/dist/device/fan.js.map +1 -0
- package/dist/device/hub.d.ts +16 -60
- package/dist/device/hub.d.ts.map +1 -1
- package/dist/device/hub.js +246 -613
- 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 +232 -593
- 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 +185 -600
- package/dist/device/iosensor.js.map +1 -1
- package/dist/device/lightstrip.d.ts +11 -69
- package/dist/device/lightstrip.d.ts.map +1 -1
- package/dist/device/lightstrip.js +277 -586
- package/dist/device/lightstrip.js.map +1 -1
- package/dist/device/lock.d.ts +16 -63
- package/dist/device/lock.d.ts.map +1 -1
- package/dist/device/lock.js +241 -560
- package/dist/device/lock.js.map +1 -1
- package/dist/device/meter.d.ts +14 -64
- package/dist/device/meter.d.ts.map +1 -1
- package/dist/device/meter.js +181 -593
- package/dist/device/meter.js.map +1 -1
- package/dist/device/meterplus.d.ts +14 -65
- package/dist/device/meterplus.d.ts.map +1 -1
- package/dist/device/meterplus.js +170 -593
- package/dist/device/meterplus.js.map +1 -1
- package/dist/device/motion.d.ts +13 -69
- package/dist/device/motion.d.ts.map +1 -1
- package/dist/device/motion.js +178 -531
- 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 +92 -458
- package/dist/device/plug.js.map +1 -1
- package/dist/device/robotvacuumcleaner.d.ts +12 -63
- package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
- package/dist/device/robotvacuumcleaner.js +197 -521
- package/dist/device/robotvacuumcleaner.js.map +1 -1
- package/dist/device/waterdetector.d.ts +10 -65
- package/dist/device/waterdetector.d.ts.map +1 -1
- package/dist/device/waterdetector.js +139 -495
- 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 +9 -49
- 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 +8 -39
- 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 +7 -36
- 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 +10 -43
- package/dist/irdevice/fan.d.ts.map +1 -1
- package/dist/irdevice/fan.js +100 -338
- package/dist/irdevice/fan.js.map +1 -1
- package/dist/irdevice/irdevice.d.ts +40 -0
- package/dist/irdevice/irdevice.d.ts.map +1 -0
- package/dist/irdevice/irdevice.js +330 -0
- package/dist/irdevice/irdevice.js.map +1 -0
- package/dist/irdevice/light.d.ts +12 -45
- package/dist/irdevice/light.d.ts.map +1 -1
- package/dist/irdevice/light.js +87 -357
- 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 +352 -546
- 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 +82 -321
- 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 +41 -284
- 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 +42 -287
- package/dist/irdevice/waterheater.js.map +1 -1
- package/dist/platform.d.ts +19 -6
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +310 -169
- package/dist/platform.js.map +1 -1
- package/dist/settings.d.ts +62 -19
- package/dist/settings.d.ts.map +1 -1
- package/dist/settings.js +16 -552
- package/dist/settings.js.map +1 -1
- package/dist/utils.d.ts +77 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +638 -1
- package/dist/utils.js.map +1 -1
- package/eslint.config.js +96 -0
- package/package.json +19 -15
- package/src/custom.d.ts +2 -1
- package/src/device/blindtilt.ts +442 -814
- package/src/device/bot.ts +754 -915
- package/src/device/ceilinglight.ts +255 -633
- package/src/device/colorbulb.ts +295 -685
- package/src/device/contact.ts +277 -571
- package/src/device/curtain.ts +431 -835
- package/src/device/device.ts +709 -0
- package/src/device/fan.ts +591 -0
- package/src/device/hub.ts +286 -644
- package/src/device/humidifier.ts +266 -646
- package/src/device/iosensor.ts +220 -625
- package/src/device/lightstrip.ts +305 -620
- package/src/device/lock.ts +280 -600
- package/src/device/meter.ts +216 -617
- package/src/device/meterplus.ts +205 -619
- package/src/device/motion.ts +206 -556
- package/src/device/plug.ts +107 -490
- package/src/device/robotvacuumcleaner.ts +223 -550
- package/src/device/waterdetector.ts +160 -519
- 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 +333 -447
- package/src/irdevice/airpurifier.ts +92 -322
- package/src/irdevice/camera.ts +45 -294
- package/src/irdevice/fan.ts +115 -353
- package/src/irdevice/irdevice.ts +346 -0
- package/src/irdevice/light.ts +110 -385
- package/src/irdevice/other.ts +399 -616
- package/src/irdevice/tv.ts +103 -340
- package/src/irdevice/vacuumcleaner.ts +52 -306
- package/src/irdevice/waterheater.ts +53 -311
- package/src/platform.ts +355 -216
- package/src/settings.ts +116 -611
- package/src/utils.ts +642 -1
- package/src/device/waterdetector.json +0 -11
package/src/device/colorbulb.ts
CHANGED
|
@@ -1,138 +1,97 @@
|
|
|
1
|
+
/* Copyright(C) 2021-2024, donavanbecker (https://github.com/donavanbecker). All rights reserved.
|
|
2
|
+
*
|
|
3
|
+
* blindtilt.ts: @switchbot/homebridge-switchbot.
|
|
4
|
+
*/
|
|
1
5
|
import { request } from 'undici';
|
|
2
|
-
import {
|
|
6
|
+
import { deviceBase } from './device.js';
|
|
3
7
|
import { interval, Subject } from 'rxjs';
|
|
4
|
-
import {
|
|
8
|
+
import { Devices } from '../settings.js';
|
|
9
|
+
import { hs2rgb, rgb2hs, m2hs } from '../utils.js';
|
|
5
10
|
import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
} from 'homebridge';
|
|
11
|
+
|
|
12
|
+
import type { SwitchBotPlatform } from '../platform.js';
|
|
13
|
+
import type { device, devicesConfig, deviceStatus, serviceData} from '../settings.js';
|
|
14
|
+
import type { Service, PlatformAccessory, CharacteristicValue, ControllerConstructor, Controller, ControllerServiceMap } from 'homebridge';
|
|
10
15
|
|
|
11
16
|
/**
|
|
12
17
|
* Platform Accessory
|
|
13
18
|
* An instance of this class is created for each accessory your platform registers
|
|
14
19
|
* Each accessory may expose multiple services of different service types.
|
|
15
20
|
*/
|
|
16
|
-
export class ColorBulb {
|
|
17
|
-
public readonly api: API;
|
|
18
|
-
public readonly log: Logging;
|
|
19
|
-
public readonly config!: SwitchBotPlatformConfig;
|
|
20
|
-
protected readonly hap: HAP;
|
|
21
|
+
export class ColorBulb extends deviceBase {
|
|
21
22
|
// 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
|
-
OpenAPI_ColorTemperature?: deviceStatus['colorTemperature'];
|
|
38
|
-
|
|
39
|
-
// BLE Status
|
|
40
|
-
BLE_ColorTemperature: serviceData['color_temperature'];
|
|
41
|
-
BLE_Power: serviceData['power'];
|
|
42
|
-
BLE_Red: serviceData['red'];
|
|
43
|
-
BLE_Blue: serviceData['blue'];
|
|
44
|
-
BLE_Green: serviceData['green'];
|
|
45
|
-
BLE_On: serviceData['state'];
|
|
46
|
-
BLE_Delay: serviceData['delay'];
|
|
47
|
-
BLE_WifiRssi: serviceData['wifiRssi'];
|
|
48
|
-
BLE_Brightness: serviceData['brightness'];
|
|
49
|
-
BLE_Saturation;
|
|
50
|
-
BLE_Hue;
|
|
51
|
-
|
|
52
|
-
// BLE Others
|
|
53
|
-
BLE_IsConnected?: boolean;
|
|
54
|
-
|
|
55
|
-
// Config
|
|
56
|
-
set_minStep?: number;
|
|
57
|
-
scanDuration!: number;
|
|
58
|
-
deviceLogging!: string;
|
|
59
|
-
deviceRefreshRate!: number;
|
|
60
|
-
adaptiveLightingShift?: number;
|
|
23
|
+
private LightBulb: {
|
|
24
|
+
Name: CharacteristicValue;
|
|
25
|
+
Service: Service;
|
|
26
|
+
On: CharacteristicValue;
|
|
27
|
+
Hue: CharacteristicValue;
|
|
28
|
+
Saturation: CharacteristicValue;
|
|
29
|
+
Brightness: CharacteristicValue;
|
|
30
|
+
ColorTemperature?: CharacteristicValue;
|
|
31
|
+
};
|
|
61
32
|
|
|
62
33
|
// Adaptive Lighting
|
|
63
34
|
AdaptiveLightingController?: ControllerConstructor | Controller<ControllerServiceMap>;
|
|
64
|
-
|
|
65
|
-
maxKelvin!: number;
|
|
66
|
-
|
|
67
|
-
// Others
|
|
68
|
-
cacheKelvin!: number;
|
|
35
|
+
adaptiveLightingShift?: number;
|
|
69
36
|
|
|
70
37
|
// Updates
|
|
71
38
|
colorBulbUpdateInProgress!: boolean;
|
|
72
39
|
doColorBulbUpdate!: Subject<void>;
|
|
73
40
|
|
|
74
|
-
// Connection
|
|
75
|
-
private readonly OpenAPI: boolean;
|
|
76
|
-
private readonly BLE: boolean;
|
|
77
|
-
|
|
78
41
|
constructor(
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
42
|
+
readonly platform: SwitchBotPlatform,
|
|
43
|
+
accessory: PlatformAccessory,
|
|
44
|
+
device: device & devicesConfig,
|
|
82
45
|
) {
|
|
83
|
-
|
|
84
|
-
this.log = this.platform.log;
|
|
85
|
-
this.config = this.platform.config;
|
|
86
|
-
this.hap = this.api.hap;
|
|
87
|
-
// Connection
|
|
88
|
-
this.BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
|
|
89
|
-
this.OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
|
|
46
|
+
super(platform, accessory, device);
|
|
90
47
|
// default placeholders
|
|
91
|
-
this.deviceLogs(device);
|
|
92
|
-
this.scan(device);
|
|
93
|
-
this.refreshRate(device);
|
|
94
48
|
this.adaptiveLighting(device);
|
|
95
|
-
this.deviceContext();
|
|
96
|
-
this.deviceConfig(device);
|
|
97
49
|
|
|
98
50
|
// this is subject we use to track when we need to POST changes to the SwitchBot API
|
|
99
51
|
this.doColorBulbUpdate = new Subject();
|
|
100
52
|
this.colorBulbUpdateInProgress = false;
|
|
101
53
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
.getService(this.hap.Service.
|
|
108
|
-
.
|
|
109
|
-
.
|
|
110
|
-
.
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
(this.
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
this.accessory.
|
|
121
|
-
this.lightBulbService = this.accessory.addService(this.hap.Service.Lightbulb);
|
|
122
|
-
this.accessory.context.adaptiveLighting = false;
|
|
123
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLighting: ${this.accessory.context.adaptiveLighting}`);
|
|
54
|
+
accessory.context.LightBulb = {};
|
|
55
|
+
|
|
56
|
+
// Initialize LightBulb property
|
|
57
|
+
this.LightBulb = {
|
|
58
|
+
Name: accessory.context.LightBulb.Name ?? accessory.displayName,
|
|
59
|
+
Service: accessory.getService(this.hap.Service.Lightbulb) ?? accessory.addService(this.hap.Service.Lightbulb) as Service,
|
|
60
|
+
On: accessory.context.On ?? false,
|
|
61
|
+
Hue: accessory.context.Hue ?? 0,
|
|
62
|
+
Saturation: accessory.context.Saturation ?? 0,
|
|
63
|
+
Brightness: accessory.context.Brightness ?? 0,
|
|
64
|
+
ColorTemperature: accessory.context.ColorTemperature ?? 140,
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// Adaptive Lighting
|
|
68
|
+
if (this.adaptiveLightingShift === -1 && accessory.context.adaptiveLighting) {
|
|
69
|
+
accessory.removeService(this.LightBulb.Service);
|
|
70
|
+
this.LightBulb.Service = accessory.addService(this.hap.Service.Lightbulb);
|
|
71
|
+
accessory.context.adaptiveLighting = false;
|
|
72
|
+
this.debugLog(`${device.deviceType}: ${accessory.displayName} adaptiveLighting: ${accessory.context.adaptiveLighting}`);
|
|
124
73
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
74
|
+
if (this.adaptiveLightingShift !== -1) {
|
|
75
|
+
this.AdaptiveLightingController = new platform.api.hap.AdaptiveLightingController(this.LightBulb.Service, {
|
|
76
|
+
customTemperatureAdjustment: this.adaptiveLightingShift,
|
|
77
|
+
});
|
|
78
|
+
accessory.configureController(this.AdaptiveLightingController);
|
|
79
|
+
accessory.context.adaptiveLighting = true;
|
|
80
|
+
this.debugLog(`${device.deviceType}: ${this.accessory.displayName} adaptiveLighting: ${accessory.context.adaptiveLighting},`
|
|
81
|
+
+ ` adaptiveLightingShift: ${this.adaptiveLightingShift}`);
|
|
129
82
|
}
|
|
83
|
+
this.debugLog(`${device.deviceType}: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
|
|
130
84
|
|
|
131
|
-
//
|
|
132
|
-
this.
|
|
85
|
+
// Initialize LightBulb Characteristics
|
|
86
|
+
this.LightBulb.Service
|
|
87
|
+
.setCharacteristic(this.hap.Characteristic.Name, this.LightBulb.Name)
|
|
88
|
+
.getCharacteristic(this.hap.Characteristic.On)
|
|
89
|
+
.onGet(() => {
|
|
90
|
+
return this.LightBulb.On;
|
|
91
|
+
})
|
|
92
|
+
.onSet(this.OnSet.bind(this));
|
|
133
93
|
|
|
134
|
-
|
|
135
|
-
this.lightBulbService
|
|
94
|
+
this.LightBulb.Service
|
|
136
95
|
.getCharacteristic(this.hap.Characteristic.Brightness)
|
|
137
96
|
.setProps({
|
|
138
97
|
minStep: this.minStep(device),
|
|
@@ -141,12 +100,11 @@ export class ColorBulb {
|
|
|
141
100
|
validValueRanges: [0, 100],
|
|
142
101
|
})
|
|
143
102
|
.onGet(() => {
|
|
144
|
-
return this.Brightness;
|
|
103
|
+
return this.LightBulb.Brightness;
|
|
145
104
|
})
|
|
146
105
|
.onSet(this.BrightnessSet.bind(this));
|
|
147
106
|
|
|
148
|
-
|
|
149
|
-
this.lightBulbService
|
|
107
|
+
this.LightBulb.Service
|
|
150
108
|
.getCharacteristic(this.hap.Characteristic.ColorTemperature)
|
|
151
109
|
.setProps({
|
|
152
110
|
minValue: 140,
|
|
@@ -154,12 +112,11 @@ export class ColorBulb {
|
|
|
154
112
|
validValueRanges: [140, 500],
|
|
155
113
|
})
|
|
156
114
|
.onGet(() => {
|
|
157
|
-
return this.ColorTemperature!;
|
|
115
|
+
return this.LightBulb.ColorTemperature!;
|
|
158
116
|
})
|
|
159
117
|
.onSet(this.ColorTemperatureSet.bind(this));
|
|
160
118
|
|
|
161
|
-
|
|
162
|
-
this.lightBulbService
|
|
119
|
+
this.LightBulb.Service
|
|
163
120
|
.getCharacteristic(this.hap.Characteristic.Hue)
|
|
164
121
|
.setProps({
|
|
165
122
|
minValue: 0,
|
|
@@ -167,12 +124,11 @@ export class ColorBulb {
|
|
|
167
124
|
validValueRanges: [0, 360],
|
|
168
125
|
})
|
|
169
126
|
.onGet(() => {
|
|
170
|
-
return this.Hue;
|
|
127
|
+
return this.LightBulb.Hue;
|
|
171
128
|
})
|
|
172
129
|
.onSet(this.HueSet.bind(this));
|
|
173
130
|
|
|
174
|
-
|
|
175
|
-
this.lightBulbService
|
|
131
|
+
this.LightBulb.Service
|
|
176
132
|
.getCharacteristic(this.hap.Characteristic.Saturation)
|
|
177
133
|
.setProps({
|
|
178
134
|
minValue: 0,
|
|
@@ -180,22 +136,13 @@ export class ColorBulb {
|
|
|
180
136
|
validValueRanges: [0, 100],
|
|
181
137
|
})
|
|
182
138
|
.onGet(() => {
|
|
183
|
-
return this.Saturation;
|
|
139
|
+
return this.LightBulb.Saturation;
|
|
184
140
|
})
|
|
185
141
|
.onSet(this.SaturationSet.bind(this));
|
|
142
|
+
accessory.context.LightBulb.Name = this.LightBulb.Name;
|
|
186
143
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
this.AdaptiveLightingController = new platform.api.hap.AdaptiveLightingController(this.lightBulbService, {
|
|
190
|
-
customTemperatureAdjustment: this.adaptiveLightingShift,
|
|
191
|
-
});
|
|
192
|
-
this.accessory.configureController(this.AdaptiveLightingController);
|
|
193
|
-
this.accessory.context.adaptiveLighting = true;
|
|
194
|
-
this.debugLog(
|
|
195
|
-
`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLighting: ${this.accessory.context.adaptiveLighting},` +
|
|
196
|
-
` adaptiveLightingShift: ${this.adaptiveLightingShift}`,
|
|
197
|
-
);
|
|
198
|
-
}
|
|
144
|
+
// Retrieve initial values and updateHomekit
|
|
145
|
+
this.refreshStatus();
|
|
199
146
|
|
|
200
147
|
// Update Homekit
|
|
201
148
|
this.updateHomeKitCharacteristics();
|
|
@@ -208,19 +155,33 @@ export class ColorBulb {
|
|
|
208
155
|
});
|
|
209
156
|
|
|
210
157
|
//regisiter webhook event handler
|
|
211
|
-
if (
|
|
212
|
-
this.
|
|
158
|
+
if (device.webhook) {
|
|
159
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} is listening webhook.`);
|
|
213
160
|
this.platform.webhookEventHandler[this.device.deviceId] = async (context) => {
|
|
214
161
|
try {
|
|
215
162
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
|
|
216
163
|
const { powerState, brightness, color, colorTemperature } = context;
|
|
217
|
-
const { On, Brightness, Hue, Saturation, ColorTemperature } = this;
|
|
164
|
+
const { On, Brightness, Hue, Saturation, ColorTemperature } = this.LightBulb;
|
|
218
165
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
|
|
219
166
|
'(powerState, brightness, color, colorTemperature) = ' +
|
|
220
167
|
`Webhook:(${powerState}, ${brightness}, ${color}, ${colorTemperature}), ` +
|
|
221
168
|
`current:(${On}, ${Brightness}, ${Hue}, ${Saturation}, ${ColorTemperature})`);
|
|
222
|
-
|
|
223
|
-
|
|
169
|
+
|
|
170
|
+
// On
|
|
171
|
+
this.LightBulb.On = powerState === 'ON' ? true : false;
|
|
172
|
+
if (this.accessory.context.Brightness !== this.LightBulb.On) {
|
|
173
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
|
|
174
|
+
} else {
|
|
175
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Brightness
|
|
179
|
+
this.LightBulb.Brightness = brightness;
|
|
180
|
+
if (this.accessory.context.Brightness !== this.LightBulb.Brightness) {
|
|
181
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
|
|
182
|
+
} else {
|
|
183
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
|
|
184
|
+
}
|
|
224
185
|
|
|
225
186
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} color: ${JSON.stringify(color)}`);
|
|
226
187
|
const [red, green, blue] = color!.split(':');
|
|
@@ -234,26 +195,26 @@ export class ColorBulb {
|
|
|
234
195
|
);
|
|
235
196
|
|
|
236
197
|
// Hue
|
|
237
|
-
this.Hue = hue;
|
|
238
|
-
if (this.accessory.context.Hue !== this.Hue) {
|
|
239
|
-
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.Hue}`);
|
|
198
|
+
this.LightBulb.Hue = hue;
|
|
199
|
+
if (this.accessory.context.Hue !== this.LightBulb.Hue) {
|
|
200
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.LightBulb.Hue}`);
|
|
240
201
|
} else {
|
|
241
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.Hue}`);
|
|
202
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.LightBulb.Hue}`);
|
|
242
203
|
}
|
|
243
204
|
|
|
244
205
|
// Saturation
|
|
245
|
-
this.Saturation = saturation;
|
|
246
|
-
if (this.accessory.context.Saturation !== this.Saturation) {
|
|
247
|
-
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.Saturation}`);
|
|
206
|
+
this.LightBulb.Saturation = saturation;
|
|
207
|
+
if (this.accessory.context.Saturation !== this.LightBulb.Saturation) {
|
|
208
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.LightBulb.Saturation}`);
|
|
248
209
|
} else {
|
|
249
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.Saturation}`);
|
|
210
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.LightBulb.Saturation}`);
|
|
250
211
|
}
|
|
251
212
|
|
|
252
|
-
this.ColorTemperature = colorTemperature;
|
|
253
|
-
if (this.accessory.context.ColorTemperature !== this.ColorTemperature) {
|
|
254
|
-
this.
|
|
213
|
+
this.LightBulb.ColorTemperature = colorTemperature;
|
|
214
|
+
if (this.accessory.context.ColorTemperature !== this.LightBulb.ColorTemperature) {
|
|
215
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${this.LightBulb.ColorTemperature}`);
|
|
255
216
|
} else {
|
|
256
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${this.ColorTemperature}`);
|
|
217
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${this.LightBulb.ColorTemperature}`);
|
|
257
218
|
}
|
|
258
219
|
this.updateHomeKitCharacteristics();
|
|
259
220
|
} catch (e: any) {
|
|
@@ -287,89 +248,75 @@ export class ColorBulb {
|
|
|
287
248
|
}
|
|
288
249
|
|
|
289
250
|
/**
|
|
290
|
-
* Parse the device status from the
|
|
251
|
+
* Parse the device status from the SwitchBotBLE API
|
|
291
252
|
*/
|
|
292
|
-
async
|
|
293
|
-
if (!this.device.enableCloudService && this.OpenAPI) {
|
|
294
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} parseStatus enableCloudService: ${this.device.enableCloudService}`);
|
|
295
|
-
} else if (this.BLE) {
|
|
296
|
-
await this.BLEparseStatus();
|
|
297
|
-
} else if (this.OpenAPI && this.platform.config.credentials?.token) {
|
|
298
|
-
await this.openAPIparseStatus();
|
|
299
|
-
} else {
|
|
300
|
-
await this.offlineOff();
|
|
301
|
-
this.debugWarnLog(
|
|
302
|
-
`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:` + ` ${this.device.connectionType}, parseStatus will not happen.`,
|
|
303
|
-
);
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
async BLEparseStatus(): Promise<void> {
|
|
253
|
+
async BLEparseStatus(serviceData: serviceData): Promise<void> {
|
|
308
254
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
|
|
309
255
|
// State
|
|
310
|
-
switch (
|
|
256
|
+
switch (serviceData.power) {
|
|
311
257
|
case true:
|
|
312
|
-
this.On = true;
|
|
258
|
+
this.LightBulb.On = true;
|
|
313
259
|
break;
|
|
314
260
|
default:
|
|
315
|
-
this.On = false;
|
|
261
|
+
this.LightBulb.On = false;
|
|
316
262
|
}
|
|
317
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
|
|
263
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
|
|
318
264
|
|
|
319
265
|
// Brightness
|
|
320
|
-
this.Brightness = Number(
|
|
321
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.Brightness}`);
|
|
266
|
+
this.LightBulb.Brightness = Number(serviceData.brightness);
|
|
267
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
|
|
322
268
|
|
|
323
269
|
// Color, Hue & Brightness
|
|
324
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} red: ${
|
|
325
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} green: ${
|
|
326
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} blue: ${
|
|
270
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} red: ${serviceData.red}`);
|
|
271
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} green: ${serviceData.green}`);
|
|
272
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} blue: ${serviceData.blue}`);
|
|
327
273
|
|
|
328
|
-
const [hue, saturation] = rgb2hs(Number(
|
|
274
|
+
const [hue, saturation] = rgb2hs(Number(serviceData.red), Number(serviceData.green), Number(serviceData.blue));
|
|
329
275
|
this.debugLog(
|
|
330
276
|
`${this.device.deviceType}: ${this.accessory.displayName}` +
|
|
331
|
-
` hs: ${JSON.stringify(rgb2hs(Number(
|
|
277
|
+
` hs: ${JSON.stringify(rgb2hs(Number(serviceData.red), Number(serviceData.green), Number(serviceData.blue)))}`,
|
|
332
278
|
);
|
|
333
|
-
this.BLE_Hue = hue;
|
|
334
|
-
this.BLE_Saturation = saturation;
|
|
335
279
|
|
|
336
280
|
// Hue
|
|
337
|
-
this.Hue =
|
|
338
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.Hue}`);
|
|
281
|
+
this.LightBulb.Hue = hue;
|
|
282
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.LightBulb.Hue}`);
|
|
339
283
|
|
|
340
284
|
// Saturation
|
|
341
|
-
this.Saturation =
|
|
342
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.Saturation}`);
|
|
285
|
+
this.LightBulb.Saturation = saturation;
|
|
286
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.LightBulb.Saturation}`);
|
|
343
287
|
|
|
344
288
|
// ColorTemperature
|
|
345
|
-
if (
|
|
346
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${
|
|
347
|
-
this.ColorTemperature =
|
|
289
|
+
if (serviceData.color_temperature) {
|
|
290
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${serviceData.color_temperature}`);
|
|
291
|
+
this.LightBulb.ColorTemperature = serviceData.color_temperature!;
|
|
348
292
|
|
|
349
|
-
this.ColorTemperature = Math.max(Math.min(this.ColorTemperature, 500), 140);
|
|
350
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${this.ColorTemperature}`);
|
|
293
|
+
this.LightBulb.ColorTemperature = Math.max(Math.min(this.LightBulb.ColorTemperature, 500), 140);
|
|
294
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${this.LightBulb.ColorTemperature}`);
|
|
351
295
|
}
|
|
352
296
|
}
|
|
353
297
|
|
|
354
|
-
|
|
298
|
+
/**
|
|
299
|
+
* Parse the device status from the SwitchBot OpenAPI
|
|
300
|
+
*/
|
|
301
|
+
async openAPIparseStatus(deviceStatus: deviceStatus): Promise<void> {
|
|
355
302
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
|
|
356
|
-
switch (
|
|
303
|
+
switch (deviceStatus.body.power) {
|
|
357
304
|
case 'on':
|
|
358
|
-
this.On = true;
|
|
305
|
+
this.LightBulb.On = true;
|
|
359
306
|
break;
|
|
360
307
|
default:
|
|
361
|
-
this.On = false;
|
|
308
|
+
this.LightBulb.On = false;
|
|
362
309
|
}
|
|
363
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
|
|
310
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
|
|
364
311
|
|
|
365
312
|
// Brightness
|
|
366
|
-
this.Brightness = Number(
|
|
367
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.Brightness}`);
|
|
313
|
+
this.LightBulb.Brightness = Number(deviceStatus.body.brightness);
|
|
314
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
|
|
368
315
|
|
|
369
316
|
// Color, Hue & Brightness
|
|
370
|
-
if (
|
|
371
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} color: ${JSON.stringify(
|
|
372
|
-
const [red, green, blue] =
|
|
317
|
+
if (deviceStatus.body.color) {
|
|
318
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} color: ${JSON.stringify(deviceStatus.body.color)}`);
|
|
319
|
+
const [red, green, blue] = deviceStatus.body.color!.split(':');
|
|
373
320
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} red: ${JSON.stringify(red)}`);
|
|
374
321
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} green: ${JSON.stringify(green)}`);
|
|
375
322
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} blue: ${JSON.stringify(blue)}`);
|
|
@@ -380,28 +327,36 @@ export class ColorBulb {
|
|
|
380
327
|
);
|
|
381
328
|
|
|
382
329
|
// Hue
|
|
383
|
-
this.Hue = hue;
|
|
384
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.Hue}`);
|
|
330
|
+
this.LightBulb.Hue = hue;
|
|
331
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.LightBulb.Hue}`);
|
|
385
332
|
|
|
386
333
|
// Saturation
|
|
387
|
-
this.Saturation = saturation;
|
|
388
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.Saturation}`);
|
|
334
|
+
this.LightBulb.Saturation = saturation;
|
|
335
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.LightBulb.Saturation}`);
|
|
389
336
|
}
|
|
390
337
|
|
|
391
338
|
// ColorTemperature
|
|
392
|
-
if (!Number.isNaN(
|
|
393
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${
|
|
394
|
-
const mired = Math.round(1000000 /
|
|
339
|
+
if (!Number.isNaN(deviceStatus.body.colorTemperature)) {
|
|
340
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${deviceStatus.body.colorTemperature}`);
|
|
341
|
+
const mired = Math.round(1000000 / deviceStatus.body.colorTemperature!);
|
|
395
342
|
|
|
396
|
-
this.ColorTemperature = Number(mired);
|
|
343
|
+
this.LightBulb.ColorTemperature = Number(mired);
|
|
397
344
|
|
|
398
|
-
this.ColorTemperature = Math.max(Math.min(this.ColorTemperature, 500), 140);
|
|
399
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${this.ColorTemperature}`);
|
|
345
|
+
this.LightBulb.ColorTemperature = Math.max(Math.min(this.LightBulb.ColorTemperature, 500), 140);
|
|
346
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${this.LightBulb.ColorTemperature}`);
|
|
400
347
|
}
|
|
401
348
|
|
|
402
|
-
//
|
|
403
|
-
|
|
404
|
-
this.
|
|
349
|
+
// Firmware Version
|
|
350
|
+
const version = deviceStatus.body.version?.toString();
|
|
351
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Firmware Version: ${version?.replace(/^V|-.*$/g, '')}`);
|
|
352
|
+
if (deviceStatus.body.version) {
|
|
353
|
+
this.accessory.context.version = version?.replace(/^V|-.*$/g, '');
|
|
354
|
+
this.accessory
|
|
355
|
+
.getService(this.hap.Service.AccessoryInformation)!
|
|
356
|
+
.setCharacteristic(this.hap.Characteristic.FirmwareRevision, this.accessory.context.version)
|
|
357
|
+
.getCharacteristic(this.hap.Characteristic.FirmwareRevision)
|
|
358
|
+
.updateValue(this.accessory.context.version);
|
|
359
|
+
}
|
|
405
360
|
}
|
|
406
361
|
|
|
407
362
|
/**
|
|
@@ -436,113 +391,43 @@ export class ColorBulb {
|
|
|
436
391
|
// Start to monitor advertisement packets
|
|
437
392
|
(async () => {
|
|
438
393
|
// Start to monitor advertisement packets
|
|
439
|
-
await switchbot.startScan({
|
|
440
|
-
model: 'u',
|
|
441
|
-
id: this.device.bleMac,
|
|
442
|
-
});
|
|
394
|
+
await switchbot.startScan({ model: this.device.bleModel, id: this.device.bleMac });
|
|
443
395
|
// Set an event handler
|
|
444
396
|
switchbot.onadvertisement = (ad: any) => {
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
397
|
+
if (this.device.bleMac === ad.address && ad.model === this.device.bleModel) {
|
|
398
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
|
|
399
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.model}`);
|
|
448
400
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
449
|
-
this.BLE_ColorTemperature = ad.serviceData.color_temperature;
|
|
450
|
-
this.BLE_Power = ad.serviceData.power;
|
|
451
|
-
this.BLE_On = ad.serviceData.state;
|
|
452
|
-
this.BLE_Red = ad.serviceData.red;
|
|
453
|
-
this.BLE_Green = ad.serviceData.green;
|
|
454
|
-
this.BLE_Blue = ad.serviceData.blue;
|
|
455
|
-
this.BLE_Brightness = ad.serviceData.brightness;
|
|
456
|
-
this.BLE_Delay = ad.serviceData.delay;
|
|
457
401
|
} else {
|
|
458
402
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
459
403
|
}
|
|
460
404
|
};
|
|
461
|
-
// Wait
|
|
405
|
+
// Wait 10 seconds
|
|
462
406
|
await switchbot.wait(this.scanDuration * 1000);
|
|
463
407
|
// Stop to monitor
|
|
464
408
|
await switchbot.stopScan();
|
|
465
409
|
// Update HomeKit
|
|
466
|
-
await this.BLEparseStatus();
|
|
410
|
+
await this.BLEparseStatus(switchbot.onadvertisement.serviceData);
|
|
467
411
|
await this.updateHomeKitCharacteristics();
|
|
468
412
|
})();
|
|
469
|
-
|
|
470
|
-
switchbot
|
|
471
|
-
.startScan({
|
|
472
|
-
model: 'u',
|
|
473
|
-
id: this.device.bleMac,
|
|
474
|
-
})
|
|
475
|
-
.then(async () => {
|
|
476
|
-
// Set an event handler
|
|
477
|
-
switchbot.onadvertisement = async (ad: ad) => {
|
|
478
|
-
this.debugLog(
|
|
479
|
-
`${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},` +
|
|
480
|
-
` BLE Address Found: ${ad.address}`,
|
|
481
|
-
);
|
|
482
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
483
|
-
this.BLE_Power = ad.serviceData.power;
|
|
484
|
-
this.BLE_On = ad.serviceData.state;
|
|
485
|
-
this.BLE_Red = ad.serviceData.red;
|
|
486
|
-
this.BLE_Green = ad.serviceData.green;
|
|
487
|
-
this.BLE_Blue = ad.serviceData.blue;
|
|
488
|
-
this.BLE_ColorTemperature = ad.serviceData.color_temperature;
|
|
489
|
-
this.BLE_Brightness = ad.serviceData.brightness;
|
|
490
|
-
this.BLE_Delay = ad.serviceData.delay;
|
|
491
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
492
|
-
this.debugLog(
|
|
493
|
-
`${this.device.deviceType}: ${this.accessory.displayName} state: ${ad.serviceData.state}, ` +
|
|
494
|
-
`delay: ${ad.serviceData.delay}, timer: ${ad.serviceData.timer}, syncUtcTime: ${ad.serviceData.syncUtcTime} ` +
|
|
495
|
-
`wifiRssi: ${ad.serviceData.wifiRssi}, overload: ${ad.serviceData.overload}, currentPower: ${ad.serviceData.currentPower}`,
|
|
496
|
-
);
|
|
497
|
-
|
|
498
|
-
if (ad.serviceData) {
|
|
499
|
-
this.BLE_IsConnected = true;
|
|
500
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.BLE_IsConnected}`);
|
|
501
|
-
await this.stopScanning(switchbot);
|
|
502
|
-
} else {
|
|
503
|
-
this.BLE_IsConnected = false;
|
|
504
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.BLE_IsConnected}`);
|
|
505
|
-
}
|
|
506
|
-
};
|
|
507
|
-
// Wait
|
|
508
|
-
return await sleep(this.scanDuration * 1000);
|
|
509
|
-
})
|
|
510
|
-
.then(async () => {
|
|
511
|
-
// Stop to monitor
|
|
512
|
-
await this.stopScanning(switchbot);
|
|
513
|
-
})
|
|
514
|
-
.catch(async (e: any) => {
|
|
515
|
-
this.apiError(e);
|
|
516
|
-
this.errorLog(
|
|
517
|
-
`${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}` +
|
|
518
|
-
` Connection, Error Message: ${JSON.stringify(e.message)}`,
|
|
519
|
-
);
|
|
520
|
-
await this.BLERefreshConnection(switchbot);
|
|
521
|
-
});
|
|
522
|
-
} else {
|
|
413
|
+
if (switchbot === undefined) {
|
|
523
414
|
await this.BLERefreshConnection(switchbot);
|
|
524
|
-
}
|
|
415
|
+
}
|
|
525
416
|
}
|
|
526
417
|
|
|
527
|
-
async openAPIRefreshStatus() {
|
|
418
|
+
async openAPIRefreshStatus(): Promise<void> {
|
|
528
419
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
|
|
529
420
|
try {
|
|
530
|
-
const { body, statusCode } = await
|
|
531
|
-
headers: this.platform.generateHeaders()
|
|
532
|
-
});
|
|
421
|
+
const { body, statusCode } = await this.platform.retryRequest(this.deviceMaxRetries, this.deviceDelayBetweenRetries,
|
|
422
|
+
`${Devices}/${this.device.deviceId}/status`, { headers: this.platform.generateHeaders() });
|
|
533
423
|
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
|
|
534
424
|
const deviceStatus: any = await body.json();
|
|
535
425
|
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
|
|
536
426
|
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
|
|
537
427
|
if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
|
|
538
|
-
this.
|
|
428
|
+
this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
539
429
|
+ `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
|
|
540
|
-
this.
|
|
541
|
-
this.OpenAPI_RGB = deviceStatus.body.color;
|
|
542
|
-
this.OpenAPI_Brightness = deviceStatus.body.brightness;
|
|
543
|
-
this.OpenAPI_ColorTemperature = deviceStatus.body.colorTemperature;
|
|
544
|
-
this.OpenAPI_FirmwareRevision = deviceStatus.body.version;
|
|
545
|
-
this.openAPIparseStatus();
|
|
430
|
+
this.openAPIparseStatus(deviceStatus);
|
|
546
431
|
this.updateHomeKitCharacteristics();
|
|
547
432
|
} else {
|
|
548
433
|
this.statusCode(statusCode);
|
|
@@ -592,8 +477,9 @@ export class ColorBulb {
|
|
|
592
477
|
|
|
593
478
|
async BLEpushChanges(): Promise<void> {
|
|
594
479
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`);
|
|
595
|
-
if (this.On !== this.accessory.context.On) {
|
|
596
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges
|
|
480
|
+
if (this.LightBulb.On !== this.accessory.context.On) {
|
|
481
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`
|
|
482
|
+
+ ` On: ${this.LightBulb.On}, OnCached: ${this.accessory.context.On}`);
|
|
597
483
|
const switchbot = await this.platform.connectBLE();
|
|
598
484
|
// Convert to BLE Address
|
|
599
485
|
this.device.bleMac = this.device
|
|
@@ -607,11 +493,11 @@ export class ColorBulb {
|
|
|
607
493
|
id: this.device.bleMac,
|
|
608
494
|
})
|
|
609
495
|
.then(async (device_list: any) => {
|
|
610
|
-
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
|
|
611
|
-
return await this.
|
|
612
|
-
max: this.
|
|
496
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
|
|
497
|
+
return await this.retryBLE({
|
|
498
|
+
max: await this.maxRetryBLE(),
|
|
613
499
|
fn: async () => {
|
|
614
|
-
if (this.On) {
|
|
500
|
+
if (this.LightBulb.On) {
|
|
615
501
|
return await device_list[0].turnOn({ id: this.device.bleMac });
|
|
616
502
|
} else {
|
|
617
503
|
return await device_list[0].turnOff({ id: this.device.bleMac });
|
|
@@ -621,7 +507,9 @@ export class ColorBulb {
|
|
|
621
507
|
})
|
|
622
508
|
.then(() => {
|
|
623
509
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
|
|
624
|
-
this.
|
|
510
|
+
this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
511
|
+
+ `On: ${this.LightBulb.On} sent over BLE, sent successfully`);
|
|
512
|
+
this.LightBulb.On = false;
|
|
625
513
|
})
|
|
626
514
|
.catch(async (e: any) => {
|
|
627
515
|
this.apiError(e);
|
|
@@ -632,27 +520,28 @@ export class ColorBulb {
|
|
|
632
520
|
await this.BLEPushConnection();
|
|
633
521
|
});
|
|
634
522
|
// Push Brightness Update
|
|
635
|
-
if (this.On) {
|
|
523
|
+
if (this.LightBulb.On) {
|
|
636
524
|
await this.BLEpushBrightnessChanges();
|
|
637
525
|
}
|
|
638
526
|
// Push ColorTemperature Update
|
|
639
|
-
if (this.On) {
|
|
527
|
+
if (this.LightBulb.On) {
|
|
640
528
|
await this.BLEpushColorTemperatureChanges();
|
|
641
529
|
}
|
|
642
530
|
// Push Hue & Saturation Update
|
|
643
|
-
if (this.On) {
|
|
531
|
+
if (this.LightBulb.On) {
|
|
644
532
|
await this.BLEpushRGBChanges();
|
|
645
533
|
}
|
|
646
534
|
} else {
|
|
647
535
|
this.debugLog(
|
|
648
|
-
`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges.`
|
|
536
|
+
`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges.`
|
|
537
|
+
+ `On: ${this.LightBulb.On}, ` + `OnCached: ${this.accessory.context.On}`,
|
|
649
538
|
);
|
|
650
539
|
}
|
|
651
540
|
}
|
|
652
541
|
|
|
653
542
|
async BLEpushBrightnessChanges(): Promise<void> {
|
|
654
543
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushBrightnessChanges`);
|
|
655
|
-
if (this.Brightness !== this.accessory.context.Brightness) {
|
|
544
|
+
if (this.LightBulb.Brightness !== this.accessory.context.Brightness) {
|
|
656
545
|
const switchbot = await this.platform.connectBLE();
|
|
657
546
|
// Convert to BLE Address
|
|
658
547
|
this.device.bleMac = this.device
|
|
@@ -666,12 +555,12 @@ export class ColorBulb {
|
|
|
666
555
|
id: this.device.bleMac,
|
|
667
556
|
})
|
|
668
557
|
.then(async (device_list: any) => {
|
|
669
|
-
this.infoLog(`${this.accessory.displayName} Target Brightness: ${this.Brightness}`);
|
|
670
|
-
return await device_list[0].setBrightness(this.Brightness);
|
|
558
|
+
this.infoLog(`${this.accessory.displayName} Target Brightness: ${this.LightBulb.Brightness}`);
|
|
559
|
+
return await device_list[0].setBrightness(this.LightBulb.Brightness);
|
|
671
560
|
})
|
|
672
561
|
.then(() => {
|
|
673
562
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
|
|
674
|
-
this.On = false;
|
|
563
|
+
this.LightBulb.On = false;
|
|
675
564
|
})
|
|
676
565
|
.catch(async (e: any) => {
|
|
677
566
|
this.apiError(e);
|
|
@@ -684,7 +573,7 @@ export class ColorBulb {
|
|
|
684
573
|
} else {
|
|
685
574
|
this.debugLog(
|
|
686
575
|
`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushBrightnessChanges.` +
|
|
687
|
-
`Brightness: ${this.Brightness}, ` +
|
|
576
|
+
`Brightness: ${this.LightBulb.Brightness}, ` +
|
|
688
577
|
`BrightnessCached: ${this.accessory.context.Brightness}`,
|
|
689
578
|
);
|
|
690
579
|
}
|
|
@@ -692,7 +581,7 @@ export class ColorBulb {
|
|
|
692
581
|
|
|
693
582
|
async BLEpushColorTemperatureChanges(): Promise<void> {
|
|
694
583
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushColorTemperatureChanges`);
|
|
695
|
-
if (this.ColorTemperature !== this.accessory.context.ColorTemperature) {
|
|
584
|
+
if (this.LightBulb.ColorTemperature !== this.accessory.context.ColorTemperature) {
|
|
696
585
|
const switchbot = await this.platform.connectBLE();
|
|
697
586
|
// Convert to BLE Address
|
|
698
587
|
this.device.bleMac = this.device
|
|
@@ -706,12 +595,12 @@ export class ColorBulb {
|
|
|
706
595
|
id: this.device.bleMac,
|
|
707
596
|
})
|
|
708
597
|
.then(async (device_list: any) => {
|
|
709
|
-
this.infoLog(`${this.accessory.displayName} Target ColorTemperature: ${this.ColorTemperature}`);
|
|
710
|
-
return await device_list[0].setColorTemperature(this.ColorTemperature);
|
|
598
|
+
this.infoLog(`${this.accessory.displayName} Target ColorTemperature: ${this.LightBulb.ColorTemperature}`);
|
|
599
|
+
return await device_list[0].setColorTemperature(this.LightBulb.ColorTemperature);
|
|
711
600
|
})
|
|
712
601
|
.then(() => {
|
|
713
602
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
|
|
714
|
-
this.On = false;
|
|
603
|
+
this.LightBulb.On = false;
|
|
715
604
|
})
|
|
716
605
|
.catch(async (e: any) => {
|
|
717
606
|
this.apiError(e);
|
|
@@ -724,18 +613,18 @@ export class ColorBulb {
|
|
|
724
613
|
} else {
|
|
725
614
|
this.debugLog(
|
|
726
615
|
`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushColorTemperatureChanges.` +
|
|
727
|
-
`ColorTemperature: ${this.ColorTemperature}, ColorTemperatureCached: ${this.accessory.context.ColorTemperature}`,
|
|
616
|
+
`ColorTemperature: ${this.LightBulb.ColorTemperature}, ColorTemperatureCached: ${this.accessory.context.ColorTemperature}`,
|
|
728
617
|
);
|
|
729
618
|
}
|
|
730
619
|
}
|
|
731
620
|
|
|
732
621
|
async BLEpushRGBChanges(): Promise<void> {
|
|
733
622
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushRGBChanges`);
|
|
734
|
-
if (this.Hue !== this.accessory.context.Hue || this.Saturation !== this.accessory.context.Saturation) {
|
|
735
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${JSON.stringify(this.Hue)}`);
|
|
736
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${JSON.stringify(this.Saturation)}`);
|
|
623
|
+
if (this.LightBulb.Hue !== this.accessory.context.Hue || this.LightBulb.Saturation !== this.accessory.context.Saturation) {
|
|
624
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${JSON.stringify(this.LightBulb.Hue)}`);
|
|
625
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${JSON.stringify(this.LightBulb.Saturation)}`);
|
|
737
626
|
|
|
738
|
-
const [red, green, blue] = hs2rgb(Number(this.Hue), Number(this.Saturation));
|
|
627
|
+
const [red, green, blue] = hs2rgb(Number(this.LightBulb.Hue), Number(this.LightBulb.Saturation));
|
|
739
628
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} rgb: ${JSON.stringify([red, green, blue])}`);
|
|
740
629
|
|
|
741
630
|
const switchbot = await this.platform.connectBLE();
|
|
@@ -751,12 +640,12 @@ export class ColorBulb {
|
|
|
751
640
|
id: this.device.bleMac,
|
|
752
641
|
})
|
|
753
642
|
.then(async (device_list: any) => {
|
|
754
|
-
this.infoLog(`${this.accessory.displayName} Target RGB: ${(this.Brightness, red, green, blue)}`);
|
|
755
|
-
return await device_list[0].setRGB(this.Brightness, red, green, blue);
|
|
643
|
+
this.infoLog(`${this.accessory.displayName} Target RGB: ${(this.LightBulb.Brightness, red, green, blue)}`);
|
|
644
|
+
return await device_list[0].setRGB(this.LightBulb.Brightness, red, green, blue);
|
|
756
645
|
})
|
|
757
646
|
.then(() => {
|
|
758
647
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
|
|
759
|
-
this.On = false;
|
|
648
|
+
this.LightBulb.On = false;
|
|
760
649
|
})
|
|
761
650
|
.catch(async (e: any) => {
|
|
762
651
|
this.apiError(e);
|
|
@@ -768,17 +657,17 @@ export class ColorBulb {
|
|
|
768
657
|
});
|
|
769
658
|
} else {
|
|
770
659
|
this.debugLog(
|
|
771
|
-
`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushRGBChanges. Hue: ${this.Hue}, ` +
|
|
772
|
-
`HueCached: ${this.accessory.context.Hue}, Saturation: ${this.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`,
|
|
660
|
+
`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushRGBChanges. Hue: ${this.LightBulb.Hue}, ` +
|
|
661
|
+
`HueCached: ${this.accessory.context.Hue}, Saturation: ${this.LightBulb.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`,
|
|
773
662
|
);
|
|
774
663
|
}
|
|
775
664
|
}
|
|
776
665
|
|
|
777
666
|
async openAPIpushChanges(): Promise<void> {
|
|
778
667
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
|
|
779
|
-
if (this.On !== this.accessory.context.On) {
|
|
668
|
+
if (this.LightBulb.On !== this.accessory.context.On) {
|
|
780
669
|
let command = '';
|
|
781
|
-
if (this.On) {
|
|
670
|
+
if (this.LightBulb.On) {
|
|
782
671
|
command = 'turnOn';
|
|
783
672
|
} else {
|
|
784
673
|
command = 'turnOff';
|
|
@@ -803,6 +692,8 @@ export class ColorBulb {
|
|
|
803
692
|
if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
|
|
804
693
|
this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
805
694
|
+ `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
|
|
695
|
+
this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
696
|
+
+ `request to SwitchBot API, body: ${JSON.stringify(bodyChange)} sent successfully`);
|
|
806
697
|
} else {
|
|
807
698
|
this.statusCode(statusCode);
|
|
808
699
|
this.statusCode(deviceStatus.statusCode);
|
|
@@ -817,30 +708,30 @@ export class ColorBulb {
|
|
|
817
708
|
} else {
|
|
818
709
|
this.debugLog(
|
|
819
710
|
`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.` +
|
|
820
|
-
`On: ${this.On}, ` +
|
|
711
|
+
`On: ${this.LightBulb.On}, ` +
|
|
821
712
|
`OnCached: ${this.accessory.context.On}`,
|
|
822
713
|
);
|
|
823
714
|
}
|
|
824
715
|
// Push Hue & Saturation Update
|
|
825
|
-
if (this.On) {
|
|
716
|
+
if (this.LightBulb.On) {
|
|
826
717
|
await this.pushHueSaturationChanges();
|
|
827
718
|
}
|
|
828
719
|
// Push ColorTemperature Update
|
|
829
|
-
if (this.On) {
|
|
720
|
+
if (this.LightBulb.On) {
|
|
830
721
|
await this.pushColorTemperatureChanges();
|
|
831
722
|
}
|
|
832
723
|
// Push Brightness Update
|
|
833
|
-
if (this.On) {
|
|
724
|
+
if (this.LightBulb.On) {
|
|
834
725
|
await this.pushBrightnessChanges();
|
|
835
726
|
}
|
|
836
727
|
}
|
|
837
728
|
|
|
838
729
|
async pushHueSaturationChanges(): Promise<void> {
|
|
839
730
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushHueSaturationChanges`);
|
|
840
|
-
if (this.Hue !== this.accessory.context.Hue || this.Saturation !== this.accessory.context.Saturation) {
|
|
841
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${JSON.stringify(this.Hue)}`);
|
|
842
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${JSON.stringify(this.Saturation)}`);
|
|
843
|
-
const [red, green, blue] = hs2rgb(Number(this.Hue), Number(this.Saturation));
|
|
731
|
+
if (this.LightBulb.Hue !== this.accessory.context.Hue || this.LightBulb.Saturation !== this.accessory.context.Saturation) {
|
|
732
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${JSON.stringify(this.LightBulb.Hue)}`);
|
|
733
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${JSON.stringify(this.LightBulb.Saturation)}`);
|
|
734
|
+
const [red, green, blue] = hs2rgb(Number(this.LightBulb.Hue), Number(this.LightBulb.Saturation));
|
|
844
735
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} rgb: ${JSON.stringify([red, green, blue])}`);
|
|
845
736
|
const bodyChange = JSON.stringify({
|
|
846
737
|
command: 'setColor',
|
|
@@ -862,6 +753,8 @@ export class ColorBulb {
|
|
|
862
753
|
if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
|
|
863
754
|
this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
864
755
|
+ `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
|
|
756
|
+
this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
757
|
+
+ `request to SwitchBot API, body: ${JSON.stringify(deviceStatus)} sent successfully`);
|
|
865
758
|
} else {
|
|
866
759
|
this.statusCode(statusCode);
|
|
867
760
|
this.statusCode(deviceStatus.statusCode);
|
|
@@ -875,17 +768,17 @@ export class ColorBulb {
|
|
|
875
768
|
}
|
|
876
769
|
} else {
|
|
877
770
|
this.debugLog(
|
|
878
|
-
`${this.device.deviceType}: ${this.accessory.displayName} No pushHueSaturationChanges. Hue: ${this.Hue}, ` +
|
|
879
|
-
`HueCached: ${this.accessory.context.Hue}, Saturation: ${this.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`,
|
|
771
|
+
`${this.device.deviceType}: ${this.accessory.displayName} No pushHueSaturationChanges. Hue: ${this.LightBulb.Hue}, ` +
|
|
772
|
+
`HueCached: ${this.accessory.context.Hue}, Saturation: ${this.LightBulb.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`,
|
|
880
773
|
);
|
|
881
774
|
}
|
|
882
775
|
}
|
|
883
776
|
|
|
884
777
|
async pushColorTemperatureChanges(): Promise<void> {
|
|
885
778
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushColorTemperatureChanges`);
|
|
886
|
-
if (this.ColorTemperature !== this.accessory.context.ColorTemperature) {
|
|
887
|
-
const kelvin = Math.round(1000000 / Number(this.ColorTemperature));
|
|
888
|
-
this.
|
|
779
|
+
if (this.LightBulb.ColorTemperature !== this.accessory.context.ColorTemperature) {
|
|
780
|
+
const kelvin = Math.round(1000000 / Number(this.LightBulb.ColorTemperature));
|
|
781
|
+
this.accessory.context.kelvin = kelvin;
|
|
889
782
|
const bodyChange = JSON.stringify({
|
|
890
783
|
command: 'setColorTemperature',
|
|
891
784
|
parameter: `${kelvin}`,
|
|
@@ -906,31 +799,29 @@ export class ColorBulb {
|
|
|
906
799
|
if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
|
|
907
800
|
this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
908
801
|
+ `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
|
|
802
|
+
this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
803
|
+
+ `request to SwitchBot API, body: ${JSON.stringify(deviceStatus)} sent successfully`);
|
|
909
804
|
} else {
|
|
910
805
|
this.statusCode(statusCode);
|
|
911
806
|
this.statusCode(deviceStatus.statusCode);
|
|
912
807
|
}
|
|
913
808
|
} catch (e: any) {
|
|
914
809
|
this.apiError(e);
|
|
915
|
-
this.errorLog(
|
|
916
|
-
|
|
917
|
-
` Connection, Error Message: ${JSON.stringify(e.message)}`,
|
|
918
|
-
);
|
|
810
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushColorTemperatureChanges with ${this.device.connectionType}`
|
|
811
|
+
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
919
812
|
}
|
|
920
813
|
} else {
|
|
921
|
-
this.debugLog(
|
|
922
|
-
|
|
923
|
-
`ColorTemperature: ${this.ColorTemperature}, ColorTemperatureCached: ${this.accessory.context.ColorTemperature}`,
|
|
924
|
-
);
|
|
814
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushColorTemperatureChanges.`
|
|
815
|
+
+ `ColorTemperature: ${this.LightBulb.ColorTemperature}, ColorTemperatureCached: ${this.accessory.context.ColorTemperature}`);
|
|
925
816
|
}
|
|
926
817
|
}
|
|
927
818
|
|
|
928
819
|
async pushBrightnessChanges(): Promise<void> {
|
|
929
820
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushBrightnessChanges`);
|
|
930
|
-
if (this.Brightness !== this.accessory.context.Brightness) {
|
|
821
|
+
if (this.LightBulb.Brightness !== this.accessory.context.Brightness) {
|
|
931
822
|
const bodyChange = JSON.stringify({
|
|
932
823
|
command: 'setBrightness',
|
|
933
|
-
parameter: `${this.Brightness}`,
|
|
824
|
+
parameter: `${this.LightBulb.Brightness}`,
|
|
934
825
|
commandType: 'command',
|
|
935
826
|
});
|
|
936
827
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
|
|
@@ -948,6 +839,8 @@ export class ColorBulb {
|
|
|
948
839
|
if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
|
|
949
840
|
this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
950
841
|
+ `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
|
|
842
|
+
this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
843
|
+
+ `request to SwitchBot API, body: ${JSON.stringify(deviceStatus)} sent successfully`);
|
|
951
844
|
} else {
|
|
952
845
|
this.statusCode(statusCode);
|
|
953
846
|
this.statusCode(deviceStatus.statusCode);
|
|
@@ -962,7 +855,7 @@ export class ColorBulb {
|
|
|
962
855
|
} else {
|
|
963
856
|
this.debugLog(
|
|
964
857
|
`${this.device.deviceType}: ${this.accessory.displayName} No pushBrightnessChanges.` +
|
|
965
|
-
`Brightness: ${this.Brightness}, ` +
|
|
858
|
+
`Brightness: ${this.LightBulb.Brightness}, ` +
|
|
966
859
|
`BrightnessCached: ${this.accessory.context.Brightness}`,
|
|
967
860
|
);
|
|
968
861
|
}
|
|
@@ -972,13 +865,13 @@ export class ColorBulb {
|
|
|
972
865
|
* Handle requests to set the value of the "On" characteristic
|
|
973
866
|
*/
|
|
974
867
|
async OnSet(value: CharacteristicValue): Promise<void> {
|
|
975
|
-
if (this.On === this.accessory.context.On) {
|
|
868
|
+
if (this.LightBulb.On === this.accessory.context.On) {
|
|
976
869
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set On: ${value}`);
|
|
977
870
|
} else {
|
|
978
871
|
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set On: ${value}`);
|
|
979
872
|
}
|
|
980
873
|
|
|
981
|
-
this.On = value;
|
|
874
|
+
this.LightBulb.On = value;
|
|
982
875
|
this.doColorBulbUpdate.next();
|
|
983
876
|
}
|
|
984
877
|
|
|
@@ -986,15 +879,15 @@ export class ColorBulb {
|
|
|
986
879
|
* Handle requests to set the value of the "Brightness" characteristic
|
|
987
880
|
*/
|
|
988
881
|
async BrightnessSet(value: CharacteristicValue): Promise<void> {
|
|
989
|
-
if (this.Brightness === this.accessory.context.Brightness) {
|
|
882
|
+
if (this.LightBulb.Brightness === this.accessory.context.Brightness) {
|
|
990
883
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Brightness: ${value}`);
|
|
991
|
-
} else if (this.On) {
|
|
884
|
+
} else if (this.LightBulb.On) {
|
|
992
885
|
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Brightness: ${value}`);
|
|
993
886
|
} else {
|
|
994
887
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Brightness: ${value}`);
|
|
995
888
|
}
|
|
996
889
|
|
|
997
|
-
this.Brightness = value;
|
|
890
|
+
this.LightBulb.Brightness = value;
|
|
998
891
|
this.doColorBulbUpdate.next();
|
|
999
892
|
}
|
|
1000
893
|
|
|
@@ -1002,30 +895,32 @@ export class ColorBulb {
|
|
|
1002
895
|
* Handle requests to set the value of the "ColorTemperature" characteristic
|
|
1003
896
|
*/
|
|
1004
897
|
async ColorTemperatureSet(value: CharacteristicValue): Promise<void> {
|
|
1005
|
-
if (this.ColorTemperature === this.accessory.context.ColorTemperature) {
|
|
898
|
+
if (this.LightBulb.ColorTemperature === this.accessory.context.ColorTemperature) {
|
|
1006
899
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set ColorTemperature: ${value}`);
|
|
1007
|
-
} else if (this.On) {
|
|
900
|
+
} else if (this.LightBulb.On) {
|
|
1008
901
|
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set ColorTemperature: ${value}`);
|
|
1009
902
|
} else {
|
|
1010
903
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set ColorTemperature: ${value}`);
|
|
1011
904
|
}
|
|
1012
905
|
|
|
906
|
+
const minKelvin = 2000;
|
|
907
|
+
const maxKelvin = 9000;
|
|
1013
908
|
// Convert mired to kelvin to nearest 100 (SwitchBot seems to need this)
|
|
1014
909
|
const kelvin = Math.round(1000000 / Number(value) / 100) * 100;
|
|
1015
910
|
|
|
1016
911
|
// Check and increase/decrease kelvin to range of device
|
|
1017
|
-
const k = Math.min(Math.max(kelvin,
|
|
912
|
+
const k = Math.min(Math.max(kelvin, minKelvin), maxKelvin);
|
|
1018
913
|
|
|
1019
|
-
if (!this.accessory.context.On || this.
|
|
914
|
+
if (!this.accessory.context.On || this.accessory.context.kelvin === k) {
|
|
1020
915
|
return;
|
|
1021
916
|
}
|
|
1022
917
|
|
|
1023
918
|
// Updating the hue/sat to the corresponding values mimics native adaptive lighting
|
|
1024
919
|
const hs = m2hs(value);
|
|
1025
|
-
this.
|
|
1026
|
-
this.
|
|
920
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Hue, hs[0]);
|
|
921
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Saturation, hs[1]);
|
|
1027
922
|
|
|
1028
|
-
this.ColorTemperature = value;
|
|
923
|
+
this.LightBulb.ColorTemperature = value;
|
|
1029
924
|
this.doColorBulbUpdate.next();
|
|
1030
925
|
}
|
|
1031
926
|
|
|
@@ -1033,17 +928,17 @@ export class ColorBulb {
|
|
|
1033
928
|
* Handle requests to set the value of the "Hue" characteristic
|
|
1034
929
|
*/
|
|
1035
930
|
async HueSet(value: CharacteristicValue): Promise<void> {
|
|
1036
|
-
if (this.Hue === this.accessory.context.Hue) {
|
|
931
|
+
if (this.LightBulb.Hue === this.accessory.context.Hue) {
|
|
1037
932
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Hue: ${value}`);
|
|
1038
|
-
} else if (this.On) {
|
|
933
|
+
} else if (this.LightBulb.On) {
|
|
1039
934
|
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Hue: ${value}`);
|
|
1040
935
|
} else {
|
|
1041
936
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Hue: ${value}`);
|
|
1042
937
|
}
|
|
1043
938
|
|
|
1044
|
-
this.
|
|
939
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, 140);
|
|
1045
940
|
|
|
1046
|
-
this.Hue = value;
|
|
941
|
+
this.LightBulb.Hue = value;
|
|
1047
942
|
this.doColorBulbUpdate.next();
|
|
1048
943
|
}
|
|
1049
944
|
|
|
@@ -1051,60 +946,61 @@ export class ColorBulb {
|
|
|
1051
946
|
* Handle requests to set the value of the "Saturation" characteristic
|
|
1052
947
|
*/
|
|
1053
948
|
async SaturationSet(value: CharacteristicValue): Promise<void> {
|
|
1054
|
-
if (this.Saturation === this.accessory.context.Saturation) {
|
|
949
|
+
if (this.LightBulb.Saturation === this.accessory.context.Saturation) {
|
|
1055
950
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Saturation: ${value}`);
|
|
1056
|
-
} else if (this.On) {
|
|
951
|
+
} else if (this.LightBulb.On) {
|
|
1057
952
|
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Saturation: ${value}`);
|
|
1058
953
|
} else {
|
|
1059
954
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Saturation: ${value}`);
|
|
1060
955
|
}
|
|
1061
956
|
|
|
1062
|
-
this.
|
|
957
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, 140);
|
|
1063
958
|
|
|
1064
|
-
this.Saturation = value;
|
|
959
|
+
this.LightBulb.Saturation = value;
|
|
1065
960
|
this.doColorBulbUpdate.next();
|
|
1066
961
|
}
|
|
1067
962
|
|
|
1068
963
|
async updateHomeKitCharacteristics(): Promise<void> {
|
|
1069
964
|
// On
|
|
1070
|
-
if (this.On === undefined) {
|
|
1071
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
|
|
965
|
+
if (this.LightBulb.On === undefined) {
|
|
966
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
|
|
1072
967
|
} else {
|
|
1073
|
-
this.accessory.context.On = this.On;
|
|
1074
|
-
this.
|
|
1075
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
|
|
968
|
+
this.accessory.context.On = this.LightBulb.On;
|
|
969
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.On, this.LightBulb.On);
|
|
970
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.LightBulb.On}`);
|
|
1076
971
|
}
|
|
1077
972
|
// Brightness
|
|
1078
|
-
if (this.Brightness === undefined) {
|
|
1079
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.Brightness}`);
|
|
973
|
+
if (this.LightBulb.Brightness === undefined) {
|
|
974
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
|
|
1080
975
|
} else {
|
|
1081
|
-
this.accessory.context.Brightness = this.Brightness;
|
|
1082
|
-
this.
|
|
1083
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Brightness: ${this.Brightness}`);
|
|
976
|
+
this.accessory.context.Brightness = this.LightBulb.Brightness;
|
|
977
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Brightness, this.LightBulb.Brightness);
|
|
978
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Brightness: ${this.LightBulb.Brightness}`);
|
|
1084
979
|
}
|
|
1085
980
|
// ColorTemperature
|
|
1086
|
-
if (this.ColorTemperature === undefined) {
|
|
1087
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${this.ColorTemperature}`);
|
|
981
|
+
if (this.LightBulb.ColorTemperature === undefined) {
|
|
982
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${this.LightBulb.ColorTemperature}`);
|
|
1088
983
|
} else {
|
|
1089
|
-
this.accessory.context.ColorTemperature = this.ColorTemperature;
|
|
1090
|
-
this.
|
|
1091
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic
|
|
984
|
+
this.accessory.context.ColorTemperature = this.LightBulb.ColorTemperature;
|
|
985
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, this.LightBulb.ColorTemperature);
|
|
986
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
|
|
987
|
+
+ ` ColorTemperature: ${this.LightBulb.ColorTemperature}`);
|
|
1092
988
|
}
|
|
1093
989
|
// Hue
|
|
1094
|
-
if (this.Hue === undefined) {
|
|
1095
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.Hue}`);
|
|
990
|
+
if (this.LightBulb.Hue === undefined) {
|
|
991
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.LightBulb.Hue}`);
|
|
1096
992
|
} else {
|
|
1097
|
-
this.accessory.context.Hue = this.Hue;
|
|
1098
|
-
this.
|
|
1099
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Hue: ${this.Hue}`);
|
|
993
|
+
this.accessory.context.Hue = this.LightBulb.Hue;
|
|
994
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Hue, this.LightBulb.Hue);
|
|
995
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Hue: ${this.LightBulb.Hue}`);
|
|
1100
996
|
}
|
|
1101
997
|
// Saturation
|
|
1102
|
-
if (this.Saturation === undefined) {
|
|
1103
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.Saturation}`);
|
|
998
|
+
if (this.LightBulb.Saturation === undefined) {
|
|
999
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.LightBulb.Saturation}`);
|
|
1104
1000
|
} else {
|
|
1105
|
-
this.accessory.context.Saturation = this.Saturation;
|
|
1106
|
-
this.
|
|
1107
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Saturation: ${this.Saturation}`);
|
|
1001
|
+
this.accessory.context.Saturation = this.LightBulb.Saturation;
|
|
1002
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Saturation, this.LightBulb.Saturation);
|
|
1003
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Saturation: ${this.LightBulb.Saturation}`);
|
|
1108
1004
|
}
|
|
1109
1005
|
}
|
|
1110
1006
|
|
|
@@ -1118,35 +1014,6 @@ export class ColorBulb {
|
|
|
1118
1014
|
}
|
|
1119
1015
|
}
|
|
1120
1016
|
|
|
1121
|
-
async stopScanning(switchbot: any) {
|
|
1122
|
-
switchbot.stopScan();
|
|
1123
|
-
if (this.BLE_IsConnected) {
|
|
1124
|
-
await this.BLEparseStatus();
|
|
1125
|
-
await this.updateHomeKitCharacteristics();
|
|
1126
|
-
} else {
|
|
1127
|
-
await this.BLERefreshConnection(switchbot);
|
|
1128
|
-
}
|
|
1129
|
-
}
|
|
1130
|
-
|
|
1131
|
-
async getCustomBLEAddress(switchbot: any) {
|
|
1132
|
-
if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
|
|
1133
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} customBLEaddress: ${this.device.customBLEaddress}`);
|
|
1134
|
-
(async () => {
|
|
1135
|
-
// Start to monitor advertisement packets
|
|
1136
|
-
await switchbot.startScan({
|
|
1137
|
-
model: 'u',
|
|
1138
|
-
});
|
|
1139
|
-
// Set an event handler
|
|
1140
|
-
switchbot.onadvertisement = (ad: any) => {
|
|
1141
|
-
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
|
|
1142
|
-
};
|
|
1143
|
-
await sleep(10000);
|
|
1144
|
-
// Stop to monitor
|
|
1145
|
-
switchbot.stopScan();
|
|
1146
|
-
})();
|
|
1147
|
-
}
|
|
1148
|
-
}
|
|
1149
|
-
|
|
1150
1017
|
async BLEPushConnection() {
|
|
1151
1018
|
if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
|
|
1152
1019
|
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Push Changes`);
|
|
@@ -1163,284 +1030,27 @@ export class ColorBulb {
|
|
|
1163
1030
|
}
|
|
1164
1031
|
}
|
|
1165
1032
|
|
|
1166
|
-
async retry({ max, fn }: { max: number; fn: { (): any; (): Promise<any> } }): Promise<null> {
|
|
1167
|
-
return fn().catch(async (e: any) => {
|
|
1168
|
-
if (max === 0) {
|
|
1169
|
-
throw e;
|
|
1170
|
-
}
|
|
1171
|
-
this.infoLog(e);
|
|
1172
|
-
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Retrying`);
|
|
1173
|
-
await sleep(1000);
|
|
1174
|
-
return this.retry({ max: max - 1, fn });
|
|
1175
|
-
});
|
|
1176
|
-
}
|
|
1177
|
-
|
|
1178
|
-
maxRetry(): number {
|
|
1179
|
-
if (this.device.maxRetry) {
|
|
1180
|
-
return this.device.maxRetry;
|
|
1181
|
-
} else {
|
|
1182
|
-
return 5;
|
|
1183
|
-
}
|
|
1184
|
-
}
|
|
1185
|
-
|
|
1186
1033
|
minStep(device: device & devicesConfig): number {
|
|
1034
|
+
let set_minStep: number;
|
|
1187
1035
|
if (device.colorbulb?.set_minStep) {
|
|
1188
|
-
|
|
1189
|
-
} else {
|
|
1190
|
-
this.set_minStep = 1;
|
|
1191
|
-
}
|
|
1192
|
-
return this.set_minStep;
|
|
1193
|
-
}
|
|
1194
|
-
|
|
1195
|
-
async scan(device: device & devicesConfig): Promise<void> {
|
|
1196
|
-
if (device.scanDuration) {
|
|
1197
|
-
this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
|
|
1198
|
-
if (this.BLE) {
|
|
1199
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
|
|
1200
|
-
}
|
|
1036
|
+
set_minStep = device.colorbulb?.set_minStep;
|
|
1201
1037
|
} else {
|
|
1202
|
-
|
|
1203
|
-
if (this.BLE) {
|
|
1204
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
|
|
1205
|
-
}
|
|
1206
|
-
}
|
|
1207
|
-
}
|
|
1208
|
-
|
|
1209
|
-
async statusCode(statusCode: number): Promise<void> {
|
|
1210
|
-
switch (statusCode) {
|
|
1211
|
-
case 151:
|
|
1212
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this deviceType, statusCode: ${statusCode}`);
|
|
1213
|
-
break;
|
|
1214
|
-
case 152:
|
|
1215
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found, statusCode: ${statusCode}`);
|
|
1216
|
-
break;
|
|
1217
|
-
case 160:
|
|
1218
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported, statusCode: ${statusCode}`);
|
|
1219
|
-
break;
|
|
1220
|
-
case 161:
|
|
1221
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline, statusCode: ${statusCode}`);
|
|
1222
|
-
this.offlineOff();
|
|
1223
|
-
break;
|
|
1224
|
-
case 171:
|
|
1225
|
-
this.errorLog(
|
|
1226
|
-
`${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. ` +
|
|
1227
|
-
`Hub: ${this.device.hubDeviceId}`,
|
|
1228
|
-
);
|
|
1229
|
-
this.offlineOff();
|
|
1230
|
-
break;
|
|
1231
|
-
case 190:
|
|
1232
|
-
this.errorLog(
|
|
1233
|
-
`${this.device.deviceType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
|
|
1234
|
-
` Or command format is invalid, statusCode: ${statusCode}`,
|
|
1235
|
-
);
|
|
1236
|
-
break;
|
|
1237
|
-
case 100:
|
|
1238
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent, statusCode: ${statusCode}`);
|
|
1239
|
-
break;
|
|
1240
|
-
case 200:
|
|
1241
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Request successful, statusCode: ${statusCode}`);
|
|
1242
|
-
break;
|
|
1243
|
-
case 400:
|
|
1244
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Bad Request, The client has issued an invalid request. `
|
|
1245
|
-
+ `This is commonly used to specify validation errors in a request payload, statusCode: ${statusCode}`);
|
|
1246
|
-
break;
|
|
1247
|
-
case 401:
|
|
1248
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unauthorized, Authorization for the API is required, `
|
|
1249
|
-
+ `but the request has not been authenticated, statusCode: ${statusCode}`);
|
|
1250
|
-
break;
|
|
1251
|
-
case 403:
|
|
1252
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Forbidden, The request has been authenticated but does not `
|
|
1253
|
-
+ `have appropriate permissions, or a requested resource is not found, statusCode: ${statusCode}`);
|
|
1254
|
-
break;
|
|
1255
|
-
case 404:
|
|
1256
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Found, Specifies the requested path does not exist, `
|
|
1257
|
-
+ `statusCode: ${statusCode}`);
|
|
1258
|
-
break;
|
|
1259
|
-
case 406:
|
|
1260
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Acceptable, The client has requested a MIME type via `
|
|
1261
|
-
+ `the Accept header for a value not supported by the server, statusCode: ${statusCode}`);
|
|
1262
|
-
break;
|
|
1263
|
-
case 415:
|
|
1264
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unsupported Media Type, The client has defined a contentType `
|
|
1265
|
-
+ `header that is not supported by the server, statusCode: ${statusCode}`);
|
|
1266
|
-
break;
|
|
1267
|
-
case 422:
|
|
1268
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unprocessable Entity, The client has made a valid request, `
|
|
1269
|
-
+ `but the server cannot process it. This is often used for APIs for which certain limits have been exceeded, statusCode: ${statusCode}`);
|
|
1270
|
-
break;
|
|
1271
|
-
case 429:
|
|
1272
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Too Many Requests, The client has exceeded the number of `
|
|
1273
|
-
+ `requests allowed for a given time window, statusCode: ${statusCode}`);
|
|
1274
|
-
break;
|
|
1275
|
-
case 500:
|
|
1276
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Internal Server Error, An unexpected error on the SmartThings `
|
|
1277
|
-
+ `servers has occurred. These errors should be rare, statusCode: ${statusCode}`);
|
|
1278
|
-
break;
|
|
1279
|
-
default:
|
|
1280
|
-
this.infoLog(
|
|
1281
|
-
`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: ` +
|
|
1282
|
-
`${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`,
|
|
1283
|
-
);
|
|
1038
|
+
set_minStep = 1;
|
|
1284
1039
|
}
|
|
1040
|
+
return set_minStep;
|
|
1285
1041
|
}
|
|
1286
1042
|
|
|
1287
1043
|
async offlineOff(): Promise<void> {
|
|
1288
1044
|
if (this.device.offline) {
|
|
1289
|
-
|
|
1290
|
-
await this.updateHomeKitCharacteristics();
|
|
1045
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.On, false);
|
|
1291
1046
|
}
|
|
1292
1047
|
}
|
|
1293
1048
|
|
|
1294
1049
|
apiError(e: any): void {
|
|
1295
|
-
this.
|
|
1296
|
-
this.
|
|
1297
|
-
this.
|
|
1298
|
-
this.
|
|
1299
|
-
this.
|
|
1300
|
-
}
|
|
1301
|
-
|
|
1302
|
-
async deviceContext(): Promise<void> {
|
|
1303
|
-
if (this.On === undefined) {
|
|
1304
|
-
this.On = false;
|
|
1305
|
-
} else {
|
|
1306
|
-
this.On = this.accessory.context.On;
|
|
1307
|
-
}
|
|
1308
|
-
if (this.Hue === undefined) {
|
|
1309
|
-
this.Hue = 0;
|
|
1310
|
-
} else {
|
|
1311
|
-
this.Hue = this.accessory.context.Hue;
|
|
1312
|
-
}
|
|
1313
|
-
if (this.Brightness === undefined) {
|
|
1314
|
-
this.Brightness = 0;
|
|
1315
|
-
} else {
|
|
1316
|
-
this.Brightness = this.accessory.context.Brightness;
|
|
1317
|
-
}
|
|
1318
|
-
if (this.Saturation === undefined) {
|
|
1319
|
-
this.Saturation = 0;
|
|
1320
|
-
} else {
|
|
1321
|
-
this.Saturation = this.accessory.context.Saturation;
|
|
1322
|
-
}
|
|
1323
|
-
if (this.ColorTemperature === undefined) {
|
|
1324
|
-
this.ColorTemperature = 140;
|
|
1325
|
-
} else {
|
|
1326
|
-
this.ColorTemperature = this.accessory.context.ColorTemperature;
|
|
1327
|
-
}
|
|
1328
|
-
this.minKelvin = 2000;
|
|
1329
|
-
this.maxKelvin = 9000;
|
|
1330
|
-
if (this.FirmwareRevision === undefined) {
|
|
1331
|
-
this.FirmwareRevision = this.platform.version;
|
|
1332
|
-
this.accessory.context.FirmwareRevision = this.FirmwareRevision;
|
|
1333
|
-
}
|
|
1334
|
-
}
|
|
1335
|
-
|
|
1336
|
-
async refreshRate(device: device & devicesConfig): Promise<void> {
|
|
1337
|
-
if (device.refreshRate) {
|
|
1338
|
-
this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
|
|
1339
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
|
|
1340
|
-
} else if (this.platform.config.options!.refreshRate) {
|
|
1341
|
-
this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options!.refreshRate;
|
|
1342
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
|
|
1343
|
-
}
|
|
1344
|
-
}
|
|
1345
|
-
|
|
1346
|
-
async deviceConfig(device: device & devicesConfig): Promise<void> {
|
|
1347
|
-
let config = {};
|
|
1348
|
-
if (device.colorbulb) {
|
|
1349
|
-
config = device.colorbulb;
|
|
1350
|
-
}
|
|
1351
|
-
if (device.connectionType !== undefined) {
|
|
1352
|
-
config['connectionType'] = device.connectionType;
|
|
1353
|
-
}
|
|
1354
|
-
if (device.external !== undefined) {
|
|
1355
|
-
config['external'] = device.external;
|
|
1356
|
-
}
|
|
1357
|
-
if (device.logging !== undefined) {
|
|
1358
|
-
config['logging'] = device.logging;
|
|
1359
|
-
}
|
|
1360
|
-
if (device.refreshRate !== undefined) {
|
|
1361
|
-
config['refreshRate'] = device.refreshRate;
|
|
1362
|
-
}
|
|
1363
|
-
if (device.scanDuration !== undefined) {
|
|
1364
|
-
config['scanDuration'] = device.scanDuration;
|
|
1365
|
-
}
|
|
1366
|
-
if (device.offline !== undefined) {
|
|
1367
|
-
config['offline'] = device.offline;
|
|
1368
|
-
}
|
|
1369
|
-
if (device.maxRetry !== undefined) {
|
|
1370
|
-
config['maxRetry'] = device.maxRetry;
|
|
1371
|
-
}
|
|
1372
|
-
if (device.webhook !== undefined) {
|
|
1373
|
-
config['webhook'] = device.webhook;
|
|
1374
|
-
}
|
|
1375
|
-
if (Object.entries(config).length !== 0) {
|
|
1376
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
|
|
1377
|
-
}
|
|
1378
|
-
}
|
|
1379
|
-
|
|
1380
|
-
async deviceLogs(device: device & devicesConfig): Promise<void> {
|
|
1381
|
-
if (this.platform.debugMode) {
|
|
1382
|
-
this.deviceLogging = this.accessory.context.logging = 'debugMode';
|
|
1383
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
|
|
1384
|
-
} else if (device.logging) {
|
|
1385
|
-
this.deviceLogging = this.accessory.context.logging = device.logging;
|
|
1386
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
|
|
1387
|
-
} else if (this.platform.config.options?.logging) {
|
|
1388
|
-
this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
|
|
1389
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
|
|
1390
|
-
} else {
|
|
1391
|
-
this.deviceLogging = this.accessory.context.logging = 'standard';
|
|
1392
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
|
|
1393
|
-
}
|
|
1394
|
-
}
|
|
1395
|
-
|
|
1396
|
-
/**
|
|
1397
|
-
* Logging for Device
|
|
1398
|
-
*/
|
|
1399
|
-
infoLog(...log: any[]): void {
|
|
1400
|
-
if (this.enablingDeviceLogging()) {
|
|
1401
|
-
this.platform.log.info(String(...log));
|
|
1402
|
-
}
|
|
1403
|
-
}
|
|
1404
|
-
|
|
1405
|
-
warnLog(...log: any[]): void {
|
|
1406
|
-
if (this.enablingDeviceLogging()) {
|
|
1407
|
-
this.platform.log.warn(String(...log));
|
|
1408
|
-
}
|
|
1409
|
-
}
|
|
1410
|
-
|
|
1411
|
-
debugWarnLog(...log: any[]): void {
|
|
1412
|
-
if (this.enablingDeviceLogging()) {
|
|
1413
|
-
if (this.deviceLogging?.includes('debug')) {
|
|
1414
|
-
this.platform.log.warn('[DEBUG]', String(...log));
|
|
1415
|
-
}
|
|
1416
|
-
}
|
|
1417
|
-
}
|
|
1418
|
-
|
|
1419
|
-
errorLog(...log: any[]): void {
|
|
1420
|
-
if (this.enablingDeviceLogging()) {
|
|
1421
|
-
this.platform.log.error(String(...log));
|
|
1422
|
-
}
|
|
1423
|
-
}
|
|
1424
|
-
|
|
1425
|
-
debugErrorLog(...log: any[]): void {
|
|
1426
|
-
if (this.enablingDeviceLogging()) {
|
|
1427
|
-
if (this.deviceLogging?.includes('debug')) {
|
|
1428
|
-
this.platform.log.error('[DEBUG]', String(...log));
|
|
1429
|
-
}
|
|
1430
|
-
}
|
|
1431
|
-
}
|
|
1432
|
-
|
|
1433
|
-
debugLog(...log: any[]): void {
|
|
1434
|
-
if (this.enablingDeviceLogging()) {
|
|
1435
|
-
if (this.deviceLogging === 'debug') {
|
|
1436
|
-
this.platform.log.info('[DEBUG]', String(...log));
|
|
1437
|
-
} else {
|
|
1438
|
-
this.platform.log.debug(String(...log));
|
|
1439
|
-
}
|
|
1440
|
-
}
|
|
1441
|
-
}
|
|
1442
|
-
|
|
1443
|
-
enablingDeviceLogging(): boolean {
|
|
1444
|
-
return this.deviceLogging.includes('debug') || this.deviceLogging === 'standard';
|
|
1050
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.On, e);
|
|
1051
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Hue, e);
|
|
1052
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Brightness, e);
|
|
1053
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Saturation, e);
|
|
1054
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, e);
|
|
1445
1055
|
}
|
|
1446
1056
|
}
|