Search in sources :

Example 1 with NodeAttributes

use of org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.NodeAttributes 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 NodeAttributes

use of org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.NodeAttributes in project smarthome by eclipse.

the class HomieThingHandlerTests method handleCommandRefresh.

@SuppressWarnings("null")
@Test
public void handleCommandRefresh() {
    // Create mocked homie device tree with one node and one read-only property
    Node node = thingHandler.device.createNode("node", spy(new NodeAttributes()));
    doReturn(future).when(node.attributes).subscribeAndReceive(any(), any(), anyString(), any(), anyInt());
    doReturn(future).when(node.attributes).unsubscribe();
    node.attributes.name = "testnode";
    Property property = node.createProperty("property", spy(new PropertyAttributes()));
    doReturn(future).when(property.attributes).subscribeAndReceive(any(), any(), anyString(), any(), anyInt());
    doReturn(future).when(property.attributes).unsubscribe();
    property.attributes.name = "testprop";
    property.attributes.datatype = DataTypeEnum.string_;
    property.attributes.settable = false;
    property.attributesReceived();
    node.properties.put(property.propertyID, property);
    thingHandler.device.nodes.put(node.nodeID, node);
    thingHandler.connection = connection;
    thingHandler.handleCommand(property.channelUID, RefreshType.REFRESH);
    verify(callback).stateUpdated(argThat(arg -> property.channelUID.equals(arg)), argThat(arg -> property.getChannelState().getCache().getChannelState().equals(arg)));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Channel(org.eclipse.smarthome.core.thing.Channel) ArgumentMatchers(org.mockito.ArgumentMatchers) ScheduledFuture(java.util.concurrent.ScheduledFuture) ChannelState(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelState) Command(org.eclipse.smarthome.core.types.Command) MockitoAnnotations(org.mockito.MockitoAnnotations) Nullable(org.eclipse.jdt.annotation.Nullable) Map(java.util.Map) AbstractMqttAttributeClass(org.eclipse.smarthome.binding.mqtt.generic.internal.mapping.AbstractMqttAttributeClass) Thing(org.eclipse.smarthome.core.thing.Thing) StringType(org.eclipse.smarthome.core.library.types.StringType) MqttBindingConstants(org.eclipse.smarthome.binding.mqtt.generic.internal.MqttBindingConstants) Device(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Device) MqttChannelTypeProvider(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.MqttChannelTypeProvider) Configuration(org.eclipse.smarthome.config.core.Configuration) ChannelStateHelper(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelStateHelper) DeviceAttributes(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.DeviceAttributes) ReadyState(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.DeviceAttributes.ReadyState) MqttBrokerConnection(org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnection) RefreshType(org.eclipse.smarthome.core.types.RefreshType) List(java.util.List) NonNull(org.eclipse.jdt.annotation.NonNull) ChildMap(org.eclipse.smarthome.binding.mqtt.generic.internal.tools.ChildMap) ThingStatus(org.eclipse.smarthome.core.thing.ThingStatus) Mock(org.mockito.Mock) TypeParser(org.eclipse.smarthome.core.types.TypeParser) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) AbstractBrokerHandler(org.eclipse.smarthome.binding.mqtt.handler.AbstractBrokerHandler) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ChannelKind(org.eclipse.smarthome.core.thing.type.ChannelKind) NodeAttributes(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.NodeAttributes) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ThingChannelConstants.testHomieThing(org.eclipse.smarthome.binding.mqtt.generic.internal.handler.ThingChannelConstants.testHomieThing) Before(org.junit.Before) ThingHandlerCallback(org.eclipse.smarthome.core.thing.binding.ThingHandlerCallback) Value(org.eclipse.smarthome.binding.mqtt.generic.internal.values.Value) DelayedBatchProcessing(org.eclipse.smarthome.binding.mqtt.generic.internal.tools.DelayedBatchProcessing) Property(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Property) ThingStatusInfo(org.eclipse.smarthome.core.thing.ThingStatusInfo) Test(org.junit.Test) SubscribeFieldToMQTTtopic(org.eclipse.smarthome.binding.mqtt.generic.internal.mapping.SubscribeFieldToMQTTtopic) Field(java.lang.reflect.Field) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) PropertyAttributes(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.PropertyAttributes) Mockito(org.mockito.Mockito) Node(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Node) ThingBuilder(org.eclipse.smarthome.core.thing.binding.builder.ThingBuilder) ThingStatusDetail(org.eclipse.smarthome.core.thing.ThingStatusDetail) DataTypeEnum(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.PropertyAttributes.DataTypeEnum) Assert(org.junit.Assert) PropertyAttributes(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.PropertyAttributes) Node(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Node) NodeAttributes(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.NodeAttributes) Property(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Property) Test(org.junit.Test)

