2 * Copyright (c) 2010-2021 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.junit.jupiter.api.Test;
21 import org.openhab.binding.mqtt.generic.values.OnOffValue;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.types.UnDefType;
26 * Tests for {@link BinarySensor}
28 * @author Anton Kharuzhy - Initial contribution
30 public class BinarySensorTests extends AbstractComponentTests {
31 public static final String CONFIG_TOPIC = "binary_sensor/0x0000000000000000_binary_sensor_zigbee2mqtt";
34 public void test() throws InterruptedException {
36 var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
38 " \"availability\": [ " +
40 " \"topic\": \"zigbee2mqtt/bridge/state\" " +
44 " \"identifiers\": [ " +
45 " \"zigbee2mqtt_0x0000000000000000\" " +
47 " \"manufacturer\": \"Sensors inc\", " +
48 " \"model\": \"On Off Sensor\", " +
49 " \"name\": \"OnOffSensor\", " +
50 " \"sw_version\": \"Zigbee2MQTT 1.18.2\" " +
52 " \"name\": \"onoffsensor\", " +
53 " \"force_update\": \"true\", " +
54 " \"payload_off\": \"OFF_\", " +
55 " \"payload_on\": \"ON_\", " +
56 " \"state_topic\": \"zigbee2mqtt/sensor/state\", " +
57 " \"unique_id\": \"sn1\", " +
58 " \"value_template\": \"{{ value_json.state }}\" " +
62 assertThat(component.channels.size(), is(1));
63 assertThat(component.getName(), is("onoffsensor"));
64 assertThat(component.getGroupUID().getId(), is("sn1"));
66 assertChannel(component, BinarySensor.SENSOR_CHANNEL_ID, "zigbee2mqtt/sensor/state", "", "value",
69 publishMessage("zigbee2mqtt/sensor/state", "{ \"state\": \"ON_\" }");
70 assertState(component, BinarySensor.SENSOR_CHANNEL_ID, OnOffType.ON);
71 publishMessage("zigbee2mqtt/sensor/state", "{ \"state\": \"ON_\" }");
72 assertState(component, BinarySensor.SENSOR_CHANNEL_ID, OnOffType.ON);
73 publishMessage("zigbee2mqtt/sensor/state", "{ \"state\": \"OFF_\" }");
74 assertState(component, BinarySensor.SENSOR_CHANNEL_ID, OnOffType.OFF);
75 publishMessage("zigbee2mqtt/sensor/state", "{ \"state\": \"ON_\" }");
76 assertState(component, BinarySensor.SENSOR_CHANNEL_ID, OnOffType.ON);
80 public void offDelayTest() {
82 var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
84 " \"availability\": [ " +
86 " \"topic\": \"zigbee2mqtt/bridge/state\" " +
90 " \"identifiers\": [ " +
91 " \"zigbee2mqtt_0x0000000000000000\" " +
93 " \"manufacturer\": \"Sensors inc\", " +
94 " \"model\": \"On Off Sensor\", " +
95 " \"name\": \"OnOffSensor\", " +
96 " \"sw_version\": \"Zigbee2MQTT 1.18.2\" " +
98 " \"name\": \"onoffsensor\", " +
99 " \"force_update\": \"true\", " +
100 " \"off_delay\": \"1\", " +
101 " \"payload_off\": \"OFF_\", " +
102 " \"payload_on\": \"ON_\", " +
103 " \"state_topic\": \"zigbee2mqtt/sensor/state\", " +
104 " \"unique_id\": \"sn1\", " +
105 " \"value_template\": \"{{ value_json.state }}\" " +
109 publishMessage("zigbee2mqtt/sensor/state", "{ \"state\": \"ON_\" }");
110 assertState(component, BinarySensor.SENSOR_CHANNEL_ID, OnOffType.ON);
112 waitForAssert(() -> assertState(component, BinarySensor.SENSOR_CHANNEL_ID, OnOffType.OFF), 10000, 200);
116 public void expireAfterTest() {
118 var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
120 " \"availability\": [ " +
122 " \"topic\": \"zigbee2mqtt/bridge/state\" " +
126 " \"identifiers\": [ " +
127 " \"zigbee2mqtt_0x0000000000000000\" " +
129 " \"manufacturer\": \"Sensors inc\", " +
130 " \"model\": \"On Off Sensor\", " +
131 " \"name\": \"OnOffSensor\", " +
132 " \"sw_version\": \"Zigbee2MQTT 1.18.2\" " +
134 " \"name\": \"onoffsensor\", " +
135 " \"expire_after\": \"1\", " +
136 " \"force_update\": \"true\", " +
137 " \"payload_off\": \"OFF_\", " +
138 " \"payload_on\": \"ON_\", " +
139 " \"state_topic\": \"zigbee2mqtt/sensor/state\", " +
140 " \"unique_id\": \"sn1\", " +
141 " \"value_template\": \"{{ value_json.state }}\" " +
145 publishMessage("zigbee2mqtt/sensor/state", "{ \"state\": \"OFF_\" }");
146 assertState(component, BinarySensor.SENSOR_CHANNEL_ID, OnOffType.OFF);
148 waitForAssert(() -> assertState(component, BinarySensor.SENSOR_CHANNEL_ID, UnDefType.UNDEF), 10000, 200);
151 protected Set<String> getConfigTopics() {
152 return Set.of(CONFIG_TOPIC);