Search in sources :

Example 6 with ConfigDescriptionParameter

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

the class RuleEngine method isOptionalConfig.

private boolean isOptionalConfig(List<ConfigDescriptionParameter> configDescriptions) {
    if (configDescriptions != null && !configDescriptions.isEmpty()) {
        boolean required = false;
        Iterator<ConfigDescriptionParameter> i = configDescriptions.iterator();
        while (i.hasNext()) {
            ConfigDescriptionParameter param = i.next();
            required = required || param.isRequired();
        }
        return !required;
    }
    return true;
}
Also used : ConfigDescriptionParameter(org.eclipse.smarthome.config.core.ConfigDescriptionParameter)

Example 7 with ConfigDescriptionParameter

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

the class Printer method getConfigurationDescriptionRecords.

/**
 * This method is responsible for printing the set of {@link ConfigDescriptionParameter}s.
 *
 * @param configDescriptions set of {@link ConfigDescriptionParameter}s for printing.
 * @return a formated string, representing the set of {@link ConfigDescriptionParameter}s.
 */
private static List<String> getConfigurationDescriptionRecords(List<ConfigDescriptionParameter> configDescriptions) {
    List<String> configParamContent = new ArrayList<String>();
    if (configDescriptions != null && !configDescriptions.isEmpty()) {
        for (ConfigDescriptionParameter parameter : configDescriptions) {
            int[] columnWidths = new int[] { COLUMN_CONFIG_PARAMETER, COLUMN_CONFIG_PARAMETER_PROP, COLUMN_CONFIG_PARAMETER_PROP_VALUE };
            configParamContent.add(Utils.getColumn(COLUMN_PROPERTY_VALUE, parameter.getName() + " : "));
            List<String> configParamProperty = new ArrayList<String>();
            configParamProperty.add("");
            configParamProperty.add(TYPE);
            configParamProperty.add(parameter.getType().toString());
            configParamContent.add(Utils.getRow(columnWidths, configParamProperty));
            if (parameter.getLabel() != null) {
                configParamProperty.set(1, LABEL);
                configParamProperty.set(2, parameter.getLabel());
                configParamContent.add(Utils.getRow(columnWidths, configParamProperty));
            }
            if (parameter.getDescription() != null) {
                configParamProperty.set(1, DESCRIPTION);
                configParamProperty.set(2, parameter.getDescription());
                configParamContent.add(Utils.getRow(columnWidths, configParamProperty));
            }
            if (parameter.getDefault() != null) {
                configParamProperty.set(1, DEFAULT);
                configParamProperty.set(2, parameter.getDefault());
                configParamContent.add(Utils.getRow(columnWidths, configParamProperty));
            }
            if (parameter.getContext() != null) {
                configParamProperty.set(1, CONTEXT);
                configParamProperty.set(2, parameter.getContext());
                configParamContent.add(Utils.getRow(columnWidths, configParamProperty));
            }
            if (parameter.getPattern() != null) {
                configParamProperty.set(1, PATTERN);
                configParamProperty.set(2, parameter.getPattern());
                configParamContent.add(Utils.getRow(columnWidths, configParamProperty));
            }
            if (parameter.getStepSize() != null) {
                configParamProperty.set(1, STEP_SIZE);
                configParamProperty.set(2, parameter.getStepSize().toString());
                configParamContent.add(Utils.getRow(columnWidths, configParamProperty));
            }
            if (parameter.getMinimum() != null) {
                configParamProperty.set(1, MIN);
                configParamProperty.set(2, parameter.getMinimum().toString());
                configParamContent.add(Utils.getRow(columnWidths, configParamProperty));
            }
            if (parameter.getMaximum() != null) {
                configParamProperty.set(1, MAX);
                configParamProperty.set(2, parameter.getMaximum().toString());
                configParamContent.add(Utils.getRow(columnWidths, configParamProperty));
            }
            columnWidths = new int[] { COLUMN_CONFIG_PARAMETER_PROP, COLUMN_CONFIG_PARAMETER_PROP_VALUE };
            List<String> options = collectRecords(columnWidths, OPTIONS, parameter.getOptions());
            for (String option : options) {
                configParamContent.add(Utils.getColumn(COLUMN_CONFIG_PARAMETER, "") + option);
            }
            List<String> filters = collectRecords(columnWidths, FILTER_CRITERIA, parameter.getFilterCriteria());
            for (String filter : filters) {
                configParamContent.add(Utils.getColumn(COLUMN_CONFIG_PARAMETER, "") + filter);
            }
            configParamContent.add(Utils.getColumn(COLUMN_PROPERTY_VALUE, Utils.printChars('-', COLUMN_PROPERTY_VALUE)));
        }
    }
    return configParamContent;
}
Also used : ArrayList(java.util.ArrayList) ConfigDescriptionParameter(org.eclipse.smarthome.config.core.ConfigDescriptionParameter)

