Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEW: SDK3 #5

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@

node_modules/

.homeybuild/
26 changes: 5 additions & 21 deletions README.md
@@ -1,27 +1,11 @@
This App brings support for the "Xiaomi Yeelight Candela BLE Lights".
Introducing support for the "Xiaomi Yeelight Candela BLE Lights," this application offers the following functionalities:

![](upload://fpe2l5YlFvJoKPWXrmL6uem0d8N.jpeg)

Link to: [Official Site](https://www.yeelight.com/en_US/product/gingko)
Visit the [Official Site](https://www.yeelight.com/en_US/product/gingko) to learn more about the product.

**Features:**

- Turn light on.
- Turn light off.
- Set brightness.

**Known shortcommings:**

- No state feedback

**Version Information:**

0.0.4:

- Initial version.

0.0.5

- Added brandcollor.
- Added Notification UUID, (not been able to use it yet.)
- Changed Readme.
- Power on the light.
- Power off the light.
- Adjust the brightness level.
3 changes: 0 additions & 3 deletions api.js

This file was deleted.

18 changes: 0 additions & 18 deletions app.js
@@ -1,26 +1,8 @@
"use strict";

const Homey = require("homey");

class CandelaApp extends Homey.App {
onInit() {
this.log("Cluster Lights App is running!");

Homey.on("unload", () => {
this.log("App unload.");
});

Homey.on("memwarn", () => {
this.log("App memwarn.");
});

process.on("unhandledRejection", error => {
this.log("unhandledRejection: ", error);
});

process.on("uncaughtException", error => {
this.log("uncaughtException: ", error);
});
}
}

Expand Down
18 changes: 8 additions & 10 deletions app.json
@@ -1,8 +1,8 @@
{
"id": "nl.nielsdeklerk.XiaomiYeelightCandela",
"version": "0.0.5",
"compatibility": ">2.4.0",
"sdk": 2,
"compatibility": ">=5.0.0",
"sdk": 3,
"brandColor": "#DF2D31",
"permissions": ["homey:wireless:ble"],
"name": {
Expand All @@ -28,6 +28,10 @@
{
"name": "Niels de Klerk",
"email": "mail@nielsdeklerk.nl"
},
{
"name": "Martijn Poppen",
"email": "yeelight@martijnpoppen.nl"
}
]
},
Expand All @@ -42,7 +46,6 @@
"url": "https://github.com/nklerk/nl.nielsdeklerk.XiaomiYeelightCandela"
},
"homeyCommunityTopicId": 16518,
"capabilities": {},
"drivers": [
{
"id": "candela",
Expand Down Expand Up @@ -80,12 +83,7 @@
"id": "add_devices",
"template": "add_devices"
}
],
"settings": []
]
}
],
"flow": {
"triggers": [],
"actions": []
}
]
}
45 changes: 26 additions & 19 deletions drivers/candela/device.js
@@ -1,13 +1,11 @@
"use strict";

const Homey = require("homey");

const COMMAND_ON = Buffer.from("4340010101f4", "hex");
const COMMAND_OFF = Buffer.from("4340020101f4", "hex");
const COMMAND_DIM_HEADER = "4342";
const COMMAND_DIM_FOOTER = "0101f4";

const SERVICE_UUID = "fe87";
const SERVICE_UUID = "0000fe8700001000800000805f9b34fb";
const SERVICE_NOTIFY_UUID = "8f65073d9f574aaaafea397d19d5bbeb";
const SERVICE_COMMAND_UUID = "aa7d3f342d4f41e0807f52fbf8cf7443";

Expand All @@ -20,15 +18,11 @@ class CandelaBle extends Homey.Device {

async connectBleService() {
try {
const BLEdevice = await Homey.ManagerBLE.find(this.getData().id);

//Workaround for BUGs in ManagerBLE, (Cashing Bug, Connection Bug)
if (BLEdevice.__peripheral) {
BLEdevice.__peripheral = null;
}
const BLEdevice = await this.homey.ble.find(this.getData().id);

this.log("Connecting to BLE device");
this.log("Connecting to BLE device", BLEdevice);
this.connectedBLEdevice = await BLEdevice.connect();

const BleService = await this.connectedBLEdevice.getService(SERVICE_UUID);

return Promise.resolve(BleService);
Expand Down Expand Up @@ -60,30 +54,43 @@ class CandelaBle extends Homey.Device {
registerAtHomey() {
this.registerCapabilityListener("onoff", async value => {
this.log(`Power is set to: ${value}`);
if (value) {
if (value && this.getCapabilityValue('dim') === 0) {
const brightnessHex = ("0" + Number(10).toString(16)).slice(-2);
const command = COMMAND_DIM_HEADER + brightnessHex + COMMAND_DIM_FOOTER;

const levelBuffer = Buffer.from(command, "hex");


await this.sendCommand(COMMAND_ON);
await this.sendCommand(levelBuffer);

return Promise.resolve(true);
} else if (value) {
await this.sendCommand(COMMAND_ON);
return Promise.resolve(true);
} else {
await this.sendCommand(COMMAND_OFF);
return Promise.resolve(true);
}
return Promise.resolve(true);
});

this.registerCapabilityListener("dim", async value => {
this.log(`Brightness is set to ${value}`);

const brightness = parseInt(100 * value);
let command = "";

if (brightness == 0) {
command = COMMAND_OFF;
await this.sendCommand(COMMAND_OFF);
return Promise.resolve(true);
} else {
const brightnessHex = ("0" + Number(brightness).toString(16)).slice(-2);
command = COMMAND_DIM_HEADER + brightnessHex + COMMAND_DIM_FOOTER;
}
let levelBuffer = Buffer.from(command, "hex");
const command = COMMAND_DIM_HEADER + brightnessHex + COMMAND_DIM_FOOTER;

await this.sendCommand(levelBuffer); //this.ds.dimLevel(value));
const levelBuffer = Buffer.from(command, "hex");

return Promise.resolve(true);
await this.sendCommand(levelBuffer); //this.ds.dimLevel(value));
return Promise.resolve(true);
}
});
}
}
Expand Down
37 changes: 21 additions & 16 deletions drivers/candela/driver.js
Expand Up @@ -12,29 +12,34 @@ class CandelaBleDriver extends Homey.Driver {
async discoverLights() {
this.log("Discovering Candela BLE devices.");
try {
const discoveredDevices = await Homey.ManagerBLE.discover([SERVICE_UUID]);
const mappedDiscoveredDevices = discoveredDevices.map(bleDevice => {
const device = {
name: `${PRESENTATION_NAME} ${bleDevice.address.substr(bleDevice.address.length - 5)}`,
data: { id: bleDevice.uuid }
};
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 device;
});
return Promise.resolve(mappedDiscoveredDevices);
} catch (error) {
return Promise.reject(error);
}
}

onPairListDevices(data, callback) {
this.discoverLights()
.then(deviceList => {
callback(null, deviceList);
})
.catch(error => {
callback(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);
}
});
}
}

Expand Down
3 changes: 0 additions & 3 deletions locales/en.json

This file was deleted.

4 changes: 4 additions & 0 deletions yarn.lock
@@ -0,0 +1,4 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1