Search in sources :

Example 16 with ItemChannelLink

use of org.openhab.core.thing.link.ItemChannelLink in project openhab-core by openhab.

the class ChannelLinkNotifierOSGiTest method addItemsAndLinks.

private void addItemsAndLinks(Thing thing, String itemSuffix) {
    forEachThingChannelUID(thing, channelUID -> {
        String itemName = getItemName(thing, channelUID, itemSuffix);
        managedItemProvider.add(new NumberItem(itemName));
        managedItemChannelLinkProvider.add(new ItemChannelLink(itemName, channelUID));
    });
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) ItemChannelLink(org.openhab.core.thing.link.ItemChannelLink)

Example 17 with ItemChannelLink

use of org.openhab.core.thing.link.ItemChannelLink 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 18 with ItemChannelLink

use of org.openhab.core.thing.link.ItemChannelLink in project openhab-core by openhab.

the class ChannelCommandDescriptionProviderOSGiTest method wrongItemCommandDescription.

@Test
public void wrongItemCommandDescription() throws ItemNotFoundException {
    ThingRegistry thingRegistry = getService(ThingRegistry.class);
    assertNotNull(thingRegistry);
    ManagedThingProvider managedThingProvider = getService(ManagedThingProvider.class);
    assertNotNull(managedThingProvider);
    registerService(new MalfunctioningDynamicCommandDescriptionProvider(), DynamicCommandDescriptionProvider.class.getName());
    registerService(new TestDynamicCommandDescriptionProvider(), DynamicCommandDescriptionProvider.class.getName());
    Thing thing = thingRegistry.createThingOfType(new ThingTypeUID("hue:lamp"), new ThingUID("hue:lamp:lamp1"), null, "test thing", new Configuration());
    assertNotNull(thing);
    if (thing == null) {
        throw new IllegalStateException("thing is null");
    }
    managedThingProvider.add(thing);
    ItemChannelLink link = new ItemChannelLink("TestItem7_2", getChannel(thing, "7_2").getUID());
    linkRegistry.add(link);
    // 
    final Collection<Item> items = itemRegistry.getItems();
    assertFalse(items.isEmpty());
    Item item = itemRegistry.getItem("TestItem7_2");
    CommandDescription command = item.getCommandDescription();
    assertNotNull(command);
    List<CommandOption> opts = command.getCommandOptions();
    assertNotNull(opts);
    assertEquals(1, opts.size());
    final CommandOption opt2 = opts.get(0);
    assertEquals("NEW COMMAND", opt2.getCommand());
    assertEquals("My new command.", opt2.getLabel());
}
Also used : Configuration(org.openhab.core.config.core.Configuration) CommandOption(org.openhab.core.types.CommandOption) CommandDescription(org.openhab.core.types.CommandDescription) ItemChannelLink(org.openhab.core.thing.link.ItemChannelLink) ThingRegistry(org.openhab.core.thing.ThingRegistry) NumberItem(org.openhab.core.library.items.NumberItem) Item(org.openhab.core.items.Item) DynamicCommandDescriptionProvider(org.openhab.core.thing.type.DynamicCommandDescriptionProvider) BaseDynamicCommandDescriptionProvider(org.openhab.core.thing.binding.BaseDynamicCommandDescriptionProvider) ThingUID(org.openhab.core.thing.ThingUID) ManagedThingProvider(org.openhab.core.thing.ManagedThingProvider) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) Thing(org.openhab.core.thing.Thing) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 19 with ItemChannelLink

use of org.openhab.core.thing.link.ItemChannelLink in project openhab-core by openhab.

the class ChannelStateDescriptionProviderOSGiTest method presentItemStateDescription.

/**
 * Assert that item's state description is present.
 */
