Files
esphome-dev/esphome/components/tuya/climate/tuya_climate.cpp
T
Jesse Hills 68d29c5af5 Add support for Tuya Climate devices (#1076)
* Start support for tuya climate devices

* Add tuya climate to test4

* Fix lint and cast

* Remove blank line

* Try to display accurate action based on observed behaviour.

* Fix action when in off mode

* Improve config dump

* merge use of CONF_SWITCH_DATAPOINT

Co-authored-by: Guillermo Ruffino <glm.net@gmail.com>
2020-07-11 19:04:34 -03:00

155 lines
5.7 KiB
C++

#include "esphome/core/log.h"
#include "tuya_climate.h"
namespace esphome {
namespace tuya {
static const char *TAG = "tuya.climate";
void TuyaClimate::setup() {
if (this->switch_id_.has_value()) {
this->parent_->register_listener(*this->switch_id_, [this](TuyaDatapoint datapoint) {
if (datapoint.value_bool) {
this->mode = climate::CLIMATE_MODE_HEAT;
} else {
this->mode = climate::CLIMATE_MODE_OFF;
}
this->compute_state_();
this->publish_state();
ESP_LOGD(TAG, "MCU reported switch is: %s", ONOFF(datapoint.value_bool));
});
}
if (this->target_temperature_id_.has_value()) {
this->parent_->register_listener(*this->target_temperature_id_, [this](TuyaDatapoint datapoint) {
this->target_temperature = datapoint.value_int;
this->compute_state_();
this->publish_state();
ESP_LOGD(TAG, "MCU reported target temperature is: %d", datapoint.value_int);
});
}
if (this->current_temperature_id_.has_value()) {
this->parent_->register_listener(*this->current_temperature_id_, [this](TuyaDatapoint datapoint) {
this->current_temperature = datapoint.value_int;
this->compute_state_();
this->publish_state();
ESP_LOGD(TAG, "MCU reported current temperature is: %d", datapoint.value_int);
});
}
// if (this->eco_mode_id_.has_value()) {
// this->parent_->register_listener(*this->eco_mode_id_, [this](TuyaDatapoint datapoint) {
// this->eco_mode = datapoint.value_bool;
// this->compute_state_();
// this->publish_state();
// ESP_LOGD(TAG, "MCU reported eco mode of: %s", ONOFF(datapoint.value_bool));
// });
// }
}
void TuyaClimate::control(const climate::ClimateCall &call) {
if (call.get_mode().has_value()) {
this->mode = *call.get_mode();
TuyaDatapoint datapoint{};
datapoint.id = *this->switch_id_;
datapoint.type = TuyaDatapointType::BOOLEAN;
datapoint.value_bool = this->mode != climate::CLIMATE_MODE_OFF;
this->parent_->set_datapoint_value(datapoint);
ESP_LOGD(TAG, "Setting switch: %s", ONOFF(datapoint.value_bool));
}
if (call.get_target_temperature_low().has_value())
this->target_temperature_low = *call.get_target_temperature_low();
if (call.get_target_temperature_high().has_value())
this->target_temperature_high = *call.get_target_temperature_high();
if (call.get_target_temperature().has_value()) {
this->target_temperature = *call.get_target_temperature();
TuyaDatapoint datapoint{};
datapoint.id = *this->target_temperature_id_;
datapoint.type = TuyaDatapointType::INTEGER;
datapoint.value_int = (int) this->target_temperature;
this->parent_->set_datapoint_value(datapoint);
ESP_LOGD(TAG, "Setting target temperature: %d", datapoint.value_int);
}
// if (call.get_eco_mode().has_value()) {
// this->eco_mode = *call.get_eco_mode();
// TuyaDatapoint datapoint{};
// datapoint.id = *this->eco_mode_id_;
// datapoint.type = TuyaDatapointType::BOOLEAN;
// datapoint.value_bool = this->eco_mode;
// this->parent_->set_datapoint_value(datapoint);
// ESP_LOGD(TAG, "Setting eco mode: %s", ONOFF(datapoint.value_bool));
// }
this->compute_state_();
this->publish_state();
}
climate::ClimateTraits TuyaClimate::traits() {
auto traits = climate::ClimateTraits();
traits.set_supports_current_temperature(this->current_temperature_id_.has_value());
traits.set_supports_heat_mode(true);
// traits.set_supports_eco_mode(this->eco_mode_id_.has_value());
traits.set_supports_action(true);
return traits;
}
void TuyaClimate::dump_config() {
LOG_CLIMATE("", "Tuya Climate", this);
if (this->switch_id_.has_value())
ESP_LOGCONFIG(TAG, " Switch has datapoint ID %u", *this->switch_id_);
if (this->target_temperature_id_.has_value())
ESP_LOGCONFIG(TAG, " Target Temperature has datapoint ID %u", *this->target_temperature_id_);
if (this->current_temperature_id_.has_value())
ESP_LOGCONFIG(TAG, " Current Temperature has datapoint ID %u", *this->current_temperature_id_);
// if (this->eco_mode_id_.has_value())
// ESP_LOGCONFIG(TAG, " Eco Mode has datapoint ID %u", *this->mode_id_);
}
void TuyaClimate::compute_state_() {
if (isnan(this->current_temperature) || isnan(this->target_temperature)) {
// if any control parameters are nan, go to OFF action (not IDLE!)
this->switch_to_action_(climate::CLIMATE_ACTION_OFF);
return;
}
if (this->mode == climate::CLIMATE_MODE_OFF) {
this->switch_to_action_(climate::CLIMATE_ACTION_OFF);
return;
}
const bool too_cold = this->current_temperature < this->target_temperature - 1;
const bool too_hot = this->current_temperature > this->target_temperature + 1;
const bool on_target = this->current_temperature == this->target_temperature;
climate::ClimateAction target_action;
if (too_cold) {
// too cold -> show as heating if possible, else idle
if (this->traits().supports_mode(climate::CLIMATE_MODE_HEAT)) {
target_action = climate::CLIMATE_ACTION_HEATING;
} else {
target_action = climate::CLIMATE_ACTION_IDLE;
}
} else if (too_hot) {
// too hot -> show as cooling if possible, else idle
if (this->traits().supports_mode(climate::CLIMATE_MODE_COOL)) {
target_action = climate::CLIMATE_ACTION_COOLING;
} else {
target_action = climate::CLIMATE_ACTION_IDLE;
}
} else if (on_target) {
target_action = climate::CLIMATE_ACTION_IDLE;
} else {
target_action = this->action;
}
this->switch_to_action_(target_action);
}
void TuyaClimate::switch_to_action_(climate::ClimateAction action) {
// For now this just sets the current action but could include triggers later
this->action = action;
}
} // namespace tuya
} // namespace esphome