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="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>
</div>
<script type="text/javascript">
function showError( msg ) {
$('.alert').removeClass('alert-success').addClass('alert-danger').removeClass('hide').addClass('show').removeClass('d-none');
$('#alert').text( msg );
$('#alert').removeClass('alert-success').addClass('alert-danger').removeClass('hide').addClass('show').removeClass('d-none');
$('#alert-msg').text( msg );
}
function showSuccess( msg ) {
$('.alert').addClass('alert-success').removeClass('alert-danger').removeClass('hide').addClass('show').removeClass('d-none');
$('#alert').text( msg );
$('#alert').addClass('alert-success').removeClass('alert-danger').removeClass('hide').addClass('show').removeClass('d-none');
$('#alert-msg').text( msg );
}
$("#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>
<div class="accordion" id="accordion">
@ -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";

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
//
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;

View File

@ -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; }
};

View File

@ -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;
}

View File

@ -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.