-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add power usage node * Add power usage node tests * Register power usage node
- Loading branch information
Showing
4 changed files
with
133 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<script type="text/javascript"> | ||
RED.nodes.registerType('ewelink-power-usage', { | ||
category: 'eWeLink', | ||
color: '#a6bbcf', | ||
defaults: { | ||
name: { value: '' }, | ||
deviceId: { value: '' }, | ||
auth: { value: '', type: 'ewelink-credentials' } | ||
}, | ||
inputs: 1, | ||
outputs: 1, | ||
label: function () { | ||
return this.name || 'eWeLink Power Usage'; | ||
} | ||
}); | ||
</script> | ||
|
||
<script type="text/x-red" data-template-name="ewelink-power-usage"> | ||
<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-usage"> | ||
<p>Retrieves or sets the power state of the device</p> | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const eWeLinkConnect = require('../utils/ewelink-connect'); | ||
|
||
/** | ||
* Power usage node. | ||
*/ | ||
module.exports = (RED) => { | ||
/** | ||
* Power usage node constructor. | ||
* | ||
* @param {object} config The node configuration. | ||
*/ | ||
function PowerUsageNode(config) { | ||
// Create the node | ||
RED.nodes.createNode(this, config); | ||
|
||
// Initialize device node | ||
eWeLinkConnect.initializeDeviceNode(RED, this, config, 'getDevicePowerUsage'); | ||
} | ||
|
||
// Register node | ||
RED.nodes.registerType('ewelink-power-usage', PowerUsageNode); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 powerUsageNode = require('../src/power-usage/power-usage'); | ||
const eWeLinkConnect = require('../src/utils/ewelink-connect'); | ||
|
||
describe('Power Usage 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-usage', auth: 'n1', deviceId: '12345', name: 'Device 123' } | ||
]; | ||
helper.load([credentialsNode, powerUsageNode], flow, () => { | ||
const n2 = helper.getNode('n2'); | ||
|
||
n2.should.have.property('name', 'Device 123'); | ||
|
||
done(); | ||
}); | ||
}); | ||
|
||
it('should return the result of the call', done => { | ||
const connection = { getDevicePowerUsage() { } }; | ||
|
||
const methodStub = sinon.stub(connection, 'getDevicePowerUsage') | ||
.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-usage', auth: 'n1', deviceId: '12345', wires: [['n4']] }, | ||
{ id: 'n3', type: 'helper', wires: [['n2']] }, | ||
{ id: 'n4', type: 'helper' } | ||
]; | ||
helper.load([credentialsNode, powerUsageNode], 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: '' }); | ||
}); | ||
}); | ||
}); | ||
}); |