Search in sources :

Example 6 with ChannelDefinition

use of org.openhab.core.thing.type.ChannelDefinition in project openhab-core by openhab.

the class ThingFactoryHelper method createChannels.

/**
 * Create {@link Channel} instances for the given Thing.
 *
 * @param thingType the type of the Thing (must not be null)
 * @param thingUID the Thing's UID (must not be null)
 * @param configDescriptionRegistry {@link ConfigDescriptionRegistry} that will be used to initialize the
 *            {@link Channel}s with their corresponding default values, if given.
 * @return a list of {@link Channel}s
 */
public static List<Channel> createChannels(ThingType thingType, ThingUID thingUID, ConfigDescriptionRegistry configDescriptionRegistry) {
    List<Channel> channels = new ArrayList<>();
    List<ChannelDefinition> channelDefinitions = thingType.getChannelDefinitions();
    for (ChannelDefinition channelDefinition : channelDefinitions) {
        Channel channel = createChannel(channelDefinition, thingUID, null, configDescriptionRegistry);
        if (channel != null) {
            channels.add(channel);
        }
    }
    List<ChannelGroupDefinition> channelGroupDefinitions = thingType.getChannelGroupDefinitions();
    withChannelGroupTypeRegistry(channelGroupTypeRegistry -> {
        for (ChannelGroupDefinition channelGroupDefinition : channelGroupDefinitions) {
            ChannelGroupType channelGroupType = null;
            if (channelGroupTypeRegistry != null) {
                channelGroupType = channelGroupTypeRegistry.getChannelGroupType(channelGroupDefinition.getTypeUID());
            }
            if (channelGroupType != null) {
                List<ChannelDefinition> channelGroupChannelDefinitions = channelGroupType.getChannelDefinitions();
                for (ChannelDefinition channelDefinition : channelGroupChannelDefinitions) {
                    Channel channel = createChannel(channelDefinition, thingUID, channelGroupDefinition.getId(), configDescriptionRegistry);
                    if (channel != null) {
                        channels.add(channel);
                    }
                }
            } else {
                logger.warn("Could not create channels for channel group '{}' for thing type '{}', because channel group type '{}' could not be found.", channelGroupDefinition.getId(), thingUID, channelGroupDefinition.getTypeUID());
            }
        }
        return null;
    });
    return channels;
}
Also used : ChannelDefinition(org.openhab.core.thing.type.ChannelDefinition) Channel(org.openhab.core.thing.Channel) ArrayList(java.util.ArrayList) ChannelGroupType(org.openhab.core.thing.type.ChannelGroupType) ChannelGroupDefinition(org.openhab.core.thing.type.ChannelGroupDefinition)

Example 7 with ChannelDefinition

use of org.openhab.core.thing.type.ChannelDefinition in project openhab-core by openhab.

the class ChannelGroupTypeI18nLocalizationService method createLocalizedChannelGroupType.

public ChannelGroupType createLocalizedChannelGroupType(Bundle bundle, ChannelGroupType channelGroupType, @Nullable Locale locale) {
    ChannelGroupTypeUID channelGroupTypeUID = channelGroupType.getUID();
    String defaultLabel = channelGroupType.getLabel();
    String label = thingTypeI18nUtil.getChannelGroupLabel(bundle, channelGroupTypeUID, defaultLabel, locale);
    String description = thingTypeI18nUtil.getChannelGroupDescription(bundle, channelGroupTypeUID, channelGroupType.getDescription(), locale);
    List<ChannelDefinition> localizedChannelDefinitions = channelI18nUtil.createLocalizedChannelDefinitions(bundle, channelGroupType.getChannelDefinitions(), channelDefinition -> thingTypeI18nUtil.getChannelLabel(bundle, channelGroupTypeUID, channelDefinition, channelDefinition.getLabel(), locale), channelDefinition -> thingTypeI18nUtil.getChannelDescription(bundle, channelGroupTypeUID, channelDefinition, channelDefinition.getDescription(), locale), locale);
    ChannelGroupTypeBuilder builder = ChannelGroupTypeBuilder.instance(channelGroupTypeUID, label == null ? defaultLabel : label).withChannelDefinitions(localizedChannelDefinitions);
    if (description != null) {
        builder.withDescription(description);
    }
    String category = channelGroupType.getCategory();
    if (category != null) {
        builder.withCategory(category);
    }
    return builder.build();
}
Also used : ChannelDefinition(org.openhab.core.thing.type.ChannelDefinition) ChannelGroupTypeUID(org.openhab.core.thing.type.ChannelGroupTypeUID) ChannelGroupTypeBuilder(org.openhab.core.thing.type.ChannelGroupTypeBuilder)

Example 8 with ChannelDefinition

use of org.openhab.core.thing.type.ChannelDefinition in project openhab-core by openhab.

the class SystemChannelsInChannelGroupsTest method thingTypesWithSystemChannelsInChannelsGoupsShouldHavePorperChannelDefinitions.

