Search in sources :

Example 1 with DimmerItem

use of org.eclipse.smarthome.core.library.items.DimmerItem in project smarthome by eclipse.

the class GroupItemTest method assertThatGroupItemWithDimmeritemBaseItemConversionWorks.

@Test
public void assertThatGroupItemWithDimmeritemBaseItemConversionWorks() {
    // initially this group has State UndefType.NULL
    GroupItem groupItem = new GroupItem("root", new DimmerItem("myDimmer"));
    State groupStateAsPercent = groupItem.getStateAs(PercentType.class);
    // a state conversion from NULL to PercentType should not be possible
    assertNull(groupStateAsPercent);
    // init group
    groupItem.setState(new PercentType(80));
    groupStateAsPercent = groupItem.getStateAs(PercentType.class);
    assertTrue(groupStateAsPercent instanceof PercentType);
    assertThat(((PercentType) groupStateAsPercent).intValue(), is(80));
}
Also used : State(org.eclipse.smarthome.core.types.State) DimmerItem(org.eclipse.smarthome.core.library.items.DimmerItem) PercentType(org.eclipse.smarthome.core.library.types.PercentType) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 2 with DimmerItem

use of org.eclipse.smarthome.core.library.items.DimmerItem 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);
}
Also used : Locale(java.util.Locale) ArrayList(java.util.ArrayList) ChannelGroupType(org.eclipse.smarthome.core.thing.type.ChannelGroupType) ColorItem(org.eclipse.smarthome.core.library.items.ColorItem) DynamicStateDescriptionProvider(org.eclipse.smarthome.core.thing.type.DynamicStateDescriptionProvider) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) ColorItem(org.eclipse.smarthome.core.library.items.ColorItem) Item(org.eclipse.smarthome.core.items.Item) StringItem(org.eclipse.smarthome.core.library.items.StringItem) DimmerItem(org.eclipse.smarthome.core.library.items.DimmerItem) ChannelTypeUID(org.eclipse.smarthome.core.thing.type.ChannelTypeUID) DimmerItem(org.eclipse.smarthome.core.library.items.DimmerItem) ItemChannelLinkRegistry(org.eclipse.smarthome.core.thing.link.ItemChannelLinkRegistry) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) StateDescription(org.eclipse.smarthome.core.types.StateDescription) Channel(org.eclipse.smarthome.core.thing.Channel) ChannelGroupTypeUID(org.eclipse.smarthome.core.thing.type.ChannelGroupTypeUID) BaseThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory) ThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory) StringItem(org.eclipse.smarthome.core.library.items.StringItem) StateOption(org.eclipse.smarthome.core.types.StateOption) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) ChannelDefinition(org.eclipse.smarthome.core.thing.type.ChannelDefinition) ChannelTypeProvider(org.eclipse.smarthome.core.thing.type.ChannelTypeProvider) Collection(java.util.Collection) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) ChannelType(org.eclipse.smarthome.core.thing.type.ChannelType) StateDescriptionProvider(org.eclipse.smarthome.core.types.StateDescriptionProvider) DynamicStateDescriptionProvider(org.eclipse.smarthome.core.thing.type.DynamicStateDescriptionProvider) Nullable(org.eclipse.jdt.annotation.Nullable) Before(org.junit.Before)

Example 3 with DimmerItem

use of org.eclipse.smarthome.core.library.items.DimmerItem in project smarthome by eclipse.

the class GroupItemTest method assertThatGroupItemwithDimmeritemAcceptsGetsPercentTypeStateIfMembersHavePercentTypeStates.

