Search in sources :

Example 56 with State

use of org.eclipse.smarthome.core.types.State in project smarthome by eclipse.

the class PersistenceExtensions method minimumSince.

/**
 * Gets the historic item with the minimum value of the state of a given <code>item</code> since
 * a certain point in time. The {@link PersistenceService} identified by the <code>serviceId</code> is used.
 *
 * @param item the item to get the minimum state value for
 * @param timestamp the point in time from which to search for the minimum state value
 * @param serviceId the name of the {@link PersistenceService} to use
 * @return the historic item with the minimum state value since the given point in time, or a {@link HistoricItem}
 *         constructed from the <code>item</code>'s state if <code>item</code>'s state is the minimum value or if
 *         the given <code>serviceId</code> does not refer to an available {@link QueryablePersistenceService}
 */
public static HistoricItem minimumSince(final Item item, AbstractInstant timestamp, String serviceId) {
    Iterable<HistoricItem> result = getAllStatesSince(item, timestamp, serviceId);
    Iterator<HistoricItem> it = result.iterator();
    HistoricItem minimumHistoricItem = null;
    DecimalType minimum = (DecimalType) item.getStateAs(DecimalType.class);
    while (it.hasNext()) {
        HistoricItem historicItem = it.next();
        State state = historicItem.getState();
        if (state instanceof DecimalType) {
            DecimalType value = (DecimalType) state;
            if (minimum == null || value.compareTo(minimum) < 0) {
                minimum = value;
                minimumHistoricItem = historicItem;
            }
        }
    }
    if (minimumHistoricItem == null && minimum != null) {
        // the minimal state is the current one, so construct a historic item on the fly
        final DecimalType state = minimum;
        return new HistoricItem() {

            @Override
            public Date getTimestamp() {
                return Calendar.getInstance().getTime();
            }

            @Override
            public State getState() {
                return state;
            }

            @Override
            public String getName() {
                return item.getName();
            }
        };
    } else {
        return minimumHistoricItem;
    }
}
Also used : State(org.eclipse.smarthome.core.types.State) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem)

Example 57 with State

use of org.eclipse.smarthome.core.types.State in project smarthome by eclipse.

the class AbstractDmxThingTest method assertChannelStateUpdate.

protected void assertChannelStateUpdate(ChannelUID channelUID, Consumer<State> stateAssertion) {
    ArgumentCaptor<State> captor = ArgumentCaptor.forClass(State.class);
    verify(mockCallback, atLeastOnce()).stateUpdated(ArgumentMatchers.eq(channelUID), captor.capture());
    State value = captor.getValue();
    stateAssertion.accept(value);
}
Also used : State(org.eclipse.smarthome.core.types.State)

Example 58 with State

use of org.eclipse.smarthome.core.types.State in project habot by ghys.

the class CardBuilder method buildCard.

/**
 * Retrieves or build a card for the specified intent and matched items
 *
 * @param intent the intent including entities
 * @param matchedItems the matched items
 * @return the card (either retrieved or built)
 */
