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.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),
40 " \"availability\": [ " +
42 " \"topic\": \"zigbee2mqtt/bridge/state\" " +
46 " \"identifiers\": [ " +
47 " \"zigbee2mqtt_0x0000000000000000\" " +
49 " \"manufacturer\": \"Sensors inc\", " +
50 " \"model\": \"On Off Sensor\", " +
51 " \"name\": \"OnOffSensor\", " +
52 " \"sw_version\": \"Zigbee2MQTT 1.18.2\" " +
54 " \"name\": \"onoffsensor\", " +
55 " \"force_update\": \"true\", " +
56 " \"payload_off\": \"OFF_\", " +
57 " \"payload_on\": \"ON_\", " +
58 " \"state_topic\": \"zigbee2mqtt/sensor/state\", " +
59 " \"unique_id\": \"sn1\", " +
60 " \"value_template\": \"{{ value_json.state }}\" " +
64 assertThat(component.channels.size(), is(1));
65 assertThat(component.getName(), is("onoffsensor"));
66 assertThat(component.getGroupUID().getId(), is("sn1"));
68 assertChannel(component, BinarySensor.SENSOR_CHANNEL_ID, "zigbee2mqtt/sensor/state", "", "value",
71 publishMessage("zigbee2mqtt/sensor/state", "{ \"state\": \"ON_\" }");
72 assertState(component, BinarySensor.SENSOR_CHANNEL_ID, OnOffType.ON);
73 publishMessage("zigbee2mqtt/sensor/state", "{ \"state\": \"ON_\" }");
74 assertState(component, BinarySensor.SENSOR_CHANNEL_ID, OnOffType.ON);
75 publishMessage("zigbee2mqtt/sensor/state", "{ \"state\": \"OFF_\" }");
76 assertState(component, BinarySensor.SENSOR_CHANNEL_ID, OnOffType.OFF);
77 publishMessage("zigbee2mqtt/sensor/state", "{ \"state\": \"ON_\" }");
78 assertState(component, BinarySensor.SENSOR_CHANNEL_ID, OnOffType.ON);
82 public void offDelayTest() {
84 var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
86 " \"availability\": [ " +
88 " \"topic\": \"zigbee2mqtt/bridge/state\" " +
92 " \"identifiers\": [ " +
93 " \"zigbee2mqtt_0x0000000000000000\" " +
95 " \"manufacturer\": \"Sensors inc\", " +
96 " \"model\": \"On Off Sensor\", " +
97 " \"name\": \"OnOffSensor\", " +
98 " \"sw_version\": \"Zigbee2MQTT 1.18.2\" " +
100 " \"name\": \"onoffsensor\", " +
101 " \"force_update\": \"true\", " +
102 " \"off_delay\": \"1\", " +
103 " \"payload_off\": \"OFF_\", " +
104 " \"payload_on\": \"ON_\", " +
105 " \"state_topic\": \"zigbee2mqtt/sensor/state\", " +
106 " \"unique_id\": \"sn1\", " +
107 " \"value_template\": \"{{ value_json.state }}\" " +
111 publishMessage("zigbee2mqtt/sensor/state", "{ \"state\": \"ON_\" }");
112 assertState(component, BinarySensor.SENSOR_CHANNEL_ID, OnOffType.ON);
114 waitForAssert(() -> assertState(component, BinarySensor.SENSOR_CHANNEL_ID, OnOffType.OFF), 10000, 200);
118 public void expireAfterTest() {
120 var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
122 " \"availability\": [ " +
124 " \"topic\": \"zigbee2mqtt/bridge/state\" " +
128 " \"identifiers\": [ " +
129 " \"zigbee2mqtt_0x0000000000000000\" " +
131 " \"manufacturer\": \"Sensors inc\", " +
132 " \"model\": \"On Off Sensor\", " +
133 " \"name\": \"OnOffSensor\", " +
134 " \"sw_version\": \"Zigbee2MQTT 1.18.2\" " +
136 " \"name\": \"onoffsensor\", " +
137 " \"expire_after\": \"1\", " +
138 " \"force_update\": \"true\", " +
139 " \"payload_off\": \"OFF_\", " +
140 " \"payload_on\": \"ON_\", " +
141 " \"state_topic\": \"zigbee2mqtt/sensor/state\", " +
142 " \"unique_id\": \"sn1\", " +
143 " \"value_template\": \"{{ value_json.state }}\" " +
147 publishMessage("zigbee2mqtt/sensor/state", "{ \"state\": \"OFF_\" }");
148 assertState(component, BinarySensor.SENSOR_CHANNEL_ID, OnOffType.OFF);
150 waitForAssert(() -> assertState(component, BinarySensor.SENSOR_CHANNEL_ID, UnDefType.UNDEF), 10000, 200);
154 protected Set<String> getConfigTopics() {
155 return Set.of(CONFIG_TOPIC);