@Test
public void assertThatGroupItemwithDimmeritemAcceptsGetsPercentTypeStateIfMembersHavePercentTypeStates() {
    events.clear();
    GroupItem groupItem = new GroupItem("root", new DimmerItem("myDimmer"), new ArithmeticGroupFunction.Avg());
    groupItem.setItemStateConverter(itemStateConverter);
    DimmerItem member1 = new DimmerItem("dimmer1");
    groupItem.addMember(member1);
    DimmerItem member2 = new DimmerItem("dimmer2");
    groupItem.addMember(member2);
    groupItem.setEventPublisher(publisher);
    member1.setState(new PercentType(50));
    waitForAssert(() -> assertThat(events.size(), is(1)));
    List<Event> changes = events.stream().filter(it -> it instanceof GroupItemStateChangedEvent).collect(Collectors.toList());
    GroupItemStateChangedEvent change = (GroupItemStateChangedEvent) changes.get(0);
    assertTrue(change.getItemName().equals(groupItem.getName()));
    State newEventState = change.getItemState();
    assertTrue(newEventState instanceof PercentType);
    assertThat(((PercentType) newEventState).intValue(), is(50));
    State newGroupState = groupItem.getState();
    assertTrue(newGroupState instanceof PercentType);
    assertThat(((PercentType) newGroupState).intValue(), is(50));
    events.clear();
    member2.setState(new PercentType(10));
    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()));
    newEventState = change.getItemState();
    assertTrue(newEventState instanceof PercentType);
    assertThat(((PercentType) newEventState).intValue(), is(30));
    newGroupState = groupItem.getState();
    assertTrue(newGroupState instanceof PercentType);
    assertThat(((PercentType) newGroupState).intValue(), is(30));
}
Also used : SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) CoreMatchers(org.hamcrest.CoreMatchers) MockitoAnnotations.initMocks(org.mockito.MockitoAnnotations.initMocks) RollershutterItem(org.eclipse.smarthome.core.library.items.RollershutterItem) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) Mock(org.mockito.Mock) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) UnDefType(org.eclipse.smarthome.core.types.UnDefType) EventFilter(org.eclipse.smarthome.core.events.EventFilter) EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) GroupFunctionDTO(org.eclipse.smarthome.core.items.dto.GroupFunctionDTO) GroupFunctionHelper(org.eclipse.smarthome.core.internal.items.GroupFunctionHelper) HashSet(java.util.HashSet) ColorItem(org.eclipse.smarthome.core.library.items.ColorItem) GroupItemStateChangedEvent(org.eclipse.smarthome.core.items.events.GroupItemStateChangedEvent) Temperature(javax.measure.quantity.Temperature) Quantity(javax.measure.Quantity) Pressure(javax.measure.quantity.Pressure) ItemStateConverterImpl(org.eclipse.smarthome.core.internal.items.ItemStateConverterImpl) QuantityType(org.eclipse.smarthome.core.library.types.QuantityType) CoreItemFactory(org.eclipse.smarthome.core.library.CoreItemFactory) StringType(org.eclipse.smarthome.core.library.types.StringType) LinkedList(java.util.LinkedList) State(org.eclipse.smarthome.core.types.State) Dimensionless(javax.measure.quantity.Dimensionless) Before(org.junit.Before) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) RefreshType(org.eclipse.smarthome.core.types.RefreshType) Set(java.util.Set) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) ItemUpdatedEvent(org.eclipse.smarthome.core.items.events.ItemUpdatedEvent) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) Collectors(java.util.stream.Collectors) RawType(org.eclipse.smarthome.core.library.types.RawType) IsCollectionWithSize.hasSize(org.hamcrest.collection.IsCollectionWithSize.hasSize) List(java.util.List) HSBType(org.eclipse.smarthome.core.library.types.HSBType) Ignore(org.junit.Ignore) PercentType(org.eclipse.smarthome.core.library.types.PercentType) Units(tec.uom.se.unit.Units) ArithmeticGroupFunction(org.eclipse.smarthome.core.library.types.ArithmeticGroupFunction) UnitProvider(org.eclipse.smarthome.core.i18n.UnitProvider) DimmerItem(org.eclipse.smarthome.core.library.items.DimmerItem) Assert(org.junit.Assert) Event(org.eclipse.smarthome.core.events.Event) Collections(java.util.Collections) GroupItemStateChangedEvent(org.eclipse.smarthome.core.items.events.GroupItemStateChangedEvent) State(org.eclipse.smarthome.core.types.State) DimmerItem(org.eclipse.smarthome.core.library.items.DimmerItem) GroupItemStateChangedEvent(org.eclipse.smarthome.core.items.events.GroupItemStateChangedEvent) ItemUpdatedEvent(org.eclipse.smarthome.core.items.events.ItemUpdatedEvent) Event(org.eclipse.smarthome.core.events.Event) PercentType(org.eclipse.smarthome.core.library.types.PercentType) ArithmeticGroupFunction(org.eclipse.smarthome.core.library.types.ArithmeticGroupFunction) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 4 with DimmerItem

use of org.eclipse.smarthome.core.library.items.DimmerItem in project smarthome by eclipse.

the class ArithmeticGroupFunctionTest method testOrFunction_differntTypes.

@Test
public void testOrFunction_differntTypes() {
    DimmerItem dimmer1 = new DimmerItem("TestDimmer1");
    dimmer1.setState(new PercentType("42"));
    DimmerItem dimmer2 = new DimmerItem("TestDimmer2");
    dimmer2.setState(new DecimalType("0"));
    SwitchItem switch1 = new SwitchItem("TestSwitch1");
    switch1.setState(OnOffType.ON);
    SwitchItem switch2 = new SwitchItem("TestSwitch2");
    switch2.setState(OnOffType.OFF);
    items.add(dimmer1);
    items.add(dimmer2);
    items.add(switch1);
    items.add(switch2);
    function = new ArithmeticGroupFunction.Or(OnOffType.ON, OnOffType.OFF);
    State state = function.calculate(items);
    State decimalState = function.getStateAs(items, DecimalType.class);
    assertEquals(OnOffType.ON, state);
    assertEquals(new DecimalType("2"), decimalState);
}
Also used : State(org.eclipse.smarthome.core.types.State) DimmerItem(org.eclipse.smarthome.core.library.items.DimmerItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Test(org.junit.Test)

Example 5 with DimmerItem

use of org.eclipse.smarthome.core.library.items.DimmerItem in project smarthome by eclipse.

the class ItemResourceOSGiTest method setup.

@Before
public void setup() {
    initMocks(this);
    itemResource = getService(ItemResource.class);
    itemResource.uriInfo = mock(UriInfo.class);
    registerVolatileStorageService();
    managedItemProvider = getService(ManagedItemProvider.class);
    item1 = new SwitchItem(ITEM_NAME1);
    item2 = new SwitchItem(ITEM_NAME2);
    item3 = new DimmerItem(ITEM_NAME3);
    when(itemProvider.getAll()).thenReturn(Arrays.asList(item1, item2, item3));
    registerService(itemProvider);
}
Also used : ManagedItemProvider(org.eclipse.smarthome.core.items.ManagedItemProvider) DimmerItem(org.eclipse.smarthome.core.library.items.DimmerItem) UriInfo(javax.ws.rs.core.UriInfo) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Before(org.junit.Before)

Aggregations

DimmerItem (org.eclipse.smarthome.core.library.items.DimmerItem)5 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)4 State (org.eclipse.smarthome.core.types.State)3 Before (org.junit.Before)3 Test (org.junit.Test)3 ColorItem (org.eclipse.smarthome.core.library.items.ColorItem)2 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Locale (java.util.Locale)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Quantity (javax.measure.Quantity)1 Dimensionless (javax.measure.quantity.Dimensionless)1 Pressure (javax.measure.quantity.Pressure)1 Temperature (javax.measure.quantity.Temperature)1