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