Search in sources :

Example 11 with ChannelType

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

the class ThingTypeResource method convertToChannelDefinitionDTOs.

private List<ChannelDefinitionDTO> convertToChannelDefinitionDTOs(List<ChannelDefinition> channelDefinitions, Locale locale) {
    List<ChannelDefinitionDTO> channelDefinitionDTOs = new ArrayList<>();
    for (ChannelDefinition channelDefinition : channelDefinitions) {
        ChannelType channelType = channelTypeRegistry.getChannelType(channelDefinition.getChannelTypeUID(), locale);
        if (channelType == null) {
            logger.warn("Cannot find channel type: {}", channelDefinition.getChannelTypeUID());
        } else {
            // Default to the channelDefinition label to override the
            // channelType
            String label = channelDefinition.getLabel();
            if (label == null) {
                label = channelType.getLabel();
            }
            // Default to the channelDefinition description to override the
            // channelType
            String description = channelDefinition.getDescription();
            if (description == null) {
                description = channelType.getDescription();
            }
            ChannelDefinitionDTO channelDefinitionDTO = new ChannelDefinitionDTO(channelDefinition.getId(), channelDefinition.getChannelTypeUID().toString(), label, description, channelType.getTags(), channelType.getCategory(), channelType.getState(), channelType.isAdvanced(), channelDefinition.getProperties());
            channelDefinitionDTOs.add(channelDefinitionDTO);
        }
    }
    return channelDefinitionDTOs;
}
Also used : ChannelDefinitionDTO(org.openhab.core.thing.dto.ChannelDefinitionDTO) ChannelDefinition(org.openhab.core.thing.type.ChannelDefinition) ArrayList(java.util.ArrayList) ChannelType(org.openhab.core.thing.type.ChannelType)

Example 12 with ChannelType

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

the class GenericThingProviderTest3 method assertThatThingsAreUpdatedOnceTheXMLfilesHaveBeenProcessed.

@Test
public void assertThatThingsAreUpdatedOnceTheXMLfilesHaveBeenProcessed() {
    assertThat(thingRegistry.getAll().size(), is(1));
    Thing thing1 = thingRegistry.getAll().iterator().next();
    assertThat(thing1.getUID().toString(), is("dumb:DUMB:boo"));
    assertThat(thing1.getChannels().size(), is(1));
    assertThat(thing1.getChannel("manual"), is(notNullValue()));
    assertThat(thing1.getLabel(), is("Test Label"));
    assertThat(thing1.getLocation(), is("Test Location"));
    assertThat(thing1.getConfiguration().getProperties().get("testConf"), is("foo"));
    // now become smart again...
    dumbThingHandlerFactory.setDumb(false);
    ChannelType channelType1 = ChannelTypeBuilder.state(new ChannelTypeUID(DumbThingHandlerFactory.BINDING_ID, "channel1"), "Channel 1", CoreItemFactory.STRING).build();
    ChannelTypeProvider channelTypeProvider = mock(ChannelTypeProvider.class);
    when(channelTypeProvider.getChannelType(any(), nullable(Locale.class))).thenAnswer(invocation -> {
        if ("channel1".equals(channelType1.getUID().getId())) {
            return channelType1;
        }
        return null;
    });
    registerService(channelTypeProvider);
    // ensure thing type was considered and manual and predefined values are there.
    waitForAssert(() -> {
        Thing thing2 = thingRegistry.getAll().iterator().next();
        assertThat(thing2.getLabel(), is("Test Label"));
        assertThat(thing2.getLocation(), is("Test Location"));
        assertThat(thing2.getChannels().size(), is(2));
        assertThat(thing2.getChannel("manual"), is(notNullValue()));
        assertThat(thing2.getChannel("channel1"), is(notNullValue()));
        // there is a default, so make sure the manually configured one (from the DSL) wins
        assertThat(thing2.getConfiguration().getProperties().get("testConf"), is("foo"));
        // it's not manually configured, but the thing type defines a default, so ensure it's in
        assertThat(thing2.getConfiguration().getProperties().get("testAdditional"), is("hello world"));
    });
}
Also used : Locale(java.util.Locale) ChannelTypeUID(org.openhab.core.thing.type.ChannelTypeUID) ChannelTypeProvider(org.openhab.core.thing.type.ChannelTypeProvider) ChannelType(org.openhab.core.thing.type.ChannelType) Thing(org.openhab.core.thing.Thing) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 13 with ChannelType

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