Example 3 with NodeAttributes

use of org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.NodeAttributes in project smarthome by eclipse.

the class ChildMapTests method createNode.

private Node createNode(String id) {
    Node node = new Node(deviceTopic, id, ThingChannelConstants.testHomieThing, callback, spy(new NodeAttributes()));
    doReturn(future).when(node.attributes).subscribeAndReceive(any(), any(), anyString(), any(), anyInt());
    doReturn(future).when(node.attributes).unsubscribe();
    return node;
}
Also used : Node(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Node) NodeAttributes(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.NodeAttributes)

Example 4 with NodeAttributes

use of org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.NodeAttributes in project smarthome by eclipse.

the class HomieImplementationTests method createSpyNode.

// Inject a spy'ed node
public Node createSpyNode(InvocationOnMock invocation) {
    final Device device = (Device) invocation.getMock();
    final String id = (String) invocation.getArguments()[0];
    // Create the node
    Node node = spy(device.createNode(id, spy(new NodeAttributes())));
    // Intercept creating a property in the next call and inject a spy'ed property.
    doAnswer(this::createSpyProperty).when(node).createProperty(any());
    return node;
}
Also used : Device(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Device) Node(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Node) NodeAttributes(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.NodeAttributes)

Example 5 with NodeAttributes

use of org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.NodeAttributes in project smarthome by eclipse.

the class HomieThingHandlerTests method handleCommandUpdate.

@SuppressWarnings("null")
@Test
public void handleCommandUpdate() {
    // Create mocked homie device tree with one node and one writable property
    Node node = thingHandler.device.createNode("node", spy(new NodeAttributes()));
    doReturn(future).when(node.attributes).subscribeAndReceive(any(), any(), anyString(), any(), anyInt());
    doReturn(future).when(node.attributes).unsubscribe();
    node.attributes.name = "testnode";
    Property property = node.createProperty("property", spy(new PropertyAttributes()));
    doReturn(future).when(property.attributes).subscribeAndReceive(any(), any(), anyString(), any(), anyInt());
    doReturn(future).when(property.attributes).unsubscribe();
    property.attributes.name = "testprop";
    property.attributes.datatype = DataTypeEnum.string_;
    property.attributes.settable = true;
    property.attributesReceived();
    node.properties.put(property.propertyID, property);
    thingHandler.device.nodes.put(node.nodeID, node);
    ChannelState channelState = property.getChannelState();
    assertNotNull(channelState);
    // Pretend we called start()
    ChannelStateHelper.setConnection(channelState, connection);
    thingHandler.connection = connection;
    StringType updateValue = new StringType("UPDATE");
    thingHandler.handleCommand(property.channelUID, updateValue);
    assertThat(property.getChannelState().getCache().getChannelState().toString(), is("UPDATE"));
    verify(connection, times(1)).publish(any(), any(), anyInt(), anyBoolean());
    // Check non writable property
    property.attributes.settable = false;
    property.attributesReceived();
    // Assign old value
    Value value = property.getChannelState().getCache();
    Command command = TypeParser.parseCommand(value.getSupportedCommandTypes(), "OLDVALUE");
    property.getChannelState().getCache().update(command);
    // Try to update with new value
    updateValue = new StringType("SOMETHINGNEW");
    thingHandler.handleCommand(property.channelUID, updateValue);
    // Expect old value and no MQTT publish
    assertThat(property.getChannelState().getCache().getChannelState().toString(), is("OLDVALUE"));
    verify(connection, times(1)).publish(any(), any(), anyInt(), anyBoolean());
}
Also used : ChannelState(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelState) PropertyAttributes(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.PropertyAttributes) StringType(org.eclipse.smarthome.core.library.types.StringType) Command(org.eclipse.smarthome.core.types.Command) Node(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Node) NodeAttributes(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.NodeAttributes) Value(org.eclipse.smarthome.binding.mqtt.generic.internal.values.Value) Property(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Property) Test(org.junit.Test)

Aggregations

Node (org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Node)6 NodeAttributes (org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.NodeAttributes)6 Property (org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Property)3 PropertyAttributes (org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.PropertyAttributes)3 ChannelState (org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelState)3 Test (org.junit.Test)3 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 Device (org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Device)2 Value (org.eclipse.smarthome.binding.mqtt.generic.internal.values.Value)2 StringType (org.eclipse.smarthome.core.library.types.StringType)2 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 ScheduledFuture (java.util.concurrent.ScheduledFuture)1 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)1 TimeUnit (java.util.concurrent.TimeUnit)1 NonNull (org.eclipse.jdt.annotation.NonNull)1