Search in sources :

Example 1 with PropertyAttributes

use of org.openhab.binding.mqtt.homie.internal.homie300.PropertyAttributes in project openhab-addons by openhab.

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 = requireNonNull(property.getChannelState());
    assertNotNull(channelState);
    // Pretend we called start()
    ChannelStateHelper.setConnection(channelState, connection);
    ThingHandlerHelper.setConnection(thingHandler, 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");
    if (command != null) {
        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.openhab.binding.mqtt.generic.ChannelState) PropertyAttributes(org.openhab.binding.mqtt.homie.internal.homie300.PropertyAttributes) StringType(org.openhab.core.library.types.StringType) Command(org.openhab.core.types.Command) Node(org.openhab.binding.mqtt.homie.internal.homie300.Node) NodeAttributes(org.openhab.binding.mqtt.homie.internal.homie300.NodeAttributes) Value(org.openhab.binding.mqtt.generic.values.Value) Property(org.openhab.binding.mqtt.homie.internal.homie300.Property) Test(org.junit.jupiter.api.Test)

Example 2 with PropertyAttributes

use of org.openhab.binding.mqtt.homie.internal.homie300.PropertyAttributes 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 3 with PropertyAttributes

use of org.openhab.binding.mqtt.homie.internal.homie300.PropertyAttributes 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 4 with PropertyAttributes

use of org.openhab.binding.mqtt.homie.internal.homie300.PropertyAttributes in project smarthome by eclipse.

the class HomieThingHandlerTests method createSpyProperty.

public Property createSpyProperty(String propertyID, Node node) {
    // Create a property with the same ID and insert it instead
    Property property = spy(node.createProperty(propertyID, spy(new PropertyAttributes())));
    doAnswer(this::createSubscriberAnswer).when(property.attributes).createSubscriber(any(), any(), any(), anyBoolean());
    property.attributes.name = "testprop";
    property.attributes.datatype = DataTypeEnum.string_;
    return property;
}
Also used : PropertyAttributes(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.PropertyAttributes) Property(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Property)

Example 5 with PropertyAttributes

use of org.openhab.binding.mqtt.homie.internal.homie300.PropertyAttributes in project smarthome by eclipse.

the class HomieImplementationTests method createSpyProperty.

// Inject a spy'ed property
public Property createSpyProperty(InvocationOnMock invocation) {
    final Node node = (Node) invocation.getMock();
    final String id = (String) invocation.getArguments()[0];
    Property property = spy(node.createProperty(id, spy(new PropertyAttributes())));
    return property;
}
Also used : PropertyAttributes(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.PropertyAttributes) Node(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Node) Property(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Property)

Aggregations

Property (org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Property)5 PropertyAttributes (org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.PropertyAttributes)5 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)4 Node (org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Node)4 PropertyAttributes (org.openhab.binding.mqtt.homie.internal.homie300.PropertyAttributes)4 NodeAttributes (org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.NodeAttributes)3 ChannelState (org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelState)3 Test (org.junit.Test)3 Node (org.openhab.binding.mqtt.homie.internal.homie300.Node)3 Property (org.openhab.binding.mqtt.homie.internal.homie300.Property)3 Field (java.lang.reflect.Field)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Map (java.util.Map)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ExecutionException (java.util.concurrent.ExecutionException)2 ScheduledFuture (java.util.concurrent.ScheduledFuture)2 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)2 TimeUnit (java.util.concurrent.TimeUnit)2 NonNull (org.eclipse.jdt.annotation.NonNull)2