From 1ff7cc0946024a510f11aef8de33e6cc6cd935cc Mon Sep 17 00:00:00 2001 From: Mark Herwege Date: Sat, 3 Feb 2024 22:08:19 +0100 Subject: [PATCH 1/5] channelType add unitHint Signed-off-by: Mark Herwege --- .../internal/channel/ChannelTypeResource.java | 5 +++-- .../core/internal/thing/ThingTypeResource.java | 5 +++-- .../schema/thing/thing-description-1.0.0.xsd | 1 + .../core/thing/dto/ChannelDefinitionDTO.java | 6 +++++- .../openhab/core/thing/dto/ChannelTypeDTO.java | 5 ++++- .../type/StateChannelTypeBuilderImpl.java | 16 ++++++++++++---- .../type/TriggerChannelTypeBuilderImpl.java | 2 +- .../org/openhab/core/thing/type/ChannelType.java | 15 ++++++++++++++- .../core/thing/type/StateChannelTypeBuilder.java | 9 +++++++++ .../thing/xml/internal/ChannelTypeConverter.java | 8 +++++++- 10 files changed, 59 insertions(+), 13 deletions(-) diff --git a/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/channel/ChannelTypeResource.java b/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/channel/ChannelTypeResource.java index 4d49ea77e2c..80a8c2faf28 100644 --- a/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/channel/ChannelTypeResource.java +++ b/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/channel/ChannelTypeResource.java @@ -79,6 +79,7 @@ * @author Yannick Schaus - Added filter to getAll * @author Markus Rathgeb - Migrated to JAX-RS Whiteboard Specification * @author Wouter Born - Migrated to OpenAPI annotations + * @author Mark Herwege - added unit hint */ @Component @JaxrsResource @@ -200,7 +201,7 @@ private ChannelTypeDTO convertToChannelTypeDTO(ChannelType channelType, Locale l return new ChannelTypeDTO(channelType.getUID().toString(), channelType.getLabel(), channelType.getDescription(), channelType.getCategory(), channelType.getItemType(), channelType.getKind(), parameters, - parameterGroups, channelType.getState(), channelType.getTags(), channelType.isAdvanced(), - channelType.getCommandDescription()); + parameterGroups, channelType.getState(), channelType.getUnitHint(), channelType.getTags(), + channelType.isAdvanced(), channelType.getCommandDescription()); } } diff --git a/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/thing/ThingTypeResource.java b/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/thing/ThingTypeResource.java index f80dfea8f48..603faf8978f 100644 --- a/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/thing/ThingTypeResource.java +++ b/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/thing/ThingTypeResource.java @@ -90,6 +90,7 @@ * @author Yannick Schaus - Added filter to getAll * @author Markus Rathgeb - Migrated to JAX-RS Whiteboard Specification * @author Wouter Born - Migrated to OpenAPI annotations + * @author Mark Herwege - added unit hint */ @Component @JaxrsResource @@ -250,8 +251,8 @@ private List convertToChannelDefinitionDTOs(List + diff --git a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/dto/ChannelDefinitionDTO.java b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/dto/ChannelDefinitionDTO.java index 8dfd0985642..17bc4fcf6ff 100644 --- a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/dto/ChannelDefinitionDTO.java +++ b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/dto/ChannelDefinitionDTO.java @@ -22,6 +22,7 @@ * * @author Dennis Nobel - Initial contribution * @author Chris Jackson - Added properties + * @author Mark Herwege - added unit hint */ public class ChannelDefinitionDTO { @@ -32,6 +33,7 @@ public class ChannelDefinitionDTO { public Map properties; public String category; public StateDescription stateDescription; + public String unitHint; public boolean advanced; public String typeUID; @@ -39,7 +41,8 @@ public ChannelDefinitionDTO() { } public ChannelDefinitionDTO(String id, String typeUID, String label, String description, Set tags, - String category, StateDescription stateDescription, boolean advanced, Map properties) { + String category, StateDescription stateDescription, String unitHint, boolean advanced, + Map properties) { this.description = description; this.label = label; this.id = id; @@ -47,6 +50,7 @@ public ChannelDefinitionDTO(String id, String typeUID, String label, String desc this.tags = tags; this.category = category; this.stateDescription = stateDescription; + this.unitHint = unitHint; this.advanced = advanced; this.properties = properties; } diff --git a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/dto/ChannelTypeDTO.java b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/dto/ChannelTypeDTO.java index 2b1de820200..061ce0dbe8e 100644 --- a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/dto/ChannelTypeDTO.java +++ b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/dto/ChannelTypeDTO.java @@ -25,6 +25,7 @@ * This is a data transfer object that is used with to serialize channel types. * * @author Chris Jackson - Initial contribution + * @author Mark Herwege - added unit hint */ public class ChannelTypeDTO { @@ -36,6 +37,7 @@ public class ChannelTypeDTO { public String itemType; public String kind; public StateDescription stateDescription; + public String unitHint; public Set tags; public String UID; public boolean advanced; @@ -47,7 +49,7 @@ public ChannelTypeDTO() { public ChannelTypeDTO(String uid, String label, String description, String category, String itemType, ChannelKind kind, List parameters, List parameterGroups, StateDescription stateDescription, - Set tags, boolean advanced, CommandDescription commandDescription) { + String unitHint, Set tags, boolean advanced, CommandDescription commandDescription) { this.UID = uid; this.label = label; this.description = description; @@ -55,6 +57,7 @@ public ChannelTypeDTO(String uid, String label, String description, String categ this.parameters = parameters; this.parameterGroups = parameterGroups; this.stateDescription = stateDescription; + this.unitHint = unitHint; this.tags = tags; this.kind = kind.toString(); this.itemType = itemType; diff --git a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/type/StateChannelTypeBuilderImpl.java b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/type/StateChannelTypeBuilderImpl.java index 806fa761b24..0a360dfbfe9 100644 --- a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/type/StateChannelTypeBuilderImpl.java +++ b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/type/StateChannelTypeBuilderImpl.java @@ -30,6 +30,7 @@ * StateChannelTypeBuilder to create {@link ChannelType}s of kind STATE * * @author Stefan Triller - Initial contribution + * @author Mark Herwege - added unit hint */ @NonNullByDefault public class StateChannelTypeBuilderImpl extends AbstractChannelTypeBuilder @@ -39,10 +40,10 @@ private static class StateChannelTypeImpl extends ChannelType { private StateChannelTypeImpl(ChannelTypeUID uid, boolean advanced, String itemType, String label, @Nullable String description, @Nullable String category, @Nullable Set tags, @Nullable StateDescription state, @Nullable CommandDescription commandDescription, - @Nullable URI configDescriptionURI, @Nullable AutoUpdatePolicy autoUpdatePolicy) - throws IllegalArgumentException { + @Nullable String unitHint, @Nullable URI configDescriptionURI, + @Nullable AutoUpdatePolicy autoUpdatePolicy) throws IllegalArgumentException { super(uid, advanced, itemType, ChannelKind.STATE, label, description, category, tags, state, - commandDescription, null, configDescriptionURI, autoUpdatePolicy); + commandDescription, unitHint, null, configDescriptionURI, autoUpdatePolicy); } } @@ -50,6 +51,7 @@ private StateChannelTypeImpl(ChannelTypeUID uid, boolean advanced, String itemTy private @Nullable StateDescriptionFragment stateDescriptionFragment; private @Nullable AutoUpdatePolicy autoUpdatePolicy; private @Nullable CommandDescription commandDescription; + private @Nullable String unitHint; public StateChannelTypeBuilderImpl(ChannelTypeUID channelTypeUID, String label, String itemType) { super(channelTypeUID, label); @@ -80,10 +82,16 @@ public StateChannelTypeBuilder withCommandDescription(@Nullable CommandDescripti return this; } + @Override + public StateChannelTypeBuilder withUnitHint(@Nullable String unitHint) { + this.unitHint = unitHint; + return this; + } + @Override public ChannelType build() { return new StateChannelTypeImpl(channelTypeUID, advanced, itemType, label, description, category, tags, stateDescriptionFragment != null ? stateDescriptionFragment.toStateDescription() : null, - commandDescription, configDescriptionURI, autoUpdatePolicy); + commandDescription, unitHint, configDescriptionURI, autoUpdatePolicy); } } diff --git a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/type/TriggerChannelTypeBuilderImpl.java b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/type/TriggerChannelTypeBuilderImpl.java index 226d0271ca9..cf733c9ecd2 100644 --- a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/type/TriggerChannelTypeBuilderImpl.java +++ b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/type/TriggerChannelTypeBuilderImpl.java @@ -36,7 +36,7 @@ private static class TriggerChannelTypeImpl extends ChannelType { TriggerChannelTypeImpl(ChannelTypeUID uid, boolean advanced, String label, @Nullable String description, @Nullable String category, @Nullable Set tags, @Nullable EventDescription event, @Nullable URI configDescriptionURI) throws IllegalArgumentException { - super(uid, advanced, null, ChannelKind.TRIGGER, label, description, category, tags, null, null, event, + super(uid, advanced, null, ChannelKind.TRIGGER, label, description, category, tags, null, null, null, event, configDescriptionURI, null); } } diff --git a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/type/ChannelType.java b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/type/ChannelType.java index 492ff5f8cee..ef9913d2fa7 100644 --- a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/type/ChannelType.java +++ b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/type/ChannelType.java @@ -32,6 +32,7 @@ * * @author Michael Grammling - Initial contribution * @author Henning Treu - add command options + * @author Mark Herwege - added unit hint */ @NonNullByDefault public class ChannelType extends AbstractDescriptionType { @@ -43,6 +44,7 @@ public class ChannelType extends AbstractDescriptionType { private final @Nullable String category; private final @Nullable StateDescription state; private final @Nullable CommandDescription commandDescription; + private final @Nullable String unitHint; private final @Nullable EventDescription event; private final @Nullable AutoUpdatePolicy autoUpdatePolicy; @@ -71,7 +73,7 @@ public class ChannelType extends AbstractDescriptionType { protected ChannelType(ChannelTypeUID uid, boolean advanced, @Nullable String itemType, ChannelKind kind, String label, @Nullable String description, @Nullable String category, @Nullable Set tags, @Nullable StateDescription state, @Nullable CommandDescription commandDescription, - @Nullable EventDescription event, @Nullable URI configDescriptionURI, + @Nullable String unitHint, @Nullable EventDescription event, @Nullable URI configDescriptionURI, @Nullable AutoUpdatePolicy autoUpdatePolicy) throws IllegalArgumentException { super(uid, label, description, configDescriptionURI); @@ -90,6 +92,7 @@ protected ChannelType(ChannelTypeUID uid, boolean advanced, @Nullable String ite this.category = category; this.state = state; this.commandDescription = commandDescription; + this.unitHint = unitHint; this.event = event; this.autoUpdatePolicy = autoUpdatePolicy; } @@ -141,6 +144,16 @@ public String toString() { return state; } + /** + * Returns the {@link unitHint} of a channel which gives information on what unit to suggest when creating an item + * linked to the channel. + * + * @return the {@link unitHint} + */ + public @Nullable String getUnitHint() { + return unitHint; + } + /** * Returns information about the supported events. * diff --git a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/type/StateChannelTypeBuilder.java b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/type/StateChannelTypeBuilder.java index 749c64f8892..6cf4fe47f9e 100644 --- a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/type/StateChannelTypeBuilder.java +++ b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/type/StateChannelTypeBuilder.java @@ -22,6 +22,7 @@ * Interface for builders for {@link ChannelType}s of kind STATE * * @author Stefan Triller - Initial contribution + * @author Mark Herwege - added unit hint */ @NonNullByDefault public interface StateChannelTypeBuilder extends ChannelTypeBuilder { @@ -48,4 +49,12 @@ public interface StateChannelTypeBuilder extends ChannelTypeBuilder { @@ -161,6 +162,10 @@ private boolean readBoolean(Map attributes, String attributeName return null; } + private @Nullable String readUnitHint(NodeIterator nodeIterator) throws ConversionException { + return (String) nodeIterator.nextValue("unit-hint", false); + } + @Override protected @Nullable ChannelTypeXmlResult unmarshalType(HierarchicalStreamReader reader, UnmarshallingContext context, Map attributes, NodeIterator nodeIterator) @@ -184,6 +189,7 @@ private boolean readBoolean(Map attributes, String attributeName : null; CommandDescription commandDescription = readCommandDescription(nodeIterator); + String unitHint = readUnitHint(nodeIterator); EventDescription eventDescription = readEventDescription(nodeIterator); AutoUpdatePolicy autoUpdatePolicy = readAutoUpdatePolicy(nodeIterator); @@ -203,7 +209,7 @@ private boolean readBoolean(Map attributes, String attributeName builder = ChannelTypeBuilder.state(channelTypeUID, label, itemType).isAdvanced(advanced) .withConfigDescriptionURI(configDescriptionURI) .withStateDescriptionFragment(stateDescriptionFragment).withAutoUpdatePolicy(autoUpdatePolicy) - .withCommandDescription(commandDescription); + .withCommandDescription(commandDescription).withUnitHint(unitHint); } else if (cKind == ChannelKind.TRIGGER) { TriggerChannelTypeBuilder triggerChannelTypeBuilder = ChannelTypeBuilder.trigger(channelTypeUID, label) .isAdvanced(advanced).withConfigDescriptionURI(configDescriptionURI); From 8ada35cc43e6f4e71b530776304f1afc07e57c3d Mon Sep 17 00:00:00 2001 From: Mark Herwege Date: Sun, 4 Feb 2024 21:53:50 +0100 Subject: [PATCH 2/5] fix thing dto from xml generation Signed-off-by: Mark Herwege --- bundles/org.openhab.core.thing/schema/catalog.cat | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bundles/org.openhab.core.thing/schema/catalog.cat b/bundles/org.openhab.core.thing/schema/catalog.cat index 847187e6ff4..655078ebcf8 100644 --- a/bundles/org.openhab.core.thing/schema/catalog.cat +++ b/bundles/org.openhab.core.thing/schema/catalog.cat @@ -1 +1 @@ -REWRITE_SYSTEM "https://openhab.org/schemas" "file:${project.basedir}/xsd/thing" \ No newline at end of file +REWRITE_SYSTEM "https://openhab.org/schemas" "thing" \ No newline at end of file From 4e881eae7d4da407493e056695307f44413c3e8c Mon Sep 17 00:00:00 2001 From: Mark Herwege Date: Mon, 5 Feb 2024 21:10:39 +0100 Subject: [PATCH 3/5] add xstream unit-hint alias Signed-off-by: Mark Herwege --- .../openhab/core/thing/xml/internal/ThingDescriptionReader.java | 1 + 1 file changed, 1 insertion(+) diff --git a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/xml/internal/ThingDescriptionReader.java b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/xml/internal/ThingDescriptionReader.java index 194b89ec7ba..416057f89cb 100644 --- a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/xml/internal/ThingDescriptionReader.java +++ b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/xml/internal/ThingDescriptionReader.java @@ -105,6 +105,7 @@ public void registerAliases(XStream xstream) { xstream.alias("tag", NodeValue.class); xstream.alias("state", StateDescription.class); xstream.alias("command", CommandDescription.class); + xstream.alias("unit-hint", NodeValue.class); xstream.alias("event", EventDescription.class); xstream.alias("options", NodeList.class); xstream.alias("option", NodeValue.class); From 1649418431e82682652898d9a2eed589aae421b6 Mon Sep 17 00:00:00 2001 From: Mark Herwege Date: Tue, 6 Feb 2024 10:49:36 +0100 Subject: [PATCH 4/5] unitHint attribute of item-type Signed-off-by: Mark Herwege --- .../internal/channel/ChannelTypeResource.java | 6 ++-- .../internal/thing/ThingTypeResource.java | 5 ++- .../schema/thing/thing-description-1.0.0.xsd | 11 +++++-- .../core/thing/dto/ChannelDefinitionDTO.java | 6 +--- .../core/thing/dto/ChannelTypeDTO.java | 8 ++--- .../ChannelTypeI18nLocalizationService.java | 4 ++- .../type/StateChannelTypeBuilderImpl.java | 32 +++++++++---------- .../type/TriggerChannelTypeBuilderImpl.java | 2 +- .../openhab/core/thing/type/ChannelType.java | 13 ++++---- .../xml/internal/ChannelTypeConverter.java | 24 +++++++++----- .../xml/internal/ThingDescriptionReader.java | 1 - 11 files changed, 62 insertions(+), 50 deletions(-) diff --git a/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/channel/ChannelTypeResource.java b/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/channel/ChannelTypeResource.java index 80a8c2faf28..917990b3f9e 100644 --- a/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/channel/ChannelTypeResource.java +++ b/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/channel/ChannelTypeResource.java @@ -200,8 +200,8 @@ private ChannelTypeDTO convertToChannelTypeDTO(ChannelType channelType, Locale l : configDescriptionDTO.parameterGroups; return new ChannelTypeDTO(channelType.getUID().toString(), channelType.getLabel(), channelType.getDescription(), - channelType.getCategory(), channelType.getItemType(), channelType.getKind(), parameters, - parameterGroups, channelType.getState(), channelType.getUnitHint(), channelType.getTags(), - channelType.isAdvanced(), channelType.getCommandDescription()); + channelType.getCategory(), channelType.getItemType(), channelType.getUnitHint(), channelType.getKind(), + parameters, parameterGroups, channelType.getState(), channelType.getTags(), channelType.isAdvanced(), + channelType.getCommandDescription()); } } diff --git a/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/thing/ThingTypeResource.java b/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/thing/ThingTypeResource.java index 603faf8978f..f80dfea8f48 100644 --- a/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/thing/ThingTypeResource.java +++ b/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/thing/ThingTypeResource.java @@ -90,7 +90,6 @@ * @author Yannick Schaus - Added filter to getAll * @author Markus Rathgeb - Migrated to JAX-RS Whiteboard Specification * @author Wouter Born - Migrated to OpenAPI annotations - * @author Mark Herwege - added unit hint */ @Component @JaxrsResource @@ -251,8 +250,8 @@ private List convertToChannelDefinitionDTOs(List - + @@ -71,7 +71,6 @@ - @@ -142,6 +141,14 @@ + + + + + + + + diff --git a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/dto/ChannelDefinitionDTO.java b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/dto/ChannelDefinitionDTO.java index 17bc4fcf6ff..8dfd0985642 100644 --- a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/dto/ChannelDefinitionDTO.java +++ b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/dto/ChannelDefinitionDTO.java @@ -22,7 +22,6 @@ * * @author Dennis Nobel - Initial contribution * @author Chris Jackson - Added properties - * @author Mark Herwege - added unit hint */ public class ChannelDefinitionDTO { @@ -33,7 +32,6 @@ public class ChannelDefinitionDTO { public Map properties; public String category; public StateDescription stateDescription; - public String unitHint; public boolean advanced; public String typeUID; @@ -41,8 +39,7 @@ public ChannelDefinitionDTO() { } public ChannelDefinitionDTO(String id, String typeUID, String label, String description, Set tags, - String category, StateDescription stateDescription, String unitHint, boolean advanced, - Map properties) { + String category, StateDescription stateDescription, boolean advanced, Map properties) { this.description = description; this.label = label; this.id = id; @@ -50,7 +47,6 @@ public ChannelDefinitionDTO(String id, String typeUID, String label, String desc this.tags = tags; this.category = category; this.stateDescription = stateDescription; - this.unitHint = unitHint; this.advanced = advanced; this.properties = properties; } diff --git a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/dto/ChannelTypeDTO.java b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/dto/ChannelTypeDTO.java index 061ce0dbe8e..7fbf98579ee 100644 --- a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/dto/ChannelTypeDTO.java +++ b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/dto/ChannelTypeDTO.java @@ -35,9 +35,9 @@ public class ChannelTypeDTO { public String label; public String category; public String itemType; + public String unitHint; public String kind; public StateDescription stateDescription; - public String unitHint; public Set tags; public String UID; public boolean advanced; @@ -47,9 +47,9 @@ public ChannelTypeDTO() { } public ChannelTypeDTO(String uid, String label, String description, String category, String itemType, - ChannelKind kind, List parameters, + String unitHint, ChannelKind kind, List parameters, List parameterGroups, StateDescription stateDescription, - String unitHint, Set tags, boolean advanced, CommandDescription commandDescription) { + Set tags, boolean advanced, CommandDescription commandDescription) { this.UID = uid; this.label = label; this.description = description; @@ -57,10 +57,10 @@ public ChannelTypeDTO(String uid, String label, String description, String categ this.parameters = parameters; this.parameterGroups = parameterGroups; this.stateDescription = stateDescription; - this.unitHint = unitHint; this.tags = tags; this.kind = kind.toString(); this.itemType = itemType; + this.unitHint = unitHint; this.advanced = advanced; this.commandDescription = commandDescription; } diff --git a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/i18n/ChannelTypeI18nLocalizationService.java b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/i18n/ChannelTypeI18nLocalizationService.java index 0f74c172433..496154d63a1 100644 --- a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/i18n/ChannelTypeI18nLocalizationService.java +++ b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/i18n/ChannelTypeI18nLocalizationService.java @@ -48,6 +48,7 @@ * @author Christoph Weitkamp - factored out from {@link org.openhab.core.thing.xml.internal.XmlChannelTypeProvider} and * {@link org.openhab.core.thing.xml.internal.XmlChannelGroupTypeProvider} * @author Henning Treu - factored out from {@link ThingTypeI18nLocalizationService} + * @author Mark Herwege - added unit hint */ @Component(service = ChannelTypeI18nLocalizationService.class) @NonNullByDefault @@ -146,9 +147,10 @@ public ChannelType createLocalizedChannelType(Bundle bundle, ChannelType channel if (itemType == null || itemType.isBlank()) { throw new IllegalArgumentException("If the kind is 'state', the item type must be set!"); } + String unitHint = channelType.getUnitHint(); builder = ChannelTypeBuilder.state(channelTypeUID, label == null ? defaultLabel : label, itemType) - .withStateDescriptionFragment(stateDescriptionFragment) + .withUnitHint(unitHint).withStateDescriptionFragment(stateDescriptionFragment) .withAutoUpdatePolicy(channelType.getAutoUpdatePolicy()).withCommandDescription(command); break; case TRIGGER: diff --git a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/type/StateChannelTypeBuilderImpl.java b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/type/StateChannelTypeBuilderImpl.java index 0a360dfbfe9..87bf10b32b1 100644 --- a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/type/StateChannelTypeBuilderImpl.java +++ b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/type/StateChannelTypeBuilderImpl.java @@ -37,21 +37,21 @@ public class StateChannelTypeBuilderImpl extends AbstractChannelTypeBuilder tags, + private StateChannelTypeImpl(ChannelTypeUID uid, boolean advanced, String itemType, @Nullable String unitHint, + String label, @Nullable String description, @Nullable String category, @Nullable Set tags, @Nullable StateDescription state, @Nullable CommandDescription commandDescription, - @Nullable String unitHint, @Nullable URI configDescriptionURI, - @Nullable AutoUpdatePolicy autoUpdatePolicy) throws IllegalArgumentException { - super(uid, advanced, itemType, ChannelKind.STATE, label, description, category, tags, state, - commandDescription, unitHint, null, configDescriptionURI, autoUpdatePolicy); + @Nullable URI configDescriptionURI, @Nullable AutoUpdatePolicy autoUpdatePolicy) + throws IllegalArgumentException { + super(uid, advanced, itemType, unitHint, ChannelKind.STATE, label, description, category, tags, state, + commandDescription, null, configDescriptionURI, autoUpdatePolicy); } } private final String itemType; + private @Nullable String unitHint; private @Nullable StateDescriptionFragment stateDescriptionFragment; private @Nullable AutoUpdatePolicy autoUpdatePolicy; private @Nullable CommandDescription commandDescription; - private @Nullable String unitHint; public StateChannelTypeBuilderImpl(ChannelTypeUID channelTypeUID, String label, String itemType) { super(channelTypeUID, label); @@ -63,6 +63,12 @@ public StateChannelTypeBuilderImpl(ChannelTypeUID channelTypeUID, String label, this.itemType = itemType; } + @Override + public StateChannelTypeBuilder withUnitHint(@Nullable String unitHint) { + this.unitHint = unitHint; + return this; + } + @Override public StateChannelTypeBuilder withStateDescriptionFragment( @Nullable StateDescriptionFragment stateDescriptionFragment) { @@ -82,16 +88,10 @@ public StateChannelTypeBuilder withCommandDescription(@Nullable CommandDescripti return this; } - @Override - public StateChannelTypeBuilder withUnitHint(@Nullable String unitHint) { - this.unitHint = unitHint; - return this; - } - @Override public ChannelType build() { - return new StateChannelTypeImpl(channelTypeUID, advanced, itemType, label, description, category, tags, - stateDescriptionFragment != null ? stateDescriptionFragment.toStateDescription() : null, - commandDescription, unitHint, configDescriptionURI, autoUpdatePolicy); + return new StateChannelTypeImpl(channelTypeUID, advanced, itemType, unitHint, label, description, category, + tags, stateDescriptionFragment != null ? stateDescriptionFragment.toStateDescription() : null, + commandDescription, configDescriptionURI, autoUpdatePolicy); } } diff --git a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/type/TriggerChannelTypeBuilderImpl.java b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/type/TriggerChannelTypeBuilderImpl.java index cf733c9ecd2..c1dfc0c89f3 100644 --- a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/type/TriggerChannelTypeBuilderImpl.java +++ b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/type/TriggerChannelTypeBuilderImpl.java @@ -36,7 +36,7 @@ private static class TriggerChannelTypeImpl extends ChannelType { TriggerChannelTypeImpl(ChannelTypeUID uid, boolean advanced, String label, @Nullable String description, @Nullable String category, @Nullable Set tags, @Nullable EventDescription event, @Nullable URI configDescriptionURI) throws IllegalArgumentException { - super(uid, advanced, null, ChannelKind.TRIGGER, label, description, category, tags, null, null, null, event, + super(uid, advanced, null, null, ChannelKind.TRIGGER, label, description, category, tags, null, null, event, configDescriptionURI, null); } } diff --git a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/type/ChannelType.java b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/type/ChannelType.java index ef9913d2fa7..09aedcc4ecd 100644 --- a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/type/ChannelType.java +++ b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/type/ChannelType.java @@ -70,11 +70,12 @@ public class ChannelType extends AbstractDescriptionType { * @throws IllegalArgumentException if the UID or the item type is null or empty, * or the meta information is null */ - protected ChannelType(ChannelTypeUID uid, boolean advanced, @Nullable String itemType, ChannelKind kind, - String label, @Nullable String description, @Nullable String category, @Nullable Set tags, - @Nullable StateDescription state, @Nullable CommandDescription commandDescription, - @Nullable String unitHint, @Nullable EventDescription event, @Nullable URI configDescriptionURI, - @Nullable AutoUpdatePolicy autoUpdatePolicy) throws IllegalArgumentException { + protected ChannelType(ChannelTypeUID uid, boolean advanced, @Nullable String itemType, @Nullable String unitHint, + ChannelKind kind, String label, @Nullable String description, @Nullable String category, + @Nullable Set tags, @Nullable StateDescription state, + @Nullable CommandDescription commandDescription, @Nullable EventDescription event, + @Nullable URI configDescriptionURI, @Nullable AutoUpdatePolicy autoUpdatePolicy) + throws IllegalArgumentException { super(uid, label, description, configDescriptionURI); if (kind == ChannelKind.STATE && (itemType == null || itemType.isBlank())) { @@ -85,6 +86,7 @@ protected ChannelType(ChannelTypeUID uid, boolean advanced, @Nullable String ite } this.itemType = itemType; + this.unitHint = unitHint; this.kind = kind; this.tags = tags == null ? Set.of() : Set.copyOf(tags); @@ -92,7 +94,6 @@ protected ChannelType(ChannelTypeUID uid, boolean advanced, @Nullable String ite this.category = category; this.state = state; this.commandDescription = commandDescription; - this.unitHint = unitHint; this.event = event; this.autoUpdatePolicy = autoUpdatePolicy; } diff --git a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/xml/internal/ChannelTypeConverter.java b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/xml/internal/ChannelTypeConverter.java index 5125125156e..76b0c6f4c3b 100644 --- a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/xml/internal/ChannelTypeConverter.java +++ b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/xml/internal/ChannelTypeConverter.java @@ -58,6 +58,9 @@ @NonNullByDefault public class ChannelTypeConverter extends AbstractDescriptionTypeConverter { + record ItemType(@Nullable String itemType, @Nullable String unitHint) { + } + public ChannelTypeConverter() { super(ChannelTypeXmlResult.class, "channel-type"); attributeMapValidator = new ConverterAttributeMapValidator( @@ -74,8 +77,15 @@ private boolean readBoolean(Map attributes, String attributeName return defaultValue; } - private @Nullable String readItemType(NodeIterator nodeIterator) throws ConversionException { - return (String) nodeIterator.nextValue("item-type", false); + private ItemType readItemType(NodeIterator nodeIterator) throws ConversionException { + Object next = nodeIterator.next("item-type", false); + if (next instanceof NodeValue nodeValue) { + String itemType = (String) nodeValue.getValue(); + Map attributes = nodeValue.getAttributes(); + String unitHint = attributes != null ? attributes.get("unitHint") : null; + return new ItemType(itemType, unitHint); + } + return new ItemType(null, null); } private @Nullable String readKind(NodeIterator nodeIterator) throws ConversionException { @@ -162,10 +172,6 @@ private boolean readBoolean(Map attributes, String attributeName return null; } - private @Nullable String readUnitHint(NodeIterator nodeIterator) throws ConversionException { - return (String) nodeIterator.nextValue("unit-hint", false); - } - @Override protected @Nullable ChannelTypeXmlResult unmarshalType(HierarchicalStreamReader reader, UnmarshallingContext context, Map attributes, NodeIterator nodeIterator) @@ -176,7 +182,10 @@ private boolean readBoolean(Map attributes, String attributeName String uid = system ? XmlHelper.getSystemUID(super.getID(attributes)) : super.getUID(attributes, context); ChannelTypeUID channelTypeUID = new ChannelTypeUID(uid); - String itemType = readItemType(nodeIterator); + ItemType type = readItemType(nodeIterator); + String itemType = type.itemType(); + String unitHint = type.unitHint(); + String kind = readKind(nodeIterator); String label = super.readLabel(nodeIterator); String description = super.readDescription(nodeIterator); @@ -189,7 +198,6 @@ private boolean readBoolean(Map attributes, String attributeName : null; CommandDescription commandDescription = readCommandDescription(nodeIterator); - String unitHint = readUnitHint(nodeIterator); EventDescription eventDescription = readEventDescription(nodeIterator); AutoUpdatePolicy autoUpdatePolicy = readAutoUpdatePolicy(nodeIterator); diff --git a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/xml/internal/ThingDescriptionReader.java b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/xml/internal/ThingDescriptionReader.java index 416057f89cb..194b89ec7ba 100644 --- a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/xml/internal/ThingDescriptionReader.java +++ b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/xml/internal/ThingDescriptionReader.java @@ -105,7 +105,6 @@ public void registerAliases(XStream xstream) { xstream.alias("tag", NodeValue.class); xstream.alias("state", StateDescription.class); xstream.alias("command", CommandDescription.class); - xstream.alias("unit-hint", NodeValue.class); xstream.alias("event", EventDescription.class); xstream.alias("options", NodeList.class); xstream.alias("option", NodeValue.class); From a16738c7f4476aaa63fae6909fe1bf2bdae90b2e Mon Sep 17 00:00:00 2001 From: Mark Herwege Date: Thu, 4 Apr 2024 16:45:26 +0200 Subject: [PATCH 5/5] Throw IAE on wrong use of unit hint Signed-off-by: Mark Herwege --- .../main/java/org/openhab/core/thing/type/ChannelType.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/type/ChannelType.java b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/type/ChannelType.java index 09aedcc4ecd..9eeec5fad9b 100644 --- a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/type/ChannelType.java +++ b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/type/ChannelType.java @@ -84,6 +84,10 @@ protected ChannelType(ChannelTypeUID uid, boolean advanced, @Nullable String ite if (kind == ChannelKind.TRIGGER && itemType != null) { throw new IllegalArgumentException("If the kind is 'trigger', the item type must not be set!"); } + if ((itemType == null || !itemType.startsWith("Number:")) && unitHint != null) { + throw new IllegalArgumentException( + "A unit hint must not be set if the item type is not a number with dimension!"); + } this.itemType = itemType; this.unitHint = unitHint;