]> git.basschouten.com Git - openhab-addons.git/blob
60849f8761d533dd7e4898e5c4ea39f11167279e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mqtt.homeassistant.internal.component;
14
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.hamcrest.MatcherAssert.assertThat;
17
18 import java.util.Set;
19
20 import org.junit.jupiter.api.Test;
21 import org.openhab.binding.mqtt.generic.values.NumberValue;
22 import org.openhab.core.library.types.DecimalType;
23 import org.openhab.core.types.UnDefType;
24
25 /**
26  * Tests for {@link Sensor}
27  *
28  * @author Anton Kharuzhy - Initial contribution
29  */
30 @SuppressWarnings("ConstantConditions")
31 public class SensorTests extends AbstractComponentTests {
32     public static final String CONFIG_TOPIC = "sensor/0x0000000000000000_sensor_zigbee2mqtt";
33
34     @Test
35     public void test() throws InterruptedException {
36         // @formatter:off
37         var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
38                 "{ " +
39                         "  \"availability\": [ " +
40                         "    { " +
41                         "      \"topic\": \"zigbee2mqtt/bridge/state\" " +
42                         "    } " +
43                         "  ], " +
44                         "  \"device\": { " +
45                         "    \"identifiers\": [ " +
46                         "      \"zigbee2mqtt_0x0000000000000000\" " +
47                         "    ], " +
48                         "    \"manufacturer\": \"Sensors inc\", " +
49                         "    \"model\": \"Sensor\", " +
50                         "    \"name\": \"Sensor\", " +
51                         "    \"sw_version\": \"Zigbee2MQTT 1.18.2\" " +
52                         "  }, " +
53                         "  \"name\": \"sensor1\", " +
54                         "  \"expire_after\": \"1\", " +
55                         "  \"force_update\": \"true\", " +
56                         "  \"unit_of_measurement\": \"W\", " +
57                         "  \"state_topic\": \"zigbee2mqtt/sensor/state\", " +
58                         "  \"unique_id\": \"sn1\" " +
59                         "}");
60         // @formatter:on
61
62         assertThat(component.channels.size(), is(1));
63         assertThat(component.getName(), is("sensor1"));
64         assertThat(component.getGroupUID().getId(), is("sn1"));
65
66         assertChannel(component, Sensor.SENSOR_CHANNEL_ID, "zigbee2mqtt/sensor/state", "", "sensor1",
67                 NumberValue.class);
68
69         publishMessage("zigbee2mqtt/sensor/state", "10");
70         assertState(component, Sensor.SENSOR_CHANNEL_ID, DecimalType.valueOf("10"));
71         publishMessage("zigbee2mqtt/sensor/state", "20");
72         assertState(component, Sensor.SENSOR_CHANNEL_ID, DecimalType.valueOf("20"));
73         assertThat(component.getChannel(Sensor.SENSOR_CHANNEL_ID).getState().getCache().createStateDescription(true)
74                 .build().getPattern(), is("%s W"));
75
76         waitForAssert(() -> assertState(component, Sensor.SENSOR_CHANNEL_ID, UnDefType.UNDEF), 10000, 200);
77     }
78
79     protected Set<String> getConfigTopics() {
80         return Set.of(CONFIG_TOPIC);
81     }
82 }