Allow using quotes in metadata state/command description value/label (#3363)

Signed-off-by: Miguel Álvarez <miguelwork92@gmail.com>
pull/3488/head
GiviMAD 2023-03-26 19:30:21 +02:00 committed by GitHub
parent 850a9239de
commit b6c8aeaf10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 5 deletions

View File

@ -63,8 +63,18 @@ public class MetadataCommandDescriptionProvider implements CommandDescriptionPro
if (metadata.getConfiguration().containsKey("options")) {
Stream.of(metadata.getConfiguration().get("options").toString().split(",")).forEach(o -> {
if (o.contains("=")) {
commandDescription.addCommandOption(
new CommandOption(o.split("=")[0].trim(), o.split("=")[1].trim()));
String command;
String label;
if (o.startsWith("\"")) {
String[] parts = o.trim().split("\"=\"");
command = removeSurroundingQuotes(parts[0]);
label = removeSurroundingQuotes(parts[1]);
} else {
String[] parts = o.trim().split("=");
command = parts[0];
label = parts[1];
}
commandDescription.addCommandOption(new CommandOption(command.trim(), label.trim()));
} else {
commandDescription.addCommandOption(new CommandOption(o.trim(), null));
}
@ -80,4 +90,15 @@ public class MetadataCommandDescriptionProvider implements CommandDescriptionPro
return null;
}
public static String removeSurroundingQuotes(String input) {
String output = input;
if (input.startsWith("\"")) {
output = output.substring(1);
}
if (input.endsWith("\"")) {
output = output.substring(0, output.length() - 1);
}
return output;
}
}

View File

@ -12,6 +12,8 @@
*/
package org.openhab.core.internal.items;
import static org.openhab.core.internal.items.MetadataCommandDescriptionProvider.removeSurroundingQuotes;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
@ -103,9 +105,22 @@ public class MetadataStateDescriptionFragmentProvider implements StateDescriptio
if (metadata.getConfiguration().containsKey("options")) {
List<StateOption> stateOptions = Stream
.of(metadata.getConfiguration().get("options").toString().split(",")).map(o -> {
return (o.contains("="))
? new StateOption(o.split("=")[0].trim(), o.split("=")[1].trim())
: new StateOption(o.trim(), null);
if (o.contains("=")) {
String value;
String label;
if (o.startsWith("\"")) {
String[] parts = o.trim().split("\"=\"");
value = removeSurroundingQuotes(parts[0]);
label = removeSurroundingQuotes(parts[1]);
} else {
String[] parts = o.trim().split("=");
value = parts[0];
label = parts[1];
}
return new StateOption(value.trim(), label.trim());
} else {
return new StateOption(o.trim(), null);
}
}).collect(Collectors.toList());
builder.withOptions(stateOptions);
}