Added new Homeassistent broadcast logic

This commit is contained in:
cschwinne 2019-03-24 00:49:26 +01:00
parent cf77153647
commit 8a929a8348
2 changed files with 48 additions and 101 deletions

View File

@ -24,7 +24,7 @@
//#define WLED_DISABLE_INFRARED //there is no pin left for this on ESP8266-01 //#define WLED_DISABLE_INFRARED //there is no pin left for this on ESP8266-01
//#define WLED_DISABLE_MOBILE_UI //#define WLED_DISABLE_MOBILE_UI
//#define WLED_ENABLE_HOMEASSISTANT_AUTODISCOVERY #define WLED_ENABLE_HOMEASSISTANT_AUTODISCOVERY
#define WLED_DISABLE_FILESYSTEM //SPIFFS is not used by any WLED feature yet #define WLED_DISABLE_FILESYSTEM //SPIFFS is not used by any WLED feature yet
//#define WLED_ENABLE_FS_SERVING //Enable sending html file from SPIFFS before serving progmem version //#define WLED_ENABLE_FS_SERVING //Enable sending html file from SPIFFS before serving progmem version
@ -99,7 +99,7 @@
//version code in format yymmddb (b = daily build) //version code in format yymmddb (b = daily build)
#define VERSION 1903191 #define VERSION 1903233
char versionString[] = "0.8.4-dev"; char versionString[] = "0.8.4-dev";

View File

