]> git.basschouten.com Git - openhab-addons.git/blob
1f39a197d8bbe611722fb61736d95de8897383f3
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.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;
31
32 /**
33  * Tests for {@link Climate}
34  *
35  * @author Anton Kharuzhy - Initial contribution
36  */
37 @SuppressWarnings("ConstantConditions")
38 @NonNullByDefault
39 public class ClimateTests extends AbstractComponentTests {
40     public static final String CONFIG_TOPIC = "climate/0x847127fffe11dd6a_climate_zigbee2mqtt";
41
42     @Test
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\"}");
69
70         assertThat(component.channels.size(), is(6));
71         assertThat(component.getName(), is("th1"));
72
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",
75                 OnOffValue.class);
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",
78                 TextValue.class);
79         assertChannel(component, Climate.MODE_CH_ID, "zigbee2mqtt/th1", "zigbee2mqtt/th1/set/system_mode", "th1",
80                 TextValue.class);
81         assertChannel(component, Climate.TEMPERATURE_CH_ID, "zigbee2mqtt/th1",
82                 "zigbee2mqtt/th1/set/current_heating_setpoint", "th1", NumberValue.class);
83
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));
94
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");
103     }
104
105     @Test
106     public void testTS0601ClimateNotSendIfOff() {
107         var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC), "{"
108                 + " \"action_template\": \"{% set values = {'idle':'off','heat':'heating','cool':'cooling','fan only':'fan'} %}{{ values[value_json.running_state] }}\","
109                 + " \"action_topic\": \"zigbee2mqtt/th1\", \"availability\": [ {"
110                 + " \"topic\": \"zigbee2mqtt/bridge/state\" } ],"
111                 + " \"away_mode_command_topic\": \"zigbee2mqtt/th1/set/away_mode\","
112                 + " \"away_mode_state_template\": \"{{ value_json.away_mode }}\","
113                 + " \"away_mode_state_topic\": \"zigbee2mqtt/th1\","
114                 + " \"current_temperature_template\": \"{{ value_json.local_temperature }}\","
115                 + " \"current_temperature_topic\": \"zigbee2mqtt/th1\", \"device\": {"
116                 + " \"identifiers\": [ \"zigbee2mqtt_0x847127fffe11dd6a\" ],  \"manufacturer\": \"TuYa\","
117                 + " \"model\": \"Radiator valve with thermostat (TS0601_thermostat)\","
118                 + " \"name\": \"th1\", \"sw_version\": \"Zigbee2MQTT 1.18.2\" },"
119                 + " \"hold_command_topic\": \"zigbee2mqtt/th1/set/preset\", \"hold_modes\": ["
120                 + " \"schedule\", \"manual\", \"boost\", \"complex\",  \"comfort\", \"eco\" ],"
121                 + " \"hold_state_template\": \"{{ value_json.preset }}\","
122                 + " \"hold_state_topic\": \"zigbee2mqtt/th1\","
123                 + " \"json_attributes_topic\": \"zigbee2mqtt/th1\", \"max_temp\": \"35\","
124                 + " \"min_temp\": \"5\", \"mode_command_topic\": \"zigbee2mqtt/th1/set/system_mode\","
125                 + " \"mode_state_template\": \"{{ value_json.system_mode }}\","
126                 + " \"mode_state_topic\": \"zigbee2mqtt/th1\", \"modes\": [ \"heat\","
127                 + " \"auto\", \"off\" ], \"name\": \"th1\", \"temp_step\": 0.5,"
128                 + " \"temperature_command_topic\": \"zigbee2mqtt/th1/set/current_heating_setpoint\","
129                 + " \"temperature_state_template\": \"{{ value_json.current_heating_setpoint }}\","
130                 + " \"temperature_state_topic\": \"zigbee2mqtt/th1\", \"temperature_unit\": \"C\","
131                 + " \"power_command_topic\": \"zigbee2mqtt/th1/power\","
132                 + " \"unique_id\": \"0x847127fffe11dd6a_climate_zigbee2mqtt\", \"send_if_off\": \"false\"}");
133
134         assertThat(component.channels.size(), is(7));
135         assertThat(component.getName(), is("th1"));
136
137         assertChannel(component, Climate.ACTION_CH_ID, "zigbee2mqtt/th1", "", "th1", TextValue.class);
138         assertChannel(component, Climate.AWAY_MODE_CH_ID, "zigbee2mqtt/th1", "zigbee2mqtt/th1/set/away_mode", "th1",
139                 OnOffValue.class);
140         assertChannel(component, Climate.CURRENT_TEMPERATURE_CH_ID, "zigbee2mqtt/th1", "", "th1", NumberValue.class);
141         assertChannel(component, Climate.HOLD_CH_ID, "zigbee2mqtt/th1", "zigbee2mqtt/th1/set/preset", "th1",
142                 TextValue.class);
143         assertChannel(component, Climate.MODE_CH_ID, "zigbee2mqtt/th1", "zigbee2mqtt/th1/set/system_mode", "th1",
144                 TextValue.class);
145         assertChannel(component, Climate.TEMPERATURE_CH_ID, "zigbee2mqtt/th1",
146                 "zigbee2mqtt/th1/set/current_heating_setpoint", "th1", NumberValue.class);
147
148         publishMessage("zigbee2mqtt/th1",
149                 "{\"running_state\": \"idle\", \"away_mode\": \"ON\", "
150                         + "\"local_temperature\": \"22.2\", \"preset\": \"schedule\", \"system_mode\": \"heat\", "
151                         + "\"current_heating_setpoint\": \"24\"}");
152         assertState(component, Climate.ACTION_CH_ID, new StringType("off"));
153         assertState(component, Climate.AWAY_MODE_CH_ID, OnOffType.ON);
154         assertState(component, Climate.CURRENT_TEMPERATURE_CH_ID, new QuantityType<>(22.2, SIUnits.CELSIUS));
155         assertState(component, Climate.HOLD_CH_ID, new StringType("schedule"));
156         assertState(component, Climate.MODE_CH_ID, new StringType("heat"));
157         assertState(component, Climate.TEMPERATURE_CH_ID, new QuantityType<>(24, SIUnits.CELSIUS));
158
159         // Climate is in OFF state
160         component.getChannel(Climate.AWAY_MODE_CH_ID).getState().publishValue(OnOffType.OFF);
161         assertNotPublished("zigbee2mqtt/th1/set/away_mode", "OFF");
162         component.getChannel(Climate.HOLD_CH_ID).getState().publishValue(new StringType("eco"));
163         assertNotPublished("zigbee2mqtt/th1/set/preset", "eco");
164         component.getChannel(Climate.MODE_CH_ID).getState().publishValue(new StringType("auto"));
165         assertNotPublished("zigbee2mqtt/th1/set/system_mode", "auto");
166         component.getChannel(Climate.TEMPERATURE_CH_ID).getState().publishValue(new DecimalType(25));
167         assertNotPublished("zigbee2mqtt/th1/set/current_heating_setpoint", "25");
168         component.getChannel(Climate.POWER_CH_ID).getState().publishValue(OnOffType.ON);
169         assertPublished("zigbee2mqtt/th1/power", "ON");
170
171         // Enabled
172         publishMessage("zigbee2mqtt/th1",
173                 "{\"running_state\": \"heat\", \"away_mode\": \"ON\", "
174                         + "\"local_temperature\": \"22.2\", \"preset\": \"schedule\", \"system_mode\": \"heat\", "
175                         + "\"current_heating_setpoint\": \"24\"}");
176
177         // Climate is in ON state
178         component.getChannel(Climate.AWAY_MODE_CH_ID).getState().publishValue(OnOffType.OFF);
179         assertPublished("zigbee2mqtt/th1/set/away_mode", "OFF");
180         component.getChannel(Climate.HOLD_CH_ID).getState().publishValue(new StringType("eco"));
181         assertPublished("zigbee2mqtt/th1/set/preset", "eco");
182         component.getChannel(Climate.MODE_CH_ID).getState().publishValue(new StringType("auto"));
183         assertPublished("zigbee2mqtt/th1/set/system_mode", "auto");
184         component.getChannel(Climate.TEMPERATURE_CH_ID).getState().publishValue(new DecimalType(25));
185         assertPublished("zigbee2mqtt/th1/set/current_heating_setpoint", "25");
186     }
187
188     @Test
189     public void testClimate() {
190         var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
191                 "{\"action_template\": \"{{ value_json.action }}\", \"action_topic\": \"zigbee2mqtt/th1\","
192                         + " \"aux_command_topic\": \"zigbee2mqtt/th1/aux\","
193                         + " \"aux_state_template\": \"{{ value_json.aux }}\", \"aux_state_topic\": \"zigbee2mqtt/th1\","
194                         + " \"away_mode_command_topic\": \"zigbee2mqtt/th1/away_mode\","
195                         + " \"away_mode_state_template\": \"{{ value_json.away_mode }}\","
196                         + " \"away_mode_state_topic\": \"zigbee2mqtt/th1\","
197                         + " \"current_temperature_template\": \"{{ value_json.current_temperature }}\","
198                         + " \"current_temperature_topic\": \"zigbee2mqtt/th1\","
199                         + " \"fan_mode_command_template\": \"fan_mode={{ value }}\","
200                         + " \"fan_mode_command_topic\": \"zigbee2mqtt/th1/fan_mode\","
201                         + " \"fan_mode_state_template\": \"{{ value_json.fan_mode }}\","
202                         + " \"fan_mode_state_topic\": \"zigbee2mqtt/th1\", \"fan_modes\": [ \"p1\","
203                         + " \"p2\" ], \"hold_command_template\": \"hold={{ value }}\","
204                         + " \"hold_command_topic\": \"zigbee2mqtt/th1/hold\","
205                         + " \"hold_state_template\": \"{{ value_json.hold }}\","
206                         + " \"hold_state_topic\": \"zigbee2mqtt/th1\", \"hold_modes\": [ \"u1\", \"u2\","
207                         + " \"u3\" ], \"json_attributes_template\": \"{{ value_json.attrs }}\","
208                         + " \"json_attributes_topic\": \"zigbee2mqtt/th1\","
209                         + " \"mode_command_template\": \"mode={{ value }}\","
210                         + " \"mode_command_topic\": \"zigbee2mqtt/th1/mode\","
211                         + " \"mode_state_template\": \"{{ value_json.mode }}\","
212                         + " \"mode_state_topic\": \"zigbee2mqtt/th1\", \"modes\": [ \"B1\", \"B2\""
213                         + " ], \"swing_command_template\": \"swing={{ value }}\","
214                         + " \"swing_command_topic\": \"zigbee2mqtt/th1/swing\","
215                         + " \"swing_state_template\": \"{{ value_json.swing }}\","
216                         + " \"swing_state_topic\": \"zigbee2mqtt/th1\", \"swing_modes\": [ \"G1\","
217                         + " \"G2\" ], \"temperature_command_template\": \"temperature={{ value }}\","
218                         + " \"temperature_command_topic\": \"zigbee2mqtt/th1/temperature\","
219                         + " \"temperature_state_template\": \"{{ value_json.temperature }}\","
220                         + " \"temperature_state_topic\": \"zigbee2mqtt/th1\","
221                         + " \"temperature_high_command_template\": \"temperature_high={{ value }}\","
222                         + " \"temperature_high_command_topic\": \"zigbee2mqtt/th1/temperature_high\","
223                         + " \"temperature_high_state_template\": \"{{ value_json.temperature_high }}\","
224                         + " \"temperature_high_state_topic\": \"zigbee2mqtt/th1\","
225                         + " \"temperature_low_command_template\": \"temperature_low={{ value }}\","
226                         + " \"temperature_low_command_topic\": \"zigbee2mqtt/th1/temperature_low\","
227                         + " \"temperature_low_state_template\": \"{{ value_json.temperature_low }}\","
228                         + " \"temperature_low_state_topic\": \"zigbee2mqtt/th1\","
229                         + " \"power_command_topic\": \"zigbee2mqtt/th1/power\", \"initial\": \"10\","
230                         + " \"max_temp\": \"40\", \"min_temp\": \"0\", \"temperature_unit\": \"F\","
231                         + " \"temp_step\": \"1\", \"precision\": \"0.5\", \"send_if_off\": \"false\" }");
232
233         assertThat(component.channels.size(), is(12));
234         assertThat(component.getName(), is("MQTT HVAC"));
235
236         assertChannel(component, Climate.ACTION_CH_ID, "zigbee2mqtt/th1", "", "MQTT HVAC", TextValue.class);
237         assertChannel(component, Climate.AUX_CH_ID, "zigbee2mqtt/th1", "zigbee2mqtt/th1/aux", "MQTT HVAC",
238                 OnOffValue.class);
239         assertChannel(component, Climate.AWAY_MODE_CH_ID, "zigbee2mqtt/th1", "zigbee2mqtt/th1/away_mode", "MQTT HVAC",
240                 OnOffValue.class);
241         assertChannel(component, Climate.CURRENT_TEMPERATURE_CH_ID, "zigbee2mqtt/th1", "", "MQTT HVAC",
242                 NumberValue.class);
243         assertChannel(component, Climate.FAN_MODE_CH_ID, "zigbee2mqtt/th1", "zigbee2mqtt/th1/fan_mode", "MQTT HVAC",
244                 TextValue.class);
245         assertChannel(component, Climate.HOLD_CH_ID, "zigbee2mqtt/th1", "zigbee2mqtt/th1/hold", "MQTT HVAC",
246                 TextValue.class);
247         assertChannel(component, Climate.MODE_CH_ID, "zigbee2mqtt/th1", "zigbee2mqtt/th1/mode", "MQTT HVAC",
248                 TextValue.class);
249         assertChannel(component, Climate.SWING_CH_ID, "zigbee2mqtt/th1", "zigbee2mqtt/th1/swing", "MQTT HVAC",
250                 TextValue.class);
251         assertChannel(component, Climate.TEMPERATURE_CH_ID, "zigbee2mqtt/th1", "zigbee2mqtt/th1/temperature",
252                 "MQTT HVAC", NumberValue.class);
253         assertChannel(component, Climate.TEMPERATURE_HIGH_CH_ID, "zigbee2mqtt/th1", "zigbee2mqtt/th1/temperature_high",
254                 "MQTT HVAC", NumberValue.class);
255         assertChannel(component, Climate.TEMPERATURE_LOW_CH_ID, "zigbee2mqtt/th1", "zigbee2mqtt/th1/temperature_low",
256                 "MQTT HVAC", NumberValue.class);
257         assertChannel(component, Climate.POWER_CH_ID, "", "zigbee2mqtt/th1/power", "MQTT HVAC", OnOffValue.class);
258
259         publishMessage("zigbee2mqtt/th1",
260                 "{ \"action\": \"fan\",  \"aux\": \"ON\",  \"away_mode\": \"OFF\", "
261                         + "\"current_temperature\": \"35.5\",  \"fan_mode\": \"p2\",  \"hold\": \"u2\", "
262                         + "\"mode\": \"B1\",  \"swing\": \"G1\",  \"temperature\": \"30\", "
263                         + "\"temperature_high\": \"37\",  \"temperature_low\": \"20\" }");
264
265         assertState(component, Climate.ACTION_CH_ID, new StringType("fan"));
266         assertState(component, Climate.AUX_CH_ID, OnOffType.ON);
267         assertState(component, Climate.AWAY_MODE_CH_ID, OnOffType.OFF);
268         assertState(component, Climate.CURRENT_TEMPERATURE_CH_ID, new QuantityType<>(35.5, ImperialUnits.FAHRENHEIT));
269         assertState(component, Climate.FAN_MODE_CH_ID, new StringType("p2"));
270         assertState(component, Climate.HOLD_CH_ID, new StringType("u2"));
271         assertState(component, Climate.MODE_CH_ID, new StringType("B1"));
272         assertState(component, Climate.SWING_CH_ID, new StringType("G1"));
273         assertState(component, Climate.TEMPERATURE_CH_ID, new QuantityType<>(30, ImperialUnits.FAHRENHEIT));
274         assertState(component, Climate.TEMPERATURE_HIGH_CH_ID, new QuantityType<>(37, ImperialUnits.FAHRENHEIT));
275         assertState(component, Climate.TEMPERATURE_LOW_CH_ID, new QuantityType<>(20, ImperialUnits.FAHRENHEIT));
276
277         component.getChannel(Climate.AUX_CH_ID).getState().publishValue(OnOffType.OFF);
278         assertPublished("zigbee2mqtt/th1/aux", "OFF");
279         component.getChannel(Climate.AWAY_MODE_CH_ID).getState().publishValue(OnOffType.ON);
280         assertPublished("zigbee2mqtt/th1/away_mode", "ON");
281         component.getChannel(Climate.FAN_MODE_CH_ID).getState().publishValue(new StringType("p1"));
282         assertPublished("zigbee2mqtt/th1/fan_mode", "fan_mode=p1");
283         component.getChannel(Climate.HOLD_CH_ID).getState().publishValue(new StringType("u3"));
284         assertPublished("zigbee2mqtt/th1/hold", "hold=u3");
285         component.getChannel(Climate.MODE_CH_ID).getState().publishValue(new StringType("B2"));
286         assertPublished("zigbee2mqtt/th1/mode", "mode=B2");
287         component.getChannel(Climate.SWING_CH_ID).getState().publishValue(new StringType("G2"));
288         assertPublished("zigbee2mqtt/th1/swing", "swing=G2");
289         component.getChannel(Climate.TEMPERATURE_CH_ID).getState().publishValue(new DecimalType(30.5));
290         assertPublished("zigbee2mqtt/th1/temperature", "temperature=30.5");
291         component.getChannel(Climate.TEMPERATURE_HIGH_CH_ID).getState().publishValue(new DecimalType(39.5));
292         assertPublished("zigbee2mqtt/th1/temperature_high", "temperature_high=39.5");
293         component.getChannel(Climate.TEMPERATURE_LOW_CH_ID).getState().publishValue(new DecimalType(19.5));
294         assertPublished("zigbee2mqtt/th1/temperature_low", "temperature_low=19.5");
295         component.getChannel(Climate.POWER_CH_ID).getState().publishValue(OnOffType.OFF);
296         assertPublished("zigbee2mqtt/th1/power", "OFF");
297     }
298
299     @Override
300     protected Set<String> getConfigTopics() {
301         return Set.of(CONFIG_TOPIC);
302     }
303 }