@Test
public void thingTypesWithSystemChannelsInChannelsGoupsShouldHavePorperChannelDefinitions() throws Exception {
    try (final AutoCloseable unused = loadedTestBundle()) {
        List<ThingType> thingTypes = thingTypeProvider.getThingTypes(null).stream().filter(it -> "wireless-router".equals(it.getUID().getId())).collect(Collectors.toList());
        assertThat(thingTypes.size(), is(1));
        List<ChannelGroupType> channelGroupTypes = channelGroupTypeRegistry.getChannelGroupTypes();
        ChannelGroupType channelGroup = channelGroupTypes.stream().filter(it -> it.getUID().equals(new ChannelGroupTypeUID("SystemChannelsInChannelGroups:channelGroup"))).findFirst().get();
        assertThat(channelGroup, is(notNullValue()));
        List<ChannelDefinition> channelDefs = channelGroup.getChannelDefinitions();
        List<ChannelDefinition> myChannel = channelDefs.stream().filter(it -> "test".equals(it.getId()) && "system:my-channel".equals(it.getChannelTypeUID().getAsString())).collect(Collectors.toList());
        List<ChannelDefinition> sigStr = channelDefs.stream().filter(it -> "sigstr".equals(it.getId()) && "system:signal-strength".equals(it.getChannelTypeUID().getAsString())).collect(Collectors.toList());
        List<ChannelDefinition> lowBat = channelDefs.stream().filter(it -> "lowbat".equals(it.getId()) && "system:low-battery".equals(it.getChannelTypeUID().getAsString())).collect(Collectors.toList());
        assertThat(myChannel.size(), is(1));
        assertThat(sigStr.size(), is(1));
        assertThat(lowBat.size(), is(1));
    }
}
Also used : CoreMatchers(org.hamcrest.CoreMatchers) BeforeEach(org.junit.jupiter.api.BeforeEach) ChannelGroupType(org.openhab.core.thing.type.ChannelGroupType) ThingType(org.openhab.core.thing.type.ThingType) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) ChannelDefinition(org.openhab.core.thing.type.ChannelDefinition) StuffAddition(org.openhab.core.thing.xml.test.LoadedTestBundle.StuffAddition) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) ThingTypeProvider(org.openhab.core.thing.binding.ThingTypeProvider) ChannelTypeRegistry(org.openhab.core.thing.type.ChannelTypeRegistry) List(java.util.List) ChannelGroupTypeUID(org.openhab.core.thing.type.ChannelGroupTypeUID) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest) ChannelGroupTypeRegistry(org.openhab.core.thing.type.ChannelGroupTypeRegistry) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ChannelDefinition(org.openhab.core.thing.type.ChannelDefinition) ChannelGroupType(org.openhab.core.thing.type.ChannelGroupType) ChannelGroupTypeUID(org.openhab.core.thing.type.ChannelGroupTypeUID) ThingType(org.openhab.core.thing.type.ThingType) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 9 with ChannelDefinition

use of org.openhab.core.thing.type.ChannelDefinition in project openhab-core by openhab.

the class SystemWideChannelTypesTest method systemChannelsShouldTranslateProperly.

@Test
public void systemChannelsShouldTranslateProperly() throws Exception {
    try (final AutoCloseable unused = loadedSystemChannelsBundle()) {
        Collection<ThingType> thingTypes = thingTypeProvider.getThingTypes(Locale.GERMAN);
        ThingType wirelessRouterType = thingTypes.stream().filter(it -> "SystemChannels:wireless-router".equals(it.getUID().getAsString())).findFirst().get();
        assertNotNull(wirelessRouterType);
        List<ChannelDefinition> channelDefs = wirelessRouterType.getChannelDefinitions();
        assertEquals(3, channelDefs.size());
        ChannelDefinition myChannel = channelDefs.stream().filter(it -> "test".equals(it.getId()) && "system:my-channel".equals(it.getChannelTypeUID().getAsString())).findFirst().get();
        assertNotNull(myChannel);
        ChannelDefinition sigStr = channelDefs.stream().filter(it -> "sigstr".equals(it.getId()) && "system:signal-strength".equals(it.getChannelTypeUID().getAsString())).findFirst().get();
        assertNotNull(sigStr);
        ChannelDefinition lowBat = channelDefs.stream().filter(it -> "lowbat".equals(it.getId()) && "system:low-battery".equals(it.getChannelTypeUID().getAsString())).findFirst().get();
        assertNotNull(lowBat);
        ChannelType lowBatType = systemChannelTypeProvider.getChannelType(lowBat.getChannelTypeUID(), Locale.GERMAN);
        ChannelType myChannelChannelType = channelTypeRegistry.getChannelType(myChannel.getChannelTypeUID(), Locale.GERMAN);
        assertNotNull(myChannelChannelType);
        assertEquals("Mein String My Channel", myChannelChannelType.getLabel());
        assertEquals("Wetterinformation mit My Channel Type Beschreibung", myChannelChannelType.getDescription());
        assertEquals("Mein String My Channel", myChannel.getLabel());
        assertEquals("Wetterinformation mit My Channel Type Beschreibung", myChannel.getDescription());
        assertEquals("Meine spezial Signalstärke", sigStr.getLabel());
        assertEquals("Meine spezial Beschreibung für Signalstärke", sigStr.getDescription());
        assertEquals("Niedriger Batteriestatus", lowBatType.getLabel());
        assertNull(lowBatType.getDescription());
    }
}
Also used : ChannelDefinition(org.openhab.core.thing.type.ChannelDefinition) ThingType(org.openhab.core.thing.type.ThingType) ChannelType(org.openhab.core.thing.type.ChannelType) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 10 with ChannelDefinition

