Skip to content

Commit

Permalink
fix some ha discovery issues
Browse files Browse the repository at this point in the history
add auto discovery mode
  • Loading branch information
softwarecrash committed Nov 26, 2023
1 parent 51e2f1e commit 271d664
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 9 deletions.
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ monitor_filters = esp8266_exception_decoder, default, time, printable, colorize
board_build.ldscript = eagle.flash.4m.ld ; 4MB (FS:4MB OTA:~3600KB)
upload_speed = 921600

custom_prog_version = 1.1.1
custom_prog_version = 1.1.1HA-2

build_flags =
-DVERSION=${this.custom_prog_version}
Expand Down
6 changes: 6 additions & 0 deletions src/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Settings
bool webUIdarkmode; // flag for dark mode
char httpUser[40]; // http basic auth username
char httpPass[40]; // http basic auth password
bool haDiscovery; // HomeAssistant Discovery switch
} data;

void load()
Expand Down Expand Up @@ -108,6 +109,10 @@ class Settings
{
strcpy(data.httpPass, "");
}
if (data.haDiscovery && !data.haDiscovery)
{
data.haDiscovery = false;
}
}
void coVersCheck()
{
Expand All @@ -126,6 +131,7 @@ class Settings
data.webUIdarkmode = false;
strcpy(data.httpUser, "");
strcpy(data.httpPass, "");
data.haDiscovery = false;
save();
load();
}
Expand Down
2 changes: 1 addition & 1 deletion src/VeDirectDataList.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ static const char * haDescriptor[][4] PROGMEM {
{"Voltage_3", "car-battery", "V", "voltage"},
{"Starter_voltage", "flash-triangle-outline", "V", "voltage"},
{"Mid_voltage", "battery-outline", "V", "voltage"},
{"Mid_deviation", "", "%", ""},
{"Mid_deviation", "eye", "%", "percent"},
{"Panel_voltage", "solar-panel", "V", "voltage"},
{"Panel_power","solar-power","W","power"},
{"Battery_current", "current-dc", "A", "current"},
Expand Down
2 changes: 2 additions & 0 deletions src/htmlProzessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@ String htmlProcessor(const String &var)
return (_settings.data.httpUser);
if (var == F("pre_http_pass"))
return (_settings.data.httpPass);
if (var == F("pre_hadiscovery"))
return (_settings.data.haDiscovery ? "checked" : "");
return String();
}
58 changes: 51 additions & 7 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ bool restartNow = false;
bool workerCanRun = true;
bool dataProzessing = false;
bool haDiscTrigger = false;
bool haAutoDiscTrigger = false;
unsigned int jsonSize = 0;
unsigned long mqtttimer = 0;
unsigned long RestartTimer = 0;
byte wsReqInvNum = 1;
Expand Down Expand Up @@ -188,6 +190,7 @@ void setup()
digitalWrite(MYPORT_TX, 0);
resetCounter(true);
_settings.load();
haAutoDiscTrigger = _settings.data.haDiscovery;
WiFi.persistent(true); // fix wifi save bug
// AsyncWiFiManager wm(&server, &dns); // create wifimanager instance

Expand Down Expand Up @@ -346,6 +349,7 @@ void setup()
_settings.data.webUIdarkmode = (request->arg("post_webuicolormode") == "true") ? true : false;
strncpy(_settings.data.httpUser, request->arg("post_httpUser").c_str(), 40);
strncpy(_settings.data.httpPass, request->arg("post_httpPass").c_str(), 40);
_settings.data.haDiscovery = (request->arg("post_hadiscovery") == "true") ? true : false;
_settings.save();
request->redirect("/reboot"); });

Expand Down Expand Up @@ -444,11 +448,15 @@ void loop()
}
notificationLED(); // notification LED routine

if (haDiscTrigger)
{
sendHaDiscovery();
haDiscTrigger = false;
}
//if ((haDiscTrigger || haAutoDiscTrigger) && measureJson(Json) > jsonSize)
if ((haDiscTrigger || _settings.data.haDiscovery) && measureJson(Json) > jsonSize)
{
if(sendHaDiscovery()){
haDiscTrigger = false;
//haAutoDiscTrigger = false;
jsonSize = measureJson(Json);
}
}
}

