use of org.eclipse.smarthome.core.thing.binding.builder.ChannelBuilder in project smarthome by eclipse.
the class ThingFactoryHelper method createChannelBuilder.
static ChannelBuilder createChannelBuilder(ChannelUID channelUID, ChannelType channelType, ConfigDescriptionRegistry configDescriptionRegistry) {
ChannelBuilder channelBuilder = //
ChannelBuilder.create(channelUID, channelType.getItemType()).withType(//
channelType.getUID()).withDefaultTags(//
channelType.getTags()).withKind(//
channelType.getKind()).withLabel(channelType.getLabel());
String description = channelType.getDescription();
if (description != null) {
channelBuilder = channelBuilder.withDescription(description);
}
// Initialize channel configuration with default-values
URI channelConfigDescriptionURI = channelType.getConfigDescriptionURI();
if (configDescriptionRegistry != null && channelConfigDescriptionURI != null) {
ConfigDescription cd = configDescriptionRegistry.getConfigDescription(channelConfigDescriptionURI);
if (cd != null) {
Configuration config = new Configuration();
for (ConfigDescriptionParameter param : cd.getParameters()) {
String defaultValue = param.getDefault();
if (defaultValue != null) {
Object value = getDefaultValueAsCorrectType(param.getType(), defaultValue);
if (value != null) {
config.put(param.getName(), value);
}
}
}
channelBuilder = channelBuilder.withConfiguration(config);
}
}
return channelBuilder;
}
use of org.eclipse.smarthome.core.thing.binding.builder.ChannelBuilder in project smarthome by eclipse.
the class ThingFactoryHelper method createChannel.
private static Channel createChannel(ChannelDefinition channelDefinition, ThingUID thingUID, String groupId, ConfigDescriptionRegistry configDescriptionRegistry) {
ChannelType type = withChannelTypeRegistry(channelTypeRegistry -> {
return (channelTypeRegistry != null) ? channelTypeRegistry.getChannelType(channelDefinition.getChannelTypeUID()) : null;
});
if (type == null) {
logger.warn("Could not create channel '{}' for thing type '{}', because channel type '{}' could not be found.", channelDefinition.getId(), thingUID, channelDefinition.getChannelTypeUID());
return null;
}
ChannelUID channelUID = new ChannelUID(thingUID, groupId, channelDefinition.getId());
ChannelBuilder channelBuilder = createChannelBuilder(channelUID, type, configDescriptionRegistry);
// If we want to override the label, add it...
if (channelDefinition.getLabel() != null) {
channelBuilder = channelBuilder.withLabel(channelDefinition.getLabel());
}
// If we want to override the description, add it...
if (channelDefinition.getDescription() != null) {
channelBuilder = channelBuilder.withDescription(channelDefinition.getDescription());
}
channelBuilder = channelBuilder.withProperties(channelDefinition.getProperties());
return channelBuilder.build();
}
use of org.eclipse.smarthome.core.thing.binding.builder.ChannelBuilder in project smarthome by eclipse.
the class ThingManagerOSGiJavaTest method testCreateChannelBuilder.
@Test
public void testCreateChannelBuilder() throws Exception {
registerThingTypeProvider();
registerChannelTypeProvider();
AtomicReference<ThingHandlerCallback> thc = new AtomicReference<>();
ThingHandlerFactory thingHandlerFactory = new BaseThingHandlerFactory() {
@Override
public boolean supportsThingType(@NonNull ThingTypeUID thingTypeUID) {
return true;
}
@Override
@Nullable
protected ThingHandler createHandler(@NonNull Thing thing) {
ThingHandler mockHandler = mock(ThingHandler.class);
doAnswer(a -> {
thc.set((ThingHandlerCallback) a.getArguments()[0]);
return null;
}).when(mockHandler).setCallback(any(ThingHandlerCallback.class));
when(mockHandler.getThing()).thenReturn(THING);
return mockHandler;
}
};
registerService(thingHandlerFactory, ThingHandlerFactory.class.getName());
new Thread((Runnable) () -> managedThingProvider.add(THING)).start();
waitForAssert(() -> {
assertNotNull(thc.get());
});
ChannelBuilder channelBuilder = thc.get().createChannelBuilder(new ChannelUID(THING_UID, "test"), CHANNEL_TYPE_UID);
Channel channel = channelBuilder.build();
assertThat(channel.getLabel(), is("Test Label"));
assertThat(channel.getDescription(), is("Test Description"));
assertThat(channel.getAcceptedItemType(), is("Switch"));
assertThat(channel.getDefaultTags().size(), is(1));
assertThat(channel.getDefaultTags().iterator().next(), is("Test Tag"));
}
Aggregations