Skip to content

Commit

Permalink
Extend Homeassistant discovery with button for flow start (#3415)
Browse files Browse the repository at this point in the history
* feat(homeassistant discovery): Add button for flow start

* Update

* Update

* Allow Flow Start MQTT topic to have a zero-length payload

* remove unused payload

---------

Co-authored-by: Slider0007 <[email protected]>
  • Loading branch information
caco3 and Slider0007 authored Dec 4, 2024
1 parent 50ada0a commit 179005f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
8 changes: 1 addition & 7 deletions code/components/jomjol_mqtt/interface_mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,13 +389,7 @@ bool mqtt_handler_flow_start(std::string _topic, char* _data, int _data_len)
{
ESP_LOGD(TAG, "Handler called: topic %s, data %.*s", _topic.c_str(), _data_len, _data);

if (_data_len > 0) {
MQTTCtrlFlowStart(_topic);
}
else {
LogFile.WriteToFile(ESP_LOG_WARN, TAG, "handler_flow_start: handler called, but no data");
}

MQTTCtrlFlowStart(_topic);
return ESP_OK;
}

Expand Down
13 changes: 10 additions & 3 deletions code/components/jomjol_mqtt/server_mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,12 @@ bool sendHomeAssistantDiscoveryTopic(std::string group, std::string field,
* This means a maintopic "home/test/watermeter" is transformed to the discovery topic "homeassistant/sensor/watermeter/..."
*/
std::string node_id = createNodeId(maintopic);
if (field == "problem") { // Special binary sensor which is based on error topic
if (field == "problem") { // Special case: Binary sensor which is based on error topic
topicFull = "homeassistant/binary_sensor/" + node_id + "/" + configTopic + "/config";
}
else if (field == "flowstart") { // Special case: Button
topicFull = "homeassistant/button/" + node_id + "/" + configTopic + "/config";
}
else {
topicFull = "homeassistant/sensor/" + node_id + "/" + configTopic + "/config";
}
Expand All @@ -102,7 +105,7 @@ bool sendHomeAssistantDiscoveryTopic(std::string group, std::string field,
"\"icon\": \"mdi:" + icon + "\",";

if (group != "") {
if (field == "problem") { // Special binary sensor which is based on error topic
if (field == "problem") { // Special case: Binary sensor which is based on error topic
payload += "\"state_topic\": \"~/" + group + "/error\",";
payload += "\"value_template\": \"{{ 'OFF' if 'no error' in value else 'ON'}}\",";
}
Expand All @@ -111,10 +114,13 @@ bool sendHomeAssistantDiscoveryTopic(std::string group, std::string field,
}
}
else {
if (field == "problem") { // Special binary sensor which is based on error topic
if (field == "problem") { // Special case: Binary sensor which is based on error topic
payload += "\"state_topic\": \"~/error\",";
payload += "\"value_template\": \"{{ 'OFF' if 'no error' in value else 'ON'}}\",";
}
else if (field == "flowstart") { // Special case: Button
payload += "\"cmd_t\":\"~/ctrl/flow_start\","; // Add command topic
}
else {
payload += "\"state_topic\": \"~/" + field + "\",";
}
Expand Down Expand Up @@ -177,6 +183,7 @@ bool MQTThomeassistantDiscovery(int qos) {
allSendsSuccessed |= sendHomeAssistantDiscoveryTopic("", "interval", "Interval", "clock-time-eight-outline", "min", "" , "measurement", "diagnostic", qos);
allSendsSuccessed |= sendHomeAssistantDiscoveryTopic("", "IP", "IP", "network-outline", "", "", "", "diagnostic", qos);
allSendsSuccessed |= sendHomeAssistantDiscoveryTopic("", "status", "Status", "list-status", "", "", "", "diagnostic", qos);
allSendsSuccessed |= sendHomeAssistantDiscoveryTopic("", "flowstart", "Manual Flow Start", "timer-play-outline", "", "", "", "", qos);


for (int i = 0; i < (*NUMBERS).size(); ++i) {
Expand Down

0 comments on commit 179005f

Please sign in to comment.