Skip to content

Commit

Permalink
feat: hass.io connection delay toggleable
Browse files Browse the repository at this point in the history
hass.io user now have the option to enable/disable the 5 second
connection delay
  • Loading branch information
zachowj committed Feb 26, 2019
1 parent ef07e7a commit 11c440c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
5 changes: 4 additions & 1 deletion lib/ha-websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,10 @@ class HaWebsocket extends EventEmitter {
return new Promise((resolve, reject) => {
// if hass.io, do a 5 second delay so it doesn't spam the hass.io proxy
// https://github.com/zachowj/node-red-contrib-home-assistant-websocket/issues/76
if (process.env.HASSIO_TOKEN) {
if (
process.env.HASSIO_TOKEN &&
self.config.connectionDelay !== false
) {
setTimeout(
() =>
connect(
Expand Down
33 changes: 24 additions & 9 deletions nodes/config-server/config-server.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
legacy: { value: false },
hassio: { value: false },
rejectUnauthorizedCerts: { value: true },
ha_boolean: { value: "y|yes|true|on|home|open" }
ha_boolean: { value: "y|yes|true|on|home|open" },
connectionDelay: { value: true }
},
credentials: {
host: { value: "", required: true },
Expand All @@ -17,9 +18,9 @@
return this.name || this.url;
},
oneditprepare: function() {
var $hassio = $("#node-config-input-hassio");
var $host = $("#node-config-input-host");
var $legacy = $("#node-config-input-legacy");
const $hassio = $("#node-config-input-hassio");
const $host = $("#node-config-input-host");
const $legacy = $("#node-config-input-legacy");

if (this.ha_boolean === undefined) {
$("#node-config-input-ha_boolean").val("y|yes|true|on|home|open");
Expand All @@ -29,6 +30,10 @@
$("#accept_unauthorized_certs").prop("checked", true);
}

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

if (
!$hassio.prop("checked") &&
$host.val() === "http://hassio/homeassistant"
Expand All @@ -37,14 +42,15 @@
}
function updateHassio() {
$("#server-info").toggle(!$hassio.prop("checked"));
$(".hassio").toggle($hassio.prop("checked"));
}
updateHassio();
$hassio.on("click", function() {
updateHassio();
});

function updateLegacy() {
var tokenName = $legacy.prop("checked") ? "Password" : "Access Token";
const tokenName = $legacy.prop("checked") ? "Password" : "Access Token";

$("#access-token-label").html(
'<i class="fa fa-user-secret"></i> ' + tokenName
Expand All @@ -60,16 +66,19 @@
});
},
oneditsave: function() {
var hassio = $("#node-config-input-hassio").is(":checked");
var $host = $("#node-config-input-host");
var hostname = $host.val();
const hassio = $("#node-config-input-hassio").is(":checked");
const $host = $("#node-config-input-host");
const hostname = $host.val();

if (hassio) {
$host.val("http://hassio/homeassistant");
this.legacy = false;
this.rejectUnauthorizedCerts = true;
// this.connectionDelay = $("#node-config-input-connectionDelay").is(
// ":checked"
// );
} else {
var parser = document.createElement("a");
const parser = document.createElement("a");
parser.href = hostname;

if (hostname !== parser.origin) {
Expand All @@ -80,6 +89,7 @@
this.rejectUnauthorizedCerts = !$("#accept_unauthorized_certs").prop(
"checked"
);
this.connectionDelay = false;
}
}
});
Expand All @@ -96,6 +106,11 @@
<label for="node-config-input-hassio" style="width: auto;"> I use Hass.io</label>
</div>

<div class="form-row hassio">
<input type="checkbox" id="node-config-input-connectionDelay" style="width: auto;margin-left: 125px;vertical-align: top" />
<label for="node-config-input-connectionDelay" style="width: auto;"> Delay connection attempts</label>
</div>

<div id="server-info">
<div class="form-row">
<label for="node-config-input-host" style="width: 120px"><i class="fa fa-link"></i> Base URL</label>
Expand Down
6 changes: 4 additions & 2 deletions nodes/config-server/config-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ module.exports = function(RED) {
legacy: {},
hassio: {},
rejectUnauthorizedCerts: {},
ha_boolean: {}
ha_boolean: {},
connectionDelay: {}
}
};

Expand Down Expand Up @@ -154,7 +155,8 @@ module.exports = function(RED) {
apiPass: this.credentials.access_token,
legacy: this.nodeConfig.legacy,
rejectUnauthorizedCerts: this.nodeConfig
.rejectUnauthorizedCerts
.rejectUnauthorizedCerts,
connectionDelay: this.nodeConfig.connectionDelay
});
this.api = this.homeAssistant.api;
this.websocket = this.homeAssistant.websocket;
Expand Down

0 comments on commit 11c440c

Please sign in to comment.