From 699f056381ffd886b60ba04b8a08f8155670eacd Mon Sep 17 00:00:00 2001 From: John Date: Sat, 3 Jun 2023 10:34:32 +0200 Subject: [PATCH] add value list support --- contrib/etc/ebusd/mqtt-hassio.cfg | 21 ++++++++++++++++++- contrib/etc/ebusd/mqtt-integration.cfg | 11 ++++++++++ src/ebusd/mqtthandler.cpp | 28 ++++++++++++++++++++++++++ src/lib/ebus/data.h | 4 ++++ 4 files changed, 63 insertions(+), 1 deletion(-) diff --git a/contrib/etc/ebusd/mqtt-hassio.cfg b/contrib/etc/ebusd/mqtt-hassio.cfg index 2adb4775..ec8c76fb 100644 --- a/contrib/etc/ebusd/mqtt-hassio.cfg +++ b/contrib/etc/ebusd/mqtt-hassio.cfg @@ -200,12 +200,13 @@ type_switch-number = type_switch-w-list = switch,, = onoff switch,,,yesno = yesno + select,, = # HA integration: the mapping list for rather binary sensor entities by field type, name, message, and unit. type_switch-list = binary_sensor,,measurement = onoff binary_sensor,,measurement,yesno = yesno - sensor,, = + sensor,,,list = # HA integration: currently unused mapping lists for non-numeric/non-binary entities. #type_switch-string = @@ -278,6 +279,24 @@ type_part-binary_sensoryesno = , "payload_on":"yes", "payload_off":"no"%state_class +# optional format string for converting a fields value list into %field_values. +# "$value" and "$text" are being replaced by the corresponding part. +field_values-entry = "$text" +# optional separator for concatenating of field value list items. +field_values-separator = , +# optional prefix for surrounding field value list items. +field_values-prefix = [ +# optional suffix for surrounding field value list items. +field_values-suffix = ] + +# HA integration: %type_part variable for select %type_topic and sensor with list of known values +type_part-select = , + "command_topic":"%topic/set", + "options":%field_values +type_part-sensorlist = , + "device_class":"enum", + "options":%field_values + # the field specific part (evaluated after the message specific part). # HA integration: set to the mapped %type_part from above field_payload = %type_part diff --git a/contrib/etc/ebusd/mqtt-integration.cfg b/contrib/etc/ebusd/mqtt-integration.cfg index d07e3d4e..d87a287e 100644 --- a/contrib/etc/ebusd/mqtt-integration.cfg +++ b/contrib/etc/ebusd/mqtt-integration.cfg @@ -149,6 +149,17 @@ type_map-datetime = string #type_part-number = , +# optional format string for converting a fields value list into %field_values. +# "$value" and "$text" are being replaced by the corresponding part. +#field_values-entry = $text +# optional separator for concatenating of field value list items. +#field_values-separator = , +# optional prefix for surrounding field value list items. +#field_values-prefix = +# optional suffix for surrounding field value list items. +#field_values-suffix = + + # the field specific part (evaluated after the message specific part). #field_payload = %type_part diff --git a/src/ebusd/mqtthandler.cpp b/src/ebusd/mqtthandler.cpp index e51d6f17..a4575062 100755 --- a/src/ebusd/mqtthandler.cpp +++ b/src/ebusd/mqtthandler.cpp @@ -1111,6 +1111,34 @@ void MqttHandler::run() { } values.set("step", ostr.str()); } + if (dataType->isNumeric() && field->isList() && !values["field_values-entry"].empty()) { + auto vl = (dynamic_cast(field))->getList(); + string entryFormat = values["field_values-entry"]; + string::size_type pos = -1; + while ((pos = entryFormat.find('$', pos+1)) != std::string::npos) { + if (entryFormat.substr(pos+1, 4) == "text" || entryFormat.substr(pos+1, 5) == "value") { + entryFormat.replace(pos, 1, "%"); + } + } + entryFormat.replace(0, 0, "entry = "); + string result = values["field_values-prefix"]; + bool first = true; + for (const auto& it : vl) { + StringReplacers entry; + entry.parseLine(entryFormat); + entry.set("value", it.first); + entry.set("text", it.second); + entry.reduce(); + if (first) { + first = false; + } else { + result += values["field_values-separator"]; + } + result += entry.get("entry", false, false); + } + result += values["field_values-suffix"]; + values.set("field_values", result); + } if (!m_typeSwitches.empty()) { values.reduce(true); str = values.get("type_switch-by", false, false); diff --git a/src/lib/ebus/data.h b/src/lib/ebus/data.h index d154febf..49b11c51 100755 --- a/src/lib/ebus/data.h +++ b/src/lib/ebus/data.h @@ -551,6 +551,10 @@ class ValueListDataField : public SingleDataField { // @copydoc void dump(bool prependFieldSeparator, OutputFormat outputFormat, ostream* output) const override; + /** + * @return the value=text assignments. + */ + const map& getList() const { return m_values; } protected: // @copydoc