Skip to content

Commit 98554af

Browse files
authored
Add unitHint to AbstractStorageBasedTypeProvider.ChannelTypeEntity (#4317)
* Add unitHint to AbstractStorageBasedTypeProvider.ChannelTypeEntity Signed-off-by: Anders Alfredsson <[email protected]>
1 parent 9d4ec61 commit 98554af

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/binding/AbstractStorageBasedTypeProvider.java

+5
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ static ChannelTypeEntity mapToEntity(ChannelType channelType) {
251251
entity.commandDescription = channelType.getCommandDescription();
252252
entity.event = channelType.getEvent();
253253
entity.autoUpdatePolicy = channelType.getAutoUpdatePolicy();
254+
entity.unitHint = channelType.getUnitHint();
254255
return entity;
255256
}
256257

@@ -334,6 +335,9 @@ static ChannelType mapFromEntity(ChannelTypeEntity entity) {
334335
if (entity.autoUpdatePolicy != null) {
335336
stateBuilder.withAutoUpdatePolicy(Objects.requireNonNull(entity.autoUpdatePolicy));
336337
}
338+
if (entity.unitHint != null) {
339+
stateBuilder.withUnitHint(entity.unitHint);
340+
}
337341
}
338342
if (builder instanceof TriggerChannelTypeBuilderImpl triggerBuilder) {
339343
if (entity.event != null) {
@@ -424,6 +428,7 @@ static class ChannelTypeEntity {
424428
public @Nullable CommandDescription commandDescription;
425429
public @Nullable EventDescription event;
426430
public @Nullable AutoUpdatePolicy autoUpdatePolicy;
431+
public @Nullable String unitHint;
427432
}
428433

429434
static class ChannelGroupTypeEntity {

bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/binding/AbstractStorageBasedTypeProviderTest.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,15 @@ public class AbstractStorageBasedTypeProviderTest {
4848

4949
@Test
5050
public void testStateChannelTypeProperlyMappedToEntityAndBack() {
51-
ChannelTypeUID channelTypeUID = new ChannelTypeUID("TestBinding:testChannelType");
51+
ChannelTypeUID channelTypeUID = new ChannelTypeUID("TestBinding:testQuantityChannelType");
5252

53-
ChannelType expected = ChannelTypeBuilder.state(channelTypeUID, "testLabel", "Switch")
53+
ChannelType expected = ChannelTypeBuilder.state(channelTypeUID, "testLabel", "Number:Length")
5454
.withDescription("testDescription").withCategory("testCategory")
5555
.withConfigDescriptionURI(URI.create("testBinding:testConfig"))
5656
.withAutoUpdatePolicy(AutoUpdatePolicy.VETO).isAdvanced(true).withTag("testTag")
5757
.withCommandDescription(CommandDescriptionBuilder.create().build())
58-
.withStateDescriptionFragment(StateDescriptionFragmentBuilder.create().build()).build();
58+
.withStateDescriptionFragment(StateDescriptionFragmentBuilder.create().build()).withUnitHint("km")
59+
.build();
5960
AbstractStorageBasedTypeProvider.ChannelTypeEntity entity = AbstractStorageBasedTypeProvider
6061
.mapToEntity(expected);
6162
ChannelType actual = AbstractStorageBasedTypeProvider.mapFromEntity(entity);
@@ -73,6 +74,7 @@ public void testStateChannelTypeProperlyMappedToEntityAndBack() {
7374
assertThat(actual.getState(), is(expected.getState()));
7475
assertThat(actual.getItemType(), is(expected.getItemType()));
7576
assertThat(actual.getTags(), hasItems(expected.getTags().toArray(String[]::new)));
77+
assertThat(actual.getUnitHint(), is(expected.getUnitHint()));
7678
}
7779

7880
@Test

0 commit comments

Comments
 (0)