2 * Copyright (c) 2010-2024 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.OnOffValue;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.types.UnDefType;
27 * Tests for {@link BinarySensor}
29 * @author Anton Kharuzhy - Initial contribution
32 public class BinarySensorTests extends AbstractComponentTests {
33 public static final String CONFIG_TOPIC = "binary_sensor/0x0000000000000000_binary_sensor_zigbee2mqtt";
36 public void test() throws InterruptedException {
38 var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
43 "topic": "zigbee2mqtt/bridge/state" \
48 "zigbee2mqtt_0x0000000000000000" \
50 "manufacturer": "Sensors inc", \
51 "model": "On Off Sensor", \
52 "name": "OnOffSensor", \
53 "sw_version": "Zigbee2MQTT 1.18.2" \
55 "name": "onoffsensor", \
56 "force_update": "true", \
57 "payload_off": "OFF_", \
58 "payload_on": "ON_", \
59 "state_topic": "zigbee2mqtt/sensor/state", \
61 "value_template": "{{ value_json.state }}" \
66 assertThat(component.channels.size(), is(1));
67 assertThat(component.getName(), is("onoffsensor"));
68 assertThat(component.getGroupId(), is("sn1"));
70 assertChannel(component, BinarySensor.SENSOR_CHANNEL_ID, "zigbee2mqtt/sensor/state", "", "onoffsensor",
73 publishMessage("zigbee2mqtt/sensor/state", "{ \"state\": \"ON_\" }");
74 assertState(component, BinarySensor.SENSOR_CHANNEL_ID, OnOffType.ON);
75 publishMessage("zigbee2mqtt/sensor/state", "{ \"state\": \"ON_\" }");
76 assertState(component, BinarySensor.SENSOR_CHANNEL_ID, OnOffType.ON);
77 publishMessage("zigbee2mqtt/sensor/state", "{ \"state\": \"OFF_\" }");
78 assertState(component, BinarySensor.SENSOR_CHANNEL_ID, OnOffType.OFF);
79 publishMessage("zigbee2mqtt/sensor/state", "{ \"state\": \"ON_\" }");
80 assertState(component, BinarySensor.SENSOR_CHANNEL_ID, OnOffType.ON);
84 public void offDelayTest() {
86 var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
91 "topic": "zigbee2mqtt/bridge/state" \
96 "zigbee2mqtt_0x0000000000000000" \
98 "manufacturer": "Sensors inc", \
99 "model": "On Off Sensor", \
100 "name": "OnOffSensor", \
101 "sw_version": "Zigbee2MQTT 1.18.2" \
103 "name": "onoffsensor", \
104 "force_update": "true", \
106 "payload_off": "OFF_", \
107 "payload_on": "ON_", \
108 "state_topic": "zigbee2mqtt/sensor/state", \
109 "unique_id": "sn1", \
110 "value_template": "{{ value_json.state }}" \
115 publishMessage("zigbee2mqtt/sensor/state", "{ \"state\": \"ON_\" }");
116 assertState(component, BinarySensor.SENSOR_CHANNEL_ID, OnOffType.ON);
118 waitForAssert(() -> assertState(component, BinarySensor.SENSOR_CHANNEL_ID, OnOffType.OFF), 10000, 200);
122 public void expireAfterTest() {
124 var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
129 "topic": "zigbee2mqtt/bridge/state" \
134 "zigbee2mqtt_0x0000000000000000" \
136 "manufacturer": "Sensors inc", \
137 "model": "On Off Sensor", \
138 "name": "OnOffSensor", \
139 "sw_version": "Zigbee2MQTT 1.18.2" \
141 "name": "onoffsensor", \
142 "expire_after": "1", \
143 "force_update": "true", \
144 "payload_off": "OFF_", \
145 "payload_on": "ON_", \
146 "state_topic": "zigbee2mqtt/sensor/state", \
147 "unique_id": "sn1", \
148 "value_template": "{{ value_json.state }}" \
153 publishMessage("zigbee2mqtt/sensor/state", "{ \"state\": \"OFF_\" }");
154 assertState(component, BinarySensor.SENSOR_CHANNEL_ID, OnOffType.OFF);
156 waitForAssert(() -> assertState(component, BinarySensor.SENSOR_CHANNEL_ID, UnDefType.UNDEF), 10000, 200);
160 protected Set<String> getConfigTopics() {
161 return Set.of(CONFIG_TOPIC);