Skip to content

Commit

Permalink
device: bind extra argument to the output message
Browse files Browse the repository at this point in the history
reason: some driver can add useful information like timestamp or unit or
descriptionText
  • Loading branch information
fblackburn1 committed Apr 15, 2020
1 parent f7c091b commit 08fdaa1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion nodes/device.js
Expand Up @@ -101,7 +101,7 @@ module.exports = function HubitatDeviceModule(RED) {
node.log('Attributes refreshed');
}
if (node.sendEvent) {
const msg = { ...attribute };
const msg = { ...event, ...attribute };
node.send({ payload: msg, topic: node.name });
}
}
Expand Down
39 changes: 39 additions & 0 deletions test/nodes/device_spec.js
Expand Up @@ -72,6 +72,45 @@ describe('Hubitat Device Node', () => {
n1.hubitat.hubitatEvent.emit('device.42', hubitatEvent);
});
});
it('should send event with extra properties when event received', (done) => {
const flow = [
defaultConfigNode,
{ ...defaultDeviceNode, wires: [['n2']] },
{ id: 'n2', type: 'helper' },
];
const hubitatEvent = {
deviceId: '42',
name: 'testAttribute',
value: 'new-value',
dataType: 'overriden attribute',
extra: 'extra-arg',
};
helper.load([deviceNode, configNode], flow, () => {
const n1 = helper.getNode('n1');
const n2 = helper.getNode('n2');
n1.currentAttributes = {
testAttribute: {
name: 'testAttribute', value: 'old-value', deviceId: '42', dataType: 'STRING',
},
};
n2.on('input', (msg) => {
try {
msg.should.have.property('payload', {
deviceId: '42',
name: 'testAttribute',
value: 'new-value',
currentValue: 'new-value',
dataType: 'STRING',
extra: 'extra-arg',
});
done();
} catch (err) {
done(err);
}
});
n1.hubitat.hubitatEvent.emit('device.42', hubitatEvent);
});
});
it('should not send event when event received with wrong deviceId', (done) => {
const flow = [
defaultConfigNode,
Expand Down

0 comments on commit 08fdaa1

Please sign in to comment.