Search in sources :

Example 1 with ChannelBuilder

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;
}
Also used : Configuration(org.eclipse.smarthome.config.core.Configuration) ConfigDescription(org.eclipse.smarthome.config.core.ConfigDescription) ConfigDescriptionParameter(org.eclipse.smarthome.config.core.ConfigDescriptionParameter) ChannelBuilder(org.eclipse.smarthome.core.thing.binding.builder.ChannelBuilder) URI(java.net.URI)

Example 2 with 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();
}
Also used : ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) ChannelBuilder(org.eclipse.smarthome.core.thing.binding.builder.ChannelBuilder) ChannelType(org.eclipse.smarthome.core.thing.type.ChannelType)

Example 3 with ChannelBuilder

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"));
}
Also used : Channel(org.eclipse.smarthome.core.thing.Channel) ThingHandlerCallback(org.eclipse.smarthome.core.thing.binding.ThingHandlerCallback) AtomicReference(java.util.concurrent.atomic.AtomicReference) BaseThingHandler(org.eclipse.smarthome.core.thing.binding.BaseThingHandler) ThingHandler(org.eclipse.smarthome.core.thing.binding.ThingHandler) BaseThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory) ThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory) BaseThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) NonNull(org.eclipse.jdt.annotation.NonNull) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) ChannelBuilder(org.eclipse.smarthome.core.thing.binding.builder.ChannelBuilder) Thing(org.eclipse.smarthome.core.thing.Thing) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Aggregations

ChannelBuilder (org.eclipse.smarthome.core.thing.binding.builder.ChannelBuilder)3 ChannelUID (org.eclipse.smarthome.core.thing.ChannelUID)2 URI (java.net.URI)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 NonNull (org.eclipse.jdt.annotation.NonNull)1 ConfigDescription (org.eclipse.smarthome.config.core.ConfigDescription)1 ConfigDescriptionParameter (org.eclipse.smarthome.config.core.ConfigDescriptionParameter)1 Configuration (org.eclipse.smarthome.config.core.Configuration)1 Channel (org.eclipse.smarthome.core.thing.Channel)1 Thing (org.eclipse.smarthome.core.thing.Thing)1 ThingTypeUID (org.eclipse.smarthome.core.thing.ThingTypeUID)1 BaseThingHandler (org.eclipse.smarthome.core.thing.binding.BaseThingHandler)1 BaseThingHandlerFactory (org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory)1 ThingHandler (org.eclipse.smarthome.core.thing.binding.ThingHandler)1 ThingHandlerCallback (org.eclipse.smarthome.core.thing.binding.ThingHandlerCallback)1 ThingHandlerFactory (org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory)1 ChannelType (org.eclipse.smarthome.core.thing.type.ChannelType)1 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)1 Test (org.junit.Test)1