Browse Source

Home Assistant auto-discovery feature

fastled
Xose Pérez 7 years ago
parent
commit
173a30c4a9
9 changed files with 3207 additions and 3093 deletions
  1. +11
    -1
      code/espurna/config/general.h
  2. BIN
      code/espurna/data/index.html.gz
  3. +60
    -0
      code/espurna/homeassitant.ino
  4. +12
    -0
      code/espurna/mqtt.ino
  5. +3098
    -3092
      code/espurna/static/index.html.gz.h
  6. +14
    -0
      code/espurna/web.ino
  7. +1
    -0
      code/html/custom.css
  8. +3
    -0
      code/html/custom.js
  9. +8
    -0
      code/html/index.html

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

@ -442,13 +442,23 @@ PROGMEM const char* const custom_reset_string[] = {
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
#ifndef DOMOTICZ_SUPPORT #ifndef DOMOTICZ_SUPPORT
#define DOMOTICZ_SUPPORT 1 // Build with domoticz support
#define DOMOTICZ_SUPPORT 1 // Build with domoticz support
#endif #endif
#define DOMOTICZ_ENABLED 0 // Disable domoticz by default #define DOMOTICZ_ENABLED 0 // Disable domoticz by default
#define DOMOTICZ_IN_TOPIC "domoticz/in" // Default subscription topic #define DOMOTICZ_IN_TOPIC "domoticz/in" // Default subscription topic
#define DOMOTICZ_OUT_TOPIC "domoticz/out" // Default publication topic #define DOMOTICZ_OUT_TOPIC "domoticz/out" // Default publication topic
// -----------------------------------------------------------------------------
// HOME ASSISTANT
// -----------------------------------------------------------------------------
#ifndef HOMEASSISTANT_SUPPORT
#define HOMEASSISTANT_SUPPORT 1 // Build with home assistant support
#endif
#define HOMEASSISTANT_PREFIX "homeassistant" // Default MQTT prefix
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// INFLUXDB // INFLUXDB
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------


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


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

@ -0,0 +1,60 @@
/*
HOME ASSISTANT MODULE
Copyright (C) 2017 by Xose Pérez <xose dot perez at gmail dot com>
*/
#if HOMEASSISTANT_SUPPORT
void haSend() {
DEBUG_MSG_P(PSTR("[HA] Sending autodiscovery MQTT message\n"));
String component = String("light");
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["name"] = getSetting("hostname");
root["platform"] = "mqtt";
if (relayCount()) {
root["state_topic"] = getTopic(MQTT_TOPIC_RELAY, 0, false);
root["command_topic"] = getTopic(MQTT_TOPIC_RELAY, 0, true);
}
#if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
if (lightHasColor()) {
root["brightness"] = 1;
root["brightness_state_topic"] = getTopic(MQTT_TOPIC_BRIGHTNESS, false);
root["brightness_command_topic"] = getTopic(MQTT_TOPIC_BRIGHTNESS, true);
root["rgb"] = 1;
root["rgb_state_topic"] = getTopic(MQTT_TOPIC_COLOR, false);
root["rgb_command_topic"] = getTopic(MQTT_TOPIC_COLOR, true);
root["color_temp"] = 1;
root["color_temp_command_topic"] = getTopic(MQTT_TOPIC_MIRED, true);
}
if (lightChannels() > 3) {
root["white_value"] = 1;
root["white_value_state_topic"] = getTopic(MQTT_TOPIC_CHANNEL, 3, false);
root["white_value_command_topic"] = getTopic(MQTT_TOPIC_CHANNEL, 3, true);
}
#endif // LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
String output;
root.printTo(output);
String topic = getSetting("haPrefix", HOMEASSISTANT_PREFIX)
+ String("/") + component + "/" + getSetting("hostname");
mqttSendRaw(topic.c_str(), output.c_str());
}
#endif // HOMEASSISTANT_SUPPORT

+ 12
- 0
code/espurna/mqtt.ino View File

@ -91,6 +91,18 @@ void mqttSendRaw(const char * topic, const char * message) {
} }
} }
String getTopic(const char * topic, bool set) {
String output = _mqttTopic + String(topic);
if (set) output += _mqttSetter;
return output;
}
String getTopic(const char * topic, unsigned int index, bool set) {
char buffer[strlen(topic)+5];
snprintf_P(buffer, sizeof(buffer), PSTR("%s/%d"), topic, index);
return getTopic(buffer, set);
}
void _mqttFlush() { void _mqttFlush() {
if (_mqtt_queue.size() == 0) return; if (_mqtt_queue.size() == 0) return;


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


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

@ -155,6 +155,15 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) {
} }
#if HOMEASSISTANT_SUPPORT
if (action.equals("ha_send") && root.containsKey("data")) {
String value = root["data"];
setSetting("haPrefix", value);
haSend();
wsSend_P(client_id, PSTR("{\"message\": \"Home Assistant auto-discovery message sent.\"}"));
}
#endif
#if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
if (lightHasColor()) { if (lightHasColor()) {
@ -482,6 +491,11 @@ void _wsStart(uint32_t client_id) {
root["tmpUnits"] = getSetting("tmpUnits", TMP_UNITS).toInt(); root["tmpUnits"] = getSetting("tmpUnits", TMP_UNITS).toInt();
#if HOMEASSISTANT_SUPPORT
root["haVisible"] = 1;
root["haPrefix"] = getSetting("haPrefix", HOMEASSISTANT_PREFIX);
#endif // HOMEASSISTANT_SUPPORT
#if DOMOTICZ_SUPPORT #if DOMOTICZ_SUPPORT
root["dczVisible"] = 1; root["dczVisible"] = 1;


+ 1
- 0
code/html/custom.css View File

@ -55,6 +55,7 @@
margin-left: 5px; margin-left: 5px;
} }
.button-upgrade-browse, .button-upgrade-browse,
.button-ha-send,
.button-apikey { .button-apikey {
background: rgb(0, 202, 0); background: rgb(0, 202, 0);
margin-left: 5px; margin-left: 5px;


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

@ -739,6 +739,9 @@ function init() {
$(".button-add-network").on('click', function() { $(".button-add-network").on('click', function() {
$("div.more", addNetwork()).toggle(); $("div.more", addNetwork()).toggle();
}); });
$(".button-ha-send").on('click', function() {
websock.send(JSON.stringify({'action': 'ha_send', 'data': $("input[name='haPrefix']").val()}));
});
var protocol = location.protocol; var protocol = location.protocol;
var host = window.location.hostname; var host = window.location.hostname;


+ 8
- 0
code/html/index.html View File

@ -405,6 +405,14 @@
<div class="pure-u-1 pure-u-md-3-4 hint">More gain means better range but also more power consumption. Might not work equally on all devices.</div> <div class="pure-u-1 pure-u-md-3-4 hint">More gain means better range but also more power consumption. Might not work equally on all devices.</div>
</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-2" name="haPrefix" type="text" tabindex="31" />
<div class="pure-u-1-3 pure-u-md-1-8"><button type="button" class="pure-u-23-24 pure-button button-ha-send">Send</button></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.</div>
</div>
<div class="pure-g module module-ds module-dht"> <div class="pure-g module module-ds module-dht">
<label class="pure-u-1 pure-u-sm-1-4" for="tmpUnits">Temperature units</label> <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="12" value="0"> Celsius (�C)</input></div> <div class="pure-u-1 pure-u-sm-1-4"><input type="radio" name="tmpUnits" tabindex="12" value="0"> Celsius (�C)</input></div>


Loading…
Cancel
Save