use of org.eclipse.smarthome.core.library.items.SwitchItem in project smarthome by eclipse.
the class GroupItemTest method assertCyclicGroupItemsCalculateState.
@Test
public void assertCyclicGroupItemsCalculateState() {
GroupFunction countFn = new ArithmeticGroupFunction.Count(new StringType(".*"));
GroupItem rootGroup = new GroupItem("rootGroup", new SwitchItem("baseItem"), countFn);
TestItem rootMember = new TestItem("rootMember");
rootGroup.addMember(rootMember);
GroupItem group1 = new GroupItem("group1");
GroupItem group2 = new GroupItem("group2");
rootGroup.addMember(group1);
group1.addMember(group2);
group2.addMember(group1);
group1.addMember(new TestItem("sub1"));
group2.addMember(new TestItem("sub2-1"));
group2.addMember(new TestItem("sub2-2"));
group2.addMember(new TestItem("sub2-3"));
// count: rootMember, sub1, sub2-1, sub2-2, sub2-3
assertThat(rootGroup.getStateAs(DecimalType.class), is(new DecimalType(5)));
}
use of org.eclipse.smarthome.core.library.items.SwitchItem in project smarthome by eclipse.
the class GroupItemTest method assertThatGroupItemChangesRespectGroupFunctionORwithUNDEF.
@Test
public void assertThatGroupItemChangesRespectGroupFunctionORwithUNDEF() throws InterruptedException {
events.clear();
GroupItem groupItem = new GroupItem("root", new SwitchItem("mySwitch"), new ArithmeticGroupFunction.Or(OnOffType.ON, OnOffType.OFF));
groupItem.setItemStateConverter(itemStateConverter);
SwitchItem sw1 = new SwitchItem("switch1");
SwitchItem sw2 = new SwitchItem("switch2");
groupItem.addMember(sw1);
groupItem.addMember(sw2);
groupItem.setEventPublisher(publisher);
// State changes -> one change event is fired
sw1.setState(OnOffType.ON);
waitForAssert(() -> assertThat(events.size(), is(1)));
List<Event> changes = events.stream().filter(it -> it instanceof GroupItemStateChangedEvent).collect(Collectors.toList());
assertThat(changes.size(), is(1));
GroupItemStateChangedEvent change = (GroupItemStateChangedEvent) changes.get(0);
assertTrue(change.getItemName().equals(groupItem.getName()));
assertTrue(change.getOldItemState().equals(UnDefType.NULL));
assertTrue(change.getItemState().equals(OnOffType.ON));
events.clear();
sw2.setState(OnOffType.ON);
sw2.setState(UnDefType.UNDEF);
// wait to see that the event doesn't fire
Thread.sleep(WAIT_EVENT_TO_BE_HANDLED);
assertThat(events.size(), is(0));
assertTrue(groupItem.getState().equals(OnOffType.ON));
}
use of org.eclipse.smarthome.core.library.items.SwitchItem in project smarthome by eclipse.
the class GroupItemTest method assertThatGroupItemChangesRespectGroupFunctionOR.
@Test
public void assertThatGroupItemChangesRespectGroupFunctionOR() {
events.clear();
GroupItem groupItem = new GroupItem("root", new SwitchItem("mySwitch"), new ArithmeticGroupFunction.Or(OnOffType.ON, OnOffType.OFF));
groupItem.setItemStateConverter(itemStateConverter);
SwitchItem sw1 = new SwitchItem("switch1");
SwitchItem sw2 = new SwitchItem("switch2");
groupItem.addMember(sw1);
groupItem.addMember(sw2);
groupItem.setEventPublisher(publisher);
// State changes -> one change event is fired
sw1.setState(OnOffType.ON);
waitForAssert(() -> assertThat(events.size(), is(1)));
List<Event> changes = events.stream().filter(it -> it instanceof GroupItemStateChangedEvent).collect(Collectors.toList());
assertThat(changes.size(), is(1));
GroupItemStateChangedEvent change = (GroupItemStateChangedEvent) changes.get(0);
assertTrue(change.getItemName().equals(groupItem.getName()));
assertTrue(change.getOldItemState().equals(UnDefType.NULL));
assertTrue(change.getItemState().equals(OnOffType.ON));
assertTrue(groupItem.getState().equals(OnOffType.ON));
}
use of org.eclipse.smarthome.core.library.items.SwitchItem in project smarthome by eclipse.
the class GroupItemTest method assertThatGroupItemChangesRespectGroupFunctionAND.
@Test
public void assertThatGroupItemChangesRespectGroupFunctionAND() {
events.clear();
GroupItem groupItem = new GroupItem("root", new SwitchItem("mySwitch"), new ArithmeticGroupFunction.And(OnOffType.ON, OnOffType.OFF));
groupItem.setItemStateConverter(itemStateConverter);
SwitchItem sw1 = new SwitchItem("switch1");
SwitchItem sw2 = new SwitchItem("switch2");
groupItem.addMember(sw1);
groupItem.addMember(sw2);
groupItem.setEventPublisher(publisher);
// State changes -> one change event is fired
sw1.setState(OnOffType.ON);
waitForAssert(() -> assertThat(events.size(), is(1)));
List<Event> changes = events.stream().filter(it -> it instanceof GroupItemStateChangedEvent).collect(Collectors.toList());
assertThat(changes.size(), is(1));
GroupItemStateChangedEvent change = (GroupItemStateChangedEvent) changes.get(0);
assertTrue(change.getItemName().equals(groupItem.getName()));
// we expect that the group should now have status "OFF"
assertTrue(change.getOldItemState().equals(UnDefType.NULL));
assertTrue(change.getItemState().equals(OnOffType.OFF));
events.clear();
// State changes -> one change event is fired
sw2.setState(OnOffType.ON);
waitForAssert(() -> assertThat(events.size(), is(1)));
changes = events.stream().filter(it -> it instanceof GroupItemStateChangedEvent).collect(Collectors.toList());
assertThat(changes.size(), is(1));
change = (GroupItemStateChangedEvent) changes.get(0);
assertTrue(change.getItemName().equals(groupItem.getName()));
// we expect that the group should now have status "ON"
assertTrue(change.getOldItemState().equals(OnOffType.OFF));
assertTrue(change.getItemState().equals(OnOffType.ON));
assertTrue(groupItem.getState().equals(OnOffType.ON));
}
use of org.eclipse.smarthome.core.library.items.SwitchItem 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();
}
Aggregations