From 716045a174173b5fe5ac8de69196d5ef46f97a42 Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Mon, 5 Aug 2024 15:19:30 -0600 Subject: [PATCH] Support lists for metadata properties in items files (#4330) Signed-off-by: Cody Cutrer --- .../src/org/openhab/core/model/Items.xtext | 2 +- .../item/internal/GenericItemProvider.java | 10 +++++- .../internal/GenericItemProviderTest.java | 35 +++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/bundles/org.openhab.core.model.item/src/org/openhab/core/model/Items.xtext b/bundles/org.openhab.core.model.item/src/org/openhab/core/model/Items.xtext index ce2d709d1..fabd011d1 100644 --- a/bundles/org.openhab.core.model.item/src/org/openhab/core/model/Items.xtext +++ b/bundles/org.openhab.core.model.item/src/org/openhab/core/model/Items.xtext @@ -49,7 +49,7 @@ ModelBinding: ; ModelProperty: - key=ID '=' value=ValueType + key=ID '=' value+=ValueType (',' value+=ValueType)* ; ValueType returns ecore::EJavaObject: diff --git a/bundles/org.openhab.core.model.item/src/org/openhab/core/model/item/internal/GenericItemProvider.java b/bundles/org.openhab.core.model.item/src/org/openhab/core/model/item/internal/GenericItemProvider.java index e8d9c83ca..0e154869c 100644 --- a/bundles/org.openhab.core.model.item/src/org/openhab/core/model/item/internal/GenericItemProvider.java +++ b/bundles/org.openhab.core.model.item/src/org/openhab/core/model/item/internal/GenericItemProvider.java @@ -349,7 +349,15 @@ public class GenericItemProvider extends AbstractProvider String config = binding.getConfiguration(); Configuration configuration = new Configuration(); - binding.getProperties().forEach(p -> configuration.put(p.getKey(), p.getValue())); + binding.getProperties().forEach(p -> { + Object value = p.getValue(); + // Single valued lists get unwrapped to just their one value for + // backwards compatibility + if (value instanceof List listValue && listValue.size() == 1) { + value = listValue.get(0); + } + configuration.put(p.getKey(), value); + }); BindingConfigReader localReader = reader; if (reader == null) { diff --git a/itests/org.openhab.core.model.item.tests/src/main/java/org/openhab/core/model/item/internal/GenericItemProviderTest.java b/itests/org.openhab.core.model.item.tests/src/main/java/org/openhab/core/model/item/internal/GenericItemProviderTest.java index 9f8043d7d..09abe7bca 100644 --- a/itests/org.openhab.core.model.item.tests/src/main/java/org/openhab/core/model/item/internal/GenericItemProviderTest.java +++ b/itests/org.openhab.core.model.item.tests/src/main/java/org/openhab/core/model/item/internal/GenericItemProviderTest.java @@ -623,6 +623,41 @@ public class GenericItemProviderTest extends JavaOSGiTest { assertThat(metadata3, is(nullValue())); } + @Test + public void testMetadataPropertyTypes() { + String model = "Switch simple { namespace=\"value\"[string=\"string\", bool=false, int=2, stringList=\"string1\",\"string2\", boolList=false,true] } "; + + modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes())); + Item item = itemRegistry.get("simple"); + assertThat(item, is(notNullValue())); + + Metadata res = metadataRegistry.get(new MetadataKey("namespace", "simple")); + assertThat(res, is(notNullValue())); + assertThat(res.getValue(), is("value")); + assertThat(res.getConfiguration(), is(notNullValue())); + var config = res.getConfiguration(); + assertThat(config.size(), is(5)); + assertThat(config.get("string"), is("string")); + assertThat(config.get("bool"), is(false)); + assertThat(config.get("int"), is(new BigDecimal("2"))); + + var list = config.get("stringList"); + assertThat(list, is(notNullValue())); + assertThat(list, instanceOf(List.class)); + List values = (List) list; + assertThat(values.size(), is(2)); + assertThat(values.get(0), is("string1")); + assertThat(values.get(1), is("string2")); + + list = config.get("boolList"); + assertThat(list, is(notNullValue())); + assertThat(list, instanceOf(List.class)); + values = (List) list; + assertThat(values.size(), is(2)); + assertThat(values.get(0), is(false)); + assertThat(values.get(1), is(true)); + } + @Test public void testTagUpdate() { modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream("Switch s [foo]".getBytes()));