2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mqtt.homeassistant.internal.component;
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.hamcrest.MatcherAssert.assertThat;
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.binding.mqtt.generic.values.TextValue;
24 import org.openhab.core.library.types.QuantityType;
25 import org.openhab.core.library.unit.Units;
26 import org.openhab.core.types.UnDefType;
29 * Tests for {@link Sensor}
31 * @author Anton Kharuzhy - Initial contribution
33 @SuppressWarnings("ConstantConditions")
35 public class SensorTests extends AbstractComponentTests {
36 public static final String CONFIG_TOPIC = "sensor/0x0000000000000000_sensor_zigbee2mqtt";
39 public void test() throws InterruptedException {
41 var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
43 " \"availability\": [ " +
45 " \"topic\": \"zigbee2mqtt/bridge/state\" " +
49 " \"identifiers\": [ " +
50 " \"zigbee2mqtt_0x0000000000000000\" " +
52 " \"manufacturer\": \"Sensors inc\", " +
53 " \"model\": \"Sensor\", " +
54 " \"name\": \"Sensor\", " +
55 " \"sw_version\": \"Zigbee2MQTT 1.18.2\" " +
57 " \"name\": \"sensor1\", " +
58 " \"expire_after\": \"1\", " +
59 " \"force_update\": \"true\", " +
60 " \"unit_of_measurement\": \"W\", " +
61 " \"state_topic\": \"zigbee2mqtt/sensor/state\", " +
62 " \"unique_id\": \"sn1\" " +
66 assertThat(component.channels.size(), is(1));
67 assertThat(component.getName(), is("sensor1"));
68 assertThat(component.getGroupUID().getId(), is("sn1"));
70 assertChannel(component, Sensor.SENSOR_CHANNEL_ID, "zigbee2mqtt/sensor/state", "", "sensor1",
73 publishMessage("zigbee2mqtt/sensor/state", "10");
74 assertState(component, Sensor.SENSOR_CHANNEL_ID, new QuantityType<>(10, Units.WATT));
75 publishMessage("zigbee2mqtt/sensor/state", "20");
76 assertState(component, Sensor.SENSOR_CHANNEL_ID, new QuantityType<>(20, Units.WATT));
77 assertThat(component.getChannel(Sensor.SENSOR_CHANNEL_ID).getState().getCache().createStateDescription(true)
78 .build().getPattern(), is("%s %unit%"));
80 waitForAssert(() -> assertState(component, Sensor.SENSOR_CHANNEL_ID, UnDefType.UNDEF), 10000, 200);
84 public void testMeasurementStateClass() throws InterruptedException {
86 var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
89 " \"identifiers\": [ " +
90 " \"zigbee2mqtt_0x0000000000000000\" " +
92 " \"manufacturer\": \"Sensors inc\", " +
93 " \"model\": \"Sensor\", " +
94 " \"name\": \"Sensor\", " +
95 " \"sw_version\": \"Zigbee2MQTT 1.18.2\" " +
97 " \"name\": \"sensor1\", " +
98 " \"expire_after\": \"1\", " +
99 " \"force_update\": \"true\", " +
100 " \"state_class\": \"measurement\", " +
101 " \"state_topic\": \"zigbee2mqtt/sensor/state\", " +
102 " \"unique_id\": \"sn1\" " +
106 assertChannel(component, Sensor.SENSOR_CHANNEL_ID, "zigbee2mqtt/sensor/state", "", "sensor1",
111 public void testNonNumericSensor() throws InterruptedException {
113 var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
116 " \"identifiers\": [ " +
117 " \"zigbee2mqtt_0x0000000000000000\" " +
119 " \"manufacturer\": \"Sensors inc\", " +
120 " \"model\": \"Sensor\", " +
121 " \"name\": \"Sensor\", " +
122 " \"sw_version\": \"Zigbee2MQTT 1.18.2\" " +
124 " \"name\": \"sensor1\", " +
125 " \"expire_after\": \"1\", " +
126 " \"force_update\": \"true\", " +
127 " \"state_topic\": \"zigbee2mqtt/sensor/state\", " +
128 " \"unique_id\": \"sn1\" " +
132 assertChannel(component, Sensor.SENSOR_CHANNEL_ID, "zigbee2mqtt/sensor/state", "", "sensor1", TextValue.class);
136 protected Set<String> getConfigTopics() {
137 return Set.of(CONFIG_TOPIC);