2
0

feat(devices): handle yeelight basic support

Delete gateway in action config.
Close #4, #8 and #9
This commit is contained in:
Pierre CLEMENT
2018-01-11 00:41:54 +01:00
parent c85e131bc1
commit 809f9a6efb
18 changed files with 286 additions and 1163 deletions

View File

@@ -0,0 +1,57 @@
const miDevicesUtils = require('../src/utils');
const Yeelight = require("yeelight2");
module.exports = (RED) => {
function XiaomiYeelightOutputNode(config) {
RED.nodes.createNode(this, config);
this.ip = config.ip;
this.port = config.port;
this.status({fill:"grey", shape:"ring", text:"na"});
this.setupConnection = function(){
try {
this.light = Yeelight(`yeelight://${this.ip}:${this.port}`);
this.status({fill:"blue", shape:"dot", text:"connected"});
} catch(err) {
this.status({fill:"red",shape:"ring",text:err.message});
this.light = null;
this.error(err);
// try to reconnect in 5 minutes
window.setTimeout((function(self) {
return function() {
self.setupConnection.apply(self, arguments);
}
})(this), 1000*60*5);
}
}
if (this.ip && this.port) {
this.setupConnection();
this.on('input', (msg) => {
if(msg.payload === "on") {
this.light && this.light.set_power('on');
}
else if(msg.payload === "off") {
this.light && this.light.set_power('off');
}
else if(msg.payload === "toggle") {
this.light && this.light.toggle();
}
if(msg.payload.color !== undefined) {
this.light && this.light.set_rgb(msg.payload.color);
}
if(msg.payload.brightness !== undefined) {
this.light && this.light.set_bright(msg.payload.brightness);
}
});
this.on('close', () => {
this.light && this.light.exit();
});
}
}
RED.nodes.registerType("xiaomi-yeelight out", XiaomiYeelightOutputNode);
};