@ -100,6 +100,8 @@ void publishMqtt()
} }
#ifdef WLED_ENABLE_HOMEASSISTANT_AUTODISCOVERY #ifdef WLED_ENABLE_HOMEASSISTANT_AUTODISCOVERY
const char HA_static_JSON[] PROGMEM = R"=====(,"bri_val_tpl":"{{value}}","rgb_cmd_tpl":"{{'#%02x%02x%02x' | format(red, green, blue)}}","rgb_val_tpl":"{{value[1:3]|int(base=16)}},{{value[3:5]|int(base=16)}},{{value[5:7]|int(base=16)}}","qos":0,"opt":true,"pl_on":"ON","pl_off":"OFF","fx_val_tpl":"{{value}}","fx_list":[)=====";
void sendHADiscoveryMQTT(){ void sendHADiscoveryMQTT(){
/* /*
@ -211,7 +213,7 @@ Send out HA MQTT Discovery message on MQTT connect (~2.4kB):
} }
*/ */
char bufc[38], bufcol[38], bufg[38], bufapi[38]; char bufc[36], bufcol[38], bufg[36], bufapi[38], buffer[2500];
strcpy(bufc, mqttDeviceTopic); strcpy(bufc, mqttDeviceTopic);
strcpy(bufcol, mqttDeviceTopic); strcpy(bufcol, mqttDeviceTopic);
@ -223,8 +225,7 @@ Send out HA MQTT Discovery message on MQTT connect (~2.4kB):
strcat(bufg, "/g"); strcat(bufg, "/g");
strcat(bufapi, "/api"); strcat(bufapi, "/api");
StaticJsonBuffer<JSON_OBJECT_SIZE(8)> jsonBuffer;
DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(80) + JSON_OBJECT_SIZE(18) + 3000);
JsonObject& root = jsonBuffer.createObject(); JsonObject& root = jsonBuffer.createObject();
root["name"] = serverDescription; root["name"] = serverDescription;
root["stat_t"] = bufc; root["stat_t"] = bufc;
@ -233,109 +234,55 @@ Send out HA MQTT Discovery message on MQTT connect (~2.4kB):
root["rgb_cmd_t"] = bufcol; root["rgb_cmd_t"] = bufcol;
root["bri_cmd_t"] = mqttDeviceTopic; root["bri_cmd_t"] = mqttDeviceTopic;
root["bri_stat_t"] = bufg; root["bri_stat_t"] = bufg;
root["bri_val_tpl"] = "{{value}}";
root["rgb_cmd_tpl"] = "{{'#%02x%02x%02x' | format(red, green, blue)}}";
root["rgb_val_tpl"] = "{{value[1:3]|int(base=16)}},{{value[3:5]|int(base=16)}},{{value[5:7]|int(base=16)}}";
root["qos"] = 0;
root["opt"] = true;
root["pl_on"] = "ON";
root["pl_off"] = "OFF";
root["fx_cmd_t"] = bufapi; root["fx_cmd_t"] = bufapi;
root["fx_stat_t"] = bufapi; root["fx_stat_t"] = bufapi;
root["fx_val_tpl"] = "{{value}}";
JsonArray& fx_list = root.createNestedArray("fx_list"); size_t jlen = root.measureLength();
// There should be a better way to do this like a for loop but there is no WS2812FX::getModeCount() or WS2812FX::getModeName(), instead there is "JSON_mode_names" char pubt[21 + sizeof(serverDescription) + 8];
fx_list.add("[FX=00] STATIC"); DEBUG_PRINTLN(jlen);
fx_list.add("[FX=01] BLINK");
fx_list.add("[FX=02] BREATH");
fx_list.add("[FX=03] COLOR_WIPE");
fx_list.add("[FX=04] COLOR_WIPE_RANDOM");
fx_list.add("[FX=05] RANDOM_COLOR");
fx_list.add("[FX=06] COLOR_SWEEP");
fx_list.add("[FX=07] DYNAMIC");
fx_list.add("[FX=08] RAINBOW");
fx_list.add("[FX=09] RAINBOW_CYCLE");
fx_list.add("[FX=10] SCAN");
fx_list.add("[FX=11] DUAL_SCAN");
fx_list.add("[FX=12] FADE");
fx_list.add("[FX=13] THEATER_CHASE");
fx_list.add("[FX=14] THEATER_C_RAINBOW");
fx_list.add("[FX=15] RUNNING_LIGHTS");
fx_list.add("[FX=16] SAW");
fx_list.add("[FX=17] TWINKLE");
fx_list.add("[FX=18] DISSOLVE");
fx_list.add("[FX=19] DISSOLVE_RANDOM");
fx_list.add("[FX=20] SPARKLE");
fx_list.add("[FX=21] FLASH_SPARKLE");
fx_list.add("[FX=22] HYPER_SPARKLE");
fx_list.add("[FX=23] STROBE");
fx_list.add("[FX=24] STROBE_RAINBOW");
fx_list.add("[FX=25] MULTI_STROBE");
fx_list.add("[FX=26] BLINK_RAINBOW");
fx_list.add("[FX=27] ANDROID");
fx_list.add("[FX=28] CHASE_COLOR");
fx_list.add("[FX=29] CHASE_RANDOM");
fx_list.add("[FX=30] CHASE_RAINBOW");
fx_list.add("[FX=31] CHASE_FLASH");
fx_list.add("[FX=32] CHASE_FLASH_RANDOM");
fx_list.add("[FX=33] CHASE_RAINBOW_WHITE");
fx_list.add("[FX=34] COLORFUL");
fx_list.add("[FX=35] TRAFFIC_LIGHT");
fx_list.add("[FX=36] COLOR_SWEEP_RANDOM");
fx_list.add("[FX=37] RUNNING_COLOR");
fx_list.add("[FX=38] RUNNING_RED_BLUE");
fx_list.add("[FX=39] RUNNING_RANDOM");
fx_list.add("[FX=40] LARSON_SCANNER");
fx_list.add("[FX=41] COMET");
fx_list.add("[FX=42] FIREWORKS");
fx_list.add("[FX=43] RAIN");
fx_list.add("[FX=44] MERRY_CHRISTMAS");
fx_list.add("[FX=45] FIRE_FLICKER");
fx_list.add("[FX=46] GRADIENT");
fx_list.add("[FX=47] LOADING");
fx_list.add("[FX=48] DUAL_COLOR_WIPE_IN_OUT");
fx_list.add("[FX=49] DUAL_COLOR_WIPE_IN_IN");
fx_list.add("[FX=50] DUAL_COLOR_WIPE_OUT_OUT");
fx_list.add("[FX=51] DUAL_COLOR_WIPE_OUT_IN");
fx_list.add("[FX=52] CIRCUS_COMBUSTUS");
fx_list.add("[FX=53] HALLOWEEN");
fx_list.add("[FX=54] TRICOLOR_CHASE");
fx_list.add("[FX=55] TRICOLOR_WIPE");
fx_list.add("[FX=56] TRICOLOR_FADE");
fx_list.add("[FX=57] LIGHTNING");
fx_list.add("[FX=58] ICU");
fx_list.add("[FX=59] MULTI_COMET");
fx_list.add("[FX=60] DUAL_LARSON_SCANNER");
fx_list.add("[FX=61] RANDOM_CHASE");
fx_list.add("[FX=62] OSCILLATE");
fx_list.add("[FX=63] PRIDE_2015");
fx_list.add("[FX=64] JUGGLE");
fx_list.add("[FX=65] PALETTE");
fx_list.add("[FX=66] FIRE_2012");
fx_list.add("[FX=67] COLORWAVES");
fx_list.add("[FX=68] BPM");
fx_list.add("[FX=69] FILLNOISE8");
fx_list.add("[FX=70] NOISE16_1");
fx_list.add("[FX=71] NOISE16_2");
fx_list.add("[FX=72] NOISE16_3");
fx_list.add("[FX=73] NOISE16_4");
fx_list.add("[FX=74] COLORTWINKLE");
fx_list.add("[FX=75] LAKE");
fx_list.add("[FX=76] METEOR");
fx_list.add("[FX=77] METEOR_SMOOTH");
fx_list.add("[FX=78] RAILWAY");
fx_list.add("[FX=79] RIPPLE");
size_t jlen = root.measureLength() + 1;
char buffer[jlen], pubt[21 + sizeof(serverDescription) + 8];
root.printTo(buffer, jlen); root.printTo(buffer, jlen);
//add values which don't change
strcpy_P(buffer + jlen -1, HA_static_JSON);
olen = 0;
obuf = buffer + jlen -1 + strlen_P(HA_static_JSON);
//add fx_list
uint16_t jmnlen = strlen_P(JSON_mode_names);
uint16_t nameStart = 0, nameEnd = 0;
int i = 0;
bool isNameStart = true;
for (uint16_t j = 0; j < jmnlen; j++)
{
if (pgm_read_byte(JSON_mode_names + j) == '\"' || j == jmnlen -1)
{
if (isNameStart)
{
nameStart = j +1;
}
else
{
nameEnd = j;
char mdnfx[64], mdn[56];
uint16_t namelen = nameEnd - nameStart;
strncpy_P(mdn, JSON_mode_names + nameStart, namelen);
mdn[namelen] = 0;
snprintf(mdnfx, 64, "\"[FX=%02d] %s\",", i, mdn);
oappend(mdnfx);
DEBUG_PRINTLN(mdnfx);
i++;
}
isNameStart = !isNameStart;
}
}
DEBUG_PRINT("HA Discovery Sending >>"); DEBUG_PRINT("HA Discovery Sending >>");
DEBUG_PRINTLN(buffer); DEBUG_PRINTLN(buffer);
strcpy(pubt, "homeassistant/light/"); strcpy(pubt, "homeassistant/light/WLED_");
strcat(pubt, serverDescription); strcat(pubt, escapedMac.c_str());
strcat(pubt, "/config"); strcat(pubt, "/config");
mqtt->publish(pubt, 0, true, buffer); mqtt->publish(pubt, 0, true, buffer);
} }
@ -357,6 +304,6 @@ bool initMqtt()
mqtt->onMessage(onMqttMessage); mqtt->onMessage(onMqttMessage);
mqtt->onConnect(onMqttConnect); mqtt->onConnect(onMqttConnect);
mqtt->connect(); mqtt->connect();
DEBUG_PRINTLN("MQTT ready."); DEBUG_PRINTLN("MQTT ready");
return true; return true;
} }