From 271d664e7b407815fc170f46781298cc51af77c0 Mon Sep 17 00:00:00 2001 From: Softwarecrash Date: Sun, 26 Nov 2023 16:01:32 +0100 Subject: [PATCH] fix some ha discovery issues add auto discovery mode --- platformio.ini | 2 +- src/Settings.h | 6 +++ src/VeDirectDataList.h | 2 +- src/htmlProzessor.h | 2 + src/main.cpp | 58 ++++++++++++++++++++++++---- src/webpages/HTML_SETTINGS_EDIT.html | 7 ++++ 6 files changed, 68 insertions(+), 9 deletions(-) diff --git a/platformio.ini b/platformio.ini index c1fb45b..04082be 100644 --- a/platformio.ini +++ b/platformio.ini @@ -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} diff --git a/src/Settings.h b/src/Settings.h index bc87fa2..79f55ac 100644 --- a/src/Settings.h +++ b/src/Settings.h @@ -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() @@ -108,6 +109,10 @@ class Settings { strcpy(data.httpPass, ""); } + if (data.haDiscovery && !data.haDiscovery) + { + data.haDiscovery = false; + } } void coVersCheck() { @@ -126,6 +131,7 @@ class Settings data.webUIdarkmode = false; strcpy(data.httpUser, ""); strcpy(data.httpPass, ""); + data.haDiscovery = false; save(); load(); } diff --git a/src/VeDirectDataList.h b/src/VeDirectDataList.h index 2fb93d7..69daf8c 100644 --- a/src/VeDirectDataList.h +++ b/src/VeDirectDataList.h @@ -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"}, diff --git a/src/htmlProzessor.h b/src/htmlProzessor.h index 8eb1606..ea4008d 100644 --- a/src/htmlProzessor.h +++ b/src/htmlProzessor.h @@ -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(); } diff --git a/src/main.cpp b/src/main.cpp index 2665f6a..ca898fc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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; @@ -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 @@ -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"); }); @@ -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)) @@ -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().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\"}}", @@ -726,6 +769,7 @@ bool sendHaDiscovery() mqttclient.write(configBuff[i]); } mqttclient.endPublish(); + */ } } return true; diff --git a/src/webpages/HTML_SETTINGS_EDIT.html b/src/webpages/HTML_SETTINGS_EDIT.html index 93ad91b..397ce85 100644 --- a/src/webpages/HTML_SETTINGS_EDIT.html +++ b/src/webpages/HTML_SETTINGS_EDIT.html @@ -50,6 +50,13 @@

Edit Configuration

id="mqttjson" name="post_mqttjson" value="true" %pre_mqtt_json%> +
+ HA Discovery +
+ +
+
WebUI Dark Mode