Skip to content

Commit

Permalink
feat: Add search button to find home assisant instances for server co…
Browse files Browse the repository at this point in the history
…nfig
  • Loading branch information
zachowj committed Jan 14, 2020
1 parent 7c9762e commit 8f25ef6
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 86 deletions.
6 changes: 5 additions & 1 deletion docs/guide/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ pencil icon next to the Server field.

![screenshot](./images/getting-started_01.png)

Fill in the Base Url and Access Token on the server config node then Add/Update the
Search for a local Home Assistant instance or manually fill in the Base Url.

![screenshot](./images/getting-started_03.png)

Enter an Access Token on the server config node then Add/Update the
Server Config and Events: all nodes and deploy. After deploying a green box with
a status message, i.e. connected, stated_changed, should appear below
the Events: all node. If you see an open box with the status of connecting or
Expand Down
Binary file added docs/guide/images/getting-started_03.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions lib/common/common.scss
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,27 @@ li span.property-type {
}
}
}

.button-box {
div.text-box {
display: inline-block;
position: relative;
width: 70%;
height: 20px;

div {
position: absolute;
left: 0px;
right: 40px;

input {
width: 100%;
}
}
a {
position: absolute;
right: 0px;
top: 0px;
}
}
}
29 changes: 19 additions & 10 deletions nodes/config-server/config-server.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
const BaseNode = require('../../lib/base-node');
const HomeAssistant = require('../../lib/home-assistant');
const bonjour = require('bonjour')();
const flatten = require('flat');
const uniq = require('lodash.uniq');

module.exports = function(RED) {
const HomeAssistant = require('../../lib/home-assistant');

// Handle static files
// RED.httpAdmin.get('/homeassistant/static/*', function(req, res, next) {
// res.sendFile(req.params[0], {
// root: require('path').join(__dirname, '../..', '/libs'),
// dotfiles: 'deny'
// });
// });

const httpHandlers = {
disableCache: function(req, res, next) {
if (this.nodeConfig.cacheJson === false) {
Expand Down Expand Up @@ -110,6 +102,23 @@ module.exports = function(RED) {
}
};

RED.httpAdmin.get('/homeassistant/discover', async function(req, res) {
const instances = [];
bonjour.find({ type: 'home-assistant' }, service => {
instances.push({
label: service.name
? `${service.name} (${service.txt.base_url})`
: service.txt.base_url,
value: service.txt.base_url
});
});

// Add a bit of delay for all services to be discovered
setTimeout(() => {
res.json(instances);
}, 3000);
});

const nodeOptions = {
debug: true,
config: {
Expand Down
60 changes: 34 additions & 26 deletions nodes/config-server/ui-config-server.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="form-row">
<label for="node-config-input-name" style="width: 120px"
><i class="fa fa-tag"></i> Name</label
>
<label for="node-config-input-name" style="width: 120px">
<i class="fa fa-tag"></i> Name
</label>
<input type="text" id="node-config-input-name" />
</div>

Expand All @@ -12,8 +12,8 @@
style="width: auto;margin-left: 125px;vertical-align: top"
/>
<label for="node-config-input-hassio" style="width: auto;">
I use the Hass.io Add-on</label
>
I use the Hass.io Add-on
</label>
</div>

<div class="form-row hassio">
Expand All @@ -23,29 +23,37 @@
style="width: auto;margin-left: 125px;vertical-align: top"
/>
<label for="node-config-input-connectionDelay" style="width: auto;">
Delay connection attempts</label
>
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
>
<input
type="text"
id="node-config-input-host"
placeholder="http://localhost:8123"
/>
<div class="form-row button-box">
<label for="node-config-input-host" style="width: 124px">
<i class="fa fa-link"></i> Base URL
</label>
<div class="text-box">
<div>
<input
type="text"
id="node-config-input-host"
placeholder="http://localhost:8123"
/>
</div>
<a id="discoverInstances" class="red-ui-button">
<i class="fa fa-search"></i>
</a>
</div>
</div>

<div class="form-row">
<label
id="access-token-label"
for="node-config-input-access_token"
style="width: 120px"
><i class="fa fa-user-secret"></i> Access Token</label
>
<i class="fa fa-user-secret"></i> Access Token
</label>
<input
type="text"
id="node-config-input-access_token"
Expand All @@ -60,8 +68,8 @@
style="width: auto;margin-left: 125px;vertical-align: top"
/>
<label for="node-config-input-legacy" style="width: auto;">
Use Legacy API Password</label
>
Use Legacy API Password
</label>
</div>

<div class="form-row">
Expand All @@ -71,15 +79,15 @@
style="width: auto;margin-left: 125px;vertical-align: top"
/>
<label for="accept_unauthorized_certs" style="width: auto;">
Accept Unauthorized SSL Certificates</label
>
Accept Unauthorized SSL Certificates
</label>
</div>
</div>

<div class="form-row">
<label for="node-config-input-ha_boolean" style="width: 120px"
><i class="fa fa-link"></i> State Boolean</label
>
<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"
Expand All @@ -94,6 +102,6 @@
style="width: auto;margin-left: 125px;vertical-align: top"
/>
<label for="node-config-input-cacheJson" style="width: auto;">
Cache Autocomplete Results</label
>
Cache Autocomplete Results
</label>
</div>
33 changes: 33 additions & 0 deletions nodes/config-server/ui-config-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,39 @@ RED.nodes.registerType('server', {
$legacy.on('click', function() {
updateLegacy();
});

try {
$host.autocomplete('destroy');
} catch (err) {}
const $discovery = $('#discoverInstances');
$discovery.on('click', function() {
if ($(this).hasClass('disabled')) return;
const $icon = $('i', this)
.removeClass('fa-search')
.addClass('spinner');
$discovery.addClass('disabled');
$host
.autocomplete({
source: (request, response) => {
$.ajax({
dataType: 'json',
url: '/homeassistant/discover',
cache: false
}).done(data => {
response(data);
});
},
minLength: 0,
response: () => {
$icon.addClass('fa-search').removeClass('spinner');
$discovery.removeClass('disabled');
},
close: (event, ui) => {
$host.autocomplete('destroy');
}
})
.autocomplete('search', '');
});
},
oneditsave: function() {
const hassio = $('#node-config-input-hassio').is(':checked');
Expand Down

0 comments on commit 8f25ef6

Please sign in to comment.