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