]> git.basschouten.com Git - openhab-addons.git/blob
c0efaae9a4fe1735f96cba608f1d4c1087137bc2
[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
24 /**
25  * Tests for {@link Lock}
26  *
27  * @author Anton Kharuzhy - Initial contribution
28  */
29 @SuppressWarnings("ALL")
30 public class LockTests extends AbstractComponentTests {
31     public static final String CONFIG_TOPIC = "lock/0x0000000000000000_lock_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\": \"Locks inc\", " +
48                         "    \"model\": \"Lock\", " +
49                         "    \"name\": \"LockBlower\", " +
50                         "    \"sw_version\": \"Zigbee2MQTT 1.18.2\" " +
51                         "  }, " +
52                         "  \"name\": \"lock\", " +
53                         "  \"payload_unlock\": \"UNLOCK_\", " +
54                         "  \"payload_lock\": \"LOCK_\", " +
55                         "  \"state_topic\": \"zigbee2mqtt/lock/state\", " +
56                         "  \"command_topic\": \"zigbee2mqtt/lock/set/state\" " +
57                         "}");
58         // @formatter:on
59
60         assertThat(component.channels.size(), is(1));
61         assertThat(component.getName(), is("lock"));
62
63         assertChannel(component, Lock.SWITCH_CHANNEL_ID, "zigbee2mqtt/lock/state", "zigbee2mqtt/lock/set/state", "lock",
64                 OnOffValue.class);
65
66         publishMessage("zigbee2mqtt/lock/state", "LOCK_");
67         assertState(component, Lock.SWITCH_CHANNEL_ID, OnOffType.ON);
68         publishMessage("zigbee2mqtt/lock/state", "LOCK_");
69         assertState(component, Lock.SWITCH_CHANNEL_ID, OnOffType.ON);
70         publishMessage("zigbee2mqtt/lock/state", "UNLOCK_");
71         assertState(component, Lock.SWITCH_CHANNEL_ID, OnOffType.OFF);
72         publishMessage("zigbee2mqtt/lock/state", "LOCK_");
73         assertState(component, Lock.SWITCH_CHANNEL_ID, OnOffType.ON);
74
75         component.getChannel(Lock.SWITCH_CHANNEL_ID).getState().publishValue(OnOffType.OFF);
76         assertPublished("zigbee2mqtt/lock/set/state", "UNLOCK_");
77         component.getChannel(Lock.SWITCH_CHANNEL_ID).getState().publishValue(OnOffType.ON);
78         assertPublished("zigbee2mqtt/lock/set/state", "LOCK_");
79     }
80
81     @Test
82     public void forceOptimisticIsNotSupported() {
83         // @formatter:off
84         publishMessage(configTopicToMqtt(CONFIG_TOPIC),
85                 "{ " +
86                         "  \"availability\": [ " +
87                         "    { " +
88                         "      \"topic\": \"zigbee2mqtt/bridge/state\" " +
89                         "    } " +
90                         "  ], " +
91                         "  \"device\": { " +
92                         "    \"identifiers\": [ " +
93                         "      \"zigbee2mqtt_0x0000000000000000\" " +
94                         "    ], " +
95                         "    \"manufacturer\": \"Locks inc\", " +
96                         "    \"model\": \"Lock\", " +
97                         "    \"name\": \"LockBlower\", " +
98                         "    \"sw_version\": \"Zigbee2MQTT 1.18.2\" " +
99                         "  }, " +
100                         "  \"name\": \"lock\", " +
101                         "  \"payload_unlock\": \"UNLOCK_\", " +
102                         "  \"payload_lock\": \"LOCK_\", " +
103                         "  \"optimistic\": \"true\", " +
104                         "  \"state_topic\": \"zigbee2mqtt/lock/state\", " +
105                         "  \"command_topic\": \"zigbee2mqtt/lock/set/state\" " +
106                         "}");
107         // @formatter:on
108     }
109
110     @Override
111     protected Set<String> getConfigTopics() {
112         return Set.of(CONFIG_TOPIC);
113     }
114 }