/
driver.js
46 lines (38 loc) · 1.1 KB
/
driver.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"use strict";
const Homey = require("homey");
const SERVICE_UUID = "fe87";
const PRESENTATION_NAME = "Ambiance Lamp";
class CandelaBleDriver extends Homey.Driver {
onInit() {
this.log("Initializing candela BLE driver.");
}
async discoverLights() {
this.log("Discovering Candela BLE devices.");
try {
const discoveredDevices = await this.homey.ble.discover([SERVICE_UUID]);
const mappedDiscoveredDevices = discoveredDevices.map((bleDevice) => ({
name: `${PRESENTATION_NAME} ${bleDevice.address.substr(
bleDevice.address.length - 5
)}`,
data: { id: bleDevice.uuid },
}));
return Promise.resolve(mappedDiscoveredDevices);
} catch (error) {
return Promise.reject(error);
}
}
async onPair(session) {
session.setHandler("list_devices", async () => {
try {
const devices = await this.discoverLights();
if(devices.length) {
return devices;
}
return [];
} catch (error) {
this.log(error);
}
});
}
}
module.exports = CandelaBleDriver;