Search in sources :

Example 66 with Configuration

use of org.eclipse.smarthome.config.core.Configuration in project smarthome by eclipse.

the class WelcomeHomeRulesProvider method createLightsRule.

/**
 * This method creates a rule from scratch by using trigger, condition, action, configDescriptions and
 * configuration, tags.
 *
 * @return the created rule
 */
private Rule createLightsRule() {
    // initialize the trigger
    String triggerId = "LightsSwitchOnRuleTrigger";
    List<Trigger> triggers = new ArrayList<Trigger>();
    triggers.add(new Trigger(triggerId, LightsTriggerType.UID, null));
    // initialize the condition - here the tricky part is the referring into the condition input - trigger output.
    // The syntax is a similar to the JUEL syntax.
    Configuration config = new Configuration();
    config.put(StateConditionType.CONFIG_STATE, "on");
    List<Condition> conditions = new ArrayList<Condition>();
    Map<String, String> inputs = new HashMap<String, String>();
    inputs.put(StateConditionType.INPUT_CURRENT_STATE, triggerId + "." + StateConditionType.INPUT_CURRENT_STATE);
    conditions.add(new Condition("LightsStateCondition", StateConditionType.UID, config, inputs));
    // initialize the action - here the tricky part is the referring into the action configuration parameter - the
    // template configuration parameter. The syntax is a similar to the JUEL syntax.
    config = new Configuration();
    config.put(WelcomeHomeActionType.CONFIG_DEVICE, "Lights");
    config.put(WelcomeHomeActionType.CONFIG_RESULT, "Lights are switched on");
    List<Action> actions = new ArrayList<Action>();
    actions.add(new Action("LightsSwitchOnAction", WelcomeHomeActionType.UID, config, null));
    // initialize the configDescriptions
    List<ConfigDescriptionParameter> configDescriptions = new ArrayList<ConfigDescriptionParameter>();
    final ConfigDescriptionParameter device = ConfigDescriptionParameterBuilder.create(WelcomeHomeRulesProvider.CONFIG_UNIT, Type.TEXT).withRequired(true).withReadOnly(true).withMultiple(false).withLabel("Device").withDescription("Device description").build();
    final ConfigDescriptionParameter result = ConfigDescriptionParameterBuilder.create(WelcomeHomeRulesProvider.CONFIG_EXPECTED_RESULT, Type.TEXT).withRequired(true).withReadOnly(true).withMultiple(false).withLabel("Result").withDescription("Result description").build();
    configDescriptions.add(device);
    configDescriptions.add(result);
    // initialize the configuration
    config = new Configuration();
    config.put(CONFIG_UNIT, "Lights");
    config.put(CONFIG_EXPECTED_RESULT, "The lights are switched on.");
    // create the rule
    Rule lightsSwitchOn = new Rule(L_UID);
    lightsSwitchOn.setTriggers(triggers);
    lightsSwitchOn.setConfigurationDescriptions(configDescriptions);
    lightsSwitchOn.setConditions(conditions);
    lightsSwitchOn.setActions(actions);
    // initialize the tags
    Set<String> tags = new HashSet<String>();
    tags.add("lights");
    // set the tags
    lightsSwitchOn.setTags(tags);
    return lightsSwitchOn;
}
Also used : Condition(org.eclipse.smarthome.automation.Condition) Action(org.eclipse.smarthome.automation.Action) Configuration(org.eclipse.smarthome.config.core.Configuration) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Trigger(org.eclipse.smarthome.automation.Trigger) ConfigDescriptionParameter(org.eclipse.smarthome.config.core.ConfigDescriptionParameter) Rule(org.eclipse.smarthome.automation.Rule) HashSet(java.util.HashSet)

Example 67 with Configuration

use of org.eclipse.smarthome.config.core.Configuration in project smarthome by eclipse.

the class ChannelStateDescriptionProviderOSGiTest method presentItemStateDescription.