@Test
public void presentItemStateDescription() throws ItemNotFoundException {
    ThingRegistry thingRegistry = getService(ThingRegistry.class);
    assertNotNull(thingRegistry);
    ManagedThingProvider managedThingProvider = getService(ManagedThingProvider.class);
    assertNotNull(managedThingProvider);
    registerService(new TestDynamicStateDescriptionProvider(), DynamicStateDescriptionProvider.class.getName());
    Thing thing = thingRegistry.createThingOfType(new ThingTypeUID("hue:lamp"), new ThingUID("hue:lamp:lamp1"), null, "test thing", new Configuration());
    assertNotNull(thing);
    if (thing == null) {
        throw new IllegalStateException("thing is null");
    }
    managedThingProvider.add(thing);
    ItemChannelLink link = new ItemChannelLink("TestItem", getChannel(thing, "1").getUID());
    linkRegistry.add(link);
    link = new ItemChannelLink("TestItem2", getChannel(thing, "2").getUID());
    linkRegistry.add(link);
    link = new ItemChannelLink("TestItem3", getChannel(thing, "3").getUID());
    linkRegistry.add(link);
    link = new ItemChannelLink("TestItem4", getChannel(thing, "4").getUID());
    linkRegistry.add(link);
    link = new ItemChannelLink("TestItem5", getChannel(thing, "5").getUID());
    linkRegistry.add(link);
    link = new ItemChannelLink("TestItem6", getChannel(thing, "6").getUID());
    linkRegistry.add(link);
    link = new ItemChannelLink("TestItem7_1", getChannel(thing, "7_1").getUID());
    linkRegistry.add(link);
    link = new ItemChannelLink("TestItem7_2", getChannel(thing, "7_2").getUID());
    linkRegistry.add(link);
    // 
    final Collection<Item> items = itemRegistry.getItems();
    assertFalse(items.isEmpty());
    Item item = itemRegistry.getItem("TestItem");
    assertEquals(CoreItemFactory.NUMBER, item.getType());
    StateDescription state = item.getStateDescription();
    assertNotNull(state);
    assertEquals(BigDecimal.ZERO, state.getMinimum());
    assertEquals(BigDecimal.valueOf(100), state.getMaximum());
    assertEquals(BigDecimal.TEN, state.getStep());
    assertEquals("%d Peek", state.getPattern());
    assertTrue(state.isReadOnly());
    List<StateOption> opts = state.getOptions();
    assertEquals(1, opts.size());
    final StateOption opt = opts.get(0);
    assertEquals("SOUND", opt.getValue());
    assertEquals("My great sound.", opt.getLabel());
    item = itemRegistry.getItem("TestItem2");
    assertEquals(CoreItemFactory.NUMBER, item.getType());
    state = item.getStateDescription();
    assertNotNull(state);
    assertEquals(BigDecimal.ZERO, state.getMinimum());
    assertEquals(BigDecimal.valueOf(256), state.getMaximum());
    assertEquals(BigDecimal.valueOf(8), state.getStep());
    assertEquals("%.0f", state.getPattern());
    assertFalse(state.isReadOnly());
    opts = state.getOptions();
    assertEquals(0, opts.size());
    item = itemRegistry.getItem("TestItem3");
    assertEquals(CoreItemFactory.STRING, item.getType());
    state = item.getStateDescription();
    assertNotNull(state);
    assertNull(state.getMinimum());
    assertNull(state.getMaximum());
    assertNull(state.getStep());
    assertEquals("%s", state.getPattern());
    assertFalse(state.isReadOnly());
    opts = state.getOptions();
    assertEquals(0, opts.size());
    item = itemRegistry.getItem("TestItem4");
    assertEquals(CoreItemFactory.COLOR, item.getType());
    state = item.getStateDescription();
    assertNull(state);
    item = itemRegistry.getItem("TestItem5");
    assertEquals(CoreItemFactory.DIMMER, item.getType());
    state = item.getStateDescription();
    assertNull(state);
    item = itemRegistry.getItem("TestItem6");
    assertEquals(CoreItemFactory.SWITCH, item.getType());
    state = item.getStateDescription();
    assertNull(state);
    item = itemRegistry.getItem("TestItem7_1");
    assertEquals(CoreItemFactory.NUMBER, item.getType());
    state = item.getStateDescription();
    assertNotNull(state);
    assertEquals(BigDecimal.valueOf(10), state.getMinimum());
    assertEquals(BigDecimal.valueOf(100), state.getMaximum());
    assertEquals(BigDecimal.valueOf(5), state.getStep());
    assertEquals("VALUE %d", state.getPattern());
    assertFalse(state.isReadOnly());
    opts = state.getOptions();
    assertNotNull(opts);
    assertEquals(2, opts.size());
    final StateOption opt0 = opts.get(0);
    assertNotNull(opt0);
    assertEquals(opt0.getValue(), "value0");
    assertEquals(opt0.getLabel(), "label0");
    final StateOption opt1 = opts.get(1);
    assertNotNull(opt1);
    assertEquals(opt1.getValue(), "value1");
    assertEquals(opt1.getLabel(), "label1");
    item = itemRegistry.getItem("TestItem7_2");
    assertEquals(CoreItemFactory.NUMBER, item.getType());
    state = item.getStateDescription();
    assertNotNull(state);
    assertEquals(BigDecimal.valueOf(1), state.getMinimum());
    assertEquals(BigDecimal.valueOf(101), state.getMaximum());
    assertEquals(BigDecimal.valueOf(20), state.getStep());
    assertEquals("NEW %d Peek", state.getPattern());
    assertFalse(state.isReadOnly());
    opts = state.getOptions();
    assertNotNull(opts);
    assertEquals(1, opts.size());
    final StateOption opt2 = opts.get(0);
    assertEquals("NEW SOUND", opt2.getValue());
    assertEquals("My great new sound.", opt2.getLabel());
}
Also used : Configuration(org.openhab.core.config.core.Configuration) ItemChannelLink(org.openhab.core.thing.link.ItemChannelLink) BaseDynamicStateDescriptionProvider(org.openhab.core.thing.binding.BaseDynamicStateDescriptionProvider) DynamicStateDescriptionProvider(org.openhab.core.thing.type.DynamicStateDescriptionProvider) StateOption(org.openhab.core.types.StateOption) ThingRegistry(org.openhab.core.thing.ThingRegistry) NumberItem(org.openhab.core.library.items.NumberItem) DimmerItem(org.openhab.core.library.items.DimmerItem) SwitchItem(org.openhab.core.library.items.SwitchItem) ColorItem(org.openhab.core.library.items.ColorItem) Item(org.openhab.core.items.Item) StringItem(org.openhab.core.library.items.StringItem) ThingUID(org.openhab.core.thing.ThingUID) ManagedThingProvider(org.openhab.core.thing.ManagedThingProvider) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) Thing(org.openhab.core.thing.Thing) StateDescription(org.openhab.core.types.StateDescription) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 20 with ItemChannelLink

