Search in sources :

Example 36 with Item

use of org.eclipse.smarthome.core.items.Item in project smarthome by eclipse.

the class ScriptEngineOSGiTest method setup.

@Before
public void setup() {
    registerVolatileStorageService();
    EventPublisher eventPublisher = event -> {
    };
    registerService(eventPublisher);
    itemRegistry = getService(ItemRegistry.class);
    assertNotNull(itemRegistry);
    itemProvider = new ItemProvider() {

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

        @Override
        public Collection<Item> getAll() {
            return Lists.newArrayList(new SwitchItem(ITEM_NAME), createNumberItem(NUMBER_ITEM_TEMPERATURE, Temperature.class), createNumberItem(NUMBER_ITEM_LENGTH, Length.class), new NumberItem(NUMBER_ITEM_DECIMAL));
        }

        @Override
        public void removeProviderChangeListener(ProviderChangeListener<Item> listener) {
        }
    };
    registerService(itemProvider);
    ScriptServiceUtil scriptServiceUtil = getService(ScriptServiceUtil.class);
    assertNotNull(scriptServiceUtil);
    scriptEngine = ScriptServiceUtil.getScriptEngine();
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) ProviderChangeListener(org.eclipse.smarthome.core.common.registry.ProviderChangeListener) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) Collection(java.util.Collection) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) ItemProvider(org.eclipse.smarthome.core.items.ItemProvider) Test(org.junit.Test) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) Item(org.eclipse.smarthome.core.items.Item) ScriptServiceUtil(org.eclipse.smarthome.model.script.ScriptServiceUtil) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) Lists(com.google.common.collect.Lists) Temperature(javax.measure.quantity.Temperature) Length(javax.measure.quantity.Length) QuantityType(org.eclipse.smarthome.core.library.types.QuantityType) After(org.junit.After) Assert(org.junit.Assert) State(org.eclipse.smarthome.core.types.State) NonNull(org.eclipse.jdt.annotation.NonNull) Before(org.junit.Before) ItemProvider(org.eclipse.smarthome.core.items.ItemProvider) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) Item(org.eclipse.smarthome.core.items.Item) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) ScriptServiceUtil(org.eclipse.smarthome.model.script.ScriptServiceUtil) Collection(java.util.Collection) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Before(org.junit.Before)

Example 37 with Item

use of org.eclipse.smarthome.core.items.Item in project smarthome by eclipse.

the class ScriptEngineOSGiTest method testLessThanWithItemState.

@SuppressWarnings("null")
@Test
public void testLessThanWithItemState() throws ScriptExecutionException, ScriptParsingException {
    Item numberItem = itemRegistry.get(NUMBER_ITEM_TEMPERATURE);
    ((NumberItem) numberItem).setState(new QuantityType<>("20 °F"));
    assertTrue(runScript("NumberA.state < 20 [°C]"));
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) Item(org.eclipse.smarthome.core.items.Item) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 38 with Item

use of org.eclipse.smarthome.core.items.Item in project smarthome by eclipse.

the class BusEvent method sendCommand.

/**
 * Sends a command for a specified item to the event bus.
 *
 * @param itemName the name of the item to send the command to
 * @param commandString the command to send
 */
public static Object sendCommand(String itemName, String commandString) {
    ItemRegistry registry = ScriptServiceUtil.getItemRegistry();
    EventPublisher publisher = ScriptServiceUtil.getEventPublisher();
    if (publisher != null && registry != null) {
        try {
            Item item = registry.getItem(itemName);
            Command command = TypeParser.parseCommand(item.getAcceptedCommandTypes(), commandString);
            if (command != null) {
                publisher.post(ItemEventFactory.createCommandEvent(itemName, command));
            } else {
                LoggerFactory.getLogger(BusEvent.class).warn("Cannot convert '{}' to a command type which item '{}' accepts: {}.", commandString, itemName, getAcceptedCommandNames(item));
            }
        } catch (ItemNotFoundException e) {
            LoggerFactory.getLogger(BusEvent.class).warn("Item '{}' does not exist.", itemName);
        }
    }
    return null;
}
Also used : Item(org.eclipse.smarthome.core.items.Item) GroupItem(org.eclipse.smarthome.core.items.GroupItem) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) Command(org.eclipse.smarthome.core.types.Command) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 39 with Item

use of org.eclipse.smarthome.core.items.Item in project smarthome by eclipse.

the class NtpOSGiTest method getItemState.

