Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix storing of StateDescriptionFragment in AbstractStorageBasedTypeProvider #3598

Merged
merged 1 commit into from
May 9, 2023
Merged
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 @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -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)
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -382,7 +421,7 @@ static class ChannelTypeEntity {
public @NonNullByDefault({}) ChannelKind kind;
public Set<String> 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;
Expand All @@ -396,4 +435,13 @@ static class ChannelGroupTypeEntity {
public List<ChannelDefinitionEntity> 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<StateOption> options;
public @Nullable String pattern;
public boolean isReadOnly = false;
}
}