]> git.basschouten.com Git - openhab-addons.git/blob
e454d0d2154d8e6bf85e60ad4e7a0d705a187dd1
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
18
19 import java.io.BufferedReader;
20 import java.io.IOException;
21 import java.io.InputStreamReader;
22 import java.util.Arrays;
23 import java.util.List;
24
25 import org.eclipse.jdt.annotation.NonNull;
26 import org.junit.jupiter.api.Test;
27 import org.openhab.binding.mqtt.homeassistant.internal.config.ChannelConfigurationTypeAdapterFactory;
28 import org.openhab.binding.mqtt.homeassistant.internal.config.dto.AbstractChannelConfiguration;
29 import org.openhab.binding.mqtt.homeassistant.internal.config.dto.Connection;
30 import org.openhab.binding.mqtt.homeassistant.internal.config.dto.Device;
31
32 import com.google.gson.Gson;
33 import com.google.gson.GsonBuilder;
34
35 /**
36  * @author Jochen Klein - Initial contribution
37  */
38 public class HAConfigurationTests {
39
40     private Gson gson = new GsonBuilder().registerTypeAdapterFactory(new ChannelConfigurationTypeAdapterFactory())
41             .create();
42
43     private static String readTestJson(final String name) {
44         StringBuilder result = new StringBuilder();
45
46         try (BufferedReader in = new BufferedReader(
47                 new InputStreamReader(HAConfigurationTests.class.getResourceAsStream(name), "UTF-8"))) {
48             String line;
49
50             while ((line = in.readLine()) != null) {
51                 result.append(line).append('\n');
52             }
53             return result.toString();
54         } catch (IOException e) {
55             throw new RuntimeException(e);
56         }
57     }
58
59     @Test
60     public void testAbbreviations() {
61         String json = readTestJson("configA.json");
62
63         AbstractChannelConfiguration config = AbstractChannelConfiguration.fromString(json, gson);
64
65         assertThat(config.getName(), is("A"));
66         assertThat(config.getIcon(), is("2"));
67         assertThat(config.getQos(), is(1));
68         assertThat(config.isRetain(), is(true));
69         assertThat(config.getValueTemplate(), is("B"));
70         assertThat(config.getUniqueId(), is("C"));
71         assertThat(config.getAvailabilityTopic(), is("D/E"));
72         assertThat(config.getPayloadAvailable(), is("F"));
73         assertThat(config.getPayloadNotAvailable(), is("G"));
74
75         assertThat(config.getDevice(), is(notNullValue()));
76
77         Device device = config.getDevice();
78         if (device != null) {
79             assertThat(device.getIdentifiers(), contains("H"));
80             assertThat(device.getConnections(), is(notNullValue()));
81             List<@NonNull Connection> connections = device.getConnections();
82             if (connections != null) {
83                 assertThat(connections.get(0).getType(), is("I1"));
84                 assertThat(connections.get(0).getIdentifier(), is("I2"));
85             }
86             assertThat(device.getName(), is("J"));
87             assertThat(device.getModel(), is("K"));
88             assertThat(device.getSwVersion(), is("L"));
89             assertThat(device.getManufacturer(), is("M"));
90         }
91     }
92
93     @Test
94     public void testTildeSubstritution() {
95         String json = readTestJson("configB.json");
96
97         Switch.ChannelConfiguration config = AbstractChannelConfiguration.fromString(json, gson,
98                 Switch.ChannelConfiguration.class);
99
100         assertThat(config.getAvailabilityTopic(), is("D/E"));
101         assertThat(config.state_topic, is("O/D/"));
102         assertThat(config.command_topic, is("P~Q"));
103         assertThat(config.getDevice(), is(notNullValue()));
104
105         Device device = config.getDevice();
106         if (device != null) {
107             assertThat(device.getIdentifiers(), contains("H"));
108         }
109     }
110
111     @Test
112     public void testSampleFanConfig() {
113         String json = readTestJson("configFan.json");
114
115         Fan.ChannelConfiguration config = AbstractChannelConfiguration.fromString(json, gson,
116                 Fan.ChannelConfiguration.class);
117         assertThat(config.getName(), is("Bedroom Fan"));
118     }
119
120     @Test
121     public void testDeviceListConfig() {
122         String json = readTestJson("configDeviceList.json");
123
124         Fan.ChannelConfiguration config = AbstractChannelConfiguration.fromString(json, gson,
125                 Fan.ChannelConfiguration.class);
126         assertThat(config.getDevice(), is(notNullValue()));
127
128         Device device = config.getDevice();
129         if (device != null) {
130             assertThat(device.getIdentifiers(), is(Arrays.asList("A", "B", "C")));
131         }
132     }
133
134     @Test
135     public void testDeviceSingleStringConfig() {
136         String json = readTestJson("configDeviceSingleString.json");
137
138         Fan.ChannelConfiguration config = AbstractChannelConfiguration.fromString(json, gson,
139                 Fan.ChannelConfiguration.class);
140         assertThat(config.getDevice(), is(notNullValue()));
141
142         Device device = config.getDevice();
143         if (device != null) {
144             assertThat(device.getIdentifiers(), is(Arrays.asList("A")));
145         }
146     }
147
148     @Test
149     public void testTS0601ClimateConfig() {
150         String json = readTestJson("configTS0601ClimateThermostat.json");
151         Climate.ChannelConfiguration config = AbstractChannelConfiguration.fromString(json, gson,
152                 Climate.ChannelConfiguration.class);
153         assertThat(config.getDevice(), is(notNullValue()));
154         assertThat(config.getDevice().getIdentifiers(), is(notNullValue()));
155         assertThat(config.getDevice().getIdentifiers().get(0), is("zigbee2mqtt_0x847127fffe11dd6a"));
156         assertThat(config.getDevice().getManufacturer(), is("TuYa"));
157         assertThat(config.getDevice().getModel(), is("Radiator valve with thermostat (TS0601_thermostat)"));
158         assertThat(config.getDevice().getName(), is("th1"));
159         assertThat(config.getDevice().getSwVersion(), is("Zigbee2MQTT 1.18.2"));
160
161         assertThat(config.action_template, is(
162                 "{% set values = {'idle':'off','heat':'heating','cool':'cooling','fan only':'fan'} %}{{ values[value_json.running_state] }}"));
163         assertThat(config.action_topic, is("zigbee2mqtt/th1"));
164         assertThat(config.away_mode_command_topic, is("zigbee2mqtt/th1/set/away_mode"));
165         assertThat(config.away_mode_state_template, is("{{ value_json.away_mode }}"));
166         assertThat(config.away_mode_state_topic, is("zigbee2mqtt/th1"));
167         assertThat(config.current_temperature_template, is("{{ value_json.local_temperature }}"));
168         assertThat(config.current_temperature_topic, is("zigbee2mqtt/th1"));
169         assertThat(config.hold_command_topic, is("zigbee2mqtt/th1/set/preset"));
170         assertThat(config.hold_modes, is(List.of("schedule", "manual", "boost", "complex", "comfort", "eco")));
171         assertThat(config.hold_state_template, is("{{ value_json.preset }}"));
172         assertThat(config.hold_state_topic, is("zigbee2mqtt/th1"));
173         assertThat(config.json_attributes_topic, is("zigbee2mqtt/th1"));
174         assertThat(config.max_temp, is(35f));
175         assertThat(config.min_temp, is(5f));
176         assertThat(config.mode_command_topic, is("zigbee2mqtt/th1/set/system_mode"));
177         assertThat(config.mode_state_template, is("{{ value_json.system_mode }}"));
178         assertThat(config.mode_state_topic, is("zigbee2mqtt/th1"));
179         assertThat(config.modes, is(List.of("heat", "auto", "off")));
180         assertThat(config.getName(), is("th1"));
181         assertThat(config.temp_step, is(0.5f));
182         assertThat(config.temperature_command_topic, is("zigbee2mqtt/th1/set/current_heating_setpoint"));
183         assertThat(config.temperature_state_template, is("{{ value_json.current_heating_setpoint }}"));
184         assertThat(config.temperature_state_topic, is("zigbee2mqtt/th1"));
185         assertThat(config.temperature_unit, is("C"));
186         assertThat(config.getUniqueId(), is("0x847127fffe11dd6a_climate_zigbee2mqtt"));
187
188         assertThat(config.initial, is(21));
189         assertThat(config.send_if_off, is(true));
190     }
191
192     @Test
193     public void testClimateConfig() {
194         String json = readTestJson("configClimate.json");
195         Climate.ChannelConfiguration config = AbstractChannelConfiguration.fromString(json, gson,
196                 Climate.ChannelConfiguration.class);
197         assertThat(config.action_template, is("a"));
198         assertThat(config.action_topic, is("b"));
199         assertThat(config.aux_command_topic, is("c"));
200         assertThat(config.aux_state_template, is("d"));
201         assertThat(config.aux_state_topic, is("e"));
202         assertThat(config.away_mode_command_topic, is("f"));
203         assertThat(config.away_mode_state_template, is("g"));
204         assertThat(config.away_mode_state_topic, is("h"));
205         assertThat(config.current_temperature_template, is("i"));
206         assertThat(config.current_temperature_topic, is("j"));
207         assertThat(config.fan_mode_command_template, is("k"));
208         assertThat(config.fan_mode_command_topic, is("l"));
209         assertThat(config.fan_mode_state_template, is("m"));
210         assertThat(config.fan_mode_state_topic, is("n"));
211         assertThat(config.fan_modes, is(List.of("p1", "p2")));
212         assertThat(config.hold_command_template, is("q"));
213         assertThat(config.hold_command_topic, is("r"));
214         assertThat(config.hold_state_template, is("s"));
215         assertThat(config.hold_state_topic, is("t"));
216         assertThat(config.hold_modes, is(List.of("u1", "u2", "u3")));
217         assertThat(config.json_attributes_template, is("v"));
218         assertThat(config.json_attributes_topic, is("w"));
219         assertThat(config.mode_command_template, is("x"));
220         assertThat(config.mode_command_topic, is("y"));
221         assertThat(config.mode_state_template, is("z"));
222         assertThat(config.mode_state_topic, is("A"));
223         assertThat(config.modes, is(List.of("B1", "B2")));
224         assertThat(config.swing_command_template, is("C"));
225         assertThat(config.swing_command_topic, is("D"));
226         assertThat(config.swing_state_template, is("E"));
227         assertThat(config.swing_state_topic, is("F"));
228         assertThat(config.swing_modes, is(List.of("G1")));
229         assertThat(config.temperature_command_template, is("H"));
230         assertThat(config.temperature_command_topic, is("I"));
231         assertThat(config.temperature_state_template, is("J"));
232         assertThat(config.temperature_state_topic, is("K"));
233         assertThat(config.temperature_high_command_template, is("L"));
234         assertThat(config.temperature_high_command_topic, is("N"));
235         assertThat(config.temperature_high_state_template, is("O"));
236         assertThat(config.temperature_high_state_topic, is("P"));
237         assertThat(config.temperature_low_command_template, is("Q"));
238         assertThat(config.temperature_low_command_topic, is("R"));
239         assertThat(config.temperature_low_state_template, is("S"));
240         assertThat(config.temperature_low_state_topic, is("T"));
241         assertThat(config.power_command_topic, is("U"));
242         assertThat(config.initial, is(10));
243         assertThat(config.max_temp, is(40f));
244         assertThat(config.min_temp, is(0f));
245         assertThat(config.temperature_unit, is("F"));
246         assertThat(config.temp_step, is(1f));
247         assertThat(config.precision, is(0.5f));
248         assertThat(config.send_if_off, is(false));
249     }
250 }