]> git.basschouten.com Git - openhab-addons.git/blob
8b9ef9f29282b51d12df651d9356254f2a58ac08
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.Rule;
21 import org.junit.jupiter.api.Test;
22 import org.junit.rules.ExpectedException;
23 import org.openhab.binding.mqtt.generic.values.OnOffValue;
24 import org.openhab.core.library.types.OnOffType;
25
26 /**
27  * Tests for {@link Lock}
28  *
29  * @author Anton Kharuzhy - Initial contribution
30  */
31 @SuppressWarnings("ALL")
32 public class LockTests extends AbstractComponentTests {
33     public static final String CONFIG_TOPIC = "lock/0x0000000000000000_lock_zigbee2mqtt";
34
35     @Rule
36     public ExpectedException exceptionGrabber = ExpectedException.none();
37
38     @Test
39     public void test() throws InterruptedException {
40         // @formatter:off
41         var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
42                 "{ " +
43                         "  \"availability\": [ " +
44                         "    { " +
45                         "      \"topic\": \"zigbee2mqtt/bridge/state\" " +
46                         "    } " +
47                         "  ], " +
48                         "  \"device\": { " +
49                         "    \"identifiers\": [ " +
50                         "      \"zigbee2mqtt_0x0000000000000000\" " +
51                         "    ], " +
52                         "    \"manufacturer\": \"Locks inc\", " +
53                         "    \"model\": \"Lock\", " +
54                         "    \"name\": \"LockBlower\", " +
55                         "    \"sw_version\": \"Zigbee2MQTT 1.18.2\" " +
56                         "  }, " +
57                         "  \"name\": \"lock\", " +
58                         "  \"payload_unlock\": \"UNLOCK_\", " +
59                         "  \"payload_lock\": \"LOCK_\", " +
60                         "  \"state_topic\": \"zigbee2mqtt/lock/state\", " +
61                         "  \"command_topic\": \"zigbee2mqtt/lock/set/state\" " +
62                         "}");
63         // @formatter:on
64
65         assertThat(component.channels.size(), is(1));
66         assertThat(component.getName(), is("lock"));
67
68         assertChannel(component, Lock.SWITCH_CHANNEL_ID, "zigbee2mqtt/lock/state", "zigbee2mqtt/lock/set/state", "lock",
69                 OnOffValue.class);
70
71         publishMessage("zigbee2mqtt/lock/state", "LOCK_");
72         assertState(component, Lock.SWITCH_CHANNEL_ID, OnOffType.ON);
73         publishMessage("zigbee2mqtt/lock/state", "LOCK_");
74         assertState(component, Lock.SWITCH_CHANNEL_ID, OnOffType.ON);
75         publishMessage("zigbee2mqtt/lock/state", "UNLOCK_");
76         assertState(component, Lock.SWITCH_CHANNEL_ID, OnOffType.OFF);
77         publishMessage("zigbee2mqtt/lock/state", "LOCK_");
78         assertState(component, Lock.SWITCH_CHANNEL_ID, OnOffType.ON);
79
80         component.getChannel(Lock.SWITCH_CHANNEL_ID).getState().publishValue(OnOffType.OFF);
81         assertPublished("zigbee2mqtt/lock/set/state", "UNLOCK_");
82         component.getChannel(Lock.SWITCH_CHANNEL_ID).getState().publishValue(OnOffType.ON);
83         assertPublished("zigbee2mqtt/lock/set/state", "LOCK_");
84     }
85
86     @Test
87     public void forceOptimisticIsNotSupported() {
88         exceptionGrabber.expect(UnsupportedOperationException.class);
89
90         // @formatter:off
91         publishMessage(configTopicToMqtt(CONFIG_TOPIC),
92                 "{ " +
93                         "  \"availability\": [ " +
94                         "    { " +
95                         "      \"topic\": \"zigbee2mqtt/bridge/state\" " +
96                         "    } " +
97                         "  ], " +
98                         "  \"device\": { " +
99                         "    \"identifiers\": [ " +
100                         "      \"zigbee2mqtt_0x0000000000000000\" " +
101                         "    ], " +
102                         "    \"manufacturer\": \"Locks inc\", " +
103                         "    \"model\": \"Lock\", " +
104                         "    \"name\": \"LockBlower\", " +
105                         "    \"sw_version\": \"Zigbee2MQTT 1.18.2\" " +
106                         "  }, " +
107                         "  \"name\": \"lock\", " +
108                         "  \"payload_unlock\": \"UNLOCK_\", " +
109                         "  \"payload_lock\": \"LOCK_\", " +
110                         "  \"optimistic\": \"true\", " +
111                         "  \"state_topic\": \"zigbee2mqtt/lock/state\", " +
112                         "  \"command_topic\": \"zigbee2mqtt/lock/set/state\" " +
113                         "}");
114         // @formatter:on
115     }
116
117     protected Set<String> getConfigTopics() {
118         return Set.of(CONFIG_TOPIC);
119     }
120 }