/**
 * Assert that item's state description is present.
 */
@Test
public void presentItemStateDescription() throws ItemNotFoundException {
    ThingRegistry thingRegistry = getService(ThingRegistry.class);
    ManagedThingProvider managedThingProvider = getService(ManagedThingProvider.class);
    Thing thing = thingRegistry.createThingOfType(new ThingTypeUID("hue:lamp"), new ThingUID("hue:lamp:lamp1"), null, "test thing", new Configuration());
    assertNotNull(thing);
    managedThingProvider.add(thing);
    ItemChannelLink link = new ItemChannelLink("TestItem", getChannel(thing, "1").getUID());
    linkRegistry.add(link);
    link = new ItemChannelLink("TestItem2", getChannel(thing, "2").getUID());
    linkRegistry.add(link);
    link = new ItemChannelLink("TestItem3", getChannel(thing, "3").getUID());
    linkRegistry.add(link);
    link = new ItemChannelLink("TestItem4", getChannel(thing, "4").getUID());
    linkRegistry.add(link);
    link = new ItemChannelLink("TestItem5", getChannel(thing, "5").getUID());
    linkRegistry.add(link);
    link = new ItemChannelLink("TestItem6", getChannel(thing, "6").getUID());
    linkRegistry.add(link);
    link = new ItemChannelLink("TestItem7_1", getChannel(thing, "7_1").getUID());
    linkRegistry.add(link);
    link = new ItemChannelLink("TestItem7_2", getChannel(thing, "7_2").getUID());
    linkRegistry.add(link);
    // 
    final Collection<Item> items = itemRegistry.getItems();
    assertEquals(false, items.isEmpty());
    Item item = itemRegistry.getItem("TestItem");
    assertEquals("Number", item.getType());
    StateDescription state = item.getStateDescription();
    assertNotNull(state);
    assertEquals(BigDecimal.ZERO, state.getMinimum());
    assertEquals(BigDecimal.valueOf(100), state.getMaximum());
    assertEquals(BigDecimal.TEN, state.getStep());
    assertEquals("%d Peek", state.getPattern());
    assertEquals(true, state.isReadOnly());
    List<StateOption> opts = state.getOptions();
    assertEquals(1, opts.size());
    final StateOption opt = opts.get(0);
    assertEquals("SOUND", opt.getValue());
    assertEquals("My great sound.", opt.getLabel());
    item = itemRegistry.getItem("TestItem2");
    assertEquals("Number", item.getType());
    state = item.getStateDescription();
    assertNotNull(state);
    assertEquals(BigDecimal.ZERO, state.getMinimum());
    assertEquals(BigDecimal.valueOf(256), state.getMaximum());
    assertEquals(BigDecimal.valueOf(8), state.getStep());
    assertEquals("%.0f", state.getPattern());
    assertEquals(false, state.isReadOnly());
    opts = state.getOptions();
    assertEquals(0, opts.size());
    item = itemRegistry.getItem("TestItem3");
    assertEquals("String", item.getType());
    state = item.getStateDescription();
    assertNotNull(state);
    assertNull(state.getMinimum());
    assertNull(state.getMaximum());
    assertNull(state.getStep());
    assertEquals("%s", state.getPattern());
    assertEquals(false, state.isReadOnly());
    opts = state.getOptions();
    assertEquals(0, opts.size());
    item = itemRegistry.getItem("TestItem4");
    assertEquals("Color", item.getType());
    state = item.getStateDescription();
    assertNull(state);
    item = itemRegistry.getItem("TestItem5");
    assertEquals("Dimmer", item.getType());
    state = item.getStateDescription();
    assertNull(state);
    item = itemRegistry.getItem("TestItem6");
    assertEquals("Switch", item.getType());
    state = item.getStateDescription();
    assertNull(state);
    item = itemRegistry.getItem("TestItem7_1");
    assertEquals("Number", item.getType());
    state = item.getStateDescription();
    assertNotNull(state);
    assertEquals(BigDecimal.valueOf(10), state.getMinimum());
    assertEquals(BigDecimal.valueOf(100), state.getMaximum());
    assertEquals(BigDecimal.valueOf(5), state.getStep());
    assertEquals("VALUE %d", state.getPattern());
    assertEquals(false, state.isReadOnly());
    opts = state.getOptions();
    assertNotNull(opts);
    assertEquals(2, opts.size());
    final StateOption opt0 = opts.get(0);
    assertNotNull(opt0);
    assertEquals(opt0.getValue(), "value0");
    assertEquals(opt0.getLabel(), "label0");
    final StateOption opt1 = opts.get(1);
    assertNotNull(opt1);
    assertEquals(opt1.getValue(), "value1");
    assertEquals(opt1.getLabel(), "label1");
    item = itemRegistry.getItem("TestItem7_2");
    assertEquals("Number", item.getType());
    state = item.getStateDescription();
    assertNotNull(state);
    assertEquals(BigDecimal.valueOf(1), state.getMinimum());
    assertEquals(BigDecimal.valueOf(101), state.getMaximum());
    assertEquals(BigDecimal.valueOf(20), state.getStep());
    assertEquals("NEW %d Peek", state.getPattern());
    assertEquals(true, state.isReadOnly());
    opts = state.getOptions();
    assertNotNull(opts);
    assertEquals(1, opts.size());
    final StateOption opt2 = opts.get(0);
    assertEquals("SOUND", opt2.getValue());
    assertEquals("My great sound.", opt2.getLabel());
}
Also used : 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) Configuration(org.eclipse.smarthome.config.core.Configuration) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) ItemChannelLink(org.eclipse.smarthome.core.thing.link.ItemChannelLink) ManagedThingProvider(org.eclipse.smarthome.core.thing.ManagedThingProvider) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) Thing(org.eclipse.smarthome.core.thing.Thing) StateOption(org.eclipse.smarthome.core.types.StateOption) ThingRegistry(org.eclipse.smarthome.core.thing.ThingRegistry) StateDescription(org.eclipse.smarthome.core.types.StateDescription) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 68 with Configuration

