Search in sources :

Example 41 with ItemChannelLink

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

the class ChannelCommandDescriptionProviderOSGiTest method presentItemCommandDescription.

/**
 * Assert that item's command description is present.
 */
@Test
public void presentItemCommandDescription() throws ItemNotFoundException {
    ThingRegistry thingRegistry = getService(ThingRegistry.class);
    assertNotNull(thingRegistry);
    ManagedThingProvider managedThingProvider = getService(ManagedThingProvider.class);
    assertNotNull(managedThingProvider);
    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("TestItem1", getChannel(thing, "1").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("TestItem1");
    assertEquals(CoreItemFactory.NUMBER, item.getType());
    CommandDescription command = item.getCommandDescription();
    assertNotNull(command);
    List<CommandOption> opts = command.getCommandOptions();
    assertNotNull(opts);
    assertEquals(1, opts.size());
    final CommandOption opt0 = opts.get(0);
    assertNotNull(opt0);
    assertEquals("SOUND", opt0.getCommand());
    assertEquals("My great sound.", opt0.getLabel());
    item = itemRegistry.getItem("TestItem7_1");
    assertEquals(CoreItemFactory.NUMBER, item.getType());
    command = item.getCommandDescription();
    assertNotNull(command);
    opts = command.getCommandOptions();
    assertNotNull(opts);
    assertEquals(1, opts.size());
    final CommandOption opt1 = opts.get(0);
    assertNotNull(opt1);
    assertEquals("COMMAND", opt1.getCommand());
    assertEquals("My command.", opt1.getLabel());
    item = itemRegistry.getItem("TestItem7_2");
    assertEquals(CoreItemFactory.NUMBER, item.getType());
    command = item.getCommandDescription();
    assertNotNull(command);
    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 42 with ItemChannelLink

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

the class ChannelStateDescriptionProviderOSGiTest method wrongItemStateDescription.

@Test
public void wrongItemStateDescription() throws ItemNotFoundException {
    ThingRegistry thingRegistry = getService(ThingRegistry.class);
    assertNotNull(thingRegistry);
    ManagedThingProvider managedThingProvider = getService(ManagedThingProvider.class);
    assertNotNull(managedThingProvider);
    registerService(new TestMalfunctioningDynamicStateDescriptionProvider(), DynamicStateDescriptionProvider.class.getName());
    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("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");
    StateDescription 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());
    List<StateOption> 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 43 with ItemChannelLink

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

the class ThingManagerOSGiTest method thingManagerHandlesThingUpdatesCorrectly.

@Test
@SuppressWarnings({ "null", "unchecked" })
public void thingManagerHandlesThingUpdatesCorrectly() {
    String itemName = "name";
    managedThingProvider.add(thing);
    managedItemChannelLinkProvider.add(new ItemChannelLink(itemName, CHANNEL_UID));
    class ThingHandlerState {

        @Nullable
        ThingHandlerCallback callback;
    }
    final ThingHandlerState state = new ThingHandlerState();
    ThingHandler thingHandler = mock(ThingHandler.class);
    doAnswer(new Answer<Void>() {

        @Override
        @Nullable
        public Void answer(InvocationOnMock invocation) throws Throwable {
            state.callback = (ThingHandlerCallback) invocation.getArgument(0);
            return null;
        }
    }).when(thingHandler).setCallback(any(ThingHandlerCallback.class));
    when(thingHandler.getThing()).thenReturn(thing);
    ThingHandlerFactory thingHandlerFactory = mock(ThingHandlerFactory.class);
    when(thingHandlerFactory.supportsThingType(any(ThingTypeUID.class))).thenReturn(true);
    when(thingHandlerFactory.registerHandler(any(Thing.class))).thenReturn(thingHandler);
    registerService(thingHandlerFactory);
    ThingRegistry thingRegistry = getService(ThingRegistry.class);
    RegistryChangeListener<Thing> registryChangeListener = mock(RegistryChangeListener.class);
    try {
        thingRegistry.addRegistryChangeListener(registryChangeListener);
        state.callback.thingUpdated(thing);
        verify(registryChangeListener, times(1)).updated(any(Thing.class), any(Thing.class));
    } finally {
        thingRegistry.removeRegistryChangeListener(registryChangeListener);
    }
}
Also used : ItemChannelLink(org.openhab.core.thing.link.ItemChannelLink) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) ThingHandler(org.openhab.core.thing.binding.ThingHandler) ThingHandlerFactory(org.openhab.core.thing.binding.ThingHandlerFactory) ThingRegistry(org.openhab.core.thing.ThingRegistry) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) Nullable(org.eclipse.jdt.annotation.Nullable) Thing(org.openhab.core.thing.Thing) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 44 with ItemChannelLink

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

