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;
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;
27 * Tests for {@link Select}
29 * @author Cody Cutrer - Initial contribution
32 public class SelectTests extends AbstractComponentTests {
33 public static final String CONFIG_TOPIC = "select/0x54ef44100064b266";
35 @SuppressWarnings("null")
37 public void testSelectWithStateAndCommand() {
38 var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC), """
41 {"topic": "zigbee2mqtt/bridge/state"},
42 {"topic": "zigbee2mqtt/gbos/availability"}
44 "availability_mode": "all",
45 "command_topic": "zigbee2mqtt/gbos/set/approach_distance",
47 "configuration_url": "#/device/0x54ef44100064b266/info",
49 "zigbee2mqtt_0x54ef44100064b266"
51 "manufacturer": "Xiaomi",
52 "model": "Aqara presence detector FP1 (RTCZCGQ11LM)",
53 "name": "Guest Bathroom Occupancy Sensor",
56 "name": "Guest Bathroom Occupancy Sensor approach distance",
62 "state_topic": "zigbee2mqtt/gbos",
63 "unique_id": "0x54ef44100064b266_approach_distance_zigbee2mqtt",
64 "value_template":"{{ value_json.approach_distance }}"
68 assertThat(component.channels.size(), is(1));
69 assertThat(component.getName(), is("Guest Bathroom Occupancy Sensor approach distance"));
71 assertChannel(component, Select.SELECT_CHANNEL_ID, "zigbee2mqtt/gbos", "zigbee2mqtt/gbos/set/approach_distance",
72 "Guest Bathroom Occupancy Sensor approach distance", TextValue.class);
74 publishMessage("zigbee2mqtt/gbos", "{\"approach_distance\": \"far\"}");
75 assertState(component, Select.SELECT_CHANNEL_ID, new StringType("far"));
76 publishMessage("zigbee2mqtt/gbos", "{\"approach_distance\": \"medium\"}");
77 assertState(component, Select.SELECT_CHANNEL_ID, new StringType("medium"));
79 component.getChannel(Select.SELECT_CHANNEL_ID).getState().publishValue(new StringType("near"));
80 assertPublished("zigbee2mqtt/gbos/set/approach_distance", "near");
81 component.getChannel(Select.SELECT_CHANNEL_ID).getState().publishValue(new StringType("medium"));
82 assertPublished("zigbee2mqtt/gbos/set/approach_distance", "medium");
83 assertThrows(IllegalArgumentException.class,
84 () -> component.getChannel(Select.SELECT_CHANNEL_ID).getState().publishValue(new StringType("bogus")));
85 assertNotPublished("zigbee2mqtt/gbos/set/approach_distance", "bogus");
88 @SuppressWarnings("null")
90 public void testSelectWithCommandTemplate() {
91 var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC), """
94 {"topic": "zigbee2mqtt/bridge/state"},
95 {"topic": "zigbee2mqtt/gbos/availability"}
97 "availability_mode": "all",
98 "command_topic": "zigbee2mqtt/gbos/set/approach_distance",
99 "command_template": "set to {{ value }}",
101 "configuration_url": "#/device/0x54ef44100064b266/info",
103 "zigbee2mqtt_0x54ef44100064b266"
105 "manufacturer": "Xiaomi",
106 "model": "Aqara presence detector FP1 (RTCZCGQ11LM)",
107 "name": "Guest Bathroom Occupancy Sensor",
110 "name": "Guest Bathroom Occupancy Sensor approach distance",
116 "state_topic": "zigbee2mqtt/gbos",
117 "unique_id": "0x54ef44100064b266_approach_distance_zigbee2mqtt",
118 "value_template":"{{ value_json.approach_distance }}"
122 component.getChannel(Select.SELECT_CHANNEL_ID).getState().publishValue(new StringType("near"));
123 assertPublished("zigbee2mqtt/gbos/set/approach_distance", "set to near");
127 protected Set<String> getConfigTopics() {
128 return Set.of(CONFIG_TOPIC);