Search in sources :

Example 1 with DeviceAttributes

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

the class HomieImplementationTest method parseHomieTree.

@SuppressWarnings("null")
@Test
public void parseHomieTree() throws InterruptedException, ExecutionException, TimeoutException {
    // Create a Homie Device object. Because spied Nodes are required for call verification,
    // the full Device constructor need to be used and a ChildMap object need to be created manually.
    ChildMap<Node> nodeMap = new ChildMap<>();
    Device device = spy(new Device(ThingChannelConstants.testHomieThing, callback, new DeviceAttributes(), nodeMap));
    // Intercept creating a node in initialize()->start() and inject a spy'ed node.
    doAnswer(this::createSpyNode).when(device).createNode(any());
    // initialize the device, subscribe and wait.
    device.initialize(BASE_TOPIC, DEVICE_ID, Collections.emptyList());
    device.subscribe(connection, scheduler, 1500).get();
    assertThat(device.isInitialized(), is(true));
    // Check device attributes
    assertThat(device.attributes.homie, is("3.0"));
    assertThat(device.attributes.name, is("Name"));
    assertThat(device.attributes.state, is(ReadyState.ready));
    assertThat(device.attributes.nodes.length, is(1));
    verify(device, times(4)).attributeChanged(any(), any(), any(), any(), anyBoolean());
    verify(callback).readyStateChanged(eq(ReadyState.ready));
    verify(device).attributesReceived(any(), any(), anyInt());
    // Expect 1 node
    assertThat(device.nodes.size(), is(1));
    // Check node and node attributes
    Node node = device.nodes.get("testnode");
    verify(node).subscribe(any(), any(), anyInt());
    verify(node).attributesReceived(any(), any(), anyInt());
    verify(node.attributes).subscribeAndReceive(any(), any(), anyString(), any(), anyInt());
    assertThat(node.attributes.type, is("Type"));
    assertThat(node.attributes.name, is("Testnode"));
    // Expect 2 property
    assertThat(node.properties.size(), is(3));
    // Check property and property attributes
    Property property = node.properties.get("temperature");
    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();
    assertNotNull(property.getChannelState());
    assertThat(property.getType().getState().getMinimum().intValue(), is(-100));
    assertThat(property.getType().getState().getMaximum().intValue(), is(100));
    // Check property and property attributes
    Property propertyBell = node.properties.get("doorbell");
    verify(propertyBell).attributesReceived();
    assertThat(propertyBell.attributes.settable, is(false));
    assertThat(propertyBell.attributes.retained, is(false));
    assertThat(propertyBell.attributes.name, is("Doorbell"));
    assertThat(propertyBell.attributes.datatype, is(DataTypeEnum.boolean_));
    // The device->node->property tree is ready. Now subscribe to property values.
    device.startChannels(connection, scheduler, 50, handler).get();
    assertThat(propertyBell.getChannelState().isStateful(), is(false));
    assertThat(propertyBell.getChannelState().getCache().getChannelState(), is(UnDefType.UNDEF));
    assertThat(property.getChannelState().getCache().getChannelState(), is(new DecimalType(10)));
    property = node.properties.get("testRetain");
    WaitForTopicValue watcher = new WaitForTopicValue(embeddedConnection, propertyTestTopic + "/set");
    // Watch the topic. Publish a retain=false value to MQTT
    property.getChannelState().publishValue(OnOffType.OFF).get();
    assertThat(watcher.waitForTopicValue(1000), is("false"));
    // Publish a retain=false value to MQTT.
    property.getChannelState().publishValue(OnOffType.ON).get();
    // No value is expected to be retained on this MQTT topic
    waitForAssert(() -> {
        WaitForTopicValue w = new WaitForTopicValue(embeddedConnection, propertyTestTopic + "/set");
        assertNull(w.waitForTopicValue(50));
    }, 500, 100);
}
Also used : ChildMap(org.openhab.binding.mqtt.generic.tools.ChildMap) Device(org.openhab.binding.mqtt.homie.internal.homie300.Device) WaitForTopicValue(org.openhab.binding.mqtt.generic.tools.WaitForTopicValue) Node(org.openhab.binding.mqtt.homie.internal.homie300.Node) DeviceAttributes(org.openhab.binding.mqtt.homie.internal.homie300.DeviceAttributes) DecimalType(org.openhab.core.library.types.DecimalType) Property(org.openhab.binding.mqtt.homie.internal.homie300.Property) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 2 with DeviceAttributes

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

the class HomieThingHandlerTests method setUp.

