Support direct relay state feedback for tuya climate component (#1668)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Stefan Rado
2021-09-15 10:23:35 +02:00
committed by GitHub
parent 0ea77de98c
commit 2db8c42e1d
3 changed files with 98 additions and 15 deletions
@@ -31,6 +31,15 @@ void TuyaClimate::setup() {
this->compute_state_();
this->publish_state();
});
} else {
if (this->heating_state_pin_ != nullptr) {
this->heating_state_pin_->setup();
this->heating_state_ = this->heating_state_pin_->digital_read();
}
if (this->cooling_state_pin_ != nullptr) {
this->cooling_state_pin_->setup();
this->cooling_state_ = this->cooling_state_pin_->digital_read();
}
}
if (this->target_temperature_id_.has_value()) {
this->parent_->register_listener(*this->target_temperature_id_, [this](const TuyaDatapoint &datapoint) {
@@ -50,6 +59,34 @@ void TuyaClimate::setup() {
}
}
void TuyaClimate::loop() {
if (this->active_state_id_.has_value())
return;
bool state_changed = false;
if (this->heating_state_pin_ != nullptr) {
bool heating_state = this->heating_state_pin_->digital_read();
if (heating_state != this->heating_state_) {
ESP_LOGV(TAG, "Heating state pin changed to: %s", ONOFF(heating_state));
this->heating_state_ = heating_state;
state_changed = true;
}
}
if (this->cooling_state_pin_ != nullptr) {
bool cooling_state = this->cooling_state_pin_->digital_read();
if (cooling_state != this->cooling_state_) {
ESP_LOGV(TAG, "Cooling state pin changed to: %s", ONOFF(cooling_state));
this->cooling_state_ = cooling_state;
state_changed = true;
}
}
if (state_changed) {
this->compute_state_();
this->publish_state();
}
}
void TuyaClimate::control(const climate::ClimateCall &call) {
if (call.get_mode().has_value()) {
const bool switch_state = *call.get_mode() != climate::CLIMATE_MODE_OFF;
@@ -86,6 +123,8 @@ void TuyaClimate::dump_config() {
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_);
LOG_PIN(" Heating State Pin: ", this->heating_state_pin_);
LOG_PIN(" Cooling State Pin: ", this->cooling_state_pin_);
}
void TuyaClimate::compute_state_() {
@@ -102,6 +141,7 @@ void TuyaClimate::compute_state_() {
climate::ClimateAction target_action = climate::CLIMATE_ACTION_IDLE;
if (this->active_state_id_.has_value()) {
// Use state from MCU datapoint
if (this->supports_heat_ && this->active_state_heating_value_.has_value() &&
this->active_state_ == this->active_state_heating_value_) {
target_action = climate::CLIMATE_ACTION_HEATING;
@@ -109,6 +149,13 @@ void TuyaClimate::compute_state_() {
this->active_state_ == this->active_state_cooling_value_) {
target_action = climate::CLIMATE_ACTION_COOLING;
}
} else if (this->heating_state_pin_ != nullptr || this->cooling_state_pin_ != nullptr) {
// Use state from input pins
if (this->heating_state_) {
target_action = climate::CLIMATE_ACTION_HEATING;
} else if (this->cooling_state_) {
target_action = climate::CLIMATE_ACTION_COOLING;
}
} else {
// Fallback to active state calc based on temp and hysteresis
const float temp_diff = this->target_temperature - this->current_temperature;