Search in sources :

Example 6 with NodeAttributes

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

use of org.openhab.binding.mqtt.homie.internal.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 8 with NodeAttributes

use of org.openhab.binding.mqtt.homie.internal.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)

Example 9 with NodeAttributes

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

the class HomieThingHandlerTests method createSpyNode.

public Node createSpyNode(String propertyID, Device device) {
    // Create the node
    Node node = spy(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";
    node.attributes.properties = new String[] { "property" };
    doAnswer(this::createSubscriberAnswer).when(node.attributes).createSubscriber(any(), any(), any(), anyBoolean());
    // Intercept creating a property in the next call and inject a spy'ed property.
    doAnswer(i -> createSpyProperty("property", node)).when(node).createProperty(any());
    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 10 with NodeAttributes

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

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);
    ThingHandlerHelper.setConnection(thingHandler, connection);
    // we need to set a channel value first, undefined values ignored on REFRESH
    property.getChannelState().getCache().update(new StringType("testString"));
    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) BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers(org.mockito.ArgumentMatchers) ScheduledFuture(java.util.concurrent.ScheduledFuture) StringType(org.openhab.core.library.types.StringType) Value(org.openhab.binding.mqtt.generic.values.Value) ReadyState(org.openhab.binding.mqtt.homie.internal.homie300.DeviceAttributes.ReadyState) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Nullable(org.eclipse.jdt.annotation.Nullable) Configuration(org.openhab.core.config.core.Configuration) Device(org.openhab.binding.mqtt.homie.internal.homie300.Device) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) Map(java.util.Map) TypeParser(org.openhab.core.types.TypeParser) ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) DelayedBatchProcessing(org.openhab.binding.mqtt.generic.tools.DelayedBatchProcessing) ThingTypeRegistry(org.openhab.core.thing.type.ThingTypeRegistry) Test(org.junit.jupiter.api.Test) Channel(org.openhab.core.thing.Channel) List(java.util.List) Node(org.openhab.binding.mqtt.homie.internal.homie300.Node) NonNull(org.eclipse.jdt.annotation.NonNull) Strictness(org.mockito.quality.Strictness) MqttBrokerConnection(org.openhab.core.io.transport.mqtt.MqttBrokerConnection) MockitoSettings(org.mockito.junit.jupiter.MockitoSettings) MqttChannelTypeProvider(org.openhab.binding.mqtt.generic.MqttChannelTypeProvider) ThingHandlerHelper(org.openhab.binding.mqtt.homie.ThingHandlerHelper) Mock(org.mockito.Mock) AbstractMqttAttributeClass(org.openhab.binding.mqtt.generic.mapping.AbstractMqttAttributeClass) CompletableFuture(java.util.concurrent.CompletableFuture) AbstractBrokerHandler(org.openhab.binding.mqtt.handler.AbstractBrokerHandler) Checks.requireNonNull(org.eclipse.jdt.annotation.Checks.requireNonNull) ArrayList(java.util.ArrayList) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Thing(org.openhab.core.thing.Thing) Property(org.openhab.binding.mqtt.homie.internal.homie300.Property) ChannelKind(org.openhab.core.thing.type.ChannelKind) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) SubscribeFieldToMQTTtopic(org.openhab.binding.mqtt.generic.mapping.SubscribeFieldToMQTTtopic) NodeAttributes(org.openhab.binding.mqtt.homie.internal.homie300.NodeAttributes) ThingStatusInfo(org.openhab.core.thing.ThingStatusInfo) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) PropertyAttributes(org.openhab.binding.mqtt.homie.internal.homie300.PropertyAttributes) ChannelStateHelper(org.openhab.binding.mqtt.homie.ChannelStateHelper) DeviceAttributes(org.openhab.binding.mqtt.homie.internal.homie300.DeviceAttributes) ThingStatus(org.openhab.core.thing.ThingStatus) Command(org.openhab.core.types.Command) TEST_HOMIE_THING(org.openhab.binding.mqtt.homie.internal.handler.ThingChannelConstants.TEST_HOMIE_THING) DataTypeEnum(org.openhab.binding.mqtt.homie.internal.homie300.PropertyAttributes.DataTypeEnum) RefreshType(org.openhab.core.types.RefreshType) ThingStatusDetail(org.openhab.core.thing.ThingStatusDetail) Field(java.lang.reflect.Field) ChildMap(org.openhab.binding.mqtt.generic.tools.ChildMap) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) ChannelState(org.openhab.binding.mqtt.generic.ChannelState) MqttBindingConstants(org.openhab.binding.mqtt.homie.generic.internal.MqttBindingConstants) Assertions(org.junit.jupiter.api.Assertions) PropertyAttributes(org.openhab.binding.mqtt.homie.internal.homie300.PropertyAttributes) StringType(org.openhab.core.library.types.StringType) Node(org.openhab.binding.mqtt.homie.internal.homie300.Node) NodeAttributes(org.openhab.binding.mqtt.homie.internal.homie300.NodeAttributes) Property(org.openhab.binding.mqtt.homie.internal.homie300.Property) Test(org.junit.jupiter.api.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 Node (org.openhab.binding.mqtt.homie.internal.homie300.Node)6 NodeAttributes (org.openhab.binding.mqtt.homie.internal.homie300.NodeAttributes)6 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)4 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 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 Nullable (org.eclipse.jdt.annotation.Nullable)2