Search in sources :

Example 1 with ColorValue

use of org.eclipse.smarthome.binding.mqtt.generic.internal.values.ColorValue in project smarthome by eclipse.

the class ChannelStateTests method receiveRGBColorTest.

@Test
public void receiveRGBColorTest() throws InterruptedException, ExecutionException, TimeoutException {
    ColorValue value = new ColorValue(true, "FON", "FOFF", 10);
    ChannelState c = spy(new ChannelState(config, channelUID, value, channelStateUpdateListener));
    c.start(connection, mock(ScheduledExecutorService.class), 100);
    // Normal on state
    c.processMessage("state", "ON".getBytes());
    assertThat(value.getChannelState().toString(), is("0,0,10"));
    assertThat(value.getMQTTpublishValue().toString(), is("25,25,25"));
    // Custom off state
    c.processMessage("state", "FOFF".getBytes());
    assertThat(value.getChannelState().toString(), is("0,0,0"));
    assertThat(value.getMQTTpublishValue().toString(), is("0,0,0"));
    // Brightness only
    c.processMessage("state", "10".getBytes());
    assertThat(value.getChannelState().toString(), is("0,0,10"));
    assertThat(value.getMQTTpublishValue().toString(), is("25,25,25"));
    HSBType t = HSBType.fromRGB(12, 18, 231);
    c.processMessage("state", "12,18,231".getBytes());
    // HSB
    assertThat(value.getChannelState(), is(t));
    // rgb -> hsv -> rgb is quite lossy
    assertThat(value.getMQTTpublishValue().toString(), is("13,20,225"));
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ColorValue(org.eclipse.smarthome.binding.mqtt.generic.internal.values.ColorValue) HSBType(org.eclipse.smarthome.core.library.types.HSBType) Test(org.junit.Test)

Example 2 with ColorValue

use of org.eclipse.smarthome.binding.mqtt.generic.internal.values.ColorValue in project smarthome by eclipse.

the class ChannelStateTests method receiveHSBColorTest.

@Test
public void receiveHSBColorTest() throws InterruptedException, ExecutionException, TimeoutException {
    ColorValue value = new ColorValue(false, "FON", "FOFF", 10);
    ChannelState c = spy(new ChannelState(config, channelUID, value, channelStateUpdateListener));
    c.start(connection, mock(ScheduledExecutorService.class), 100);
    // Normal on state
    c.processMessage("state", "ON".getBytes());
    assertThat(value.getChannelState().toString(), is("0,0,10"));
    assertThat(value.getMQTTpublishValue().toString(), is("0,0,10"));
    // Custom off state
    c.processMessage("state", "FOFF".getBytes());
    assertThat(value.getChannelState().toString(), is("0,0,0"));
    assertThat(value.getMQTTpublishValue().toString(), is("0,0,0"));
    // Brightness only
    c.processMessage("state", "10".getBytes());
    assertThat(value.getChannelState().toString(), is("0,0,10"));
    assertThat(value.getMQTTpublishValue().toString(), is("0,0,10"));
    c.processMessage("state", "12,18,100".getBytes());
    assertThat(value.getChannelState().toString(), is("12,18,100"));
    assertThat(value.getMQTTpublishValue().toString(), is("12,18,100"));
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ColorValue(org.eclipse.smarthome.binding.mqtt.generic.internal.values.ColorValue) Test(org.junit.Test)

Example 3 with ColorValue

use of org.eclipse.smarthome.binding.mqtt.generic.internal.values.ColorValue in project smarthome by eclipse.

the class Property method createChannelFromAttribute.

public void createChannelFromAttribute() {
    final String commandTopic = topic + "/set";
    final String stateTopic = topic;
    Value value;
    Boolean isDecimal = null;
    switch(attributes.datatype) {
        case boolean_:
            value = new OnOffValue("true", "false");
            break;
        case color_:
            value = new ColorValue(attributes.format.contains("rgb"), null, null, 100);
            break;
        case enum_:
            String[] enumValues = attributes.format.split(",");
            value = new TextValue(enumValues);
            break;
        case float_:
        case integer_:
            isDecimal = attributes.datatype == DataTypeEnum.float_;
            String[] s = attributes.format.split("\\:");
            BigDecimal min = s.length == 2 ? convertFromString(s[0]) : null;
            BigDecimal max = s.length == 2 ? convertFromString(s[1]) : null;
            BigDecimal step = (min != null && max != null) ? max.subtract(min).divide(new BigDecimal(100.0), new MathContext(isDecimal ? 2 : 0)) : null;
            if (step != null && !isDecimal && step.intValue() <= 0) {
                step = new BigDecimal(1);
            }
            value = new NumberValue(min, max, step);
            break;
        case string_:
        case unknown:
        default:
            value = new TextValue();
            break;
    }
    ChannelConfigBuilder b = ChannelConfigBuilder.create().makeTrigger(!attributes.retained).withStateTopic(stateTopic);
    if (isDecimal != null && !isDecimal) {
        // Apply formatter to only publish integers
        b = b.withFormatter("%d");
    }
    if (attributes.settable) {
        b = b.withCommandTopic(commandTopic);
    }
    final ChannelState channelState = new ChannelState(b.build(), channelUID, value, callback);
    this.channelState = channelState;
    final ChannelType type = createChannelType(attributes, channelState);
    this.type = type;
    this.channel = ChannelBuilder.create(channelUID, type.getItemType()).withType(type.getUID()).withKind(type.getKind()).withLabel(attributes.name).withConfiguration(new Configuration(attributes.asMap())).build();
}
Also used : ChannelState(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelState) OnOffValue(org.eclipse.smarthome.binding.mqtt.generic.internal.values.OnOffValue) Configuration(org.eclipse.smarthome.config.core.Configuration) ColorValue(org.eclipse.smarthome.binding.mqtt.generic.internal.values.ColorValue) BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext) ChannelConfigBuilder(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelConfigBuilder) NumberValue(org.eclipse.smarthome.binding.mqtt.generic.internal.values.NumberValue) TextValue(org.eclipse.smarthome.binding.mqtt.generic.internal.values.TextValue) NumberValue(org.eclipse.smarthome.binding.mqtt.generic.internal.values.NumberValue) ColorValue(org.eclipse.smarthome.binding.mqtt.generic.internal.values.ColorValue) Value(org.eclipse.smarthome.binding.mqtt.generic.internal.values.Value) OnOffValue(org.eclipse.smarthome.binding.mqtt.generic.internal.values.OnOffValue) TextValue(org.eclipse.smarthome.binding.mqtt.generic.internal.values.TextValue) ChannelType(org.eclipse.smarthome.core.thing.type.ChannelType)

Aggregations

ColorValue (org.eclipse.smarthome.binding.mqtt.generic.internal.values.ColorValue)3 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 Test (org.junit.Test)2 BigDecimal (java.math.BigDecimal)1 MathContext (java.math.MathContext)1 ChannelConfigBuilder (org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelConfigBuilder)1 ChannelState (org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelState)1 NumberValue (org.eclipse.smarthome.binding.mqtt.generic.internal.values.NumberValue)1 OnOffValue (org.eclipse.smarthome.binding.mqtt.generic.internal.values.OnOffValue)1 TextValue (org.eclipse.smarthome.binding.mqtt.generic.internal.values.TextValue)1 Value (org.eclipse.smarthome.binding.mqtt.generic.internal.values.Value)1 Configuration (org.eclipse.smarthome.config.core.Configuration)1 HSBType (org.eclipse.smarthome.core.library.types.HSBType)1 ChannelType (org.eclipse.smarthome.core.thing.type.ChannelType)1