]> git.basschouten.com Git - openhab-addons.git/blob
64c6e23026c80f889c6a40e68205c23426ac6e9e
[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 Select}
28  *
29  * @author Cody Cutrer - Initial contribution
30  */
31 @NonNullByDefault
32 public class SelectTests extends AbstractComponentTests {
33     public static final String CONFIG_TOPIC = "select/0x54ef44100064b266";
34
35     @SuppressWarnings("null")
36     @Test
37     public void testSelectWithStateAndCommand() {
38         var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC), """
39                     {
40                         "availability": [
41                             {"topic": "zigbee2mqtt/bridge/state"},
42                             {"topic": "zigbee2mqtt/gbos/availability"}
43                         ],
44                         "availability_mode": "all",
45                         "command_topic": "zigbee2mqtt/gbos/set/approach_distance",
46                         "device": {
47                             "configuration_url": "#/device/0x54ef44100064b266/info",
48                             "identifiers": [
49                                 "zigbee2mqtt_0x54ef44100064b266"
50                             ],
51                             "manufacturer": "Xiaomi",
52                             "model": "Aqara presence detector FP1 (RTCZCGQ11LM)",
53                             "name": "Guest Bathroom Occupancy Sensor",
54                             "sw_version": ""
55                         },
56                         "name": "Guest Bathroom Occupancy Sensor approach distance",
57                         "options": [
58                             "far",
59                             "medium",
60                             "near"
61                         ],
62                         "state_topic": "zigbee2mqtt/gbos",
63                         "unique_id": "0x54ef44100064b266_approach_distance_zigbee2mqtt",
64                         "value_template":"{{ value_json.approach_distance }}"
65                     }
66                 """);
67
68         assertThat(component.channels.size(), is(1));
69         assertThat(component.getName(), is("Guest Bathroom Occupancy Sensor approach distance"));
70
71         assertChannel(component, Select.SELECT_CHANNEL_ID, "zigbee2mqtt/gbos", "zigbee2mqtt/gbos/set/approach_distance",
72                 "Guest Bathroom Occupancy Sensor approach distance", TextValue.class);
73
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"));
78
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");
86     }
87
88     @SuppressWarnings("null")
89     @Test
90     public void testSelectWithCommandTemplate() {
91         var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC), """
92                     {
93                         "availability": [
94                             {"topic": "zigbee2mqtt/bridge/state"},
95                             {"topic": "zigbee2mqtt/gbos/availability"}
96                         ],
97                         "availability_mode": "all",
98                         "command_topic": "zigbee2mqtt/gbos/set/approach_distance",
99                         "command_template": "set to {{ value }}",
100                         "device": {
101                             "configuration_url": "#/device/0x54ef44100064b266/info",
102                             "identifiers": [
103                                 "zigbee2mqtt_0x54ef44100064b266"
104                             ],
105                             "manufacturer": "Xiaomi",
106                             "model": "Aqara presence detector FP1 (RTCZCGQ11LM)",
107                             "name": "Guest Bathroom Occupancy Sensor",
108                             "sw_version": ""
109                         },
110                         "name": "Guest Bathroom Occupancy Sensor approach distance",
111                         "options": [
112                             "far",
113                             "medium",
114                             "near"
115                         ],
116                         "state_topic": "zigbee2mqtt/gbos",
117                         "unique_id": "0x54ef44100064b266_approach_distance_zigbee2mqtt",
118                         "value_template":"{{ value_json.approach_distance }}"
119                     }
120                 """);
121
122         component.getChannel(Select.SELECT_CHANNEL_ID).getState().publishValue(new StringType("near"));
123         assertPublished("zigbee2mqtt/gbos/set/approach_distance", "set to near");
124     }
125
126     @Override
127     protected Set<String> getConfigTopics() {
128         return Set.of(CONFIG_TOPIC);
129     }
130 }