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.NumberValue;
23 import org.openhab.binding.mqtt.generic.values.OnOffValue;
24 import org.openhab.binding.mqtt.generic.values.TextValue;
25 import org.openhab.core.library.types.DecimalType;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.library.types.QuantityType;
28 import org.openhab.core.library.types.StringType;
29 import org.openhab.core.library.unit.ImperialUnits;
30 import org.openhab.core.library.unit.SIUnits;
33 * Tests for {@link Climate}
35 * @author Anton Kharuzhy - Initial contribution
38 public class ClimateTests extends AbstractComponentTests {
39 public static final String CONFIG_TOPIC = "climate/0x847127fffe11dd6a_climate_zigbee2mqtt";
41 @SuppressWarnings("null")
43 public void testTS0601Climate() {
44 var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC), "{"
45 + " \"action_template\": \"{% set values = {'idle':'off','heat':'heating','cool':'cooling','fan only':'fan'} %}{{ values[value_json.running_state] }}\","
46 + " \"action_topic\": \"zigbee2mqtt/th1\", \"availability\": [ {"
47 + " \"topic\": \"zigbee2mqtt/bridge/state\" } ],"
48 + " \"away_mode_command_topic\": \"zigbee2mqtt/th1/set/away_mode\","
49 + " \"away_mode_state_template\": \"{{ value_json.away_mode }}\","
50 + " \"away_mode_state_topic\": \"zigbee2mqtt/th1\","
51 + " \"current_temperature_template\": \"{{ value_json.local_temperature }}\","
52 + " \"current_temperature_topic\": \"zigbee2mqtt/th1\", \"device\": {"
53 + " \"identifiers\": [ \"zigbee2mqtt_0x847127fffe11dd6a\" ], \"manufacturer\": \"TuYa\","
54 + " \"model\": \"Radiator valve with thermostat (TS0601_thermostat)\","
55 + " \"name\": \"th1\", \"sw_version\": \"Zigbee2MQTT 1.18.2\" },"
56 + " \"hold_command_topic\": \"zigbee2mqtt/th1/set/preset\", \"hold_modes\": ["
57 + " \"schedule\", \"manual\", \"boost\", \"complex\", \"comfort\", \"eco\" ],"
58 + " \"hold_state_template\": \"{{ value_json.preset }}\","
59 + " \"hold_state_topic\": \"zigbee2mqtt/th1\","
60 + " \"json_attributes_topic\": \"zigbee2mqtt/th1\", \"max_temp\": \"35\","
61 + " \"min_temp\": \"5\", \"mode_command_topic\": \"zigbee2mqtt/th1/set/system_mode\","
62 + " \"mode_state_template\": \"{{ value_json.system_mode }}\","
63 + " \"mode_state_topic\": \"zigbee2mqtt/th1\", \"modes\": [ \"heat\","
64 + " \"auto\", \"off\" ], \"name\": \"th1\", \"temp_step\": 0.5,"
65 + " \"temperature_command_topic\": \"zigbee2mqtt/th1/set/current_heating_setpoint\","
66 + " \"temperature_state_template\": \"{{ value_json.current_heating_setpoint }}\","
67 + " \"temperature_state_topic\": \"zigbee2mqtt/th1\", \"temperature_unit\": \"C\","
68 + " \"unique_id\": \"0x847127fffe11dd6a_climate_zigbee2mqtt\"}");
70 assertThat(component.channels.size(), is(6));
71 assertThat(component.getName(), is("th1"));
73 assertChannel(component, Climate.ACTION_CH_ID, "zigbee2mqtt/th1", "", "th1", TextValue.class);
74 assertChannel(component, Climate.AWAY_MODE_CH_ID, "zigbee2mqtt/th1", "zigbee2mqtt/th1/set/away_mode", "th1",
76 assertChannel(component, Climate.CURRENT_TEMPERATURE_CH_ID, "zigbee2mqtt/th1", "", "th1", NumberValue.class);
77 assertChannel(component, Climate.HOLD_CH_ID, "zigbee2mqtt/th1", "zigbee2mqtt/th1/set/preset", "th1",
79 assertChannel(component, Climate.MODE_CH_ID, "zigbee2mqtt/th1", "zigbee2mqtt/th1/set/system_mode", "th1",
81 assertChannel(component, Climate.TEMPERATURE_CH_ID, "zigbee2mqtt/th1",
82 "zigbee2mqtt/th1/set/current_heating_setpoint", "th1", NumberValue.class);
84 publishMessage("zigbee2mqtt/th1",
85 "{\"running_state\": \"idle\", \"away_mode\": \"ON\", "
86 + "\"local_temperature\": \"22.2\", \"preset\": \"schedule\", \"system_mode\": \"heat\", "
87 + "\"current_heating_setpoint\": \"24\"}");
88 assertState(component, Climate.ACTION_CH_ID, new StringType("off"));
89 assertState(component, Climate.AWAY_MODE_CH_ID, OnOffType.ON);
90 assertState(component, Climate.CURRENT_TEMPERATURE_CH_ID, new QuantityType<>(22.2, SIUnits.CELSIUS));
91 assertState(component, Climate.HOLD_CH_ID, new StringType("schedule"));
92 assertState(component, Climate.MODE_CH_ID, new StringType("heat"));
93 assertState(component, Climate.TEMPERATURE_CH_ID, new QuantityType<>(24, SIUnits.CELSIUS));
95 component.getChannel(Climate.AWAY_MODE_CH_ID).getState().publishValue(OnOffType.OFF);
96 assertPublished("zigbee2mqtt/th1/set/away_mode", "OFF");
97 component.getChannel(Climate.HOLD_CH_ID).getState().publishValue(new StringType("eco"));
98 assertPublished("zigbee2mqtt/th1/set/preset", "eco");
99 component.getChannel(Climate.MODE_CH_ID).getState().publishValue(new StringType("auto"));
100 assertPublished("zigbee2mqtt/th1/set/system_mode", "auto");
101 component.getChannel(Climate.TEMPERATURE_CH_ID).getState().publishValue(new DecimalType(25));
102 assertPublished("zigbee2mqtt/th1/set/current_heating_setpoint", "25");
105 @SuppressWarnings("null")
107 public void testTS0601ClimateNotSendIfOff() {
108 var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC), "{"
109 + " \"action_template\": \"{% set values = {'idle':'off','heat':'heating','cool':'cooling','fan only':'fan'} %}{{ values[value_json.running_state] }}\","
110 + " \"action_topic\": \"zigbee2mqtt/th1\", \"availability\": [ {"
111 + " \"topic\": \"zigbee2mqtt/bridge/state\" } ],"
112 + " \"away_mode_command_topic\": \"zigbee2mqtt/th1/set/away_mode\","
113 + " \"away_mode_state_template\": \"{{ value_json.away_mode }}\","
114 + " \"away_mode_state_topic\": \"zigbee2mqtt/th1\","
115 + " \"current_temperature_template\": \"{{ value_json.local_temperature }}\","
116 + " \"current_temperature_topic\": \"zigbee2mqtt/th1\", \"device\": {"
117 + " \"identifiers\": [ \"zigbee2mqtt_0x847127fffe11dd6a\" ], \"manufacturer\": \"TuYa\","
118 + " \"model\": \"Radiator valve with thermostat (TS0601_thermostat)\","
119 + " \"name\": \"th1\", \"sw_version\": \"Zigbee2MQTT 1.18.2\" },"
120 + " \"hold_command_topic\": \"zigbee2mqtt/th1/set/preset\", \"hold_modes\": ["
121 + " \"schedule\", \"manual\", \"boost\", \"complex\", \"comfort\", \"eco\" ],"
122 + " \"hold_state_template\": \"{{ value_json.preset }}\","
123 + " \"hold_state_topic\": \"zigbee2mqtt/th1\","
124 + " \"json_attributes_topic\": \"zigbee2mqtt/th1\", \"max_temp\": \"35\","
125 + " \"min_temp\": \"5\", \"mode_command_topic\": \"zigbee2mqtt/th1/set/system_mode\","
126 + " \"mode_state_template\": \"{{ value_json.system_mode }}\","
127 + " \"mode_state_topic\": \"zigbee2mqtt/th1\", \"modes\": [ \"heat\","
128 + " \"auto\", \"off\" ], \"name\": \"th1\", \"temp_step\": 0.5,"
129 + " \"temperature_command_topic\": \"zigbee2mqtt/th1/set/current_heating_setpoint\","
130 + " \"temperature_state_template\": \"{{ value_json.current_heating_setpoint }}\","
131 + " \"temperature_state_topic\": \"zigbee2mqtt/th1\", \"temperature_unit\": \"C\","
132 + " \"power_command_topic\": \"zigbee2mqtt/th1/power\","
133 + " \"unique_id\": \"0x847127fffe11dd6a_climate_zigbee2mqtt\", \"send_if_off\": \"false\"}");
135 assertThat(component.channels.size(), is(7));
136 assertThat(component.getName(), is("th1"));
138 assertChannel(component, Climate.ACTION_CH_ID, "zigbee2mqtt/th1", "", "th1", TextValue.class);
139 assertChannel(component, Climate.AWAY_MODE_CH_ID, "zigbee2mqtt/th1", "zigbee2mqtt/th1/set/away_mode", "th1",
141 assertChannel(component, Climate.CURRENT_TEMPERATURE_CH_ID, "zigbee2mqtt/th1", "", "th1", NumberValue.class);
142 assertChannel(component, Climate.HOLD_CH_ID, "zigbee2mqtt/th1", "zigbee2mqtt/th1/set/preset", "th1",
144 assertChannel(component, Climate.MODE_CH_ID, "zigbee2mqtt/th1", "zigbee2mqtt/th1/set/system_mode", "th1",
146 assertChannel(component, Climate.TEMPERATURE_CH_ID, "zigbee2mqtt/th1",
147 "zigbee2mqtt/th1/set/current_heating_setpoint", "th1", NumberValue.class);
149 publishMessage("zigbee2mqtt/th1",
150 "{\"running_state\": \"idle\", \"away_mode\": \"ON\", "
151 + "\"local_temperature\": \"22.2\", \"preset\": \"schedule\", \"system_mode\": \"heat\", "
152 + "\"current_heating_setpoint\": \"24\"}");
153 assertState(component, Climate.ACTION_CH_ID, new StringType("off"));
154 assertState(component, Climate.AWAY_MODE_CH_ID, OnOffType.ON);
155 assertState(component, Climate.CURRENT_TEMPERATURE_CH_ID, new QuantityType<>(22.2, SIUnits.CELSIUS));
156 assertState(component, Climate.HOLD_CH_ID, new StringType("schedule"));
157 assertState(component, Climate.MODE_CH_ID, new StringType("heat"));
158 assertState(component, Climate.TEMPERATURE_CH_ID, new QuantityType<>(24, SIUnits.CELSIUS));
160 // Climate is in OFF state
161 component.getChannel(Climate.AWAY_MODE_CH_ID).getState().publishValue(OnOffType.OFF);
162 assertNotPublished("zigbee2mqtt/th1/set/away_mode", "OFF");
163 component.getChannel(Climate.HOLD_CH_ID).getState().publishValue(new StringType("eco"));
164 assertNotPublished("zigbee2mqtt/th1/set/preset", "eco");
165 component.getChannel(Climate.MODE_CH_ID).getState().publishValue(new StringType("auto"));
166 assertNotPublished("zigbee2mqtt/th1/set/system_mode", "auto");
167 component.getChannel(Climate.TEMPERATURE_CH_ID).getState().publishValue(new DecimalType(25));
168 assertNotPublished("zigbee2mqtt/th1/set/current_heating_setpoint", "25");
169 component.getChannel(Climate.POWER_CH_ID).getState().publishValue(OnOffType.ON);
170 assertPublished("zigbee2mqtt/th1/power", "ON");
173 publishMessage("zigbee2mqtt/th1",
174 "{\"running_state\": \"heat\", \"away_mode\": \"ON\", "
175 + "\"local_temperature\": \"22.2\", \"preset\": \"schedule\", \"system_mode\": \"heat\", "
176 + "\"current_heating_setpoint\": \"24\"}");
178 // Climate is in ON state
179 component.getChannel(Climate.AWAY_MODE_CH_ID).getState().publishValue(OnOffType.OFF);
180 assertPublished("zigbee2mqtt/th1/set/away_mode", "OFF");
181 component.getChannel(Climate.HOLD_CH_ID).getState().publishValue(new StringType("eco"));
182 assertPublished("zigbee2mqtt/th1/set/preset", "eco");
183 component.getChannel(Climate.MODE_CH_ID).getState().publishValue(new StringType("auto"));
184 assertPublished("zigbee2mqtt/th1/set/system_mode", "auto");
185 component.getChannel(Climate.TEMPERATURE_CH_ID).getState().publishValue(new DecimalType(25));
186 assertPublished("zigbee2mqtt/th1/set/current_heating_setpoint", "25");
189 @SuppressWarnings("null")
191 public void testClimate() {
192 var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
193 "{\"action_template\": \"{{ value_json.action }}\", \"action_topic\": \"zigbee2mqtt/th1\","
194 + " \"aux_command_topic\": \"zigbee2mqtt/th1/aux\","
195 + " \"aux_state_template\": \"{{ value_json.aux }}\", \"aux_state_topic\": \"zigbee2mqtt/th1\","
196 + " \"away_mode_command_topic\": \"zigbee2mqtt/th1/away_mode\","
197 + " \"away_mode_state_template\": \"{{ value_json.away_mode }}\","
198 + " \"away_mode_state_topic\": \"zigbee2mqtt/th1\","
199 + " \"current_temperature_template\": \"{{ value_json.current_temperature }}\","
200 + " \"current_temperature_topic\": \"zigbee2mqtt/th1\","
201 + " \"fan_mode_command_template\": \"fan_mode={{ value }}\","
202 + " \"fan_mode_command_topic\": \"zigbee2mqtt/th1/fan_mode\","
203 + " \"fan_mode_state_template\": \"{{ value_json.fan_mode }}\","
204 + " \"fan_mode_state_topic\": \"zigbee2mqtt/th1\", \"fan_modes\": [ \"p1\","
205 + " \"p2\" ], \"hold_command_template\": \"hold={{ value }}\","
206 + " \"hold_command_topic\": \"zigbee2mqtt/th1/hold\","
207 + " \"hold_state_template\": \"{{ value_json.hold }}\","
208 + " \"hold_state_topic\": \"zigbee2mqtt/th1\", \"hold_modes\": [ \"u1\", \"u2\","
209 + " \"u3\" ], \"json_attributes_template\": \"{{ value_json.attrs }}\","
210 + " \"json_attributes_topic\": \"zigbee2mqtt/th1\","
211 + " \"mode_command_template\": \"mode={{ value }}\","
212 + " \"mode_command_topic\": \"zigbee2mqtt/th1/mode\","
213 + " \"mode_state_template\": \"{{ value_json.mode }}\","
214 + " \"mode_state_topic\": \"zigbee2mqtt/th1\", \"modes\": [ \"B1\", \"B2\""
215 + " ], \"swing_command_template\": \"swing={{ value }}\","
216 + " \"swing_command_topic\": \"zigbee2mqtt/th1/swing\","
217 + " \"swing_state_template\": \"{{ value_json.swing }}\","
218 + " \"swing_state_topic\": \"zigbee2mqtt/th1\", \"swing_modes\": [ \"G1\","
219 + " \"G2\" ], \"temperature_command_template\": \"temperature={{ value }}\","
220 + " \"temperature_command_topic\": \"zigbee2mqtt/th1/temperature\","
221 + " \"temperature_state_template\": \"{{ value_json.temperature }}\","
222 + " \"temperature_state_topic\": \"zigbee2mqtt/th1\","
223 + " \"temperature_high_command_template\": \"temperature_high={{ value }}\","
224 + " \"temperature_high_command_topic\": \"zigbee2mqtt/th1/temperature_high\","
225 + " \"temperature_high_state_template\": \"{{ value_json.temperature_high }}\","
226 + " \"temperature_high_state_topic\": \"zigbee2mqtt/th1\","
227 + " \"temperature_low_command_template\": \"temperature_low={{ value }}\","
228 + " \"temperature_low_command_topic\": \"zigbee2mqtt/th1/temperature_low\","
229 + " \"temperature_low_state_template\": \"{{ value_json.temperature_low }}\","
230 + " \"temperature_low_state_topic\": \"zigbee2mqtt/th1\","
231 + " \"power_command_topic\": \"zigbee2mqtt/th1/power\", \"initial\": \"10\","
232 + " \"max_temp\": \"40\", \"min_temp\": \"0\", \"temperature_unit\": \"F\","
233 + " \"temp_step\": \"1\", \"precision\": \"0.5\", \"send_if_off\": \"false\" }");
235 assertThat(component.channels.size(), is(12));
236 assertThat(component.getName(), is("MQTT HVAC"));
238 assertChannel(component, Climate.ACTION_CH_ID, "zigbee2mqtt/th1", "", "MQTT HVAC", TextValue.class);
239 assertChannel(component, Climate.AUX_CH_ID, "zigbee2mqtt/th1", "zigbee2mqtt/th1/aux", "MQTT HVAC",
241 assertChannel(component, Climate.AWAY_MODE_CH_ID, "zigbee2mqtt/th1", "zigbee2mqtt/th1/away_mode", "MQTT HVAC",
243 assertChannel(component, Climate.CURRENT_TEMPERATURE_CH_ID, "zigbee2mqtt/th1", "", "MQTT HVAC",
245 assertChannel(component, Climate.FAN_MODE_CH_ID, "zigbee2mqtt/th1", "zigbee2mqtt/th1/fan_mode", "MQTT HVAC",
247 assertChannel(component, Climate.HOLD_CH_ID, "zigbee2mqtt/th1", "zigbee2mqtt/th1/hold", "MQTT HVAC",
249 assertChannel(component, Climate.MODE_CH_ID, "zigbee2mqtt/th1", "zigbee2mqtt/th1/mode", "MQTT HVAC",
251 assertChannel(component, Climate.SWING_CH_ID, "zigbee2mqtt/th1", "zigbee2mqtt/th1/swing", "MQTT HVAC",
253 assertChannel(component, Climate.TEMPERATURE_CH_ID, "zigbee2mqtt/th1", "zigbee2mqtt/th1/temperature",
254 "MQTT HVAC", NumberValue.class);
255 assertChannel(component, Climate.TEMPERATURE_HIGH_CH_ID, "zigbee2mqtt/th1", "zigbee2mqtt/th1/temperature_high",
256 "MQTT HVAC", NumberValue.class);
257 assertChannel(component, Climate.TEMPERATURE_LOW_CH_ID, "zigbee2mqtt/th1", "zigbee2mqtt/th1/temperature_low",
258 "MQTT HVAC", NumberValue.class);
259 assertChannel(component, Climate.POWER_CH_ID, "", "zigbee2mqtt/th1/power", "MQTT HVAC", OnOffValue.class);
261 publishMessage("zigbee2mqtt/th1",
262 "{ \"action\": \"fan\", \"aux\": \"ON\", \"away_mode\": \"OFF\", "
263 + "\"current_temperature\": \"35.5\", \"fan_mode\": \"p2\", \"hold\": \"u2\", "
264 + "\"mode\": \"B1\", \"swing\": \"G1\", \"temperature\": \"30\", "
265 + "\"temperature_high\": \"37\", \"temperature_low\": \"20\" }");
267 assertState(component, Climate.ACTION_CH_ID, new StringType("fan"));
268 assertState(component, Climate.AUX_CH_ID, OnOffType.ON);
269 assertState(component, Climate.AWAY_MODE_CH_ID, OnOffType.OFF);
270 assertState(component, Climate.CURRENT_TEMPERATURE_CH_ID, new QuantityType<>(35.5, ImperialUnits.FAHRENHEIT));
271 assertState(component, Climate.FAN_MODE_CH_ID, new StringType("p2"));
272 assertState(component, Climate.HOLD_CH_ID, new StringType("u2"));
273 assertState(component, Climate.MODE_CH_ID, new StringType("B1"));
274 assertState(component, Climate.SWING_CH_ID, new StringType("G1"));
275 assertState(component, Climate.TEMPERATURE_CH_ID, new QuantityType<>(30, ImperialUnits.FAHRENHEIT));
276 assertState(component, Climate.TEMPERATURE_HIGH_CH_ID, new QuantityType<>(37, ImperialUnits.FAHRENHEIT));
277 assertState(component, Climate.TEMPERATURE_LOW_CH_ID, new QuantityType<>(20, ImperialUnits.FAHRENHEIT));
279 component.getChannel(Climate.AUX_CH_ID).getState().publishValue(OnOffType.OFF);
280 assertPublished("zigbee2mqtt/th1/aux", "OFF");
281 component.getChannel(Climate.AWAY_MODE_CH_ID).getState().publishValue(OnOffType.ON);
282 assertPublished("zigbee2mqtt/th1/away_mode", "ON");
283 component.getChannel(Climate.FAN_MODE_CH_ID).getState().publishValue(new StringType("p1"));
284 assertPublished("zigbee2mqtt/th1/fan_mode", "fan_mode=p1");
285 component.getChannel(Climate.HOLD_CH_ID).getState().publishValue(new StringType("u3"));
286 assertPublished("zigbee2mqtt/th1/hold", "hold=u3");
287 component.getChannel(Climate.MODE_CH_ID).getState().publishValue(new StringType("B2"));
288 assertPublished("zigbee2mqtt/th1/mode", "mode=B2");
289 component.getChannel(Climate.SWING_CH_ID).getState().publishValue(new StringType("G2"));
290 assertPublished("zigbee2mqtt/th1/swing", "swing=G2");
291 component.getChannel(Climate.TEMPERATURE_CH_ID).getState().publishValue(new DecimalType(30.5));
292 assertPublished("zigbee2mqtt/th1/temperature", "temperature=30.5");
293 component.getChannel(Climate.TEMPERATURE_HIGH_CH_ID).getState().publishValue(new DecimalType(39.5));
294 assertPublished("zigbee2mqtt/th1/temperature_high", "temperature_high=39.5");
295 component.getChannel(Climate.TEMPERATURE_LOW_CH_ID).getState().publishValue(new DecimalType(19.5));
296 assertPublished("zigbee2mqtt/th1/temperature_low", "temperature_low=19.5");
297 component.getChannel(Climate.POWER_CH_ID).getState().publishValue(OnOffType.OFF);
298 assertPublished("zigbee2mqtt/th1/power", "OFF");
302 protected Set<String> getConfigTopics() {
303 return Set.of(CONFIG_TOPIC);