public Card buildCard(Intent intent, Collection<Item> matchedItems) {
    Set<String> tags = intent.getEntities().entrySet().stream().filter(e -> e.getKey().equals("object") || e.getKey().equals("location")).map(e -> e.getKey() + ":" + e.getValue()).collect(Collectors.toSet());
    Collection<Card> cardsInRegistry = this.cardRegistry.getCardByTags(tags).stream().filter(c -> !c.isNotReuseableInChat() && !c.isEphemeral()).collect(Collectors.toList());
    if (cardsInRegistry.size() > 0) {
        // don't handle multiple cards, just return the first one
        Card existingCard = cardsInRegistry.iterator().next();
        existingCard.updateTimestamp();
        cardRegistry.update(existingCard);
        return existingCard;
    }
    Card card = new Card("HbCard");
    card.addTags(tags);
    card.setEphemeral(true);
    card.addConfig("bigger", true);
    card.updateTimestamp();
    if (matchedItems.size() == 1) {
        Item item = matchedItems.stream().findFirst().get();
        card.setTitle(item.getLabel());
        card.setSubtitle(item.getName());
        switch(item.getType()) {
            case CoreItemFactory.SWITCH:
                Component switchComponent = new Component("HbSwitch");
                switchComponent.addConfig("item", item.getName());
                card.addComponent("right", switchComponent);
                break;
            case CoreItemFactory.DIMMER:
                if (item.hasTag("habot:switchable")) {
                    Component dimmerSwitchComponent = new Component("HbSwitch");
                    dimmerSwitchComponent.addConfig("item", item.getName());
                    card.addComponent("right", dimmerSwitchComponent);
                } else if (!item.hasTag("habot:control:knob")) {
                    Component dimmerValueComponent = new Component("HbSingleItemValue");
                    dimmerValueComponent.addConfig("item", item.getName());
                    card.addComponent("right", dimmerValueComponent);
                }
                Component dimmerContainerComponent = new Component("HbContainer");
                dimmerContainerComponent.addConfig("classes", new String[] { "full-width", "text-center" });
                if (item.hasTag("habot:control:knob")) {
                    Component knobComponent = new Component("HbKnob");
                    knobComponent.addConfig("item", item.getName());
                    knobComponent.addConfig("size", "200px");
                    knobComponent.addConfig("textSize", "2rem");
                    knobComponent.addConfig("color", "primary");
                    dimmerContainerComponent.addComponent("main", knobComponent);
                } else {
                    Component sliderComponent = new Component("HbSlider");
                    sliderComponent.addConfig("item", item.getName());
                    dimmerContainerComponent.addComponent("main", sliderComponent);
                }
                card.addComponent("main", dimmerContainerComponent);
                break;
            case CoreItemFactory.ROLLERSHUTTER:
                Component shutterValueComponent = new Component("HbSingleItemValue");
                shutterValueComponent.addConfig("item", item.getName());
                card.addComponent("right", shutterValueComponent);
                Component shutterContainerComponent = new Component("HbContainer");
                shutterContainerComponent.addConfig("classes", new String[] { "full-width", "text-center" });
                Component shutterControlComponent = new Component("HbShutterControl");
                shutterControlComponent.addConfig("item", item.getName());
                shutterControlComponent.addConfig("size", "lg");
                shutterControlComponent.addConfig("rounded", true);
                shutterControlComponent.addConfig("glossy", true);
                shutterControlComponent.addConfig("push", true);
                shutterControlComponent.addConfig("stopIcon", "close");
                shutterContainerComponent.addComponent("main", shutterControlComponent);
                card.addComponent("main", shutterContainerComponent);
                break;
            case CoreItemFactory.PLAYER:
                Component playerContainerComponent = new Component("HbContainer");
                playerContainerComponent.addConfig("classes", new String[] { "full-width", "text-center" });
                Component playerComponent = new Component("HbPlayer");
                playerComponent.addConfig("item", item.getName());
                playerComponent.addConfig("size", "lg");
                playerContainerComponent.addComponent("main", playerComponent);
                card.addComponent("main", playerContainerComponent);
                break;
            case CoreItemFactory.COLOR:
                Component colorPickerComponent = new Component("HbColorPicker");
                colorPickerComponent.addConfig("item", item.getName());
                card.addComponent("right", colorPickerComponent);
                Component brightnessDimmerComponent = new Component("HbSwitch");
                brightnessDimmerComponent.addConfig("item", item.getName());
                card.addComponent("right", brightnessDimmerComponent);
                Component brightnessDimmerContainerComponent = new Component("HbContainer");
                brightnessDimmerContainerComponent.addConfig("classes", new String[] { "full-width", "text-center" });
                Component brightnessSliderComponent = new Component("HbSlider");
                brightnessSliderComponent.addConfig("item", item.getName());
                brightnessDimmerContainerComponent.addComponent("main", brightnessSliderComponent);
                card.addComponent("main", brightnessDimmerContainerComponent);
                break;
            default:
                if (item.getType() == CoreItemFactory.IMAGE || item.getTags().stream().anyMatch(t -> t.startsWith("habot:image:sitemap:"))) {
                    /*
                         * If the item is an image (or a String with a tag indicating it's an image), build a
                         * HbImage component in the "media" slot
                         */
                    Component singleImageComponent = new Component("HbImage");
                    singleImageComponent.addConfig("item", item.getName());
                    card.addComponent("media", singleImageComponent);
                } else {
                    /*
                         * Try to get a formatted state to determine whether it's small enough to display
                         * in the "right" slot - otherwise add it to the "main" slot
                         */
                    String formattedState = formatState(item, item.getState());
                    Component singleItemComponent = new Component("HbSingleItemValue");
                    singleItemComponent.addConfig("item", item.getName());
                    if (formattedState.length() < 10) {
                        card.addComponent("right", singleItemComponent);
                    } else {
                        card.addComponent("main", singleItemComponent);
                    }
                }
                break;
        }
    } else {
        card.setTitle(getCardTitleFromGroupLabels(tags));
        // TODO: i18n
        card.setSubtitle(matchedItems.size() + " items");
        // TODO: detect images and build a HbCarousel with them - for webcams etc.
        Component list = new Component("HbList");
        for (Item item : matchedItems) {
            Component listItem = new Component("HbListItem");
            listItem.addConfig("item", item.getName());
            listItem.addConfig("label", item.getLabel());
            list.addComponent("items", listItem);
        }
        card.addComponent("list", list);
    }
    // Adds the (ephemeral) card to the registry anyways so it appears in the "recent cards" page
    cardRegistry.add(card);
    return card;
}
Also used : CardRegistry(org.openhab.ui.habot.card.internal.CardRegistry) TransformationHelper(org.eclipse.smarthome.core.transform.TransformationHelper) Collection(java.util.Collection) Set(java.util.Set) HistoryLastChangesSkill(org.openhab.ui.habot.nlp.internal.skill.HistoryLastChangesSkill) Collectors(java.util.stream.Collectors) Item(org.eclipse.smarthome.core.items.Item) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) CoreItemFactory(org.eclipse.smarthome.core.library.CoreItemFactory) StateDescription(org.eclipse.smarthome.core.types.StateDescription) State(org.eclipse.smarthome.core.types.State) Reference(org.osgi.service.component.annotations.Reference) Intent(org.openhab.ui.habot.nlp.Intent) FrameworkUtil(org.osgi.framework.FrameworkUtil) Item(org.eclipse.smarthome.core.items.Item)

