Search in sources :

Example 46 with Thing

use of org.eclipse.smarthome.core.thing.Thing in project smarthome by eclipse.

the class ModelRestrictedFirmwareUpdateServiceOSGiTest method createAndRegisterThing.

/**
 * Creates a thing, adds it to the thing registry, and registers a {@link FirmwareUpdateHandler} for the thing.
 */
private Thing createAndRegisterThing(String thingUID, String modelId, String firmwareVersion) {
    Thing thing = ThingBuilder.create(thingType.getUID(), thingUID).build();
    thing.setProperty(PROPERTY_MODEL_ID, modelId);
    thing.setProperty(PROPERTY_FIRMWARE_VERSION, firmwareVersion);
    managedThingProvider.add(thing);
    registerService(createFirmwareUpdateHandler(thing));
    return thing;
}
Also used : Thing(org.eclipse.smarthome.core.thing.Thing)

Example 47 with Thing

use of org.eclipse.smarthome.core.thing.Thing in project smarthome by eclipse.

the class ChannelStateDescriptionProviderOSGiTest method presentItemStateDescription.

/**
 * Assert that item's state description is present.
 */
@Test
public void presentItemStateDescription() throws ItemNotFoundException {
    ThingRegistry thingRegistry = getService(ThingRegistry.class);
    ManagedThingProvider managedThingProvider = getService(ManagedThingProvider.class);
    Thing thing = thingRegistry.createThingOfType(new ThingTypeUID("hue:lamp"), new ThingUID("hue:lamp:lamp1"), null, "test thing", new Configuration());
    assertNotNull(thing);
    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();
    assertEquals(false, items.isEmpty());
    Item item = itemRegistry.getItem("TestItem");
    assertEquals("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());
    assertEquals(true, 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("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());
    assertEquals(false, state.isReadOnly());
    opts = state.getOptions();
    assertEquals(0, opts.size());
    item = itemRegistry.getItem("TestItem3");
    assertEquals("String", item.getType());
    state = item.getStateDescription();
    assertNotNull(state);
    assertNull(state.getMinimum());
    assertNull(state.getMaximum());
    assertNull(state.getStep());
    assertEquals("%s", state.getPattern());
    assertEquals(false, state.isReadOnly());
    opts = state.getOptions();
    assertEquals(0, opts.size());
    item = itemRegistry.getItem("TestItem4");
    assertEquals("Color", item.getType());
    state = item.getStateDescription();
    assertNull(state);
    item = itemRegistry.getItem("TestItem5");
    assertEquals("Dimmer", item.getType());
    state = item.getStateDescription();
    assertNull(state);
    item = itemRegistry.getItem("TestItem6");
    assertEquals("Switch", item.getType());
    state = item.getStateDescription();
    assertNull(state);
    item = itemRegistry.getItem("TestItem7_1");
    assertEquals("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());
    assertEquals(false, 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("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());
    assertEquals(true, state.isReadOnly());
    opts = state.getOptions();
    assertNotNull(opts);
    assertEquals(1, opts.size());
    final StateOption opt2 = opts.get(0);
    assertEquals("SOUND", opt2.getValue());
    assertEquals("My great sound.", opt2.getLabel());
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) ColorItem(org.eclipse.smarthome.core.library.items.ColorItem) Item(org.eclipse.smarthome.core.items.Item) StringItem(org.eclipse.smarthome.core.library.items.StringItem) DimmerItem(org.eclipse.smarthome.core.library.items.DimmerItem) Configuration(org.eclipse.smarthome.config.core.Configuration) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) ItemChannelLink(org.eclipse.smarthome.core.thing.link.ItemChannelLink) ManagedThingProvider(org.eclipse.smarthome.core.thing.ManagedThingProvider) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) Thing(org.eclipse.smarthome.core.thing.Thing) StateOption(org.eclipse.smarthome.core.types.StateOption) ThingRegistry(org.eclipse.smarthome.core.thing.ThingRegistry) StateDescription(org.eclipse.smarthome.core.types.StateDescription) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 48 with Thing

use of org.eclipse.smarthome.core.thing.Thing in project smarthome by eclipse.

the class CommunicationManagerTest method testItemStateEvent_typeDowncast.

@Test
public void testItemStateEvent_typeDowncast() {
    Thing thing = ThingBuilder.create(THING_TYPE_UID, THING_UID).withChannels(ChannelBuilder.create(STATE_CHANNEL_UID_2, "Dimmer").withKind(ChannelKind.STATE).build()).build();
    thing.setHandler(mockHandler);
    when(thingRegistry.get(eq(THING_UID))).thenReturn(thing);
    manager.receive(ItemEventFactory.createStateEvent(ITEM_NAME_2, HSBType.fromRGB(128, 128, 128)));
    waitForAssert(() -> {
        ArgumentCaptor<State> stateCaptor = ArgumentCaptor.forClass(State.class);
        verify(stateProfile).onStateUpdateFromItem(stateCaptor.capture());
        State state = stateCaptor.getValue();
        assertNotNull(state);
        assertEquals(PercentType.class, state.getClass());
    });
    verifyNoMoreInteractions(stateProfile);
    verifyNoMoreInteractions(triggerProfile);
}
Also used : State(org.eclipse.smarthome.core.types.State) Thing(org.eclipse.smarthome.core.thing.Thing) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 49 with Thing

use of org.eclipse.smarthome.core.thing.Thing in project smarthome by eclipse.

the class ThingManagerOSGiJavaTest method assertThingStatus.

private void assertThingStatus(Map<String, Object> propsThing, Map<String, Object> propsChannel, ThingStatus status, ThingStatusDetail statusDetail) {
    Configuration configThing = new Configuration(propsThing);
    Configuration configChannel = new Configuration(propsChannel);
    Thing thing = ThingBuilder.create(THING_TYPE_UID, THING_UID).withChannels(// 
    Collections.singletonList(ChannelBuilder.create(CHANNEL_UID, "Switch").withType(CHANNEL_TYPE_UID).withConfiguration(configChannel).build())).withConfiguration(configThing).build();
    managedThingProvider.add(thing);
    waitForAssert(() -> {
        assertEquals(status, thing.getStatus());
        assertEquals(statusDetail, thing.getStatusInfo().getStatusDetail());
    });
    managedThingProvider.remove(thing.getUID());
}
Also used : Configuration(org.eclipse.smarthome.config.core.Configuration) Thing(org.eclipse.smarthome.core.thing.Thing)

Example 50 with Thing

use of org.eclipse.smarthome.core.thing.Thing in project smarthome by eclipse.

the class ThingManagerOSGiJavaTest method testChildHandlerInitialized_modifiedUninitializedThing.

@Test
public void testChildHandlerInitialized_modifiedUninitializedThing() {
    Semaphore childHandlerInitializedSemaphore = new Semaphore(1);
    Semaphore thingUpdatedSemapthore = new Semaphore(1);
    registerThingHandlerFactory(BRIDGE_TYPE_UID, bridge -> new BaseBridgeHandler((Bridge) bridge) {

        @Override
        public void handleCommand(@NonNull ChannelUID channelUID, @NonNull Command command) {
        }

        @Override
        public void initialize() {
            updateStatus(ThingStatus.ONLINE);
        }

        @Override
        public void childHandlerInitialized(ThingHandler childHandler, Thing childThing) {
            try {
                childHandlerInitializedSemaphore.acquire();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    });
    registerThingHandlerFactory(THING_TYPE_UID, thing -> new BaseThingHandler(thing) {

        @Override
        public void handleCommand(@NonNull ChannelUID channelUID, @NonNull Command command) {
        }

        @Override
        public void initialize() {
            if (getBridge() == null) {
                throw new RuntimeException("Fail because of missing bridge");
            }
            updateStatus(ThingStatus.ONLINE);
        }

        @Override
        public void thingUpdated(Thing thing) {
            this.thing = thing;
            try {
                thingUpdatedSemapthore.acquire();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    });
    Bridge bridge = BridgeBuilder.create(BRIDGE_TYPE_UID, BRIDGE_UID).build();
    managedThingProvider.add(bridge);
    waitForAssert(() -> {
        assertEquals(ThingStatus.ONLINE, bridge.getStatus());
    });
    Thing thing = ThingBuilder.create(THING_TYPE_UID, THING_UID).build();
    managedThingProvider.add(thing);
    waitForAssert(() -> {
        assertEquals(ThingStatus.UNINITIALIZED, thing.getStatus());
        assertEquals(ThingStatusDetail.HANDLER_INITIALIZING_ERROR, thing.getStatusInfo().getStatusDetail());
    });
    assertEquals(1, childHandlerInitializedSemaphore.availablePermits());
    thing.setBridgeUID(bridge.getUID());
    managedThingProvider.update(thing);
    waitForAssert(() -> {
        assertEquals(ThingStatus.ONLINE, thing.getStatus());
    });
    // childHandlerInitialized(...) must be called
    waitForAssert(() -> assertEquals(0, childHandlerInitializedSemaphore.availablePermits()));
    // thingUpdated(...) is not called
    assertEquals(1, thingUpdatedSemapthore.availablePermits());
}
Also used : BaseBridgeHandler(org.eclipse.smarthome.core.thing.binding.BaseBridgeHandler) Command(org.eclipse.smarthome.core.types.Command) BaseThingHandler(org.eclipse.smarthome.core.thing.binding.BaseThingHandler) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) BaseThingHandler(org.eclipse.smarthome.core.thing.binding.BaseThingHandler) ThingHandler(org.eclipse.smarthome.core.thing.binding.ThingHandler) Semaphore(java.util.concurrent.Semaphore) Bridge(org.eclipse.smarthome.core.thing.Bridge) Thing(org.eclipse.smarthome.core.thing.Thing) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Aggregations

Thing (org.eclipse.smarthome.core.thing.Thing)98 Test (org.junit.Test)43 ThingUID (org.eclipse.smarthome.core.thing.ThingUID)28 ChannelUID (org.eclipse.smarthome.core.thing.ChannelUID)24 Configuration (org.eclipse.smarthome.config.core.Configuration)19 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)19 JavaTest (org.eclipse.smarthome.test.java.JavaTest)18 ThingHandler (org.eclipse.smarthome.core.thing.binding.ThingHandler)14 ThingTypeUID (org.eclipse.smarthome.core.thing.ThingTypeUID)13 ApiOperation (io.swagger.annotations.ApiOperation)9 ApiResponses (io.swagger.annotations.ApiResponses)9 Item (org.eclipse.smarthome.core.items.Item)9 Path (javax.ws.rs.Path)8 Nullable (org.eclipse.jdt.annotation.Nullable)8 Locale (java.util.Locale)7 RolesAllowed (javax.annotation.security.RolesAllowed)7 Channel (org.eclipse.smarthome.core.thing.Channel)7 ThingHandlerCallback (org.eclipse.smarthome.core.thing.binding.ThingHandlerCallback)7 Command (org.eclipse.smarthome.core.types.Command)7 List (java.util.List)6