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;
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;
26 * Tests for {@link Switch}
28 * @author Anton Kharuzhy - Initial contribution
31 public class SwitchTests extends AbstractComponentTests {
32 public static final String CONFIG_TOPIC = "switch/0x847127fffe11dd6a_auto_lock_zigbee2mqtt";
34 @SuppressWarnings("null")
36 public void testSwitchWithStateAndCommand() {
37 var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC), """
41 "topic": "zigbee2mqtt/bridge/state"
44 "command_topic": "zigbee2mqtt/th1/set/auto_lock",
47 "zigbee2mqtt_0x847127fffe11dd6a"
49 "manufacturer": "TuYa",
50 "model": "Radiator valve with thermostat (TS0601_thermostat)",
52 "sw_version": "Zigbee2MQTT 1.18.2"
54 "json_attributes_topic": "zigbee2mqtt/th1",
55 "name": "th1 auto lock",
56 "payload_off": "MANUAL",
58 "state_off": "MANUAL",
60 "state_topic": "zigbee2mqtt/th1",
61 "unique_id": "0x847127fffe11dd6a_auto_lock_zigbee2mqtt",
62 "value_template": "{{ value_json.auto_lock }}"
66 assertThat(component.channels.size(), is(1));
67 assertThat(component.getName(), is("th1 auto lock"));
69 assertChannel(component, Switch.SWITCH_CHANNEL_ID, "zigbee2mqtt/th1", "zigbee2mqtt/th1/set/auto_lock", "state",
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);
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");
84 public void testSwitchWithState() {
85 var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC), """
89 "topic": "zigbee2mqtt/bridge/state"
94 "zigbee2mqtt_0x847127fffe11dd6a"
96 "manufacturer": "TuYa",
97 "model": "Radiator valve with thermostat (TS0601_thermostat)",
99 "sw_version": "Zigbee2MQTT 1.18.2"
101 "json_attributes_topic": "zigbee2mqtt/th1",
102 "name": "th1 auto lock",
103 "state_off": "MANUAL",
105 "state_topic": "zigbee2mqtt/th1",
106 "unique_id": "0x847127fffe11dd6a_auto_lock_zigbee2mqtt",
107 "value_template": "{{ value_json.auto_lock }}"
111 assertThat(component.channels.size(), is(1));
112 assertThat(component.getName(), is("th1 auto lock"));
114 assertChannel(component, Switch.SWITCH_CHANNEL_ID, "zigbee2mqtt/th1", "", "state", OnOffValue.class);
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);
122 @SuppressWarnings("null")
124 public void testSwitchWithCommand() {
125 var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC), """
129 "topic": "zigbee2mqtt/bridge/state"
132 "command_topic": "zigbee2mqtt/th1/set/auto_lock",
135 "zigbee2mqtt_0x847127fffe11dd6a"
137 "manufacturer": "TuYa",
138 "model": "Radiator valve with thermostat (TS0601_thermostat)",
140 "sw_version": "Zigbee2MQTT 1.18.2"
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 }}"
151 assertThat(component.channels.size(), is(1));
152 assertThat(component.getName(), is("th1 auto lock"));
154 assertChannel(component, Switch.SWITCH_CHANNEL_ID, "", "zigbee2mqtt/th1/set/auto_lock", "state",
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");
164 protected Set<String> getConfigTopics() {
165 return Set.of(CONFIG_TOPIC);