]> git.basschouten.com Git - openhab-addons.git/blob
fcb5444129e250949e62b99e1316da127b29347f
[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.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                         "  \"availability\": [ " +
41                         "    { " +
42                         "      \"topic\": \"zigbee2mqtt/bridge/state\" " +
43                         "    } " +
44                         "  ], " +
45                         "  \"device\": { " +
46                         "    \"identifiers\": [ " +
47                         "      \"zigbee2mqtt_0x0000000000000000\" " +
48                         "    ], " +
49                         "    \"manufacturer\": \"Sensors inc\", " +
50                         "    \"model\": \"On Off Sensor\", " +
51                         "    \"name\": \"OnOffSensor\", " +
52                         "    \"sw_version\": \"Zigbee2MQTT 1.18.2\" " +
53                         "  }, " +
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 }}\" " +
61                         "}");
62         // @formatter:on
63
64         assertThat(component.channels.size(), is(1));
65         assertThat(component.getName(), is("onoffsensor"));
66         assertThat(component.getGroupUID().getId(), is("sn1"));
67
68         assertChannel(component, BinarySensor.SENSOR_CHANNEL_ID, "zigbee2mqtt/sensor/state", "", "value",
69                 OnOffValue.class);
70
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);
79     }
80
81     @Test
82     public void offDelayTest() {
83         // @formatter:off
84         var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
85                 "{ " +
86                         "  \"availability\": [ " +
87                         "    { " +
88                         "      \"topic\": \"zigbee2mqtt/bridge/state\" " +
89                         "    } " +
90                         "  ], " +
91                         "  \"device\": { " +
92                         "    \"identifiers\": [ " +
93                         "      \"zigbee2mqtt_0x0000000000000000\" " +
94                         "    ], " +
95                         "    \"manufacturer\": \"Sensors inc\", " +
96                         "    \"model\": \"On Off Sensor\", " +
97                         "    \"name\": \"OnOffSensor\", " +
98                         "    \"sw_version\": \"Zigbee2MQTT 1.18.2\" " +
99                         "  }, " +
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 }}\" " +
108                         "}");
109         // @formatter:on
110
111         publishMessage("zigbee2mqtt/sensor/state", "{ \"state\": \"ON_\" }");
112         assertState(component, BinarySensor.SENSOR_CHANNEL_ID, OnOffType.ON);
113
114         waitForAssert(() -> assertState(component, BinarySensor.SENSOR_CHANNEL_ID, OnOffType.OFF), 10000, 200);
115     }
116
117     @Test
118     public void expireAfterTest() {
119         // @formatter:off
120         var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
121                 "{ " +
122                         "  \"availability\": [ " +
123                         "    { " +
124                         "      \"topic\": \"zigbee2mqtt/bridge/state\" " +
125                         "    } " +
126                         "  ], " +
127                         "  \"device\": { " +
128                         "    \"identifiers\": [ " +
129                         "      \"zigbee2mqtt_0x0000000000000000\" " +
130                         "    ], " +
131                         "    \"manufacturer\": \"Sensors inc\", " +
132                         "    \"model\": \"On Off Sensor\", " +
133                         "    \"name\": \"OnOffSensor\", " +
134                         "    \"sw_version\": \"Zigbee2MQTT 1.18.2\" " +
135                         "  }, " +
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 }}\" " +
144                         "}");
145         // @formatter:on
146
147         publishMessage("zigbee2mqtt/sensor/state", "{ \"state\": \"OFF_\" }");
148         assertState(component, BinarySensor.SENSOR_CHANNEL_ID, OnOffType.OFF);
149
150         waitForAssert(() -> assertState(component, BinarySensor.SENSOR_CHANNEL_ID, UnDefType.UNDEF), 10000, 200);
151     }
152
153     @Override
154     protected Set<String> getConfigTopics() {
155         return Set.of(CONFIG_TOPIC);
156     }
157 }