the class CommunicationManagerOSGiTest method beforeEach.

@BeforeEach
public void beforeEach() {
    safeCaller = getService(SafeCaller.class);
    assertNotNull(safeCaller);
    SystemProfileFactory profileFactory = getService(ProfileTypeProvider.class, SystemProfileFactory.class);
    assertNotNull(profileFactory);
    if (profileFactory == null) {
        throw new IllegalStateException("thing is null");
    }
    manager = new CommunicationManager(autoUpdateManagerMock, channelTypeRegistryMock, profileFactory, iclRegistry, itemRegistryMock, itemStateConverterMock, eventPublisherMock, safeCaller, thingRegistryMock);
    doAnswer(invocation -> {
        switch(((Channel) invocation.getArguments()[0]).getKind()) {
            case STATE:
                return new ProfileTypeUID("test:state");
            case TRIGGER:
                return new ProfileTypeUID("test:trigger");
        }
        return null;
    }).when(profileAdvisorMock).getSuggestedProfileTypeUID(isA(Channel.class), isA(String.class));
    doAnswer(invocation -> {
        switch(((ProfileTypeUID) invocation.getArguments()[0]).toString()) {
            case "test:state":
                return stateProfileMock;
            case "test:trigger":
                return triggerProfileMock;
        }
        return null;
    }).when(profileFactoryMock).createProfile(isA(ProfileTypeUID.class), isA(ProfileCallback.class), isA(ProfileContext.class));
    when(profileFactoryMock.getSupportedProfileTypeUIDs()).thenReturn(Stream.of(new ProfileTypeUID("test:state"), new ProfileTypeUID("test:trigger")).collect(Collectors.toList()));
    manager.addProfileFactory(profileFactoryMock);
    manager.addProfileAdvisor(profileAdvisorMock);
    iclRegistry.addProvider(new ItemChannelLinkProvider() {

        @Override
        public void addProviderChangeListener(ProviderChangeListener<ItemChannelLink> listener) {
        }

        @Override
        public void removeProviderChangeListener(ProviderChangeListener<ItemChannelLink> listener) {
        }

        @Override
        public Collection<ItemChannelLink> getAll() {
            return List.of(LINK_1_S1, LINK_1_S2, LINK_2_S2, LINK_1_T1, LINK_1_T2, LINK_2_T2, LINK_3_S3, LINK_4_S4);
        }
    });
    when(itemRegistryMock.get(eq(ITEM_NAME_1))).thenReturn(ITEM_1);
    when(itemRegistryMock.get(eq(ITEM_NAME_2))).thenReturn(ITEM_2);
    when(itemRegistryMock.get(eq(ITEM_NAME_3))).thenReturn(ITEM_3);
    when(itemRegistryMock.get(eq(ITEM_NAME_4))).thenReturn(ITEM_4);
    ChannelType channelType4 = mock(ChannelType.class);
    when(channelType4.getItemType()).thenReturn("Number:Temperature");
    when(channelTypeRegistryMock.getChannelType(CHANNEL_TYPE_UID_4)).thenReturn(channelType4);
    THING.setHandler(thingHandlerMock);
    when(thingRegistryMock.get(eq(THING_UID))).thenReturn(THING);
    manager.addItemFactory(new CoreItemFactory());
    UnitProvider unitProvider = mock(UnitProvider.class);
    when(unitProvider.getUnit(Temperature.class)).thenReturn(SIUnits.CELSIUS);
    ITEM_3.setUnitProvider(unitProvider);
    ITEM_4.setUnitProvider(unitProvider);
}
Also used : ProfileCallback(org.openhab.core.thing.profiles.ProfileCallback) Channel(org.openhab.core.thing.Channel) ItemChannelLink(org.openhab.core.thing.link.ItemChannelLink) ProfileTypeUID(org.openhab.core.thing.profiles.ProfileTypeUID) CoreItemFactory(org.openhab.core.library.CoreItemFactory) SafeCaller(org.openhab.core.common.SafeCaller) SystemProfileFactory(org.openhab.core.thing.internal.profiles.SystemProfileFactory) ProfileContext(org.openhab.core.thing.profiles.ProfileContext) UnitProvider(org.openhab.core.i18n.UnitProvider) Collection(java.util.Collection) ChannelType(org.openhab.core.thing.type.ChannelType) ItemChannelLinkProvider(org.openhab.core.thing.link.ItemChannelLinkProvider) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 14 with ChannelType

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

