Skip to content

Commit

Permalink
Fix for it ignoring custom paths in node-red
Browse files Browse the repository at this point in the history
  • Loading branch information
zachowj committed Sep 21, 2018
1 parent 8a2d87c commit cf2a4ad
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lib/base-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class BaseNode {
}

getPrettyDate() {
return new Date().toLocaleDateString('en-US', {month: 'short', day: 'numeric', hour12: false, hour: 'numeric', minute: 'numeric'});
return new Date().toLocaleDateString('en-US', { month: 'short', day: 'numeric', hour12: false, hour: 'numeric', minute: 'numeric' });
}
}

Expand Down
2 changes: 1 addition & 1 deletion nodes/call-service/call-service.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
// Calls the custom api backend defined in the config-server.js node
// TODO: Not sure how this works with node-red if auth is enabled
function setServicesAndDomains(node) {
return $.get('/homeassistant/services')
return $.get(`${RED.settings.pollStateAdminPrefix}homeassistant/services`)
.then(services => {
node.allServices = JSON.parse(services);
node.allDomains = Object.keys(node.allServices);
Expand Down
2 changes: 1 addition & 1 deletion nodes/current-state/current-state.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
if (!isConfigValid) { $entityIdField.addClass('disabled'); }
else { $entityIdField.removeClass('disabled'); }

$.get('/homeassistant/entities')
$.get(`${RED.settings.apiCurrentState}homeassistant/entities`)
.done((entities) => {
this.availableEntities = JSON.parse(entities);

Expand Down
15 changes: 11 additions & 4 deletions nodes/current-state/current-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = function(RED) {
/* eslint-disable camelcase */
async onInput({ parsedMessage, message }) {
const entity_id = this.nodeConfig.entity_id ? this.nodeConfig.entity_id : parsedMessage.entity_id.value;
const logAndContinueEmpty = (logMsg) => { this.node.warn(logMsg); return ({ payload: {}}) };
const logAndContinueEmpty = (logMsg) => { this.node.warn(logMsg); return ({ payload: {} }) };

if (!entity_id) return logAndContinueEmpty('entity ID not set, cannot get current state, sending empty payload');

Expand All @@ -47,7 +47,7 @@ module.exports = function(RED) {
const debugMsg = `Get current state: halting processing due to current state of ${entity_id} matches "halt if state" option`;
this.debug(debugMsg);
this.debugToClient(debugMsg);
this.status({fill: 'red', shape: 'ring', text: `${currentState.state} at: ${this.getPrettyDate()}`});
this.status({ fill: 'red', shape: 'ring', text: `${currentState.state} at: ${this.getPrettyDate()}` });
return null;
}

Expand All @@ -60,10 +60,17 @@ module.exports = function(RED) {

message.data = currentState;

this.status({fill: 'green', shape: 'dot', text: `${currentState.state} at: ${this.getPrettyDate()}`});
this.status({ fill: 'green', shape: 'dot', text: `${currentState.state} at: ${this.getPrettyDate()}` });
this.node.send(message);
}
}

RED.nodes.registerType('api-current-state', CurrentStateNode);
RED.nodes.registerType('api-current-state', CurrentStateNode, {
settings: {
apiCurrentState: {
value: RED.settings.httpAdminRoot,
exportable: true
}
}
});
};
16 changes: 8 additions & 8 deletions nodes/events-state-changed/events-state-changed.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
category: 'home_assistant',
color: '#038FC7',
defaults: {
name: { value: '' },
server: { value: '', type: 'server', required: true },
entityidfilter: { value: '', required: true },
name: { value: '' },
server: { value: '', type: 'server', required: true },
entityidfilter: { value: '', required: true },
entityidfiltertype: { value: 'substring' },
haltifstate: { value: '' }
haltifstate: { value: '' }
},
inputs: 0,
outputs: 1,
icon: "arrow-right-bold-hexagon-outline.png",
paletteLabel: 'events: state',
label: function() { return this.name || `state_changed: ${this.entityidfilter || 'all entities'}` },
oneditprepare: function () {
const $entityidfilter = $('#node-input-entityidfilter');
const $entityidfilter = $('#node-input-entityidfilter');
const $entityidfiltertype = $('#node-input-entityidfiltertype');
const $server = $('#node-input-server');
const $server = $('#node-input-server');

$entityidfilter.val(this.entityidfilter);
this.entityidfiltertype = this.entityidfiltertype || 'substring';
Expand All @@ -43,7 +43,7 @@

// A home assistant server is selected in the node config
if (node.server || (selectedServer && selectedServer !== '_ADD_')) {
$.get('/homeassistant/entities').done((entities) => {
$.get(`${RED.settings.serverStateChangedAdminPrefix}homeassistant/entities`).done((entities) => {
node.availableEntities = JSON.parse(entities);
$entityidfilter.autocomplete({ source: node.availableEntities, minLength: 0 });
})
Expand All @@ -54,7 +54,7 @@
$server.change(() => setupAutocomplete(this));
},
oneditsave: function() {
this.entityidfilter = $("#node-input-entityidfilter").val();
this.entityidfilter = $("#node-input-entityidfilter").val();
}
});
</script>
Expand Down
13 changes: 10 additions & 3 deletions nodes/events-state-changed/events-state-changed.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module.exports = function(RED) {
if (shouldIncludeEvent) {
if (shouldHaltIfState) {
this.debug('flow halted due to "halt if state" setting');
this.status({fill: 'red', shape: 'ring', text: `${event.new_state.state} at: ${this.getPrettyDate()}`});
this.status({ fill: 'red', shape: 'ring', text: `${event.new_state.state} at: ${this.getPrettyDate()}` });

return null;
}
Expand All @@ -47,7 +47,7 @@ module.exports = function(RED) {
data: event
};

this.status({fill: 'green', shape: 'dot', text: `${event.new_state.state} at: ${this.getPrettyDate()}`});
this.status({ fill: 'green', shape: 'dot', text: `${event.new_state.state} at: ${this.getPrettyDate()}` });
(event.old_state)
? this.debug(`Incoming state event: entity_id: ${event.entity_id}, new_state: ${event.new_state.state}, old_state: ${event.old_state.state}`)
: this.debug(`Incoming state event: entity_id: ${event.entity_id}, new_state: ${event.new_state.state}`);
Expand Down Expand Up @@ -86,5 +86,12 @@ module.exports = function(RED) {
}
}

RED.nodes.registerType('server-state-changed', ServerStateChangedNode);
RED.nodes.registerType('server-state-changed', ServerStateChangedNode, {
settings: {
serverStateChangedAdminPrefix: {
value: RED.settings.httpAdminRoot,
exportable: true
}
}
});
};
2 changes: 1 addition & 1 deletion nodes/poll-state/poll-state.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
if (!isConfigValid) { $entityIdField.addClass('disabled'); }
else { $entityIdField.removeClass('disabled'); }

$.get('/homeassistant/entities')
$.get(`${RED.settings.pollStateAdminPrefix}homeassistant/entities`)
.done((entities) => {
this.availableEntities = JSON.parse(entities);

Expand Down
9 changes: 8 additions & 1 deletion nodes/poll-state/poll-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,12 @@ module.exports = function(RED) {
return state;
}
}
RED.nodes.registerType('poll-state', TimeSinceStateNode);
RED.nodes.registerType('poll-state', TimeSinceStateNode, {
settings: {
pollStateAdminPrefix: {
value: RED.settings.httpAdminRoot,
exportable: true
}
}
});
};
2 changes: 1 addition & 1 deletion nodes/trigger-state/trigger-state.html
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ <h3>Add Outputs</h3>

// A home assistant server is selected in the node config
if (NODE.server || (selectedServer && selectedServer !== '_ADD_')) {
$.get('/homeassistant/entities').done((entities) => {
$.get(`${RED.settings.triggerState}homeassistant/entities`).done((entities) => {
NODE.availableEntities = JSON.parse(entities);
$entityid.autocomplete({ source: NODE.availableEntities, minLength: 0 });
})
Expand Down
9 changes: 8 additions & 1 deletion nodes/trigger-state/trigger-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,5 +239,12 @@ module.exports = function(RED) {
}
}

RED.nodes.registerType('trigger-state', TriggerState);
RED.nodes.registerType('trigger-state', TriggerState, {
settings: {
triggerState: {
value: RED.settings.httpAdminRoot,
exportable: true
}
}
});
};

0 comments on commit cf2a4ad

Please sign in to comment.