[homematic] Fix min/max values for rollershutters (#13821)

* [homematic] Fix min/max values for rollershutters

For dimmers, the 1.0 max value sent by CCU was already converted to
percent values in the item state description. Do the same thing also for
roller shutters.

Signed-off-by: Danny Baumann <dannybaumann@web.de>
This commit is contained in:
maniac103 2022-12-03 13:29:52 +01:00 committed by GitHub
parent 3bd34b3256
commit 86e3b65816
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -31,7 +31,6 @@ import org.openhab.binding.homematic.internal.model.HmChannel;
import org.openhab.binding.homematic.internal.model.HmDatapoint; import org.openhab.binding.homematic.internal.model.HmDatapoint;
import org.openhab.binding.homematic.internal.model.HmDevice; import org.openhab.binding.homematic.internal.model.HmDevice;
import org.openhab.binding.homematic.internal.model.HmParamsetType; import org.openhab.binding.homematic.internal.model.HmParamsetType;
import org.openhab.binding.homematic.internal.type.MetadataUtils.OptionsBuilder;
import org.openhab.core.config.core.ConfigDescriptionBuilder; import org.openhab.core.config.core.ConfigDescriptionBuilder;
import org.openhab.core.config.core.ConfigDescriptionParameter; import org.openhab.core.config.core.ConfigDescriptionParameter;
import org.openhab.core.config.core.ConfigDescriptionParameterBuilder; import org.openhab.core.config.core.ConfigDescriptionParameterBuilder;
@ -260,57 +259,43 @@ public class HomematicTypeGeneratorImpl implements HomematicTypeGenerator {
channelType = DefaultSystemChannelTypeProvider.SYSTEM_BUTTON; channelType = DefaultSystemChannelTypeProvider.SYSTEM_BUTTON;
} else { } else {
String itemType = MetadataUtils.getItemType(dp); String itemType = MetadataUtils.getItemType(dp);
String category = MetadataUtils.getCategory(dp, itemType); StateDescriptionFragmentBuilder stateFragment = StateDescriptionFragmentBuilder.create()
String label = MetadataUtils.getLabel(dp); .withPattern(MetadataUtils.getStatePattern(dp)).withReadOnly(dp.isReadOnly());
String description = MetadataUtils.getDatapointDescription(dp);
List<StateOption> options = null;
if (dp.isEnumType()) {
options = MetadataUtils.generateOptions(dp, new OptionsBuilder<StateOption>() {
@Override
public StateOption createOption(String value, String description) {
return new StateOption(value, description);
}
});
}
StateDescriptionFragmentBuilder stateFragment = StateDescriptionFragmentBuilder.create();
if (dp.isNumberType()) { if (dp.isNumberType()) {
BigDecimal min = MetadataUtils.createBigDecimal(dp.getMinValue()); final BigDecimal min, max;
BigDecimal max = MetadataUtils.createBigDecimal(dp.getMaxValue()); if (ITEM_TYPE_DIMMER.equals(itemType) || ITEM_TYPE_ROLLERSHUTTER.equals(itemType)) {
if (ITEM_TYPE_DIMMER.equals(itemType) // those types use PercentTypeConverter, so set up min and max as percent values
&& (max.compareTo(new BigDecimal("1.0")) == 0 || max.compareTo(new BigDecimal("1.01")) == 0)) {
// For dimmers with a max value of 1.01 or 1.0 the values must be corrected
min = MetadataUtils.createBigDecimal(0); min = MetadataUtils.createBigDecimal(0);
max = MetadataUtils.createBigDecimal(100); max = MetadataUtils.createBigDecimal(100);
} else {
min = MetadataUtils.createBigDecimal(dp.getMinValue());
max = MetadataUtils.createBigDecimal(dp.getMaxValue());
}
stateFragment.withMinimum(min).withMaximum(max);
} else if (dp.isEnumType()) {
List<StateOption> options = MetadataUtils.generateOptions(dp,
(value, description) -> new StateOption(value, description));
if (options != null) {
stateFragment.withOptions(options);
} }
stateFragment.withMinimum(min).withMaximum(max).withPattern(MetadataUtils.getStatePattern(dp))
.withReadOnly(dp.isReadOnly());
} else {
stateFragment.withPattern(MetadataUtils.getStatePattern(dp)).withReadOnly(dp.isReadOnly());
}
if (options != null) {
stateFragment.withOptions(options);
} }
ChannelTypeBuilder channelTypeBuilder; String label = MetadataUtils.getLabel(dp);
EventDescription eventDescription = null; final ChannelTypeBuilder channelTypeBuilder;
if (dp.isTrigger()) { if (dp.isTrigger()) {
eventDescription = new EventDescription( EventDescription eventDescription = new EventDescription(
MetadataUtils.generateOptions(dp, new OptionsBuilder<EventOption>() { MetadataUtils.generateOptions(dp, (value, description) -> new EventOption(value, description)));
@Override
public EventOption createOption(String value, String description) {
return new EventOption(value, description);
}
}));
channelTypeBuilder = ChannelTypeBuilder.trigger(channelTypeUID, label) channelTypeBuilder = ChannelTypeBuilder.trigger(channelTypeUID, label)
.withEventDescription(eventDescription); .withEventDescription(eventDescription);
} else { } else {
channelTypeBuilder = ChannelTypeBuilder.state(channelTypeUID, label, itemType) channelTypeBuilder = ChannelTypeBuilder.state(channelTypeUID, label, itemType)
.withStateDescriptionFragment(stateFragment.build()); .withStateDescriptionFragment(stateFragment.build());
} }
channelType = channelTypeBuilder.isAdvanced(!MetadataUtils.isStandard(dp)).withDescription(description) channelType = channelTypeBuilder.isAdvanced(!MetadataUtils.isStandard(dp))
.withCategory(category).withConfigDescriptionURI(configDescriptionUriChannel).build(); .withDescription(MetadataUtils.getDatapointDescription(dp))
.withCategory(MetadataUtils.getCategory(dp, itemType))
.withConfigDescriptionURI(configDescriptionUriChannel).build();
} }
return channelType; return channelType;
} }
@ -326,41 +311,29 @@ public class HomematicTypeGeneratorImpl implements HomematicTypeGenerator {
for (HmDatapoint dp : channel.getDatapoints()) { for (HmDatapoint dp : channel.getDatapoints()) {
if (dp.getParamsetType() == HmParamsetType.MASTER) { if (dp.getParamsetType() == HmParamsetType.MASTER) {
String defaultValueString = Objects.toString(dp.getDefaultValue(), "");
ConfigDescriptionParameterBuilder builder = ConfigDescriptionParameterBuilder.create( ConfigDescriptionParameterBuilder builder = ConfigDescriptionParameterBuilder.create(
MetadataUtils.getParameterName(dp), MetadataUtils.getConfigDescriptionParameterType(dp)); MetadataUtils.getParameterName(dp), MetadataUtils.getConfigDescriptionParameterType(dp));
builder.withLabel(MetadataUtils.getLabel(dp)); builder.withLabel(MetadataUtils.getLabel(dp));
builder.withDefault(Objects.toString(dp.getDefaultValue(), "")); builder.withDefault(defaultValueString);
builder.withDescription(MetadataUtils.getDatapointDescription(dp)); builder.withDescription(MetadataUtils.getDatapointDescription(dp));
if (dp.isEnumType()) { if (dp.isEnumType()) {
builder.withLimitToOptions(dp.isEnumType()); builder.withLimitToOptions(dp.isEnumType());
List<ParameterOption> options = MetadataUtils.generateOptions(dp, List<ParameterOption> options = MetadataUtils.generateOptions(dp,
new OptionsBuilder<ParameterOption>() { (value, description) -> new ParameterOption(value, description));
@Override
public ParameterOption createOption(String value, String description) {
return new ParameterOption(value, description);
}
});
builder.withOptions(options); builder.withOptions(options);
if (dp.isEnumType()) { if (dp.isEnumType()) {
logger.trace("Checking if default option {} is valid", logger.trace("Checking if default option {} is valid",
Objects.toString(dp.getDefaultValue(), "")); Objects.toString(dp.getDefaultValue(), ""));
boolean needsChange = true; boolean needsChange = options.stream()
for (ParameterOption option : options) { .noneMatch(opt -> opt.getValue().equals(defaultValueString));
if (option.getValue().equals(Objects.toString(dp.getDefaultValue(), ""))) {
needsChange = false;
break;
}
}
if (needsChange) { if (needsChange) {
String defStr = Objects.toString(dp.getDefaultValue(), "0"); String defStr = Objects.toString(dp.getDefaultValue(), "0");
if (defStr == null) { int offset = defStr != null ? Integer.valueOf(defStr) : 0;
defStr = "0";
}
int offset = Integer.parseInt(defStr);
if (offset >= 0 && offset < options.size()) { if (offset >= 0 && offset < options.size()) {
ParameterOption defaultOption = options.get(offset); ParameterOption defaultOption = options.get(offset);
logger.trace("Changing default option to {} (offset {})", defaultOption, defStr); logger.trace("Changing default option to {} (offset {})", defaultOption, offset);
builder.withDefault(defaultOption.getValue()); builder.withDefault(defaultOption.getValue());
} else if (options.size() > 0) { } else if (options.size() > 0) {
ParameterOption defaultOption = options.get(0); ParameterOption defaultOption = options.get(0);