use of org.openhab.core.thing.link.ItemChannelLink in project openhab-core by openhab.

the class GenericItemChannelLinkProvider method createItemChannelLink.

private void createItemChannelLink(String context, String itemName, String channelUID, Configuration configuration) throws BindingConfigParseException {
    ChannelUID channelUIDObject = null;
    try {
        channelUIDObject = new ChannelUID(channelUID);
    } catch (IllegalArgumentException e) {
        throw new BindingConfigParseException(e.getMessage());
    }
    ItemChannelLink itemChannelLink = new ItemChannelLink(itemName, channelUIDObject, configuration);
    Set<String> itemNames = contextMap.get(context);
    if (itemNames == null) {
        itemNames = new HashSet<>();
        contextMap.put(context, itemNames);
    }
    itemNames.add(itemName);
    if (previousItemNames != null) {
        previousItemNames.remove(itemName);
    }
    Set<ItemChannelLink> links = itemChannelLinkMap.get(itemName);
    if (links == null) {
        itemChannelLinkMap.put(itemName, links = new HashSet<>());
    }
    if (!links.contains(itemChannelLink)) {
        links.add(itemChannelLink);
        notifyListenersAboutAddedElement(itemChannelLink);
    } else {
        notifyListenersAboutUpdatedElement(itemChannelLink, itemChannelLink);
    }
}
Also used : ChannelUID(org.openhab.core.thing.ChannelUID) BindingConfigParseException(org.openhab.core.model.item.BindingConfigParseException) ItemChannelLink(org.openhab.core.thing.link.ItemChannelLink) HashSet(java.util.HashSet)

Aggregations

ItemChannelLink (org.openhab.core.thing.link.ItemChannelLink)49 Test (org.junit.jupiter.api.Test)30 Thing (org.openhab.core.thing.Thing)14 Item (org.openhab.core.items.Item)13 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)13 ChannelUID (org.openhab.core.thing.ChannelUID)11 StringItem (org.openhab.core.library.items.StringItem)8 ThingTypeUID (org.openhab.core.thing.ThingTypeUID)8 Configuration (org.openhab.core.config.core.Configuration)6 NumberItem (org.openhab.core.library.items.NumberItem)6 ThingUID (org.openhab.core.thing.ThingUID)6 Nullable (org.eclipse.jdt.annotation.Nullable)5 Channel (org.openhab.core.thing.Channel)5 ThingRegistry (org.openhab.core.thing.ThingRegistry)5 ThingHandler (org.openhab.core.thing.binding.ThingHandler)5 ThingHandlerCallback (org.openhab.core.thing.binding.ThingHandlerCallback)5 ManagedItemChannelLinkProvider (org.openhab.core.thing.link.ManagedItemChannelLinkProvider)5 ArrayList (java.util.ArrayList)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 ManagedThingProvider (org.openhab.core.thing.ManagedThingProvider)4