Skip to content

Commit

Permalink
Send HA auto discover messages on connect (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoseperez committed Nov 20, 2017
1 parent 41804ae commit 793d50d
Show file tree
Hide file tree
Showing 8 changed files with 2,825 additions and 2,836 deletions.
1 change: 1 addition & 0 deletions code/espurna/config/general.h
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,7 @@ PROGMEM const char* const custom_reset_string[] = {
#define HOMEASSISTANT_SUPPORT 1 // Build with home assistant support
#endif

#define HOMEASSISTANT_ENABLED 0 // Integration not enabled by default
#define HOMEASSISTANT_PREFIX "homeassistant" // Default MQTT prefix

// -----------------------------------------------------------------------------
Expand Down
Binary file modified code/espurna/data/index.html.gz
Binary file not shown.
3 changes: 3 additions & 0 deletions code/espurna/espurna.ino
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,9 @@ void setup() {
#if DOMOTICZ_SUPPORT
domoticzSetup();
#endif
#if HOMEASSISTANT_SUPPORT
haSetup();
#endif

// Prepare configuration for version 2.0
hwUpwardsCompatibility();
Expand Down
14 changes: 14 additions & 0 deletions code/espurna/homeassitant.ino
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Copyright (C) 2017 by Xose Pérez <xose dot perez at gmail dot com>

#include <ArduinoJson.h>

bool _haEnabled = false;

void haSend(bool add) {

DEBUG_MSG_P(PSTR("[HA] Sending autodiscovery MQTT message\n"));
Expand Down Expand Up @@ -66,5 +68,17 @@ void haSend(bool add) {

}

void haConfigure() {
bool enabled = getSetting("haEnabled", HOMEASSISTANT_ENABLED).toInt() == 1;
if (enabled != _haEnabled) haSend(enabled);
_haEnabled = enabled;
}

void haSetup() {
haConfigure();
mqttRegister([](unsigned int type, const char * topic, const char * payload) {
if (type == MQTT_CONNECT_EVENT) haSend(_haEnabled);
});
}

#endif // HOMEASSISTANT_SUPPORT
5,558 changes: 2,775 additions & 2,783 deletions code/espurna/static/index.html.gz.h

Large diffs are not rendered by default.

57 changes: 21 additions & 36 deletions code/espurna/web.ino
Original file line number Diff line number Diff line change
Expand Up @@ -175,21 +175,6 @@ void _wsParse(AsyncWebSocketClient *client, uint8_t * payload, size_t length) {

}

#if HOMEASSISTANT_SUPPORT
if (action.equals("ha_add") && root.containsKey("data")) {
String value = root["data"];
setSetting("haPrefix", value);
haSend(true);
wsSend_P(client_id, PSTR("{\"message\": 6}"));
}
if (action.equals("ha_del") && root.containsKey("data")) {
String value = root["data"];
setSetting("haPrefix", value);
haSend(false);
wsSend_P(client_id, PSTR("{\"message\": 6}"));
}
#endif

#if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE

if (lightHasColor()) {
Expand Down Expand Up @@ -375,27 +360,27 @@ void _wsParse(AsyncWebSocketClient *client, uint8_t * payload, size_t length) {
// Clean wifi networks
int i = 0;
while (i < network) {
if (getSetting("ssid" + String(i)).length() == 0) {
delSetting("ssid" + String(i));
if (!hasSetting("ssid", i)) {
delSetting("ssid", i);
break;
}
if (getSetting("pass" + String(i)).length() == 0) delSetting("pass" + String(i));
if (getSetting("ip" + String(i)).length() == 0) delSetting("ip" + String(i));
if (getSetting("gw" + String(i)).length() == 0) delSetting("gw" + String(i));
if (getSetting("mask" + String(i)).length() == 0) delSetting("mask" + String(i));
if (getSetting("dns" + String(i)).length() == 0) delSetting("dns" + String(i));
if (!hasSetting("pass", i)) delSetting("pass", i);
if (!hasSetting("ip", i)) delSetting("ip", i);
if (!hasSetting("gw", i)) delSetting("gw", i);
if (!hasSetting("mask", i)) delSetting("mask", i);
if (!hasSetting("dns", i)) delSetting("dns", i);
++i;
}
while (i < WIFI_MAX_NETWORKS) {
if (getSetting("ssid" + String(i)).length() > 0) {
save = changed = true;
}
delSetting("ssid" + String(i));
delSetting("pass" + String(i));
delSetting("ip" + String(i));
delSetting("gw" + String(i));
delSetting("mask" + String(i));
delSetting("dns" + String(i));
if (hasSetting("ssid", i)) {
save = changed = true;
}
delSetting("ssid", i);
delSetting("pass", i);
delSetting("ip", i);
delSetting("gw", i);
delSetting("mask", i);
delSetting("dns", i);
++i;
}

Expand Down Expand Up @@ -430,16 +415,17 @@ void _wsParse(AsyncWebSocketClient *client, uint8_t * payload, size_t length) {
#if POWER_PROVIDER != POWER_PROVIDER_NONE
powerConfigure();
#endif

#if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
#if LIGHT_SAVE_ENABLED == 0
lightSave();
#endif
#endif

#if NTP_SUPPORT
if (changedNTP) ntpConfigure();
#endif
#if HOMEASSISTANT_SUPPORT
haConfigure();
#endif

}

Expand Down Expand Up @@ -478,11 +464,10 @@ void _wsStart(uint32_t client_id) {
root["app_name"] = APP_NAME;
root["app_version"] = APP_VERSION;
root["app_build"] = buildTime();

root["manufacturer"] = String(MANUFACTURER);
root["manufacturer"] = MANUFACTURER;
root["chipid"] = chipid;
root["mac"] = WiFi.macAddress();
root["device"] = String(DEVICE);
root["device"] = DEVICE;
root["hostname"] = getSetting("hostname");
root["network"] = getNetwork();
root["deviceip"] = getIP();
Expand Down
7 changes: 0 additions & 7 deletions code/html/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ function initMessages() {
messages[03] = "Error parsing data!";
messages[04] = "The file does not look like a valid configuration backup or is corrupted";
messages[05] = "Changes saved. You should reboot your board now";
messages[06] = "Home Assistant auto-discovery message sent";
messages[07] = "Passwords do not match!";
messages[08] = "Changes saved";
messages[09] = "No changes detected";
Expand Down Expand Up @@ -952,12 +951,6 @@ function init() {
$(".button-add-network").on('click', function() {
$("div.more", addNetwork()).toggle();
});
$(".button-ha-add").on('click', function() {
websock.send(JSON.stringify({'action': 'ha_add', 'data': $("input[name='haPrefix']").val()}));
});
$(".button-ha-del").on('click', function() {
websock.send(JSON.stringify({'action': 'ha_del', 'data': $("input[name='haPrefix']").val()}));
});

$(document).on('change', 'input', hasChanged);
$(document).on('change', 'select', hasChanged);
Expand Down
21 changes: 11 additions & 10 deletions code/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -416,24 +416,25 @@ <h2>General configuration values</h2>
</div>

<div class="pure-g module module-ha">
<label class="pure-u-1 pure-u-md-1-4" for="haPrefix">Home Assistant Prefix</label>
<input class="pure-u-1 pure-u-md-1-4" name="haPrefix" type="text" tabindex="13" />
<div class="pure-u-1-2 pure-u-md-1-8"><button type="button" class="pure-u-23-24 pure-button button-ha-add">Add</button></div>
<div class="pure-u-1-2 pure-u-md-1-8"><button type="button" class="pure-u-23-24 pure-button button-ha-del">Delete</button></div>
<div class="pure-u-0 pure-u-md-1-4">&nbsp;</div>
<div class="pure-u-1 pure-u-sm-1-4"><label for="haEnabled">Home Assistant</label></div>
<div class="pure-u-1 pure-u-sm-1-4"><input type="checkbox" name="haEnabled" tabindex="13" /></div>
<div class="pure-u-0 pure-u-md-1-2">&nbsp;</div>
<div class="pure-u-0 pure-u-md-1-4">&nbsp;</div>
<div class="pure-u-1 pure-u-md-3-4 hint">
Home Assistant auto-discovery feature.<br />
Add should immediately add the device to your HA console. Messages are retained so the device should be there even after a HA reboot<br />
To remove the device click on the Del button (retained message will be deleted) and reboot HA.<br />
Home Assistant auto-discovery feature. Enable and save to add the device to your HA console.
You might want to disable CSS style (above) so Home Assistant can parse the color.
</div>
</div>

<div class="pure-g module module-ha">
<label class="pure-u-1 pure-u-md-1-4" for="haPrefix">Home Assistant Prefix</label>
<input class="pure-u-1 pure-u-md-1-4" name="haPrefix" type="text" tabindex="14" />
</div>

<div class="pure-g module module-ds module-dht">
<label class="pure-u-1 pure-u-sm-1-4" for="tmpUnits">Temperature units</label>
<div class="pure-u-1 pure-u-sm-1-4"><input type="radio" name="tmpUnits" tabindex="14" value="0"> Celsius (&deg;C)</input></div>
<div class="pure-u-1 pure-u-sm-1-4"><input type="radio" name="tmpUnits" tabindex="15" value="1"> Fahrenheit (&deg;F)</input></div>
<div class="pure-u-1 pure-u-sm-1-4"><input type="radio" name="tmpUnits" tabindex="15" value="0"> Celsius (&deg;C)</input></div>
<div class="pure-u-1 pure-u-sm-1-4"><input type="radio" name="tmpUnits" tabindex="16" value="1"> Fahrenheit (&deg;F)</input></div>
</div>

</fieldset>
Expand Down

0 comments on commit 793d50d

Please sign in to comment.