Search in sources :

Example 86 with Item

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

the class ScriptEngineOSGiTest method testGreaterEqualsWithItemState.

@SuppressWarnings("null")
@Test
public void testGreaterEqualsWithItemState() throws ScriptExecutionException, ScriptParsingException {
    Item numberItem = itemRegistry.get(NUMBER_ITEM_TEMPERATURE);
    ((NumberItem) numberItem).setState(new QuantityType<>("20 °C"));
    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 87 with Item

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

the class ScriptEngineOSGiTest method testLessEqualsWithItemState.

@SuppressWarnings("null")
@Test
public void testLessEqualsWithItemState() throws ScriptExecutionException, ScriptParsingException {
    Item numberItem = itemRegistry.get(NUMBER_ITEM_TEMPERATURE);
    ((NumberItem) numberItem).setState(new QuantityType<>("19 °F"));
    assertTrue(runScript("NumberA.state <= 20 [°F]"));
}
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 88 with Item

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

the class ScriptEngineOSGiTest method testNotEqualsWithItemState.

@SuppressWarnings("null")
@Test
public void testNotEqualsWithItemState() throws ScriptExecutionException, ScriptParsingException {
    Item numberItem = itemRegistry.get(NUMBER_ITEM_TEMPERATURE);
    ((NumberItem) numberItem).setState(new QuantityType<>("20 °C"));
    assertTrue(runScript("NumberA.state != 10 [°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 89 with Item

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

the class BusEvent method postUpdate.

/**
 * Posts a status update for a specified item to the event bus.
 *
 * @param itemName the name of the item to send the status update for
 * @param stateAsString the new state of the item
 */
public static Object postUpdate(String itemName, String stateString) {
    ItemRegistry registry = ScriptServiceUtil.getItemRegistry();
    EventPublisher publisher = ScriptServiceUtil.getEventPublisher();
    if (publisher != null && registry != null) {
        try {
            Item item = registry.getItem(itemName);
            State state = TypeParser.parseState(item.getAcceptedDataTypes(), stateString);
            if (state != null) {
                publisher.post(ItemEventFactory.createStateEvent(itemName, state));
            } else {
                LoggerFactory.getLogger(BusEvent.class).warn("Cannot convert '{}' to a state type which item '{}' accepts: {}.", stateString, itemName, getAcceptedDataTypeNames(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) State(org.eclipse.smarthome.core.types.State) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 90 with Item

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

the class SelectionRenderer method renderWidget.

@Override
public EList<Widget> renderWidget(Widget w, StringBuilder sb) throws RenderException {
    String snippet = getSnippet("selection");
    snippet = StringUtils.replace(snippet, "%category%", getCategory(w));
    snippet = StringUtils.replace(snippet, "%state%", getState(w));
    snippet = StringUtils.replace(snippet, "%format%", getFormat());
    State state = itemUIRegistry.getState(w);
    Selection selection = (Selection) w;
    String mappedValue = "";
    Item item = null;
    try {
        item = itemUIRegistry.getItem(w.getItem());
    } catch (ItemNotFoundException e) {
        logger.debug("Failed to retrieve item during widget rendering: {}", e.getMessage());
    }
    StringBuilder rowSB = new StringBuilder();
    for (Mapping mapping : selection.getMappings()) {
        String rowSnippet = getSnippet("selection_row");
        String command = mapping.getCmd() != null ? mapping.getCmd() : "";
        String label = mapping.getLabel();
        if (item instanceof NumberItem && ((NumberItem) item).getDimension() != null) {
            String unit = getUnitForWidget(w);
            command = StringUtils.replace(command, UnitUtils.UNIT_PLACEHOLDER, unit);
            label = StringUtils.replace(label, UnitUtils.UNIT_PLACEHOLDER, unit);
            // Special treatment for °C since uom library uses a single character: ℃
            // This will ensure the current state matches the cmd and the buttonClass is set accordingly.
            command = StringUtils.replace(command, "°C", "℃");
        }
        rowSnippet = StringUtils.replace(rowSnippet, "%item%", w.getItem() != null ? w.getItem() : "");
        rowSnippet = StringUtils.replace(rowSnippet, "%cmd%", StringEscapeUtils.escapeHtml(command));
        rowSnippet = StringUtils.replace(rowSnippet, "%label%", label != null ? StringEscapeUtils.escapeHtml(label) : "");
        State compareMappingState = state;
        if (state instanceof QuantityType) {
            // convert the item state to the command value for proper
            // comparison and "checked" attribute calculation
            compareMappingState = convertStateToLabelUnit((QuantityType<?>) state, command);
        }
        if (compareMappingState.toString().equals(command)) {
            rowSnippet = StringUtils.replace(rowSnippet, "%checked%", "checked=\"true\"");
            mappedValue = (label != null) ? label : command;
        } else {
            rowSnippet = StringUtils.replace(rowSnippet, "%checked%", "");
        }
        rowSB.append(rowSnippet);
    }
    snippet = StringUtils.replace(snippet, "%label_header%", getLabel(w, mappedValue));
    snippet = StringUtils.replace(snippet, "%rows%", rowSB.toString());
    // Process the color tags
    snippet = processColor(w, snippet);
    sb.append(snippet);
    return null;
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) Item(org.eclipse.smarthome.core.items.Item) QuantityType(org.eclipse.smarthome.core.library.types.QuantityType) State(org.eclipse.smarthome.core.types.State) Selection(org.eclipse.smarthome.model.sitemap.Selection) Mapping(org.eclipse.smarthome.model.sitemap.Mapping) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

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