]> git.basschouten.com Git - openhab-addons.git/blob
3286b8141c59a16f3c64c07c56f858fb7a678609
[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 Fan}
27  *
28  * @author Anton Kharuzhy - Initial contribution
29  */
30 @NonNullByDefault
31 public class FanTests extends AbstractComponentTests {
32     public static final String CONFIG_TOPIC = "fan/0x0000000000000000_fan_zigbee2mqtt";
33
34     @SuppressWarnings("null")
35     @Test
36     public void test() throws InterruptedException {
37         // @formatter:off
38         var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
39                 """
40                 { \
41                   "availability": [ \
42                     { \
43                       "topic": "zigbee2mqtt/bridge/state" \
44                     } \
45                   ], \
46                   "device": { \
47                     "identifiers": [ \
48                       "zigbee2mqtt_0x0000000000000000" \
49                     ], \
50                     "manufacturer": "Fans inc", \
51                     "model": "Fan", \
52                     "name": "FanBlower", \
53                     "sw_version": "Zigbee2MQTT 1.18.2" \
54                   }, \
55                   "name": "fan", \
56                   "payload_off": "OFF_", \
57                   "payload_on": "ON_", \
58                   "state_topic": "zigbee2mqtt/fan/state", \
59                   "command_topic": "zigbee2mqtt/fan/set/state" \
60                 }\
61                 """);
62         // @formatter:on
63
64         assertThat(component.channels.size(), is(1));
65         assertThat(component.getName(), is("fan"));
66
67         assertChannel(component, Fan.SWITCH_CHANNEL_ID, "zigbee2mqtt/fan/state", "zigbee2mqtt/fan/set/state", "fan",
68                 OnOffValue.class);
69
70         publishMessage("zigbee2mqtt/fan/state", "ON_");
71         assertState(component, Fan.SWITCH_CHANNEL_ID, OnOffType.ON);
72         publishMessage("zigbee2mqtt/fan/state", "ON_");
73         assertState(component, Fan.SWITCH_CHANNEL_ID, OnOffType.ON);
74         publishMessage("zigbee2mqtt/fan/state", "OFF_");
75         assertState(component, Fan.SWITCH_CHANNEL_ID, OnOffType.OFF);
76         publishMessage("zigbee2mqtt/fan/state", "ON_");
77         assertState(component, Fan.SWITCH_CHANNEL_ID, OnOffType.ON);
78
79         component.getChannel(Fan.SWITCH_CHANNEL_ID).getState().publishValue(OnOffType.OFF);
80         assertPublished("zigbee2mqtt/fan/set/state", "OFF_");
81         component.getChannel(Fan.SWITCH_CHANNEL_ID).getState().publishValue(OnOffType.ON);
82         assertPublished("zigbee2mqtt/fan/set/state", "ON_");
83     }
84
85     @SuppressWarnings("null")
86     @Test
87     public void testCommandTemplate() throws InterruptedException {
88         var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC), """
89                         {
90                             "availability": [
91                             {
92                                 "topic": "zigbee2mqtt/bridge/state"
93                             }
94                             ],
95                             "device": {
96                             "identifiers": [
97                                 "zigbee2mqtt_0x0000000000000000"
98                             ],
99                             "manufacturer": "Fans inc",
100                             "model": "Fan",
101                             "name": "FanBlower",
102                             "sw_version": "Zigbee2MQTT 1.18.2"
103                             },
104                             "name": "fan",
105                             "payload_off": "OFF_",
106                             "payload_on": "ON_",
107                             "state_topic": "zigbee2mqtt/fan/state",
108                             "command_topic": "zigbee2mqtt/fan/set/state",
109                             "command_template": "set to {{ value }}"
110                         }
111                 """);
112
113         assertThat(component.channels.size(), is(1));
114
115         component.getChannel(Fan.SWITCH_CHANNEL_ID).getState().publishValue(OnOffType.OFF);
116         assertPublished("zigbee2mqtt/fan/set/state", "set to OFF_");
117     }
118
119     @Override
120     protected Set<String> getConfigTopics() {
121         return Set.of(CONFIG_TOPIC);
122     }
123 }