the class ChannelCommandDescriptionProviderOSGiTest method beforeEach.

@BeforeEach
public void beforeEach() throws Exception {
    Mockito.when(componentContextMock.getBundleContext()).thenReturn(bundleContext);
    registerVolatileStorageService();
    itemRegistry = getService(ItemRegistry.class);
    assertNotNull(itemRegistry);
    final TestThingHandlerFactory thingHandlerFactory = new TestThingHandlerFactory();
    thingHandlerFactory.activate(componentContextMock);
    registerService(thingHandlerFactory, ThingHandlerFactory.class.getName());
    final StateDescriptionFragment stateDescriptionFragment = StateDescriptionFragmentBuilder.create().withMinimum(BigDecimal.ZERO).withMaximum(BigDecimal.valueOf(100)).withStep(BigDecimal.TEN).withPattern("%d Peek").withReadOnly(true).withOption(new StateOption("SOUND", "My great sound.")).build();
    final CommandDescription command = CommandDescriptionBuilder.create().withCommandOption(new CommandOption("COMMAND", "My command.")).build();
    final ChannelType channelType1 = ChannelTypeBuilder.state(new ChannelTypeUID("hue:state-as-command"), " ", CoreItemFactory.NUMBER).withStateDescriptionFragment(stateDescriptionFragment).build();
    final ChannelType channelType2 = ChannelTypeBuilder.state(new ChannelTypeUID("hue:static"), " ", CoreItemFactory.STRING).withTag("Light").withCommandDescription(command).build();
    final ChannelType channelType3 = ChannelTypeBuilder.state(CHANNEL_TYPE_UID, " ", CoreItemFactory.STRING).withTag("Light").build();
    List<ChannelType> channelTypes = new ArrayList<>();
    channelTypes.add(channelType1);
    channelTypes.add(channelType2);
    channelTypes.add(channelType3);
    registerService(new ChannelTypeProvider() {

        @Override
        public Collection<ChannelType> getChannelTypes(@Nullable Locale locale) {
            return channelTypes;
        }

        @Override
        @Nullable
        public ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) {
            for (final ChannelType channelType : channelTypes) {
                if (channelType.getUID().equals(channelTypeUID)) {
                    return channelType;
                }
            }
            return null;
        }
    });
    testBundle = SyntheticBundleInstaller.install(bundleContext, TEST_BUNDLE_NAME);
    assertThat(testBundle, is(notNullValue()));
    thingStatusInfoI18nLocalizationService = getService(ThingStatusInfoI18nLocalizationService.class);
    assertThat(thingStatusInfoI18nLocalizationService, is(notNullValue()));
    thingStatusInfoI18nLocalizationService.setBundleResolver(new BundleResolverImpl());
    List<ChannelDefinition> channelDefinitions = new ArrayList<>();
    channelDefinitions.add(new ChannelDefinitionBuilder("1", channelType1.getUID()).build());
    channelDefinitions.add(new ChannelDefinitionBuilder("7_1", channelType2.getUID()).build());
    channelDefinitions.add(new ChannelDefinitionBuilder("7_2", channelType3.getUID()).build());
    registerService(new SimpleThingTypeProvider(Set.of(ThingTypeBuilder.instance(new ThingTypeUID("hue:lamp"), "label").withChannelDefinitions(channelDefinitions).build())));
    List<Item> items = new ArrayList<>();
    items.add(new NumberItem("TestItem1"));
    items.add(new NumberItem("TestItem7_1"));
    items.add(new NumberItem("TestItem7_2"));
    registerService(new TestItemProvider(items));
    linkRegistry = getService(ItemChannelLinkRegistry.class);
}
Also used : Locale(java.util.Locale) ChannelDefinitionBuilder(org.openhab.core.thing.type.ChannelDefinitionBuilder) CommandOption(org.openhab.core.types.CommandOption) ArrayList(java.util.ArrayList) ItemRegistry(org.openhab.core.items.ItemRegistry) NumberItem(org.openhab.core.library.items.NumberItem) Item(org.openhab.core.items.Item) ChannelTypeUID(org.openhab.core.thing.type.ChannelTypeUID) ItemChannelLinkRegistry(org.openhab.core.thing.link.ItemChannelLinkRegistry) StateDescriptionFragment(org.openhab.core.types.StateDescriptionFragment) ThingStatusInfoI18nLocalizationService(org.openhab.core.thing.i18n.ThingStatusInfoI18nLocalizationService) CommandDescription(org.openhab.core.types.CommandDescription) ThingHandlerFactory(org.openhab.core.thing.binding.ThingHandlerFactory) BaseThingHandlerFactory(org.openhab.core.thing.binding.BaseThingHandlerFactory) StateOption(org.openhab.core.types.StateOption) NumberItem(org.openhab.core.library.items.NumberItem) ChannelDefinition(org.openhab.core.thing.type.ChannelDefinition) ChannelTypeProvider(org.openhab.core.thing.type.ChannelTypeProvider) Collection(java.util.Collection) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) ChannelType(org.openhab.core.thing.type.ChannelType) Nullable(org.eclipse.jdt.annotation.Nullable) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 15 with ChannelType

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

