2 * Copyright (c) 2010-2023 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.thing.ThingStatus;
27 import org.openhab.core.types.UnDefType;
30 * Tests for {@link Sensor}
32 * @author Anton Kharuzhy - Initial contribution
35 public class SensorTests extends AbstractComponentTests {
36 public static final String CONFIG_TOPIC = "sensor/0x0000000000000000_sensor_zigbee2mqtt";
38 @SuppressWarnings("null")
40 public void test() throws InterruptedException {
42 var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
44 " \"availability_topic\": \"zigbee2mqtt/bridge/state\", " +
45 " \"availability_template\": \"{{value_json.state}}\", " +
47 " \"identifiers\": [ " +
48 " \"zigbee2mqtt_0x0000000000000000\" " +
50 " \"manufacturer\": \"Sensors inc\", " +
51 " \"model\": \"Sensor\", " +
52 " \"name\": \"Sensor\", " +
53 " \"sw_version\": \"Zigbee2MQTT 1.18.2\" " +
55 " \"name\": \"sensor1\", " +
56 " \"expire_after\": \"1\", " +
57 " \"force_update\": \"true\", " +
58 " \"unit_of_measurement\": \"W\", " +
59 " \"state_topic\": \"zigbee2mqtt/sensor/state\", " +
60 " \"unique_id\": \"sn1\" " +
64 assertThat(component.channels.size(), is(1));
65 assertThat(component.getName(), is("sensor1"));
66 assertThat(component.getGroupUID().getId(), is("sn1"));
68 assertChannel(component, Sensor.SENSOR_CHANNEL_ID, "zigbee2mqtt/sensor/state", "", "sensor1",
71 publishMessage("zigbee2mqtt/bridge/state", "{ \"state\": \"online\" }");
72 assertThat(haThing.getStatus(), is(ThingStatus.ONLINE));
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 W"));
80 waitForAssert(() -> assertState(component, Sensor.SENSOR_CHANNEL_ID, UnDefType.UNDEF), 5000, 200);
82 publishMessage("zigbee2mqtt/bridge/state", "{ \"state\": \"offline\" }");
83 assertThat(haThing.getStatus(), is(ThingStatus.OFFLINE));
87 public void testMeasurementStateClass() throws InterruptedException {
89 var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
92 " \"identifiers\": [ " +
93 " \"zigbee2mqtt_0x0000000000000000\" " +
95 " \"manufacturer\": \"Sensors inc\", " +
96 " \"model\": \"Sensor\", " +
97 " \"name\": \"Sensor\", " +
98 " \"sw_version\": \"Zigbee2MQTT 1.18.2\" " +
100 " \"name\": \"sensor1\", " +
101 " \"expire_after\": \"1\", " +
102 " \"force_update\": \"true\", " +
103 " \"state_class\": \"measurement\", " +
104 " \"state_topic\": \"zigbee2mqtt/sensor/state\", " +
105 " \"unique_id\": \"sn1\" " +
109 assertChannel(component, Sensor.SENSOR_CHANNEL_ID, "zigbee2mqtt/sensor/state", "", "sensor1",
114 public void testNonNumericSensor() throws InterruptedException {
116 var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
119 " \"identifiers\": [ " +
120 " \"zigbee2mqtt_0x0000000000000000\" " +
122 " \"manufacturer\": \"Sensors inc\", " +
123 " \"model\": \"Sensor\", " +
124 " \"name\": \"Sensor\", " +
125 " \"sw_version\": \"Zigbee2MQTT 1.18.2\" " +
127 " \"name\": \"sensor1\", " +
128 " \"expire_after\": \"1\", " +
129 " \"force_update\": \"true\", " +
130 " \"state_topic\": \"zigbee2mqtt/sensor/state\", " +
131 " \"unique_id\": \"sn1\" " +
135 assertChannel(component, Sensor.SENSOR_CHANNEL_ID, "zigbee2mqtt/sensor/state", "", "sensor1", TextValue.class);
139 protected Set<String> getConfigTopics() {
140 return Set.of(CONFIG_TOPIC);