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