use of org.openhab.core.thing.type.ChannelDefinition in project openhab-core by openhab.

the class ThingTypeI18nTest method channelsInGroupTypeShouldBeLocalized.

@Test
public void channelsInGroupTypeShouldBeLocalized() throws Exception {
    try (final AutoCloseable unused = loadedTestBundle()) {
        Collection<ThingType> thingTypes = thingTypeProvider.getThingTypes(Locale.GERMAN);
        ThingType weatherGroupType = thingTypes.stream().filter(it -> "acmeweather:weather-with-group".equals(it.toString())).findFirst().get();
        assertNotNull(weatherGroupType);
        assertEquals(2, weatherGroupType.getChannelGroupDefinitions().size());
        ChannelGroupDefinition forecastTodayChannelGroupDefinition = weatherGroupType.getChannelGroupDefinitions().stream().filter(it -> "forecastToday".equals(it.getId())).findFirst().get();
        assertNotNull(forecastTodayChannelGroupDefinition);
        ChannelGroupType forecastTodayChannelGroupType = channelGroupTypeRegistry.getChannelGroupType(forecastTodayChannelGroupDefinition.getTypeUID(), Locale.GERMAN);
        assertNotNull(forecastTodayChannelGroupType);
        assertEquals(3, forecastTodayChannelGroupType.getChannelDefinitions().size());
        ChannelDefinition temperatureChannelDefinition = forecastTodayChannelGroupType.getChannelDefinitions().stream().filter(it -> "temperature".equals(it.getId())).findFirst().get();
        assertNotNull(temperatureChannelDefinition);
        assertEquals("Temperatur", temperatureChannelDefinition.getLabel());
        assertEquals("Temperatur in Grad Celsius (Metrisch) oder Fahrenheit (Imperial).", temperatureChannelDefinition.getDescription());
        ChannelDefinition minTemperatureChannelDefinition = forecastTodayChannelGroupType.getChannelDefinitions().stream().filter(it -> "minTemperature".equals(it.getId())).findFirst().get();
        assertNotNull(minTemperatureChannelDefinition);
        assertEquals("Min. Temperatur", minTemperatureChannelDefinition.getLabel());
        assertEquals("Minimale vorhergesagte Temperatur in Grad Celsius (Metrisch) oder Fahrenheit (Imperial).", minTemperatureChannelDefinition.getDescription());
        ChannelDefinition maxTemperatureChannelDefinition = forecastTodayChannelGroupType.getChannelDefinitions().stream().filter(it -> "maxTemperature".equals(it.getId())).findFirst().get();
        assertNotNull(maxTemperatureChannelDefinition);
        assertEquals("Max. Temperatur", maxTemperatureChannelDefinition.getLabel());
        assertEquals("Maximale vorhergesagte Temperatur in Grad Celsius (Metrisch) oder Fahrenheit (Imperial).", maxTemperatureChannelDefinition.getDescription());
    }
}
Also used : ChannelDefinition(org.openhab.core.thing.type.ChannelDefinition) ChannelGroupType(org.openhab.core.thing.type.ChannelGroupType) ThingType(org.openhab.core.thing.type.ThingType) ChannelGroupDefinition(org.openhab.core.thing.type.ChannelGroupDefinition) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Aggregations

ChannelDefinition (org.openhab.core.thing.type.ChannelDefinition)24 ChannelType (org.openhab.core.thing.type.ChannelType)13 ThingType (org.openhab.core.thing.type.ThingType)12 ChannelTypeUID (org.openhab.core.thing.type.ChannelTypeUID)10 ArrayList (java.util.ArrayList)9 Test (org.junit.jupiter.api.Test)9 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)9 ChannelDefinitionBuilder (org.openhab.core.thing.type.ChannelDefinitionBuilder)9 ChannelGroupType (org.openhab.core.thing.type.ChannelGroupType)8 ThingTypeUID (org.openhab.core.thing.ThingTypeUID)7 ChannelGroupDefinition (org.openhab.core.thing.type.ChannelGroupDefinition)7 BeforeEach (org.junit.jupiter.api.BeforeEach)5 Collection (java.util.Collection)4 ChannelGroupTypeUID (org.openhab.core.thing.type.ChannelGroupTypeUID)4 URI (java.net.URI)3 List (java.util.List)3 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)3 CoreMatchers (org.hamcrest.CoreMatchers)3 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)3 Channel (org.openhab.core.thing.Channel)3