2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mqtt.homeassistant.internal.component;
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.junit.jupiter.api.Assertions.assertThrows;
19 import java.util.Objects;
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;
29 * Tests for {@link Button}
31 * @author Cody Cutrer - Initial contribution
34 public class ButtonTests extends AbstractComponentTests {
35 public static final String CONFIG_TOPIC = "button/0x847127fffe11dd6a_auto_lock_zigbee2mqtt";
37 @SuppressWarnings("null")
39 public void testButton() {
40 var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC), """
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",
50 "name":"Single Car Garage Door Opener",
51 "sw":"esphome v2023.10.4 Nov 1 2023, 09:27:02",
57 assertThat(component.channels.size(), is(1));
58 assertThat(component.getName(), is("Restart"));
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));
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");
73 protected Set<String> getConfigTopics() {
74 return Set.of(CONFIG_TOPIC);