the class ChannelStateDescriptionProvider method getStateDescription.

@Nullable
private StateDescription getStateDescription(String itemName, @Nullable Locale locale) {
    Set<ChannelUID> boundChannels = itemChannelLinkRegistry.getBoundChannels(itemName);
    if (!boundChannels.isEmpty()) {
        ChannelUID channelUID = boundChannels.iterator().next();
        Channel channel = thingRegistry.getChannel(channelUID);
        if (channel != null) {
            StateDescription stateDescription = null;
            ChannelType channelType = thingTypeRegistry.getChannelType(channel, locale);
            if (channelType != null) {
                stateDescription = channelType.getState();
                String itemType = channelType.getItemType();
                if (itemType != null && (stateDescription == null || stateDescription.getPattern() == null)) {
                    String pattern = null;
                    if (CoreItemFactory.STRING.equalsIgnoreCase(itemType)) {
                        pattern = "%s";
                    } else if (itemType.startsWith(CoreItemFactory.NUMBER)) {
                        pattern = "%.0f";
                    }
                    if (pattern != null) {
                        logger.trace("Provide a default pattern {} for item {}", pattern, itemName);
                        StateDescriptionFragmentBuilder builder = (stateDescription == null) ? StateDescriptionFragmentBuilder.create() : StateDescriptionFragmentBuilder.create(stateDescription);
                        stateDescription = builder.withPattern(pattern).build().toStateDescription();
                    }
                }
            }
            StateDescription dynamicStateDescription = getDynamicStateDescription(channel, stateDescription, locale);
            if (dynamicStateDescription != null) {
                return dynamicStateDescription;
            }
            return stateDescription;
        }
    }
    return null;
}
Also used : ChannelUID(org.openhab.core.thing.ChannelUID) Channel(org.openhab.core.thing.Channel) StateDescriptionFragmentBuilder(org.openhab.core.types.StateDescriptionFragmentBuilder) ChannelType(org.openhab.core.thing.type.ChannelType) StateDescription(org.openhab.core.types.StateDescription) Nullable(org.eclipse.jdt.annotation.Nullable)

Aggregations

ChannelType (org.openhab.core.thing.type.ChannelType)57 ChannelTypeUID (org.openhab.core.thing.type.ChannelTypeUID)27 Channel (org.openhab.core.thing.Channel)21 ArrayList (java.util.ArrayList)20 Nullable (org.eclipse.jdt.annotation.Nullable)17 ChannelUID (org.openhab.core.thing.ChannelUID)16 ChannelDefinition (org.openhab.core.thing.type.ChannelDefinition)14 Test (org.junit.jupiter.api.Test)11 URI (java.net.URI)10 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)10 Thing (org.openhab.core.thing.Thing)9 ThingType (org.openhab.core.thing.type.ThingType)9 StateOption (org.openhab.core.types.StateOption)9 BigDecimal (java.math.BigDecimal)8 Collection (java.util.Collection)8 ChannelDefinitionBuilder (org.openhab.core.thing.type.ChannelDefinitionBuilder)8 ChannelTypeProvider (org.openhab.core.thing.type.ChannelTypeProvider)8 Locale (java.util.Locale)7 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)7 Configuration (org.openhab.core.config.core.Configuration)7