@switchbot/homebridge-switchbot 0.1.2-beta.7 → 0.2.0-alpha.1
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/.github/ISSUE_TEMPLATE/feature-request.yml +1 -1
- package/.github/dependabot.yml +2 -0
- package/.github/workflows/codeql-analysis.yml +71 -0
- package/CHANGELOG.md +5 -1
- package/README.md +2 -1
- package/config.schema.json +420 -471
- package/dist/devices/bots.d.ts +19 -5
- package/dist/devices/bots.d.ts.map +1 -1
- package/dist/devices/bots.js +172 -148
- package/dist/devices/bots.js.map +1 -1
- package/dist/devices/bulb.d.ts +8 -6
- package/dist/devices/bulb.d.ts.map +1 -1
- package/dist/devices/bulb.js +45 -28
- package/dist/devices/bulb.js.map +1 -1
- package/dist/devices/contact.d.ts +10 -5
- package/dist/devices/contact.d.ts.map +1 -1
- package/dist/devices/contact.js +94 -63
- package/dist/devices/contact.js.map +1 -1
- package/dist/devices/curtains.d.ts +14 -5
- package/dist/devices/curtains.d.ts.map +1 -1
- package/dist/devices/curtains.js +174 -120
- package/dist/devices/curtains.js.map +1 -1
- package/dist/devices/humidifiers.d.ts +6 -6
- package/dist/devices/humidifiers.d.ts.map +1 -1
- package/dist/devices/humidifiers.js +78 -94
- package/dist/devices/humidifiers.js.map +1 -1
- package/dist/devices/indoorcam.d.ts +43 -0
- package/dist/devices/indoorcam.d.ts.map +1 -0
- package/dist/devices/indoorcam.js +241 -0
- package/dist/devices/indoorcam.js.map +1 -0
- package/dist/devices/meters.d.ts +8 -5
- package/dist/devices/meters.d.ts.map +1 -1
- package/dist/devices/meters.js +126 -92
- package/dist/devices/meters.js.map +1 -1
- package/dist/devices/motion.d.ts +9 -5
- package/dist/devices/motion.d.ts.map +1 -1
- package/dist/devices/motion.js +74 -59
- package/dist/devices/motion.js.map +1 -1
- package/dist/devices/plugs.d.ts +5 -4
- package/dist/devices/plugs.d.ts.map +1 -1
- package/dist/devices/plugs.js +65 -56
- package/dist/devices/plugs.js.map +1 -1
- package/dist/irdevices/airconditioners.d.ts +3 -3
- package/dist/irdevices/airconditioners.d.ts.map +1 -1
- package/dist/irdevices/airconditioners.js +12 -7
- package/dist/irdevices/airconditioners.js.map +1 -1
- package/dist/irdevices/airpurifiers.d.ts +3 -3
- package/dist/irdevices/airpurifiers.d.ts.map +1 -1
- package/dist/irdevices/airpurifiers.js +1 -1
- package/dist/irdevices/airpurifiers.js.map +1 -1
- package/dist/irdevices/cameras.d.ts +3 -3
- package/dist/irdevices/cameras.d.ts.map +1 -1
- package/dist/irdevices/cameras.js +1 -1
- package/dist/irdevices/cameras.js.map +1 -1
- package/dist/irdevices/fans.d.ts +3 -3
- package/dist/irdevices/fans.d.ts.map +1 -1
- package/dist/irdevices/fans.js +11 -12
- package/dist/irdevices/fans.js.map +1 -1
- package/dist/irdevices/lights.d.ts +3 -3
- package/dist/irdevices/lights.d.ts.map +1 -1
- package/dist/irdevices/lights.js +1 -1
- package/dist/irdevices/lights.js.map +1 -1
- package/dist/irdevices/others.d.ts +3 -3
- package/dist/irdevices/others.d.ts.map +1 -1
- package/dist/irdevices/others.js +8 -8
- package/dist/irdevices/others.js.map +1 -1
- package/dist/irdevices/tvs.d.ts +3 -3
- package/dist/irdevices/tvs.d.ts.map +1 -1
- package/dist/irdevices/tvs.js +16 -6
- package/dist/irdevices/tvs.js.map +1 -1
- package/dist/irdevices/vacuumcleaners.d.ts +3 -3
- package/dist/irdevices/vacuumcleaners.d.ts.map +1 -1
- package/dist/irdevices/vacuumcleaners.js +2 -3
- package/dist/irdevices/vacuumcleaners.js.map +1 -1
- package/dist/irdevices/waterheaters.d.ts +3 -3
- package/dist/irdevices/waterheaters.d.ts.map +1 -1
- package/dist/irdevices/waterheaters.js +2 -2
- package/dist/irdevices/waterheaters.js.map +1 -1
- package/dist/platform.d.ts +14 -5
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +420 -418
- package/dist/platform.js.map +1 -1
- package/dist/settings.d.ts +18 -15
- package/dist/settings.d.ts.map +1 -1
- package/package.json +65 -65
- package/src/devices/bots.ts +188 -165
- package/src/devices/bulb.ts +48 -40
- package/src/devices/contact.ts +100 -73
- package/src/devices/curtains.ts +192 -140
- package/src/devices/humidifiers.ts +82 -124
- package/src/devices/indoorcam.ts +303 -0
- package/src/devices/meters.ts +130 -110
- package/src/devices/motion.ts +80 -69
- package/src/devices/plugs.ts +67 -65
- package/src/irdevices/airconditioners.ts +14 -10
- package/src/irdevices/airpurifiers.ts +4 -4
- package/src/irdevices/cameras.ts +4 -4
- package/src/irdevices/fans.ts +14 -18
- package/src/irdevices/lights.ts +4 -4
- package/src/irdevices/others.ts +12 -14
- package/src/irdevices/tvs.ts +21 -10
- package/src/irdevices/vacuumcleaners.ts +5 -9
- package/src/irdevices/waterheaters.ts +5 -5
- package/src/platform.ts +455 -619
- package/src/settings.ts +19 -15
- package/.github/workflows/issue-validate.yml +0 -18
package/src/devices/bots.ts
CHANGED
|
@@ -2,8 +2,7 @@ import { Service, PlatformAccessory, CharacteristicValue, MacAddress } from 'hom
|
|
|
2
2
|
import { SwitchBotPlatform } from '../platform';
|
|
3
3
|
import { interval, Subject } from 'rxjs';
|
|
4
4
|
import { debounceTime, skipWhile, tap } from 'rxjs/operators';
|
|
5
|
-
import { DeviceURL, device,
|
|
6
|
-
import { AxiosResponse } from 'axios';
|
|
5
|
+
import { DeviceURL, device, devicesConfig } from '../settings';
|
|
7
6
|
|
|
8
7
|
/**
|
|
9
8
|
* Platform Accessory
|
|
@@ -15,7 +14,7 @@ export class Bot {
|
|
|
15
14
|
|
|
16
15
|
SwitchOn!: CharacteristicValue;
|
|
17
16
|
OutletInUse!: CharacteristicValue;
|
|
18
|
-
deviceStatus!:
|
|
17
|
+
deviceStatus!: { statusCode: number; body: { deviceId: string; deviceType: string; hubDeviceId: string; power: string; }; message: string; };
|
|
19
18
|
RunTimer!: NodeJS.Timeout;
|
|
20
19
|
ScanDuration: number;
|
|
21
20
|
TargetState;
|
|
@@ -40,15 +39,15 @@ export class Bot {
|
|
|
40
39
|
constructor(
|
|
41
40
|
private readonly platform: SwitchBotPlatform,
|
|
42
41
|
private accessory: PlatformAccessory,
|
|
43
|
-
public device: device,
|
|
42
|
+
public device: device & devicesConfig,
|
|
44
43
|
) {
|
|
45
44
|
// default placeholders
|
|
46
45
|
this.SwitchOn = false;
|
|
47
46
|
this.ScanDuration = this.platform.config.options!.refreshRate!;
|
|
48
|
-
if (
|
|
47
|
+
if (device.bot?.mode !== 'switch') {
|
|
49
48
|
this.OutletInUse = true;
|
|
50
49
|
}
|
|
51
|
-
if (
|
|
50
|
+
if (device.ble) {
|
|
52
51
|
this.SwitchOn = false;
|
|
53
52
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
54
53
|
const SwitchBot = require('node-switchbot');
|
|
@@ -56,9 +55,7 @@ export class Bot {
|
|
|
56
55
|
const colon = device.deviceId!.match(/.{1,2}/g);
|
|
57
56
|
const bleMac = colon!.join(':'); //returns 1A:23:B4:56:78:9A;
|
|
58
57
|
this.device.bleMac = bleMac.toLowerCase();
|
|
59
|
-
|
|
60
|
-
this.platform.log.warn(this.device.bleMac);
|
|
61
|
-
}
|
|
58
|
+
this.platform.device(this.device.bleMac);
|
|
62
59
|
}
|
|
63
60
|
|
|
64
61
|
// this is subject we use to track when we need to POST changes to the SwitchBot API
|
|
@@ -77,14 +74,14 @@ export class Bot {
|
|
|
77
74
|
|
|
78
75
|
// get the LightBulb service if it exists, otherwise create a new LightBulb service
|
|
79
76
|
// you can create multiple services for each accessory
|
|
80
|
-
if (
|
|
77
|
+
if (device.bot?.mode === 'switch') {
|
|
81
78
|
(this.service =
|
|
82
79
|
accessory.getService(this.platform.Service.Switch) ||
|
|
83
|
-
accessory.addService(this.platform.Service.Switch)), `${
|
|
80
|
+
accessory.addService(this.platform.Service.Switch)), `${accessory.displayName} Switch`;
|
|
84
81
|
} else {
|
|
85
82
|
(this.service =
|
|
86
83
|
accessory.getService(this.platform.Service.Outlet) ||
|
|
87
|
-
accessory.addService(this.platform.Service.Outlet)), `${
|
|
84
|
+
accessory.addService(this.platform.Service.Outlet)), `${accessory.displayName} Outlet`;
|
|
88
85
|
}
|
|
89
86
|
|
|
90
87
|
// To avoid "Cannot add a Service with the same UUID another Service without also defining a unique 'subtype' property." error,
|
|
@@ -134,22 +131,28 @@ export class Bot {
|
|
|
134
131
|
/**
|
|
135
132
|
* Parse the device status from the SwitchBot api
|
|
136
133
|
*/
|
|
137
|
-
parseStatus() {
|
|
138
|
-
if (this.
|
|
139
|
-
this.platform.
|
|
134
|
+
async parseStatus() {
|
|
135
|
+
if (this.device.ble) {
|
|
136
|
+
this.platform.device('BLE');
|
|
137
|
+
await this.BLEparseStatus();
|
|
140
138
|
} else {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
139
|
+
this.platform.device('OpenAPI');
|
|
140
|
+
await this.openAPIparseStatus();
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
private async BLEparseStatus() {
|
|
145
|
+
this.platform.debug('Bots BLE Device RefreshStatus');
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
private async openAPIparseStatus() {
|
|
149
|
+
if (this.device.bot?.mode !== 'switch') {
|
|
150
|
+
this.OutletInUse = true;
|
|
151
|
+
this.SwitchOn = false;
|
|
152
|
+
this.platform.debug(`Bot ${this.accessory.displayName} OutletInUse: ${this.OutletInUse} On: ${this.SwitchOn}`);
|
|
153
|
+
} else {
|
|
154
|
+
this.SwitchOn = false;
|
|
155
|
+
this.platform.debug(`Bot ${this.accessory.displayName} On: ${this.SwitchOn}`);
|
|
153
156
|
}
|
|
154
157
|
}
|
|
155
158
|
|
|
@@ -157,62 +160,68 @@ export class Bot {
|
|
|
157
160
|
* Asks the SwitchBot API for the latest device information
|
|
158
161
|
*/
|
|
159
162
|
async refreshStatus() {
|
|
160
|
-
if (this.
|
|
161
|
-
this.platform.
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
163
|
+
if (this.device.ble) {
|
|
164
|
+
this.platform.device('BLE');
|
|
165
|
+
await this.BLErefreshStatus();
|
|
166
|
+
} else {
|
|
167
|
+
this.platform.device('OpenAPI');
|
|
168
|
+
await this.openAPIRefreshStatus();
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
private async BLErefreshStatus() {
|
|
173
|
+
this.platform.device('Bot BLE Device refreshStatus');
|
|
174
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
175
|
+
const Switchbot = require('node-switchbot');
|
|
176
|
+
const switchbot = new Switchbot();
|
|
177
|
+
const colon = this.device.deviceId!.match(/.{1,2}/g);
|
|
178
|
+
const bleMac = colon!.join(':'); //returns 1A:23:B4:56:78:9A;
|
|
179
|
+
this.device.bleMac = bleMac.toLowerCase();
|
|
180
|
+
this.platform.device(this.device.bleMac!);
|
|
181
|
+
switchbot.onadvertisement = (ad: any) => {
|
|
182
|
+
this.platform.debug(JSON.stringify(ad, null, ' '));
|
|
183
|
+
this.platform.device(`ad: ${JSON.stringify(ad)}`);
|
|
184
|
+
};
|
|
185
|
+
switchbot
|
|
186
|
+
.startScan({
|
|
187
|
+
id: this.device.bleMac,
|
|
188
|
+
})
|
|
189
|
+
.then(() => {
|
|
190
|
+
return switchbot.wait(this.platform.config.options!.refreshRate! * 1000);
|
|
191
|
+
})
|
|
192
|
+
.then(() => {
|
|
193
|
+
switchbot.stopScan();
|
|
194
|
+
})
|
|
195
|
+
.catch(async (error: any) => {
|
|
196
|
+
this.platform.log.error(error);
|
|
197
|
+
this.openAPIRefreshStatus();
|
|
198
|
+
});
|
|
199
|
+
setInterval(() => {
|
|
200
|
+
this.platform.log.info('Start scan ' + this.device.deviceName + '(' + this.device.bleMac + ')');
|
|
175
201
|
switchbot
|
|
176
202
|
.startScan({
|
|
177
|
-
|
|
203
|
+
mode: 'T',
|
|
204
|
+
id: bleMac,
|
|
178
205
|
})
|
|
179
206
|
.then(() => {
|
|
180
207
|
return switchbot.wait(this.platform.config.options!.refreshRate! * 1000);
|
|
181
208
|
})
|
|
182
209
|
.then(() => {
|
|
183
210
|
switchbot.stopScan();
|
|
211
|
+
this.platform.log.info('Stop scan ' + this.device.deviceName + '(' + this.device.bleMac + ')');
|
|
184
212
|
})
|
|
185
213
|
.catch(async (error: any) => {
|
|
186
214
|
this.platform.log.error(error);
|
|
187
|
-
|
|
215
|
+
this.openAPIRefreshStatus();
|
|
188
216
|
});
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
.startScan({
|
|
193
|
-
mode: 'T',
|
|
194
|
-
id: bleMac,
|
|
195
|
-
})
|
|
196
|
-
.then(() => {
|
|
197
|
-
return switchbot.wait(this.platform.config.options!.refreshRate! * 1000);
|
|
198
|
-
})
|
|
199
|
-
.then(() => {
|
|
200
|
-
switchbot.stopScan();
|
|
201
|
-
this.platform.log.info('Stop scan ' + this.device.deviceName + '(' + this.device.bleMac + ')');
|
|
202
|
-
})
|
|
203
|
-
.catch(async (error: any) => {
|
|
204
|
-
this.platform.log.error(error);
|
|
205
|
-
await this.openAPIRefreshStatus();
|
|
206
|
-
});
|
|
207
|
-
}, this.platform.config.options!.refreshRate! * 60000);
|
|
208
|
-
} else {
|
|
209
|
-
this.openAPIRefreshStatus();
|
|
210
|
-
}
|
|
217
|
+
this.parseStatus();
|
|
218
|
+
this.updateHomeKitCharacteristics();
|
|
219
|
+
}, this.platform.config.options!.refreshRate! * 60000);
|
|
211
220
|
}
|
|
212
221
|
|
|
213
|
-
private openAPIRefreshStatus() {
|
|
222
|
+
private async openAPIRefreshStatus() {
|
|
214
223
|
try {
|
|
215
|
-
|
|
224
|
+
this.deviceStatus = {
|
|
216
225
|
statusCode: 100,
|
|
217
226
|
body: {
|
|
218
227
|
deviceId: this.device.deviceId!,
|
|
@@ -222,14 +231,11 @@ export class Bot {
|
|
|
222
231
|
},
|
|
223
232
|
message: 'success',
|
|
224
233
|
};
|
|
225
|
-
this.deviceStatus = deviceStatus;
|
|
226
234
|
this.parseStatus();
|
|
227
235
|
this.updateHomeKitCharacteristics();
|
|
228
236
|
} catch (e: any) {
|
|
229
|
-
this.platform.log.error(
|
|
230
|
-
|
|
231
|
-
JSON.stringify(e.message),
|
|
232
|
-
this.platform.debug(`Bot ${this.accessory.displayName} - ${JSON.stringify(e)}`));
|
|
237
|
+
this.platform.log.error(`Bot ${this.accessory.displayName} failed to refresh status, Error Message: ${JSON.stringify(e.message)}`);
|
|
238
|
+
this.platform.debug(`Bot ${this.accessory.displayName}, Error: ${JSON.stringify(e)}`);
|
|
233
239
|
this.apiError(e);
|
|
234
240
|
}
|
|
235
241
|
}
|
|
@@ -242,117 +248,134 @@ export class Bot {
|
|
|
242
248
|
* Bot - "command" "press" "default" = trigger press
|
|
243
249
|
*/
|
|
244
250
|
async pushChanges() {
|
|
245
|
-
if (this.
|
|
246
|
-
this.platform.
|
|
247
|
-
|
|
248
|
-
this.platform.log.info('Target state of Bot setting: ' + (this.TargetState ? 'ON' : 'OFF'));
|
|
249
|
-
this.switchbot
|
|
250
|
-
.discover({ duration: this.ScanDuration, model: 'H', quick: true, id: (this.device.bleMac!) })
|
|
251
|
-
.then((device_list: any) => {
|
|
252
|
-
this.platform.log.info('Scan done.');
|
|
253
|
-
let targetDevice: any = null;
|
|
254
|
-
for (const device of device_list) {
|
|
255
|
-
// log.info(device.modelName, device.address);
|
|
256
|
-
if (device.address === this.device.bleMac) {
|
|
257
|
-
targetDevice = device;
|
|
258
|
-
break;
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
if (!targetDevice) {
|
|
262
|
-
this.platform.log.info('No device was found during scan.');
|
|
263
|
-
return new Promise((resolve, reject) => {
|
|
264
|
-
reject(new Error('No device was found during scan.'));
|
|
265
|
-
});
|
|
266
|
-
} else {
|
|
267
|
-
this.platform.log.info(targetDevice.modelName + ' (' + targetDevice.address + ') was found.');
|
|
268
|
-
// Set event handers
|
|
269
|
-
targetDevice.onconnect = () => {
|
|
270
|
-
// log.info('Connected.');
|
|
271
|
-
};
|
|
272
|
-
targetDevice.ondisconnect = () => {
|
|
273
|
-
// log.info('Disconnected.');
|
|
274
|
-
};
|
|
275
|
-
this.platform.log.info('Bot is running...');
|
|
276
|
-
return this.setTargetDeviceState(targetDevice, this.TargetState);
|
|
277
|
-
}
|
|
278
|
-
})
|
|
279
|
-
.then(() => {
|
|
280
|
-
this.platform.log.info('Done.');
|
|
281
|
-
this.SwitchOn = this.TargetState;
|
|
282
|
-
this.RunTimer = setTimeout(() => {
|
|
283
|
-
this.service?.getCharacteristic(this.platform.Characteristic.On).updateValue(this.SwitchOn);
|
|
284
|
-
}, 500);
|
|
285
|
-
this.platform.log.info('Bot state has been set to: ' + (this.SwitchOn ? 'ON' : 'OFF'));
|
|
286
|
-
})
|
|
287
|
-
.catch((error: any) => {
|
|
288
|
-
this.platform.log.error(error);
|
|
289
|
-
this.RunTimer = setTimeout(() => {
|
|
290
|
-
this.service?.getCharacteristic(this.platform.Characteristic.On).updateValue(this.SwitchOn);
|
|
291
|
-
}, 500);
|
|
292
|
-
this.platform.log.info('Bot state failed to be set to: ' + (this.TargetState ? 'ON' : 'OFF'));
|
|
293
|
-
});
|
|
251
|
+
if (this.device.ble) {
|
|
252
|
+
this.platform.device('BLE');
|
|
253
|
+
await this.BLEpushChanges();
|
|
294
254
|
} else {
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
parameter: 'default',
|
|
298
|
-
} as any;
|
|
299
|
-
|
|
300
|
-
if (this.platform.config.options?.bot?.device_switch?.includes(this.device.deviceId!) && this.SwitchOn) {
|
|
301
|
-
payload.command = 'turnOn';
|
|
302
|
-
this.SwitchOn = true;
|
|
303
|
-
this.platform.debug(`Switch Mode, Turning ${this.SwitchOn}`);
|
|
304
|
-
} else if (this.platform.config.options?.bot?.device_switch?.includes(this.device.deviceId!) && !this.SwitchOn) {
|
|
305
|
-
payload.command = 'turnOff';
|
|
306
|
-
this.SwitchOn = false;
|
|
307
|
-
this.platform.debug(`Switch Mode, Turning ${this.SwitchOn}`);
|
|
308
|
-
} else if (this.platform.config.options?.bot?.device_press?.includes(this.device.deviceId!)) {
|
|
309
|
-
payload.command = 'press';
|
|
310
|
-
this.platform.debug('Press Mode');
|
|
311
|
-
this.SwitchOn = false;
|
|
312
|
-
} else {
|
|
313
|
-
throw new Error('Bot Device Paramters not set for this Bot.');
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
this.platform.log.info(
|
|
317
|
-
'Sending request for',
|
|
318
|
-
this.accessory.displayName,
|
|
319
|
-
'to SwitchBot API. command:',
|
|
320
|
-
payload.command,
|
|
321
|
-
'parameter:',
|
|
322
|
-
payload.parameter,
|
|
323
|
-
'commandType:',
|
|
324
|
-
payload.commandType,
|
|
325
|
-
);
|
|
326
|
-
this.platform.debug(`Bot ${this.accessory.displayName} pushChanges - ${JSON.stringify(payload)}`);
|
|
327
|
-
|
|
328
|
-
// Make the API request
|
|
329
|
-
const push = await this.platform.axios.post(`${DeviceURL}/${this.device.deviceId}/commands`, payload);
|
|
330
|
-
this.platform.debug(`Bot ${this.accessory.displayName} Changes pushed - ${push.data}`);
|
|
331
|
-
this.statusCode(push);
|
|
255
|
+
this.platform.device('OpenAPI');
|
|
256
|
+
await this.openAPIpushChanges();
|
|
332
257
|
}
|
|
333
258
|
this.refreshStatus();
|
|
334
259
|
}
|
|
335
260
|
|
|
261
|
+
private async BLEpushChanges() {
|
|
262
|
+
this.platform.device('Bot BLE Device pushChanges');
|
|
263
|
+
// Target state has been changed.
|
|
264
|
+
this.platform.log.info('Target state of Bot setting: ' + (this.TargetState ? 'ON' : 'OFF'));
|
|
265
|
+
this.switchbot
|
|
266
|
+
.discover({ duration: this.ScanDuration, model: 'H', quick: true, id: (this.device.bleMac!) })
|
|
267
|
+
.then((device_list: any) => {
|
|
268
|
+
this.platform.log.info('Scan done.');
|
|
269
|
+
let targetDevice: any = null;
|
|
270
|
+
for (const device of device_list) {
|
|
271
|
+
// log.info(device.modelName, device.address);
|
|
272
|
+
if (device.address === this.device.bleMac) {
|
|
273
|
+
targetDevice = device;
|
|
274
|
+
break;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
if (!targetDevice) {
|
|
278
|
+
this.platform.log.info('No device was found during scan.');
|
|
279
|
+
return new Promise((resolve, reject) => {
|
|
280
|
+
reject(new Error('No device was found during scan.'));
|
|
281
|
+
});
|
|
282
|
+
} else {
|
|
283
|
+
this.platform.log.info(targetDevice.modelName + ' (' + targetDevice.address + ') was found.');
|
|
284
|
+
// Set event handers
|
|
285
|
+
targetDevice.onconnect = () => {
|
|
286
|
+
// log.info('Connected.');
|
|
287
|
+
};
|
|
288
|
+
targetDevice.ondisconnect = () => {
|
|
289
|
+
// log.info('Disconnected.');
|
|
290
|
+
};
|
|
291
|
+
this.platform.log.info('Bot is running...');
|
|
292
|
+
return this.setTargetDeviceState(targetDevice, this.TargetState);
|
|
293
|
+
}
|
|
294
|
+
})
|
|
295
|
+
.then(() => {
|
|
296
|
+
this.platform.log.info('Done.');
|
|
297
|
+
this.SwitchOn = this.TargetState;
|
|
298
|
+
this.RunTimer = setTimeout(() => {
|
|
299
|
+
this.service?.getCharacteristic(this.platform.Characteristic.On).updateValue(this.SwitchOn);
|
|
300
|
+
}, 500);
|
|
301
|
+
this.platform.log.info('Bot state has been set to: ' + (this.SwitchOn ? 'ON' : 'OFF'));
|
|
302
|
+
})
|
|
303
|
+
.catch((error: any) => {
|
|
304
|
+
this.platform.log.error(error);
|
|
305
|
+
this.RunTimer = setTimeout(() => {
|
|
306
|
+
this.service?.getCharacteristic(this.platform.Characteristic.On).updateValue(this.SwitchOn);
|
|
307
|
+
}, 500);
|
|
308
|
+
this.platform.log.info('Bot state failed to be set to: ' + (this.TargetState ? 'ON' : 'OFF'));
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
private async openAPIpushChanges() {
|
|
313
|
+
const payload = {
|
|
314
|
+
commandType: 'command',
|
|
315
|
+
parameter: 'default',
|
|
316
|
+
} as any;
|
|
317
|
+
|
|
318
|
+
if (this.device.bot?.mode === 'switch' && (this.SwitchOn)) {
|
|
319
|
+
payload.command = 'turnOn';
|
|
320
|
+
this.SwitchOn = true;
|
|
321
|
+
this.platform.debug(`Switch Mode, Turning ${this.SwitchOn}`);
|
|
322
|
+
} else if (this.device.bot?.mode === 'switch' && (!this.SwitchOn)) {
|
|
323
|
+
payload.command = 'turnOff';
|
|
324
|
+
this.SwitchOn = false;
|
|
325
|
+
this.platform.debug(`Switch Mode, Turning ${this.SwitchOn}`);
|
|
326
|
+
} else if (this.device.bot?.mode === 'press') {
|
|
327
|
+
payload.command = 'press';
|
|
328
|
+
this.platform.debug('Press Mode');
|
|
329
|
+
this.SwitchOn = false;
|
|
330
|
+
} else {
|
|
331
|
+
throw new Error('Bot Device Paramters not set for this Bot.');
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
this.platform.log.info(
|
|
335
|
+
'Sending request for',
|
|
336
|
+
this.accessory.displayName,
|
|
337
|
+
'to SwitchBot API. command:',
|
|
338
|
+
payload.command,
|
|
339
|
+
'parameter:',
|
|
340
|
+
payload.parameter,
|
|
341
|
+
'commandType:',
|
|
342
|
+
payload.commandType,
|
|
343
|
+
);
|
|
344
|
+
this.platform.debug(`Bot ${this.accessory.displayName} pushchanges: ${JSON.stringify(payload)}`);
|
|
345
|
+
|
|
346
|
+
// Make the API request
|
|
347
|
+
const push: any = (await this.platform.axios.post(`${DeviceURL}/${this.device.deviceId}/commands`, payload));
|
|
348
|
+
this.platform.debug(`Bot ${this.accessory.displayName} Changes pushed: ${JSON.stringify(push.data)}`);
|
|
349
|
+
this.statusCode(push);
|
|
350
|
+
}
|
|
351
|
+
|
|
336
352
|
/**
|
|
337
353
|
* Updates the status for each of the HomeKit Characteristics
|
|
338
354
|
*/
|
|
339
355
|
updateHomeKitCharacteristics() {
|
|
340
|
-
if (this.SwitchOn
|
|
356
|
+
if (this.SwitchOn === undefined) {
|
|
357
|
+
this.platform.debug(`Bot ${this.accessory.displayName} On: ${this.SwitchOn}`);
|
|
358
|
+
} else {
|
|
341
359
|
this.service.updateCharacteristic(this.platform.Characteristic.On, this.SwitchOn);
|
|
360
|
+
this.platform.device(`Bot ${this.accessory.displayName} updateCharacteristic On: ${this.SwitchOn}`);
|
|
342
361
|
}
|
|
343
|
-
if (
|
|
362
|
+
if (this.OutletInUse === undefined || this.device.bot?.mode === 'switch') {
|
|
363
|
+
this.platform.debug(`Bot ${this.accessory.displayName} On: ${this.OutletInUse}, Switch: ${this.device.bot?.mode}`);
|
|
364
|
+
} else {
|
|
344
365
|
this.service.updateCharacteristic(this.platform.Characteristic.OutletInUse, this.OutletInUse);
|
|
366
|
+
this.platform.device(`Bot ${this.accessory.displayName} updateCharacteristic On: ${this.OutletInUse},`
|
|
367
|
+
+ ` Switch: ${this.device.bot?.mode}`);
|
|
345
368
|
}
|
|
346
369
|
}
|
|
347
370
|
|
|
348
371
|
public apiError(e: any) {
|
|
349
372
|
this.service.updateCharacteristic(this.platform.Characteristic.On, e);
|
|
350
|
-
if (
|
|
373
|
+
if (this.device.bot?.mode !== 'switch') {
|
|
351
374
|
this.service.updateCharacteristic(this.platform.Characteristic.OutletInUse, e);
|
|
352
375
|
}
|
|
353
376
|
}
|
|
354
377
|
|
|
355
|
-
private statusCode(push:
|
|
378
|
+
private statusCode(push: { data: { statusCode: any; }; }) {
|
|
356
379
|
switch (push.data.statusCode) {
|
|
357
380
|
case 151:
|
|
358
381
|
this.platform.log.error('Command not supported by this device type.');
|
|
@@ -384,9 +407,9 @@ export class Bot {
|
|
|
384
407
|
* Handle requests to set the "On" characteristic
|
|
385
408
|
*/
|
|
386
409
|
private handleOnSet(value: CharacteristicValue) {
|
|
387
|
-
if (this.
|
|
410
|
+
if (this.device.ble) {
|
|
388
411
|
this.TargetState = value as boolean;
|
|
389
|
-
this.platform.
|
|
412
|
+
this.platform.device(`Bot BLE Device - ${this.TargetState}`);
|
|
390
413
|
if (this.TargetState === this.SwitchOn) {
|
|
391
414
|
this.platform.log.info('Target state of Bot has not changed: ' + (this.SwitchOn ? 'ON' : 'OFF'));
|
|
392
415
|
this.service?.getCharacteristic(this.platform.Characteristic.On).updateValue(this.SwitchOn);
|