Updated workflow for HA registration

This commit is contained in:
Magnus Persson 2022-08-01 22:28:17 +02:00
parent 980099a5e5
commit 787c5c09bb
6 changed files with 87 additions and 25 deletions

View File

@ -39,24 +39,36 @@
<div class="container row-margin-10"> <div class="container row-margin-10">
<div class="alert alert-success alert-dismissible hide fade d-none" role="alert"> <div class="alert alert-success alert-dismissible hide fade d-none" role="alert">
<div id="alert"></div> <div id="alert-msg"></div>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<div class="alert alert-warning alert-dismissible hide fade d-none" role="alert" id="warning-ha">
<div>Home Assistant device configuration detected in MQTT format. These messages will be posted when format is saved and not during gravity measurement.</div>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button> <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div> </div>
<script type="text/javascript"> <script type="text/javascript">
function showError( msg ) { function showError( msg ) {
$('.alert').removeClass('alert-success').addClass('alert-danger').removeClass('hide').addClass('show').removeClass('d-none'); $('#alert').removeClass('alert-success').addClass('alert-danger').removeClass('hide').addClass('show').removeClass('d-none');
$('#alert').text( msg ); $('#alert-msg').text( msg );
} }
function showSuccess( msg ) { function showSuccess( msg ) {
$('.alert').addClass('alert-success').removeClass('alert-danger').removeClass('hide').addClass('show').removeClass('d-none'); $('#alert').addClass('alert-success').removeClass('alert-danger').removeClass('hide').addClass('show').removeClass('d-none');
$('#alert').text( msg ); $('#alert-msg').text( msg );
} }
$("#alert-btn").click(function(e){ $("#alert-btn").click(function(e){
$('.alert').addClass('hide').removeClass('show').addClass('d-none'); $('#alert').addClass('hide').removeClass('show').addClass('d-none');
}); });
function showWarningHomeAssistant() {
$('#warning-ha').removeClass('d-none').addClass('show').removeClass('hide');
}
function hideWarningHomeAssistant() {
$('#warning-ha').addClass('d-none').removeClass('show').addClass('hide');
}
</script> </script>
<div class="accordion" id="accordion"> <div class="accordion" id="accordion">
@ -153,6 +165,16 @@
} }
}); });
/*
$("#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){ $("#push-target").change(function(e){
console.log(e) console.log(e)
selectFormat(); selectFormat();
@ -179,19 +201,53 @@
// Store the format // Store the format
$("#format-btn").click(function(e) { $("#format-btn").click(function(e) {
var s = $("#format").val(); var s = $("#format").val();
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", ""); s = s.replaceAll("\n", "");
var obj = 'id=' + $("#id").val() + '&' + $("#push-target").val() + '=' + encodeURIComponent(s); var obj = 'id=' + $("#id").val() + "&" + $("#push-target").val() + '=' + encodeURIComponent(s);
console.log(obj); console.log(obj);
$.ajax( { $.ajax( {
type: "POST", type: "POST",
url: "/api/config/format", url: "/api/config/format",
data: obj, 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.'); } 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 the calibration
$("#test-btn").click(function(e) { $("#test-btn").click(function(e) {
var url = "/api/status"; var url = "/api/status";

File diff suppressed because one or more lines are too long

View File

@ -416,9 +416,9 @@ void PushTarget::sendHttpGet(TemplatingEngine& engine, bool isSecure) {
// //
// Send data to mqtt target // 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) #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 #endif
_lastCode = 0; _lastCode = 0;
_lastSuccess = false; _lastSuccess = false;
@ -483,6 +483,10 @@ void PushTarget::sendMqtt(TemplatingEngine& engine, bool isSecure) {
Log.verbose(F("PUSH: topic '%s', value '%s'." CR), topic.c_str(), Log.verbose(F("PUSH: topic '%s', value '%s'." CR), topic.c_str(),
value.c_str()); value.c_str());
#endif #endif
if (skipHomeAssistantRegistration && topic.startsWith("homeassistant/sensor/")) {
Log.notice(F("PUSH: Ignoring Home Assistant registration topic %s" CR), topic.c_str());
} else {
if (mqtt.publish(topic, value)) { if (mqtt.publish(topic, value)) {
_lastSuccess = true; _lastSuccess = true;
Log.notice(F("PUSH: MQTT publish successful on %s" CR), topic.c_str()); Log.notice(F("PUSH: MQTT publish successful on %s" CR), topic.c_str());
@ -493,6 +497,7 @@ void PushTarget::sendMqtt(TemplatingEngine& engine, bool isSecure) {
errLog.addEntry("PUSH: MQTT push on " + topic + errLog.addEntry("PUSH: MQTT push on " + topic +
" failed error=" + String(mqtt.lastError())); " failed error=" + String(mqtt.lastError()));
} }
}
index = next + 1; index = next + 1;
lines--; lines--;

View File

@ -61,7 +61,7 @@ class PushTarget {
sendHttpGet(engine, isSecure); sendHttpGet(engine, isSecure);
} }
void sendInfluxDb2(TemplatingEngine& engine, bool 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; } int getLastCode() { return _lastCode; }
bool getLastSuccess() { return _lastSuccess; } bool getLastSuccess() { return _lastSuccess; }
}; };

View File

@ -947,7 +947,7 @@ void WebServerHandler::webHandleTestPush() {
push.sendInfluxDb2(engine, myConfig.isInfluxSSL()); push.sendInfluxDb2(engine, myConfig.isInfluxSSL());
enabled = true; enabled = true;
} else if (!type.compareTo(PARAM_FORMAT_MQTT) && myConfig.isMqttActive()) { } else if (!type.compareTo(PARAM_FORMAT_MQTT) && myConfig.isMqttActive()) {
push.sendMqtt(engine, myConfig.isMqttSSL()); push.sendMqtt(engine, myConfig.isMqttSSL(), false);
enabled = true; enabled = true;
} }

View File

@ -17,6 +17,7 @@ Documentation
User interface 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) 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. * (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. * Under format options its now possible to select brewfather ispindle format to avoid mixing endpoints.