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;
}
}
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);
}
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;
}
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);
}
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));
}
Aggregations