]> git.basschouten.com Git - openhab-addons.git/blob
910cb78130491f524e929ed9a274948d27822f63
[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.RollershutterValue;
22 import org.openhab.core.library.types.PercentType;
23 import org.openhab.core.library.types.StopMoveType;
24
25 /**
26  * Tests for {@link Cover}
27  *
28  * @author Anton Kharuzhy - Initial contribution
29  */
30 @SuppressWarnings("ConstantConditions")
31 public class CoverTests extends AbstractComponentTests {
32     public static final String CONFIG_TOPIC = "cover/0x0000000000000000_cover_zigbee2mqtt";
33
34     @Test
35     public void test() throws InterruptedException {
36         // @formatter:off
37         var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
38                 "{ " +
39                         "  \"availability\": [ " +
40                         "    { " +
41                         "      \"topic\": \"zigbee2mqtt/bridge/state\" " +
42                         "    } " +
43                         "  ], " +
44                         "  \"device\": { " +
45                         "    \"identifiers\": [ " +
46                         "      \"zigbee2mqtt_0x0000000000000000\" " +
47                         "    ], " +
48                         "    \"manufacturer\": \"Covers inc\", " +
49                         "    \"model\": \"cover v1\", " +
50                         "    \"name\": \"Cover\", " +
51                         "    \"sw_version\": \"Zigbee2MQTT 1.18.2\" " +
52                         "  }, " +
53                         "  \"name\": \"cover\", " +
54                         "  \"payload_open\": \"OPEN_\", " +
55                         "  \"payload_close\": \"CLOSE_\", " +
56                         "  \"payload_stop\": \"STOP_\", " +
57                         "  \"state_topic\": \"zigbee2mqtt/cover/state\", " +
58                         "  \"command_topic\": \"zigbee2mqtt/cover/set/state\" " +
59                         "}");
60         // @formatter:on
61
62         assertThat(component.channels.size(), is(1));
63         assertThat(component.getName(), is("cover"));
64
65         assertChannel(component, Cover.SWITCH_CHANNEL_ID, "zigbee2mqtt/cover/state", "zigbee2mqtt/cover/set/state",
66                 "cover", RollershutterValue.class);
67
68         publishMessage("zigbee2mqtt/cover/state", "100");
69         assertState(component, Cover.SWITCH_CHANNEL_ID, PercentType.HUNDRED);
70         publishMessage("zigbee2mqtt/cover/state", "0");
71         assertState(component, Cover.SWITCH_CHANNEL_ID, PercentType.ZERO);
72
73         component.getChannel(Cover.SWITCH_CHANNEL_ID).getState().publishValue(PercentType.ZERO);
74         assertPublished("zigbee2mqtt/cover/set/state", "OPEN_");
75         component.getChannel(Cover.SWITCH_CHANNEL_ID).getState().publishValue(PercentType.HUNDRED);
76         assertPublished("zigbee2mqtt/cover/set/state", "CLOSE_");
77         component.getChannel(Cover.SWITCH_CHANNEL_ID).getState().publishValue(StopMoveType.STOP);
78         assertPublished("zigbee2mqtt/cover/set/state", "STOP_");
79         component.getChannel(Cover.SWITCH_CHANNEL_ID).getState().publishValue(PercentType.ZERO);
80         assertPublished("zigbee2mqtt/cover/set/state", "OPEN_", 2);
81         component.getChannel(Cover.SWITCH_CHANNEL_ID).getState().publishValue(StopMoveType.STOP);
82         assertPublished("zigbee2mqtt/cover/set/state", "STOP_", 2);
83     }
84
85     protected Set<String> getConfigTopics() {
86         return Set.of(CONFIG_TOPIC);
87     }
88 }