@switchbot/homebridge-switchbot 3.5.0-beta.9 → 3.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/settings.json +4 -1
- package/CHANGELOG.md +4 -1
- package/config.schema.json +166 -24
- package/dist/device/blindtilt.d.ts +21 -96
- package/dist/device/blindtilt.d.ts.map +1 -1
- package/dist/device/blindtilt.js +439 -817
- package/dist/device/blindtilt.js.map +1 -1
- package/dist/device/bot.d.ts +27 -72
- package/dist/device/bot.d.ts.map +1 -1
- package/dist/device/bot.js +835 -865
- package/dist/device/bot.js.map +1 -1
- package/dist/device/ceilinglight.d.ts +15 -69
- package/dist/device/ceilinglight.d.ts.map +1 -1
- package/dist/device/ceilinglight.js +287 -653
- package/dist/device/ceilinglight.js.map +1 -1
- package/dist/device/colorbulb.d.ts +15 -76
- package/dist/device/colorbulb.d.ts.map +1 -1
- package/dist/device/colorbulb.js +354 -732
- package/dist/device/colorbulb.js.map +1 -1
- package/dist/device/contact.d.ts +14 -66
- package/dist/device/contact.d.ts.map +1 -1
- package/dist/device/contact.js +268 -576
- package/dist/device/contact.js.map +1 -1
- package/dist/device/curtain.d.ts +18 -89
- package/dist/device/curtain.d.ts.map +1 -1
- package/dist/device/curtain.js +431 -851
- package/dist/device/curtain.js.map +1 -1
- package/dist/device/device.d.ts +60 -0
- package/dist/device/device.d.ts.map +1 -0
- package/dist/device/device.js +739 -0
- package/dist/device/device.js.map +1 -0
- package/dist/device/fan.d.ts +63 -0
- package/dist/device/fan.d.ts.map +1 -0
- package/dist/device/fan.js +698 -0
- package/dist/device/fan.js.map +1 -0
- package/dist/device/hub.d.ts +15 -60
- package/dist/device/hub.d.ts.map +1 -1
- package/dist/device/hub.js +257 -631
- package/dist/device/hub.js.map +1 -1
- package/dist/device/humidifier.d.ts +11 -66
- package/dist/device/humidifier.d.ts.map +1 -1
- package/dist/device/humidifier.js +266 -629
- package/dist/device/humidifier.js.map +1 -1
- package/dist/device/iosensor.d.ts +12 -64
- package/dist/device/iosensor.d.ts.map +1 -1
- package/dist/device/iosensor.js +215 -615
- package/dist/device/iosensor.js.map +1 -1
- package/dist/device/lightstrip.d.ts +12 -70
- package/dist/device/lightstrip.d.ts.map +1 -1
- package/dist/device/lightstrip.js +336 -645
- package/dist/device/lightstrip.js.map +1 -1
- package/dist/device/lock.d.ts +17 -63
- package/dist/device/lock.d.ts.map +1 -1
- package/dist/device/lock.js +290 -595
- package/dist/device/lock.js.map +1 -1
- package/dist/device/meter.d.ts +15 -64
- package/dist/device/meter.d.ts.map +1 -1
- package/dist/device/meter.js +211 -608
- package/dist/device/meter.js.map +1 -1
- package/dist/device/meterplus.d.ts +15 -65
- package/dist/device/meterplus.d.ts.map +1 -1
- package/dist/device/meterplus.js +205 -615
- package/dist/device/meterplus.js.map +1 -1
- package/dist/device/motion.d.ts +12 -69
- package/dist/device/motion.d.ts.map +1 -1
- package/dist/device/motion.js +202 -563
- package/dist/device/motion.js.map +1 -1
- package/dist/device/plug.d.ts +10 -57
- package/dist/device/plug.d.ts.map +1 -1
- package/dist/device/plug.js +125 -484
- package/dist/device/plug.js.map +1 -1
- package/dist/device/robotvacuumcleaner.d.ts +13 -63
- package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
- package/dist/device/robotvacuumcleaner.js +233 -549
- package/dist/device/robotvacuumcleaner.js.map +1 -1
- package/dist/device/waterdetector.d.ts +11 -63
- package/dist/device/waterdetector.d.ts.map +1 -1
- package/dist/device/waterdetector.js +170 -493
- package/dist/device/waterdetector.js.map +1 -1
- package/dist/homebridge-ui/public/index.html +22 -16
- package/dist/homebridge-ui/server.js +1 -1
- package/dist/homebridge-ui/server.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/irdevice/airconditioner.d.ts +10 -50
- package/dist/irdevice/airconditioner.d.ts.map +1 -1
- package/dist/irdevice/airconditioner.js +340 -468
- package/dist/irdevice/airconditioner.js.map +1 -1
- package/dist/irdevice/airpurifier.d.ts +8 -39
- package/dist/irdevice/airpurifier.d.ts.map +1 -1
- package/dist/irdevice/airpurifier.js +105 -319
- package/dist/irdevice/airpurifier.js.map +1 -1
- package/dist/irdevice/camera.d.ts +7 -36
- package/dist/irdevice/camera.d.ts.map +1 -1
- package/dist/irdevice/camera.js +44 -285
- package/dist/irdevice/camera.js.map +1 -1
- package/dist/irdevice/fan.d.ts +7 -43
- package/dist/irdevice/fan.d.ts.map +1 -1
- package/dist/irdevice/fan.js +92 -350
- 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 +369 -0
- package/dist/irdevice/irdevice.js.map +1 -0
- package/dist/irdevice/light.d.ts +12 -45
- package/dist/irdevice/light.d.ts.map +1 -1
- package/dist/irdevice/light.js +118 -362
- package/dist/irdevice/light.js.map +1 -1
- package/dist/irdevice/other.d.ts +19 -48
- package/dist/irdevice/other.d.ts.map +1 -1
- package/dist/irdevice/other.js +438 -570
- package/dist/irdevice/other.js.map +1 -1
- package/dist/irdevice/tv.d.ts +8 -40
- package/dist/irdevice/tv.d.ts.map +1 -1
- package/dist/irdevice/tv.js +91 -339
- package/dist/irdevice/tv.js.map +1 -1
- package/dist/irdevice/vacuumcleaner.d.ts +7 -36
- package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
- package/dist/irdevice/vacuumcleaner.js +45 -285
- package/dist/irdevice/vacuumcleaner.js.map +1 -1
- package/dist/irdevice/waterheater.d.ts +7 -36
- package/dist/irdevice/waterheater.d.ts.map +1 -1
- package/dist/irdevice/waterheater.js +47 -291
- package/dist/irdevice/waterheater.js.map +1 -1
- package/dist/platform.d.ts +22 -8
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +342 -191
- package/dist/platform.js.map +1 -1
- package/dist/settings.d.ts +64 -17
- package/dist/settings.d.ts.map +1 -1
- package/dist/settings.js +16 -552
- package/dist/settings.js.map +1 -1
- package/dist/utils.d.ts +77 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +638 -1
- package/dist/utils.js.map +1 -1
- package/eslint.config.js +96 -0
- package/package.json +20 -16
- package/src/custom.d.ts +2 -1
- package/src/device/blindtilt.ts +472 -888
- package/src/device/bot.ts +893 -968
- package/src/device/ceilinglight.ts +304 -709
- package/src/device/colorbulb.ts +370 -803
- package/src/device/contact.ts +301 -611
- package/src/device/curtain.ts +460 -908
- package/src/device/device.ts +755 -0
- package/src/device/fan.ts +749 -0
- package/src/device/hub.ts +294 -664
- package/src/device/humidifier.ts +299 -696
- package/src/device/iosensor.ts +251 -644
- package/src/device/lightstrip.ts +362 -712
- package/src/device/lock.ts +328 -653
- package/src/device/meter.ts +247 -637
- package/src/device/meterplus.ts +240 -644
- package/src/device/motion.ts +230 -598
- package/src/device/plug.ts +139 -525
- package/src/device/robotvacuumcleaner.ts +258 -593
- package/src/device/waterdetector.ts +191 -522
- package/src/homebridge-ui/public/index.html +22 -16
- package/src/homebridge-ui/server.ts +1 -1
- package/src/index.ts +4 -3
- package/src/irdevice/airconditioner.ts +370 -527
- package/src/irdevice/airpurifier.ts +132 -356
- package/src/irdevice/camera.ts +58 -311
- package/src/irdevice/fan.ts +109 -379
- package/src/irdevice/irdevice.ts +382 -0
- package/src/irdevice/light.ts +157 -412
- package/src/irdevice/other.ts +507 -648
- package/src/irdevice/tv.ts +121 -375
- package/src/irdevice/vacuumcleaner.ts +59 -311
- package/src/irdevice/waterheater.ts +61 -320
- package/src/platform.ts +395 -259
- package/src/settings.ts +118 -609
- package/src/utils.ts +642 -1
- package/.github/npm-version-script.cjs +0 -81
- package/src/device/waterdetector.json +0 -11
|
@@ -0,0 +1,698 @@
|
|
|
1
|
+
/* Copyright(C) 2021-2024, donavanbecker (https://github.com/donavanbecker). All rights reserved.
|
|
2
|
+
*
|
|
3
|
+
* plug.ts: @switchbot/homebridge-switchbot.
|
|
4
|
+
*/
|
|
5
|
+
import { request } from 'undici';
|
|
6
|
+
import { deviceBase } from './device.js';
|
|
7
|
+
import { Devices } from '../settings.js';
|
|
8
|
+
import { Subject, debounceTime, interval, skipWhile, take, tap } from 'rxjs';
|
|
9
|
+
export class Fan extends deviceBase {
|
|
10
|
+
platform;
|
|
11
|
+
// Services
|
|
12
|
+
Fan;
|
|
13
|
+
Battery;
|
|
14
|
+
LightBulb;
|
|
15
|
+
// Updates
|
|
16
|
+
fanUpdateInProgress;
|
|
17
|
+
doFanUpdate;
|
|
18
|
+
constructor(platform, accessory, device) {
|
|
19
|
+
super(platform, accessory, device);
|
|
20
|
+
this.platform = platform;
|
|
21
|
+
// this is subject we use to track when we need to POST changes to the SwitchBot API
|
|
22
|
+
this.doFanUpdate = new Subject();
|
|
23
|
+
this.fanUpdateInProgress = false;
|
|
24
|
+
// Initialize Fan Service
|
|
25
|
+
accessory.context.Fan = accessory.context.Fan ?? {};
|
|
26
|
+
this.Fan = {
|
|
27
|
+
Name: accessory.context.Fan.Name ?? accessory.displayName,
|
|
28
|
+
Service: accessory.getService(this.hap.Service.Fanv2) ?? accessory.addService(this.hap.Service.Fanv2),
|
|
29
|
+
Active: accessory.context.Active ?? this.hap.Characteristic.Active.INACTIVE,
|
|
30
|
+
SwingMode: accessory.context.SwingMode ?? this.hap.Characteristic.SwingMode.SWING_DISABLED,
|
|
31
|
+
RotationSpeed: accessory.context.RotationSpeed ?? 0,
|
|
32
|
+
};
|
|
33
|
+
accessory.context.Fan = this.Fan;
|
|
34
|
+
// Initialize Fan Service
|
|
35
|
+
this.Fan.Service
|
|
36
|
+
.setCharacteristic(this.hap.Characteristic.Name, this.Fan.Name)
|
|
37
|
+
.getCharacteristic(this.hap.Characteristic.Active)
|
|
38
|
+
.onGet(() => {
|
|
39
|
+
return this.Fan.Active;
|
|
40
|
+
})
|
|
41
|
+
.onSet(this.ActiveSet.bind(this));
|
|
42
|
+
// Initialize Fan RotationSpeed Characteristic
|
|
43
|
+
this.Fan.Service
|
|
44
|
+
.getCharacteristic(this.hap.Characteristic.RotationSpeed)
|
|
45
|
+
.onGet(() => {
|
|
46
|
+
return this.Fan.RotationSpeed;
|
|
47
|
+
})
|
|
48
|
+
.onSet(this.RotationSpeedSet.bind(this));
|
|
49
|
+
// Initialize Fan SwingMode Characteristic
|
|
50
|
+
this.Fan.Service.getCharacteristic(this.hap.Characteristic.SwingMode)
|
|
51
|
+
.onGet(() => {
|
|
52
|
+
return this.Fan.SwingMode;
|
|
53
|
+
})
|
|
54
|
+
.onSet(this.SwingModeSet.bind(this));
|
|
55
|
+
// Initialize Battery Service
|
|
56
|
+
accessory.context.Battery = accessory.context.Battery ?? {};
|
|
57
|
+
this.Battery = {
|
|
58
|
+
Name: accessory.context.Battery.Name ?? accessory.displayName,
|
|
59
|
+
Service: accessory.getService(this.hap.Service.Battery) ?? accessory.addService(this.hap.Service.Battery),
|
|
60
|
+
BatteryLevel: accessory.context.BatteryLevel ?? 100,
|
|
61
|
+
StatusLowBattery: accessory.context.StatusLowBattery ?? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL,
|
|
62
|
+
ChargingState: accessory.context.ChargingState ?? this.hap.Characteristic.ChargingState.NOT_CHARGING,
|
|
63
|
+
};
|
|
64
|
+
accessory.context.Battery = this.Battery;
|
|
65
|
+
// Initialize Battery Service
|
|
66
|
+
this.Battery.Service
|
|
67
|
+
.setCharacteristic(this.hap.Characteristic.Name, this.Battery.Name)
|
|
68
|
+
.getCharacteristic(this.hap.Characteristic.BatteryLevel)
|
|
69
|
+
.onGet(() => {
|
|
70
|
+
return this.Battery.BatteryLevel;
|
|
71
|
+
});
|
|
72
|
+
// Initialize Battery ChargingState Characteristic
|
|
73
|
+
this.Battery.Service
|
|
74
|
+
.getCharacteristic(this.hap.Characteristic.ChargingState)
|
|
75
|
+
.onGet(() => {
|
|
76
|
+
return this.Battery.ChargingState;
|
|
77
|
+
});
|
|
78
|
+
// Initialize Battery StatusLowBattery Characteristic
|
|
79
|
+
this.Battery.Service
|
|
80
|
+
.getCharacteristic(this.hap.Characteristic.StatusLowBattery)
|
|
81
|
+
.onGet(() => {
|
|
82
|
+
return this.Battery.StatusLowBattery;
|
|
83
|
+
});
|
|
84
|
+
// Initialize LightBulb Service
|
|
85
|
+
accessory.context.LightBulb = accessory.context.LightBulb ?? {};
|
|
86
|
+
this.LightBulb = {
|
|
87
|
+
Name: accessory.context.LightBulb.Name ?? accessory.displayName,
|
|
88
|
+
Service: accessory.getService(this.hap.Service.Lightbulb) ?? accessory.addService(this.hap.Service.Lightbulb),
|
|
89
|
+
On: accessory.context.On ?? false,
|
|
90
|
+
Brightness: accessory.context.Brightness ?? 0,
|
|
91
|
+
};
|
|
92
|
+
accessory.context.LightBulb = this.LightBulb;
|
|
93
|
+
// Initialize LightBulb Characteristics
|
|
94
|
+
this.LightBulb.Service
|
|
95
|
+
.setCharacteristic(this.hap.Characteristic.Name, this.LightBulb.Name)
|
|
96
|
+
.getCharacteristic(this.hap.Characteristic.On)
|
|
97
|
+
.onGet(() => {
|
|
98
|
+
return this.LightBulb.On;
|
|
99
|
+
})
|
|
100
|
+
.onSet(this.OnSet.bind(this));
|
|
101
|
+
// Initialize LightBulb Brightness Characteristic
|
|
102
|
+
this.LightBulb.Service
|
|
103
|
+
.getCharacteristic(this.hap.Characteristic.Brightness)
|
|
104
|
+
.onGet(() => {
|
|
105
|
+
return this.LightBulb.Brightness;
|
|
106
|
+
})
|
|
107
|
+
.onSet(this.BrightnessSet.bind(this));
|
|
108
|
+
// Retrieve initial values and updateHomekit
|
|
109
|
+
this.refreshStatus();
|
|
110
|
+
// Update Homekit
|
|
111
|
+
this.updateHomeKitCharacteristics();
|
|
112
|
+
// Start an update interval
|
|
113
|
+
interval(this.deviceRefreshRate * 1000)
|
|
114
|
+
.pipe(skipWhile(() => this.fanUpdateInProgress))
|
|
115
|
+
.subscribe(async () => {
|
|
116
|
+
await this.refreshStatus();
|
|
117
|
+
});
|
|
118
|
+
//regisiter webhook event handler
|
|
119
|
+
this.registerWebhook(accessory, device);
|
|
120
|
+
// Watch for Plug change events
|
|
121
|
+
// We put in a debounce of 100ms so we don't make duplicate calls
|
|
122
|
+
this.doFanUpdate
|
|
123
|
+
.pipe(tap(() => {
|
|
124
|
+
this.fanUpdateInProgress = true;
|
|
125
|
+
}), debounceTime(this.devicePushRate * 1000))
|
|
126
|
+
.subscribe(async () => {
|
|
127
|
+
try {
|
|
128
|
+
await this.pushChanges();
|
|
129
|
+
}
|
|
130
|
+
catch (e) {
|
|
131
|
+
this.apiError(e);
|
|
132
|
+
this.errorLog(`${device.deviceType}: ${accessory.displayName} failed pushChanges with ${device.connectionType} Connection,`
|
|
133
|
+
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
134
|
+
}
|
|
135
|
+
this.fanUpdateInProgress = false;
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
async BLEparseStatus(serviceData) {
|
|
139
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
|
|
140
|
+
// State
|
|
141
|
+
switch (serviceData.state) {
|
|
142
|
+
case 'on':
|
|
143
|
+
this.Fan.Active = true;
|
|
144
|
+
break;
|
|
145
|
+
default:
|
|
146
|
+
this.Fan.Active = false;
|
|
147
|
+
}
|
|
148
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.Fan.Active}`);
|
|
149
|
+
}
|
|
150
|
+
async openAPIparseStatus(deviceStatus) {
|
|
151
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
|
|
152
|
+
// Active
|
|
153
|
+
this.Fan.Active = deviceStatus.body.power === 'on' ? this.hap.Characteristic.Active.ACTIVE : this.hap.Characteristic.Active.INACTIVE;
|
|
154
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Active: ${this.Fan.Active}`);
|
|
155
|
+
// SwingMode
|
|
156
|
+
this.Fan.SwingMode = deviceStatus.body.oscillation === 'on' ?
|
|
157
|
+
this.hap.Characteristic.SwingMode.SWING_ENABLED : this.hap.Characteristic.SwingMode.SWING_DISABLED;
|
|
158
|
+
// RotationSpeed
|
|
159
|
+
this.Fan.RotationSpeed = deviceStatus.body.fanSpeed;
|
|
160
|
+
// ChargingState
|
|
161
|
+
this.Battery.ChargingState = deviceStatus.body.chargingStatus === 'charging' ?
|
|
162
|
+
this.hap.Characteristic.ChargingState.CHARGING : this.hap.Characteristic.ChargingState.NOT_CHARGING;
|
|
163
|
+
// BatteryLevel
|
|
164
|
+
this.Battery.BatteryLevel = Number(deviceStatus.body.battery);
|
|
165
|
+
if (this.Battery.BatteryLevel < 10) {
|
|
166
|
+
this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
|
|
170
|
+
}
|
|
171
|
+
if (Number.isNaN(this.Battery.BatteryLevel)) {
|
|
172
|
+
this.Battery.BatteryLevel = 100;
|
|
173
|
+
}
|
|
174
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel},`
|
|
175
|
+
+ ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
|
|
176
|
+
// Firmware Version
|
|
177
|
+
const version = deviceStatus.body.version?.toString();
|
|
178
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Firmware Version: ${version?.replace(/^V|-.*$/g, '')}`);
|
|
179
|
+
if (deviceStatus.body.version) {
|
|
180
|
+
const deviceVersion = version?.replace(/^V|-.*$/g, '') ?? '0.0.0';
|
|
181
|
+
this.accessory
|
|
182
|
+
.getService(this.hap.Service.AccessoryInformation)
|
|
183
|
+
.setCharacteristic(this.hap.Characteristic.HardwareRevision, deviceVersion)
|
|
184
|
+
.setCharacteristic(this.hap.Characteristic.FirmwareRevision, deviceVersion)
|
|
185
|
+
.getCharacteristic(this.hap.Characteristic.FirmwareRevision)
|
|
186
|
+
.updateValue(deviceVersion);
|
|
187
|
+
this.accessory.context.deviceVersion = deviceVersion;
|
|
188
|
+
this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceVersion: ${this.accessory.context.deviceVersion}`);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Asks the SwitchBot API for the latest device information
|
|
193
|
+
*/
|
|
194
|
+
async refreshStatus() {
|
|
195
|
+
if (!this.device.enableCloudService && this.OpenAPI) {
|
|
196
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus enableCloudService: ${this.device.enableCloudService}`);
|
|
197
|
+
}
|
|
198
|
+
else if (this.BLE) {
|
|
199
|
+
await this.BLERefreshStatus();
|
|
200
|
+
}
|
|
201
|
+
else if (this.OpenAPI && this.platform.config.credentials?.token) {
|
|
202
|
+
await this.openAPIRefreshStatus();
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
await this.offlineOff();
|
|
206
|
+
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
|
|
207
|
+
+ ` ${this.device.connectionType}, refreshStatus will not happen.`);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
async BLERefreshStatus() {
|
|
211
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
|
|
212
|
+
const switchbot = await this.platform.connectBLE();
|
|
213
|
+
// Convert to BLE Address
|
|
214
|
+
this.device.bleMac = this.device
|
|
215
|
+
.deviceId.match(/.{1,2}/g)
|
|
216
|
+
.join(':')
|
|
217
|
+
.toLowerCase();
|
|
218
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
219
|
+
this.getCustomBLEAddress(switchbot);
|
|
220
|
+
// Start to monitor advertisement packets
|
|
221
|
+
(async () => {
|
|
222
|
+
// Start to monitor advertisement packets
|
|
223
|
+
await switchbot.startScan({ model: this.device.bleModel, id: this.device.bleMac });
|
|
224
|
+
// Set an event handler
|
|
225
|
+
switchbot.onadvertisement = (ad) => {
|
|
226
|
+
if (this.device.bleMac === ad.address && ad.model === this.device.bleModel) {
|
|
227
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
|
|
228
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.model}`);
|
|
229
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
232
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
// Wait 10 seconds
|
|
236
|
+
await switchbot.wait(this.scanDuration * 1000);
|
|
237
|
+
// Stop to monitor
|
|
238
|
+
await switchbot.stopScan();
|
|
239
|
+
// Update HomeKit
|
|
240
|
+
await this.BLEparseStatus(switchbot.onadvertisement.serviceData);
|
|
241
|
+
await this.updateHomeKitCharacteristics();
|
|
242
|
+
})();
|
|
243
|
+
if (switchbot === undefined) {
|
|
244
|
+
await this.BLERefreshConnection(switchbot);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
async openAPIRefreshStatus() {
|
|
248
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
|
|
249
|
+
try {
|
|
250
|
+
const { body, statusCode } = await this.platform.retryRequest(this.deviceMaxRetries, this.deviceDelayBetweenRetries, `${Devices}/${this.device.deviceId}/status`, { headers: this.platform.generateHeaders() });
|
|
251
|
+
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
|
|
252
|
+
const deviceStatus = await body.json();
|
|
253
|
+
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
|
|
254
|
+
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
|
|
255
|
+
if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
|
|
256
|
+
this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
257
|
+
+ `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
|
|
258
|
+
this.openAPIparseStatus(deviceStatus);
|
|
259
|
+
this.updateHomeKitCharacteristics();
|
|
260
|
+
}
|
|
261
|
+
else {
|
|
262
|
+
this.statusCode(statusCode);
|
|
263
|
+
this.statusCode(deviceStatus.statusCode);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
catch (e) {
|
|
267
|
+
this.apiError(e);
|
|
268
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
|
|
269
|
+
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
async registerWebhook(accessory, device) {
|
|
273
|
+
if (device.webhook) {
|
|
274
|
+
this.debugLog(`${device.deviceType}: ${accessory.displayName} is listening webhook.`);
|
|
275
|
+
this.platform.webhookEventHandler[device.deviceId] = async (context) => {
|
|
276
|
+
try {
|
|
277
|
+
this.debugLog(`${device.deviceType}: ${accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
|
|
278
|
+
const { version, battery, powerState, oscillation, chargingStatus, fanSpeed } = context;
|
|
279
|
+
const { Active, SwingMode, RotationSpeed } = this.Fan;
|
|
280
|
+
const { BatteryLevel, ChargingState } = this.Battery;
|
|
281
|
+
const { FirmwareRevision } = accessory.context;
|
|
282
|
+
this.debugLog(`${device.deviceType}: ${accessory.displayName} (version, battery, powerState, oscillation, chargingStatus, fanSpeed) = `
|
|
283
|
+
+ `Webhook:(${version}, ${battery}, ${powerState}, ${oscillation}, ${chargingStatus}, ${fanSpeed}), `
|
|
284
|
+
+ `current:(${FirmwareRevision}, ${BatteryLevel}, ${Active}, ${SwingMode}, ${ChargingState}, ${RotationSpeed})`);
|
|
285
|
+
// Active
|
|
286
|
+
this.Fan.Active = powerState === 'ON' ? this.hap.Characteristic.Active.ACTIVE : this.hap.Characteristic.Active.INACTIVE;
|
|
287
|
+
// SwingMode
|
|
288
|
+
this.Fan.SwingMode = oscillation === 'on' ?
|
|
289
|
+
this.hap.Characteristic.SwingMode.SWING_ENABLED : this.hap.Characteristic.SwingMode.SWING_DISABLED;
|
|
290
|
+
// RotationSpeed
|
|
291
|
+
this.Fan.RotationSpeed = fanSpeed;
|
|
292
|
+
// ChargingState
|
|
293
|
+
this.Battery.ChargingState = chargingStatus === 'charging' ?
|
|
294
|
+
this.hap.Characteristic.ChargingState.CHARGING : this.hap.Characteristic.ChargingState.NOT_CHARGING;
|
|
295
|
+
// BatteryLevel
|
|
296
|
+
this.Battery.BatteryLevel = battery;
|
|
297
|
+
// Firmware Version
|
|
298
|
+
if (version) {
|
|
299
|
+
accessory.context.version = version;
|
|
300
|
+
accessory
|
|
301
|
+
.getService(this.hap.Service.AccessoryInformation)
|
|
302
|
+
.setCharacteristic(this.hap.Characteristic.FirmwareRevision, accessory.context.version)
|
|
303
|
+
.getCharacteristic(this.hap.Characteristic.FirmwareRevision)
|
|
304
|
+
.updateValue(accessory.context.version);
|
|
305
|
+
}
|
|
306
|
+
this.updateHomeKitCharacteristics();
|
|
307
|
+
}
|
|
308
|
+
catch (e) {
|
|
309
|
+
this.errorLog(`${device.deviceType}: ${accessory.displayName} failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
|
|
310
|
+
}
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
else {
|
|
314
|
+
this.debugLog(`${device.deviceType}: ${accessory.displayName} is not listening webhook.`);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Pushes the requested changes to the SwitchBot API
|
|
319
|
+
* commandType command parameter Description
|
|
320
|
+
* "command" "turnOff" "default" = set to OFF state
|
|
321
|
+
* "command" "turnOn" "default" = set to ON state
|
|
322
|
+
* "command" "setNightLightMode" "off, 1, or 2" = off, turn off nightlight, (1, bright) (2, dim)
|
|
323
|
+
* "command" "setWindMode" "direct, natural, sleep, or baby" = Set fan mode
|
|
324
|
+
* "command" "setWindSpeed" "{1-100} e.g. 10" = Set fan speed 1~100
|
|
325
|
+
*/
|
|
326
|
+
async pushChanges() {
|
|
327
|
+
if (!this.device.enableCloudService && this.OpenAPI) {
|
|
328
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} pushChanges enableCloudService: ${this.device.enableCloudService}`);
|
|
329
|
+
}
|
|
330
|
+
else if (this.BLE) {
|
|
331
|
+
await this.BLEpushChanges();
|
|
332
|
+
}
|
|
333
|
+
else if (this.OpenAPI && this.platform.config.credentials?.token) {
|
|
334
|
+
await this.openAPIpushChanges();
|
|
335
|
+
}
|
|
336
|
+
else {
|
|
337
|
+
await this.offlineOff();
|
|
338
|
+
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
|
|
339
|
+
+ ` ${this.device.connectionType}, pushChanges will not happen.`);
|
|
340
|
+
}
|
|
341
|
+
// Refresh the status from the API
|
|
342
|
+
interval(15000)
|
|
343
|
+
.pipe(skipWhile(() => this.fanUpdateInProgress))
|
|
344
|
+
.pipe(take(1))
|
|
345
|
+
.subscribe(async () => {
|
|
346
|
+
await this.refreshStatus();
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
async BLEpushChanges() {
|
|
350
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`);
|
|
351
|
+
if (this.Fan.Active !== this.accessory.context.Active) {
|
|
352
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`
|
|
353
|
+
+ ` On: ${this.Fan.Active} OnCached: ${this.accessory.context.Active}`);
|
|
354
|
+
const switchbot = await this.platform.connectBLE();
|
|
355
|
+
// Convert to BLE Address
|
|
356
|
+
this.device.bleMac = this.device
|
|
357
|
+
.deviceId.match(/.{1,2}/g)
|
|
358
|
+
.join(':')
|
|
359
|
+
.toLowerCase();
|
|
360
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
361
|
+
switchbot
|
|
362
|
+
.discover({
|
|
363
|
+
model: this.device.bleModel,
|
|
364
|
+
id: this.device.bleMac,
|
|
365
|
+
})
|
|
366
|
+
.then(async (device_list) => {
|
|
367
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.Fan.Active}`);
|
|
368
|
+
return await this.retryBLE({
|
|
369
|
+
max: await this.maxRetryBLE(),
|
|
370
|
+
fn: async () => {
|
|
371
|
+
if (this.Fan.Active) {
|
|
372
|
+
return await device_list[0].turnOn({ id: this.device.bleMac });
|
|
373
|
+
}
|
|
374
|
+
else {
|
|
375
|
+
return await device_list[0].turnOff({ id: this.device.bleMac });
|
|
376
|
+
}
|
|
377
|
+
},
|
|
378
|
+
});
|
|
379
|
+
})
|
|
380
|
+
.then(() => {
|
|
381
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
|
|
382
|
+
this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
383
|
+
+ `Active: ${this.Fan.Active} sent over BLE, sent successfully`);
|
|
384
|
+
this.Fan.Active = false;
|
|
385
|
+
})
|
|
386
|
+
.catch(async (e) => {
|
|
387
|
+
this.apiError(e);
|
|
388
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
|
|
389
|
+
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
390
|
+
await this.BLEPushConnection();
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
else {
|
|
394
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges,`
|
|
395
|
+
+ ` Active: ${this.Fan.Active}, ActiveCached: ${this.accessory.context.Active}`);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
async openAPIpushChanges() {
|
|
399
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
|
|
400
|
+
if (this.Fan.Active !== this.accessory.context.Active) {
|
|
401
|
+
let command = '';
|
|
402
|
+
if (this.Fan.Active) {
|
|
403
|
+
command = 'turnOn';
|
|
404
|
+
}
|
|
405
|
+
else {
|
|
406
|
+
command = 'turnOff';
|
|
407
|
+
}
|
|
408
|
+
const bodyChange = JSON.stringify({
|
|
409
|
+
command: `${command}`,
|
|
410
|
+
parameter: 'default',
|
|
411
|
+
commandType: 'command',
|
|
412
|
+
});
|
|
413
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
|
|
414
|
+
try {
|
|
415
|
+
const { body, statusCode } = await request(`${Devices}/${this.device.deviceId}/commands`, {
|
|
416
|
+
body: bodyChange,
|
|
417
|
+
method: 'POST',
|
|
418
|
+
headers: this.platform.generateHeaders(),
|
|
419
|
+
});
|
|
420
|
+
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
|
|
421
|
+
const deviceStatus = await body.json();
|
|
422
|
+
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
|
|
423
|
+
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
|
|
424
|
+
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
|
|
425
|
+
if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
|
|
426
|
+
this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
427
|
+
+ `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
|
|
428
|
+
this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
429
|
+
+ `request to SwitchBot API, body: ${JSON.stringify(JSON.parse(bodyChange))} sent successfully`);
|
|
430
|
+
}
|
|
431
|
+
else {
|
|
432
|
+
this.statusCode(statusCode);
|
|
433
|
+
this.statusCode(deviceStatus.statusCode);
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
catch (e) {
|
|
437
|
+
this.apiError(e);
|
|
438
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
|
|
439
|
+
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
else {
|
|
443
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.`
|
|
444
|
+
+ `On: ${this.Fan.Active}, ActiveCached: ${this.accessory.context.Active}`);
|
|
445
|
+
}
|
|
446
|
+
// Push RotationSpeed Update
|
|
447
|
+
if (this.Fan.Active) {
|
|
448
|
+
await this.pushRotationSpeedChanges();
|
|
449
|
+
}
|
|
450
|
+
// Push SwingMode Update
|
|
451
|
+
if (this.Fan.Active) {
|
|
452
|
+
await this.pushSwingModeChanges();
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
async pushRotationSpeedChanges() {
|
|
456
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushRotationSpeedChanges`);
|
|
457
|
+
if (this.Fan.SwingMode !== this.accessory.context.SwingMode) {
|
|
458
|
+
const bodyChange = JSON.stringify({
|
|
459
|
+
command: 'setWindSpeed',
|
|
460
|
+
parameter: `${this.Fan.RotationSpeed}`,
|
|
461
|
+
commandType: 'command',
|
|
462
|
+
});
|
|
463
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
|
|
464
|
+
try {
|
|
465
|
+
const { body, statusCode } = await request(`${Devices}/${this.device.deviceId}/commands`, {
|
|
466
|
+
body: bodyChange,
|
|
467
|
+
method: 'POST',
|
|
468
|
+
headers: this.platform.generateHeaders(),
|
|
469
|
+
});
|
|
470
|
+
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
|
|
471
|
+
const deviceStatus = await body.json();
|
|
472
|
+
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
|
|
473
|
+
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
|
|
474
|
+
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
|
|
475
|
+
if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
|
|
476
|
+
this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
477
|
+
+ `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
|
|
478
|
+
this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
479
|
+
+ `request to SwitchBot API, body: ${JSON.stringify(JSON.parse(bodyChange))} sent successfully`);
|
|
480
|
+
}
|
|
481
|
+
else {
|
|
482
|
+
this.statusCode(statusCode);
|
|
483
|
+
this.statusCode(deviceStatus.statusCode);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
catch (e) {
|
|
487
|
+
this.apiError(e);
|
|
488
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushRotationSpeedChanges with ${this.device.connectionType}`
|
|
489
|
+
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
else {
|
|
493
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, pushRotationSpeedChanges: ${this.Fan.RotationSpeed}, `
|
|
494
|
+
+ `pushRotationSpeedChangesCached: ${this.accessory.context.RotationSpeed}`);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
async pushSwingModeChanges() {
|
|
498
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushSwingModeChanges`);
|
|
499
|
+
if (this.Fan.SwingMode !== this.accessory.context.SwingMode) {
|
|
500
|
+
let parameter = '';
|
|
501
|
+
if (this.Fan.SwingMode === this.hap.Characteristic.SwingMode.SWING_ENABLED) {
|
|
502
|
+
parameter = 'on';
|
|
503
|
+
}
|
|
504
|
+
else {
|
|
505
|
+
parameter = 'off';
|
|
506
|
+
}
|
|
507
|
+
const bodyChange = JSON.stringify({
|
|
508
|
+
command: 'setOscillation',
|
|
509
|
+
parameter: `${parameter}`,
|
|
510
|
+
commandType: 'command',
|
|
511
|
+
});
|
|
512
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
|
|
513
|
+
try {
|
|
514
|
+
const { body, statusCode } = await request(`${Devices}/${this.device.deviceId}/commands`, {
|
|
515
|
+
body: bodyChange,
|
|
516
|
+
method: 'POST',
|
|
517
|
+
headers: this.platform.generateHeaders(),
|
|
518
|
+
});
|
|
519
|
+
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
|
|
520
|
+
const deviceStatus = await body.json();
|
|
521
|
+
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
|
|
522
|
+
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
|
|
523
|
+
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
|
|
524
|
+
if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
|
|
525
|
+
this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
526
|
+
+ `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
|
|
527
|
+
this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
528
|
+
+ `request to SwitchBot API, body: ${JSON.stringify(JSON.parse(bodyChange))} sent successfully`);
|
|
529
|
+
}
|
|
530
|
+
else {
|
|
531
|
+
this.statusCode(statusCode);
|
|
532
|
+
this.statusCode(deviceStatus.statusCode);
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
catch (e) {
|
|
536
|
+
this.apiError(e);
|
|
537
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushSwingModeChanges with ${this.device.connectionType}`
|
|
538
|
+
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
else {
|
|
542
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, SwingMode: ${this.Fan.SwingMode}, `
|
|
543
|
+
+ `SwingModeCached: ${this.accessory.context.SwingMode}`);
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
/**
|
|
547
|
+
* Handle requests to set the value of the "On" characteristic
|
|
548
|
+
*/
|
|
549
|
+
async ActiveSet(value) {
|
|
550
|
+
if (this.Fan.Active === this.accessory.context.Active) {
|
|
551
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Active: ${value}`);
|
|
552
|
+
}
|
|
553
|
+
else {
|
|
554
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Active: ${value}`);
|
|
555
|
+
}
|
|
556
|
+
this.Fan.Active = value;
|
|
557
|
+
this.doFanUpdate.next();
|
|
558
|
+
}
|
|
559
|
+
/**
|
|
560
|
+
* Handle requests to set the value of the "On" characteristic
|
|
561
|
+
*/
|
|
562
|
+
async RotationSpeedSet(value) {
|
|
563
|
+
if (this.Fan.RotationSpeed === this.accessory.context.RotationSpeed) {
|
|
564
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set RotationSpeed: ${value}`);
|
|
565
|
+
}
|
|
566
|
+
else {
|
|
567
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set RotationSpeed: ${value}`);
|
|
568
|
+
}
|
|
569
|
+
this.Fan.RotationSpeed = value;
|
|
570
|
+
this.doFanUpdate.next();
|
|
571
|
+
}
|
|
572
|
+
/**
|
|
573
|
+
* Handle requests to set the value of the "On" characteristic
|
|
574
|
+
*/
|
|
575
|
+
async SwingModeSet(value) {
|
|
576
|
+
if (this.Fan.SwingMode === this.accessory.context.SwingMode) {
|
|
577
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set SwingMode: ${value}`);
|
|
578
|
+
}
|
|
579
|
+
else {
|
|
580
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set SwingMode: ${value}`);
|
|
581
|
+
}
|
|
582
|
+
this.Fan.SwingMode = value;
|
|
583
|
+
this.doFanUpdate.next();
|
|
584
|
+
}
|
|
585
|
+
/**
|
|
586
|
+
* Handle requests to set the value of the "On" characteristic
|
|
587
|
+
*/
|
|
588
|
+
async OnSet(value) {
|
|
589
|
+
if (this.LightBulb.On === this.accessory.context.On) {
|
|
590
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set On: ${value}`);
|
|
591
|
+
}
|
|
592
|
+
else {
|
|
593
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set On: ${value}`);
|
|
594
|
+
}
|
|
595
|
+
this.LightBulb.On = value;
|
|
596
|
+
this.doFanUpdate.next();
|
|
597
|
+
}
|
|
598
|
+
/**
|
|
599
|
+
* Handle requests to set the value of the "Brightness" characteristic
|
|
600
|
+
*/
|
|
601
|
+
async BrightnessSet(value) {
|
|
602
|
+
if (this.LightBulb.Brightness === this.accessory.context.Brightness) {
|
|
603
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Brightness: ${value}`);
|
|
604
|
+
}
|
|
605
|
+
else if (this.LightBulb.On) {
|
|
606
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Brightness: ${value}`);
|
|
607
|
+
}
|
|
608
|
+
else {
|
|
609
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Brightness: ${value}`);
|
|
610
|
+
}
|
|
611
|
+
this.LightBulb.Brightness = value;
|
|
612
|
+
this.doFanUpdate.next();
|
|
613
|
+
}
|
|
614
|
+
async updateHomeKitCharacteristics() {
|
|
615
|
+
// Active
|
|
616
|
+
if (this.Fan.Active === undefined) {
|
|
617
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Active: ${this.Fan.Active}`);
|
|
618
|
+
}
|
|
619
|
+
else {
|
|
620
|
+
this.accessory.context.Active = this.Fan.Active;
|
|
621
|
+
this.Fan.Service.updateCharacteristic(this.hap.Characteristic.Active, this.Fan.Active);
|
|
622
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Active: ${this.Fan.Active}`);
|
|
623
|
+
}
|
|
624
|
+
// RotationSpeed
|
|
625
|
+
if (this.Fan.RotationSpeed === undefined) {
|
|
626
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} RotationSpeed: ${this.Fan.RotationSpeed}`);
|
|
627
|
+
}
|
|
628
|
+
else {
|
|
629
|
+
this.accessory.context.RotationSpeed = this.Fan.RotationSpeed;
|
|
630
|
+
this.Fan.Service.updateCharacteristic(this.hap.Characteristic.RotationSpeed, this.Fan.RotationSpeed);
|
|
631
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic RotationSpeed: ${this.Fan.RotationSpeed}`);
|
|
632
|
+
}
|
|
633
|
+
// SwingMode
|
|
634
|
+
if (this.Fan.SwingMode === undefined) {
|
|
635
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} SwingMode: ${this.Fan.SwingMode}`);
|
|
636
|
+
}
|
|
637
|
+
else {
|
|
638
|
+
this.accessory.context.SwingMode = this.Fan.SwingMode;
|
|
639
|
+
this.Fan.Service.updateCharacteristic(this.hap.Characteristic.SwingMode, this.Fan.SwingMode);
|
|
640
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic SwingMode: ${this.Fan.SwingMode}`);
|
|
641
|
+
}
|
|
642
|
+
// BateryLevel
|
|
643
|
+
if (this.Battery.BatteryLevel === undefined) {
|
|
644
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}`);
|
|
645
|
+
}
|
|
646
|
+
else {
|
|
647
|
+
this.accessory.context.BatteryLevel = this.Battery.BatteryLevel;
|
|
648
|
+
this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.Battery.BatteryLevel);
|
|
649
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.Battery.BatteryLevel}`);
|
|
650
|
+
}
|
|
651
|
+
// ChargingState
|
|
652
|
+
if (this.Battery.ChargingState === undefined) {
|
|
653
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ChargingState: ${this.Battery.ChargingState}`);
|
|
654
|
+
}
|
|
655
|
+
else {
|
|
656
|
+
this.accessory.context.ChargingState = this.Battery.ChargingState;
|
|
657
|
+
this.Battery.Service.updateCharacteristic(this.hap.Characteristic.ChargingState, this.Battery.ChargingState);
|
|
658
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic ChargingState: ${this.Battery.ChargingState}`);
|
|
659
|
+
}
|
|
660
|
+
// StatusLowBattery
|
|
661
|
+
if (this.Battery.StatusLowBattery === undefined) {
|
|
662
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.Battery.StatusLowBattery}`);
|
|
663
|
+
}
|
|
664
|
+
else {
|
|
665
|
+
this.accessory.context.StatusLowBattery = this.Battery.StatusLowBattery;
|
|
666
|
+
this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.Battery.StatusLowBattery);
|
|
667
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
|
|
668
|
+
+ ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
async BLEPushConnection() {
|
|
672
|
+
if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
|
|
673
|
+
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Push Changes`);
|
|
674
|
+
await this.openAPIpushChanges();
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
async BLERefreshConnection(switchbot) {
|
|
678
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot:`
|
|
679
|
+
+ ` ${JSON.stringify(switchbot)}`);
|
|
680
|
+
if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
|
|
681
|
+
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Refresh Status`);
|
|
682
|
+
await this.openAPIRefreshStatus();
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
async offlineOff() {
|
|
686
|
+
if (this.device.offline) {
|
|
687
|
+
this.Fan.Service.updateCharacteristic(this.hap.Characteristic.Active, this.hap.Characteristic.Active.INACTIVE);
|
|
688
|
+
this.Fan.Service.updateCharacteristic(this.hap.Characteristic.RotationSpeed, 0);
|
|
689
|
+
this.Fan.Service.updateCharacteristic(this.hap.Characteristic.SwingMode, this.hap.Characteristic.SwingMode.SWING_DISABLED);
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
async apiError(e) {
|
|
693
|
+
this.Fan.Service.updateCharacteristic(this.hap.Characteristic.Active, e);
|
|
694
|
+
this.Fan.Service.updateCharacteristic(this.hap.Characteristic.RotationSpeed, e);
|
|
695
|
+
this.Fan.Service.updateCharacteristic(this.hap.Characteristic.SwingMode, e);
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
//# sourceMappingURL=fan.js.map
|