Fix StateDescription deprecations (#9352)

Related to #1408

Signed-off-by: Wouter Born <github@maindrain.net>
This commit is contained in:
Wouter Born 2020-12-13 08:53:16 +01:00 committed by GitHub
parent 863606e311
commit 8f53e320dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 59 additions and 51 deletions

View File

@ -37,7 +37,8 @@ import org.openhab.core.thing.type.ChannelType;
import org.openhab.core.thing.type.ChannelTypeBuilder;
import org.openhab.core.thing.type.ChannelTypeProvider;
import org.openhab.core.thing.type.ChannelTypeUID;
import org.openhab.core.types.StateDescription;
import org.openhab.core.types.StateDescriptionFragment;
import org.openhab.core.types.StateDescriptionFragmentBuilder;
import org.openhab.core.types.StateOption;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
@ -322,7 +323,7 @@ public class DsChannelTypeProvider extends BaseDsI18n implements ChannelTypeProv
return null;
}
private StateDescription getSensorStateDescription(SensorEnum sensorType) {
private StateDescriptionFragment getSensorStateDescription(SensorEnum sensorType) {
// the digitalSTROM resolution for temperature in kelvin is not correct but sensor-events and cached values are
// shown in °C so we will use this unit for temperature sensors
String unitShortCut = sensorType.getUnitShortcut();
@ -332,14 +333,15 @@ public class DsChannelTypeProvider extends BaseDsI18n implements ChannelTypeProv
if (sensorType.toString().contains("TEMPERATURE")) {
unitShortCut = "°C";
}
return new StateDescription(null, null, null, sensorType.getPattern() + " " + unitShortCut, true, null);
return StateDescriptionFragmentBuilder.create().withPattern(sensorType.getPattern() + " " + unitShortCut)
.withReadOnly(true).build();
}
private String getStageChannelOption(String type, String option) {
return buildIdentifier(type, STAGE, OPTION, option);
}
private StateDescription getStageDescription(String channelID, Locale locale) {
private StateDescriptionFragment getStageDescription(String channelID, Locale locale) {
if (channelID.contains(STAGE.toLowerCase())) {
List<StateOption> stateOptions = new ArrayList<>();
if (channelID.contains(LIGHT)) {
@ -369,11 +371,12 @@ public class DsChannelTypeProvider extends BaseDsI18n implements ChannelTypeProv
locale)));
}
}
return new StateDescription(null, null, null, null, false, stateOptions);
return StateDescriptionFragmentBuilder.create().withReadOnly(false).withOptions(stateOptions).build();
}
if (channelID.contains(TEMPERATURE_CONTROLLED)) {
return new StateDescription(new BigDecimal(0), new BigDecimal(50), new BigDecimal(0.1), "%.1f °C", false,
null);
return StateDescriptionFragmentBuilder.create().withMinimum(new BigDecimal(0))
.withMaximum(new BigDecimal(50)).withStep(new BigDecimal(0.1)).withPattern("%.1f °C")
.withReadOnly(false).build();
}
return null;
}
@ -485,14 +488,14 @@ public class DsChannelTypeProvider extends BaseDsI18n implements ChannelTypeProv
return ChannelTypeBuilder.state(channelTypeUID, getLabelText(channelID, locale), NUMBER)
.withDescription(getDescText(channelID, locale)).withCategory(getSensorCategory(sensorType))
.withTags(getSimpleTags(channelID, locale))
.withStateDescription(getSensorStateDescription(sensorType)).build();
.withStateDescriptionFragment(getSensorStateDescription(sensorType)).build();
} catch (IllegalArgumentException e) {
if (SUPPORTED_OUTPUT_CHANNEL_TYPES.contains(channelID)) {
return ChannelTypeBuilder
.state(channelTypeUID, getLabelText(channelID, locale), getItemType(channelID))
.withDescription(getDescText(channelID, locale)).withCategory(getCategory(channelID))
.withTags(getTags(channelID, locale))
.withStateDescription(getStageDescription(channelID, locale)).build();
.withStateDescriptionFragment(getStageDescription(channelID, locale)).build();
}
MeteringTypeEnum meteringType = getMeteringType(channelID);
if (meteringType != null) {
@ -501,11 +504,14 @@ public class DsChannelTypeProvider extends BaseDsI18n implements ChannelTypeProv
if (MeteringTypeEnum.CONSUMPTION.equals(meteringType)) {
pattern = "%d W";
}
return ChannelTypeBuilder.state(channelTypeUID, getLabelText(channelID, locale), NUMBER)
.withDescription(getDescText(channelID, locale)).withCategory(CATEGORY_ENERGY)
.withTags(
new HashSet<>(Arrays.asList(getLabelText(channelID, locale), getText(DS, locale))))
.withStateDescription(new StateDescription(null, null, null, pattern, true, null)).build();
.withStateDescriptionFragment(StateDescriptionFragmentBuilder.create().withPattern(pattern)
.withReadOnly(true).build())
.build();
}
try {
DeviceBinarayInputEnum binarayInputType = DeviceBinarayInputEnum
@ -514,8 +520,9 @@ public class DsChannelTypeProvider extends BaseDsI18n implements ChannelTypeProv
.state(channelTypeUID, getLabelText(channelID, locale), getItemType(channelID))
.withDescription(getDescText(channelID, locale))
.withCategory(getBinaryInputCategory(binarayInputType))
.withTags(getSimpleTags(channelTypeUID.getId(), locale))
.withStateDescription(new StateDescription(null, null, null, null, true, null)).build();
.withTags(getSimpleTags(channelTypeUID.getId(), locale)).withStateDescriptionFragment(
StateDescriptionFragmentBuilder.create().withReadOnly(true).build())
.build();
} catch (IllegalArgumentException e1) {
// ignore
}

View File

@ -41,7 +41,7 @@ import org.openhab.core.thing.type.ChannelTypeBuilder;
import org.openhab.core.thing.type.ChannelTypeUID;
import org.openhab.core.types.Command;
import org.openhab.core.types.RefreshType;
import org.openhab.core.types.StateDescription;
import org.openhab.core.types.StateDescriptionFragmentBuilder;
import org.openhab.core.types.StateOption;
import org.openhab.core.types.UnDefType;
import org.slf4j.Logger;
@ -187,7 +187,8 @@ public class HarmonyDeviceHandler extends BaseThingHandler {
ChannelType channelType = ChannelTypeBuilder.state(channelTypeUID, "Send Button Press", "String")
.withDescription("Send a button press to device " + getThing().getLabel())
.withStateDescription(new StateDescription(null, null, null, null, false, states)).build();
.withStateDescriptionFragment(StateDescriptionFragmentBuilder.create().withOptions(states).build())
.build();
factory.addChannelType(channelType);

View File

@ -50,7 +50,7 @@ import org.openhab.core.thing.type.ChannelTypeBuilder;
import org.openhab.core.thing.type.ChannelTypeUID;
import org.openhab.core.types.Command;
import org.openhab.core.types.RefreshType;
import org.openhab.core.types.StateDescription;
import org.openhab.core.types.StateDescriptionFragmentBuilder;
import org.openhab.core.types.StateOption;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -382,7 +382,9 @@ public class HarmonyHubHandler extends BaseBridgeHandler implements HarmonyClien
ChannelType channelType = ChannelTypeBuilder.state(channelTypeUID, "Current Activity", "String")
.withDescription("Current activity for " + getThing().getLabel())
.withStateDescription(new StateDescription(null, null, null, "%s", false, states)).build();
.withStateDescriptionFragment(StateDescriptionFragmentBuilder.create().withPattern("%s")
.withReadOnly(false).withOptions(states).build())
.build();
factory.addChannelType(channelType);

View File

@ -189,7 +189,7 @@ public class KM200ThingHandler extends BaseThingHandler {
.withDescription(description) //
.withCategory(checkCategory(unitOfMeasure, category, state.isReadOnly())) //
.withTags(checkTags(unitOfMeasure, state.isReadOnly())) //
.withStateDescription(state.toStateDescription()) //
.withStateDescriptionFragment(state) //
.withConfigDescriptionURI(configDescriptionUriChannel).build();
} catch (URISyntaxException ex) {
logger.warn("Can't create ConfigDescription URI '{}', ConfigDescription for channels not avilable!",

View File

@ -38,7 +38,7 @@ import org.openhab.core.thing.type.ChannelDefinitionBuilder;
import org.openhab.core.thing.type.ChannelType;
import org.openhab.core.thing.type.ChannelTypeBuilder;
import org.openhab.core.thing.type.ChannelTypeUID;
import org.openhab.core.types.StateDescription;
import org.openhab.core.types.StateDescriptionFragment;
/**
* An {@link AbstractComponent}s derived class consists of one or multiple channels.
@ -208,11 +208,10 @@ public class CChannel {
type = ChannelTypeBuilder.trigger(channelTypeUID, label)
.withConfigDescriptionURI(URI.create(MqttBindingConstants.CONFIG_HA_CHANNEL)).build();
} else {
StateDescription description = valueState.createStateDescription(command_topic == null).build()
.toStateDescription();
StateDescriptionFragment description = valueState.createStateDescription(command_topic == null).build();
type = ChannelTypeBuilder.state(channelTypeUID, label, channelState.getItemType())
.withConfigDescriptionURI(URI.create(MqttBindingConstants.CONFIG_HA_CHANNEL))
.withStateDescription(description).build();
.withStateDescriptionFragment(description).build();
}
Configuration configuration = new Configuration();

View File

@ -142,8 +142,8 @@ public class Property implements AttributeChanged {
if (attributes.retained) {
return ChannelTypeBuilder.state(channelTypeUID, attributes.name, channelState.getItemType())
.withConfigDescriptionURI(URI.create(MqttBindingConstants.CONFIG_HOMIE_CHANNEL))
.withStateDescription(channelState.getCache().createStateDescription(!attributes.settable).build()
.toStateDescription())
.withStateDescriptionFragment(
channelState.getCache().createStateDescription(!attributes.settable).build())
.build();
} else {
// Non-retained and settable property -> State channel

View File

@ -113,7 +113,7 @@ class NUTDynamicChannelFactory {
final ChannelTypeUID channelTypeUID = new ChannelTypeUID(BINDING_ID, channel.getUID().getId() + "Type");
final String label = channel.getLabel();
final ChannelType channelType = ChannelTypeBuilder.state(channelTypeUID, label == null ? "" : label, itemType)
.withStateDescription(sdb.withReadOnly(Boolean.TRUE).build().toStateDescription())
.withStateDescriptionFragment(sdb.withReadOnly(Boolean.TRUE).build())
.withConfigDescriptionURI(NUTBindingConstants.DYNAMIC_CHANNEL_CONFIG_QUANTITY_TYPE).build();
channelTypeProvider.addChannelType(channelType);
return channelTypeUID;

View File

@ -389,7 +389,7 @@ public class SensiboSkyHandler extends SensiboBaseThingHandler implements Channe
stateDescription = stateDescription.withPattern(pattern);
}
final StateChannelTypeBuilder builder = ChannelTypeBuilder.state(channelTypeUID, label, itemType)
.withStateDescription(stateDescription.build().toStateDescription());
.withStateDescriptionFragment(stateDescription.build());
if (tag != null) {
builder.withTag(tag);
}

View File

@ -83,16 +83,14 @@ public class SmartMeterChannelTypeProvider implements ChannelTypeProvider, Meter
stateDescriptionBuilder = ChannelTypeBuilder
.state(new ChannelTypeUID(SmartMeterBindingConstants.BINDING_ID, obisChannelId), obis,
CoreItemFactory.NUMBER + ":" + dimension)
.withStateDescription(StateDescriptionFragmentBuilder.create().withReadOnly(true)
.withPattern("%.2f %unit%").build().toStateDescription())
.withStateDescriptionFragment(StateDescriptionFragmentBuilder.create().withReadOnly(true)
.withPattern("%.2f %unit%").build())
.withConfigDescriptionURI(URI.create(SmartMeterBindingConstants.CHANNEL_TYPE_METERREADER_OBIS));
} else {
stateDescriptionBuilder = ChannelTypeBuilder
.state(new ChannelTypeUID(SmartMeterBindingConstants.BINDING_ID, obisChannelId), obis,
CoreItemFactory.STRING)
.withStateDescription(
StateDescriptionFragmentBuilder.create().withReadOnly(true).build().toStateDescription());
.withStateDescriptionFragment(StateDescriptionFragmentBuilder.create().withReadOnly(true).build());
}
return stateDescriptionBuilder.build();
}

View File

@ -469,8 +469,8 @@ public class ApiPageParser extends AbstractSimpleMarkupHandler {
}
ChannelType ct = ChannelTypeBuilder
.state(new ChannelTypeUID(TACmiBindingConstants.BINDING_ID, shortName), shortName, itemType)
.withDescription("Auto-created for " + shortName)
.withStateDescription(sdb.build().toStateDescription()).build();
.withDescription("Auto-created for " + shortName).withStateDescriptionFragment(sdb.build())
.build();
channelTypeProvider.addChannelType(ct);
channelBuilder.withType(ct.getUID());
} else {

View File

@ -31,7 +31,8 @@ import org.openhab.core.thing.type.ChannelType;
import org.openhab.core.thing.type.ChannelTypeBuilder;
import org.openhab.core.thing.type.ChannelTypeProvider;
import org.openhab.core.thing.type.ChannelTypeUID;
import org.openhab.core.types.StateDescription;
import org.openhab.core.types.StateDescriptionFragment;
import org.openhab.core.types.StateDescriptionFragmentBuilder;
import org.openhab.core.types.StateOption;
/**
@ -64,12 +65,12 @@ public class ChannelsTypeProviderAvailableInputs implements ChannelTypeProvider,
return channelTypeUID;
}
private void createChannelType(StateDescription state) {
private void createChannelType(StateDescriptionFragment state) {
channelType = ChannelTypeBuilder.state(channelTypeUID, "Input source", "String")
.withDescription("Select the input source of the AVR").withStateDescription(state).build();
.withDescription("Select the input source of the AVR").withStateDescriptionFragment(state).build();
}
private StateDescription getDefaultStateDescription() {
private StateDescriptionFragment getDefaultStateDescription() {
List<StateOption> options = new ArrayList<>();
options.add(new StateOption(INPUT_NET_RADIO, "Net Radio"));
options.add(new StateOption(INPUT_PC, "PC"));
@ -110,8 +111,8 @@ public class ChannelsTypeProviderAvailableInputs implements ChannelTypeProvider,
options.add(new StateOption(INPUT_PANDORA, "Pandora"));
options.add(new StateOption(INPUT_NAPSTER, "Napster"));
options.add(new StateOption(INPUT_SPOTIFY, "Spotify"));
StateDescription state = new StateDescription(null, null, null, "%s", false, options);
return state;
return StateDescriptionFragmentBuilder.create().withPattern("%s").withReadOnly(false).withOptions(options)
.build();
}
public void changeAvailableInputs(Map<String, String> availableInputs) {
@ -119,7 +120,8 @@ public class ChannelsTypeProviderAvailableInputs implements ChannelTypeProvider,
for (Entry<String, String> inputEntry : availableInputs.entrySet()) {
options.add(new StateOption(inputEntry.getKey(), inputEntry.getValue()));
}
createChannelType(new StateDescription(null, null, null, "%s", false, options));
createChannelType(StateDescriptionFragmentBuilder.create().withPattern("%s").withReadOnly(false)
.withOptions(options).build());
}
@NonNullByDefault({})

View File

@ -31,7 +31,8 @@ import org.openhab.core.thing.type.ChannelType;
import org.openhab.core.thing.type.ChannelTypeBuilder;
import org.openhab.core.thing.type.ChannelTypeProvider;
import org.openhab.core.thing.type.ChannelTypeUID;
import org.openhab.core.types.StateDescription;
import org.openhab.core.types.StateDescriptionFragment;
import org.openhab.core.types.StateDescriptionFragmentBuilder;
import org.openhab.core.types.StateOption;
/**
@ -64,25 +65,24 @@ public class ChannelsTypeProviderPreset implements ChannelTypeProvider, ThingHan
return channelTypeUID;
}
private StateDescription getDefaultStateDescription() {
private StateDescriptionFragment getDefaultStateDescription() {
List<StateOption> options = IntStream.rangeClosed(1, 40)
.mapToObj(i -> new StateOption(Integer.toString(i), "Item_" + i)).collect(toList());
StateDescription state = new StateDescription(null, null, null, "%s", false, options);
return state;
return StateDescriptionFragmentBuilder.create().withPattern("%s").withReadOnly(false).withOptions(options)
.build();
}
public void changePresetNames(List<PresetInfoState.Preset> presets) {
List<StateOption> options = presets.stream()
.map(preset -> new StateOption(String.valueOf(preset.getValue()), preset.getName())).collect(toList());
StateDescription state = new StateDescription(null, null, null, "%s", false, options);
createChannelType(state);
createChannelType(StateDescriptionFragmentBuilder.create().withPattern("%s").withReadOnly(false)
.withOptions(options).build());
}
private void createChannelType(StateDescription state) {
private void createChannelType(StateDescriptionFragment state) {
channelType = ChannelTypeBuilder.state(channelTypeUID, "Preset", "Number")
.withDescription("Select a saved channel by its preset number").withStateDescription(state).build();
.withDescription("Select a saved channel by its preset number").withStateDescriptionFragment(state)
.build();
}
@NonNullByDefault({})
@ -96,8 +96,7 @@ public class ChannelsTypeProviderPreset implements ChannelTypeProvider, ThingHan
channelTypeUID = new ChannelTypeUID(BINDING_ID,
CHANNEL_PLAYBACK_PRESET_TYPE_NAMED + handler.getThing().getUID().getId());
StateDescription state = getDefaultStateDescription();
createChannelType(state);
createChannelType(getDefaultStateDescription());
}
@Override