From 304768c0f5e2490755b5f850e26f929a299d6fe4 Mon Sep 17 00:00:00 2001 From: Leo Siepel Date: Sat, 2 Nov 2024 22:08:11 +0100 Subject: [PATCH] First part Signed-off-by: Leo Siepel --- .../schema/thing/thing-description-1.0.0.xsd | 1 + .../AbstractStorageBasedTypeProvider.java | 5 +++ .../internal/StateDescriptionConverter.java | 10 ++++- .../thing/events/ThingEventFactoryTest.java | 4 ++ ...adataStateDescriptionFragmentProvider.java | 5 +++ .../types/StateDescriptionFragmentImpl.java | 42 ++++++++++++++----- .../openhab/core/types/StateDescription.java | 25 ++++++++--- .../core/types/StateDescriptionFragment.java | 8 ++++ .../StateDescriptionFragmentBuilder.java | 16 ++++++- ...aStateDescriptionFragmentProviderTest.java | 2 + .../StateDescriptionServiceImplTest.java | 8 ++++ .../StateDescriptionFragmentImplTest.java | 3 ++ .../StateDescriptionFragmentBuilderTest.java | 17 +++++--- 13 files changed, 123 insertions(+), 23 deletions(-) diff --git a/bundles/org.openhab.core.thing/schema/thing/thing-description-1.0.0.xsd b/bundles/org.openhab.core.thing/schema/thing/thing-description-1.0.0.xsd index 03e162e0213..d254ec9e4c5 100644 --- a/bundles/org.openhab.core.thing/schema/thing/thing-description-1.0.0.xsd +++ b/bundles/org.openhab.core.thing/schema/thing/thing-description-1.0.0.xsd @@ -226,6 +226,7 @@ + diff --git a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/binding/AbstractStorageBasedTypeProvider.java b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/binding/AbstractStorageBasedTypeProvider.java index 35f7083b337..5f220aeca6f 100644 --- a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/binding/AbstractStorageBasedTypeProvider.java +++ b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/binding/AbstractStorageBasedTypeProvider.java @@ -271,6 +271,7 @@ static StateDescriptionFragmentEntity mapToEntity(StateDescriptionFragment fragm entity.maximum = fragment.getMaximum(); entity.minimum = fragment.getMinimum(); entity.step = fragment.getStep(); + entity.rangeUnit = fragment.getRangeUnit(); entity.options = fragment.getOptions(); entity.pattern = fragment.getPattern(); entity.isReadOnly = fragment.isReadOnly(); @@ -371,6 +372,9 @@ static StateDescriptionFragment mapFromEntity(StateDescriptionFragmentEntity ent if (entity.step != null) { builder.withStep(Objects.requireNonNull(entity.step)); } + if (entity.rangeUnit != null) { + builder.withRangeUnit(Objects.requireNonNull(entity.rangeUnit)); + } if (entity.options != null) { builder.withOptions(Objects.requireNonNull(entity.options)); } @@ -444,6 +448,7 @@ static class StateDescriptionFragmentEntity { public @Nullable BigDecimal maximum; public @Nullable BigDecimal minimum; public @Nullable BigDecimal step; + public @Nullable String rangeUnit; public @Nullable List options; public @Nullable String pattern; public boolean isReadOnly = false; diff --git a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/xml/internal/StateDescriptionConverter.java b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/xml/internal/StateDescriptionConverter.java index d9a4167cd50..5c086806eae 100644 --- a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/xml/internal/StateDescriptionConverter.java +++ b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/xml/internal/StateDescriptionConverter.java @@ -51,8 +51,9 @@ public class StateDescriptionConverter extends GenericUnmarshaller attributes, String attribute, @@ -123,6 +124,11 @@ private StateOption toChannelStateOption(NodeValue nodeValue) throws ConversionE builder.withStep(step); } + String rangeUnit = attributes.get("pattern"); + if (rangeUnit != null) { + builder.withRangeUnit(rangeUnit); + } + String pattern = attributes.get("pattern"); if (pattern != null) { builder.withPattern(pattern); diff --git a/bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/events/ThingEventFactoryTest.java b/bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/events/ThingEventFactoryTest.java index ec23985aa46..c314fbfe3bb 100644 --- a/bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/events/ThingEventFactoryTest.java +++ b/bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/events/ThingEventFactoryTest.java @@ -107,6 +107,7 @@ public class ThingEventFactoryTest { .withMinimum(BigDecimal.ZERO) // .withMaximum(new BigDecimal(1000)) // .withStep(new BigDecimal(100)) // + .withRangeUnit("K") // .withPattern("%.0f K") // .build()); private static final String CHANNEL_DESCRIPTION_OLD_STATE_DESCRIPTION_PAYLOAD = JSONCONVERTER @@ -114,6 +115,7 @@ public class ThingEventFactoryTest { .withMinimum(BigDecimal.ZERO) // .withMaximum(new BigDecimal(6000)) // .withStep(new BigDecimal(100)) // + .withRangeUnit("K") // .withPattern("%.0f K") // .build()); private static final String CHANNEL_DESCRIPTION_CHANGED_EVENT_PAYLOAD_NEW_AND_OLD_DESCRIPTION = JSONCONVERTER @@ -340,12 +342,14 @@ public void testCreateChannelDescriptionChangedEventOldAndNewStateDescription() .withMinimum(BigDecimal.ZERO) // .withMaximum(new BigDecimal(1000)) // .withStep(new BigDecimal(100)) // + .withRangeUnit("K") // .withPattern("%.0f K") // .build(); StateDescriptionFragment oldStateDescriptionFragment = StateDescriptionFragmentBuilder.create() // .withMinimum(BigDecimal.ZERO) // .withMaximum(new BigDecimal(6000)) // .withStep(new BigDecimal(100)) // + .withRangeUnit("K") // .withPattern("%.0f K") // .build(); ChannelDescriptionChangedEvent event = ThingEventFactory.createChannelDescriptionChangedEvent(CHANNEL_UID, diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/items/MetadataStateDescriptionFragmentProvider.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/items/MetadataStateDescriptionFragmentProvider.java index 6bbf754e665..fefae9afa0c 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/items/MetadataStateDescriptionFragmentProvider.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/items/MetadataStateDescriptionFragmentProvider.java @@ -96,6 +96,11 @@ public MetadataStateDescriptionFragmentProvider(final @Reference MetadataRegistr builder.withStep(getBigDecimal(step)); } + Object rangeUnit = metadata.getConfiguration().get("rangeUnit"); + if (rangeUnit != null) { + builder.withRangeUnit((String) rangeUnit); + } + Object readOnly = metadata.getConfiguration().get("readOnly"); if (readOnly != null) { builder.withReadOnly(getBoolean(readOnly)); diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/types/StateDescriptionFragmentImpl.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/types/StateDescriptionFragmentImpl.java index e719f799e38..665311238cf 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/types/StateDescriptionFragmentImpl.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/types/StateDescriptionFragmentImpl.java @@ -34,14 +34,16 @@ public class StateDescriptionFragmentImpl implements StateDescriptionFragment { private static class StateDescriptionImpl extends StateDescription { StateDescriptionImpl(@Nullable BigDecimal minimum, @Nullable BigDecimal maximum, @Nullable BigDecimal step, - @Nullable String pattern, boolean readOnly, @Nullable List options) { - super(minimum, maximum, step, pattern, readOnly, options); + @Nullable String rangeUnit, @Nullable String pattern, boolean readOnly, + @Nullable List options) { + super(minimum, maximum, step, rangeUnit, pattern, readOnly, options); } } private @Nullable BigDecimal minimum; private @Nullable BigDecimal maximum; private @Nullable BigDecimal step; + private @Nullable String rangeUnit; private @Nullable String pattern; private @Nullable Boolean readOnly; private @Nullable List options; @@ -59,6 +61,7 @@ public StateDescriptionFragmentImpl() { * @param minimum minimum value of the state * @param maximum maximum value of the state * @param step step size + * @param rangeUnit unit that applies to the min, max and step value * @param pattern pattern to render the state * @param readOnly if the state can be changed by the system * @param options predefined list of options @@ -66,11 +69,12 @@ public StateDescriptionFragmentImpl() { */ @Deprecated public StateDescriptionFragmentImpl(@Nullable BigDecimal minimum, @Nullable BigDecimal maximum, - @Nullable BigDecimal step, @Nullable String pattern, @Nullable Boolean readOnly, + @Nullable BigDecimal step, @Nullable String rangeUnit, @Nullable String pattern, @Nullable Boolean readOnly, @Nullable List options) { this.minimum = minimum; this.maximum = maximum; this.step = step; + this.rangeUnit = rangeUnit; this.pattern = pattern; this.readOnly = readOnly; this.options = options == null || options.isEmpty() ? List.of() : Collections.unmodifiableList(options); @@ -88,6 +92,7 @@ public StateDescriptionFragmentImpl(StateDescription legacy) { this.minimum = legacy.getMinimum(); this.maximum = legacy.getMaximum(); this.step = legacy.getStep(); + this.rangeUnit = legacy.getRangeUnit(); this.pattern = legacy.getPattern(); this.readOnly = Boolean.valueOf(legacy.isReadOnly()); if (!legacy.getOptions().isEmpty()) { @@ -104,6 +109,7 @@ public StateDescriptionFragmentImpl(StateDescriptionFragmentImpl source) { this.minimum = source.getMinimum(); this.maximum = source.getMaximum(); this.step = source.getStep(); + this.rangeUnit = source.getRangeUnit(); this.pattern = source.getPattern(); this.readOnly = source.isReadOnly(); this.options = source.getOptions(); @@ -136,6 +142,15 @@ public void setStep(BigDecimal step) { this.step = step; } + @Override + public @Nullable String getRangeUnit() { + return rangeUnit; + } + + public void setRangeUnit(String rangeUnit) { + this.rangeUnit = rangeUnit; + } + @Override public @Nullable String getPattern() { return pattern; @@ -165,12 +180,13 @@ public void setOptions(List options) { @Override public @Nullable StateDescription toStateDescription() { - if (minimum == null && maximum == null && step == null && readOnly == null && pattern == null - && options == null) { + if (minimum == null && maximum == null && step == null && rangeUnit == null && readOnly == null + && pattern == null && options == null) { return null; } final Boolean ro = readOnly; - return new StateDescriptionImpl(minimum, maximum, step, pattern, ro != null && ro.booleanValue(), options); + return new StateDescriptionImpl(minimum, maximum, step, rangeUnit, pattern, ro != null && ro.booleanValue(), + options); } /** @@ -190,6 +206,9 @@ public StateDescriptionFragment merge(StateDescriptionFragment fragment) { if (step == null) { step = fragment.getStep(); } + if (rangeUnit == null) { + rangeUnit = fragment.getRangeUnit(); + } if (pattern == null) { pattern = fragment.getPattern(); } @@ -209,6 +228,7 @@ public int hashCode() { result = prime * result + (minimum != null ? minimum.hashCode() : 0); result = prime * result + (maximum != null ? maximum.hashCode() : 0); result = prime * result + (step != null ? step.hashCode() : 0); + result = prime * result + (rangeUnit != null ? rangeUnit.hashCode() : 0); result = prime * result + (pattern != null ? pattern.hashCode() : 0); result = prime * result + (readOnly ? 1231 : 1237); result = prime * result + (options != null ? options.hashCode() : 0); @@ -228,13 +248,15 @@ public boolean equals(@Nullable Object obj) { } StateDescriptionFragmentImpl other = (StateDescriptionFragmentImpl) obj; return Objects.equals(minimum, other.minimum) && Objects.equals(maximum, other.maximum) - && Objects.equals(step, other.step) && Objects.equals(pattern, other.pattern) - && Objects.equals(readOnly, other.readOnly) && Objects.equals(options, other.options); + && Objects.equals(step, other.step) && Objects.equals(rangeUnit, other.rangeUnit) + && Objects.equals(pattern, other.pattern) && Objects.equals(readOnly, other.readOnly) + && Objects.equals(options, other.options); } @Override public String toString() { - return "StateDescription [minimum=" + minimum + ", maximum=" + maximum + ", step=" + step + ", pattern=" - + pattern + ", readOnly=" + readOnly + ", channelStateOptions=" + options + "]"; + return "StateDescription [minimum=" + minimum + ", maximum=" + maximum + ", step=" + step + ", rangeUnit=" + + rangeUnit + ", pattern=" + pattern + ", readOnly=" + readOnly + ", channelStateOptions=" + options + + "]"; } } diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/types/StateDescription.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/types/StateDescription.java index e946abde789..1b7305dcf45 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/types/StateDescription.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/types/StateDescription.java @@ -31,6 +31,7 @@ public class StateDescription { protected @Nullable final BigDecimal minimum; protected @Nullable final BigDecimal maximum; protected @Nullable final BigDecimal step; + protected @Nullable final String rangeUnit; protected @Nullable final String pattern; protected final boolean readOnly; protected final List options; @@ -41,15 +42,18 @@ public class StateDescription { * @param minimum minimum value of the state * @param maximum maximum value of the state * @param step step size + * @param rangeUnit unit that applies to the min, max and step value * @param pattern pattern to render the state * @param readOnly if the state can be changed by the system * @param options predefined list of options */ protected StateDescription(@Nullable BigDecimal minimum, @Nullable BigDecimal maximum, @Nullable BigDecimal step, - @Nullable String pattern, boolean readOnly, @Nullable List options) { + @Nullable String rangeUnit, @Nullable String pattern, boolean readOnly, + @Nullable List options) { this.minimum = minimum; this.maximum = maximum; this.step = step; + this.rangeUnit = rangeUnit; this.pattern = pattern; this.readOnly = readOnly; this.options = options == null ? List.of() : Collections.unmodifiableList(options); @@ -82,6 +86,15 @@ protected StateDescription(@Nullable BigDecimal minimum, @Nullable BigDecimal ma return step; } + /** + * Returns the unit that applies to the min, max and step + * + * @return range unit + */ + public @Nullable String getRangeUnit() { + return rangeUnit; + } + /** * Returns the pattern to render the state to a string. * @@ -119,6 +132,7 @@ public int hashCode() { result = prime * result + (minimum != null ? minimum.hashCode() : 0); result = prime * result + (maximum != null ? maximum.hashCode() : 0); result = prime * result + (step != null ? step.hashCode() : 0); + result = prime * result + (rangeUnit != null ? rangeUnit.hashCode() : 0); result = prime * result + (pattern != null ? pattern.hashCode() : 0); result = prime * result + (readOnly ? 1231 : 1237); result = prime * result + options.hashCode(); @@ -138,14 +152,15 @@ public boolean equals(@Nullable Object obj) { } StateDescription other = (StateDescription) obj; return Objects.equals(minimum, other.minimum) && Objects.equals(maximum, other.maximum) - && Objects.equals(step, other.step) && Objects.equals(pattern, other.pattern) - && readOnly == other.readOnly // + && Objects.equals(step, other.step) && Objects.equals(rangeUnit, other.rangeUnit) + && Objects.equals(pattern, other.pattern) && readOnly == other.readOnly // && options.equals(other.options); } @Override public String toString() { - return "StateDescription [minimum=" + minimum + ", maximum=" + maximum + ", step=" + step + ", pattern=" - + pattern + ", readOnly=" + readOnly + ", channelStateOptions=" + options + "]"; + return "StateDescription [minimum=" + minimum + ", maximum=" + maximum + ", step=" + step + ", rangeUnit=" + + rangeUnit + ", pattern=" + pattern + ", readOnly=" + readOnly + ", channelStateOptions=" + options + + "]"; } } diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/types/StateDescriptionFragment.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/types/StateDescriptionFragment.java index 87efacaf151..6bdd6fc6d4b 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/types/StateDescriptionFragment.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/types/StateDescriptionFragment.java @@ -51,6 +51,14 @@ public interface StateDescriptionFragment { @Nullable BigDecimal getStep(); + /** + * Returns the unit that applies to the min, max and step + * + * @return range unit + */ + @Nullable + String getRangeUnit(); + /** * Returns the pattern to render the state to a string. * diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/types/StateDescriptionFragmentBuilder.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/types/StateDescriptionFragmentBuilder.java index ff108fe24e7..aee69f44969 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/types/StateDescriptionFragmentBuilder.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/types/StateDescriptionFragmentBuilder.java @@ -31,6 +31,7 @@ public class StateDescriptionFragmentBuilder { private @Nullable BigDecimal minimum; private @Nullable BigDecimal maximum; private @Nullable BigDecimal step; + private @Nullable String rangeUnit; private @Nullable String pattern; private @Nullable Boolean readOnly; private @Nullable List options; @@ -43,6 +44,7 @@ private StateDescriptionFragmentBuilder(StateDescriptionFragment fragment) { this.minimum = fragment.getMinimum(); this.maximum = fragment.getMaximum(); this.step = fragment.getStep(); + this.rangeUnit = fragment.getRangeUnit(); this.pattern = fragment.getPattern(); this.readOnly = fragment.isReadOnly(); final List stateOptions = fragment.getOptions(); @@ -55,6 +57,7 @@ private StateDescriptionFragmentBuilder(StateDescription legacy) { this.minimum = legacy.getMinimum(); this.maximum = legacy.getMaximum(); this.step = legacy.getStep(); + this.rangeUnit = legacy.getRangeUnit(); this.pattern = legacy.getPattern(); this.readOnly = Boolean.valueOf(legacy.isReadOnly()); if (!legacy.getOptions().isEmpty()) { @@ -100,7 +103,7 @@ public static StateDescriptionFragmentBuilder create(StateDescription legacy) { */ @SuppressWarnings("deprecation") public StateDescriptionFragment build() { - return new StateDescriptionFragmentImpl(minimum, maximum, step, pattern, readOnly, options); + return new StateDescriptionFragmentImpl(minimum, maximum, step, rangeUnit, pattern, readOnly, options); } /** @@ -136,6 +139,17 @@ public StateDescriptionFragmentBuilder withStep(BigDecimal step) { return this; } + /** + * Set the range unit for the resulting {@link StateDescriptionFragment}. + * + * @param rangeUnit the range unit for the resulting {@link StateDescriptionFragment}. + * @return this builder. + */ + public StateDescriptionFragmentBuilder withRangeUnit(String rangeUnit) { + this.rangeUnit = rangeUnit; + return this; + } + /** * Set the pattern for the resulting {@link StateDescriptionFragment}. * diff --git a/bundles/org.openhab.core/src/test/java/org/openhab/core/internal/items/MetadataStateDescriptionFragmentProviderTest.java b/bundles/org.openhab.core/src/test/java/org/openhab/core/internal/items/MetadataStateDescriptionFragmentProviderTest.java index 0e5585bdd9c..89f29552429 100644 --- a/bundles/org.openhab.core/src/test/java/org/openhab/core/internal/items/MetadataStateDescriptionFragmentProviderTest.java +++ b/bundles/org.openhab.core/src/test/java/org/openhab/core/internal/items/MetadataStateDescriptionFragmentProviderTest.java @@ -94,6 +94,7 @@ public void testFragment() throws Exception { metadataConfig.put("min", 18.5); metadataConfig.put("max", "34"); metadataConfig.put("step", 3); + metadataConfig.put("rangeUnit", "°C"); metadataConfig.put("readOnly", "true"); metadataConfig.put("options", "OPTION1,OPTION2 , 3 =Option 3 ,\"4=4\"=\" Option=4 \" "); Metadata metadata = new Metadata(metadataKey, "N/A", metadataConfig); @@ -106,6 +107,7 @@ public void testFragment() throws Exception { assertEquals(new BigDecimal(18.5), stateDescriptionFragment.getMinimum()); assertEquals(new BigDecimal(34), stateDescriptionFragment.getMaximum()); assertEquals(new BigDecimal(3), stateDescriptionFragment.getStep()); + assertEquals("°C", stateDescriptionFragment.getRangeUnit()); assertEquals(true, stateDescriptionFragment.isReadOnly()); assertNotNull(stateDescriptionFragment.getOptions()); Iterator it = stateDescriptionFragment.getOptions().iterator(); diff --git a/bundles/org.openhab.core/src/test/java/org/openhab/core/internal/service/StateDescriptionServiceImplTest.java b/bundles/org.openhab.core/src/test/java/org/openhab/core/internal/service/StateDescriptionServiceImplTest.java index 701bc23fa0a..f5b18e68fde 100644 --- a/bundles/org.openhab.core/src/test/java/org/openhab/core/internal/service/StateDescriptionServiceImplTest.java +++ b/bundles/org.openhab.core/src/test/java/org/openhab/core/internal/service/StateDescriptionServiceImplTest.java @@ -43,6 +43,7 @@ public class StateDescriptionServiceImplTest { private static final BigDecimal STATE_DESCRIPTION_PROVIDER_DEFAULT_MIN_VALUE = BigDecimal.ZERO; private static final BigDecimal STATE_DESCRIPTION_PROVIDER_DEFAULT_MAX_VALUE = BigDecimal.ZERO; private static final BigDecimal STATE_DESCRIPTION_PROVIDER_DEFAULT_STEP = BigDecimal.ZERO; + private static final String STATE_DESCRIPTION_PROVIDER_DEFAULT_RANGE_UNIT = "x"; private static final String STATE_DESCRIPTION_PROVIDER_DEFAULT_PATTERN = "pattern1"; private static final Boolean STATE_DESCRIPTION_PROVIDER_DEFAULT_IS_READONLY = Boolean.FALSE; private static final List STATE_DESCRIPTION_PROVIDER_DEFAULT_OPTIONS = List.of(); @@ -64,6 +65,7 @@ public void testServiceWithOneStateDescriptionProvider() { .withMinimum(STATE_DESCRIPTION_PROVIDER_DEFAULT_MIN_VALUE) .withMaximum(STATE_DESCRIPTION_PROVIDER_DEFAULT_MAX_VALUE) .withStep(STATE_DESCRIPTION_PROVIDER_DEFAULT_STEP) + .withRangeUnit(STATE_DESCRIPTION_PROVIDER_DEFAULT_RANGE_UNIT) .withPattern(STATE_DESCRIPTION_PROVIDER_DEFAULT_PATTERN) .withReadOnly(STATE_DESCRIPTION_PROVIDER_DEFAULT_IS_READONLY) .withOptions(STATE_DESCRIPTION_PROVIDER_DEFAULT_OPTIONS).build(); @@ -74,6 +76,7 @@ public void testServiceWithOneStateDescriptionProvider() { assertThat(stateDescription.getMinimum(), is(STATE_DESCRIPTION_PROVIDER_DEFAULT_MIN_VALUE)); assertThat(stateDescription.getMaximum(), is(STATE_DESCRIPTION_PROVIDER_DEFAULT_MAX_VALUE)); assertThat(stateDescription.getStep(), is(STATE_DESCRIPTION_PROVIDER_DEFAULT_STEP)); + assertThat(stateDescription.getRangeUnit(), is(STATE_DESCRIPTION_PROVIDER_DEFAULT_RANGE_UNIT)); assertThat(stateDescription.getPattern(), is(STATE_DESCRIPTION_PROVIDER_DEFAULT_PATTERN)); assertThat(stateDescription.isReadOnly(), is(STATE_DESCRIPTION_PROVIDER_DEFAULT_IS_READONLY)); assertThat(stateDescription.getOptions(), is(STATE_DESCRIPTION_PROVIDER_DEFAULT_OPTIONS)); @@ -85,6 +88,7 @@ public void testMinValueMaxValueStepAndPatternTwoDescriptionProviders() { .withMinimum(new BigDecimal(-1)) // .withMaximum(new BigDecimal(-1)) // .withStep(new BigDecimal(-1)) // + .withRangeUnit("x") // .withPattern("pattern1").build(); registerStateDescriptionFragmentProvider(stateDescriptionFragment1, -1); @@ -92,6 +96,7 @@ public void testMinValueMaxValueStepAndPatternTwoDescriptionProviders() { .withMinimum(new BigDecimal(-2)) // .withMaximum(new BigDecimal(-2)) // .withStep(new BigDecimal(-2)) // + .withRangeUnit("y") // .withPattern("pattern2").build(); registerStateDescriptionFragmentProvider(stateDescriptionFragment2, -2); @@ -100,6 +105,7 @@ public void testMinValueMaxValueStepAndPatternTwoDescriptionProviders() { assertThat(stateDescription.getMinimum(), is(stateDescriptionFragment1.getMinimum())); assertThat(stateDescription.getMaximum(), is(stateDescriptionFragment1.getMaximum())); assertThat(stateDescription.getStep(), is(stateDescriptionFragment1.getStep())); + assertThat(stateDescription.getRangeUnit(), is(stateDescriptionFragment1.getRangeUnit())); assertThat(stateDescription.getPattern(), is(stateDescriptionFragment1.getPattern())); } @@ -188,6 +194,7 @@ public void testFragmentsAreMergedInProviderOrder() { .withMinimum(BigDecimal.ONE) // .withMaximum(BigDecimal.ONE) // .withStep(BigDecimal.ONE) // + .withRangeUnit("x") // .withPattern("base_pattern") // .withOptions(options).build(); registerStateDescriptionFragmentProvider(stateDescriptionFragment2, -2); @@ -197,6 +204,7 @@ public void testFragmentsAreMergedInProviderOrder() { assertThat(stateDescription.getMinimum(), is(BigDecimal.ZERO)); assertThat(stateDescription.getMaximum(), is(BigDecimal.TEN)); assertThat(stateDescription.getStep(), is(BigDecimal.ONE)); + assertThat(stateDescription.getRangeUnit(), is("x")); assertThat(stateDescription.getPattern(), is("pattern")); assertThat(stateDescription.isReadOnly(), is(true)); assertThat(stateDescription.getOptions(), is(options)); diff --git a/bundles/org.openhab.core/src/test/java/org/openhab/core/internal/types/StateDescriptionFragmentImplTest.java b/bundles/org.openhab.core/src/test/java/org/openhab/core/internal/types/StateDescriptionFragmentImplTest.java index 52b12183274..b1583d29cc2 100644 --- a/bundles/org.openhab.core/src/test/java/org/openhab/core/internal/types/StateDescriptionFragmentImplTest.java +++ b/bundles/org.openhab.core/src/test/java/org/openhab/core/internal/types/StateDescriptionFragmentImplTest.java @@ -42,6 +42,7 @@ public void setup() { source.setMinimum(BigDecimal.ZERO); source.setMaximum(BigDecimal.TEN); source.setStep(BigDecimal.ONE); + source.setRangeUnit("x"); source.setPattern("pattern"); source.setReadOnly(Boolean.TRUE); source.setOptions(List.of()); @@ -54,6 +55,7 @@ public void mergeFragment() { assertThat(fragment.getMinimum(), is(source.getMinimum())); assertThat(fragment.getMaximum(), is(source.getMaximum())); assertThat(fragment.getStep(), is(source.getStep())); + assertThat(fragment.getRangeUnit(), is(source.getRangeUnit())); assertThat(fragment.getPattern(), is(source.getPattern())); assertThat(fragment.isReadOnly(), is(source.isReadOnly())); assertThat(fragment.getOptions(), is(source.getOptions())); @@ -82,6 +84,7 @@ public void toStateDescription() { assertThat(stateDescription.getMinimum(), is(source.getMinimum())); assertThat(stateDescription.getMaximum(), is(source.getMaximum())); assertThat(stateDescription.getStep(), is(source.getStep())); + assertThat(stateDescription.getRangeUnit(), is(source.getRangeUnit())); assertThat(stateDescription.getPattern(), is(source.getPattern())); assertThat(stateDescription.isReadOnly(), is(source.isReadOnly())); assertThat(stateDescription.getOptions(), is(source.getOptions())); diff --git a/bundles/org.openhab.core/src/test/java/org/openhab/core/types/StateDescriptionFragmentBuilderTest.java b/bundles/org.openhab.core/src/test/java/org/openhab/core/types/StateDescriptionFragmentBuilderTest.java index 6652996d7d8..c372780fdb4 100644 --- a/bundles/org.openhab.core/src/test/java/org/openhab/core/types/StateDescriptionFragmentBuilderTest.java +++ b/bundles/org.openhab.core/src/test/java/org/openhab/core/types/StateDescriptionFragmentBuilderTest.java @@ -52,6 +52,11 @@ public void builderWithStep() { assertThat(builder.withStep(BigDecimal.TEN).build().getStep(), is(BigDecimal.TEN)); } + @Test + public void builderWithRangeUnit() { + assertThat(builder.withRangeUnit("x").build().getRangeUnit(), is("x")); + } + @Test public void builderWithPattern() { assertThat(builder.withPattern("pattern").build().getPattern(), is("pattern")); @@ -82,14 +87,15 @@ public void builderWithOptions() { @Test public void builderWithStateDescription() { - StateDescription source = new StateDescription(BigDecimal.ZERO, BigDecimal.TEN, BigDecimal.ONE, "pattern", true, - List.of(new StateOption("value", "label"))); + StateDescription source = new StateDescription(BigDecimal.ZERO, BigDecimal.TEN, BigDecimal.ONE, "rangeUnit", + "pattern", true, List.of(new StateOption("value", "label"))); StateDescriptionFragmentBuilder builder = StateDescriptionFragmentBuilder.create(source); StateDescriptionFragment fragment = builder.build(); assertThat(fragment.getMinimum(), is(source.getMinimum())); assertThat(fragment.getMaximum(), is(source.getMaximum())); assertThat(fragment.getStep(), is(source.getStep())); + assertThat(fragment.getRangeUnit(), is(source.getRangeUnit())); assertThat(fragment.getPattern(), is(source.getPattern())); assertThat(fragment.isReadOnly(), is(source.isReadOnly())); assertThat(fragment.getOptions(), is(source.getOptions())); @@ -100,15 +106,16 @@ public void builderWithStateDescription() { @Test public void subsequentBuildsCreateIndependentFragments() { StateDescriptionFragment fragment1 = builder.withMinimum(BigDecimal.ZERO).withMaximum(BigDecimal.TEN) - .withStep(BigDecimal.ONE).withPattern("pattern").withReadOnly(Boolean.FALSE) + .withStep(BigDecimal.ONE).withRangeUnit("x").withPattern("pattern").withReadOnly(Boolean.FALSE) .withOptions(List.of(new StateOption("value", "label"))).build(); StateDescriptionFragment fragment2 = builder.withMinimum(BigDecimal.ONE).withMaximum(BigDecimal.ONE) - .withStep(BigDecimal.ZERO).withPattern("pattern_new").withReadOnly(Boolean.TRUE).withOptions(List.of()) - .build(); + .withStep(BigDecimal.ZERO).withRangeUnit("y").withPattern("pattern_new").withReadOnly(Boolean.TRUE) + .withOptions(List.of()).build(); assertThat(fragment1.getMinimum(), is(not(fragment2.getMinimum()))); assertThat(fragment1.getMaximum(), is(not(fragment2.getMaximum()))); assertThat(fragment1.getStep(), is(not(fragment2.getStep()))); + assertThat(fragment1.getRangeUnit(), is(not(fragment2.getRangeUnit()))); assertThat(fragment1.getPattern(), is(not(fragment2.getPattern()))); assertThat(fragment1.isReadOnly(), is(not(fragment2.isReadOnly()))); assertThat(fragment1.getOptions(), is(not(fragment2.getOptions())));