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