@switchbot/homebridge-switchbot 3.5.1-beta.0 → 3.5.1-beta.10
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/README.md +16 -0
- package/dist/device/blindtilt.d.ts +15 -6
- package/dist/device/blindtilt.d.ts.map +1 -1
- package/dist/device/blindtilt.js +224 -370
- package/dist/device/blindtilt.js.map +1 -1
- package/dist/device/bot.d.ts +9 -4
- package/dist/device/bot.d.ts.map +1 -1
- package/dist/device/bot.js +197 -221
- package/dist/device/bot.js.map +1 -1
- package/dist/device/ceilinglight.d.ts +10 -5
- package/dist/device/ceilinglight.d.ts.map +1 -1
- package/dist/device/ceilinglight.js +214 -292
- package/dist/device/ceilinglight.js.map +1 -1
- package/dist/device/colorbulb.d.ts +10 -5
- package/dist/device/colorbulb.d.ts.map +1 -1
- package/dist/device/colorbulb.js +335 -421
- package/dist/device/colorbulb.js.map +1 -1
- package/dist/device/contact.d.ts +9 -4
- package/dist/device/contact.d.ts.map +1 -1
- package/dist/device/contact.js +121 -200
- package/dist/device/contact.js.map +1 -1
- package/dist/device/curtain.d.ts +20 -7
- package/dist/device/curtain.d.ts.map +1 -1
- package/dist/device/curtain.js +229 -403
- package/dist/device/curtain.js.map +1 -1
- package/dist/device/device.d.ts +41 -22
- package/dist/device/device.d.ts.map +1 -1
- package/dist/device/device.js +212 -181
- package/dist/device/device.js.map +1 -1
- package/dist/device/fan.d.ts +9 -4
- package/dist/device/fan.d.ts.map +1 -1
- package/dist/device/fan.js +204 -249
- package/dist/device/fan.js.map +1 -1
- package/dist/device/hub.d.ts +9 -5
- package/dist/device/hub.d.ts.map +1 -1
- package/dist/device/hub.js +107 -233
- package/dist/device/hub.js.map +1 -1
- package/dist/device/humidifier.d.ts +12 -4
- package/dist/device/humidifier.d.ts.map +1 -1
- package/dist/device/humidifier.js +193 -235
- package/dist/device/humidifier.js.map +1 -1
- package/dist/device/iosensor.d.ts +9 -4
- package/dist/device/iosensor.d.ts.map +1 -1
- package/dist/device/iosensor.js +92 -159
- package/dist/device/iosensor.js.map +1 -1
- package/dist/device/lightstrip.d.ts +10 -5
- package/dist/device/lightstrip.d.ts.map +1 -1
- package/dist/device/lightstrip.js +288 -370
- package/dist/device/lightstrip.js.map +1 -1
- package/dist/device/lock.d.ts +10 -5
- package/dist/device/lock.d.ts.map +1 -1
- package/dist/device/lock.js +172 -219
- package/dist/device/lock.js.map +1 -1
- package/dist/device/meter.d.ts +9 -4
- package/dist/device/meter.d.ts.map +1 -1
- package/dist/device/meter.js +94 -160
- package/dist/device/meter.js.map +1 -1
- package/dist/device/meterplus.d.ts +9 -4
- package/dist/device/meterplus.d.ts.map +1 -1
- package/dist/device/meterplus.js +95 -157
- package/dist/device/meterplus.js.map +1 -1
- package/dist/device/motion.d.ts +9 -4
- package/dist/device/motion.d.ts.map +1 -1
- package/dist/device/motion.js +85 -144
- package/dist/device/motion.js.map +1 -1
- package/dist/device/plug.d.ts +9 -4
- package/dist/device/plug.d.ts.map +1 -1
- package/dist/device/plug.js +100 -127
- package/dist/device/plug.js.map +1 -1
- package/dist/device/robotvacuumcleaner.d.ts +9 -6
- package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
- package/dist/device/robotvacuumcleaner.js +172 -259
- package/dist/device/robotvacuumcleaner.js.map +1 -1
- package/dist/device/waterdetector.d.ts +12 -4
- package/dist/device/waterdetector.d.ts.map +1 -1
- package/dist/device/waterdetector.js +106 -131
- package/dist/device/waterdetector.js.map +1 -1
- package/dist/irdevice/airconditioner.d.ts +2 -1
- package/dist/irdevice/airconditioner.d.ts.map +1 -1
- package/dist/irdevice/airconditioner.js +2 -3
- package/dist/irdevice/airconditioner.js.map +1 -1
- package/dist/irdevice/airpurifier.d.ts +2 -1
- package/dist/irdevice/airpurifier.d.ts.map +1 -1
- package/dist/irdevice/airpurifier.js +2 -3
- package/dist/irdevice/airpurifier.js.map +1 -1
- package/dist/irdevice/camera.d.ts +2 -1
- package/dist/irdevice/camera.d.ts.map +1 -1
- package/dist/irdevice/camera.js +2 -3
- package/dist/irdevice/camera.js.map +1 -1
- package/dist/irdevice/fan.d.ts +2 -1
- package/dist/irdevice/fan.d.ts.map +1 -1
- package/dist/irdevice/fan.js +2 -3
- package/dist/irdevice/fan.js.map +1 -1
- package/dist/irdevice/irdevice.d.ts +3 -1
- package/dist/irdevice/irdevice.d.ts.map +1 -1
- package/dist/irdevice/irdevice.js +5 -1
- package/dist/irdevice/irdevice.js.map +1 -1
- package/dist/irdevice/light.d.ts +2 -1
- package/dist/irdevice/light.d.ts.map +1 -1
- package/dist/irdevice/light.js +2 -3
- package/dist/irdevice/light.js.map +1 -1
- package/dist/irdevice/other.d.ts +2 -1
- package/dist/irdevice/other.d.ts.map +1 -1
- package/dist/irdevice/other.js +2 -3
- package/dist/irdevice/other.js.map +1 -1
- package/dist/irdevice/tv.d.ts +2 -1
- package/dist/irdevice/tv.d.ts.map +1 -1
- package/dist/irdevice/tv.js +2 -3
- package/dist/irdevice/tv.js.map +1 -1
- package/dist/irdevice/vacuumcleaner.d.ts +2 -1
- package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
- package/dist/irdevice/vacuumcleaner.js +2 -3
- package/dist/irdevice/vacuumcleaner.js.map +1 -1
- package/dist/irdevice/waterheater.d.ts +2 -1
- package/dist/irdevice/waterheater.d.ts.map +1 -1
- package/dist/irdevice/waterheater.js +2 -3
- package/dist/irdevice/waterheater.js.map +1 -1
- package/dist/platform.d.ts +14 -14
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +102 -86
- package/dist/platform.js.map +1 -1
- package/dist/settings.d.ts +49 -190
- package/dist/settings.d.ts.map +1 -1
- package/dist/settings.js +19 -0
- package/dist/settings.js.map +1 -1
- package/dist/types/bledevicestatus.d.ts +279 -0
- package/dist/types/bledevicestatus.d.ts.map +1 -0
- package/dist/types/bledevicestatus.js +2 -0
- package/dist/types/bledevicestatus.js.map +1 -0
- package/dist/types/devicelist.d.ts +89 -0
- package/dist/types/devicelist.d.ts.map +1 -0
- package/dist/types/devicelist.js +6 -0
- package/dist/types/devicelist.js.map +1 -0
- package/dist/types/deviceresponse.d.ts +13 -0
- package/dist/types/deviceresponse.d.ts.map +1 -0
- package/dist/types/deviceresponse.js +2 -0
- package/dist/types/deviceresponse.js.map +1 -0
- package/dist/types/devicestatus.d.ts +149 -0
- package/dist/types/devicestatus.d.ts.map +1 -0
- package/dist/types/devicestatus.js +3 -0
- package/dist/types/devicestatus.js.map +1 -0
- package/dist/types/devicewebhookstatus.d.ts +156 -0
- package/dist/types/devicewebhookstatus.d.ts.map +1 -0
- package/dist/types/devicewebhookstatus.js +2 -0
- package/dist/types/devicewebhookstatus.js.map +1 -0
- package/dist/types/irdevicelist.d.ts +10 -0
- package/dist/types/irdevicelist.d.ts.map +1 -0
- package/dist/types/irdevicelist.js +2 -0
- package/dist/types/irdevicelist.js.map +1 -0
- package/dist/types/pushbody.d.ts +6 -0
- package/dist/types/pushbody.d.ts.map +1 -0
- package/dist/types/pushbody.js +2 -0
- package/dist/types/pushbody.js.map +1 -0
- package/dist/utils.d.ts +25 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +28 -0
- package/dist/utils.js.map +1 -1
- package/package.json +11 -11
- package/src/device/blindtilt.ts +254 -376
- package/src/device/bot.ts +209 -224
- package/src/device/ceilinglight.ts +227 -297
- package/src/device/colorbulb.ts +345 -435
- package/src/device/contact.ts +139 -200
- package/src/device/curtain.ts +254 -414
- package/src/device/device.ts +221 -190
- package/src/device/fan.ts +222 -253
- package/src/device/hub.ts +127 -248
- package/src/device/humidifier.ts +217 -246
- package/src/device/iosensor.ts +115 -168
- package/src/device/lightstrip.ts +303 -380
- package/src/device/lock.ts +188 -224
- package/src/device/meter.ts +117 -179
- package/src/device/meterplus.ts +118 -176
- package/src/device/motion.ts +100 -146
- package/src/device/plug.ts +111 -131
- package/src/device/robotvacuumcleaner.ts +191 -258
- package/src/device/waterdetector.ts +124 -141
- package/src/irdevice/airconditioner.ts +4 -4
- package/src/irdevice/airpurifier.ts +4 -4
- package/src/irdevice/camera.ts +4 -4
- package/src/irdevice/fan.ts +4 -4
- package/src/irdevice/irdevice.ts +8 -2
- package/src/irdevice/light.ts +4 -4
- package/src/irdevice/other.ts +4 -4
- package/src/irdevice/tv.ts +4 -4
- package/src/irdevice/vacuumcleaner.ts +4 -4
- package/src/irdevice/waterheater.ts +4 -4
- package/src/platform.ts +104 -87
- package/src/settings.ts +30 -238
- package/src/types/bledevicestatus.ts +305 -0
- package/src/types/devicelist.ts +124 -0
- package/src/types/deviceresponse.ts +15 -0
- package/src/types/devicestatus.ts +178 -0
- package/src/types/devicewebhookstatus.ts +196 -0
- package/src/types/irdevicelist.ts +15 -0
- package/src/types/pushbody.ts +9 -0
- package/src/utils.ts +33 -0
|
@@ -3,12 +3,16 @@
|
|
|
3
3
|
* ceilinglight.ts: @switchbot/homebridge-switchbot.
|
|
4
4
|
*/
|
|
5
5
|
import { deviceBase } from './device.js';
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import { debounceTime, skipWhile, take, tap } from 'rxjs
|
|
6
|
+
import { hs2rgb, m2hs } from '../utils.js';
|
|
7
|
+
import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot';
|
|
8
|
+
import { Subject, debounceTime, interval, skipWhile, take, tap } from 'rxjs';
|
|
9
9
|
|
|
10
|
+
import type { devicesConfig } from '../settings.js';
|
|
11
|
+
import type { device } from '../types/devicelist.js';
|
|
10
12
|
import type { SwitchBotPlatform } from '../platform.js';
|
|
11
|
-
import type {
|
|
13
|
+
import type { ceilingLightServiceData, ceilingLightProServiceData } from '../types/bledevicestatus.js';
|
|
14
|
+
import type { ceilingLightStatus, ceilingLightProStatus } from '../types/devicestatus.js';
|
|
15
|
+
import type { ceilingLightProWebhookContext, ceilingLightWebhookContext } from '../types/devicewebhookstatus.js';
|
|
12
16
|
import type { Service, PlatformAccessory, CharacteristicValue, ControllerConstructor, Controller, ControllerServiceMap } from 'homebridge';
|
|
13
17
|
|
|
14
18
|
/**
|
|
@@ -52,7 +56,7 @@ export class CeilingLight extends deviceBase {
|
|
|
52
56
|
// Initialize LightBulb Service
|
|
53
57
|
accessory.context.LightBulb = accessory.context.LightBulb ?? {};
|
|
54
58
|
this.LightBulb = {
|
|
55
|
-
Name: accessory.context.
|
|
59
|
+
Name: accessory.context.LightBulb.Name ?? accessory.displayName,
|
|
56
60
|
Service: accessory.getService(this.hap.Service.Lightbulb) ?? accessory.addService(this.hap.Service.Lightbulb) as Service,
|
|
57
61
|
On: accessory.context.On ?? false,
|
|
58
62
|
Hue: accessory.context.Hue ?? 0,
|
|
@@ -67,7 +71,7 @@ export class CeilingLight extends deviceBase {
|
|
|
67
71
|
accessory.removeService(this.LightBulb.Service);
|
|
68
72
|
this.LightBulb.Service = accessory.addService(this.hap.Service.Lightbulb);
|
|
69
73
|
accessory.context.adaptiveLighting = false;
|
|
70
|
-
this.debugLog(
|
|
74
|
+
this.debugLog(`adaptiveLighting: ${accessory.context.adaptiveLighting}`);
|
|
71
75
|
}
|
|
72
76
|
if (this.adaptiveLightingShift !== -1) {
|
|
73
77
|
this.AdaptiveLightingController = new platform.api.hap.AdaptiveLightingController(this.LightBulb.Service, {
|
|
@@ -75,10 +79,10 @@ export class CeilingLight extends deviceBase {
|
|
|
75
79
|
});
|
|
76
80
|
this.accessory.configureController(this.AdaptiveLightingController);
|
|
77
81
|
this.accessory.context.adaptiveLighting = true;
|
|
78
|
-
this.debugLog(
|
|
82
|
+
this.debugLog(`adaptiveLighting: ${this.accessory.context.adaptiveLighting},`
|
|
79
83
|
+ ` adaptiveLightingShift: ${this.adaptiveLightingShift}`);
|
|
80
84
|
}
|
|
81
|
-
this.debugLog(
|
|
85
|
+
this.debugLog(`adaptiveLightingShift: ${this.adaptiveLightingShift}`);
|
|
82
86
|
|
|
83
87
|
// Initialize LightBulb Characteristics
|
|
84
88
|
this.LightBulb.Service
|
|
@@ -148,6 +152,9 @@ export class CeilingLight extends deviceBase {
|
|
|
148
152
|
// Update Homekit
|
|
149
153
|
this.updateHomeKitCharacteristics();
|
|
150
154
|
|
|
155
|
+
//regisiter webhook event handler
|
|
156
|
+
this.registerWebhook();
|
|
157
|
+
|
|
151
158
|
// Start an update interval
|
|
152
159
|
interval(this.deviceRefreshRate * 1000)
|
|
153
160
|
.pipe(skipWhile(() => this.ceilingLightUpdateInProgress))
|
|
@@ -155,9 +162,6 @@ export class CeilingLight extends deviceBase {
|
|
|
155
162
|
await this.refreshStatus();
|
|
156
163
|
});
|
|
157
164
|
|
|
158
|
-
//regisiter webhook event handler
|
|
159
|
-
this.registerWebhook(accessory, device);
|
|
160
|
-
|
|
161
165
|
// Watch for Bulb change events
|
|
162
166
|
// We put in a debounce of 100ms so we don't make duplicate calls
|
|
163
167
|
this.doCeilingLightUpdate
|
|
@@ -171,9 +175,8 @@ export class CeilingLight extends deviceBase {
|
|
|
171
175
|
try {
|
|
172
176
|
await this.pushChanges();
|
|
173
177
|
} catch (e: any) {
|
|
174
|
-
this.apiError(e);
|
|
175
|
-
this.errorLog(
|
|
176
|
-
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
178
|
+
await this.apiError(e);
|
|
179
|
+
await this.errorLog(`failed pushChanges with ${device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
177
180
|
}
|
|
178
181
|
this.ceilingLightUpdateInProgress = false;
|
|
179
182
|
});
|
|
@@ -182,74 +185,42 @@ export class CeilingLight extends deviceBase {
|
|
|
182
185
|
/**
|
|
183
186
|
* Parse the device status from the SwitchBotBLE API
|
|
184
187
|
*/
|
|
185
|
-
async BLEparseStatus(serviceData:
|
|
186
|
-
this.debugLog(
|
|
187
|
-
//
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
this.LightBulb.On = true;
|
|
191
|
-
break;
|
|
192
|
-
default:
|
|
193
|
-
this.LightBulb.On = false;
|
|
194
|
-
}
|
|
195
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
|
|
188
|
+
async BLEparseStatus(serviceData: ceilingLightServiceData | ceilingLightProServiceData): Promise<void> {
|
|
189
|
+
await this.debugLog('BLEparseStatus');
|
|
190
|
+
// On
|
|
191
|
+
this.LightBulb.On = serviceData.state;
|
|
192
|
+
await this.debugLog(`On: ${this.LightBulb.On}`);
|
|
196
193
|
}
|
|
197
194
|
|
|
198
195
|
/**
|
|
199
196
|
* Parse the device status from the SwitchBot OpenAPI
|
|
200
197
|
*/
|
|
201
|
-
async openAPIparseStatus(deviceStatus:
|
|
202
|
-
this.debugLog(
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
break;
|
|
207
|
-
default:
|
|
208
|
-
this.LightBulb.On = false;
|
|
209
|
-
}
|
|
210
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.LightBulb.On}`);
|
|
198
|
+
async openAPIparseStatus(deviceStatus: ceilingLightStatus | ceilingLightProStatus): Promise<void> {
|
|
199
|
+
await this.debugLog('openAPIparseStatus');
|
|
200
|
+
// On
|
|
201
|
+
this.LightBulb.On = deviceStatus.power;
|
|
202
|
+
await this.debugLog(`On: ${this.LightBulb.On}`);
|
|
211
203
|
|
|
212
204
|
// Brightness
|
|
213
|
-
this.LightBulb.Brightness =
|
|
214
|
-
this.debugLog(
|
|
215
|
-
|
|
216
|
-
// Color, Hue & Brightness
|
|
217
|
-
if (deviceStatus.body.color) {
|
|
218
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} color: ${JSON.stringify(deviceStatus.body.color)}`);
|
|
219
|
-
const [red, green, blue] = deviceStatus.body.color!.split(':');
|
|
220
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} red: ${JSON.stringify(red)}`);
|
|
221
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} green: ${JSON.stringify(green)}`);
|
|
222
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} blue: ${JSON.stringify(blue)}`);
|
|
223
|
-
|
|
224
|
-
const [hue, saturation] = rgb2hs(Number(red), Number(green), Number(blue));
|
|
225
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
|
|
226
|
-
+ ` hs: ${JSON.stringify(rgb2hs(Number(red), Number(green), Number(blue)))}`);
|
|
227
|
-
|
|
228
|
-
// Hue
|
|
229
|
-
this.LightBulb.Hue = hue;
|
|
230
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.LightBulb.Hue}`);
|
|
231
|
-
|
|
232
|
-
// Saturation
|
|
233
|
-
this.LightBulb.Saturation = saturation;
|
|
234
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.LightBulb.Saturation}`);
|
|
235
|
-
}
|
|
205
|
+
this.LightBulb.Brightness = deviceStatus.brightness;
|
|
206
|
+
await this.debugLog(`Brightness: ${this.LightBulb.Brightness}`);
|
|
236
207
|
|
|
237
208
|
// ColorTemperature
|
|
238
|
-
if (!Number.isNaN(deviceStatus.
|
|
239
|
-
this.debugLog(
|
|
240
|
-
const mired = Math.round(1000000 / deviceStatus.
|
|
209
|
+
if (!Number.isNaN(deviceStatus.colorTemperature)) {
|
|
210
|
+
await this.debugLog(`OpenAPI ColorTemperature: ${deviceStatus.colorTemperature}`);
|
|
211
|
+
const mired = Math.round(1000000 / deviceStatus.colorTemperature);
|
|
241
212
|
|
|
242
213
|
this.LightBulb.ColorTemperature = Number(mired);
|
|
243
214
|
|
|
244
215
|
this.LightBulb.ColorTemperature = Math.max(Math.min(this.LightBulb.ColorTemperature, 500), 140);
|
|
245
|
-
this.debugLog(
|
|
216
|
+
await this.debugLog(`ColorTemperature: ${this.LightBulb.ColorTemperature}`);
|
|
246
217
|
}
|
|
247
218
|
|
|
248
219
|
// Firmware Version
|
|
249
|
-
const version = deviceStatus.
|
|
250
|
-
this.debugLog(
|
|
251
|
-
if (deviceStatus.
|
|
252
|
-
const deviceVersion = version
|
|
220
|
+
const version = deviceStatus.version.toString();
|
|
221
|
+
await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
|
|
222
|
+
if (deviceStatus.version) {
|
|
223
|
+
const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
|
|
253
224
|
this.accessory
|
|
254
225
|
.getService(this.hap.Service.AccessoryInformation)!
|
|
255
226
|
.setCharacteristic(this.hap.Characteristic.HardwareRevision, deviceVersion)
|
|
@@ -257,29 +228,43 @@ export class CeilingLight extends deviceBase {
|
|
|
257
228
|
.getCharacteristic(this.hap.Characteristic.FirmwareRevision)
|
|
258
229
|
.updateValue(deviceVersion);
|
|
259
230
|
this.accessory.context.deviceVersion = deviceVersion;
|
|
260
|
-
this.debugSuccessLog(
|
|
231
|
+
await this.debugSuccessLog(`deviceVersion: ${this.accessory.context.deviceVersion}`);
|
|
261
232
|
}
|
|
262
233
|
}
|
|
263
234
|
|
|
235
|
+
async parseStatusWebhook(context: ceilingLightWebhookContext | ceilingLightProWebhookContext): Promise<void> {
|
|
236
|
+
await this.debugLog('parseStatusWebhook');
|
|
237
|
+
await this.debugLog(`(powerState, brightness, colorTemperature) = Webhook:(${context.powerState}, ${context.brightness},`
|
|
238
|
+
+ ` ${context.colorTemperature}), current:(${this.LightBulb.On}, ${this.LightBulb.Brightness}, ${this.LightBulb.ColorTemperature})`);
|
|
239
|
+
// On
|
|
240
|
+
this.LightBulb.On = context.powerState === 'ON' ? true : false;
|
|
241
|
+
await this.debugLog(`On: ${this.LightBulb.On}`);
|
|
242
|
+
// Brightness
|
|
243
|
+
this.LightBulb.Brightness = context.brightness;
|
|
244
|
+
await this.debugLog(`Brightness: ${this.LightBulb.Brightness}`);
|
|
245
|
+
// ColorTemperature
|
|
246
|
+
this.LightBulb.ColorTemperature = context.colorTemperature;
|
|
247
|
+
await this.debugLog(`ColorTemperature: ${this.LightBulb.ColorTemperature}`);
|
|
248
|
+
}
|
|
249
|
+
|
|
264
250
|
/**
|
|
265
251
|
* Asks the SwitchBot API for the latest device information
|
|
266
252
|
*/
|
|
267
253
|
async refreshStatus(): Promise<void> {
|
|
268
254
|
if (!this.device.enableCloudService && this.OpenAPI) {
|
|
269
|
-
this.errorLog(
|
|
270
|
-
|
|
271
|
-
|
|
255
|
+
await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
|
|
256
|
+
} else if (this.BLE) {
|
|
257
|
+
await this.BLERefreshStatus();
|
|
272
258
|
} else if (this.OpenAPI && this.platform.config.credentials?.token) {
|
|
273
259
|
await this.openAPIRefreshStatus();
|
|
274
260
|
} else {
|
|
275
261
|
await this.offlineOff();
|
|
276
|
-
this.debugWarnLog(
|
|
277
|
-
+ ` ${this.device.connectionType}, refreshStatus will not happen.`);
|
|
262
|
+
await this.debugWarnLog(`Connection Type: ${this.device.connectionType}, refreshStatus will not happen.`);
|
|
278
263
|
}
|
|
279
264
|
}
|
|
280
265
|
|
|
281
266
|
async BLERefreshStatus(): Promise<void> {
|
|
282
|
-
this.debugLog(
|
|
267
|
+
await this.debugLog('BLERefreshStatus');
|
|
283
268
|
const switchbot = await this.switchbotBLE();
|
|
284
269
|
|
|
285
270
|
if (switchbot === undefined) {
|
|
@@ -287,13 +272,14 @@ export class CeilingLight extends deviceBase {
|
|
|
287
272
|
} else {
|
|
288
273
|
(async () => {
|
|
289
274
|
// Start to monitor advertisement packets
|
|
290
|
-
const serviceData
|
|
275
|
+
const serviceData = await this.monitorAdvertisementPackets(switchbot) as unknown as ceilingLightServiceData;
|
|
291
276
|
// Update HomeKit
|
|
292
|
-
if (serviceData.model
|
|
277
|
+
if ((serviceData.model === SwitchBotBLEModel.CeilingLight || SwitchBotBLEModel.CeilingLightPro)
|
|
278
|
+
&& serviceData.modelName === SwitchBotBLEModelName.CeilingLight || SwitchBotBLEModelName.CeilingLightPro) {
|
|
293
279
|
await this.BLEparseStatus(serviceData);
|
|
294
280
|
await this.updateHomeKitCharacteristics();
|
|
295
281
|
} else {
|
|
296
|
-
this.errorLog(
|
|
282
|
+
await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
|
|
297
283
|
await this.BLERefreshConnection(switchbot);
|
|
298
284
|
}
|
|
299
285
|
})();
|
|
@@ -301,69 +287,39 @@ export class CeilingLight extends deviceBase {
|
|
|
301
287
|
}
|
|
302
288
|
|
|
303
289
|
async openAPIRefreshStatus(): Promise<void> {
|
|
304
|
-
this.debugLog(
|
|
290
|
+
await this.debugLog('openAPIRefreshStatus');
|
|
305
291
|
try {
|
|
306
292
|
const { body, statusCode } = await this.deviceRefreshStatus();
|
|
307
293
|
const deviceStatus: any = await body.json();
|
|
308
|
-
await this.
|
|
309
|
-
if (
|
|
310
|
-
await this.
|
|
311
|
-
await this.openAPIparseStatus(deviceStatus);
|
|
294
|
+
await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
|
|
295
|
+
if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
|
|
296
|
+
await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
|
|
297
|
+
await this.openAPIparseStatus(deviceStatus.body);
|
|
312
298
|
await this.updateHomeKitCharacteristics();
|
|
313
299
|
} else {
|
|
314
|
-
await this.
|
|
300
|
+
await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
|
|
301
|
+
await this.debugWarnLog(statusCode, deviceStatus);
|
|
315
302
|
}
|
|
316
303
|
} catch (e: any) {
|
|
317
|
-
this.apiError(e);
|
|
318
|
-
this.errorLog(
|
|
319
|
-
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
304
|
+
await this.apiError(e);
|
|
305
|
+
await this.errorLog(`failed openAPIRefreshStatus with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
320
306
|
}
|
|
321
307
|
}
|
|
322
308
|
|
|
323
|
-
async registerWebhook(
|
|
324
|
-
if (device.webhook) {
|
|
325
|
-
this.debugLog(
|
|
326
|
-
this.platform.webhookEventHandler[device.deviceId] = async (context) => {
|
|
309
|
+
async registerWebhook() {
|
|
310
|
+
if (this.device.webhook) {
|
|
311
|
+
await this.debugLog('is listening webhook.');
|
|
312
|
+
this.platform.webhookEventHandler[this.device.deviceId] = async (context: ceilingLightWebhookContext | ceilingLightProWebhookContext) => {
|
|
327
313
|
try {
|
|
328
|
-
this.debugLog(
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
this.debugLog(`${device.deviceType}: ${accessory.displayName} ` +
|
|
332
|
-
'(powerState, brightness, colorTemperature) = ' +
|
|
333
|
-
`Webhook:(${powerState}, ${brightness}, ${colorTemperature}), `
|
|
334
|
-
+ `current:(${On}, ${Brightness}, ${ColorTemperature})`);
|
|
335
|
-
|
|
336
|
-
// On
|
|
337
|
-
this.LightBulb.On = powerState === 'ON' ? true : false;
|
|
338
|
-
if (accessory.context.Brightness !== this.LightBulb.On) {
|
|
339
|
-
this.infoLog(`${device.deviceType}: ${accessory.displayName} On: ${this.LightBulb.On}`);
|
|
340
|
-
} else {
|
|
341
|
-
this.debugLog(`${device.deviceType}: ${accessory.displayName} On: ${this.LightBulb.On}`);
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
// Brightness
|
|
345
|
-
this.LightBulb.Brightness = brightness;
|
|
346
|
-
if (accessory.context.Brightness !== this.LightBulb.Brightness) {
|
|
347
|
-
this.infoLog(`${device.deviceType}: ${accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
|
|
348
|
-
} else {
|
|
349
|
-
this.debugLog(`${device.deviceType}: ${accessory.displayName} Brightness: ${this.LightBulb.Brightness}`);
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
// ColorTemperature
|
|
353
|
-
this.LightBulb.ColorTemperature = colorTemperature;
|
|
354
|
-
if (accessory.context.ColorTemperature !== this.LightBulb.ColorTemperature) {
|
|
355
|
-
this.infoLog(`${device.deviceType}: ${accessory.displayName} ColorTemperature: ${this.LightBulb.ColorTemperature}`);
|
|
356
|
-
} else {
|
|
357
|
-
this.debugLog(`${device.deviceType}: ${accessory.displayName} ColorTemperature: ${this.LightBulb.ColorTemperature}`);
|
|
358
|
-
}
|
|
359
|
-
this.updateHomeKitCharacteristics();
|
|
314
|
+
await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
|
|
315
|
+
await this.parseStatusWebhook(context);
|
|
316
|
+
await this.updateHomeKitCharacteristics();
|
|
360
317
|
} catch (e: any) {
|
|
361
|
-
this.errorLog(
|
|
362
|
-
+ `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
|
|
318
|
+
await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
|
|
363
319
|
}
|
|
364
320
|
};
|
|
365
321
|
} else {
|
|
366
|
-
this.debugLog(
|
|
322
|
+
await this.debugLog('is not listening webhook.');
|
|
367
323
|
}
|
|
368
324
|
}
|
|
369
325
|
|
|
@@ -380,15 +336,28 @@ export class CeilingLight extends deviceBase {
|
|
|
380
336
|
*/
|
|
381
337
|
async pushChanges(): Promise<void> {
|
|
382
338
|
if (!this.device.enableCloudService && this.OpenAPI) {
|
|
383
|
-
this.errorLog(
|
|
339
|
+
await this.errorLog(`pushChanges enableCloudService: ${this.device.enableCloudService}`);
|
|
384
340
|
} else if (this.BLE) {
|
|
385
341
|
await this.BLEpushChanges();
|
|
386
342
|
} else if (this.OpenAPI && this.platform.config.credentials?.token) {
|
|
387
343
|
await this.openAPIpushChanges();
|
|
344
|
+
if (this.LightBulb.On) {
|
|
345
|
+
await this.debugLog(`On: ${this.LightBulb.On}`);
|
|
346
|
+
// Push Brightness Update
|
|
347
|
+
await this.debugLog(`Brightness: ${this.LightBulb.Brightness}`);
|
|
348
|
+
await this.pushBrightnessChanges();
|
|
349
|
+
// Push ColorTemperature Update
|
|
350
|
+
await this.debugLog(`ColorTemperature: ${this.LightBulb.ColorTemperature}`);
|
|
351
|
+
await this.pushColorTemperatureChanges();
|
|
352
|
+
// Push Hue & Saturation Update
|
|
353
|
+
await this.debugLog(`Hue: ${this.LightBulb.Hue}, Saturation: ${this.LightBulb.Saturation}`);
|
|
354
|
+
await this.pushHueSaturationChanges();
|
|
355
|
+
} else {
|
|
356
|
+
await this.debugLog('BLE (Brightness), (ColorTemperature), (Hue), & (Saturation) changes will not happen, as the device is OFF.');
|
|
357
|
+
}
|
|
388
358
|
} else {
|
|
389
359
|
await this.offlineOff();
|
|
390
|
-
this.debugWarnLog(
|
|
391
|
-
+ ` ${this.device.connectionType}, pushChanges will not happen.`);
|
|
360
|
+
await this.debugWarnLog(`Connection Type: ${this.device.connectionType}, pushChanges will not happen.`);
|
|
392
361
|
}
|
|
393
362
|
// Refresh the status from the API
|
|
394
363
|
interval(15000)
|
|
@@ -400,134 +369,112 @@ export class CeilingLight extends deviceBase {
|
|
|
400
369
|
}
|
|
401
370
|
|
|
402
371
|
async BLEpushChanges(): Promise<void> {
|
|
403
|
-
this.debugLog(
|
|
372
|
+
await this.debugLog('BLEpushChanges');
|
|
404
373
|
if (this.LightBulb.On !== this.accessory.context.On) {
|
|
405
|
-
this.debugLog(
|
|
406
|
-
|
|
407
|
-
const switchbot = await this.platform.connectBLE();
|
|
408
|
-
// Convert to BLE Address
|
|
374
|
+
await this.debugLog(`BLEpushChanges On: ${this.LightBulb.On} OnCached: ${this.accessory.context.On}`);
|
|
375
|
+
const switchbot = await this.platform.connectBLE(this.accessory, this.device);
|
|
409
376
|
await this.convertBLEAddress();
|
|
410
|
-
switchbot
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
377
|
+
if (switchbot !== false) {
|
|
378
|
+
switchbot
|
|
379
|
+
.discover({ model: this.device.bleModel, id: this.device.bleMac })
|
|
380
|
+
.then(async (device_list: any) => {
|
|
381
|
+
this.infoLog(`On: ${this.LightBulb.On}`);
|
|
382
|
+
return await this.retryBLE({
|
|
383
|
+
max: await this.maxRetryBLE(),
|
|
384
|
+
fn: async () => {
|
|
385
|
+
if (this.LightBulb.On) {
|
|
386
|
+
return await device_list[0].turnOn({ id: this.device.bleMac });
|
|
387
|
+
} else {
|
|
388
|
+
return await device_list[0].turnOff({ id: this.device.bleMac });
|
|
389
|
+
}
|
|
390
|
+
},
|
|
391
|
+
});
|
|
392
|
+
})
|
|
393
|
+
.then(async () => {
|
|
394
|
+
await this.successLog(`On: ${this.LightBulb.On} sent over SwitchBot BLE, sent successfully`);
|
|
395
|
+
this.LightBulb.On = false;
|
|
396
|
+
})
|
|
397
|
+
.catch(async (e: any) => {
|
|
398
|
+
await this.apiError(e);
|
|
399
|
+
await this.errorLog(`failed BLEpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
400
|
+
await this.BLEPushConnection();
|
|
423
401
|
});
|
|
424
|
-
|
|
425
|
-
.
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
+ `On: ${this.LightBulb.On} sent over BLE, sent successfully`);
|
|
429
|
-
this.LightBulb.On = false;
|
|
430
|
-
})
|
|
431
|
-
.catch(async (e: any) => {
|
|
432
|
-
this.apiError(e);
|
|
433
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
|
|
434
|
-
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
435
|
-
await this.BLEPushConnection();
|
|
436
|
-
});
|
|
402
|
+
} else {
|
|
403
|
+
await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
|
|
404
|
+
await this.BLEPushConnection();
|
|
405
|
+
}
|
|
437
406
|
} else {
|
|
438
|
-
this.debugLog(
|
|
439
|
-
+ ` No BLEpushChanges: On: ${this.LightBulb.On}, `
|
|
440
|
-
+ `OnCached: ${this.accessory.context.On}`);
|
|
407
|
+
await this.debugLog(`No changes (BLEpushChanges): On: ${this.LightBulb.On}, OnCached: ${this.accessory.context.On}`);
|
|
441
408
|
}
|
|
442
409
|
}
|
|
443
410
|
|
|
444
411
|
async openAPIpushChanges(): Promise<void> {
|
|
445
|
-
this.debugLog(
|
|
412
|
+
await this.debugLog('openAPIpushChanges');
|
|
446
413
|
if (this.LightBulb.On !== this.accessory.context.On) {
|
|
447
|
-
|
|
448
|
-
if (this.LightBulb.On) {
|
|
449
|
-
command = 'turnOn';
|
|
450
|
-
} else {
|
|
451
|
-
command = 'turnOff';
|
|
452
|
-
}
|
|
414
|
+
const command = this.LightBulb.On ? 'turnOn' : 'turnOff';
|
|
453
415
|
const bodyChange = JSON.stringify({
|
|
454
416
|
command: `${command}`,
|
|
455
417
|
parameter: 'default',
|
|
456
418
|
commandType: 'command',
|
|
457
419
|
});
|
|
458
|
-
this.debugLog(
|
|
420
|
+
await this.debugLog(`SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`);
|
|
459
421
|
try {
|
|
460
422
|
const { body, statusCode } = await this.pushChangeRequest(bodyChange);
|
|
461
423
|
const deviceStatus: any = await body.json();
|
|
462
|
-
await this.
|
|
463
|
-
if (
|
|
464
|
-
await this.
|
|
424
|
+
await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
|
|
425
|
+
if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
|
|
426
|
+
await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
|
|
465
427
|
await this.updateHomeKitCharacteristics();
|
|
466
428
|
} else {
|
|
467
429
|
await this.statusCode(statusCode);
|
|
468
430
|
await this.statusCode(deviceStatus.statusCode);
|
|
469
431
|
}
|
|
470
432
|
} catch (e: any) {
|
|
471
|
-
this.apiError(e);
|
|
472
|
-
this.errorLog(
|
|
473
|
-
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
433
|
+
await this.apiError(e);
|
|
434
|
+
await this.errorLog(`failed openAPIpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
474
435
|
}
|
|
475
436
|
} else {
|
|
476
|
-
this.debugLog(
|
|
477
|
-
+ `On: ${this.LightBulb.On}, `
|
|
478
|
-
+ `OnCached: ${this.accessory.context.On}`);
|
|
479
|
-
}
|
|
480
|
-
// Push Hue & Saturation Update
|
|
481
|
-
if (this.LightBulb.On) {
|
|
482
|
-
await this.pushHueSaturationChanges();
|
|
483
|
-
}
|
|
484
|
-
// Push ColorTemperature Update
|
|
485
|
-
if (this.LightBulb.On) {
|
|
486
|
-
await this.pushColorTemperatureChanges();
|
|
487
|
-
}
|
|
488
|
-
// Push Brightness Update
|
|
489
|
-
if (this.LightBulb.On) {
|
|
490
|
-
await this.pushBrightnessChanges();
|
|
437
|
+
await this.debugLog(`No changes (openAPIpushChanges), On: ${this.LightBulb.On}, OnCached: ${this.accessory.context.On}`);
|
|
491
438
|
}
|
|
492
439
|
}
|
|
493
440
|
|
|
494
441
|
async pushHueSaturationChanges(): Promise<void> {
|
|
495
|
-
this.debugLog(
|
|
496
|
-
if (this.LightBulb.Hue !== this.accessory.context.Hue || this.LightBulb.Saturation !== this.accessory.context.Saturation) {
|
|
497
|
-
this.debugLog(
|
|
498
|
-
this.debugLog(
|
|
442
|
+
await this.debugLog('pushHueSaturationChanges');
|
|
443
|
+
if ((this.LightBulb.Hue !== this.accessory.context.Hue) || (this.LightBulb.Saturation !== this.accessory.context.Saturation)) {
|
|
444
|
+
await this.debugLog(`Hue: ${JSON.stringify(this.LightBulb.Hue)}`);
|
|
445
|
+
await this.debugLog(`Saturation: ${JSON.stringify(this.LightBulb.Saturation)}`);
|
|
499
446
|
const [red, green, blue] = hs2rgb(Number(this.LightBulb.Hue), Number(this.LightBulb.Saturation));
|
|
500
|
-
this.debugLog(
|
|
447
|
+
await this.debugLog(`rgb: ${JSON.stringify([red, green, blue])}`);
|
|
501
448
|
const bodyChange = JSON.stringify({
|
|
502
449
|
command: 'setColor',
|
|
503
450
|
parameter: `${red}:${green}:${blue}`,
|
|
504
451
|
commandType: 'command',
|
|
505
452
|
});
|
|
506
|
-
this.debugLog(
|
|
453
|
+
await this.debugLog(`(pushHueSaturationChanges) SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`);
|
|
507
454
|
try {
|
|
508
455
|
const { body, statusCode } = await this.pushChangeRequest(bodyChange);
|
|
509
456
|
const deviceStatus: any = await body.json();
|
|
510
|
-
await this.
|
|
511
|
-
if (
|
|
512
|
-
await this.
|
|
457
|
+
await this.debugLog(`(pushHueSaturationChanges) statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
|
|
458
|
+
if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
|
|
459
|
+
await this.debugSuccessLog(`(pushHueSaturationChanges) statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
|
|
513
460
|
await this.updateHomeKitCharacteristics();
|
|
514
461
|
} else {
|
|
515
462
|
await this.statusCode(statusCode);
|
|
516
463
|
await this.statusCode(deviceStatus.statusCode);
|
|
517
464
|
}
|
|
518
465
|
} catch (e: any) {
|
|
519
|
-
this.apiError(e);
|
|
520
|
-
this.errorLog(
|
|
521
|
-
+ `
|
|
466
|
+
await this.apiError(e);
|
|
467
|
+
await this.errorLog(`failed pushHueSaturationChanges with ${this.device.connectionType} Connection,`
|
|
468
|
+
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
522
469
|
}
|
|
523
470
|
} else {
|
|
524
|
-
this.debugLog(
|
|
525
|
-
+
|
|
471
|
+
await this.debugLog(`No changes (pushHueSaturationChanges), Hue: ${this.LightBulb.Hue}, HueCached: ${this.accessory.context.Hue},`
|
|
472
|
+
+ ` Saturation: ${this.LightBulb.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`);
|
|
526
473
|
}
|
|
527
474
|
}
|
|
528
475
|
|
|
529
476
|
async pushColorTemperatureChanges(): Promise<void> {
|
|
530
|
-
this.debugLog(
|
|
477
|
+
await this.debugLog('pushColorTemperatureChanges');
|
|
531
478
|
if (this.LightBulb.ColorTemperature !== this.accessory.context.ColorTemperature) {
|
|
532
479
|
const kelvin = Math.round(1000000 / Number(this.LightBulb.ColorTemperature));
|
|
533
480
|
this.accessory.context.kelvin = kelvin;
|
|
@@ -536,58 +483,57 @@ export class CeilingLight extends deviceBase {
|
|
|
536
483
|
parameter: `${kelvin}`,
|
|
537
484
|
commandType: 'command',
|
|
538
485
|
});
|
|
539
|
-
this.debugLog(
|
|
486
|
+
await this.debugLog(`(pushColorTemperatureChanges) SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`);
|
|
540
487
|
try {
|
|
541
488
|
const { body, statusCode } = await this.pushChangeRequest(bodyChange);
|
|
542
489
|
const deviceStatus: any = await body.json();
|
|
543
|
-
await this.
|
|
544
|
-
if (
|
|
545
|
-
await this.
|
|
490
|
+
await this.debugLog(`(pushColorTemperatureChanges) statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
|
|
491
|
+
if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
|
|
492
|
+
await this.debugSuccessLog(`(pushColorTemperatureChanges) statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
|
|
546
493
|
await this.updateHomeKitCharacteristics();
|
|
547
494
|
} else {
|
|
548
495
|
await this.statusCode(statusCode);
|
|
549
496
|
await this.statusCode(deviceStatus.statusCode);
|
|
550
497
|
}
|
|
551
498
|
} catch (e: any) {
|
|
552
|
-
this.apiError(e);
|
|
553
|
-
this.errorLog(
|
|
554
|
-
+ `
|
|
499
|
+
await this.apiError(e);
|
|
500
|
+
await this.errorLog(`failed pushColorTemperatureChanges with ${this.device.connectionType} Connection,`
|
|
501
|
+
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
555
502
|
}
|
|
556
503
|
} else {
|
|
557
|
-
this.debugLog(
|
|
558
|
-
+ `
|
|
504
|
+
await this.debugLog(`No changes (pushColorTemperatureChanges), ColorTemperature: ${this.LightBulb.ColorTemperature},`
|
|
505
|
+
+ ` ColorTemperatureCached: ${this.accessory.context.ColorTemperature}`);
|
|
559
506
|
}
|
|
560
507
|
}
|
|
561
508
|
|
|
562
509
|
async pushBrightnessChanges(): Promise<void> {
|
|
563
|
-
this.debugLog(
|
|
510
|
+
await this.debugLog('pushBrightnessChanges');
|
|
564
511
|
if (this.LightBulb.Brightness !== this.accessory.context.Brightness) {
|
|
565
512
|
const bodyChange = JSON.stringify({
|
|
566
513
|
command: 'setBrightness',
|
|
567
514
|
parameter: `${this.LightBulb.Brightness}`,
|
|
568
515
|
commandType: 'command',
|
|
569
516
|
});
|
|
570
|
-
this.debugLog(
|
|
517
|
+
await this.debugLog(`(pushBrightnessChanges) SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`);
|
|
571
518
|
try {
|
|
572
519
|
const { body, statusCode } = await this.pushChangeRequest(bodyChange);
|
|
573
520
|
const deviceStatus: any = await body.json();
|
|
574
|
-
await this.
|
|
575
|
-
if (
|
|
576
|
-
await this.
|
|
521
|
+
await this.debugLog(`(pushBrightnessChanges) statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
|
|
522
|
+
if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
|
|
523
|
+
await this.debugSuccessLog(`(pushBrightnessChanges) statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
|
|
577
524
|
await this.updateHomeKitCharacteristics();
|
|
578
525
|
} else {
|
|
579
526
|
await this.statusCode(statusCode);
|
|
580
527
|
await this.statusCode(deviceStatus.statusCode);
|
|
581
528
|
}
|
|
582
529
|
} catch (e: any) {
|
|
583
|
-
this.apiError(e);
|
|
584
|
-
this.errorLog(
|
|
585
|
-
+ `
|
|
530
|
+
await this.apiError(e);
|
|
531
|
+
await this.errorLog(`failed pushBrightnessChanges with ${this.device.connectionType} Connection,`
|
|
532
|
+
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
586
533
|
}
|
|
587
534
|
} else {
|
|
588
|
-
this.debugLog(
|
|
589
|
-
+ `
|
|
590
|
-
+ `BrightnessCached: ${this.accessory.context.Brightness}`);
|
|
535
|
+
await this.debugLog(`No changes (pushBrightnessChanges), Brightness: ${this.LightBulb.Brightness},`
|
|
536
|
+
+ ` BrightnessCached: ${this.accessory.context.Brightness}`);
|
|
591
537
|
}
|
|
592
538
|
}
|
|
593
539
|
|
|
@@ -595,10 +541,10 @@ export class CeilingLight extends deviceBase {
|
|
|
595
541
|
* Handle requests to set the value of the "On" characteristic
|
|
596
542
|
*/
|
|
597
543
|
async OnSet(value: CharacteristicValue): Promise<void> {
|
|
598
|
-
if (this.LightBulb.On
|
|
599
|
-
this.
|
|
544
|
+
if (this.LightBulb.On !== this.accessory.context.On) {
|
|
545
|
+
await this.infoLog(`Set On: ${value}`);
|
|
600
546
|
} else {
|
|
601
|
-
this.
|
|
547
|
+
await this.debugLog(`No Changes, Set On: ${value}`);
|
|
602
548
|
}
|
|
603
549
|
|
|
604
550
|
this.LightBulb.On = value;
|
|
@@ -609,14 +555,15 @@ export class CeilingLight extends deviceBase {
|
|
|
609
555
|
* Handle requests to set the value of the "Brightness" characteristic
|
|
610
556
|
*/
|
|
611
557
|
async BrightnessSet(value: CharacteristicValue): Promise<void> {
|
|
612
|
-
if (this.LightBulb.Brightness
|
|
613
|
-
this.
|
|
614
|
-
} else if (this.LightBulb.On) {
|
|
615
|
-
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Brightness: ${value}`);
|
|
558
|
+
if (this.LightBulb.On && (this.LightBulb.Brightness !== this.accessory.context.Brightness)) {
|
|
559
|
+
await this.infoLog(`Set Brightness: ${value}`);
|
|
616
560
|
} else {
|
|
617
|
-
|
|
561
|
+
if (this.LightBulb.On) {
|
|
562
|
+
this.debugLog(`No Changes, Brightness: ${value}`);
|
|
563
|
+
} else {
|
|
564
|
+
this.debugLog(`Set Brightness: ${value}, On: ${this.LightBulb.On}`);
|
|
565
|
+
}
|
|
618
566
|
}
|
|
619
|
-
|
|
620
567
|
this.LightBulb.Brightness = value;
|
|
621
568
|
this.doCeilingLightUpdate.next();
|
|
622
569
|
}
|
|
@@ -625,12 +572,14 @@ export class CeilingLight extends deviceBase {
|
|
|
625
572
|
* Handle requests to set the value of the "ColorTemperature" characteristic
|
|
626
573
|
*/
|
|
627
574
|
async ColorTemperatureSet(value: CharacteristicValue): Promise<void> {
|
|
628
|
-
if (this.LightBulb.ColorTemperature
|
|
629
|
-
this.
|
|
630
|
-
} else if (this.LightBulb.On) {
|
|
631
|
-
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set ColorTemperature: ${value}`);
|
|
575
|
+
if (this.LightBulb.On && (this.LightBulb.ColorTemperature !== this.accessory.context.ColorTemperature)) {
|
|
576
|
+
this.infoLog(`Set ColorTemperature: ${value}`);
|
|
632
577
|
} else {
|
|
633
|
-
|
|
578
|
+
if (this.LightBulb.On) {
|
|
579
|
+
this.debugLog(`No Changes, ColorTemperature: ${value}`);
|
|
580
|
+
} else {
|
|
581
|
+
this.debugLog(`Set ColorTemperature: ${value}, On: ${this.LightBulb.On}`);
|
|
582
|
+
}
|
|
634
583
|
}
|
|
635
584
|
|
|
636
585
|
const minKelvin = 2000;
|
|
@@ -658,12 +607,14 @@ export class CeilingLight extends deviceBase {
|
|
|
658
607
|
* Handle requests to set the value of the "Hue" characteristic
|
|
659
608
|
*/
|
|
660
609
|
async HueSet(value: CharacteristicValue): Promise<void> {
|
|
661
|
-
if (this.LightBulb.Hue
|
|
662
|
-
this.
|
|
663
|
-
} else if (this.LightBulb.On) {
|
|
664
|
-
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Hue: ${value}`);
|
|
610
|
+
if (this.LightBulb.On && (this.LightBulb.Hue !== this.accessory.context.Hue)) {
|
|
611
|
+
this.infoLog(`Set Hue: ${value}`);
|
|
665
612
|
} else {
|
|
666
|
-
|
|
613
|
+
if (this.LightBulb.On) {
|
|
614
|
+
this.debugLog(`No Changes, Hue: ${value}`);
|
|
615
|
+
} else {
|
|
616
|
+
this.debugLog(`Set Hue: ${value}, On: ${this.LightBulb.On}`);
|
|
617
|
+
}
|
|
667
618
|
}
|
|
668
619
|
|
|
669
620
|
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, 140);
|
|
@@ -676,12 +627,14 @@ export class CeilingLight extends deviceBase {
|
|
|
676
627
|
* Handle requests to set the value of the "Saturation" characteristic
|
|
677
628
|
*/
|
|
678
629
|
async SaturationSet(value: CharacteristicValue): Promise<void> {
|
|
679
|
-
if (this.LightBulb.Saturation
|
|
680
|
-
this.
|
|
681
|
-
} else if (this.LightBulb.On) {
|
|
682
|
-
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Saturation: ${value}`);
|
|
630
|
+
if (this.LightBulb.On && (this.LightBulb.Saturation !== this.accessory.context.Saturation)) {
|
|
631
|
+
this.infoLog(`Set Saturation: ${value}`);
|
|
683
632
|
} else {
|
|
684
|
-
|
|
633
|
+
if (this.LightBulb.On) {
|
|
634
|
+
this.debugLog(`No Changes, Saturation: ${value}`);
|
|
635
|
+
} else {
|
|
636
|
+
this.debugLog(`Set Saturation: ${value}, On: ${this.LightBulb.On}`);
|
|
637
|
+
}
|
|
685
638
|
}
|
|
686
639
|
|
|
687
640
|
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, 140);
|
|
@@ -691,66 +644,43 @@ export class CeilingLight extends deviceBase {
|
|
|
691
644
|
}
|
|
692
645
|
|
|
693
646
|
async updateHomeKitCharacteristics(): Promise<void> {
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
this.
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
this.
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
this.LightBulb
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ColorTemperature: ${this.LightBulb.ColorTemperature}`);
|
|
710
|
-
} else {
|
|
711
|
-
this.accessory.context.ColorTemperature = this.LightBulb.ColorTemperature;
|
|
712
|
-
this.LightBulb!.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, this.LightBulb.ColorTemperature);
|
|
713
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
|
|
714
|
-
+ ` updateCharacteristic ColorTemperature: ${this.LightBulb.ColorTemperature}`);
|
|
715
|
-
}
|
|
716
|
-
if (this.LightBulb.Hue === undefined) {
|
|
717
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Hue: ${this.LightBulb.Hue}`);
|
|
718
|
-
} else {
|
|
719
|
-
this.accessory.context.Hue = this.LightBulb.Hue;
|
|
720
|
-
this.LightBulb!.Service.updateCharacteristic(this.hap.Characteristic.Hue, this.LightBulb.Hue);
|
|
721
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Hue: ${this.LightBulb.Hue}`);
|
|
722
|
-
}
|
|
723
|
-
if (this.LightBulb.Saturation === undefined) {
|
|
724
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Saturation: ${this.LightBulb.Saturation}`);
|
|
725
|
-
} else {
|
|
726
|
-
this.accessory.context.Saturation = this.LightBulb.Saturation;
|
|
727
|
-
this.LightBulb!.Service.updateCharacteristic(this.hap.Characteristic.Saturation, this.LightBulb.Saturation);
|
|
728
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Saturation: ${this.LightBulb.Saturation}`);
|
|
729
|
-
}
|
|
647
|
+
// On
|
|
648
|
+
await this.updateCharacteristic(this.LightBulb.Service, this.hap.Characteristic.On,
|
|
649
|
+
this.LightBulb.On, 'On');
|
|
650
|
+
// Brightness
|
|
651
|
+
await this.updateCharacteristic(this.LightBulb.Service, this.hap.Characteristic.Brightness,
|
|
652
|
+
this.LightBulb.Brightness, 'Brightness');
|
|
653
|
+
// ColorTemperature
|
|
654
|
+
await this.updateCharacteristic(this.LightBulb.Service, this.hap.Characteristic.ColorTemperature,
|
|
655
|
+
this.LightBulb.ColorTemperature, 'ColorTemperature');
|
|
656
|
+
// Hue
|
|
657
|
+
await this.updateCharacteristic(this.LightBulb.Service, this.hap.Characteristic.Hue,
|
|
658
|
+
this.LightBulb.Hue, 'Hue');
|
|
659
|
+
// Saturation
|
|
660
|
+
await this.updateCharacteristic(this.LightBulb.Service, this.hap.Characteristic.Saturation,
|
|
661
|
+
this.LightBulb.Saturation, 'Saturation');
|
|
730
662
|
}
|
|
731
663
|
|
|
732
664
|
async adaptiveLighting(device: device & devicesConfig): Promise<void> {
|
|
733
665
|
if (device.ceilinglight?.adaptiveLightingShift) {
|
|
734
666
|
this.adaptiveLightingShift = device.ceilinglight.adaptiveLightingShift;
|
|
735
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
|
|
736
667
|
} else {
|
|
737
668
|
this.adaptiveLightingShift = 0;
|
|
738
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} adaptiveLightingShift: ${this.adaptiveLightingShift}`);
|
|
739
669
|
}
|
|
670
|
+
await this.debugLog(`adaptiveLightingShift: ${this.adaptiveLightingShift}`);
|
|
740
671
|
}
|
|
741
672
|
|
|
742
673
|
async BLEPushConnection() {
|
|
743
674
|
if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
|
|
744
|
-
this.warnLog(
|
|
675
|
+
await this.warnLog('Using OpenAPI Connection to Push Changes');
|
|
745
676
|
await this.openAPIpushChanges();
|
|
746
677
|
}
|
|
747
678
|
}
|
|
748
679
|
|
|
749
680
|
async BLERefreshConnection(switchbot: any): Promise<void> {
|
|
750
|
-
this.errorLog(
|
|
751
|
-
+ ` ${JSON.stringify(switchbot)}`);
|
|
681
|
+
await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${JSON.stringify(switchbot)}`);
|
|
752
682
|
if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
|
|
753
|
-
this.warnLog(
|
|
683
|
+
await this.warnLog('Using OpenAPI Connection to Refresh Status');
|
|
754
684
|
await this.openAPIRefreshStatus();
|
|
755
685
|
}
|
|
756
686
|
}
|
|
@@ -761,11 +691,11 @@ export class CeilingLight extends deviceBase {
|
|
|
761
691
|
}
|
|
762
692
|
}
|
|
763
693
|
|
|
764
|
-
apiError(e: any): void {
|
|
765
|
-
this.LightBulb
|
|
766
|
-
this.LightBulb
|
|
767
|
-
this.LightBulb
|
|
768
|
-
this.LightBulb
|
|
769
|
-
this.LightBulb
|
|
694
|
+
async apiError(e: any): Promise<void> {
|
|
695
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.On, e);
|
|
696
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Hue, e);
|
|
697
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Brightness, e);
|
|
698
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.Saturation, e);
|
|
699
|
+
this.LightBulb.Service.updateCharacteristic(this.hap.Characteristic.ColorTemperature, e);
|
|
770
700
|
}
|
|
771
701
|
}
|