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