if (restartNow && millis() >= (RestartTimer + 500))
Expand Down Expand Up @@ -708,13 +716,48 @@ bool sendHaDiscovery()
{
return false;
}
String haDeviceDescription = String("\"dev\":") +
"{\"ids\":[\"" + mqttClientId + "\"]," +
"\"name\":\"" + _settings.data.deviceName + "\"," +
"\"cu\":\"http://" + WiFi.localIP().toString() + "\"," +
"\"mdl\":\"" + Json["Model_description"].as<String>().c_str() + "\"," +
"\"mf\":\"SoftWareCrash\"," +
"\"sw\":\"" + SOFTWARE_VERSION + "\"" +
"}";

char topBuff[128];
char configBuff[1024];
size_t mqttContentLength;
//char configBuff[1024];
//size_t mqttContentLength;
for (size_t i = 0; i < sizeof haDescriptor / sizeof haDescriptor[0]; i++)
{
if (Json.containsKey(haDescriptor[i][0]))
{
String haPayLoad = String("{") +
"\"name\":\"" + haDescriptor[i][0] + "\"," +
"\"stat_t\":\"" + _settings.data.mqttTopic + "/" + haDescriptor[i][0] + "\"," +
"\"uniq_id\":\"" + mqttClientId + "." + haDescriptor[i][0] + "\"," +
"\"ic\":\"mdi:" + haDescriptor[i][1] + "\",";
if (strlen(haDescriptor[i][2]) != 0)
haPayLoad += (String) "\"unit_of_meas\":\"" + haDescriptor[i][2] + "\",";
if (strlen(haDescriptor[i][3]) != 0)
haPayLoad += (String) "\"dev_cla\":\"" + haDescriptor[i][3] + "\",";
haPayLoad += haDeviceDescription;
haPayLoad += "}";
sprintf(topBuff, "homeassistant/sensor/%s/%s/config", _settings.data.deviceName, haDescriptor[i][0]); // build the topic
mqttclient.beginPublish(topBuff, haPayLoad.length(), true);
for (size_t i = 0; i < haPayLoad.length(); i++)
{
mqttclient.write(haPayLoad[i]);
}
mqttclient.endPublish();






/*
sprintf(topBuff, "homeassistant/sensor/%s/%s/config", _settings.data.deviceName, haDescriptor[i][0]); // build the topic
mqttContentLength = sprintf(configBuff, "{\"state_topic\": \"%s/%s\",\"unique_id\": \"sensor.%s_%s\",\"name\": \"%s\",\"icon\": \"mdi:%s\",\"unit_of_measurement\": \"%s\",\"device_class\":\"%s\",\"device\":{\"identifiers\":[\"%s\"], \"configuration_url\":\"http://%s\",\"name\":\"%s\", \"model\":\"%s\",\"manufacturer\":\"SoftWareCrash\",\"sw_version\":\"Victron2MQTT %s\"}}",
Expand All @@ -726,6 +769,7 @@ bool sendHaDiscovery()
mqttclient.write(configBuff[i]);
}
mqttclient.endPublish();
*/
}
}
return true;
Expand Down
7 changes: 7 additions & 0 deletions src/webpages/HTML_SETTINGS_EDIT.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ <h1>Edit Configuration</h1>
id="mqttjson" name="post_mqttjson" value="true" %pre_mqtt_json%>
</div>
</div>
<div class="input-group mb-2">
<span class="input-group-text w-50" id="mqtthadisc">HA Discovery</span>
<div class="form-switch form-control mqtt-settings-switch" style="width:50%%; text-align: center;">
<input type="checkbox" class="form-check-input form control" aria-describedby="mqtthadisc"
role="switch" id="mqttjson" name="post_hadiscovery" value="true" %pre_hadiscovery%>
</div>
</div>
<div class="input-group mb-2">
<span class="input-group-text w-50" id="webuicolormode">WebUI Dark Mode</span>
<div class="form-switch form-control" style="width:50%%; text-align: center;">
Expand Down

0 comments on commit 271d664

Please sign in to comment.