use of org.eclipse.smarthome.config.core.Configuration in project smarthome by eclipse.

the class ThingManagerOSGiJavaTest method assertThingStatus.

private void assertThingStatus(Map<String, Object> propsThing, Map<String, Object> propsChannel, ThingStatus status, ThingStatusDetail statusDetail) {
    Configuration configThing = new Configuration(propsThing);
    Configuration configChannel = new Configuration(propsChannel);
    Thing thing = ThingBuilder.create(THING_TYPE_UID, THING_UID).withChannels(// 
    Collections.singletonList(ChannelBuilder.create(CHANNEL_UID, "Switch").withType(CHANNEL_TYPE_UID).withConfiguration(configChannel).build())).withConfiguration(configThing).build();
    managedThingProvider.add(thing);
    waitForAssert(() -> {
        assertEquals(status, thing.getStatus());
        assertEquals(statusDetail, thing.getStatusInfo().getStatusDetail());
    });
    managedThingProvider.remove(thing.getUID());
}
Also used : Configuration(org.eclipse.smarthome.config.core.Configuration) Thing(org.eclipse.smarthome.core.thing.Thing)

Example 69 with Configuration

use of org.eclipse.smarthome.config.core.Configuration in project smarthome by eclipse.

the class ThingRegistryOSGiTest method assertThatCreateThingDelegatesToRegisteredThingHandlerFactory.

