From 787c5c09bb8cf59a7440e5e97fc831f0f82a230e Mon Sep 17 00:00:00 2001 From: Magnus Persson Date: Mon, 1 Aug 2022 22:28:17 +0200 Subject: [PATCH] Updated workflow for HA registration --- html/format.htm | 78 +++++++++++++++++++++++++++++++----- html/format.min.htm | 4 +- src/pushtarget.cpp | 25 +++++++----- src/pushtarget.hpp | 2 +- src/webserver.cpp | 2 +- src_docs/source/releases.rst | 1 + 6 files changed, 87 insertions(+), 25 deletions(-) diff --git a/html/format.htm b/html/format.htm index 6c56627..3e5e78c 100644 --- a/html/format.htm +++ b/html/format.htm @@ -39,24 +39,36 @@
+ +
@@ -152,6 +164,16 @@ $(location.hash + '.collapse').collapse('show'); } }); + +/* + $("#format-btn").click(function(e){ + console.log(e) + var s = $("#push-target").val() + + if (s == "mqtt") { + console.log("Current format is mqtt, checking for HA device registration.") + } + });*/ $("#push-target").change(function(e){ console.log(e) @@ -179,19 +201,53 @@ // Store the format $("#format-btn").click(function(e) { var s = $("#format").val(); - s = s.replaceAll("\n", ""); - var obj = 'id=' + $("#id").val() + '&' + $("#push-target").val() + '=' + encodeURIComponent(s); - console.log(obj); + var ha = false; + + hideWarningHomeAssistant(); + if ($("#push-target").val() == "mqtt") { + console.log("Current format is mqtt, checking for HA device registration.") + if (s.search("homeassistant/sensor/") != -1) { + showWarningHomeAssistant(); + ha = true; + } + } + s = s.replaceAll("\n", ""); + var obj = 'id=' + $("#id").val() + "&" + $("#push-target").val() + '=' + encodeURIComponent(s); + console.log(obj); + $.ajax( { type: "POST", url: "/api/config/format", data: obj, - success: function(result) { showSuccess('Format stored successfully.'); getConfig(); }, + success: function(result) { showSuccess('Format stored successfully.'); getConfig(); postHomeAssistant(ha); }, error: function(result) { showError('Unable to store format.'); } } ); }); - + + function postHomeAssistant(active) { + if (!active) + return; + + var url = "/api/test/push"; + url += "?id=" + $("#id").val() + "&format=mqtt"; + //var url = "/test/push.json"; + $.getJSON(url, function (cfg) { + var code = cfg["code"]; + var success = cfg["success"]; + var enabled = cfg["enabled"]; + + if(success) { + showSuccess( "Format stored successfully. Home Assistant Device Registration Successful." ); + } else { + showError( "Format stored successfully. Home Assistant Device Registration Failed!" ); + } + }) + .fail(function () { + showError( "Format stored successfully. Home Assistant Device Registration Failed!" ); + }) + } + // Test the calibration $("#test-btn").click(function(e) { var url = "/api/status"; diff --git a/html/format.min.htm b/html/format.min.htm index 06b1d86..12f6120 100644 --- a/html/format.min.htm +++ b/html/format.min.htm @@ -1,4 +1,4 @@ -Beer Gravity Monitor


(C) Copyright 2021-22 Magnus Persson
\ No newline at end of file + { "id": "UBIDots-Post", "format": "%7B%0A%20%20%20%22temperature%22%3A%20%24%7Btemp%7D%2C%0A%20%20%20%22gravity%22%3A%20%24%7Bgravity%7D%2C%0A%20%20%20%22angle%22%3A%20%24%7Bangle%7D%2C%0A%20%20%20%22battery%22%3A%20%24%7Bbattery%7D%2C%0A%20%20%20%22rssi%22%3A%20%24%7Brssi%7D%0A%7D" } ];

(C) Copyright 2021-22 Magnus Persson
\ No newline at end of file diff --git a/src/pushtarget.cpp b/src/pushtarget.cpp index 7a8c838..e992719 100644 --- a/src/pushtarget.cpp +++ b/src/pushtarget.cpp @@ -416,9 +416,9 @@ void PushTarget::sendHttpGet(TemplatingEngine& engine, bool isSecure) { // // Send data to mqtt target // -void PushTarget::sendMqtt(TemplatingEngine& engine, bool isSecure) { +void PushTarget::sendMqtt(TemplatingEngine& engine, bool isSecure, bool skipHomeAssistantRegistration) { #if !defined(PUSH_DISABLE_LOGGING) - Log.notice(F("PUSH: Sending values to mqtt." CR)); + Log.notice(F("PUSH: Sending values to mqtt. Skip HA registration %s" CR), skipHomeAssistantRegistration ? "yes" : "no"); #endif _lastCode = 0; _lastSuccess = false; @@ -483,15 +483,20 @@ void PushTarget::sendMqtt(TemplatingEngine& engine, bool isSecure) { Log.verbose(F("PUSH: topic '%s', value '%s'." CR), topic.c_str(), value.c_str()); #endif - if (mqtt.publish(topic, value)) { - _lastSuccess = true; - Log.notice(F("PUSH: MQTT publish successful on %s" CR), topic.c_str()); - _lastCode = 0; + + if (skipHomeAssistantRegistration && topic.startsWith("homeassistant/sensor/")) { + Log.notice(F("PUSH: Ignoring Home Assistant registration topic %s" CR), topic.c_str()); } else { - _lastCode = mqtt.lastError(); - ErrorFileLog errLog; - errLog.addEntry("PUSH: MQTT push on " + topic + - " failed error=" + String(mqtt.lastError())); + if (mqtt.publish(topic, value)) { + _lastSuccess = true; + Log.notice(F("PUSH: MQTT publish successful on %s" CR), topic.c_str()); + _lastCode = 0; + } else { + _lastCode = mqtt.lastError(); + ErrorFileLog errLog; + errLog.addEntry("PUSH: MQTT push on " + topic + + " failed error=" + String(mqtt.lastError())); + } } index = next + 1; diff --git a/src/pushtarget.hpp b/src/pushtarget.hpp index 02b2485..3e56163 100644 --- a/src/pushtarget.hpp +++ b/src/pushtarget.hpp @@ -61,7 +61,7 @@ class PushTarget { sendHttpGet(engine, isSecure); } void sendInfluxDb2(TemplatingEngine& engine, bool isSecure); - void sendMqtt(TemplatingEngine& engine, bool isSecure); + void sendMqtt(TemplatingEngine& engine, bool isSecure, bool skipHomeAssistantRegistration = true); int getLastCode() { return _lastCode; } bool getLastSuccess() { return _lastSuccess; } }; diff --git a/src/webserver.cpp b/src/webserver.cpp index 8dca1a5..46a3fc5 100644 --- a/src/webserver.cpp +++ b/src/webserver.cpp @@ -947,7 +947,7 @@ void WebServerHandler::webHandleTestPush() { push.sendInfluxDb2(engine, myConfig.isInfluxSSL()); enabled = true; } else if (!type.compareTo(PARAM_FORMAT_MQTT) && myConfig.isMqttActive()) { - push.sendMqtt(engine, myConfig.isMqttSSL()); + push.sendMqtt(engine, myConfig.isMqttSSL(), false); enabled = true; } diff --git a/src_docs/source/releases.rst b/src_docs/source/releases.rst index 30978b3..0c01dd0 100644 --- a/src_docs/source/releases.rst +++ b/src_docs/source/releases.rst @@ -17,6 +17,7 @@ Documentation User interface ++++++++++++++ +* (beta3) Changed behavior for Home Assistant Device registration, this is only done when format template is saved, during normal operation only data values are posted on MQTT. * (beta2) Calibration temperature (for temp adjustment) can now be set under advanced settings. * (beta2) Changes length of device name from 12 to 63 chars. 63 is the max limit according to mdns. * Under format options its now possible to select brewfather ispindle format to avoid mixing endpoints.