2 * Copyright (c) 2010-2021 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.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
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;
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;
32 import com.google.gson.Gson;
33 import com.google.gson.GsonBuilder;
36 * @author Jochen Klein - Initial contribution
38 public class HAConfigurationTests {
40 private Gson gson = new GsonBuilder().registerTypeAdapterFactory(new ChannelConfigurationTypeAdapterFactory())
43 private static String readTestJson(final String name) {
44 StringBuilder result = new StringBuilder();
46 try (BufferedReader in = new BufferedReader(
47 new InputStreamReader(HAConfigurationTests.class.getResourceAsStream(name), "UTF-8"))) {
50 while ((line = in.readLine()) != null) {
51 result.append(line).append('\n');
53 return result.toString();
54 } catch (IOException e) {
55 throw new RuntimeException(e);
60 public void testAbbreviations() {
61 String json = readTestJson("configA.json");
63 AbstractChannelConfiguration config = AbstractChannelConfiguration.fromString(json, gson);
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"));
75 assertThat(config.getDevice(), is(notNullValue()));
77 Device device = config.getDevice();
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"));
86 assertThat(device.getName(), is("J"));
87 assertThat(device.getModel(), is("K"));
88 assertThat(device.getSwVersion(), is("L"));
89 assertThat(device.getManufacturer(), is("M"));
94 public void testTildeSubstritution() {
95 String json = readTestJson("configB.json");
97 Switch.ChannelConfiguration config = AbstractChannelConfiguration.fromString(json, gson,
98 Switch.ChannelConfiguration.class);
100 assertThat(config.getAvailabilityTopic(), is("D/E"));
101 assertThat(config.stateTopic, is("O/D/"));
102 assertThat(config.commandTopic, is("P~Q"));
103 assertThat(config.getDevice(), is(notNullValue()));
105 Device device = config.getDevice();
106 if (device != null) {
107 assertThat(device.getIdentifiers(), contains("H"));
112 public void testSampleFanConfig() {
113 String json = readTestJson("configFan.json");
115 Fan.ChannelConfiguration config = AbstractChannelConfiguration.fromString(json, gson,
116 Fan.ChannelConfiguration.class);
117 assertThat(config.getName(), is("Bedroom Fan"));
121 public void testDeviceListConfig() {
122 String json = readTestJson("configDeviceList.json");
124 Fan.ChannelConfiguration config = AbstractChannelConfiguration.fromString(json, gson,
125 Fan.ChannelConfiguration.class);
126 assertThat(config.getDevice(), is(notNullValue()));
128 Device device = config.getDevice();
129 if (device != null) {
130 assertThat(device.getIdentifiers(), is(Arrays.asList("A", "B", "C")));
135 public void testDeviceSingleStringConfig() {
136 String json = readTestJson("configDeviceSingleString.json");
138 Fan.ChannelConfiguration config = AbstractChannelConfiguration.fromString(json, gson,
139 Fan.ChannelConfiguration.class);
140 assertThat(config.getDevice(), is(notNullValue()));
142 Device device = config.getDevice();
143 if (device != null) {
144 assertThat(device.getIdentifiers(), is(Arrays.asList("A")));
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"));
161 assertThat(config.actionTemplate, is(
162 "{% set values = {'idle':'off','heat':'heating','cool':'cooling','fan only':'fan'} %}{{ values[value_json.running_state] }}"));
163 assertThat(config.actionTopic, is("zigbee2mqtt/th1"));
164 assertThat(config.awayModeCommandTopic, is("zigbee2mqtt/th1/set/away_mode"));
165 assertThat(config.awayModeStateTemplate, is("{{ value_json.away_mode }}"));
166 assertThat(config.awayModeStateTopic, is("zigbee2mqtt/th1"));
167 assertThat(config.currentTemperatureTemplate, is("{{ value_json.local_temperature }}"));
168 assertThat(config.currentTemperatureTopic, is("zigbee2mqtt/th1"));
169 assertThat(config.holdCommandTopic, is("zigbee2mqtt/th1/set/preset"));
170 assertThat(config.holdModes, is(List.of("schedule", "manual", "boost", "complex", "comfort", "eco")));
171 assertThat(config.holdStateTemplate, is("{{ value_json.preset }}"));
172 assertThat(config.holdStateTopic, is("zigbee2mqtt/th1"));
173 assertThat(config.jsonAttributesTopic, is("zigbee2mqtt/th1"));
174 assertThat(config.maxTemp, is(35f));
175 assertThat(config.minTemp, is(5f));
176 assertThat(config.modeCommandTopic, is("zigbee2mqtt/th1/set/system_mode"));
177 assertThat(config.modeStateTemplate, is("{{ value_json.system_mode }}"));
178 assertThat(config.modeStateTopic, is("zigbee2mqtt/th1"));
179 assertThat(config.modes, is(List.of("heat", "auto", "off")));
180 assertThat(config.getName(), is("th1"));
181 assertThat(config.tempStep, is(0.5f));
182 assertThat(config.temperatureCommandTopic, is("zigbee2mqtt/th1/set/current_heating_setpoint"));
183 assertThat(config.temperatureStateTemplate, is("{{ value_json.current_heating_setpoint }}"));
184 assertThat(config.temperatureStateTopic, is("zigbee2mqtt/th1"));
185 assertThat(config.temperatureUnit, is("C"));
186 assertThat(config.getUniqueId(), is("0x847127fffe11dd6a_climate_zigbee2mqtt"));
188 assertThat(config.initial, is(21));
189 assertThat(config.sendIfOff, is(true));
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.actionTemplate, is("a"));
198 assertThat(config.actionTopic, is("b"));
199 assertThat(config.auxCommandTopic, is("c"));
200 assertThat(config.auxStateTemplate, is("d"));
201 assertThat(config.auxStateTopic, is("e"));
202 assertThat(config.awayModeCommandTopic, is("f"));
203 assertThat(config.awayModeStateTemplate, is("g"));
204 assertThat(config.awayModeStateTopic, is("h"));
205 assertThat(config.currentTemperatureTemplate, is("i"));
206 assertThat(config.currentTemperatureTopic, is("j"));
207 assertThat(config.fanModeCommandTemplate, is("k"));
208 assertThat(config.fanModeCommandTopic, is("l"));
209 assertThat(config.fanModeStateTemplate, is("m"));
210 assertThat(config.fanModeStateTopic, is("n"));
211 assertThat(config.fanModes, is(List.of("p1", "p2")));
212 assertThat(config.holdCommandTemplate, is("q"));
213 assertThat(config.holdCommandTopic, is("r"));
214 assertThat(config.holdStateTemplate, is("s"));
215 assertThat(config.holdStateTopic, is("t"));
216 assertThat(config.holdModes, is(List.of("u1", "u2", "u3")));
217 assertThat(config.jsonAttributesTemplate, is("v"));
218 assertThat(config.jsonAttributesTopic, is("w"));
219 assertThat(config.modeCommandTemplate, is("x"));
220 assertThat(config.modeCommandTopic, is("y"));
221 assertThat(config.modeStateTemplate, is("z"));
222 assertThat(config.modeStateTopic, is("A"));
223 assertThat(config.modes, is(List.of("B1", "B2")));
224 assertThat(config.swingCommandTemplate, is("C"));
225 assertThat(config.swingCommandTopic, is("D"));
226 assertThat(config.swingStateTemplate, is("E"));
227 assertThat(config.swingStateTopic, is("F"));
228 assertThat(config.swingModes, is(List.of("G1")));
229 assertThat(config.temperatureCommandTemplate, is("H"));
230 assertThat(config.temperatureCommandTopic, is("I"));
231 assertThat(config.temperatureStateTemplate, is("J"));
232 assertThat(config.temperatureStateTopic, is("K"));
233 assertThat(config.temperatureHighCommandTemplate, is("L"));
234 assertThat(config.temperatureHighCommandTopic, is("N"));
235 assertThat(config.temperatureHighStateTemplate, is("O"));
236 assertThat(config.temperatureHighStateTopic, is("P"));
237 assertThat(config.temperatureLowCommandTemplate, is("Q"));
238 assertThat(config.temperatureLowCommandTopic, is("R"));
239 assertThat(config.temperatureLowStateTemplate, is("S"));
240 assertThat(config.temperatureLowStateTopic, is("T"));
241 assertThat(config.powerCommandTopic, is("U"));
242 assertThat(config.initial, is(10));
243 assertThat(config.maxTemp, is(40f));
244 assertThat(config.minTemp, is(0f));
245 assertThat(config.temperatureUnit, is("F"));
246 assertThat(config.tempStep, is(1f));
247 assertThat(config.precision, is(0.5f));
248 assertThat(config.sendIfOff, is(false));