Skip to content

Commit

Permalink
Power state node (#36)
Browse files Browse the repository at this point in the history
* Allow params callback

* Add power state node

* Add tests for power state node

* Register power state node
  • Loading branch information
ottoszika authored Dec 10, 2019
1 parent 6f49456 commit 2a91b6f
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 3 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"ewelink-credentials": "src/credentials/credentials.js",
"ewelink-devices": "src/devices/devices.js",
"ewelink-generic-device": "src/generic-device/generic-device.js",
"ewelink-firmware-version": "src/firmware-version/firmware-version.js"
"ewelink-firmware-version": "src/firmware-version/firmware-version.js",
"ewelink-power-state": "src/power-state/power-state.js"
}
},
"dependencies": {
Expand Down
43 changes: 43 additions & 0 deletions src/power-state/power-state.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<script type="text/javascript">
RED.nodes.registerType('ewelink-power-state', {
category: 'eWeLink',
color: '#a6bbcf',
defaults: {
name: { value: '' },
deviceId: { value: '' },
mode: { value: 'read' },
auth: { value: '', type: 'ewelink-credentials' }
},
inputs: 1,
outputs: 1,
label: function () {
return this.name || 'eWeLink Power State';
}
});
</script>

<script type="text/x-red" data-template-name="ewelink-power-state">
<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>
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Device ID</label>
<input type="text" id="node-input-deviceId" placeholder="Device ID">
</div>
<div class="form-row">
<label for="node-input-mode"><i class="icon-tag"></i> Mode</label>
<select id="node-input-mode" placeholder="Mode">
<option value="read">Read</option>
<option value="write">Write</option>
</select>
</div>
<div class="form-row">
<label for="node-input-auth"><i class="icon-tag"></i> eWeLink Credentials</label>
<input type="text" id="node-input-auth" placeholder="eWeLink Credentials">
</div>
</script>

<script type="text/x-red" data-help-name="ewelink-power-state">
<p>Retrieves or sets the power state of the device</p>
</script>
29 changes: 29 additions & 0 deletions src/power-state/power-state.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const eWeLinkConnect = require('../utils/ewelink-connect');

/**
* Power state node.
*/
module.exports = (RED) => {
/**
* Power state node constructor.
*
* @param {object} config The node configuration.
*/
function PowerStateNode(config) {
// Create the node
RED.nodes.createNode(this, config);

// Initialize node by mode
switch (config.mode) {
case 'read':
eWeLinkConnect.initializeDeviceNode(RED, this, config, 'getDevicePowerState');
break;
case 'write':
eWeLinkConnect.initializeDeviceNode(RED, this, config, 'setDevicePowerState', msg => [msg.payload.toLowerCase()]);
break;
}
}

// Register node
RED.nodes.registerType('ewelink-power-state', PowerStateNode);
}
4 changes: 2 additions & 2 deletions src/utils/ewelink-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ module.exports = {
// Once logged in we can listen to inputs
node.on('input', (msg) => {
method = method || msg.payload.method;
params = params || msg.payload.params || [];

params = (typeof params === 'function' ? params(msg) : params) || msg.payload.params || [];
// First parameter should be always the device ID
params.unshift(deviceId);

Expand Down
114 changes: 114 additions & 0 deletions test/power-state_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
const chai = require('chai');
const expect = chai.expect;
const sinon = require('sinon');
const helper = require('node-red-node-test-helper');
const credentialsNode = require('../src/credentials/credentials');
const powerStateNode = require('../src/power-state/power-state');
const eWeLinkConnect = require('../src/utils/ewelink-connect');

describe('Power State Node', () => {

beforeEach(done => {
helper.startServer(done);
});

afterEach(done => {
helper.unload().then(() => {
helper.stopServer(done);
});
});

it('should be loaded', done => {
const flow = [
{ id: 'n1', type: 'ewelink-credentials' },
{ id: 'n2', type: 'ewelink-power-state', auth: 'n1', deviceId: '12345', name: 'Device 123' }
];
helper.load([credentialsNode, powerStateNode], flow, () => {
const n2 = helper.getNode('n2');

n2.should.have.property('name', 'Device 123');

done();
});
});

it('should return the power state in "read" mode', done => {
const connection = { getDevicePowerState() { } };

const methodStub = sinon.stub(connection, 'getDevicePowerState')
.callsFake(() => Promise.resolve({ methodResult: 'great' }));

const loginStub = sinon.stub(eWeLinkConnect, 'login')
.callsFake(() => Promise.resolve(connection));

const flow = [
{ id: 'n1', type: 'ewelink-credentials' },
{ id: 'n2', type: 'ewelink-power-state', auth: 'n1', deviceId: '12345', mode: 'read', wires: [['n4']] },
{ id: 'n3', type: 'helper', wires: [['n2']] },
{ id: 'n4', type: 'helper' }
];
helper.load([credentialsNode, powerStateNode], flow, () => {
const n2 = helper.getNode('n2');
const n3 = helper.getNode('n3');
const n4 = helper.getNode('n4');

setTimeout(() => {
n2.on('input', () => {
sinon.assert.calledWith(methodStub, '12345');
sinon.assert.calledOnce(loginStub);

methodStub.restore();
loginStub.restore();

n4.on('input', msg => {
expect(msg).to.deep.include({ payload: { methodResult: 'great' } });

done();
});
});

n3.send({ payload: '' });
});
});
});

it('should set the power state in "write" mode', done => {
const connection = { setDevicePowerState() { } };

const methodStub = sinon.stub(connection, 'setDevicePowerState')
.callsFake(() => Promise.resolve({ methodResult: 'great' }));

const loginStub = sinon.stub(eWeLinkConnect, 'login')
.callsFake(() => Promise.resolve(connection));

const flow = [
{ id: 'n1', type: 'ewelink-credentials' },
{ id: 'n2', type: 'ewelink-power-state', auth: 'n1', deviceId: '12345', mode: 'write', wires: [['n4']] },
{ id: 'n3', type: 'helper', wires: [['n2']] },
{ id: 'n4', type: 'helper' }
];
helper.load([credentialsNode, powerStateNode], flow, () => {
const n2 = helper.getNode('n2');
const n3 = helper.getNode('n3');
const n4 = helper.getNode('n4');

setTimeout(() => {
n2.on('input', () => {
sinon.assert.calledWith(methodStub, '12345', 'action');
sinon.assert.calledOnce(loginStub);

methodStub.restore();
loginStub.restore();

n4.on('input', msg => {
expect(msg).to.deep.include({ payload: { methodResult: 'great' } });

done();
});
});

n3.send({ payload: 'action' });
});
});
});
});

0 comments on commit 2a91b6f

Please sign in to comment.