Skip to content

Commit

Permalink
feat(entity): Rename sensor node to entity and add new entity type sw…
Browse files Browse the repository at this point in the history
…itch

- Add new entity of type swtich to the entity node which can be
triggered from the service nodered.trigger
  • Loading branch information
zachowj committed Feb 17, 2020
1 parent 4680f75 commit 059c340
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 49 deletions.
12 changes: 9 additions & 3 deletions lib/base-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,18 +186,18 @@ class BaseNode {
}

updateConnectionStatus(additionalText) {
this.setConnectionStatus(this.connectionState, additionalText);
this.setConnectionStatus(additionalText);
}

setConnectionStatus(state, additionalText) {
getConnectionStatus() {
let connectionStatus = {
shape: 'ring',
fill: 'red',
text: 'node-red:common.status.disconnected'
};

if (this.websocketClient) {
switch (state) {
switch (this.connectionState) {
case this.websocketClient.CONNECTING:
connectionStatus = {
shape: 'ring',
Expand All @@ -222,6 +222,12 @@ class BaseNode {
}
}

return connectionStatus;
}

setConnectionStatus(additionalText) {
let connectionStatus = this.getConnectionStatus();

if (
Object.prototype.hasOwnProperty.call(this, 'isEnabled') &&
this.isEnabled === false
Expand Down
4 changes: 3 additions & 1 deletion lib/events-ha-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ class EventsHaNode extends EventsNode {
}

const haConfig = {};
this.nodeConfig.haConfig
// Handle both event node and sensor node switch HA config
const config = this.nodeConfig.haConfig || this.nodeConfig.config;
config
.filter(c => c.value.length)
.forEach(e => (haConfig[e.property] = e.value));

Expand Down
96 changes: 77 additions & 19 deletions nodes/entity/entity.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const slugify = require('slugify');
const EventsHaNode = require('../../lib/events-ha-node');

module.exports = function(RED) {
const EventsNode = require('../../lib/events-node');

const nodeOptions = {
config: {
name: {},
Expand Down Expand Up @@ -39,7 +38,7 @@ module.exports = function(RED) {
}
};

class EntityNode extends EventsNode {
class EntityNode extends EventsHaNode {
constructor(nodeDefinition) {
super(nodeDefinition, RED, nodeOptions);
this.registered = false;
Expand All @@ -60,10 +59,34 @@ module.exports = function(RED) {
}
}

// Disable connection status
setConnectionStatus() {}
setConnectionStatus(additionalText) {
if (this.nodeConfig.entityType === 'switch') {
this.node.status({
shape: this.isEnabled ? 'dot' : 'ring',
fill: 'blue',
text: `${
this.isEnabled ? 'on' : 'off'
} at: ${this.getPrettyDate()}`
});
}
}

setStatus(
opts = {
shape: 'dot',
fill: 'blue',
text: ''
}
) {
this.node.status(opts);
}

async registerEntity() {
if (this.nodeConfig.entityType === 'switch') {
super.registerEntity();
return;
}

if (this.websocketClient.integrationVersion === 0) {
this.error(this.integrationErrorMessage);
this.setStatusFailed('Error');
Expand All @@ -76,12 +99,8 @@ module.exports = function(RED) {

const config = {};
this.nodeConfig.config
.filter(c => {
return c.value.length;
})
.forEach(e => {
config[e.property] = e.value;
});
.filter(c => c.value.length)
.forEach(e => (config[e.property] = e.value));

const payload = {
type: 'nodered/discovery',
Expand Down Expand Up @@ -141,7 +160,23 @@ module.exports = function(RED) {
}
}

handleSwitchInput(message) {
if (this.isEnabled) {
this.setStatusSuccess('input');
this.send([message, null]);
} else {
this.setStatusFailed('input');
this.send([null, message]);
}
}

async onInput({ parsedMessage, message }) {
// Handle entity node type switch
if (this.nodeConfig.entityType === 'switch') {
this.handleSwitchInput(message);
return;
}

if (!this.isConnected) {
this.setStatusFailed('No Connection');
this.error(
Expand Down Expand Up @@ -274,6 +309,21 @@ module.exports = function(RED) {
});
}

handleTriggerMessage(data = {}) {
const msg = {
topic: 'triggered',
payload: data.payload
};

if (this.isEnabled) {
this.setStatusSuccess('triggered');
this.send([msg, null]);
} else {
this.setStatusFailed('triggered');
this.send([null, msg]);
}
}

getValue(value, valueType, msg) {
let val;
switch (valueType) {
Expand Down Expand Up @@ -312,16 +362,24 @@ module.exports = function(RED) {
}

async loadPersistedData() {
let data;
try {
const data = await this.getNodeData();
if (
data &&
Object.prototype.hasOwnProperty.call(data, 'lastPayload')
) {
this.lastPayload = data.lastPayload;
}
data = await this.getNodeData();
} catch (e) {
this.error(e.message);
this.error(e.message, {});
}

if (!data) return;

if (
this.nodeConfig.entityType === 'switch' &&
Object.prototype.hasOwnProperty.call(data, 'isEnabled')
) {
this.isEnabled = data.isEnabled;
this.updateConnectionStatus();
}
if (Object.prototype.hasOwnProperty.call(data, 'lastPayload')) {
this.lastPayload = data.lastPayload;
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions nodes/entity/ui-entity.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<div class="form-row">
<label for="node-input-name">Name</label>
<input type="text" id="node-input-name" placeholder="Name" />
<input type="hidden" id="node-input-outputs" />
</div>

<div class="form-row">
Expand All @@ -14,32 +13,33 @@
<div class="form-row">
<label for="node-input-entityType">Type</label>
<select id="node-input-entityType" style="width: 70%;">
<option value="sensor">Sensor</option>
<option value="binary_sensor">Binary Sensor</option>
<option value="sensor">Sensor</option>
<option value="switch">Switch</option>
</select>
</div>

<div class="form-row">
<div id="state-row" class="form-row">
<label for="node-input-state">State</label>
<input type="text" id="node-input-state" style="width: 68%;" />
<input type="hidden" id="node-input-stateType" />
</div>

<div class="form-row">
<div id="attributes-row" class="form-row">
<ol id="attributes"></ol>
</div>

<div class="form-row">
<div id="config-row" class="form-row">
<ol id="config"></ol>
</div>

<div class="form-row">
<div id="output-location-row" class="form-row">
<label for="node-input-outputLocation">Output Location</label>
<input type="hidden" id="node-input-outputLocationType" />
<input type="text" id="node-input-outputLocation" style="width: 68%;" />
</div>

<div class="form-row">
<div id="input-override-row" class="form-row">
<label for="node-input-inputOverride">Input Override</label>
<select id="node-input-inputOverride" style="width: 68%;">
<option value="allow">Allow</option>
Expand All @@ -48,12 +48,12 @@
</select>
</div>

<div class="form-row checkbox-option">
<div id="resend-row" class="form-row checkbox-option">
<input type="checkbox" id="node-input-resend" />
<label for="node-input-resend">Resend state and attributes</label>
</div>

<div class="form-row checkbox-option">
<div id="debug-row" class="form-row checkbox-option">
<input type="checkbox" id="node-input-debugenabled" />
<label for="node-input-debugenabled">Show Debug Information</label>
</div>
70 changes: 54 additions & 16 deletions nodes/entity/ui-entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ RED.nodes.registerType('ha-entity', {
outputs: 1,
icon: 'font-awesome/fa-genderless',
align: 'right',
paletteLabel: 'sensor',
paletteLabel: 'entity',
label: function() {
return this.name || `type: ${this.entityType}`;
},
Expand Down Expand Up @@ -37,25 +37,36 @@ RED.nodes.registerType('ha-entity', {
nodeVersion.check(this);
const node = this;
const stateTypes = {
sensor: [
binary_sensor: [
'msg',
'flow',
'global',
'jsonata',
'str',
'num',
'bool',
'date'
'bool'
],
binary_sensor: [
sensor: [
'msg',
'flow',
'global',
'jsonata',
'str',
'num',
'bool'
]
'bool',
'date'
],
switch: ['msg']
};
const haConfigOptions = {
binary_sensor: [
'name',
'device_class',
'icon',
'unit_of_measurement'
],
sensor: ['name', 'device_class', 'icon', 'unit_of_measurement'],
switch: ['name', 'icon']
};
const attributeTypes = [
'str',
Expand All @@ -67,6 +78,14 @@ RED.nodes.registerType('ha-entity', {
'flow',
'global'
];
const switchRows = [
'state',
'attributes',
'output-location',
'input-override',
'resend',
'debug'
];

haServer.init(node, '#node-input-server');

Expand All @@ -78,15 +97,6 @@ RED.nodes.registerType('ha-entity', {
type: node.stateType
});

$('#node-input-entityType')
.on('change', function() {
$('#node-input-state').typedInput(
'types',
stateTypes[$(this).val()]
);
})
.trigger('change');

$('#attributes')
.editableList({
removable: true,
Expand Down Expand Up @@ -145,6 +155,34 @@ RED.nodes.registerType('ha-entity', {
})
.editableList('addItems', node.config);

$('#node-input-entityType')
.on('change', function() {
const value = $(this).val();

$('#node-input-state').typedInput('types', stateTypes[value]);

switch (value) {
case 'binary_sensor':
case 'sensor':
node.outputs = 1;
// Show all form-rows
$('.form-row').show();
// Show all config items
$('#config').editableList('filter', () => true);
break;
case 'switch':
node.outputs = 2;
// create a comma delimited list of ids
$(`#${switchRows.join('-row,#')}-row`).hide();
// filter config options
$('#config').editableList('filter', data =>
haConfigOptions[value].includes(data.property)
);
break;
}
})
.trigger('change');

$('#node-input-outputLocation').typedInput({
types: [
'msg',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
"poll-state": "nodes/poll-state/poll-state.js",
"ha-webhook": "nodes/webhook/webhook.js",
"api-call-service": "nodes/call-service/call-service.js",
"ha-fire-event": "nodes/fire-event/fire-event.js",
"ha-entity": "nodes/entity/entity.js",
"ha-fire-event": "nodes/fire-event/fire-event.js",
"api-current-state": "nodes/current-state/current-state.js",
"ha-get-entities": "nodes/get-entities/get-entities.js",
"api-get-history": "nodes/get-history/get-history.js",
Expand Down

0 comments on commit 059c340

Please sign in to comment.