[DISC] Add runtime configuration for OpenHAB (#1658)

* [DISC] Add runtime configuration for OpenHAB

Add an MQTT configurable system key "ohdiscovery" for OpenHAB auto discovery

* Docs and environments update
This commit is contained in:
Florian authored and GitHub committed 2023-05-31 11:35:35 -05:00
1 parent 4a28f491f5
commit 6e23ca029b
11 files changed
+83 -128

No files matched your search

+21 -2
View File
@@ -192,6 +192,20 @@ void announceDeviceTrigger(bool use_gateway_info, char* topic, char* type, char*
pub_custom_topic((char*)topic_to_publish.c_str(), sensor, true);
}
/*
* Remove a substring p from a given string s
*/
std::string remove_substring(std::string s, const std::string& p) {
std::string::size_type n = p.length();
for (std::string::size_type i = s.find(p);
i != std::string::npos;
i = s.find(p))
s.erase(i, n);
return s;
}
/**
* @brief Generate message and publish it on an MQTT discovery explorer. For HA @see https://www.home-assistant.io/docs/mqtt/discovery/
*
@@ -280,8 +294,13 @@ void createDiscovery(const char* sensor_type,
sensor["uniq_id"] = unique_id; //unique_id
if (retainCmd)
sensor["retain"] = retainCmd; // Retain command
if (value_template[0])
sensor["val_tpl"] = value_template; //value_template
if (value_template[0]) {
if (strstr(value_template, " | is_defined") != NULL && OpenHABDisc) {
sensor["val_tpl"] = remove_substring(value_template, " | is_defined"); //OpenHAB compatible HA auto discovery
} else {
sensor["val_tpl"] = value_template; //HA Auto discovery
}
}
if (payload_on[0]) {
if (strcmp(sensor_type, "button") == 0) {
sensor["pl_prs"] = payload_on; // payload_press for a button press