From c264d0a423ad9d8115d5ebb8f88c1bb34879bade Mon Sep 17 00:00:00 2001 From: "Jan N. Klug" Date: Tue, 9 May 2023 16:31:01 +0200 Subject: [PATCH] Fix storing of StateDescriptionFragment in AbstractStorageBasedTypeProvider Signed-off-by: Jan N. Klug --- .../AbstractStorageBasedTypeProvider.java | 56 +++++++++++++++++-- 1 file changed, 52 insertions(+), 4 deletions(-) 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 9142371a9ce..1a646a5389d 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 @@ -12,6 +12,7 @@ */ package org.openhab.core.thing.binding; +import java.math.BigDecimal; import java.net.URI; import java.util.Collection; import java.util.List; @@ -49,6 +50,7 @@ import org.openhab.core.types.StateDescription; import org.openhab.core.types.StateDescriptionFragment; import org.openhab.core.types.StateDescriptionFragmentBuilder; +import org.openhab.core.types.StateOption; /** * The {@link AbstractStorageBasedTypeProvider} is the base class for the implementation of a {@link Storage} based @@ -241,8 +243,12 @@ static ChannelTypeEntity mapToEntity(ChannelType channelType) { entity.tags = channelType.getTags(); entity.category = channelType.getCategory(); StateDescription stateDescription = channelType.getState(); - entity.stateDescriptionFragment = stateDescription == null ? null - : StateDescriptionFragmentBuilder.create(stateDescription).build(); + if (stateDescription != null) { + StateDescriptionFragment fragment = StateDescriptionFragmentBuilder.create(stateDescription).build(); + entity.stateDescriptionFragment = mapToEntity(fragment); + } else { + entity.stateDescriptionFragment = null; + } entity.commandDescription = channelType.getCommandDescription(); entity.event = channelType.getEvent(); entity.autoUpdatePolicy = channelType.getAutoUpdatePolicy(); @@ -260,6 +266,17 @@ static ChannelGroupTypeEntity mapToEntity(ChannelGroupType channelGroupType) { return entity; } + static StateDescriptionFragmentEntity mapToEntity(StateDescriptionFragment fragment) { + StateDescriptionFragmentEntity entity = new StateDescriptionFragmentEntity(); + entity.maximum = fragment.getMaximum(); + entity.minimum = fragment.getMinimum(); + entity.step = fragment.getStep(); + entity.options = fragment.getOptions(); + entity.pattern = fragment.getPattern(); + entity.isReadOnly = fragment.isReadOnly(); + return entity; + } + static ThingType mapFromEntity(ThingTypeEntity entity) { ThingTypeBuilder builder = ThingTypeBuilder.instance(entity.uid, entity.label) .withSupportedBridgeTypeUIDs(entity.supportedBridgeTypeRefs).withProperties(entity.properties) @@ -309,7 +326,8 @@ static ChannelType mapFromEntity(ChannelTypeEntity entity) { } if (builder instanceof StateChannelTypeBuilder stateBuilder) { if (entity.stateDescriptionFragment != null) { - stateBuilder.withStateDescriptionFragment(Objects.requireNonNull(entity.stateDescriptionFragment)); + stateBuilder.withStateDescriptionFragment( + mapFromEntity(Objects.requireNonNull(entity.stateDescriptionFragment))); } if (entity.commandDescription != null) { stateBuilder.withCommandDescription(Objects.requireNonNull(entity.commandDescription)); @@ -339,6 +357,27 @@ static ChannelGroupType mapFromEntity(ChannelGroupTypeEntity entity) { return builder.build(); } + static StateDescriptionFragment mapFromEntity(StateDescriptionFragmentEntity entity) { + StateDescriptionFragmentBuilder builder = StateDescriptionFragmentBuilder.create(); + if (entity.maximum != null) { + builder.withMaximum(Objects.requireNonNull(entity.maximum)); + } + if (entity.minimum != null) { + builder.withMinimum(Objects.requireNonNull(entity.minimum)); + } + if (entity.step != null) { + builder.withStep(Objects.requireNonNull(entity.step)); + } + if (entity.options != null) { + builder.withOptions(Objects.requireNonNull(entity.options)); + } + if (entity.pattern != null) { + builder.withPattern(Objects.requireNonNull(entity.pattern)); + } + builder.withReadOnly(entity.isReadOnly); + return builder.build(); + } + static class ThingTypeEntity { public @NonNullByDefault({}) ThingTypeUID uid; public @NonNullByDefault({}) String label; @@ -382,7 +421,7 @@ static class ChannelTypeEntity { public @NonNullByDefault({}) ChannelKind kind; public Set tags = Set.of(); public @Nullable String category; - public @Nullable StateDescriptionFragment stateDescriptionFragment; + public @Nullable StateDescriptionFragmentEntity stateDescriptionFragment; public @Nullable CommandDescription commandDescription; public @Nullable EventDescription event; public @Nullable AutoUpdatePolicy autoUpdatePolicy; @@ -396,4 +435,13 @@ static class ChannelGroupTypeEntity { public List channelDefinitions = List.of(); private @Nullable String category; } + + static class StateDescriptionFragmentEntity { + public @Nullable BigDecimal maximum; + public @Nullable BigDecimal minimum; + public @Nullable BigDecimal step; + public @Nullable List options; + public @Nullable String pattern; + public boolean isReadOnly = false; + } }