Skip to content

Commit

Permalink
Merge 26eb3b7 into c3d5340
Browse files Browse the repository at this point in the history
  • Loading branch information
ottoszika authored Dec 11, 2019
2 parents c3d5340 + 26eb3b7 commit 876e040
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"ewelink-power-state": "src/power-state/power-state.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"
"ewelink-power-usage": "src/power-usage/power-usage.js",
"ewelink-humidity": "src/humidity/humidity.js"
}
},
"dependencies": {
Expand Down
35 changes: 35 additions & 0 deletions src/humidity/humidity.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<script type="text/javascript">
RED.nodes.registerType('ewelink-humidity', {
category: 'eWeLink',
color: '#a6bbcf',
defaults: {
name: { value: '' },
deviceId: { value: '' },
auth: { value: '', type: 'ewelink-credentials' }
},
inputs: 1,
outputs: 1,
label: function () {
return this.name || 'eWeLink Humidity';
}
});
</script>

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

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

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

// Register node
RED.nodes.registerType('ewelink-humidity', HumidityNode);
}
74 changes: 74 additions & 0 deletions test/humidity_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 humidityNode = require('../src/humidity/humidity');
const eWeLinkConnect = require('../src/utils/ewelink-connect');

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

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

done();
});
});

it('should return the result of the getDeviceCurrentHumidity call', done => {
const connection = { getDeviceCurrentHumidity() { } };

const methodStub = sinon.stub(connection, 'getDeviceCurrentHumidity')
.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-humidity', auth: 'n1', deviceId: '12345', wires: [['n4']] },
{ id: 'n3', type: 'helper', wires: [['n2']] },
{ id: 'n4', type: 'helper' }
];
helper.load([credentialsNode, humidityNode], 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: '' });
});
});
});
});

0 comments on commit 876e040

Please sign in to comment.