Search in sources :

Example 1 with TextValue

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

the class GenericThingHandlerTests method handleCommandRefresh.

@Test
public void handleCommandRefresh() {
    ChannelState channelConfig = mock(ChannelState.class);
    doReturn(CompletableFuture.completedFuture(true)).when(channelConfig).start(any(), any(), anyInt());
    doReturn(CompletableFuture.completedFuture(true)).when(channelConfig).stop();
    doReturn(channelConfig).when(thingHandler).createChannelState(any(), any(), any());
    thingHandler.initialize();
    TextValue value = spy(new TextValue());
    doReturn(value).when(channelConfig).getCache();
    thingHandler.connection = connection;
    thingHandler.handleCommand(textChannelUID, RefreshType.REFRESH);
    verify(value).getChannelState();
}
Also used : ChannelState(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelState) TextValue(org.eclipse.smarthome.binding.mqtt.generic.internal.values.TextValue) Test(org.junit.Test)

Example 2 with TextValue

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

the class GenericThingHandlerTests method handleCommandUpdateString.

@Test
public void handleCommandUpdateString() {
    TextValue value = spy(new TextValue());
    ChannelState channelConfig = spy(new ChannelState(ChannelConfigBuilder.create("stateTopic", "commandTopic").build(), textChannelUID, value, thingHandler));
    doReturn(channelConfig).when(thingHandler).createChannelState(any(), any(), any());
    thingHandler.initialize();
    thingHandler.connection = connection;
    StringType updateValue = new StringType("UPDATE");
    thingHandler.handleCommand(textChannelUID, updateValue);
    verify(value).update(eq(updateValue));
    assertThat(channelConfig.getCache().getChannelState().toString(), is("UPDATE"));
}
Also used : ChannelState(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelState) StringType(org.eclipse.smarthome.core.library.types.StringType) TextValue(org.eclipse.smarthome.binding.mqtt.generic.internal.values.TextValue) Test(org.junit.Test)

Example 3 with TextValue

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

the class GenericThingHandlerTests method processMessage.

@Test
public void processMessage() {
    TextValue textValue = new TextValue();
    ChannelState channelConfig = spy(new ChannelState(ChannelConfigBuilder.create("test/state", "test/state/set").build(), textChannelUID, textValue, thingHandler));
    doReturn(channelConfig).when(thingHandler).createChannelState(any(), any(), any());
    thingHandler.initialize();
    byte[] payload = "UPDATE".getBytes();
    // Test process message
    channelConfig.processMessage("test/state", payload);
    verify(callback).statusUpdated(eq(thing), argThat(arg -> arg.getStatus().equals(ThingStatus.ONLINE)));
    verify(callback).stateUpdated(eq(textChannelUID), argThat(arg -> "UPDATE".equals(arg.toString())));
    assertThat(textValue.getChannelState().toString(), is("UPDATE"));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) ArgumentMatchers(org.mockito.ArgumentMatchers) Mock(org.mockito.Mock) MqttChannelStateDescriptionProvider(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.MqttChannelStateDescriptionProvider) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) CompletableFuture(java.util.concurrent.CompletableFuture) ChannelState(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelState) TransformationServiceProvider(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.TransformationServiceProvider) ChannelConfig(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelConfig) Assert.assertThat(org.junit.Assert.assertThat) AbstractBrokerHandler(org.eclipse.smarthome.binding.mqtt.handler.AbstractBrokerHandler) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) MockitoAnnotations(org.mockito.MockitoAnnotations) Thing(org.eclipse.smarthome.core.thing.Thing) ChannelConfigBuilder(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelConfigBuilder) StringType(org.eclipse.smarthome.core.library.types.StringType) Configuration(org.eclipse.smarthome.config.core.Configuration) Before(org.junit.Before) ThingHandlerCallback(org.eclipse.smarthome.core.thing.binding.ThingHandlerCallback) ThingChannelConstants(org.eclipse.smarthome.binding.mqtt.generic.internal.handler.ThingChannelConstants) ValueFactory(org.eclipse.smarthome.binding.mqtt.generic.internal.values.ValueFactory) MqttBrokerConnection(org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnection) OnOffValue(org.eclipse.smarthome.binding.mqtt.generic.internal.values.OnOffValue) RefreshType(org.eclipse.smarthome.core.types.RefreshType) ThingStatusInfo(org.eclipse.smarthome.core.thing.ThingStatusInfo) Test(org.junit.Test) Mockito(org.mockito.Mockito) TextValue(org.eclipse.smarthome.binding.mqtt.generic.internal.values.TextValue) ThingStatusDetail(org.eclipse.smarthome.core.thing.ThingStatusDetail) ThingStatus(org.eclipse.smarthome.core.thing.ThingStatus) ChannelState(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelState) TextValue(org.eclipse.smarthome.binding.mqtt.generic.internal.values.TextValue) Test(org.junit.Test)

Example 4 with TextValue

use of org.eclipse.smarthome.binding.mqtt.generic.internal.values.TextValue 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

ChannelState (org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelState)4 TextValue (org.eclipse.smarthome.binding.mqtt.generic.internal.values.TextValue)4 Test (org.junit.Test)3 ChannelConfigBuilder (org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelConfigBuilder)2 OnOffValue (org.eclipse.smarthome.binding.mqtt.generic.internal.values.OnOffValue)2 Configuration (org.eclipse.smarthome.config.core.Configuration)2 StringType (org.eclipse.smarthome.core.library.types.StringType)2 BigDecimal (java.math.BigDecimal)1 MathContext (java.math.MathContext)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ChannelConfig (org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelConfig)1 MqttChannelStateDescriptionProvider (org.eclipse.smarthome.binding.mqtt.generic.internal.generic.MqttChannelStateDescriptionProvider)1 TransformationServiceProvider (org.eclipse.smarthome.binding.mqtt.generic.internal.generic.TransformationServiceProvider)1 ThingChannelConstants (org.eclipse.smarthome.binding.mqtt.generic.internal.handler.ThingChannelConstants)1 ColorValue (org.eclipse.smarthome.binding.mqtt.generic.internal.values.ColorValue)1 NumberValue (org.eclipse.smarthome.binding.mqtt.generic.internal.values.NumberValue)1 Value (org.eclipse.smarthome.binding.mqtt.generic.internal.values.Value)1 ValueFactory (org.eclipse.smarthome.binding.mqtt.generic.internal.values.ValueFactory)1 AbstractBrokerHandler (org.eclipse.smarthome.binding.mqtt.handler.AbstractBrokerHandler)1 OnOffType (org.eclipse.smarthome.core.library.types.OnOffType)1