Skip to content

Commit

Permalink
feat: Add ability to disable caching of autocomplete results
Browse files Browse the repository at this point in the history
Enabling or disabling this may require a restart of Node-RED for it to
take effect.
  • Loading branch information
zachowj committed Oct 19, 2019
1 parent eeac869 commit a90e041
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 19 deletions.
71 changes: 53 additions & 18 deletions nodes/config-server/config-server.html
Expand Up @@ -4,22 +4,44 @@
RED.nodes.registerType("server", {
category: "config",
defaults: {
name: { value: "Home Assistant", required: false },
legacy: { value: false },
hassio: { value: false },
rejectUnauthorizedCerts: { value: true },
ha_boolean: { value: "y|yes|true|on|home|open" },
connectionDelay: { value: true }
name: {
value: "Home Assistant",
required: false
},
legacy: {
value: false
},
hassio: {
value: false
},
rejectUnauthorizedCerts: {
value: true
},
ha_boolean: {
value: "y|yes|true|on|home|open"
},
connectionDelay: {
value: true
},
cacheJson: {
value: true
}
},
credentials: {
host: { value: "", required: true },
access_token: { value: "", required: false }
host: {
value: "",
required: true
},
access_token: {
value: "",
required: false
}
},
icon: "home.png",
label: function() {
label: function () {
return this.name || this.url;
},
oneditprepare: function() {
oneditprepare: function () {
const $hassio = $("#node-config-input-hassio");
const $host = $("#node-config-input-host");
const $legacy = $("#node-config-input-legacy");
Expand All @@ -28,26 +50,30 @@
$("#node-config-input-ha_boolean").val("y|yes|true|on|home|open");
}

// Backwards compatibility
if (this.rejectUnauthorizedCerts === false) {
$("#accept_unauthorized_certs").prop("checked", true);
}

if (this.connectionDelay === undefined) {
$("#node-config-input-connectionDelay").prop("checked", true);
}
if (this.cacheJson === undefined) {
$("#node-config-input-cacheJson").prop("checked", true);
}

if (
!$hassio.prop("checked") &&
$host.val() === "http://hassio/homeassistant"
) {
$hassio.prop("checked", true);
}

function updateHassio() {
$("#server-info").toggle(!$hassio.prop("checked"));
$(".hassio").toggle($hassio.prop("checked"));
}
updateHassio();
$hassio.on("click", function() {
$hassio.on("click", function () {
updateHassio();
});

Expand All @@ -63,11 +89,11 @@
);
}
updateLegacy();
$legacy.on("click", function() {
$legacy.on("click", function () {
updateLegacy();
});
},
oneditsave: function() {
oneditsave: function () {
const hassio = $("#node-config-input-hassio").is(":checked");
const $host = $("#node-config-input-host");
const hostname = $host.val();
Expand Down Expand Up @@ -136,13 +162,19 @@
</div>

<div class="form-row">
<label for="node-config-input-ha_boolean" style="width: 120px"><i class="fa fa-link"></i> State Boolean</label>
<input type="text" id="node-config-input-ha_boolean" placeholder="y|yes|true|on|home|open" />
<label for="node-config-input-ha_boolean" style="width: 120px"><i class="fa fa-link"></i> State Boolean</label>
<input type="text" id="node-config-input-ha_boolean" placeholder="y|yes|true|on|home|open" />
</div>

<div class="form-row">
<input type="checkbox" id="node-config-input-cacheJson" style="width: auto;margin-left: 125px;vertical-align: top" />
<label for="node-config-input-cacheJson" style="width: auto;"> Cache Autocomplete Results</label>
</div>

</script>

<script type="text/x-red" data-help-name="server">
<p>Home Assistant connection configuration</p>
<p>Home Assistant connection configuration</p>

<h3>Config</h3>
<dl class="message-properties">
Expand All @@ -166,6 +198,9 @@ <h3>Config</h3>

<dt>State Boolean <span class="property-type">string | delimited</span></dt>
<dd>A list of strings, not case sensitive, delimited by vertial pipe, | , that will return true for State Type Boolean.</dd>

<dt>Cache Autocomplete Results</dt>
<dd>Enables the caching of the JSON autocomplete requests. Enabling or disabling this may require a restart of Node-RED for it to take effect.</dd>
</dl>

<h3>Details</h3>
Expand Down Expand Up @@ -193,4 +228,4 @@ <h3>References</h3>
<ul>
<li><a href="https://home-assistant.io/developers/rest_api">HA REST API</a></li>
</ul>
</script>
</script>
19 changes: 18 additions & 1 deletion nodes/config-server/config-server.js
Expand Up @@ -14,6 +14,18 @@ module.exports = function(RED) {
});

const httpHandlers = {
disableCache: function(req, res, next) {
if (this.nodeConfig.cacheJson === false) {
res.setHeader('Surrogate-Control', 'no-store');
res.setHeader(
'Cache-Control',
'no-store, no-cache, must-revalidate, proxy-revalidate'
);
res.setHeader('Pragma', 'no-cache');
res.setHeader('Expires', '0');
}
next();
},
getEntities: function(req, res, next) {
if (!this.homeAssistant) {
return res.json([]);
Expand Down Expand Up @@ -98,7 +110,8 @@ module.exports = function(RED) {
hassio: {},
rejectUnauthorizedCerts: {},
ha_boolean: {},
connectionDelay: {}
connectionDelay: {},
cacheJson: {}
}
};

Expand All @@ -125,21 +138,25 @@ module.exports = function(RED) {
this.RED.httpAdmin.get(
`/homeassistant/${this.id}/entities`,
RED.auth.needsPermission('server.read'),
httpHandlers.disableCache.bind(this),
httpHandlers.getEntities.bind(this)
);
this.RED.httpAdmin.get(
`/homeassistant/${this.id}/states`,
RED.auth.needsPermission('server.read'),
httpHandlers.disableCache.bind(this),
httpHandlers.getStates.bind(this)
);
this.RED.httpAdmin.get(
`/homeassistant/${this.id}/services`,
RED.auth.needsPermission('server.read'),
httpHandlers.disableCache.bind(this),
httpHandlers.getServices.bind(this)
);
this.RED.httpAdmin.get(
`/homeassistant/${this.id}/properties`,
RED.auth.needsPermission('server.read'),
httpHandlers.disableCache.bind(this),
httpHandlers.getProperties.bind(this)
);

Expand Down

0 comments on commit a90e041

Please sign in to comment.