feat(yeelight): move to yeelight-wifi
Also add the configurator node, typescript & ejs. Close #29 #25 #24
This commit is contained in:
3
src/nodes/constants.ts
Normal file
3
src/nodes/constants.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export class Constants {
|
||||
static readonly NODES_PREFIX = "mi-devices";
|
||||
}
|
||||
33
src/nodes/yeelight/Searcher.ts
Normal file
33
src/nodes/yeelight/Searcher.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { Red } from "node-red";
|
||||
|
||||
import * as YeelightSearch from 'yeelight-wifi';
|
||||
import { Constants } from "../constants";
|
||||
|
||||
export class Searcher {
|
||||
static _bulbs:any[] = [];
|
||||
|
||||
static discover(RED:Red) {
|
||||
new Promise(() => {
|
||||
(new YeelightSearch()).on('found', (bulb:any) => {
|
||||
this._bulbs.push({
|
||||
name: bulb.name,
|
||||
model: bulb.model,
|
||||
sid: parseInt(bulb.id),
|
||||
ip: bulb.hostname
|
||||
});
|
||||
RED.nodes.eachNode((tmpNode) => {
|
||||
if(tmpNode.type.indexOf(`${Constants.NODES_PREFIX}-yeelight configurator`) === 0) {
|
||||
let tmpNodeInst = <any> RED.nodes.getNode(tmpNode.id);
|
||||
if(tmpNodeInst.ip === bulb.hostname || tmpNodeInst.sid === parseInt(bulb.id)) {
|
||||
tmpNodeInst.setBulb(bulb);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
static get bulbs() {
|
||||
return this._bulbs;
|
||||
}
|
||||
}
|
||||
73
src/nodes/yeelight/YeelightConfigurator.ejs
Normal file
73
src/nodes/yeelight/YeelightConfigurator.ejs
Normal file
@@ -0,0 +1,73 @@
|
||||
<script type="text/javascript">
|
||||
RED.nodes.registerType('<%= NODES_PREFIX %>-yeelight configurator', {
|
||||
category: 'config',
|
||||
defaults: {
|
||||
name: {value: ""},
|
||||
ip: {value: ""},
|
||||
sid: {value: ""}
|
||||
},
|
||||
label: function () {
|
||||
return this.name || "yeelight configurator";
|
||||
},
|
||||
oneditprepare: function() {
|
||||
RED.settings.miDevicesYeelightConfiguratorDiscoveredBulbs.forEach(function(bulb, index) {
|
||||
$('#discovered-bulbs').append('<option value="' + bulb.sid + '">' + (bulb.name || bulb.sid) + ' - ' + bulb.model + ' - ' + bulb.ip + '</option>');
|
||||
});
|
||||
var node = this;
|
||||
$('#discovered-bulbs').on('change', function() {
|
||||
var sid = $('#discovered-bulbs').val();
|
||||
var bulb = sid && RED.settings.miDevicesYeelightConfiguratorDiscoveredBulbs.filter(function(e) { return e.sid == sid })[0];
|
||||
$("#node-config-input-name").val(bulb && bulb.name);
|
||||
$("#node-config-input-sid").val(bulb && bulb.sid);
|
||||
$("#node-config-input-ip").val("");
|
||||
});
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-template-name="<%= NODES_PREFIX %>-yeelight configurator">
|
||||
<div class="form-row">
|
||||
<label for="discovered-bulbs"><i class="fa fa-search"></i> Found bulbs</label>
|
||||
<select id="discovered-bulbs">
|
||||
<option>- Select -</option>
|
||||
</select>
|
||||
</div>
|
||||
<hr>
|
||||
<div class="form-row">
|
||||
<label for="node-config-input-name"><i class="icon-tag"></i> Name</label>
|
||||
<input type="text" id="node-config-input-name" placeholder="Name">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-config-input-ip"><i class="fa fa-compass"></i> IP</label>
|
||||
<input type="text" id="node-config-input-ip" placeholder="IP">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-config-input-sid"><i class="fa fa-barcode"></i> SID</label>
|
||||
<input type="text" id="node-config-input-sid" placeholder="sid">
|
||||
</div>
|
||||
<p>Note: use <code>ip</code> or <code>sid</code> - <code>sid</code> is better.</p>
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-help-name="<%= NODES_PREFIX %>-yeelight configurator">
|
||||
<p>Xiaomi Yeelight configuration node.</p>
|
||||
<h3>Details</h3>
|
||||
<p>This configuration node is used by the Yeelight nodes. Here you can add
|
||||
devices with their device-id (SID), type and a description.</p>
|
||||
<p>At the moment the following devices are supported:
|
||||
<lu>
|
||||
<li>Humidity & Temperature sensor [sensor ht/]</li>
|
||||
<li>Body motion sensor [motion]</li>
|
||||
<li>Magnet contact sensor [contact]</li>
|
||||
<li>Wall socket plug (zigbee) [plug]</li>
|
||||
<li>Push button [switch]</li>
|
||||
</lu>
|
||||
</p>
|
||||
<p>To be able to receive messages from the Xiaomi gateway, you need to set the gateway
|
||||
in developer mode. Once in developer mode, the gateway sends JSON messages over the network as
|
||||
UDP packages. On the internet their are a lot of guides on how to put the gateway in developer mode.</p>
|
||||
<p>If you want to use the wall sockets, you need to set the key from the gateway. The key can be
|
||||
retrieved via the Xiaomi Home App when in developer mode. Enter the key here and it is used
|
||||
together with the token from the gateway's heartbeat message to recalculate the key to switch
|
||||
the plug. If you do not specify a key, the plug-node can not be used.</p>
|
||||
|
||||
</script>
|
||||
41
src/nodes/yeelight/YeelightConfigurator.ts
Normal file
41
src/nodes/yeelight/YeelightConfigurator.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { Red, NodeProperties, NodeStatus, ClearNodeStatus } from "node-red";
|
||||
import { Constants } from "../constants";
|
||||
import { Searcher } from "./Searcher";
|
||||
|
||||
export interface IYeelightConfiguratorNode {
|
||||
ip:string;
|
||||
sid:number;
|
||||
bulb:any;
|
||||
|
||||
on(event: "bulbFound", listener: () => void): any;
|
||||
}
|
||||
|
||||
export default (RED:Red) => {
|
||||
class YeelightConfigurator {
|
||||
ip:string;
|
||||
sid:number;
|
||||
_bulb:any;
|
||||
|
||||
constructor(props: NodeProperties) {
|
||||
RED.nodes.createNode(<any> this, props);
|
||||
let {ip, sid} = <any> props;
|
||||
this.sid = sid;
|
||||
this.ip = ip;
|
||||
}
|
||||
|
||||
set bulb(bulb) {
|
||||
this._bulb = bulb;
|
||||
(<any> this).emit('bulbFound');
|
||||
}
|
||||
|
||||
get bulb() {
|
||||
return this._bulb;
|
||||
}
|
||||
}
|
||||
|
||||
RED.nodes.registerType(`${Constants.NODES_PREFIX}-yeelight configurator`, <any> YeelightConfigurator, {
|
||||
settings: {
|
||||
miDevicesYeelightConfiguratorDiscoveredBulbs: { value: Searcher.bulbs, exportable: true }
|
||||
}
|
||||
});
|
||||
};
|
||||
92
src/nodes/yeelight/YeelightOut.ejs
Normal file
92
src/nodes/yeelight/YeelightOut.ejs
Normal file
@@ -0,0 +1,92 @@
|
||||
<script type="text/javascript">
|
||||
RED.nodes.registerType('<%= NODE_PREFIX %>-yeelight out', {
|
||||
category: 'xiaomi in out',
|
||||
color: '#087F8A',
|
||||
defaults: {
|
||||
name: {value: ""},
|
||||
yeelight: {value:"", type:"<%= NODE_PREFIX %>-yeelight configurator"}
|
||||
},
|
||||
inputs: 1,
|
||||
outputs: 0,
|
||||
paletteLabel: "yeelight out",
|
||||
icon: "mi-yeelight.png",
|
||||
align: "right",
|
||||
label: function () {
|
||||
return this.name || "yeelight out";
|
||||
},
|
||||
oneditprepare: function() {
|
||||
function changeGateway() {
|
||||
var configNodeID = $('#node-input-gateway').val();
|
||||
if (configNodeID) {
|
||||
var configNode = RED.nodes.node(configNodeID);
|
||||
if(configNode) {
|
||||
if(!this.name) {
|
||||
$("#node-input-name").val(configNode.name);
|
||||
}
|
||||
$('#node-input-ip').val(configNode.ip);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$("#node-input-gateway").change(function () {
|
||||
changeGateway();
|
||||
});
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-template-name="<%= NODE_PREFIX %>-yeelight out">
|
||||
<div class="form-row">
|
||||
<label for="node-input-yeelight"><i class="icon-tag"></i> Yeelight</label>
|
||||
<input type="text" id="node-input-yeelight" placeholder="yeelight">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
|
||||
<input type="text" id="node-input-name" placeholder="Name">
|
||||
</div>
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-help-name="<%= NODE_PREFIX %>-yeelight out">
|
||||
<p>The Xiaomi Yeelight node</p>
|
||||
|
||||
<h3>Inputs</h3>
|
||||
<dl class="message-properties">
|
||||
<dt>payload
|
||||
<span class="property-type">object</span>
|
||||
</dt>
|
||||
<dd>
|
||||
When the message contains a <code>sid</code> field, the node will filter the input and output only if the <code>sid</code> is the device's sid.<br>
|
||||
<hr>
|
||||
If the message doesn't contain a <code>sid</code> field, the node will be used to inject <code>sid</code> and <code>gateway</code> fields in the incoming <code>msg</code>.<br>
|
||||
<hr>
|
||||
Input Gateway node produces message of type <code>read_ack</code>, <code>heartbeat</code> or <code>report</code>.
|
||||
</dd>
|
||||
</dl>
|
||||
|
||||
<h3>Outputs</h3>
|
||||
<ol class="node-ports">
|
||||
<dl class="message-properties">
|
||||
<dt>payload <span class="property-type">object</span></dt>
|
||||
<dd>Data from gateway when used as a filter (see below).</dd>
|
||||
<dt>sid <span class="property-type">string</span></dt>
|
||||
<dd>Device SID.</dd>
|
||||
<dt>gateway <span class="property-type">object</span></dt>
|
||||
<dd>The <code>xiaomi-configurator</code> object where the device is registred.</dd>
|
||||
</dl>
|
||||
</ol>
|
||||
|
||||
<h4>Details</h4>
|
||||
<p>The incoming message is processed if the input <code>sid</code> matches the configured value for this device.</p>
|
||||
<p>Sample message:</p>
|
||||
<p><pre>{
|
||||
cmd: "report"
|
||||
model: "switch"
|
||||
sid: "158d000128b124"
|
||||
short_id: 56773
|
||||
data: {
|
||||
status: "click",
|
||||
batteryLevel: 23
|
||||
}
|
||||
}</pre></p>
|
||||
|
||||
</script>
|
||||
52
src/nodes/yeelight/YeelightOut.ts
Normal file
52
src/nodes/yeelight/YeelightOut.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { Red, Node, NodeProperties, NodeStatus, ClearNodeStatus } from "node-red";
|
||||
import { Constants } from "../constants";
|
||||
import { IYeelightConfiguratorNode } from "./YeelightConfigurator";
|
||||
|
||||
export interface IYeelightOutNode {
|
||||
yeelightNode:IYeelightConfiguratorNode;
|
||||
}
|
||||
|
||||
export default (RED:Red) => {
|
||||
class YeelightOut implements IYeelightOutNode {
|
||||
yeelightNode:IYeelightConfiguratorNode;
|
||||
|
||||
constructor(props: NodeProperties) {
|
||||
RED.nodes.createNode(<any> this, props);
|
||||
this.yeelightNode = <any> RED.nodes.getNode((<any> props).yeelight);
|
||||
|
||||
(<any> this).status({fill: "red", shape: "ring", text: "offline"});
|
||||
this.yeelightNode && this.yeelightNode.on('bulbFound', () => {
|
||||
(<any>this).status({fill:"blue", shape:"dot", text: "online"});
|
||||
});
|
||||
|
||||
this.setListener();
|
||||
}
|
||||
|
||||
protected setListener() {
|
||||
(<any> this).on('input', (msg) => {
|
||||
if (this.yeelightNode.bulb) {
|
||||
if(msg.payload === "on") {
|
||||
this.yeelightNode.bulb.turnOn();
|
||||
}
|
||||
else if(msg.payload === "off") {
|
||||
this.yeelightNode.bulb.turnOff();
|
||||
}
|
||||
else if(msg.payload === "toggle") {
|
||||
this.yeelightNode.bulb.toggle();
|
||||
}
|
||||
|
||||
if(msg.payload.color !== undefined) {
|
||||
// TODO: revoir la couleur
|
||||
this.yeelightNode.bulb.setRGB(msg.payload.color);
|
||||
}
|
||||
if(msg.payload.brightness !== undefined) {
|
||||
this.yeelightNode.bulb.setBrightness(msg.payload.brightness);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
RED.nodes.registerType(`${Constants.NODES_PREFIX}-yeelight out`, <any> YeelightOut);
|
||||
};
|
||||
2
src/nodes/yeelight/index.ejs
Normal file
2
src/nodes/yeelight/index.ejs
Normal file
@@ -0,0 +1,2 @@
|
||||
<%- include('./YeelightConfigurator', {}); %>
|
||||
<%- include('./YeelightOut', {}); %>
|
||||
13
src/nodes/yeelight/index.ts
Normal file
13
src/nodes/yeelight/index.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { Red, NodeProperties } from "node-red";
|
||||
import * as YeelightSearch from 'yeelight-wifi';
|
||||
|
||||
import { Searcher } from "./Searcher";
|
||||
import {default as YeelightConfigurator} from "./YeelightConfigurator";
|
||||
import {default as YeelightOut} from "./YeelightOut";
|
||||
|
||||
export = (RED:Red) => {
|
||||
Searcher.discover(RED);
|
||||
|
||||
YeelightConfigurator(RED);
|
||||
YeelightOut(RED);
|
||||
};
|
||||
Reference in New Issue
Block a user