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.*;
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.io.UncheckedIOException;
23 import java.math.BigDecimal;
24 import java.util.Arrays;
25 import java.util.List;
27 import org.eclipse.jdt.annotation.NonNullByDefault;
28 import org.junit.jupiter.api.Test;
29 import org.openhab.binding.mqtt.homeassistant.internal.config.ChannelConfigurationTypeAdapterFactory;
30 import org.openhab.binding.mqtt.homeassistant.internal.config.dto.AbstractChannelConfiguration;
31 import org.openhab.binding.mqtt.homeassistant.internal.config.dto.Connection;
32 import org.openhab.binding.mqtt.homeassistant.internal.config.dto.Device;
34 import com.google.gson.Gson;
35 import com.google.gson.GsonBuilder;
38 * @author Jochen Klein - Initial contribution
41 public class HAConfigurationTests {
43 private Gson gson = new GsonBuilder().registerTypeAdapterFactory(new ChannelConfigurationTypeAdapterFactory())
46 private static String readTestJson(final String name) {
47 StringBuilder result = new StringBuilder();
49 try (BufferedReader in = new BufferedReader(
50 new InputStreamReader(HAConfigurationTests.class.getResourceAsStream(name), "UTF-8"))) {
53 while ((line = in.readLine()) != null) {
54 result.append(line).append('\n');
56 return result.toString();
57 } catch (IOException e) {
58 throw new UncheckedIOException(e);
63 public void testAbbreviations() {
64 String json = readTestJson("configA.json");
66 AbstractChannelConfiguration config = AbstractChannelConfiguration.fromString(json, gson);
68 assertThat(config.getName(), is("A"));
69 assertThat(config.getIcon(), is("2"));
70 assertThat(config.getQos(), is(1));
71 assertThat(config.isRetain(), is(true));
72 assertThat(config.getValueTemplate(), is("B"));
73 assertThat(config.getUniqueId(), is("C"));
74 assertThat(config.getAvailabilityTopic(), is("D/E"));
75 assertThat(config.getPayloadAvailable(), is("F"));
76 assertThat(config.getPayloadNotAvailable(), is("G"));
78 assertThat(config.getDevice(), is(notNullValue()));
80 Device device = config.getDevice();
82 assertThat(device.getIdentifiers(), contains("H"));
83 assertThat(device.getConnections(), is(notNullValue()));
84 List<Connection> connections = device.getConnections();
85 if (connections != null) {
86 assertThat(connections.get(0).getType(), is("I1"));
87 assertThat(connections.get(0).getIdentifier(), is("I2"));
89 assertThat(device.getName(), is("J"));
90 assertThat(device.getModel(), is("K"));
91 assertThat(device.getSwVersion(), is("L"));
92 assertThat(device.getManufacturer(), is("M"));
97 public void testTildeSubstritution() {
98 String json = readTestJson("configB.json");
100 Switch.ChannelConfiguration config = AbstractChannelConfiguration.fromString(json, gson,
101 Switch.ChannelConfiguration.class);
103 assertThat(config.getAvailabilityTopic(), is("D/E"));
104 assertThat(config.stateTopic, is("O/D/"));
105 assertThat(config.commandTopic, is("P~Q"));
106 assertThat(config.getDevice(), is(notNullValue()));
108 Device device = config.getDevice();
109 if (device != null) {
110 assertThat(device.getIdentifiers(), contains("H"));
115 public void testSampleFanConfig() {
116 String json = readTestJson("configFan.json");
118 Fan.ChannelConfiguration config = AbstractChannelConfiguration.fromString(json, gson,
119 Fan.ChannelConfiguration.class);
120 assertThat(config.getName(), is("Bedroom Fan"));
124 public void testDeviceListConfig() {
125 String json = readTestJson("configDeviceList.json");
127 Fan.ChannelConfiguration config = AbstractChannelConfiguration.fromString(json, gson,
128 Fan.ChannelConfiguration.class);
129 assertThat(config.getDevice(), is(notNullValue()));
131 Device device = config.getDevice();
132 if (device != null) {
133 assertThat(device.getIdentifiers(), is(Arrays.asList("A", "B", "C")));
138 public void testDeviceSingleStringConfig() {
139 String json = readTestJson("configDeviceSingleString.json");
141 Fan.ChannelConfiguration config = AbstractChannelConfiguration.fromString(json, gson,
142 Fan.ChannelConfiguration.class);
143 assertThat(config.getDevice(), is(notNullValue()));
145 Device device = config.getDevice();
146 if (device != null) {
147 assertThat(device.getIdentifiers(), is(Arrays.asList("A")));
151 @SuppressWarnings("null")
153 public void testTS0601ClimateConfig() {
154 String json = readTestJson("configTS0601ClimateThermostat.json");
155 Climate.ChannelConfiguration config = AbstractChannelConfiguration.fromString(json, gson,
156 Climate.ChannelConfiguration.class);
157 assertThat(config.getDevice(), is(notNullValue()));
158 assertThat(config.getDevice().getIdentifiers(), is(notNullValue()));
159 assertThat(config.getDevice().getIdentifiers().get(0), is("zigbee2mqtt_0x847127fffe11dd6a"));
160 assertThat(config.getDevice().getManufacturer(), is("TuYa"));
161 assertThat(config.getDevice().getModel(), is("Radiator valve with thermostat (TS0601_thermostat)"));
162 assertThat(config.getDevice().getName(), is("th1"));
163 assertThat(config.getDevice().getSwVersion(), is("Zigbee2MQTT 1.18.2"));
165 assertThat(config.actionTemplate, is(
166 "{% set values = {'idle':'off','heat':'heating','cool':'cooling','fan only':'fan'} %}{{ values[value_json.running_state] }}"));
167 assertThat(config.actionTopic, is("zigbee2mqtt/th1"));
168 assertThat(config.awayModeCommandTopic, is("zigbee2mqtt/th1/set/away_mode"));
169 assertThat(config.awayModeStateTemplate, is("{{ value_json.away_mode }}"));
170 assertThat(config.awayModeStateTopic, is("zigbee2mqtt/th1"));
171 assertThat(config.currentTemperatureTemplate, is("{{ value_json.local_temperature }}"));
172 assertThat(config.currentTemperatureTopic, is("zigbee2mqtt/th1"));
173 assertThat(config.holdCommandTopic, is("zigbee2mqtt/th1/set/preset"));
174 assertThat(config.holdModes, is(List.of("schedule", "manual", "boost", "complex", "comfort", "eco")));
175 assertThat(config.holdStateTemplate, is("{{ value_json.preset }}"));
176 assertThat(config.holdStateTopic, is("zigbee2mqtt/th1"));
177 assertThat(config.jsonAttributesTopic, is("zigbee2mqtt/th1"));
178 assertThat(config.maxTemp, is(new BigDecimal(35)));
179 assertThat(config.minTemp, is(new BigDecimal(5)));
180 assertThat(config.modeCommandTopic, is("zigbee2mqtt/th1/set/system_mode"));
181 assertThat(config.modeStateTemplate, is("{{ value_json.system_mode }}"));
182 assertThat(config.modeStateTopic, is("zigbee2mqtt/th1"));
183 assertThat(config.modes, is(List.of("heat", "auto", "off")));
184 assertThat(config.getName(), is("th1"));
185 assertThat(config.tempStep, is(new BigDecimal("0.5")));
186 assertThat(config.temperatureCommandTopic, is("zigbee2mqtt/th1/set/current_heating_setpoint"));
187 assertThat(config.temperatureStateTemplate, is("{{ value_json.current_heating_setpoint }}"));
188 assertThat(config.temperatureStateTopic, is("zigbee2mqtt/th1"));
189 assertThat(config.temperatureUnit, is(Climate.TemperatureUnit.CELSIUS));
190 assertThat(config.getUniqueId(), is("0x847127fffe11dd6a_climate_zigbee2mqtt"));
192 assertThat(config.initial, is(21));
193 assertThat(config.sendIfOff, is(true));
197 public void testClimateConfig() {
198 String json = readTestJson("configClimate.json");
199 Climate.ChannelConfiguration config = AbstractChannelConfiguration.fromString(json, gson,
200 Climate.ChannelConfiguration.class);
201 assertThat(config.actionTemplate, is("a"));
202 assertThat(config.actionTopic, is("b"));
203 assertThat(config.auxCommandTopic, is("c"));
204 assertThat(config.auxStateTemplate, is("d"));
205 assertThat(config.auxStateTopic, is("e"));
206 assertThat(config.awayModeCommandTopic, is("f"));
207 assertThat(config.awayModeStateTemplate, is("g"));
208 assertThat(config.awayModeStateTopic, is("h"));
209 assertThat(config.currentTemperatureTemplate, is("i"));
210 assertThat(config.currentTemperatureTopic, is("j"));
211 assertThat(config.fanModeCommandTemplate, is("k"));
212 assertThat(config.fanModeCommandTopic, is("l"));
213 assertThat(config.fanModeStateTemplate, is("m"));
214 assertThat(config.fanModeStateTopic, is("n"));
215 assertThat(config.fanModes, is(List.of("p1", "p2")));
216 assertThat(config.holdCommandTemplate, is("q"));
217 assertThat(config.holdCommandTopic, is("r"));
218 assertThat(config.holdStateTemplate, is("s"));
219 assertThat(config.holdStateTopic, is("t"));
220 assertThat(config.holdModes, is(List.of("u1", "u2", "u3")));
221 assertThat(config.jsonAttributesTemplate, is("v"));
222 assertThat(config.jsonAttributesTopic, is("w"));
223 assertThat(config.modeCommandTemplate, is("x"));
224 assertThat(config.modeCommandTopic, is("y"));
225 assertThat(config.modeStateTemplate, is("z"));
226 assertThat(config.modeStateTopic, is("A"));
227 assertThat(config.modes, is(List.of("B1", "B2")));
228 assertThat(config.swingCommandTemplate, is("C"));
229 assertThat(config.swingCommandTopic, is("D"));
230 assertThat(config.swingStateTemplate, is("E"));
231 assertThat(config.swingStateTopic, is("F"));
232 assertThat(config.swingModes, is(List.of("G1")));
233 assertThat(config.temperatureCommandTemplate, is("H"));
234 assertThat(config.temperatureCommandTopic, is("I"));
235 assertThat(config.temperatureStateTemplate, is("J"));
236 assertThat(config.temperatureStateTopic, is("K"));
237 assertThat(config.temperatureHighCommandTemplate, is("L"));
238 assertThat(config.temperatureHighCommandTopic, is("N"));
239 assertThat(config.temperatureHighStateTemplate, is("O"));
240 assertThat(config.temperatureHighStateTopic, is("P"));
241 assertThat(config.temperatureLowCommandTemplate, is("Q"));
242 assertThat(config.temperatureLowCommandTopic, is("R"));
243 assertThat(config.temperatureLowStateTemplate, is("S"));
244 assertThat(config.temperatureLowStateTopic, is("T"));
245 assertThat(config.powerCommandTopic, is("U"));
246 assertThat(config.initial, is(10));
247 assertThat(config.maxTemp, is(new BigDecimal(40)));
248 assertThat(config.minTemp, is(BigDecimal.ZERO));
249 assertThat(config.temperatureUnit, is(Climate.TemperatureUnit.FAHRENHEIT));
250 assertThat(config.tempStep, is(BigDecimal.ONE));
251 assertThat(config.precision, is(new BigDecimal("0.5")));
252 assertThat(config.sendIfOff, is(false));