@Test
public void assertThatCreateThingDelegatesToRegisteredThingHandlerFactory() {
    ThingTypeUID expectedThingTypeUID = THING_TYPE_UID;
    ThingUID expectedThingUID = new ThingUID(THING_TYPE_UID, THING1_ID);
    Configuration expectedConfiguration = new Configuration();
    ThingUID expectedBridgeUID = new ThingUID(THING_TYPE_UID, THING2_ID);
    String expectedLabel = "Test Thing";
    AsyncResultWrapper<Thing> thingResultWrapper = new AsyncResultWrapper<Thing>();
    ThingRegistry thingRegistry = getService(ThingRegistry.class);
    ThingHandlerFactory thingHandlerFactory = new BaseThingHandlerFactory() {

        @Override
        public boolean supportsThingType(@NonNull ThingTypeUID thingTypeUID) {
            return true;
        }

        @Override
        @Nullable
        protected ThingHandler createHandler(@NonNull Thing thing) {
            return null;
        }

        @Override
        @Nullable
        public Thing createThing(@NonNull ThingTypeUID thingTypeUID, @NonNull Configuration configuration, @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
            assertThat(thingTypeUID, is(expectedThingTypeUID));
            assertThat(configuration, is(expectedConfiguration));
            assertThat(thingUID, is(expectedThingUID));
            assertThat(bridgeUID, is(expectedBridgeUID));
            Thing thing = ThingBuilder.create(thingTypeUID, thingUID.getId()).withBridge(bridgeUID).build();
            thingResultWrapper.set(thing);
            return thing;
        }
    };
    registerThingHandlerFactory(thingHandlerFactory);
    Thing thing = thingRegistry.createThingOfType(expectedThingTypeUID, expectedThingUID, expectedBridgeUID, expectedLabel, expectedConfiguration);
    waitForAssert(() -> {
        assertTrue(thingResultWrapper.isSet());
    });
    assertThat(thing, is(thingResultWrapper.getWrappedObject()));
}
Also used : Configuration(org.eclipse.smarthome.config.core.Configuration) BaseThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory) AsyncResultWrapper(org.eclipse.smarthome.test.AsyncResultWrapper) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) NonNull(org.eclipse.jdt.annotation.NonNull) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) BaseThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory) ThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory) Thing(org.eclipse.smarthome.core.thing.Thing) Nullable(org.eclipse.jdt.annotation.Nullable) ThingRegistry(org.eclipse.smarthome.core.thing.ThingRegistry) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 70 with Configuration

use of org.eclipse.smarthome.config.core.Configuration in project smarthome by eclipse.

the class ThingPropertiesTest method setup.

@Before
public void setup() {
    ThingType thingType = ThingTypeBuilder.instance(new ThingTypeUID("bindingId", "thingTypeId"), "label").withProperties(properties).build();
    thing = ThingFactory.createThing(thingType, new ThingUID(thingType.getUID(), "thingId"), new Configuration());
}
Also used : Configuration(org.eclipse.smarthome.config.core.Configuration) ThingType(org.eclipse.smarthome.core.thing.type.ThingType) Before(org.junit.Before)

Aggregations

Configuration (org.eclipse.smarthome.config.core.Configuration)119 Test (org.junit.Test)27 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)19 ArrayList (java.util.ArrayList)18 Thing (org.eclipse.smarthome.core.thing.Thing)18 Action (org.eclipse.smarthome.automation.Action)16 Trigger (org.eclipse.smarthome.automation.Trigger)16 Rule (org.eclipse.smarthome.automation.Rule)15 Before (org.junit.Before)14 Condition (org.eclipse.smarthome.automation.Condition)10 Bridge (org.eclipse.smarthome.core.thing.Bridge)9 ApiOperation (io.swagger.annotations.ApiOperation)8 ApiResponses (io.swagger.annotations.ApiResponses)8 HashMap (java.util.HashMap)8 ChannelUID (org.eclipse.smarthome.core.thing.ChannelUID)8 Path (javax.ws.rs.Path)7 IOException (java.io.IOException)6 BigDecimal (java.math.BigDecimal)6 Consumes (javax.ws.rs.Consumes)6 RuleRegistry (org.eclipse.smarthome.automation.RuleRegistry)6