Example 8 with ConfigDescriptionParameter

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

the class ThingFactoryHelper method createChannelBuilder.

static ChannelBuilder createChannelBuilder(ChannelUID channelUID, ChannelType channelType, ConfigDescriptionRegistry configDescriptionRegistry) {
    ChannelBuilder channelBuilder = // 
    ChannelBuilder.create(channelUID, channelType.getItemType()).withType(// 
    channelType.getUID()).withDefaultTags(// 
    channelType.getTags()).withKind(// 
    channelType.getKind()).withLabel(channelType.getLabel());
    String description = channelType.getDescription();
    if (description != null) {
        channelBuilder = channelBuilder.withDescription(description);
    }
    // Initialize channel configuration with default-values
    URI channelConfigDescriptionURI = channelType.getConfigDescriptionURI();
    if (configDescriptionRegistry != null && channelConfigDescriptionURI != null) {
        ConfigDescription cd = configDescriptionRegistry.getConfigDescription(channelConfigDescriptionURI);
        if (cd != null) {
            Configuration config = new Configuration();
            for (ConfigDescriptionParameter param : cd.getParameters()) {
                String defaultValue = param.getDefault();
                if (defaultValue != null) {
                    Object value = getDefaultValueAsCorrectType(param.getType(), defaultValue);
                    if (value != null) {
                        config.put(param.getName(), value);
                    }
                }
            }
            channelBuilder = channelBuilder.withConfiguration(config);
        }
    }
    return channelBuilder;
}
Also used : Configuration(org.eclipse.smarthome.config.core.Configuration) ConfigDescription(org.eclipse.smarthome.config.core.ConfigDescription) ConfigDescriptionParameter(org.eclipse.smarthome.config.core.ConfigDescriptionParameter) ChannelBuilder(org.eclipse.smarthome.core.thing.binding.builder.ChannelBuilder) URI(java.net.URI)

Example 9 with ConfigDescriptionParameter

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

the class ThingManagerOSGiJavaTest method testInitializeOnlyIfInitializable.

@Test
public void testInitializeOnlyIfInitializable() throws Exception {
    registerThingTypeProvider();
    registerChannelTypeProvider();
    registerThingHandlerFactory(THING_TYPE_UID, thing -> new BaseThingHandler(thing) {

        @Override
        public void handleCommand(@NonNull ChannelUID channelUID, @NonNull Command command) {
        }
    });
    ConfigDescriptionProvider mockConfigDescriptionProvider = mock(ConfigDescriptionProvider.class);
    List<ConfigDescriptionParameter> parameters = // 
    Collections.singletonList(// 
    ConfigDescriptionParameterBuilder.create(CONFIG_PARAM_NAME, Type.TEXT).withRequired(true).build());
    registerService(mockConfigDescriptionProvider, ConfigDescriptionProvider.class.getName());
    // verify a missing mandatory thing config prevents it from getting initialized
    when(mockConfigDescriptionProvider.getConfigDescription(eq(CONFIG_DESCRIPTION_THING), any())).thenReturn(new ConfigDescription(CONFIG_DESCRIPTION_THING, parameters));
    assertThingStatus(Collections.emptyMap(), Collections.emptyMap(), ThingStatus.UNINITIALIZED, ThingStatusDetail.HANDLER_CONFIGURATION_PENDING);
    // verify a missing mandatory channel config prevents it from getting initialized
    when(mockConfigDescriptionProvider.getConfigDescription(eq(CONFIG_DESCRIPTION_CHANNEL), any())).thenReturn(new ConfigDescription(CONFIG_DESCRIPTION_CHANNEL, parameters));
    assertThingStatus(Collections.singletonMap(CONFIG_PARAM_NAME, "value"), Collections.emptyMap(), ThingStatus.UNINITIALIZED, ThingStatusDetail.HANDLER_CONFIGURATION_PENDING);
    // verify a satisfied config does not prevent it from getting initialized anymore
    assertThingStatus(Collections.singletonMap(CONFIG_PARAM_NAME, "value"), Collections.singletonMap(CONFIG_PARAM_NAME, "value"), ThingStatus.ONLINE, ThingStatusDetail.NONE);
}
Also used : BaseThingHandler(org.eclipse.smarthome.core.thing.binding.BaseThingHandler) Command(org.eclipse.smarthome.core.types.Command) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) ConfigDescription(org.eclipse.smarthome.config.core.ConfigDescription) ConfigDescriptionParameter(org.eclipse.smarthome.config.core.ConfigDescriptionParameter) ConfigDescriptionProvider(org.eclipse.smarthome.config.core.ConfigDescriptionProvider) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 10 with ConfigDescriptionParameter

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

the class ConfigDescriptionsTest method ConfigDescriptionsShouldLoadProperly.

