]> git.basschouten.com Git - openhab-addons.git/blob
e8a0277a31c6050d63432dec4956be9bbf20df6f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mqtt.homeassistant.internal.component;
14
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.hamcrest.MatcherAssert.assertThat;
17
18 import java.util.Set;
19
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;
24
25 /**
26  * Tests for {@link BinarySensor}
27  *
28  * @author Anton Kharuzhy - Initial contribution
29  */
30 public class BinarySensorTests extends AbstractComponentTests {
31     public static final String CONFIG_TOPIC = "binary_sensor/0x0000000000000000_binary_sensor_zigbee2mqtt";
32
33     @Test
34     public void test() throws InterruptedException {
35         // @formatter:off
36         var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
37                 "{ " +
38                         "  \"availability\": [ " +
39                         "    { " +
40                         "      \"topic\": \"zigbee2mqtt/bridge/state\" " +
41                         "    } " +
42                         "  ], " +
43                         "  \"device\": { " +
44                         "    \"identifiers\": [ " +
45                         "      \"zigbee2mqtt_0x0000000000000000\" " +
46                         "    ], " +
47                         "    \"manufacturer\": \"Sensors inc\", " +
48                         "    \"model\": \"On Off Sensor\", " +
49                         "    \"name\": \"OnOffSensor\", " +
50                         "    \"sw_version\": \"Zigbee2MQTT 1.18.2\" " +
51                         "  }, " +
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 }}\" " +
59                         "}");
60         // @formatter:on
61
62         assertThat(component.channels.size(), is(1));
63         assertThat(component.getName(), is("onoffsensor"));
64         assertThat(component.getGroupUID().getId(), is("sn1"));
65
66         assertChannel(component, BinarySensor.SENSOR_CHANNEL_ID, "zigbee2mqtt/sensor/state", "", "value",
67                 OnOffValue.class);
68
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);
77     }
78
79     @Test
80     public void offDelayTest() {
81         // @formatter:off
82         var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
83                 "{ " +
84                         "  \"availability\": [ " +
85                         "    { " +
86                         "      \"topic\": \"zigbee2mqtt/bridge/state\" " +
87                         "    } " +
88                         "  ], " +
89                         "  \"device\": { " +
90                         "    \"identifiers\": [ " +
91                         "      \"zigbee2mqtt_0x0000000000000000\" " +
92                         "    ], " +
93                         "    \"manufacturer\": \"Sensors inc\", " +
94                         "    \"model\": \"On Off Sensor\", " +
95                         "    \"name\": \"OnOffSensor\", " +
96                         "    \"sw_version\": \"Zigbee2MQTT 1.18.2\" " +
97                         "  }, " +
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 }}\" " +
106                         "}");
107         // @formatter:on
108
109         publishMessage("zigbee2mqtt/sensor/state", "{ \"state\": \"ON_\" }");
110         assertState(component, BinarySensor.SENSOR_CHANNEL_ID, OnOffType.ON);
111
112         waitForAssert(() -> assertState(component, BinarySensor.SENSOR_CHANNEL_ID, OnOffType.OFF), 10000, 200);
113     }
114
115     @Test
116     public void expireAfterTest() {
117         // @formatter:off
118         var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
119                 "{ " +
120                         "  \"availability\": [ " +
121                         "    { " +
122                         "      \"topic\": \"zigbee2mqtt/bridge/state\" " +
123                         "    } " +
124                         "  ], " +
125                         "  \"device\": { " +
126                         "    \"identifiers\": [ " +
127                         "      \"zigbee2mqtt_0x0000000000000000\" " +
128                         "    ], " +
129                         "    \"manufacturer\": \"Sensors inc\", " +
130                         "    \"model\": \"On Off Sensor\", " +
131                         "    \"name\": \"OnOffSensor\", " +
132                         "    \"sw_version\": \"Zigbee2MQTT 1.18.2\" " +
133                         "  }, " +
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 }}\" " +
142                         "}");
143         // @formatter:on
144
145         publishMessage("zigbee2mqtt/sensor/state", "{ \"state\": \"OFF_\" }");
146         assertState(component, BinarySensor.SENSOR_CHANNEL_ID, OnOffType.OFF);
147
148         waitForAssert(() -> assertState(component, BinarySensor.SENSOR_CHANNEL_ID, UnDefType.UNDEF), 10000, 200);
149     }
150
151     protected Set<String> getConfigTopics() {
152         return Set.of(CONFIG_TOPIC);
153     }
154 }