]> git.basschouten.com Git - openhab-addons.git/blob
feffc40f1869781af5963b0bac348267590b6388
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.eclipse.jdt.annotation.NonNullByDefault;
21 import org.junit.jupiter.api.Test;
22 import org.openhab.binding.mqtt.generic.values.NumberValue;
23 import org.openhab.core.library.types.QuantityType;
24 import org.openhab.core.library.unit.Units;
25 import org.openhab.core.types.UnDefType;
26
27 /**
28  * Tests for {@link Sensor}
29  *
30  * @author Anton Kharuzhy - Initial contribution
31  */
32 @SuppressWarnings("ConstantConditions")
33 @NonNullByDefault
34 public class SensorTests extends AbstractComponentTests {
35     public static final String CONFIG_TOPIC = "sensor/0x0000000000000000_sensor_zigbee2mqtt";
36
37     @Test
38     public void test() throws InterruptedException {
39         // @formatter:off
40         var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
41                 "{ " +
42                         "  \"availability\": [ " +
43                         "    { " +
44                         "      \"topic\": \"zigbee2mqtt/bridge/state\" " +
45                         "    } " +
46                         "  ], " +
47                         "  \"device\": { " +
48                         "    \"identifiers\": [ " +
49                         "      \"zigbee2mqtt_0x0000000000000000\" " +
50                         "    ], " +
51                         "    \"manufacturer\": \"Sensors inc\", " +
52                         "    \"model\": \"Sensor\", " +
53                         "    \"name\": \"Sensor\", " +
54                         "    \"sw_version\": \"Zigbee2MQTT 1.18.2\" " +
55                         "  }, " +
56                         "  \"name\": \"sensor1\", " +
57                         "  \"expire_after\": \"1\", " +
58                         "  \"force_update\": \"true\", " +
59                         "  \"unit_of_measurement\": \"W\", " +
60                         "  \"state_topic\": \"zigbee2mqtt/sensor/state\", " +
61                         "  \"unique_id\": \"sn1\" " +
62                         "}");
63         // @formatter:on
64
65         assertThat(component.channels.size(), is(1));
66         assertThat(component.getName(), is("sensor1"));
67         assertThat(component.getGroupUID().getId(), is("sn1"));
68
69         assertChannel(component, Sensor.SENSOR_CHANNEL_ID, "zigbee2mqtt/sensor/state", "", "sensor1",
70                 NumberValue.class);
71
72         publishMessage("zigbee2mqtt/sensor/state", "10");
73         assertState(component, Sensor.SENSOR_CHANNEL_ID, new QuantityType<>(10, Units.WATT));
74         publishMessage("zigbee2mqtt/sensor/state", "20");
75         assertState(component, Sensor.SENSOR_CHANNEL_ID, new QuantityType<>(20, Units.WATT));
76         assertThat(component.getChannel(Sensor.SENSOR_CHANNEL_ID).getState().getCache().createStateDescription(true)
77                 .build().getPattern(), is("%s %unit%"));
78
79         waitForAssert(() -> assertState(component, Sensor.SENSOR_CHANNEL_ID, UnDefType.UNDEF), 10000, 200);
80     }
81
82     @Override
83     protected Set<String> getConfigTopics() {
84         return Set.of(CONFIG_TOPIC);
85     }
86 }