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