private State getItemState(String acceptedItemType) {
    final Item testItem = waitForAssert(() -> {
        Item tmp;
        try {
            tmp = itemRegistry.getItem(TEST_ITEM_NAME);
        } catch (ItemNotFoundException e) {
            tmp = null;
        }
        assertNotNull(tmp);
        return tmp;
    });
    return waitForAssert(() -> {
        final State testItemState = testItem.getState();
        if (acceptedItemType.equals(ACCEPTED_ITEM_TYPE_STRING)) {
            assertThat(testItemState, is(instanceOf(StringType.class)));
        } else if (acceptedItemType.equals(ACCEPTED_ITEM_TYPE_DATE_TIME)) {
            assertThat(testItemState, is(instanceOf(DateTimeType.class)));
        }
        return testItemState;
    }, 3 * DFL_TIMEOUT, 2 * DFL_SLEEP_TIME);
}
Also used : DateTimeItem(org.eclipse.smarthome.core.library.items.DateTimeItem) GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) StringItem(org.eclipse.smarthome.core.library.items.StringItem) DateTimeType(org.eclipse.smarthome.core.library.types.DateTimeType) State(org.eclipse.smarthome.core.types.State) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 40 with Item

use of org.eclipse.smarthome.core.items.Item in project smarthome by eclipse.

the class SayCommandTest method testSayCommand.

@Test
public void testSayCommand() throws IOException {
    String[] methodParameters = new String[2];
    methodParameters[0] = SUBCMD_SAY;
    if (defaultTTSService != null) {
        Dictionary<String, Object> config = new Hashtable<String, Object>();
        config.put(CONFIG_DEFAULT_TTS, defaultTTSService);
        ConfigurationAdmin configAdmin = super.getService(ConfigurationAdmin.class);
        String pid = "org.eclipse.smarthome.voice";
        Configuration configuration = configAdmin.getConfiguration(pid);
        configuration.update(config);
    }
    if (TTSServiceMockShouldBeRegistered) {
        registerService(ttsService);
    }
    if (shouldItemsBePassed) {
        VolatileStorageService volatileStorageService = new VolatileStorageService();
        registerService(volatileStorageService);
        ManagedThingProvider managedThingProvider = getService(ThingProvider.class, ManagedThingProvider.class);
        assertNotNull(managedThingProvider);
        ItemRegistry itemRegistry = getService(ItemRegistry.class);
        assertNotNull(itemRegistry);
        Item item = new StringItem("itemName");
        if (shouldItemsBeRegistered) {
            itemRegistry.add(item);
        }
        methodParameters[1] = "%" + item.getName() + "%";
        if (shouldMultipleItemsBeRegistered) {
            Item item1 = new StringItem("itemName1");
            itemRegistry.add(item1);
            Item item2 = new StringItem("itemName2");
            itemRegistry.add(item2);
            Item item3 = new StringItem("itemName3");
            itemRegistry.add(item3);
            methodParameters[1] = "%itemName.%";
        }
    } else {
        methodParameters[1] = "hello";
    }
    extensionService.execute(methodParameters, console);
    assertThat(sink.isStreamProcessed(), is(shouldStreamBeExpected));
}
Also used : Item(org.eclipse.smarthome.core.items.Item) StringItem(org.eclipse.smarthome.core.library.items.StringItem) Configuration(org.osgi.service.cm.Configuration) Hashtable(java.util.Hashtable) ManagedThingProvider(org.eclipse.smarthome.core.thing.ManagedThingProvider) VolatileStorageService(org.eclipse.smarthome.test.storage.VolatileStorageService) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) StringItem(org.eclipse.smarthome.core.library.items.StringItem) Test(org.junit.Test)

Aggregations

Item (org.eclipse.smarthome.core.items.Item)111 GroupItem (org.eclipse.smarthome.core.items.GroupItem)42 GenericItem (org.eclipse.smarthome.core.items.GenericItem)39 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)37 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)35 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)31 State (org.eclipse.smarthome.core.types.State)26 Test (org.junit.Test)26 ItemRegistry (org.eclipse.smarthome.core.items.ItemRegistry)14 RollershutterItem (org.eclipse.smarthome.core.library.items.RollershutterItem)14 StringItem (org.eclipse.smarthome.core.library.items.StringItem)14 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)13 DimmerItem (org.eclipse.smarthome.core.library.items.DimmerItem)12 ColorItem (org.eclipse.smarthome.core.library.items.ColorItem)10 IntentInterpretation (org.openhab.ui.habot.nlp.IntentInterpretation)10 EventPublisher (org.eclipse.smarthome.core.events.EventPublisher)9 DecimalType (org.eclipse.smarthome.core.library.types.DecimalType)9 Thing (org.eclipse.smarthome.core.thing.Thing)9 Set (java.util.Set)8 Widget (org.eclipse.smarthome.model.sitemap.Widget)8