Search in sources :

Example 1 with ChannelState

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

the class HomieImplementationTests method retrieveAttributes.

@SuppressWarnings("null")
@Test
public void retrieveAttributes() throws InterruptedException, ExecutionException {
    assertThat(connection.hasSubscribers(), is(false));
    Node node = new Node(deviceTopic, "testnode", ThingChannelConstants.testHomieThing, callback, new NodeAttributes());
    Property property = spy(new Property(deviceTopic + "/testnode", node, "temperature", callback, new PropertyAttributes()));
    // Create a scheduler
    ScheduledExecutorService scheduler = new ScheduledThreadPoolExecutor(4);
    property.subscribe(connection, scheduler, 100).get();
    assertThat(property.attributes.settable, is(true));
    assertThat(property.attributes.retained, is(true));
    assertThat(property.attributes.name, is("Testprop"));
    assertThat(property.attributes.unit, is("°C"));
    assertThat(property.attributes.datatype, is(DataTypeEnum.float_));
    assertThat(property.attributes.format, is("-100:100"));
    verify(property).attributesReceived();
    // Receive property value
    ChannelState channelState = spy(property.getChannelState());
    PropertyHelper.setChannelState(property, channelState);
    property.startChannel(connection, scheduler, 200).get();
    verify(channelState).start(any(), any(), anyInt());
    verify(channelState).processMessage(any(), any());
    verify(callback).updateChannelState(any(), any());
    assertThat(property.getChannelState().getCache().getChannelState(), is(new DecimalType(10)));
    property.stop().get();
    assertThat(connection.hasSubscribers(), is(false));
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ChannelState(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelState) PropertyAttributes(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.PropertyAttributes) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) Node(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Node) NodeAttributes(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.NodeAttributes) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) Property(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Property) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 2 with ChannelState

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

the class GenericThingHandlerTests method handleCommandUpdateBoolean.

@Test
public void handleCommandUpdateBoolean() {
    OnOffValue value = spy(new OnOffValue("ON", "OFF"));
    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("ON");
    thingHandler.handleCommand(textChannelUID, updateValue);
    verify(value).update(eq(updateValue));
    assertThat(channelConfig.getCache().getChannelState(), is(OnOffType.ON));
}
Also used : ChannelState(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelState) OnOffValue(org.eclipse.smarthome.binding.mqtt.generic.internal.values.OnOffValue) StringType(org.eclipse.smarthome.core.library.types.StringType) Test(org.junit.Test)

Example 3 with ChannelState

use of org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelState 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 4 with ChannelState

use of org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelState 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 5 with ChannelState

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

Aggregations

ChannelState (org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelState)12 Test (org.junit.Test)7 CompletableFuture (java.util.concurrent.CompletableFuture)6 TextValue (org.eclipse.smarthome.binding.mqtt.generic.internal.values.TextValue)5 StringType (org.eclipse.smarthome.core.library.types.StringType)5 ChannelUID (org.eclipse.smarthome.core.thing.ChannelUID)5 Thing (org.eclipse.smarthome.core.thing.Thing)5 ThingStatus (org.eclipse.smarthome.core.thing.ThingStatus)5 ThingStatusDetail (org.eclipse.smarthome.core.thing.ThingStatusDetail)5 MqttBrokerConnection (org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnection)5 ChannelConfig (org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelConfig)4 MqttChannelStateDescriptionProvider (org.eclipse.smarthome.binding.mqtt.generic.internal.generic.MqttChannelStateDescriptionProvider)4 TransformationServiceProvider (org.eclipse.smarthome.binding.mqtt.generic.internal.generic.TransformationServiceProvider)4 OnOffValue (org.eclipse.smarthome.binding.mqtt.generic.internal.values.OnOffValue)4 ValueFactory (org.eclipse.smarthome.binding.mqtt.generic.internal.values.ValueFactory)4 ExecutionException (java.util.concurrent.ExecutionException)3 TimeUnit (java.util.concurrent.TimeUnit)3 TimeoutException (java.util.concurrent.TimeoutException)3 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)3 Nullable (org.eclipse.jdt.annotation.Nullable)3