Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,9 @@ private YamlItemDTO buildItemDTO(Item item, List<Metadata> channelLinks, List<Me
dto.name = item.getName();

String label = item.getLabel();
boolean patternSet = false;
String defaultPattern = getDefaultStatePattern(item);
if (label != null && !label.isEmpty()) {
dto.label = item.getLabel();
}
String patternToSet = stateFormatter != null && !stateFormatter.equals(defaultPattern) ? stateFormatter : null;
dto.format = patternToSet;
patternSet = patternToSet != null;

dto.type = item.getType();
String mainType = ItemUtil.getMainItemType(item.getType());
Expand Down Expand Up @@ -191,40 +186,42 @@ private YamlItemDTO buildItemDTO(Item item, List<Metadata> channelLinks, List<Me
if (value != null && !value.isEmpty()) {
dto.autoupdate = Boolean.valueOf(value);
}
} else if ("unit".equals(namespace)) {
continue;
}

if ("unit".equals(namespace)) {
dto.unit = value; // When unit value is empty string, keep it as empty string
} else if ("expire".equals(namespace)) {
Map<String, Object> configuration = md.getConfiguration();
if (configuration.isEmpty()) {
dto.expire = value; // When expire value is empty string, keep it as empty string
} else {
YamlMetadataDTO mdDto = new YamlMetadataDTO();
mdDto.value = value.isEmpty() ? null : value;
mdDto.config = configuration;
metadataDto.put(namespace, mdDto);
}
} else {
YamlMetadataDTO mdDto = new YamlMetadataDTO();
mdDto.value = value.isEmpty() ? null : value;
Map<String, Object> configuration = new LinkedHashMap<>();
String statePattern = null;
for (ConfigParameter param : getConfigurationParameters(md)) {
configuration.put(param.name(), param.value());
if ("stateDescription".equals(namespace) && "pattern".equals(param.name())) {
statePattern = param.value().toString();
}
continue;
}

if ("expire".equals(namespace) && md.getConfiguration().isEmpty()) {
dto.expire = value; // When expire value is empty string, keep it as empty string
continue;
}

if ("stateDescription".equals(namespace) && (value == null || value.isBlank())) {
Map<String, Object> config = md.getConfiguration();

String defaultPattern = getDefaultStatePattern(item);
if (config.isEmpty() && stateFormatter != null && !stateFormatter.equals(defaultPattern)) {
dto.format = stateFormatter;
continue;
}
// Ignore state description in case it contains only a state pattern and state pattern was injected
// in field format or is the default pattern
if (!(statePattern != null && configuration.size() == 1
&& (patternSet || statePattern.equals(defaultPattern)))) {
mdDto.config = configuration.isEmpty() ? null : configuration;
metadataDto.put(namespace, mdDto);
if (patternSet && statePattern != null) {
dto.format = null;
}

if (config.get("pattern") instanceof String pattern && !pattern.isBlank() && config.size() == 1) {
dto.format = pattern;
continue;
}
}

YamlMetadataDTO mdDto = new YamlMetadataDTO();
mdDto.value = value;
Map<String, Object> configuration = new LinkedHashMap<>();
for (ConfigParameter param : getConfigurationParameters(md)) {
configuration.put(param.name(), param.value());
}
mdDto.config = configuration.isEmpty() ? null : configuration;
metadataDto.put(namespace, mdDto);
}
dto.metadata = metadataDto.isEmpty() ? null : metadataDto;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.openhab.core.model.yaml.internal.items.YamlChannelLinkProvider;
import org.openhab.core.model.yaml.internal.items.YamlItemDTO;
import org.openhab.core.model.yaml.internal.items.YamlItemProvider;
import org.openhab.core.model.yaml.internal.items.YamlMetadataDTO;
import org.openhab.core.model.yaml.internal.items.YamlMetadataProvider;

@NonNullByDefault
Expand Down Expand Up @@ -98,6 +99,30 @@ public void testUnitMetadataEmptyStringStaysInShortForm() {
assertNull(dto.metadata);
}

@Test
public void testStateDescriptionMetadataConvertedToShortForm() {
Metadata stateDescriptionMetadata = new Metadata(new MetadataKey("stateDescription", "item_name"), "",
Map.of("pattern", "%d"));
YamlItemDTO dto = convertWithMetadata(stateDescriptionMetadata, "Number");
assertEquals("%d", dto.format);
assertNull(dto.metadata);
}

@Test
public void testStateDescriptionMetadataWithOtherConfigStaysInMetadata() {
Metadata stateDescriptionMetadata = new Metadata(new MetadataKey("stateDescription", "item_name"), "",
Map.of("pattern", "%d", "min", 0, "max", 100));
YamlItemDTO dto = convertWithMetadata(stateDescriptionMetadata, "Number");
assertNull(dto.format);
assertNotNull(dto.metadata);
YamlMetadataDTO stateDescDto = dto.metadata.get("stateDescription");
assertNotNull(stateDescDto);
assertEquals("", stateDescDto.getValue());
assertEquals("%d", stateDescDto.config.get("pattern"));
assertEquals(0, stateDescDto.config.get("min"));
assertEquals(100, stateDescDto.config.get("max"));
}

private YamlItemDTO convertWithMetadata(Metadata metadata, String itemType) {
CapturingYamlModelRepository repository = new CapturingYamlModelRepository();
YamlItemConverter converter = new YamlItemConverter(repository, mock(YamlItemProvider.class),
Expand Down
Loading