]> git.basschouten.com Git - openhab-addons.git/blob
9fdce64d12a161de4aa22f18584471322d3a50b7
[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 import static org.junit.jupiter.api.Assertions.assertThrows;
18
19 import java.util.Objects;
20 import java.util.Set;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.junit.jupiter.api.Test;
24 import org.openhab.binding.mqtt.generic.values.TextValue;
25 import org.openhab.core.library.types.StringType;
26 import org.openhab.core.thing.type.AutoUpdatePolicy;
27
28 /**
29  * Tests for {@link Button}
30  *
31  * @author Cody Cutrer - Initial contribution
32  */
33 @NonNullByDefault
34 public class ButtonTests extends AbstractComponentTests {
35     public static final String CONFIG_TOPIC = "button/0x847127fffe11dd6a_auto_lock_zigbee2mqtt";
36
37     @SuppressWarnings("null")
38     @Test
39     public void testButton() {
40         var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC), """
41                   {
42                     "dev_cla":"restart",
43                     "name":"Restart",
44                     "entity_category":"config",
45                     "cmd_t":"esphome/single-car-gdo/button/restart/command",
46                     "avty_t":"esphome/single-car-gdo/status",
47                     "uniq_id":"78e36d645710-button-ba0e8e32",
48                     "dev":{
49                       "ids":"78e36d645710",
50                       "name":"Single Car Garage Door Opener",
51                       "sw":"esphome v2023.10.4 Nov  1 2023, 09:27:02",
52                       "mdl":"esp32dev",
53                       "mf":"espressif"}
54                     }
55                 """);
56
57         assertThat(component.channels.size(), is(1));
58         assertThat(component.getName(), is("Restart"));
59
60         assertChannel(component, Button.BUTTON_CHANNEL_ID, "", "esphome/single-car-gdo/button/restart/command",
61                 "Restart", TextValue.class);
62         assertThat(Objects.requireNonNull(component.getChannel(Button.BUTTON_CHANNEL_ID)).getChannel()
63                 .getAutoUpdatePolicy(), is(AutoUpdatePolicy.VETO));
64
65         assertThrows(IllegalArgumentException.class,
66                 () -> component.getChannel(Button.BUTTON_CHANNEL_ID).getState().publishValue(new StringType("ON")));
67         assertNothingPublished("esphome/single-car-gdo/button/restart/command");
68         component.getChannel(Button.BUTTON_CHANNEL_ID).getState().publishValue(new StringType("PRESS"));
69         assertPublished("esphome/single-car-gdo/button/restart/command", "PRESS");
70     }
71
72     @Override
73     protected Set<String> getConfigTopics() {
74         return Set.of(CONFIG_TOPIC);
75     }
76 }