@Test
public void ConfigDescriptionsShouldLoadProperly() throws Exception {
    int initialNumberOfConfigDescriptions = configDescriptionRegistry.getConfigDescriptions().size();
    // install test bundle
    Bundle bundle = SyntheticBundleInstaller.install(bundleContext, TEST_BUNDLE_NAME);
    assertThat(bundle, is(notNullValue()));
    Collection<ConfigDescription> configDescriptions = configDescriptionRegistry.getConfigDescriptions();
    assertThat(configDescriptions.size(), is(initialNumberOfConfigDescriptions + 3));
    URI bridgeURI = new URI("thing-type:hue:bridge");
    ConfigDescription bridgeConfigDescription = configDescriptions.stream().filter(it -> it.getUID().equals(bridgeURI)).findFirst().get();
    assertThat(bridgeConfigDescription, is(notNullValue()));
    Collection<ConfigDescriptionParameter> parameters = bridgeConfigDescription.getParameters();
    assertThat(parameters.size(), is(2));
    ConfigDescriptionParameter ipParameter = parameters.stream().filter(it -> it.getName().equals("ip")).findFirst().get();
    assertThat(ipParameter, is(notNullValue()));
    assertThat(ipParameter.getType(), is(Type.TEXT));
    assertThat(ipParameter.getContext(), is("network-address"));
    assertThat(ipParameter.getLabel(), is("Network Address"));
    assertThat(ipParameter.getDescription(), is("Network address of the hue bridge."));
    assertThat(ipParameter.isRequired(), is(true));
    ConfigDescriptionParameter userNameParameter = parameters.stream().filter(it -> it.getName().equals("username")).findFirst().get();
    assertThat(userNameParameter, is(notNullValue()));
    assertThat(userNameParameter.isAdvanced(), is(true));
    assertThat(userNameParameter.getType(), is(Type.TEXT));
    assertThat(userNameParameter.getContext(), is("password"));
    assertThat(userNameParameter.getLabel(), is("Username"));
    assertThat(userNameParameter.getDescription(), is("Name of a registered hue bridge user, that allows to access the API."));
    URI colorURI = new URI("channel-type:hue:color");
    ConfigDescription colorConfigDescription = configDescriptions.stream().filter(it -> it.getUID().equals(colorURI)).findFirst().get();
    assertThat(colorConfigDescription, is(notNullValue()));
    parameters = colorConfigDescription.getParameters();
    assertThat(parameters.size(), is(1));
    ConfigDescriptionParameter lastDimValueParameter = parameters.stream().filter(it -> it.getName().equals("lastDimValue")).findFirst().get();
    assertThat(lastDimValueParameter, is(notNullValue()));
    assertThat(lastDimValueParameter.getType(), is(Type.BOOLEAN));
    Collection<ConfigDescriptionParameterGroup> groups = bridgeConfigDescription.getParameterGroups();
    assertThat(groups.size(), is(2));
    ConfigDescriptionParameterGroup group1 = groups.stream().filter(it -> it.getName().equals("group1")).findFirst().get();
    assertThat(group1, is(notNullValue()));
    assertThat(group1.getContext(), is("Group1-context"));
    assertThat(group1.getLabel(), is("Group Label 1"));
    assertThat(group1.getDescription(), is("Group description 1"));
    // uninstall test bundle
    bundle.uninstall();
    assertThat(bundle.getState(), is(Bundle.UNINSTALLED));
}
Also used : ConfigDescriptionParameterGroup(org.eclipse.smarthome.config.core.ConfigDescriptionParameterGroup) Bundle(org.osgi.framework.Bundle) ConfigDescription(org.eclipse.smarthome.config.core.ConfigDescription) ConfigDescriptionParameter(org.eclipse.smarthome.config.core.ConfigDescriptionParameter) URI(java.net.URI) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Aggregations

ConfigDescriptionParameter (org.eclipse.smarthome.config.core.ConfigDescriptionParameter)29 ArrayList (java.util.ArrayList)13 ConfigDescription (org.eclipse.smarthome.config.core.ConfigDescription)9 URI (java.net.URI)7 Configuration (org.eclipse.smarthome.config.core.Configuration)5 ParameterOption (org.eclipse.smarthome.config.core.ParameterOption)4 Test (org.junit.Test)4 Bundle (org.osgi.framework.Bundle)4 BigDecimal (java.math.BigDecimal)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Action (org.eclipse.smarthome.automation.Action)3 Condition (org.eclipse.smarthome.automation.Condition)3 Trigger (org.eclipse.smarthome.automation.Trigger)3 ConfigDescriptionParameterGroup (org.eclipse.smarthome.config.core.ConfigDescriptionParameterGroup)3 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)3 URISyntaxException (java.net.URISyntaxException)2 HashSet (java.util.HashSet)2 Rule (org.eclipse.smarthome.automation.Rule)2 Input (org.eclipse.smarthome.automation.type.Input)2