@Before
public void setUp() {
    final ThingStatusInfo thingStatus = new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, null);
    MockitoAnnotations.initMocks(this);
    final Configuration config = new Configuration();
    config.put("basetopic", "homie");
    config.put("deviceid", deviceID);
    thing = ThingBuilder.create(MqttBindingConstants.HOMIE300_MQTT_THING, testHomieThing.getId()).withConfiguration(config).build();
    thing.setStatusInfo(thingStatus);
    // Return the mocked connection object if the bridge handler is asked for it
    when(bridgeHandler.getConnectionAsync()).thenReturn(CompletableFuture.completedFuture(connection));
    doReturn(CompletableFuture.completedFuture(true)).when(connection).subscribe(any(), any());
    doReturn(CompletableFuture.completedFuture(true)).when(connection).unsubscribe(any(), any());
    doReturn(CompletableFuture.completedFuture(true)).when(connection).unsubscribeAll();
    doReturn(CompletableFuture.completedFuture(true)).when(connection).publish(any(), any(), anyInt(), anyBoolean());
    doReturn(false).when(scheduledFuture).isDone();
    doReturn(scheduledFuture).when(scheduler).schedule(any(Runnable.class), anyLong(), any(TimeUnit.class));
    final HomieThingHandler handler = new HomieThingHandler(thing, channelTypeProvider, 30, 5);
    thingHandler = spy(handler);
    thingHandler.setCallback(callback);
    final Device device = new Device(thing.getUID(), thingHandler, spy(new DeviceAttributes()), spy(new ChildMap<>()));
    thingHandler.setInternalObjects(spy(device), spy(new DelayedBatchProcessing<Object>(500, thingHandler, scheduler)));
    // Return the bridge handler if the thing handler asks for it
    doReturn(bridgeHandler).when(thingHandler).getBridgeHandler();
    // We are by default online
    doReturn(thingStatus).when(thingHandler).getBridgeStatus();
}
Also used : DelayedBatchProcessing(org.eclipse.smarthome.binding.mqtt.generic.internal.tools.DelayedBatchProcessing) Configuration(org.eclipse.smarthome.config.core.Configuration) ChildMap(org.eclipse.smarthome.binding.mqtt.generic.internal.tools.ChildMap) Device(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Device) DeviceAttributes(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.DeviceAttributes) TimeUnit(java.util.concurrent.TimeUnit) ThingStatusInfo(org.eclipse.smarthome.core.thing.ThingStatusInfo) Before(org.junit.Before)

Example 3 with DeviceAttributes

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

the class HomieImplementationTests method parseHomieTree.

@SuppressWarnings("null")
@Test
public void parseHomieTree() throws InterruptedException, ExecutionException, TimeoutException {
    // Create a Homie Device object. Because spied Nodes are required for call verification,
    // the full Device constructor need to be used and a ChildMap object need to be created manually.
    ChildMap<Node> nodeMap = new ChildMap<>();
    Device device = spy(new Device(ThingChannelConstants.testHomieThing, callback, new DeviceAttributes(), nodeMap));
    // Intercept creating a node in initialize()->start() and inject a spy'ed node.
    doAnswer(this::createSpyNode).when(device).createNode(any());
    // initialize the device, subscribe and wait.
    device.initialize(baseTopic, deviceID, Collections.emptyList());
    device.subscribe(connection, scheduler, 200).get();
    assertThat(device.isInitialized(), is(true));
    // Check device attributes
    assertThat(device.attributes.homie, is("3.0"));
    assertThat(device.attributes.name, is("Name"));
    assertThat(device.attributes.state, is(ReadyState.ready));
    assertThat(device.attributes.nodes.length, is(1));
    verify(device, times(4)).attributeChanged(any(), any(), any(), any(), anyBoolean());
    verify(callback).readyStateChanged(eq(ReadyState.ready));
    verify(device).attributesReceived(any(), any(), anyInt());
    // Expect 1 node
    assertThat(device.nodes.size(), is(1));
    // Check node and node attributes
    Node node = device.nodes.get("testnode");
    verify(node).subscribe(any(), any(), anyInt());
    verify(node).attributesReceived(any(), any(), anyInt());
    verify(node.attributes).subscribeAndReceive(any(), any(), anyString(), any(), anyInt());
    assertThat(node.attributes.type, is("Type"));
    assertThat(node.attributes.name, is("Testnode"));
    // Expect 2 property
    assertThat(node.properties.size(), is(3));
    // Check property and property attributes
    Property property = node.properties.get("temperature");
    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();
    assertNotNull(property.getChannelState());
    assertThat(property.getType().getState().getMinimum().intValue(), is(-100));
    assertThat(property.getType().getState().getMaximum().intValue(), is(100));
    // Check property and property attributes
    Property propertyBell = node.properties.get("doorbell");
    verify(propertyBell).attributesReceived();
    assertThat(propertyBell.attributes.settable, is(false));
    assertThat(propertyBell.attributes.retained, is(false));
    assertThat(propertyBell.attributes.name, is("Doorbell"));
    assertThat(propertyBell.attributes.datatype, is(DataTypeEnum.boolean_));
    // The device->node->property tree is ready. Now subscribe to property values.
    device.startChannels(connection, scheduler, 50, handler).get();
    assertThat(propertyBell.getChannelState().isStateful(), is(false));
    assertThat(propertyBell.getChannelState().getCache().getChannelState(), is(UnDefType.UNDEF));
    assertThat(property.getChannelState().getCache().getChannelState(), is(new DecimalType(10)));
    property = node.properties.get("testRetain");
    WaitForTopicValue watcher = new WaitForTopicValue(embeddedConnection, propertyTestTopic + "/set");
    // Watch the topic. Publish a retain=false value to MQTT
    property.getChannelState().publishValue(OnOffType.OFF).get();
    assertThat(watcher.waitForTopicValue(50), is("false"));
    // Publish a retain=false value to MQTT.
    property.getChannelState().publishValue(OnOffType.ON).get();
    // This test is flaky if the MQTT broker does not get a time to "forget" this non-retained value
    Thread.sleep(50);
    // No value is expected to be retained on this MQTT topic
    watcher = new WaitForTopicValue(embeddedConnection, propertyTestTopic + "/set");
    assertNull(watcher.waitForTopicValue(50));
}
Also used : ChildMap(org.eclipse.smarthome.binding.mqtt.generic.internal.tools.ChildMap) Device(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Device) WaitForTopicValue(org.eclipse.smarthome.binding.mqtt.generic.internal.tools.WaitForTopicValue) Node(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Node) DeviceAttributes(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.DeviceAttributes) 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 4 with DeviceAttributes

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

