add value list support

This commit is contained in:
John
2023-06-03 10:45:08 +02:00
parent 5825975d48
commit 699f056381
4 changed files with 63 additions and 1 deletions
+20 -1
View File
@@ -200,12 +200,13 @@ type_switch-number =
type_switch-w-list = type_switch-w-list =
switch,, = onoff switch,, = onoff
switch,,,yesno = yesno switch,,,yesno = yesno
select,, =
# HA integration: the mapping list for rather binary sensor entities by field type, name, message, and unit. # HA integration: the mapping list for rather binary sensor entities by field type, name, message, and unit.
type_switch-list = type_switch-list =
binary_sensor,,measurement = onoff binary_sensor,,measurement = onoff
binary_sensor,,measurement,yesno = yesno binary_sensor,,measurement,yesno = yesno
sensor,, = sensor,,,list =
# HA integration: currently unused mapping lists for non-numeric/non-binary entities. # HA integration: currently unused mapping lists for non-numeric/non-binary entities.
#type_switch-string = #type_switch-string =
@@ -278,6 +279,24 @@ type_part-binary_sensoryesno = ,
"payload_on":"yes", "payload_on":"yes",
"payload_off":"no"%state_class "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). # the field specific part (evaluated after the message specific part).
# HA integration: set to the mapped %type_part from above # HA integration: set to the mapped %type_part from above
field_payload = %type_part field_payload = %type_part
+11
View File
@@ -149,6 +149,17 @@ type_map-datetime = string
#type_part-number = , #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). # the field specific part (evaluated after the message specific part).
#field_payload = %type_part #field_payload = %type_part
+28
View File
@@ -1111,6 +1111,34 @@ void MqttHandler::run() {
} }
values.set("step", ostr.str()); values.set("step", ostr.str());
} }
if (dataType->isNumeric() && field->isList() && !values["field_values-entry"].empty()) {
auto vl = (dynamic_cast<const ValueListDataField*>(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()) { if (!m_typeSwitches.empty()) {
values.reduce(true); values.reduce(true);
str = values.get("type_switch-by", false, false); str = values.get("type_switch-by", false, false);
+4
View File
@@ -551,6 +551,10 @@ class ValueListDataField : public SingleDataField {
// @copydoc // @copydoc
void dump(bool prependFieldSeparator, OutputFormat outputFormat, ostream* output) const override; void dump(bool prependFieldSeparator, OutputFormat outputFormat, ostream* output) const override;
/**
* @return the value=text assignments.
*/
const map<unsigned int, string>& getList() const { return m_values; }
protected: protected:
// @copydoc // @copydoc