Browse Source

Send HA auto discover messages on connect (#279)

fastled
Xose Pérez 7 years ago
parent
commit
793d50dd1e
8 changed files with 2825 additions and 2836 deletions
  1. +1
    -0
      code/espurna/config/general.h
  2. BIN
      code/espurna/data/index.html.gz
  3. +3
    -0
      code/espurna/espurna.ino
  4. +14
    -0
      code/espurna/homeassitant.ino
  5. +2775
    -2783
      code/espurna/static/index.html.gz.h
  6. +21
    -36
      code/espurna/web.ino
  7. +0
    -7
      code/html/custom.js
  8. +11
    -10
      code/html/index.html

+ 1
- 0
code/espurna/config/general.h View File

@ -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
// -----------------------------------------------------------------------------


BIN
code/espurna/data/index.html.gz View File


+ 3
- 0
code/espurna/espurna.ino View File

@ -305,6 +305,9 @@ void setup() {
#if DOMOTICZ_SUPPORT
domoticzSetup();
#endif
#if HOMEASSISTANT_SUPPORT
haSetup();
#endif
// Prepare configuration for version 2.0
hwUpwardsCompatibility();


+ 14
- 0
code/espurna/homeassitant.ino View File

@ -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"));
@ -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

+ 2775
- 2783
code/espurna/static/index.html.gz.h
File diff suppressed because it is too large
View File


+ 21
- 36
code/espurna/web.ino View File

@ -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()) {
@ -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;
}
@ -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
}
@ -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();


+ 0
- 7
code/html/custom.js View File

@ -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";
@ -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);


+ 11
- 10
code/html/index.html View File

@ -416,24 +416,25 @@
</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>


Loading…
Cancel
Save