]> git.basschouten.com Git - openhab-addons.git/blob
881294f36bd008a6ae66469a104ae53bd5c8694b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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
18 import java.util.Set;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.junit.jupiter.api.Test;
22 import org.openhab.binding.mqtt.generic.values.OnOffValue;
23 import org.openhab.core.library.types.OnOffType;
24
25 /**
26  * Tests for {@link Switch}
27  *
28  * @author Anton Kharuzhy - Initial contribution
29  */
30 @NonNullByDefault
31 public class SwitchTests extends AbstractComponentTests {
32     public static final String CONFIG_TOPIC = "switch/0x847127fffe11dd6a_auto_lock_zigbee2mqtt";
33
34     @SuppressWarnings("null")
35     @Test
36     public void testSwitchWithStateAndCommand() {
37         var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC), """
38                 {
39                   "availability": [
40                     {
41                       "topic": "zigbee2mqtt/bridge/state"
42                     }
43                   ],
44                   "command_topic": "zigbee2mqtt/th1/set/auto_lock",
45                   "device": {
46                     "identifiers": [
47                       "zigbee2mqtt_0x847127fffe11dd6a"
48                     ],
49                     "manufacturer": "TuYa",
50                     "model": "Radiator valve with thermostat (TS0601_thermostat)",
51                     "name": "th1",
52                     "sw_version": "Zigbee2MQTT 1.18.2"
53                   },
54                   "json_attributes_topic": "zigbee2mqtt/th1",
55                   "name": "th1 auto lock",
56                   "payload_off": "MANUAL",
57                   "payload_on": "AUTO",
58                   "state_off": "MANUAL",
59                   "state_on": "AUTO",
60                   "state_topic": "zigbee2mqtt/th1",
61                   "unique_id": "0x847127fffe11dd6a_auto_lock_zigbee2mqtt",
62                   "value_template": "{{ value_json.auto_lock }}"
63                 }\
64                 """);
65
66         assertThat(component.channels.size(), is(1));
67         assertThat(component.getName(), is("th1 auto lock"));
68
69         assertChannel(component, Switch.SWITCH_CHANNEL_ID, "zigbee2mqtt/th1", "zigbee2mqtt/th1/set/auto_lock", "state",
70                 OnOffValue.class);
71
72         publishMessage("zigbee2mqtt/th1", "{\"auto_lock\": \"MANUAL\"}");
73         assertState(component, Switch.SWITCH_CHANNEL_ID, OnOffType.OFF);
74         publishMessage("zigbee2mqtt/th1", "{\"auto_lock\": \"AUTO\"}");
75         assertState(component, Switch.SWITCH_CHANNEL_ID, OnOffType.ON);
76
77         component.getChannel(Switch.SWITCH_CHANNEL_ID).getState().publishValue(OnOffType.OFF);
78         assertPublished("zigbee2mqtt/th1/set/auto_lock", "MANUAL");
79         component.getChannel(Switch.SWITCH_CHANNEL_ID).getState().publishValue(OnOffType.ON);
80         assertPublished("zigbee2mqtt/th1/set/auto_lock", "AUTO");
81     }
82
83     @Test
84     public void testSwitchWithState() {
85         var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC), """
86                 {
87                   "availability": [
88                     {
89                       "topic": "zigbee2mqtt/bridge/state"
90                     }
91                   ],
92                   "device": {
93                     "identifiers": [
94                       "zigbee2mqtt_0x847127fffe11dd6a"
95                     ],
96                     "manufacturer": "TuYa",
97                     "model": "Radiator valve with thermostat (TS0601_thermostat)",
98                     "name": "th1",
99                     "sw_version": "Zigbee2MQTT 1.18.2"
100                   },
101                   "json_attributes_topic": "zigbee2mqtt/th1",
102                   "name": "th1 auto lock",
103                   "state_off": "MANUAL",
104                   "state_on": "AUTO",
105                   "state_topic": "zigbee2mqtt/th1",
106                   "unique_id": "0x847127fffe11dd6a_auto_lock_zigbee2mqtt",
107                   "value_template": "{{ value_json.auto_lock }}"
108                 }\
109                 """);
110
111         assertThat(component.channels.size(), is(1));
112         assertThat(component.getName(), is("th1 auto lock"));
113
114         assertChannel(component, Switch.SWITCH_CHANNEL_ID, "zigbee2mqtt/th1", "", "state", OnOffValue.class);
115
116         publishMessage("zigbee2mqtt/th1", "{\"auto_lock\": \"MANUAL\"}");
117         assertState(component, Switch.SWITCH_CHANNEL_ID, OnOffType.OFF);
118         publishMessage("zigbee2mqtt/th1", "{\"auto_lock\": \"AUTO\"}");
119         assertState(component, Switch.SWITCH_CHANNEL_ID, OnOffType.ON);
120     }
121
122     @SuppressWarnings("null")
123     @Test
124     public void testSwitchWithCommand() {
125         var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC), """
126                 {
127                   "availability": [
128                     {
129                       "topic": "zigbee2mqtt/bridge/state"
130                     }
131                   ],
132                   "command_topic": "zigbee2mqtt/th1/set/auto_lock",
133                   "device": {
134                     "identifiers": [
135                       "zigbee2mqtt_0x847127fffe11dd6a"
136                     ],
137                     "manufacturer": "TuYa",
138                     "model": "Radiator valve with thermostat (TS0601_thermostat)",
139                     "name": "th1",
140                     "sw_version": "Zigbee2MQTT 1.18.2"
141                   },
142                   "json_attributes_topic": "zigbee2mqtt/th1",
143                   "name": "th1 auto lock",
144                   "payload_off": "MANUAL",
145                   "payload_on": "AUTO",
146                   "unique_id": "0x847127fffe11dd6a_auto_lock_zigbee2mqtt",
147                   "value_template": "{{ value_json.auto_lock }}"
148                 }\
149                 """);
150
151         assertThat(component.channels.size(), is(1));
152         assertThat(component.getName(), is("th1 auto lock"));
153
154         assertChannel(component, Switch.SWITCH_CHANNEL_ID, "", "zigbee2mqtt/th1/set/auto_lock", "state",
155                 OnOffValue.class);
156
157         component.getChannel(Switch.SWITCH_CHANNEL_ID).getState().publishValue(OnOffType.OFF);
158         assertPublished("zigbee2mqtt/th1/set/auto_lock", "MANUAL");
159         component.getChannel(Switch.SWITCH_CHANNEL_ID).getState().publishValue(OnOffType.ON);
160         assertPublished("zigbee2mqtt/th1/set/auto_lock", "AUTO");
161     }
162
163     @Override
164     protected Set<String> getConfigTopics() {
165         return Set.of(CONFIG_TOPIC);
166     }
167 }