use of org.openhab.core.types.StateDescription in project openhab-addons by openhab.
the class HttpThingHandler method createChannel.
/**
* create all necessary information to handle every channel
*
* @param channel a thing channel
*/
private void createChannel(Channel channel) {
ChannelUID channelUID = channel.getUID();
HttpChannelConfig channelConfig = channel.getConfiguration().as(HttpChannelConfig.class);
String stateUrl = concatenateUrlParts(config.baseURL, channelConfig.stateExtension);
String commandUrl = channelConfig.commandExtension == null ? stateUrl : concatenateUrlParts(config.baseURL, channelConfig.commandExtension);
String acceptedItemType = channel.getAcceptedItemType();
if (acceptedItemType == null) {
logger.warn("Cannot determine item-type for channel '{}'", channelUID);
return;
}
ItemValueConverter itemValueConverter;
switch(acceptedItemType) {
case "Color":
itemValueConverter = createItemConverter(ColorItemConverter::new, commandUrl, channelUID, channelConfig);
break;
case "DateTime":
itemValueConverter = createGenericItemConverter(commandUrl, channelUID, channelConfig, DateTimeType::new);
break;
case "Dimmer":
itemValueConverter = createItemConverter(DimmerItemConverter::new, commandUrl, channelUID, channelConfig);
break;
case "Contact":
case "Switch":
itemValueConverter = createItemConverter(FixedValueMappingItemConverter::new, commandUrl, channelUID, channelConfig);
break;
case "Image":
itemValueConverter = new ImageItemConverter(state -> updateState(channelUID, state));
break;
case "Location":
itemValueConverter = createGenericItemConverter(commandUrl, channelUID, channelConfig, PointType::new);
break;
case "Number":
itemValueConverter = createItemConverter(NumberItemConverter::new, commandUrl, channelUID, channelConfig);
break;
case "Player":
itemValueConverter = createItemConverter(PlayerItemConverter::new, commandUrl, channelUID, channelConfig);
break;
case "Rollershutter":
itemValueConverter = createItemConverter(RollershutterItemConverter::new, commandUrl, channelUID, channelConfig);
break;
case "String":
itemValueConverter = createGenericItemConverter(commandUrl, channelUID, channelConfig, StringType::new);
break;
default:
logger.warn("Unsupported item-type '{}'", channel.getAcceptedItemType());
return;
}
channels.put(channelUID, itemValueConverter);
if (channelConfig.mode != HttpChannelMode.WRITEONLY) {
// we need a key consisting of stateContent and URL, only if both are equal, we can use the same cache
String key = channelConfig.stateContent + "$" + stateUrl;
channelUrls.put(channelUID, key);
urlHandlers.computeIfAbsent(key, k -> new RefreshingUrlCache(scheduler, rateLimitedHttpClient, stateUrl, config, channelConfig.stateContent)).addConsumer(itemValueConverter::process);
}
StateDescription stateDescription = StateDescriptionFragmentBuilder.create().withReadOnly(channelConfig.mode == HttpChannelMode.READONLY).build().toStateDescription();
if (stateDescription != null) {
// if the state description is not available, we don't need to add it
httpDynamicStateDescriptionProvider.setDescription(channelUID, stateDescription);
}
}
use of org.openhab.core.types.StateDescription in project openhab-addons by openhab.
the class LxControlTest method testChannel.
void testChannel(LxControl ctrl, String itemType, String namePostFix, BigDecimal min, BigDecimal max, BigDecimal step, String format, Boolean readOnly, List<StateOption> options, Set<String> tags) {
assertNotNull(ctrl);
Channel c = getChannel(getExpectedName(ctrl.getLabel(), ctrl.getRoom().getName(), namePostFix), ctrl);
assertNotNull(c);
assertNotNull(c.getUID());
assertNotNull(c.getDescription());
assertEquals(itemType, c.getAcceptedItemType());
assertEquals(ChannelKind.STATE, c.getKind());
StateDescription d = handler.stateDescriptions.get(c.getUID());
if (readOnly != null || min != null || max != null || step != null || format != null || options != null) {
assertNotNull(d);
assertEquals(min, d.getMinimum());
assertEquals(max, d.getMaximum());
assertEquals(step, d.getStep());
assertEquals(format, d.getPattern());
assertEquals(readOnly, d.isReadOnly());
List<StateOption> opts = d.getOptions();
if (options == null) {
assertTrue(opts.isEmpty());
} else {
assertNotNull(opts);
assertEquals(options.size(), opts.size());
options.forEach(o -> {
String label = o.getLabel();
long num = opts.stream().filter(f -> label != null && label.equals(f.getLabel()) && o.getValue().equals(f.getValue())).collect(Collectors.counting());
assertEquals(1, num);
});
}
} else {
assertNull(d);
}
if (tags != null) {
assertThat(c.getDefaultTags(), hasItems(tags.toArray(new String[0])));
} else {
assertThat(c.getDefaultTags(), empty());
}
}
use of org.openhab.core.types.StateDescription in project openhab-addons by openhab.
the class GenericMQTTThingHandler method initialize.
@Override
public void initialize() {
initializeAvailabilityTopicsFromConfig();
List<ChannelUID> configErrors = new ArrayList<>();
for (Channel channel : thing.getChannels()) {
final ChannelTypeUID channelTypeUID = channel.getChannelTypeUID();
if (channelTypeUID == null) {
logger.warn("Channel {} has no type", channel.getLabel());
continue;
}
final ChannelConfig channelConfig = channel.getConfiguration().as(ChannelConfig.class);
try {
Value value = ValueFactory.createValueState(channelConfig, channelTypeUID.getId());
ChannelState channelState = createChannelState(channelConfig, channel.getUID(), value);
channelStateByChannelUID.put(channel.getUID(), channelState);
StateDescription description = value.createStateDescription(channelConfig.commandTopic.isBlank()).build().toStateDescription();
if (description != null) {
stateDescProvider.setDescription(channel.getUID(), description);
}
} catch (IllegalArgumentException e) {
logger.warn("Configuration error for channel '{}'", channel.getUID(), e);
configErrors.add(channel.getUID());
}
}
// in question to the user.
if (!configErrors.isEmpty()) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Remove and recreate: " + configErrors.stream().map(ChannelUID::getAsString).collect(Collectors.joining(",")));
return;
}
super.initialize();
}
use of org.openhab.core.types.StateDescription in project addons by smarthomej.
the class AbstractDigitalOwDevice method configureChannels.
@Override
public void configureChannels() throws OwException {
Thing thing = callback.getThing();
OwDynamicStateDescriptionProvider dynamicStateDescriptionProvider = callback.getDynamicStateDescriptionProvider();
for (Integer i = 0; i < ioConfig.size(); i++) {
String channelId = ioConfig.get(i).getChannelId();
Channel channel = thing.getChannel(channelId);
if (channel != null) {
Configuration channelConfig = channel.getConfiguration();
try {
if (channelConfig.get(CONFIG_DIGITAL_MODE) != null) {
ioConfig.get(i).setIoMode((String) channelConfig.get(CONFIG_DIGITAL_MODE));
}
if (channelConfig.get(CONFIG_DIGITAL_LOGIC) != null) {
ioConfig.get(i).setIoLogic((String) channelConfig.get(CONFIG_DIGITAL_LOGIC));
}
} catch (IllegalArgumentException e) {
throw new OwException(channelId + " has invalid configuration");
}
if (dynamicStateDescriptionProvider != null) {
StateDescription stateDescription = StateDescriptionFragmentBuilder.create().withReadOnly(ioConfig.get(i).isInput()).build().toStateDescription();
if (stateDescription != null) {
dynamicStateDescriptionProvider.setDescription(ioConfig.get(i).getChannelUID(), stateDescription);
} else {
logger.warn("Failed to create state description in thing {}", thing.getUID());
}
} else {
logger.debug("state description may be inaccurate, state description provider not available in thing {}", thing.getUID());
}
logger.debug("configured {} channel {}: {}", thing.getUID(), i, ioConfig.get(i));
} else {
throw new OwException(channelId + " not found");
}
}
isConfigured = true;
}
use of org.openhab.core.types.StateDescription in project addons by smarthomej.
the class EchoHandler method createMusicProviderStateDescription.
private void createMusicProviderStateDescription(List<JsonMusicProvider> jsonMusicProviders) {
List<StateOption> options = new ArrayList<>();
for (JsonMusicProvider musicProvider : jsonMusicProviders) {
List<String> properties = musicProvider.supportedProperties;
String providerId = musicProvider.id;
String displayName = musicProvider.displayName;
if (properties != null && properties.contains("Alexa.Music.PlaySearchPhrase") && providerId != null && !providerId.isEmpty() && "AVAILABLE".equals(musicProvider.availability) && displayName != null && !displayName.isEmpty()) {
options.add(new StateOption(providerId, displayName));
}
}
ChannelUID channelUID = new ChannelUID(thing.getUID(), CHANNEL_MUSIC_PROVIDER_ID);
StateDescription stateDescription = StateDescriptionFragmentBuilder.create().withOptions(options).build().toStateDescription();
if (stateDescription != null) {
dynamicStateDescriptionProvider.setDescription(channelUID, stateDescription);
}
}
Aggregations