Skip to content

Commit

Permalink
Merge a6a1170 into 033112e
Browse files Browse the repository at this point in the history
  • Loading branch information
ottoszika authored Dec 11, 2019
2 parents 033112e + a6a1170 commit e1c1ebd
Show file tree
Hide file tree
Showing 9 changed files with 238 additions and 156 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"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-power-state": "src/power-state/power-state.js",
"ewelink-power-state-read": "src/power-state-read/power-state-read.js",
"ewelink-power-state-write": "src/power-state-write/power-state-write.js",
"ewelink-channel-count": "src/channel-count/channel-count.js",
"ewelink-event-listener": "src/event-listener/event-listener.js",
"ewelink-power-usage": "src/power-usage/power-usage.js",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
<script type="text/javascript">
RED.nodes.registerType('ewelink-power-state', {
RED.nodes.registerType('ewelink-power-state-read', {
category: 'eWeLink',
color: '#7bb6ef',
icon: 'font-awesome/fa-toggle-on',
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';
return this.name || 'eWeLink Power State Read';
},
paletteLabel: function () {
return 'power state';
}
});
</script>

<script type="text/x-red" data-template-name="ewelink-power-state">
<script type="text/x-red" data-template-name="ewelink-power-state-read">
<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">
Expand All @@ -29,19 +28,12 @@
<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>
<p>Reads the power state of the device</p>
</script>
22 changes: 22 additions & 0 deletions src/power-state-read/power-state-read.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const eWeLinkConnect = require('../utils/ewelink-connect');

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

// Initialize device node
eWeLinkConnect.initializeDeviceNode(RED, this, config, 'getDevicePowerState');
}

// Register node
RED.nodes.registerType('ewelink-power-state-read', PowerStateReadNode);
}
40 changes: 40 additions & 0 deletions src/power-state-write/power-state-write.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<script type="text/javascript">
RED.nodes.registerType('ewelink-power-state-write', {
category: 'eWeLink',
color: '#7bb6ef',
icon: 'font-awesome/fa-toggle-on',
defaults: {
name: { value: '' },
deviceId: { value: '' },
auth: { value: '', type: 'ewelink-credentials' }
},
inputs: 1,
outputs: 1,
label: function () {
return this.name || 'eWeLink Power State Write';
},
paletteLabel: function () {
return 'power state';
},
align: 'right'
});
</script>

<script type="text/x-red" data-template-name="ewelink-power-state-write">
<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-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-write">
<p>Sets the power state of the device</p>
</script>
22 changes: 22 additions & 0 deletions src/power-state-write/power-state-write.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const eWeLinkConnect = require('../utils/ewelink-connect');

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

// Initialize device node
eWeLinkConnect.initializeDeviceNode(RED, this, config, 'setDevicePowerState', msg => [msg.payload.toLowerCase()]);
}

// Register node
RED.nodes.registerType('ewelink-power-state-write', PowerStateWriteNode);
}
29 changes: 0 additions & 29 deletions src/power-state/power-state.js

This file was deleted.

74 changes: 74 additions & 0 deletions test/power-state-read_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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 powerStateReadNode = require('../src/power-state-read/power-state-read');
const eWeLinkConnect = require('../src/utils/ewelink-connect');

describe('Power State Read 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-read', auth: 'n1', deviceId: '12345', name: 'Device 123' }
];
helper.load([credentialsNode, powerStateReadNode], flow, () => {
const n2 = helper.getNode('n2');

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

done();
});
});

it('should return the result of the getDevicePowerState call', 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-read', auth: 'n1', deviceId: '12345', wires: [['n4']] },
{ id: 'n3', type: 'helper', wires: [['n2']] },
{ id: 'n4', type: 'helper' }
];
helper.load([credentialsNode, powerStateReadNode], 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: '' });
});
});
});
});
74 changes: 74 additions & 0 deletions test/power-state-write_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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 powerStateWriteNode = require('../src/power-state-write/power-state-write');
const eWeLinkConnect = require('../src/utils/ewelink-connect');

describe('Power State Write 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-write', auth: 'n1', deviceId: '12345', name: 'Device 123' }
];
helper.load([credentialsNode, powerStateWriteNode], flow, () => {
const n2 = helper.getNode('n2');

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

done();
});
});

it('should return the result of the setDevicePowerState call', 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-write', auth: 'n1', deviceId: '12345', wires: [['n4']] },
{ id: 'n3', type: 'helper', wires: [['n2']] },
{ id: 'n4', type: 'helper' }
];
helper.load([credentialsNode, powerStateWriteNode], 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: '' });
});
});
});
});
Loading

0 comments on commit e1c1ebd

Please sign in to comment.