ESPHome  2024.4.1
tuya_climate.cpp
Go to the documentation of this file.
1 #include "tuya_climate.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace tuya {
6 
7 static const char *const TAG = "tuya.climate";
8 
10  if (this->switch_id_.has_value()) {
11  this->parent_->register_listener(*this->switch_id_, [this](const TuyaDatapoint &datapoint) {
12  ESP_LOGV(TAG, "MCU reported switch is: %s", ONOFF(datapoint.value_bool));
14  if (datapoint.value_bool) {
15  if (this->supports_heat_ && this->supports_cool_) {
17  } else if (this->supports_heat_) {
19  } else if (this->supports_cool_) {
21  }
22  }
23  this->compute_state_();
24  this->publish_state();
25  });
26  }
27  if (this->active_state_id_.has_value()) {
28  this->parent_->register_listener(*this->active_state_id_, [this](const TuyaDatapoint &datapoint) {
29  ESP_LOGV(TAG, "MCU reported active state is: %u", datapoint.value_enum);
30  this->active_state_ = datapoint.value_enum;
31  this->compute_state_();
32  this->publish_state();
33  });
34  } else {
35  if (this->heating_state_pin_ != nullptr) {
36  this->heating_state_pin_->setup();
38  }
39  if (this->cooling_state_pin_ != nullptr) {
40  this->cooling_state_pin_->setup();
42  }
43  }
44  if (this->target_temperature_id_.has_value()) {
45  this->parent_->register_listener(*this->target_temperature_id_, [this](const TuyaDatapoint &datapoint) {
47  if (this->reports_fahrenheit_) {
48  this->manual_temperature_ = (this->manual_temperature_ - 32) * 5 / 9;
49  }
50 
51  ESP_LOGV(TAG, "MCU reported manual target temperature is: %.1f", this->manual_temperature_);
53  this->compute_state_();
54  this->publish_state();
55  });
56  }
57  if (this->current_temperature_id_.has_value()) {
58  this->parent_->register_listener(*this->current_temperature_id_, [this](const TuyaDatapoint &datapoint) {
60  if (this->reports_fahrenheit_) {
61  this->current_temperature = (this->current_temperature - 32) * 5 / 9;
62  }
63 
64  ESP_LOGV(TAG, "MCU reported current temperature is: %.1f", this->current_temperature);
65  this->compute_state_();
66  this->publish_state();
67  });
68  }
69  if (this->eco_id_.has_value()) {
70  this->parent_->register_listener(*this->eco_id_, [this](const TuyaDatapoint &datapoint) {
71  this->eco_ = datapoint.value_bool;
72  ESP_LOGV(TAG, "MCU reported eco is: %s", ONOFF(this->eco_));
73  this->compute_preset_();
75  this->publish_state();
76  });
77  }
78  if (this->sleep_id_.has_value()) {
79  this->parent_->register_listener(*this->sleep_id_, [this](const TuyaDatapoint &datapoint) {
80  this->sleep_ = datapoint.value_bool;
81  ESP_LOGV(TAG, "MCU reported sleep is: %s", ONOFF(this->sleep_));
82  this->compute_preset_();
84  this->publish_state();
85  });
86  }
87  if (this->swing_vertical_id_.has_value()) {
88  this->parent_->register_listener(*this->swing_vertical_id_, [this](const TuyaDatapoint &datapoint) {
89  this->swing_vertical_ = datapoint.value_bool;
90  ESP_LOGV(TAG, "MCU reported vertical swing is: %s", ONOFF(datapoint.value_bool));
91  this->compute_swingmode_();
92  this->publish_state();
93  });
94  }
95 
96  if (this->swing_horizontal_id_.has_value()) {
97  this->parent_->register_listener(*this->swing_horizontal_id_, [this](const TuyaDatapoint &datapoint) {
98  this->swing_horizontal_ = datapoint.value_bool;
99  ESP_LOGV(TAG, "MCU reported horizontal swing is: %s", ONOFF(datapoint.value_bool));
100  this->compute_swingmode_();
101  this->publish_state();
102  });
103  }
104 
105  if (this->fan_speed_id_.has_value()) {
106  this->parent_->register_listener(*this->fan_speed_id_, [this](const TuyaDatapoint &datapoint) {
107  ESP_LOGV(TAG, "MCU reported Fan Speed Mode is: %u", datapoint.value_enum);
108  this->fan_state_ = datapoint.value_enum;
109  this->compute_fanmode_();
110  this->publish_state();
111  });
112  }
113 }
114 
116  if (this->active_state_id_.has_value())
117  return;
118 
119  bool state_changed = false;
120  if (this->heating_state_pin_ != nullptr) {
121  bool heating_state = this->heating_state_pin_->digital_read();
122  if (heating_state != this->heating_state_) {
123  ESP_LOGV(TAG, "Heating state pin changed to: %s", ONOFF(heating_state));
124  this->heating_state_ = heating_state;
125  state_changed = true;
126  }
127  }
128  if (this->cooling_state_pin_ != nullptr) {
129  bool cooling_state = this->cooling_state_pin_->digital_read();
130  if (cooling_state != this->cooling_state_) {
131  ESP_LOGV(TAG, "Cooling state pin changed to: %s", ONOFF(cooling_state));
132  this->cooling_state_ = cooling_state;
133  state_changed = true;
134  }
135  }
136 
137  if (state_changed) {
138  this->compute_state_();
139  this->publish_state();
140  }
141 }
142 
144  if (call.get_mode().has_value()) {
145  const bool switch_state = *call.get_mode() != climate::CLIMATE_MODE_OFF;
146  ESP_LOGV(TAG, "Setting switch: %s", ONOFF(switch_state));
147  this->parent_->set_boolean_datapoint_value(*this->switch_id_, switch_state);
148  const climate::ClimateMode new_mode = *call.get_mode();
149 
150  if (new_mode == climate::CLIMATE_MODE_HEAT && this->supports_heat_) {
152  } else if (new_mode == climate::CLIMATE_MODE_COOL && this->supports_cool_) {
154  } else if (new_mode == climate::CLIMATE_MODE_DRY && this->active_state_drying_value_.has_value()) {
156  } else if (new_mode == climate::CLIMATE_MODE_FAN_ONLY && this->active_state_fanonly_value_.has_value()) {
158  }
159  }
160 
161  control_swing_mode_(call);
162  control_fan_mode_(call);
163 
164  if (call.get_target_temperature().has_value()) {
166  if (this->reports_fahrenheit_)
167  target_temperature = (target_temperature * 9 / 5) + 32;
168 
169  ESP_LOGV(TAG, "Setting target temperature: %.1f", target_temperature);
171  (int) (target_temperature / this->target_temperature_multiplier_));
172  }
173 
174  if (call.get_preset().has_value()) {
175  const climate::ClimatePreset preset = *call.get_preset();
176  if (this->eco_id_.has_value()) {
177  const bool eco = preset == climate::CLIMATE_PRESET_ECO;
178  ESP_LOGV(TAG, "Setting eco: %s", ONOFF(eco));
179  this->parent_->set_boolean_datapoint_value(*this->eco_id_, eco);
180  }
181  if (this->sleep_id_.has_value()) {
182  const bool sleep = preset == climate::CLIMATE_PRESET_SLEEP;
183  ESP_LOGV(TAG, "Setting sleep: %s", ONOFF(sleep));
184  this->parent_->set_boolean_datapoint_value(*this->sleep_id_, sleep);
185  }
186  }
187 }
188 
190  bool vertical_swing_changed = false;
191  bool horizontal_swing_changed = false;
192 
193  if (call.get_swing_mode().has_value()) {
194  const auto swing_mode = *call.get_swing_mode();
195 
196  switch (swing_mode) {
199  this->swing_vertical_ = false;
200  this->swing_horizontal_ = false;
201  vertical_swing_changed = true;
202  horizontal_swing_changed = true;
203  }
204  break;
205 
208  this->swing_vertical_ = true;
209  this->swing_horizontal_ = true;
210  vertical_swing_changed = true;
211  horizontal_swing_changed = true;
212  }
213  break;
214 
217  this->swing_vertical_ = true;
218  this->swing_horizontal_ = false;
219  vertical_swing_changed = true;
220  horizontal_swing_changed = true;
221  }
222  break;
223 
226  this->swing_vertical_ = false;
227  this->swing_horizontal_ = true;
228  vertical_swing_changed = true;
229  horizontal_swing_changed = true;
230  }
231  break;
232 
233  default:
234  break;
235  }
236  }
237 
238  if (vertical_swing_changed && this->swing_vertical_id_.has_value()) {
239  ESP_LOGV(TAG, "Setting vertical swing: %s", ONOFF(swing_vertical_));
241  }
242 
243  if (horizontal_swing_changed && this->swing_horizontal_id_.has_value()) {
244  ESP_LOGV(TAG, "Setting horizontal swing: %s", ONOFF(swing_horizontal_));
246  }
247 
248  // Publish the state after updating the swing mode
249  this->publish_state();
250 }
251 
253  if (call.get_fan_mode().has_value()) {
255 
256  uint8_t tuya_fan_speed;
257  switch (fan_mode) {
259  tuya_fan_speed = *fan_speed_low_value_;
260  break;
262  tuya_fan_speed = *fan_speed_medium_value_;
263  break;
265  tuya_fan_speed = *fan_speed_middle_value_;
266  break;
268  tuya_fan_speed = *fan_speed_high_value_;
269  break;
271  tuya_fan_speed = *fan_speed_auto_value_;
272  break;
273  default:
274  tuya_fan_speed = 0;
275  break;
276  }
277 
278  if (this->fan_speed_id_.has_value()) {
279  this->parent_->set_enum_datapoint_value(*this->fan_speed_id_, tuya_fan_speed);
280  }
281  }
282 }
283 
288  if (supports_heat_)
290  if (supports_cool_)
296  if (this->eco_id_.has_value()) {
298  }
299  if (this->sleep_id_.has_value()) {
301  }
302  if (this->sleep_id_.has_value() || this->eco_id_.has_value()) {
304  }
306  std::set<climate::ClimateSwingMode> supported_swing_modes = {
309  traits.set_supported_swing_modes(std::move(supported_swing_modes));
310  } else if (this->swing_vertical_id_.has_value()) {
311  std::set<climate::ClimateSwingMode> supported_swing_modes = {climate::CLIMATE_SWING_OFF,
313  traits.set_supported_swing_modes(std::move(supported_swing_modes));
314  } else if (this->swing_horizontal_id_.has_value()) {
315  std::set<climate::ClimateSwingMode> supported_swing_modes = {climate::CLIMATE_SWING_OFF,
317  traits.set_supported_swing_modes(std::move(supported_swing_modes));
318  }
319 
320  if (fan_speed_id_) {
331  }
332  return traits;
333 }
334 
336  LOG_CLIMATE("", "Tuya Climate", this);
337  if (this->switch_id_.has_value()) {
338  ESP_LOGCONFIG(TAG, " Switch has datapoint ID %u", *this->switch_id_);
339  }
340  if (this->active_state_id_.has_value()) {
341  ESP_LOGCONFIG(TAG, " Active state has datapoint ID %u", *this->active_state_id_);
342  }
343  if (this->target_temperature_id_.has_value()) {
344  ESP_LOGCONFIG(TAG, " Target Temperature has datapoint ID %u", *this->target_temperature_id_);
345  }
346  if (this->current_temperature_id_.has_value()) {
347  ESP_LOGCONFIG(TAG, " Current Temperature has datapoint ID %u", *this->current_temperature_id_);
348  }
349  LOG_PIN(" Heating State Pin: ", this->heating_state_pin_);
350  LOG_PIN(" Cooling State Pin: ", this->cooling_state_pin_);
351  if (this->eco_id_.has_value()) {
352  ESP_LOGCONFIG(TAG, " Eco has datapoint ID %u", *this->eco_id_);
353  }
354  if (this->sleep_id_.has_value()) {
355  ESP_LOGCONFIG(TAG, " Sleep has datapoint ID %u", *this->sleep_id_);
356  }
357  if (this->swing_vertical_id_.has_value()) {
358  ESP_LOGCONFIG(TAG, " Swing Vertical has datapoint ID %u", *this->swing_vertical_id_);
359  }
360  if (this->swing_horizontal_id_.has_value()) {
361  ESP_LOGCONFIG(TAG, " Swing Horizontal has datapoint ID %u", *this->swing_horizontal_id_);
362  }
363 }
364 
366  if (this->eco_) {
368  } else if (this->sleep_) {
370  } else {
372  }
373 }
374 
376  if (this->swing_vertical_ && this->swing_horizontal_) {
378  } else if (this->swing_vertical_) {
380  } else if (this->swing_horizontal_) {
382  } else {
384  }
385 }
386 
388  if (this->fan_speed_id_.has_value()) {
389  // Use state from MCU datapoint
390  if (this->fan_speed_auto_value_.has_value() && this->fan_state_ == this->fan_speed_auto_value_) {
392  } else if (this->fan_speed_high_value_.has_value() && this->fan_state_ == this->fan_speed_high_value_) {
394  } else if (this->fan_speed_medium_value_.has_value() && this->fan_state_ == this->fan_speed_medium_value_) {
396  } else if (this->fan_speed_middle_value_.has_value() && this->fan_state_ == this->fan_speed_middle_value_) {
398  } else if (this->fan_speed_low_value_.has_value() && this->fan_state_ == this->fan_speed_low_value_) {
400  }
401  }
402 }
403 
405  if (this->eco_ && this->eco_temperature_.has_value()) {
406  this->target_temperature = *this->eco_temperature_;
407  } else {
409  }
410 }
411 
413  if (std::isnan(this->current_temperature) || std::isnan(this->target_temperature)) {
414  // if any control parameters are nan, go to OFF action (not IDLE!)
416  return;
417  }
418 
419  if (this->mode == climate::CLIMATE_MODE_OFF) {
421  return;
422  }
423 
425  if (this->active_state_id_.has_value()) {
426  // Use state from MCU datapoint
429  target_action = climate::CLIMATE_ACTION_HEATING;
431  } else if (this->supports_cool_ && this->active_state_cooling_value_.has_value() &&
433  target_action = climate::CLIMATE_ACTION_COOLING;
435  } else if (this->active_state_drying_value_.has_value() &&
436  this->active_state_ == this->active_state_drying_value_) {
437  target_action = climate::CLIMATE_ACTION_DRYING;
439  } else if (this->active_state_fanonly_value_.has_value() &&
441  target_action = climate::CLIMATE_ACTION_FAN;
443  }
444  } else if (this->heating_state_pin_ != nullptr || this->cooling_state_pin_ != nullptr) {
445  // Use state from input pins
446  if (this->heating_state_) {
447  target_action = climate::CLIMATE_ACTION_HEATING;
449  } else if (this->cooling_state_) {
450  target_action = climate::CLIMATE_ACTION_COOLING;
452  }
453  } else {
454  // Fallback to active state calc based on temp and hysteresis
455  const float temp_diff = this->target_temperature - this->current_temperature;
456  if (std::abs(temp_diff) > this->hysteresis_) {
457  if (this->supports_heat_ && temp_diff > 0) {
458  target_action = climate::CLIMATE_ACTION_HEATING;
460  } else if (this->supports_cool_ && temp_diff < 0) {
461  target_action = climate::CLIMATE_ACTION_COOLING;
463  }
464  }
465  }
466 
467  this->switch_to_action_(target_action);
468 }
469 
471  // For now this just sets the current action but could include triggers later
472  this->action = action;
473 }
474 
475 } // namespace tuya
476 } // namespace esphome
This class is used to encode all control actions on a climate device.
Definition: climate.h:33
The fan mode is set to Low.
Definition: climate_mode.h:54
The climate device is off (inactive or no power)
Definition: climate_mode.h:33
ClimateSwingMode swing_mode
The active swing mode of the climate device.
Definition: climate.h:202
optional< uint8_t > sleep_id_
Definition: tuya_climate.h:105
void set_enum_datapoint_value(uint8_t datapoint_id, uint8_t value)
Definition: tuya.cpp:551
The fan mode is set to Both.
Definition: climate_mode.h:74
optional< uint8_t > fan_speed_low_value_
Definition: tuya_climate.h:112
optional< uint8_t > swing_vertical_id_
Definition: tuya_climate.h:109
The climate device is drying.
Definition: climate_mode.h:41
ClimatePreset
Enum for all preset modes.
Definition: climate_mode.h:82
float target_temperature
The target temperature of the climate device.
Definition: climate.h:186
void switch_to_action_(climate::ClimateAction action)
Switch the climate device to the given climate mode.
The climate device is in fan only mode.
Definition: climate_mode.h:43
const optional< ClimateMode > & get_mode() const
Definition: climate.cpp:273
The fan mode is set to Middle.
Definition: climate_mode.h:60
This class contains all static data for climate devices.
void control_fan_mode_(const climate::ClimateCall &call)
Override control to change settings of fan mode.
The climate device is set to heat to reach the target temperature.
Definition: climate_mode.h:18
optional< uint8_t > fan_speed_medium_value_
Definition: tuya_climate.h:113
ClimateMode mode
The active mode of the climate device.
Definition: climate.h:173
float current_temperature
The current temperature of the climate device, as reported from the integration.
Definition: climate.h:179
optional< uint8_t > active_state_cooling_value_
Definition: tuya_climate.h:94
optional< uint8_t > swing_horizontal_id_
Definition: tuya_climate.h:110
bool has_value() const
Definition: optional.h:87
void register_listener(uint8_t datapoint_id, const std::function< void(TuyaDatapoint)> &func)
Definition: tuya.cpp:667
The climate device is set to dry/humidity mode.
Definition: climate_mode.h:22
virtual void setup()=0
optional< uint8_t > switch_id_
Definition: tuya_climate.h:91
void add_supported_preset(ClimatePreset preset)
Device is prepared for sleep.
Definition: climate_mode.h:96
void compute_swingmode_()
Re-Compute the swing mode of this climate controller.
optional< uint8_t > fan_speed_auto_value_
Definition: tuya_climate.h:116
optional< uint8_t > active_state_drying_value_
Definition: tuya_climate.h:95
void compute_target_temperature_()
Re-compute the target temperature of this climate controller.
const char *const TAG
Definition: spi.cpp:8
The fan mode is set to Horizontal.
Definition: climate_mode.h:78
The climate device is set to cool to reach the target temperature.
Definition: climate_mode.h:16
const optional< ClimatePreset > & get_preset() const
Definition: climate.cpp:280
void compute_fanmode_()
Re-Compute the fan mode of this climate controller.
The fan mode is set to Auto.
Definition: climate_mode.h:52
void control(const climate::ClimateCall &call) override
Override control to change settings of the climate device.
optional< ClimatePreset > preset
The active preset of the climate device.
Definition: climate.h:208
optional< uint8_t > active_state_fanonly_value_
Definition: tuya_climate.h:96
optional< uint8_t > active_state_id_
Definition: tuya_climate.h:92
optional< uint8_t > eco_id_
Definition: tuya_climate.h:104
ClimateAction
Enum for the current action of the climate device. Values match those of ClimateMode.
Definition: climate_mode.h:31
optional< float > eco_temperature_
Definition: tuya_climate.h:106
The climate device is set to heat/cool to reach the target temperature.
Definition: climate_mode.h:14
The fan mode is set to Vertical.
Definition: climate_mode.h:76
The climate device is actively heating.
Definition: climate_mode.h:37
void add_supported_fan_mode(ClimateFanMode mode)
optional< uint8_t > fan_speed_middle_value_
Definition: tuya_climate.h:114
optional< uint8_t > fan_speed_high_value_
Definition: tuya_climate.h:115
const optional< float > & get_target_temperature() const
Definition: climate.cpp:274
optional< uint8_t > active_state_heating_value_
Definition: tuya_climate.h:93
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition: climate.cpp:395
virtual bool digital_read()=0
The fan mode is set to High.
Definition: climate_mode.h:58
ClimateMode
Enum for all modes a climate device can be in.
Definition: climate_mode.h:10
The swing mode is set to Off.
Definition: climate_mode.h:72
The climate device is off.
Definition: climate_mode.h:12
void set_supports_action(bool supports_action)
climate::ClimateTraits traits() override
Return the traits of this controller.
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition: climate.h:199
void set_boolean_datapoint_value(uint8_t datapoint_id, bool value)
Definition: tuya.cpp:539
const optional< ClimateFanMode > & get_fan_mode() const
Definition: climate.cpp:278
void set_integer_datapoint_value(uint8_t datapoint_id, uint32_t value)
Definition: tuya.cpp:543
const optional< ClimateSwingMode > & get_swing_mode() const
Definition: climate.cpp:282
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
optional< uint8_t > fan_speed_id_
Definition: tuya_climate.h:111
void set_supported_swing_modes(std::set< ClimateSwingMode > modes)
The climate device is idle (monitoring climate but no action needed)
Definition: climate_mode.h:39
Device is running an energy-saving preset.
Definition: climate_mode.h:94
void set_supports_current_temperature(bool supports_current_temperature)
void compute_state_()
Re-compute the state of this climate controller.
optional< uint8_t > target_temperature_id_
Definition: tuya_climate.h:99
The fan mode is set to Medium.
Definition: climate_mode.h:56
void control_swing_mode_(const climate::ClimateCall &call)
Override control to change settings of swing mode.
The climate device only has the fan enabled, no heating or cooling is taking place.
Definition: climate_mode.h:20
The climate device is actively cooling.
Definition: climate_mode.h:35
void add_supported_mode(ClimateMode mode)
void compute_preset_()
Re-compute the active preset of this climate controller.
optional< uint8_t > current_temperature_id_
Definition: tuya_climate.h:100
ClimateAction action
The active state of the climate device.
Definition: climate.h:176