the class HomieThingHandlerTests method setUp.

@BeforeEach
public void setUp() {
    final ThingStatusInfo thingStatus = new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, null);
    final Configuration config = new Configuration();
    config.put("basetopic", "homie");
    config.put("deviceid", deviceID);
    thing = ThingBuilder.create(MqttBindingConstants.HOMIE300_MQTT_THING, TEST_HOMIE_THING.getId()).withConfiguration(config).build();
    thing.setStatusInfo(thingStatus);
    // Return the mocked connection object if the bridge handler is asked for it
    when(bridgeHandler.getConnectionAsync()).thenReturn(CompletableFuture.completedFuture(connection));
    doReturn(CompletableFuture.completedFuture(true)).when(connection).subscribe(any(), any());
    doReturn(CompletableFuture.completedFuture(true)).when(connection).unsubscribe(any(), any());
    doReturn(CompletableFuture.completedFuture(true)).when(connection).unsubscribeAll();
    doReturn(CompletableFuture.completedFuture(true)).when(connection).publish(any(), any(), anyInt(), anyBoolean());
    doReturn(false).when(scheduledFuture).isDone();
    doReturn(scheduledFuture).when(scheduler).schedule(any(Runnable.class), anyLong(), any(TimeUnit.class));
    final HomieThingHandler handler = new HomieThingHandler(thing, channelTypeProvider, 1000, 30, 5);
    thingHandler = spy(handler);
    thingHandler.setCallback(callback);
    final Device device = new Device(thing.getUID(), thingHandler, spy(new DeviceAttributes()), spy(new ChildMap<>()));
    thingHandler.setInternalObjects(spy(device), spy(new DelayedBatchProcessing<>(500, thingHandler, scheduler)));
    // Return the bridge handler if the thing handler asks for it
    doReturn(bridgeHandler).when(thingHandler).getBridgeHandler();
    // We are by default online
    doReturn(thingStatus).when(thingHandler).getBridgeStatus();
}
Also used : DelayedBatchProcessing(org.openhab.binding.mqtt.generic.tools.DelayedBatchProcessing) Configuration(org.openhab.core.config.core.Configuration) ChildMap(org.openhab.binding.mqtt.generic.tools.ChildMap) Device(org.openhab.binding.mqtt.homie.internal.homie300.Device) DeviceAttributes(org.openhab.binding.mqtt.homie.internal.homie300.DeviceAttributes) TimeUnit(java.util.concurrent.TimeUnit) ThingStatusInfo(org.openhab.core.thing.ThingStatusInfo) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

TimeUnit (java.util.concurrent.TimeUnit)2 Device (org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Device)2 DeviceAttributes (org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.DeviceAttributes)2 ChildMap (org.eclipse.smarthome.binding.mqtt.generic.internal.tools.ChildMap)2 ChildMap (org.openhab.binding.mqtt.generic.tools.ChildMap)2 Device (org.openhab.binding.mqtt.homie.internal.homie300.Device)2 DeviceAttributes (org.openhab.binding.mqtt.homie.internal.homie300.DeviceAttributes)2 Node (org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Node)1 Property (org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homie300.Property)1 DelayedBatchProcessing (org.eclipse.smarthome.binding.mqtt.generic.internal.tools.DelayedBatchProcessing)1 WaitForTopicValue (org.eclipse.smarthome.binding.mqtt.generic.internal.tools.WaitForTopicValue)1 Configuration (org.eclipse.smarthome.config.core.Configuration)1 DecimalType (org.eclipse.smarthome.core.library.types.DecimalType)1 ThingStatusInfo (org.eclipse.smarthome.core.thing.ThingStatusInfo)1 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)1 Before (org.junit.Before)1 Test (org.junit.Test)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Test (org.junit.jupiter.api.Test)1 DelayedBatchProcessing (org.openhab.binding.mqtt.generic.tools.DelayedBatchProcessing)1