use of org.eclipse.smarthome.core.library.items.SwitchItem in project smarthome by eclipse.
the class ChannelStateDescriptionProviderOSGiTest method setup.
@Before
public void setup() {
initMocks(this);
Mockito.when(componentContext.getBundleContext()).thenReturn(bundleContext);
registerVolatileStorageService();
itemRegistry = getService(ItemRegistry.class);
assertNotNull(itemRegistry);
final TestThingHandlerFactory thingHandlerFactory = new TestThingHandlerFactory();
thingHandlerFactory.activate(componentContext);
registerService(thingHandlerFactory, ThingHandlerFactory.class.getName());
final StateDescription state = new StateDescription(BigDecimal.ZERO, BigDecimal.valueOf(100), BigDecimal.TEN, "%d Peek", true, Collections.singletonList(new StateOption("SOUND", "My great sound.")));
final StateDescription state2 = new StateDescription(BigDecimal.ZERO, BigDecimal.valueOf(256), BigDecimal.valueOf(8), null, false, null);
final ChannelType channelType = new ChannelType(new ChannelTypeUID("hue:alarm"), false, "Number", " ", "", null, null, state, null);
final ChannelType channelType2 = new ChannelType(new ChannelTypeUID("hue:num"), false, "Number", " ", "", null, null, state2, null);
final ChannelType channelType3 = new ChannelType(new ChannelTypeUID("hue:info"), true, "String", " ", "", null, null, null, null);
final ChannelType channelType4 = new ChannelType(new ChannelTypeUID("hue:color"), false, "Color", "Color", "", "ColorLight", null, null, null);
final ChannelType channelType5 = new ChannelType(new ChannelTypeUID("hue:brightness"), false, "Dimmer", "Brightness", "", "DimmableLight", null, null, null);
final ChannelType channelType6 = new ChannelType(new ChannelTypeUID("hue:switch"), false, "Switch", "Switch", "", "Light", null, null, null);
final ChannelType channelType7 = new ChannelType(new ChannelTypeUID("hue:num-dynamic"), false, "Number", " ", "", "Light", null, state, null);
List<ChannelType> channelTypes = new ArrayList<>();
channelTypes.add(channelType);
channelTypes.add(channelType2);
channelTypes.add(channelType3);
channelTypes.add(channelType4);
channelTypes.add(channelType5);
channelTypes.add(channelType6);
channelTypes.add(channelType7);
registerService(new ChannelTypeProvider() {
@Override
public Collection<ChannelType> getChannelTypes(Locale locale) {
return channelTypes;
}
@Override
public ChannelType getChannelType(ChannelTypeUID channelTypeUID, Locale locale) {
for (final ChannelType channelType : channelTypes) {
if (channelType.getUID().equals(channelTypeUID)) {
return channelType;
}
}
return null;
}
@Override
public ChannelGroupType getChannelGroupType(ChannelGroupTypeUID channelGroupTypeUID, Locale locale) {
return null;
}
@Override
public Collection<ChannelGroupType> getChannelGroupTypes(Locale locale) {
return Collections.emptySet();
}
});
registerService(new DynamicStateDescriptionProvider() {
final StateDescription newState = new StateDescription(BigDecimal.valueOf(10), BigDecimal.valueOf(100), BigDecimal.valueOf(5), "VALUE %d", false, Arrays.asList(new StateOption("value0", "label0"), new StateOption("value1", "label1")));
@Override
@Nullable
public StateDescription getStateDescription(@NonNull Channel channel, @Nullable StateDescription original, @Nullable Locale locale) {
String id = channel.getUID().getIdWithoutGroup();
if ("7_1".equals(id)) {
assertEquals(channel.getChannelTypeUID(), channelType7.getUID());
return newState;
} else if ("7_2".equals(id)) {
assertEquals(channel.getChannelTypeUID(), channelType7.getUID());
StateDescription newState2 = new StateDescription(original.getMinimum().add(BigDecimal.ONE), original.getMaximum().add(BigDecimal.ONE), original.getStep().add(BigDecimal.TEN), "NEW " + original.getPattern(), true, original.getOptions());
return newState2;
}
return null;
}
});
List<ChannelDefinition> channelDefinitions = new ArrayList<>();
channelDefinitions.add(new ChannelDefinition("1", channelType.getUID()));
channelDefinitions.add(new ChannelDefinition("2", channelType2.getUID()));
channelDefinitions.add(new ChannelDefinition("3", channelType3.getUID()));
channelDefinitions.add(new ChannelDefinition("4", channelType4.getUID()));
channelDefinitions.add(new ChannelDefinition("5", channelType5.getUID()));
channelDefinitions.add(new ChannelDefinition("6", channelType6.getUID()));
channelDefinitions.add(new ChannelDefinition("7_1", channelType7.getUID()));
channelDefinitions.add(new ChannelDefinition("7_2", channelType7.getUID()));
registerService(new SimpleThingTypeProvider(Collections.singleton(ThingTypeBuilder.instance(new ThingTypeUID("hue:lamp"), "label").withChannelDefinitions(channelDefinitions).build())));
List<Item> items = new ArrayList<>();
items.add(new NumberItem("TestItem"));
items.add(new NumberItem("TestItem2"));
items.add(new StringItem("TestItem3"));
items.add(new ColorItem("TestItem4"));
items.add(new DimmerItem("TestItem5"));
items.add(new SwitchItem("TestItem6"));
items.add(new NumberItem("TestItem7_1"));
items.add(new NumberItem("TestItem7_2"));
registerService(new TestItemProvider(items));
linkRegistry = getService(ItemChannelLinkRegistry.class);
stateDescriptionProvider = getService(StateDescriptionProvider.class);
assertNotNull(stateDescriptionProvider);
}
use of org.eclipse.smarthome.core.library.items.SwitchItem in project smarthome by eclipse.
the class GroupItemTest method assertAcceptedCommandTypesOnGroupItemsReturnsSubsetOfCommandTypesSupportedByAllMembers.
@SuppressWarnings("unchecked")
@Test()
public void assertAcceptedCommandTypesOnGroupItemsReturnsSubsetOfCommandTypesSupportedByAllMembers() {
SwitchItem switchItem = new SwitchItem("switch");
NumberItem numberItem = new NumberItem("number");
GroupItem groupItem = new GroupItem("group");
groupItem.addMember(switchItem);
groupItem.addMember(numberItem);
assertThat(groupItem.getAcceptedCommandTypes(), hasItems(RefreshType.class));
}
use of org.eclipse.smarthome.core.library.items.SwitchItem in project smarthome by eclipse.
the class GroupItemTest method assertThatGroupItemPostsEventsForChangesCorrectly.
@Test
public void assertThatGroupItemPostsEventsForChangesCorrectly() {
// from ItemEventFactory.GROUPITEM_STATE_CHANGED_EVENT_TOPIC
String GROUPITEM_STATE_CHANGED_EVENT_TOPIC = "smarthome/items/{itemName}/{memberName}/statechanged";
events.clear();
GroupItem groupItem = new GroupItem("root", new SwitchItem("mySwitch"), new GroupFunction.Equality());
groupItem.setItemStateConverter(itemStateConverter);
SwitchItem member = new SwitchItem("member1");
groupItem.addMember(member);
groupItem.setEventPublisher(publisher);
State oldGroupState = groupItem.getState();
// State changes -> one change event is fired
member.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.getMemberName().equals(member.getName()));
assertTrue(change.getTopic().equals(GROUPITEM_STATE_CHANGED_EVENT_TOPIC.replace("{memberName}", member.getName()).replace("{itemName}", groupItem.getName())));
assertTrue(change.getItemState().equals(groupItem.getState()));
assertTrue(change.getOldItemState().equals(oldGroupState));
events.clear();
// State doesn't change -> no events are fired
member.setState(member.getState());
assertThat(events.size(), is(0));
}
use of org.eclipse.smarthome.core.library.items.SwitchItem in project smarthome by eclipse.
the class GroupItemTest method testGetStateAs_shouldEqualStateUpdate.
@Test
public void testGetStateAs_shouldEqualStateUpdate() {
// Main group uses AND function
GroupItem rootGroupItem = new GroupItem("root", new SwitchItem("baseItem"), new ArithmeticGroupFunction.And(OnOffType.ON, OnOffType.OFF));
rootGroupItem.setItemStateConverter(itemStateConverter);
TestItem member1 = new TestItem("member1");
rootGroupItem.addMember(member1);
TestItem member2 = new TestItem("member2");
rootGroupItem.addMember(member2);
// Sub-group uses NAND function
GroupItem subGroup = new GroupItem("subGroup1", new SwitchItem("baseItem"), new ArithmeticGroupFunction.NAnd(OnOffType.ON, OnOffType.OFF));
TestItem subMember = new TestItem("subGroup member 1");
subGroup.addMember(subMember);
rootGroupItem.addMember(subGroup);
member1.setState(OnOffType.ON);
member2.setState(OnOffType.ON);
subMember.setState(OnOffType.OFF);
// subGroup and subMember state differ
assertThat(subGroup.getStateAs(OnOffType.class), is(OnOffType.ON));
assertThat(subMember.getStateAs(OnOffType.class), is(OnOffType.OFF));
// We expect ON here
State getStateAsState = rootGroupItem.getStateAs(OnOffType.class);
// recalculate the state
rootGroupItem.stateUpdated(member1, UnDefType.NULL);
State stateUpdatedState = rootGroupItem.getState();
assertThat(getStateAsState, is(OnOffType.ON));
assertThat(stateUpdatedState, is(OnOffType.ON));
}
use of org.eclipse.smarthome.core.library.items.SwitchItem in project smarthome by eclipse.
the class GroupItemTest method assertCyclicGroupItemsCalculateStateWithSubGroupFunction.
@Test
public void assertCyclicGroupItemsCalculateStateWithSubGroupFunction() {
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", new SwitchItem("baseItem"), new ArithmeticGroupFunction.Sum());
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, group2
assertThat(rootGroup.getStateAs(DecimalType.class), is(new DecimalType(3)));
}
Aggregations