Example 59 with State

use of org.eclipse.smarthome.core.types.State 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 60 with State

use of org.eclipse.smarthome.core.types.State in project smarthome by eclipse.

the class GroupItemTest method assertThatGroupItemWithoutFunctionCanHaveAconvertibleState.

@Test
public void assertThatGroupItemWithoutFunctionCanHaveAconvertibleState() {
    GroupItem groupItem = new GroupItem("root");
    PercentType pt = new PercentType(50);
    groupItem.setState(pt);
    State groupStateAsOnOff = groupItem.getStateAs(OnOffType.class);
    // any value >0 means on, so 50% means the group state should be ON
    assertTrue(OnOffType.ON.equals(groupStateAsOnOff));
}
Also used : State(org.eclipse.smarthome.core.types.State) PercentType(org.eclipse.smarthome.core.library.types.PercentType) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Aggregations

State (org.eclipse.smarthome.core.types.State)130 Test (org.junit.Test)59 DecimalType (org.eclipse.smarthome.core.library.types.DecimalType)23 PercentType (org.eclipse.smarthome.core.library.types.PercentType)22 Item (org.eclipse.smarthome.core.items.Item)21 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)19 Temperature (javax.measure.quantity.Temperature)18 StringType (org.eclipse.smarthome.core.library.types.StringType)18 QuantityType (org.eclipse.smarthome.core.library.types.QuantityType)17 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)15 HSBType (org.eclipse.smarthome.core.library.types.HSBType)15 OnOffType (org.eclipse.smarthome.core.library.types.OnOffType)15 RollershutterItem (org.eclipse.smarthome.core.library.items.RollershutterItem)13 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)13 ColorItem (org.eclipse.smarthome.core.library.items.ColorItem)12 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)11 DimmerItem (org.eclipse.smarthome.core.library.items.DimmerItem)10 RawType (org.eclipse.smarthome.core.library.types.RawType)10 Pressure (javax.measure.quantity.Pressure)9 GroupItem (org.eclipse.smarthome.core.items.GroupItem)8