YAML config: add "description" field to channel for things (#4795)

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
pull/4718/head
lolodomo 2025-05-18 18:57:43 +02:00 committed by GitHub
parent c4c2c4c431
commit d6fb61d0c7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 48 additions and 4 deletions

View File

@ -36,6 +36,7 @@ public class YamlChannelDTO {
public String itemType; public String itemType;
public String itemDimension; public String itemDimension;
public String label; public String label;
public String description;
public Map<@NonNull String, @NonNull Object> config; public Map<@NonNull String, @NonNull Object> config;
public YamlChannelDTO() { public YamlChannelDTO() {
@ -99,7 +100,7 @@ public class YamlChannelDTO {
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(type, getKind(), getItemType(), label); return Objects.hash(type, getKind(), getItemType(), label, description);
} }
@Override @Override
@ -112,6 +113,7 @@ public class YamlChannelDTO {
YamlChannelDTO other = (YamlChannelDTO) obj; YamlChannelDTO other = (YamlChannelDTO) obj;
return Objects.equals(type, other.type) && getKind() == other.getKind() return Objects.equals(type, other.type) && getKind() == other.getKind()
&& Objects.equals(getItemType(), other.getItemType()) && Objects.equals(label, other.label) && Objects.equals(getItemType(), other.getItemType()) && Objects.equals(label, other.label)
&& Objects.equals(description, other.description)
&& YamlElementUtils.equalsConfig(config, other.config); && YamlElementUtils.equalsConfig(config, other.config);
} }
} }

View File

@ -380,6 +380,7 @@ public class YamlThingProvider extends AbstractProvider<Thing>
ChannelKind kind = channelDto.getKind(); ChannelKind kind = channelDto.getKind();
String itemType = channelDto.getItemType(); String itemType = channelDto.getItemType();
String label = channelDto.label; String label = channelDto.label;
String description = channelDto.description;
AutoUpdatePolicy autoUpdatePolicy = null; AutoUpdatePolicy autoUpdatePolicy = null;
Configuration configuration = new Configuration(channelDto.config); Configuration configuration = new Configuration(channelDto.config);
if (channelDto.type != null) { if (channelDto.type != null) {
@ -392,6 +393,9 @@ public class YamlThingProvider extends AbstractProvider<Thing>
if (label == null) { if (label == null) {
label = channelType.getLabel(); label = channelType.getLabel();
} }
if (description == null) {
description = channelType.getDescription();
}
autoUpdatePolicy = channelType.getAutoUpdatePolicy(); autoUpdatePolicy = channelType.getAutoUpdatePolicy();
URI descUriO = channelType.getConfigDescriptionURI(); URI descUriO = channelType.getConfigDescriptionURI();
if (descUriO != null) { if (descUriO != null) {
@ -403,9 +407,15 @@ public class YamlThingProvider extends AbstractProvider<Thing>
} }
} }
Channel channel = ChannelBuilder.create(new ChannelUID(thingUID, channelId), itemType).withKind(kind) ChannelBuilder builder = ChannelBuilder.create(new ChannelUID(thingUID, channelId), itemType).withKind(kind)
.withConfiguration(configuration).withType(channelTypeUID).withLabel(label) .withConfiguration(configuration).withType(channelTypeUID).withAutoUpdatePolicy(autoUpdatePolicy);
.withAutoUpdatePolicy(autoUpdatePolicy).build(); if (label != null) {
builder.withLabel(label);
}
if (description != null) {
builder.withDescription(description);
}
Channel channel = builder.build();
channels.add(channel); channels.add(channel);
addedChannelIds.add(channelId); addedChannelIds.add(channelId);
}); });

View File

@ -118,12 +118,15 @@ public class YamlThingFileConverter extends AbstractThingFileGenerator {
ChannelType channelType = channelTypeRegistry.getChannelType(channelTypeUID, localeProvider.getLocale()); ChannelType channelType = channelTypeRegistry.getChannelType(channelTypeUID, localeProvider.getLocale());
dto.label = channelType != null && channelType.getLabel().equals(channel.getLabel()) ? null dto.label = channelType != null && channelType.getLabel().equals(channel.getLabel()) ? null
: channel.getLabel(); : channel.getLabel();
String descr = channelType != null ? channelType.getDescription() : null;
dto.description = descr != null && descr.equals(channel.getDescription()) ? null : channel.getDescription();
} else { } else {
dto.kind = channel.getKind() == ChannelKind.STATE ? null : "trigger"; dto.kind = channel.getKind() == ChannelKind.STATE ? null : "trigger";
String itemType = channel.getAcceptedItemType(); String itemType = channel.getAcceptedItemType();
dto.itemType = itemType != null ? ItemUtil.getMainItemType(itemType) : null; dto.itemType = itemType != null ? ItemUtil.getMainItemType(itemType) : null;
dto.itemDimension = ItemUtil.getItemTypeExtension(itemType); dto.itemDimension = ItemUtil.getItemTypeExtension(itemType);
dto.label = channel.getLabel(); dto.label = channel.getLabel();
dto.description = channel.getDescription();
} }
Map<String, Object> config = new LinkedHashMap<>(); Map<String, Object> config = new LinkedHashMap<>();

View File

@ -96,6 +96,10 @@ public class YamlChannelDTOTest {
ch2.label = "A label"; ch2.label = "A label";
assertTrue(ch1.equals(ch2)); 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")); 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")); ch2.config = Map.of("param1", "value", "param2", 50, "param3", true, "param4", List.of("val 1", "val 2"));
assertTrue(ch1.equals(ch2)); assertTrue(ch1.equals(ch2));
@ -218,6 +222,31 @@ public class YamlChannelDTOTest {
assertTrue(ch1.equals(ch2)); 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 @Test
public void testEqualsWithConfigurations() throws IOException { public void testEqualsWithConfigurations() throws IOException {
YamlChannelDTO ch1 = new YamlChannelDTO(); YamlChannelDTO ch1 = new YamlChannelDTO();