From d6fb61d0c739c918aacc3b9bc4e64a81a7cd9e2a Mon Sep 17 00:00:00 2001 From: lolodomo Date: Sun, 18 May 2025 18:57:43 +0200 Subject: [PATCH] YAML config: add "description" field to channel for things (#4795) Signed-off-by: Laurent Garnier --- .../yaml/internal/things/YamlChannelDTO.java | 4 ++- .../internal/things/YamlThingProvider.java | 16 ++++++++-- .../fileconverter/YamlThingFileConverter.java | 3 ++ .../internal/things/YamlChannelDTOTest.java | 29 +++++++++++++++++++ 4 files changed, 48 insertions(+), 4 deletions(-) diff --git a/bundles/org.openhab.core.model.yaml/src/main/java/org/openhab/core/model/yaml/internal/things/YamlChannelDTO.java b/bundles/org.openhab.core.model.yaml/src/main/java/org/openhab/core/model/yaml/internal/things/YamlChannelDTO.java index 66ea9d207a..fd47e9e2f7 100644 --- a/bundles/org.openhab.core.model.yaml/src/main/java/org/openhab/core/model/yaml/internal/things/YamlChannelDTO.java +++ b/bundles/org.openhab.core.model.yaml/src/main/java/org/openhab/core/model/yaml/internal/things/YamlChannelDTO.java @@ -36,6 +36,7 @@ public class YamlChannelDTO { public String itemType; public String itemDimension; public String label; + public String description; public Map<@NonNull String, @NonNull Object> config; public YamlChannelDTO() { @@ -99,7 +100,7 @@ public class YamlChannelDTO { @Override public int hashCode() { - return Objects.hash(type, getKind(), getItemType(), label); + return Objects.hash(type, getKind(), getItemType(), label, description); } @Override @@ -112,6 +113,7 @@ public class YamlChannelDTO { YamlChannelDTO other = (YamlChannelDTO) obj; return Objects.equals(type, other.type) && getKind() == other.getKind() && Objects.equals(getItemType(), other.getItemType()) && Objects.equals(label, other.label) + && Objects.equals(description, other.description) && YamlElementUtils.equalsConfig(config, other.config); } } diff --git a/bundles/org.openhab.core.model.yaml/src/main/java/org/openhab/core/model/yaml/internal/things/YamlThingProvider.java b/bundles/org.openhab.core.model.yaml/src/main/java/org/openhab/core/model/yaml/internal/things/YamlThingProvider.java index 868d0dadc5..c2f8acbd74 100644 --- a/bundles/org.openhab.core.model.yaml/src/main/java/org/openhab/core/model/yaml/internal/things/YamlThingProvider.java +++ b/bundles/org.openhab.core.model.yaml/src/main/java/org/openhab/core/model/yaml/internal/things/YamlThingProvider.java @@ -380,6 +380,7 @@ public class YamlThingProvider extends AbstractProvider ChannelKind kind = channelDto.getKind(); String itemType = channelDto.getItemType(); String label = channelDto.label; + String description = channelDto.description; AutoUpdatePolicy autoUpdatePolicy = null; Configuration configuration = new Configuration(channelDto.config); if (channelDto.type != null) { @@ -392,6 +393,9 @@ public class YamlThingProvider extends AbstractProvider if (label == null) { label = channelType.getLabel(); } + if (description == null) { + description = channelType.getDescription(); + } autoUpdatePolicy = channelType.getAutoUpdatePolicy(); URI descUriO = channelType.getConfigDescriptionURI(); if (descUriO != null) { @@ -403,9 +407,15 @@ public class YamlThingProvider extends AbstractProvider } } - Channel channel = ChannelBuilder.create(new ChannelUID(thingUID, channelId), itemType).withKind(kind) - .withConfiguration(configuration).withType(channelTypeUID).withLabel(label) - .withAutoUpdatePolicy(autoUpdatePolicy).build(); + ChannelBuilder builder = ChannelBuilder.create(new ChannelUID(thingUID, channelId), itemType).withKind(kind) + .withConfiguration(configuration).withType(channelTypeUID).withAutoUpdatePolicy(autoUpdatePolicy); + if (label != null) { + builder.withLabel(label); + } + if (description != null) { + builder.withDescription(description); + } + Channel channel = builder.build(); channels.add(channel); addedChannelIds.add(channelId); }); diff --git a/bundles/org.openhab.core.model.yaml/src/main/java/org/openhab/core/model/yaml/internal/things/fileconverter/YamlThingFileConverter.java b/bundles/org.openhab.core.model.yaml/src/main/java/org/openhab/core/model/yaml/internal/things/fileconverter/YamlThingFileConverter.java index 350a7bd7d3..2f35f7a0a2 100644 --- a/bundles/org.openhab.core.model.yaml/src/main/java/org/openhab/core/model/yaml/internal/things/fileconverter/YamlThingFileConverter.java +++ b/bundles/org.openhab.core.model.yaml/src/main/java/org/openhab/core/model/yaml/internal/things/fileconverter/YamlThingFileConverter.java @@ -118,12 +118,15 @@ public class YamlThingFileConverter extends AbstractThingFileGenerator { ChannelType channelType = channelTypeRegistry.getChannelType(channelTypeUID, localeProvider.getLocale()); dto.label = channelType != null && channelType.getLabel().equals(channel.getLabel()) ? null : channel.getLabel(); + String descr = channelType != null ? channelType.getDescription() : null; + dto.description = descr != null && descr.equals(channel.getDescription()) ? null : channel.getDescription(); } else { dto.kind = channel.getKind() == ChannelKind.STATE ? null : "trigger"; String itemType = channel.getAcceptedItemType(); dto.itemType = itemType != null ? ItemUtil.getMainItemType(itemType) : null; dto.itemDimension = ItemUtil.getItemTypeExtension(itemType); dto.label = channel.getLabel(); + dto.description = channel.getDescription(); } Map config = new LinkedHashMap<>(); diff --git a/bundles/org.openhab.core.model.yaml/src/test/java/org/openhab/core/model/yaml/internal/things/YamlChannelDTOTest.java b/bundles/org.openhab.core.model.yaml/src/test/java/org/openhab/core/model/yaml/internal/things/YamlChannelDTOTest.java index 728158c7cc..1208f22afc 100644 --- a/bundles/org.openhab.core.model.yaml/src/test/java/org/openhab/core/model/yaml/internal/things/YamlChannelDTOTest.java +++ b/bundles/org.openhab.core.model.yaml/src/test/java/org/openhab/core/model/yaml/internal/things/YamlChannelDTOTest.java @@ -96,6 +96,10 @@ public class YamlChannelDTOTest { ch2.label = "A label"; assertTrue(ch1.equals(ch2)); + ch1.description = "A description"; + ch2.description = "A description"; + assertTrue(ch1.equals(ch2)); + ch1.config = Map.of("param1", "value", "param2", 50, "param3", true, "param4", List.of("val 1", "val 2")); ch2.config = Map.of("param1", "value", "param2", 50, "param3", true, "param4", List.of("val 1", "val 2")); assertTrue(ch1.equals(ch2)); @@ -218,6 +222,31 @@ public class YamlChannelDTOTest { assertTrue(ch1.equals(ch2)); } + @Test + public void testEqualsWithDescription() throws IOException { + YamlChannelDTO ch1 = new YamlChannelDTO(); + YamlChannelDTO ch2 = new YamlChannelDTO(); + + ch1.itemType = "String"; + ch2.itemType = "String"; + + ch1.description = null; + ch2.description = null; + assertTrue(ch1.equals(ch2)); + ch1.description = "A description"; + ch2.description = null; + assertFalse(ch1.equals(ch2)); + ch1.description = null; + ch2.description = "A description"; + assertFalse(ch1.equals(ch2)); + ch1.description = "A description"; + ch2.description = "A different description"; + assertFalse(ch1.equals(ch2)); + ch1.description = "A description"; + ch2.description = "A description"; + assertTrue(ch1.equals(ch2)); + } + @Test public void testEqualsWithConfigurations() throws IOException { YamlChannelDTO ch1 = new YamlChannelDTO();