the class ThingManagerOSGiTest method thingManagerConsidersUNKNOWNasReadyToUseAndForwardsCommand.

@Test
public void thingManagerConsidersUNKNOWNasReadyToUseAndForwardsCommand() {
    class ThingHandlerState {

        boolean handleCommandCalled;

        @Nullable
        ChannelUID calledChannelUID;

        @Nullable
        Command calledCommand;

        @Nullable
        ThingHandlerCallback callback;
    }
    final ThingHandlerState state = new ThingHandlerState();
    managedThingProvider.add(thing);
    ThingHandler thingHandler = mock(ThingHandler.class);
    doAnswer(new Answer<Void>() {

        @Override
        @Nullable
        public Void answer(InvocationOnMock invocation) throws Throwable {
            state.callback = (ThingHandlerCallback) invocation.getArgument(0);
            return null;
        }
    }).when(thingHandler).setCallback(any(ThingHandlerCallback.class));
    doAnswer(new Answer<Void>() {

        @Override
        @Nullable
        public Void answer(InvocationOnMock invocation) throws Throwable {
            state.handleCommandCalled = true;
            state.calledChannelUID = (ChannelUID) invocation.getArgument(0);
            state.calledCommand = (Command) invocation.getArgument(1);
            return null;
        }
    }).when(thingHandler).handleCommand(any(ChannelUID.class), any(Command.class));
    when(thingHandler.getThing()).thenReturn(thing);
    ThingHandlerFactory thingHandlerFactory = mock(ThingHandlerFactory.class);
    when(thingHandlerFactory.supportsThingType(any(ThingTypeUID.class))).thenReturn(true);
    when(thingHandlerFactory.registerHandler(any(Thing.class))).thenReturn(thingHandler);
    registerService(thingHandlerFactory);
    String itemName = "testItem";
    Item item = new StringItem(itemName);
    itemRegistry.add(item);
    itemChannelLinkRegistry.add(new ItemChannelLink(itemName, new ChannelUID(thing.getUID(), "channel")));
    waitForAssert(() -> assertThat(itemRegistry.get(itemName), is(notNullValue())));
    eventPublisher.post(ItemEventFactory.createCommandEvent(itemName, OnOffType.ON));
    assertThat(state.handleCommandCalled, is(false));
    ThingStatusInfo unknownNone = ThingStatusInfoBuilder.create(ThingStatus.UNKNOWN, ThingStatusDetail.NONE).build();
    state.callback.statusUpdated(thing, unknownNone);
    assertThat(thing.getStatusInfo(), is(unknownNone));
    eventPublisher.post(ItemEventFactory.createCommandEvent(itemName, OnOffType.ON));
    waitForAssert(() -> {
        assertThat(state.handleCommandCalled, is(true));
        assertThat(state.calledChannelUID, is(equalTo(new ChannelUID(thing.getUID(), "channel"))));
        assertThat(state.calledCommand, is(equalTo(OnOffType.ON)));
    });
}
Also used : ItemChannelLink(org.openhab.core.thing.link.ItemChannelLink) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) ThingHandler(org.openhab.core.thing.binding.ThingHandler) ThingStatusInfo(org.openhab.core.thing.ThingStatusInfo) ThingHandlerFactory(org.openhab.core.thing.binding.ThingHandlerFactory) StringItem(org.openhab.core.library.items.StringItem) Item(org.openhab.core.items.Item) StringItem(org.openhab.core.library.items.StringItem) Command(org.openhab.core.types.Command) ChannelUID(org.openhab.core.thing.ChannelUID) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) Nullable(org.eclipse.jdt.annotation.Nullable) Thing(org.openhab.core.thing.Thing) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 45 with ItemChannelLink

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

the class ThingManagerOSGiJavaTest method testThatIsLinkedReturnsTrue.

@Test
public void testThatIsLinkedReturnsTrue() throws Exception {
    AtomicReference<ThingHandlerCallback> thc = initializeThingHandlerCallback();
    ManagedItemChannelLinkProvider managedItemChannelLinkProvider = getService(ManagedItemChannelLinkProvider.class);
    assertNotNull(managedItemChannelLinkProvider);
    managedItemChannelLinkProvider.add(new ItemChannelLink("item", CHANNEL_UID));
    assertTrue(thc.get().isChannelLinked(CHANNEL_UID));
}
Also used : ManagedItemChannelLinkProvider(org.openhab.core.thing.link.ManagedItemChannelLinkProvider) ItemChannelLink(org.openhab.core.thing.link.ItemChannelLink) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

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