mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-06-13 16:13:32 +02:00
Add support for new modes in Tuya Climate (#5159)
* Add support support for new modes Added support for Fan Only Mode, Dry Mode, Swing Mode and Fan Speed Control. Also added/fixed support for entity states syncing with current operation mode. * Add support for more climate modes in climate.tuya Added support for Fan Only Mode, Dry Mode, Swing Mode and Fan Speed Control. Also added/fixed support for entity states syncing with current operation mode. This commit fixes the namespace, because I uploaded the test files to start with. * Code Formatting Changes per Clang format. * More clang formatting fixes. * Breaking Change: Group YAML entries by type Add grouping to Preset, Swing Mode, Fan Speed and Active State. This is a breaking change. * Formatting Changes for validation Formatting changes to be compliant with black and flake8. Also changed constants to match expected format. * More constant value fixes * Final black formatting check? * Changes to init.py according to reviewer requests Make changes to _init_.py according to https://github.com/esphome/esphome/pull/5159/files/649b923804c05fa6ee750e824e3d7d70fadeabd9#r1278620976, https://github.com/esphome/esphome/pull/5159/files/649b923804c05fa6ee750e824e3d7d70fadeabd9#r1278621039, https://github.com/esphome/esphome/pull/5159/files/649b923804c05fa6ee750e824e3d7d70fadeabd9#r1278620904, and https://github.com/esphome/esphome/pull/5159/files/649b923804c05fa6ee750e824e3d7d70fadeabd9#r1278620549 Also put Sleep preset in its own config block to be consistent with other presets and fix logic for validate_cooling_values function to better align with existing documentation. * Commit reviewed change Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> * update deprecated config option wording * add "this->" to member variables that were missed adding "this->" to some member variables in the swing_mode function. * Update _init_.py to use Python 3.8 Walrus operator Adding Walrus Operator in the to_code function for _init_.py similar to https://github.com/esphome/esphome/pull/5181 * Fix Temperature_Multiplier config entry for code generation --------- Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Co-authored-by: Keith Burzinski <kbx81x@gmail.com>
This commit is contained in:
@@ -75,6 +75,41 @@ void TuyaClimate::setup() {
|
||||
this->publish_state();
|
||||
});
|
||||
}
|
||||
if (this->sleep_id_.has_value()) {
|
||||
this->parent_->register_listener(*this->sleep_id_, [this](const TuyaDatapoint &datapoint) {
|
||||
this->sleep_ = datapoint.value_bool;
|
||||
ESP_LOGV(TAG, "MCU reported sleep is: %s", ONOFF(this->sleep_));
|
||||
this->compute_preset_();
|
||||
this->compute_target_temperature_();
|
||||
this->publish_state();
|
||||
});
|
||||
}
|
||||
if (this->swing_vertical_id_.has_value()) {
|
||||
this->parent_->register_listener(*this->swing_vertical_id_, [this](const TuyaDatapoint &datapoint) {
|
||||
this->swing_vertical_ = datapoint.value_bool;
|
||||
ESP_LOGV(TAG, "MCU reported vertical swing is: %s", ONOFF(datapoint.value_bool));
|
||||
this->compute_swingmode_();
|
||||
this->publish_state();
|
||||
});
|
||||
}
|
||||
|
||||
if (this->swing_horizontal_id_.has_value()) {
|
||||
this->parent_->register_listener(*this->swing_horizontal_id_, [this](const TuyaDatapoint &datapoint) {
|
||||
this->swing_horizontal_ = datapoint.value_bool;
|
||||
ESP_LOGV(TAG, "MCU reported horizontal swing is: %s", ONOFF(datapoint.value_bool));
|
||||
this->compute_swingmode_();
|
||||
this->publish_state();
|
||||
});
|
||||
}
|
||||
|
||||
if (this->fan_speed_id_.has_value()) {
|
||||
this->parent_->register_listener(*this->fan_speed_id_, [this](const TuyaDatapoint &datapoint) {
|
||||
ESP_LOGV(TAG, "MCU reported Fan Speed Mode is: %u", datapoint.value_enum);
|
||||
this->fan_state_ = datapoint.value_enum;
|
||||
this->compute_fanmode_();
|
||||
this->publish_state();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void TuyaClimate::loop() {
|
||||
@@ -110,8 +145,22 @@ void TuyaClimate::control(const climate::ClimateCall &call) {
|
||||
const bool switch_state = *call.get_mode() != climate::CLIMATE_MODE_OFF;
|
||||
ESP_LOGV(TAG, "Setting switch: %s", ONOFF(switch_state));
|
||||
this->parent_->set_boolean_datapoint_value(*this->switch_id_, switch_state);
|
||||
const climate::ClimateMode new_mode = *call.get_mode();
|
||||
|
||||
if (new_mode == climate::CLIMATE_MODE_HEAT && this->supports_heat_) {
|
||||
this->parent_->set_enum_datapoint_value(*this->active_state_id_, *this->active_state_heating_value_);
|
||||
} else if (new_mode == climate::CLIMATE_MODE_COOL && this->supports_cool_) {
|
||||
this->parent_->set_enum_datapoint_value(*this->active_state_id_, *this->active_state_cooling_value_);
|
||||
} else if (new_mode == climate::CLIMATE_MODE_DRY && this->active_state_drying_value_.has_value()) {
|
||||
this->parent_->set_enum_datapoint_value(*this->active_state_id_, *this->active_state_drying_value_);
|
||||
} else if (new_mode == climate::CLIMATE_MODE_FAN_ONLY && this->active_state_fanonly_value_.has_value()) {
|
||||
this->parent_->set_enum_datapoint_value(*this->active_state_id_, *this->active_state_fanonly_value_);
|
||||
}
|
||||
}
|
||||
|
||||
control_swing_mode_(call);
|
||||
control_fan_mode_(call);
|
||||
|
||||
if (call.get_target_temperature().has_value()) {
|
||||
float target_temperature = *call.get_target_temperature();
|
||||
if (this->reports_fahrenheit_)
|
||||
@@ -129,6 +178,106 @@ void TuyaClimate::control(const climate::ClimateCall &call) {
|
||||
ESP_LOGV(TAG, "Setting eco: %s", ONOFF(eco));
|
||||
this->parent_->set_boolean_datapoint_value(*this->eco_id_, eco);
|
||||
}
|
||||
if (this->sleep_id_.has_value()) {
|
||||
const bool sleep = preset == climate::CLIMATE_PRESET_SLEEP;
|
||||
ESP_LOGV(TAG, "Setting sleep: %s", ONOFF(sleep));
|
||||
this->parent_->set_boolean_datapoint_value(*this->sleep_id_, sleep);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TuyaClimate::control_swing_mode_(const climate::ClimateCall &call) {
|
||||
bool vertical_swing_changed = false;
|
||||
bool horizontal_swing_changed = false;
|
||||
|
||||
if (call.get_swing_mode().has_value()) {
|
||||
const auto swing_mode = *call.get_swing_mode();
|
||||
|
||||
switch (swing_mode) {
|
||||
case climate::CLIMATE_SWING_OFF:
|
||||
if (swing_vertical_ || swing_horizontal_) {
|
||||
this->swing_vertical_ = false;
|
||||
this->swing_horizontal_ = false;
|
||||
vertical_swing_changed = true;
|
||||
horizontal_swing_changed = true;
|
||||
}
|
||||
break;
|
||||
|
||||
case climate::CLIMATE_SWING_BOTH:
|
||||
if (!swing_vertical_ || !swing_horizontal_) {
|
||||
this->swing_vertical_ = true;
|
||||
this->swing_horizontal_ = true;
|
||||
vertical_swing_changed = true;
|
||||
horizontal_swing_changed = true;
|
||||
}
|
||||
break;
|
||||
|
||||
case climate::CLIMATE_SWING_VERTICAL:
|
||||
if (!swing_vertical_ || swing_horizontal_) {
|
||||
this->swing_vertical_ = true;
|
||||
this->swing_horizontal_ = false;
|
||||
vertical_swing_changed = true;
|
||||
horizontal_swing_changed = true;
|
||||
}
|
||||
break;
|
||||
|
||||
case climate::CLIMATE_SWING_HORIZONTAL:
|
||||
if (swing_vertical_ || !swing_horizontal_) {
|
||||
this->swing_vertical_ = false;
|
||||
this->swing_horizontal_ = true;
|
||||
vertical_swing_changed = true;
|
||||
horizontal_swing_changed = true;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (vertical_swing_changed && this->swing_vertical_id_.has_value()) {
|
||||
ESP_LOGV(TAG, "Setting vertical swing: %s", ONOFF(swing_vertical_));
|
||||
this->parent_->set_boolean_datapoint_value(*this->swing_vertical_id_, swing_vertical_);
|
||||
}
|
||||
|
||||
if (horizontal_swing_changed && this->swing_horizontal_id_.has_value()) {
|
||||
ESP_LOGV(TAG, "Setting horizontal swing: %s", ONOFF(swing_horizontal_));
|
||||
this->parent_->set_boolean_datapoint_value(*this->swing_horizontal_id_, swing_horizontal_);
|
||||
}
|
||||
|
||||
// Publish the state after updating the swing mode
|
||||
this->publish_state();
|
||||
}
|
||||
|
||||
void TuyaClimate::control_fan_mode_(const climate::ClimateCall &call) {
|
||||
if (call.get_fan_mode().has_value()) {
|
||||
climate::ClimateFanMode fan_mode = *call.get_fan_mode();
|
||||
|
||||
uint8_t tuya_fan_speed;
|
||||
switch (fan_mode) {
|
||||
case climate::CLIMATE_FAN_LOW:
|
||||
tuya_fan_speed = *fan_speed_low_value_;
|
||||
break;
|
||||
case climate::CLIMATE_FAN_MEDIUM:
|
||||
tuya_fan_speed = *fan_speed_medium_value_;
|
||||
break;
|
||||
case climate::CLIMATE_FAN_MIDDLE:
|
||||
tuya_fan_speed = *fan_speed_middle_value_;
|
||||
break;
|
||||
case climate::CLIMATE_FAN_HIGH:
|
||||
tuya_fan_speed = *fan_speed_high_value_;
|
||||
break;
|
||||
case climate::CLIMATE_FAN_AUTO:
|
||||
tuya_fan_speed = *fan_speed_auto_value_;
|
||||
break;
|
||||
default:
|
||||
tuya_fan_speed = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
if (this->fan_speed_id_.has_value()) {
|
||||
this->parent_->set_enum_datapoint_value(*this->fan_speed_id_, tuya_fan_speed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -140,10 +289,46 @@ climate::ClimateTraits TuyaClimate::traits() {
|
||||
traits.add_supported_mode(climate::CLIMATE_MODE_HEAT);
|
||||
if (supports_cool_)
|
||||
traits.add_supported_mode(climate::CLIMATE_MODE_COOL);
|
||||
if (this->active_state_drying_value_.has_value())
|
||||
traits.add_supported_mode(climate::CLIMATE_MODE_DRY);
|
||||
if (this->active_state_fanonly_value_.has_value())
|
||||
traits.add_supported_mode(climate::CLIMATE_MODE_FAN_ONLY);
|
||||
if (this->eco_id_.has_value()) {
|
||||
traits.add_supported_preset(climate::CLIMATE_PRESET_NONE);
|
||||
traits.add_supported_preset(climate::CLIMATE_PRESET_ECO);
|
||||
}
|
||||
if (this->sleep_id_.has_value()) {
|
||||
traits.add_supported_preset(climate::CLIMATE_PRESET_SLEEP);
|
||||
}
|
||||
if (this->sleep_id_.has_value() || this->eco_id_.has_value()) {
|
||||
traits.add_supported_preset(climate::CLIMATE_PRESET_NONE);
|
||||
}
|
||||
if (this->swing_vertical_id_.has_value() && this->swing_horizontal_id_.has_value()) {
|
||||
std::set<climate::ClimateSwingMode> supported_swing_modes = {
|
||||
climate::CLIMATE_SWING_OFF, climate::CLIMATE_SWING_BOTH, climate::CLIMATE_SWING_VERTICAL,
|
||||
climate::CLIMATE_SWING_HORIZONTAL};
|
||||
traits.set_supported_swing_modes(std::move(supported_swing_modes));
|
||||
} else if (this->swing_vertical_id_.has_value()) {
|
||||
std::set<climate::ClimateSwingMode> supported_swing_modes = {climate::CLIMATE_SWING_OFF,
|
||||
climate::CLIMATE_SWING_VERTICAL};
|
||||
traits.set_supported_swing_modes(std::move(supported_swing_modes));
|
||||
} else if (this->swing_horizontal_id_.has_value()) {
|
||||
std::set<climate::ClimateSwingMode> supported_swing_modes = {climate::CLIMATE_SWING_OFF,
|
||||
climate::CLIMATE_SWING_HORIZONTAL};
|
||||
traits.set_supported_swing_modes(std::move(supported_swing_modes));
|
||||
}
|
||||
|
||||
if (fan_speed_id_) {
|
||||
if (fan_speed_low_value_)
|
||||
traits.add_supported_fan_mode(climate::CLIMATE_FAN_LOW);
|
||||
if (fan_speed_medium_value_)
|
||||
traits.add_supported_fan_mode(climate::CLIMATE_FAN_MEDIUM);
|
||||
if (fan_speed_middle_value_)
|
||||
traits.add_supported_fan_mode(climate::CLIMATE_FAN_MIDDLE);
|
||||
if (fan_speed_high_value_)
|
||||
traits.add_supported_fan_mode(climate::CLIMATE_FAN_HIGH);
|
||||
if (fan_speed_auto_value_)
|
||||
traits.add_supported_fan_mode(climate::CLIMATE_FAN_AUTO);
|
||||
}
|
||||
return traits;
|
||||
}
|
||||
|
||||
@@ -166,16 +351,56 @@ void TuyaClimate::dump_config() {
|
||||
if (this->eco_id_.has_value()) {
|
||||
ESP_LOGCONFIG(TAG, " Eco has datapoint ID %u", *this->eco_id_);
|
||||
}
|
||||
if (this->sleep_id_.has_value()) {
|
||||
ESP_LOGCONFIG(TAG, " Sleep has datapoint ID %u", *this->sleep_id_);
|
||||
}
|
||||
if (this->swing_vertical_id_.has_value()) {
|
||||
ESP_LOGCONFIG(TAG, " Swing Vertical has datapoint ID %u", *this->swing_vertical_id_);
|
||||
}
|
||||
if (this->swing_horizontal_id_.has_value()) {
|
||||
ESP_LOGCONFIG(TAG, " Swing Horizontal has datapoint ID %u", *this->swing_horizontal_id_);
|
||||
}
|
||||
}
|
||||
|
||||
void TuyaClimate::compute_preset_() {
|
||||
if (this->eco_) {
|
||||
this->preset = climate::CLIMATE_PRESET_ECO;
|
||||
} else if (this->sleep_) {
|
||||
this->preset = climate::CLIMATE_PRESET_SLEEP;
|
||||
} else {
|
||||
this->preset = climate::CLIMATE_PRESET_NONE;
|
||||
}
|
||||
}
|
||||
|
||||
void TuyaClimate::compute_swingmode_() {
|
||||
if (this->swing_vertical_ && this->swing_horizontal_) {
|
||||
this->swing_mode = climate::CLIMATE_SWING_BOTH;
|
||||
} else if (this->swing_vertical_) {
|
||||
this->swing_mode = climate::CLIMATE_SWING_VERTICAL;
|
||||
} else if (this->swing_horizontal_) {
|
||||
this->swing_mode = climate::CLIMATE_SWING_HORIZONTAL;
|
||||
} else {
|
||||
this->swing_mode = climate::CLIMATE_SWING_OFF;
|
||||
}
|
||||
}
|
||||
|
||||
void TuyaClimate::compute_fanmode_() {
|
||||
if (this->fan_speed_id_.has_value()) {
|
||||
// Use state from MCU datapoint
|
||||
if (this->fan_speed_auto_value_.has_value() && this->fan_state_ == this->fan_speed_auto_value_) {
|
||||
this->fan_mode = climate::CLIMATE_FAN_AUTO;
|
||||
} else if (this->fan_speed_high_value_.has_value() && this->fan_state_ == this->fan_speed_high_value_) {
|
||||
this->fan_mode = climate::CLIMATE_FAN_HIGH;
|
||||
} else if (this->fan_speed_medium_value_.has_value() && this->fan_state_ == this->fan_speed_medium_value_) {
|
||||
this->fan_mode = climate::CLIMATE_FAN_MEDIUM;
|
||||
} else if (this->fan_speed_middle_value_.has_value() && this->fan_state_ == this->fan_speed_middle_value_) {
|
||||
this->fan_mode = climate::CLIMATE_FAN_MIDDLE;
|
||||
} else if (this->fan_speed_low_value_.has_value() && this->fan_state_ == this->fan_speed_low_value_) {
|
||||
this->fan_mode = climate::CLIMATE_FAN_LOW;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TuyaClimate::compute_target_temperature_() {
|
||||
if (this->eco_ && this->eco_temperature_.has_value()) {
|
||||
this->target_temperature = *this->eco_temperature_;
|
||||
@@ -202,16 +427,28 @@ void TuyaClimate::compute_state_() {
|
||||
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;
|
||||
this->mode = climate::CLIMATE_MODE_HEAT;
|
||||
} else if (this->supports_cool_ && this->active_state_cooling_value_.has_value() &&
|
||||
this->active_state_ == this->active_state_cooling_value_) {
|
||||
target_action = climate::CLIMATE_ACTION_COOLING;
|
||||
this->mode = climate::CLIMATE_MODE_COOL;
|
||||
} else if (this->active_state_drying_value_.has_value() &&
|
||||
this->active_state_ == this->active_state_drying_value_) {
|
||||
target_action = climate::CLIMATE_ACTION_DRYING;
|
||||
this->mode = climate::CLIMATE_MODE_DRY;
|
||||
} else if (this->active_state_fanonly_value_.has_value() &&
|
||||
this->active_state_ == this->active_state_fanonly_value_) {
|
||||
target_action = climate::CLIMATE_ACTION_FAN;
|
||||
this->mode = climate::CLIMATE_MODE_FAN_ONLY;
|
||||
}
|
||||
} 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;
|
||||
this->mode = climate::CLIMATE_MODE_HEAT;
|
||||
} else if (this->cooling_state_) {
|
||||
target_action = climate::CLIMATE_ACTION_COOLING;
|
||||
this->mode = climate::CLIMATE_MODE_COOL;
|
||||
}
|
||||
} else {
|
||||
// Fallback to active state calc based on temp and hysteresis
|
||||
@@ -219,8 +456,10 @@ void TuyaClimate::compute_state_() {
|
||||
if (std::abs(temp_diff) > this->hysteresis_) {
|
||||
if (this->supports_heat_ && temp_diff > 0) {
|
||||
target_action = climate::CLIMATE_ACTION_HEATING;
|
||||
this->mode = climate::CLIMATE_MODE_HEAT;
|
||||
} else if (this->supports_cool_ && temp_diff < 0) {
|
||||
target_action = climate::CLIMATE_ACTION_COOLING;
|
||||
this->mode = climate::CLIMATE_MODE_COOL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user