Skip to content

Commit

Permalink
Merge pull request #7 from biddster/issue/6
Browse files Browse the repository at this point in the history
Issue/6
  • Loading branch information
biddster authored Aug 28, 2020
2 parents 7111799 + bc34b76 commit 4eb25ac
Show file tree
Hide file tree
Showing 6 changed files with 6,732 additions and 112 deletions.
1 change: 1 addition & 0 deletions .node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10.22.0
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# node-red-contrib-hs100

This Node-RED node is for controlling tp-link Wi-Fi Smart Plug - Model HS100.
This Node-RED node is for controlling tp-link Wi-Fi Smart Plug - Model HS100 (and HS110).

This node has only been tested with a HS100(UK). The HS100 is also available in US and EU plug
versions. We expect they will work too.
Expand Down Expand Up @@ -49,5 +49,11 @@ to the node's input.

## Obtain power consumption data

The HS100 is a simple switch and does not support consumption. The HS110 includes the power consumption feature.

I have been told that the power consumption feature is working with the Australian version of the HS110.

I don't have an HS110 to test with though.

To obtain the power consumption, send a message to this node's input with the topic or payload
set to `consumption`. The consumption data will be sent via this node's output in `msg.payload`.
90 changes: 44 additions & 46 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
*/

module.exports = function hs100(RED) {
'use strict';

var Hs100Api = require('fx-hs100-api');
const Hs100Api = require('fx-hs100-api');

// TODO address the disparity of not having on and off in here. It bothers me.
hs100.supportedActuations = {
Expand All @@ -41,30 +39,63 @@ module.exports = function hs100(RED) {
time: 'getTime',
timezone: 'getTimeZone',
scaninfo: 'getScanInfo',
model: 'getModel'
model: 'getModel',
};

hs100.newHs100Client = function() {
hs100.newHs100Client = function () {
return new Hs100Api.Client();
};

RED.nodes.registerType('hs100', function(config) {
const getActuation = function (actuation) {
if (actuation) {
const method = hs100.supportedActuations[actuation.toLowerCase()];
if (method) {
return { name: actuation, method };
}
}

return null;
};

RED.nodes.registerType('hs100', function (config) {
RED.nodes.createNode(this, config);
var node = this;
// eslint-disable-next-line consistent-this
const node = this;
const client = hs100.newHs100Client();
const plug = client.getPlug({ host: config.host });

var client = hs100.newHs100Client();
var plug = client.getPlug({ host: config.host });
const errorHandler = function (err) {
node.error(err);
node.status({ fill: 'red', shape: 'dot', text: err.message });
};

const setPowerState = function (on) {
node.status({
fill: 'orange',
shape: on ? 'dot' : 'circle',
text: 'Turning ' + (on ? 'on' : 'off'),
});
plug.setPowerState(on)
.then(() => {
node.status({
fill: 'green',
shape: on ? 'dot' : 'circle',
text: on ? 'on' : 'off',
});
})
.catch(errorHandler);
};

node.on('input', function(msg) {
node.on('input', (msg) => {
if (msg.payload === 'on' || msg.topic === 'on') {
setPowerState(true);
} else if (msg.payload === 'off' || msg.topic === 'off') {
setPowerState(false);
} else {
var actuation = getActuation(msg.payload) || getActuation(msg.topic);
const actuation = getActuation(msg.payload) || getActuation(msg.topic);
if (actuation) {
plug[actuation.method]()
.then(function(data) {
.then((data) => {
node.send({ topic: actuation.name, payload: data });
})
.catch(errorHandler);
Expand All @@ -79,41 +110,8 @@ module.exports = function hs100(RED) {
}
});

node.on('close', function() {
node.on('close', () => {
client.socket.close();
});

function setPowerState(on) {
node.status({
fill: 'orange',
shape: on ? 'dot' : 'circle',
text: 'Turning ' + (on ? 'on' : 'off')
});
plug
.setPowerState(on)
.then(function() {
node.status({
fill: 'green',
shape: on ? 'dot' : 'circle',
text: on ? 'on' : 'off'
});
})
.catch(errorHandler);
}

function getActuation(actuation) {
if (actuation) {
var method = hs100.supportedActuations[actuation.toLowerCase()];
if (method) {
return { name: actuation, method: method };
}
}
return null;
}

function errorHandler(err) {
node.error(err);
node.status({ fill: 'red', shape: 'dot', text: err.message });
}
});
};
Loading

0 comments on commit 4eb25ac

Please sign in to comment.