]> git.basschouten.com Git - openhab-addons.git/blob
6c875e74b0cb787e79543f86dba0645b8f826d20
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.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;
25
26 /**
27  * Tests for {@link BinarySensor}
28  *
29  * @author Anton Kharuzhy - Initial contribution
30  */
31 @NonNullByDefault
32 public class BinarySensorTests extends AbstractComponentTests {
33     public static final String CONFIG_TOPIC = "binary_sensor/0x0000000000000000_binary_sensor_zigbee2mqtt";
34
35     @Test
36     public void test() throws InterruptedException {
37         // @formatter:off
38         var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
39                 """
40                 { \
41                   "availability": [ \
42                     { \
43                       "topic": "zigbee2mqtt/bridge/state" \
44                     } \
45                   ], \
46                   "device": { \
47                     "identifiers": [ \
48                       "zigbee2mqtt_0x0000000000000000" \
49                     ], \
50                     "manufacturer": "Sensors inc", \
51                     "model": "On Off Sensor", \
52                     "name": "OnOffSensor", \
53                     "sw_version": "Zigbee2MQTT 1.18.2" \
54                   }, \
55                   "name": "onoffsensor", \
56                   "force_update": "true", \
57                   "payload_off": "OFF_", \
58                   "payload_on": "ON_", \
59                   "state_topic": "zigbee2mqtt/sensor/state", \
60                   "unique_id": "sn1", \
61                   "value_template": "{{ value_json.state }}" \
62                 }\
63                 """);
64         // @formatter:on
65
66         assertThat(component.channels.size(), is(1));
67         assertThat(component.getName(), is("onoffsensor"));
68         assertThat(component.getGroupId(), is("sn1"));
69
70         assertChannel(component, BinarySensor.SENSOR_CHANNEL_ID, "zigbee2mqtt/sensor/state", "", "onoffsensor",
71                 OnOffValue.class);
72
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);
81     }
82
83     @Test
84     public void offDelayTest() {
85         // @formatter:off
86         var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
87                 """
88                 { \
89                   "availability": [ \
90                     { \
91                       "topic": "zigbee2mqtt/bridge/state" \
92                     } \
93                   ], \
94                   "device": { \
95                     "identifiers": [ \
96                       "zigbee2mqtt_0x0000000000000000" \
97                     ], \
98                     "manufacturer": "Sensors inc", \
99                     "model": "On Off Sensor", \
100                     "name": "OnOffSensor", \
101                     "sw_version": "Zigbee2MQTT 1.18.2" \
102                   }, \
103                   "name": "onoffsensor", \
104                   "force_update": "true", \
105                   "off_delay": "1", \
106                   "payload_off": "OFF_", \
107                   "payload_on": "ON_", \
108                   "state_topic": "zigbee2mqtt/sensor/state", \
109                   "unique_id": "sn1", \
110                   "value_template": "{{ value_json.state }}" \
111                 }\
112                 """);
113         // @formatter:on
114
115         publishMessage("zigbee2mqtt/sensor/state", "{ \"state\": \"ON_\" }");
116         assertState(component, BinarySensor.SENSOR_CHANNEL_ID, OnOffType.ON);
117
118         waitForAssert(() -> assertState(component, BinarySensor.SENSOR_CHANNEL_ID, OnOffType.OFF), 10000, 200);
119     }
120
121     @Test
122     public void expireAfterTest() {
123         // @formatter:off
124         var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
125                 """
126                 { \
127                   "availability": [ \
128                     { \
129                       "topic": "zigbee2mqtt/bridge/state" \
130                     } \
131                   ], \
132                   "device": { \
133                     "identifiers": [ \
134                       "zigbee2mqtt_0x0000000000000000" \
135                     ], \
136                     "manufacturer": "Sensors inc", \
137                     "model": "On Off Sensor", \
138                     "name": "OnOffSensor", \
139                     "sw_version": "Zigbee2MQTT 1.18.2" \
140                   }, \
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 }}" \
149                 }\
150                 """);
151         // @formatter:on
152
153         publishMessage("zigbee2mqtt/sensor/state", "{ \"state\": \"OFF_\" }");
154         assertState(component, BinarySensor.SENSOR_CHANNEL_ID, OnOffType.OFF);
155
156         waitForAssert(() -> assertState(component, BinarySensor.SENSOR_CHANNEL_ID, UnDefType.UNDEF), 10000, 200);
157     }
158
159     @Override
160     protected Set<String> getConfigTopics() {
161         return Set.of(CONFIG_TOPIC);
162     }
163 }