This commit is contained in:
Alexander Leisentritt 2024-05-15 02:06:59 -05:00 committed by GitHub
commit 0c8926d523
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 51 additions and 7 deletions

View File

@ -73,6 +73,7 @@ from esphome.const import (
CONF_PRESET_CHANGE = "preset_change"
CONF_DEFAULT_PRESET = "default_preset"
CONF_ON_BOOT_RESTORE_FROM = "on_boot_restore_from"
CONF_PRESET_TEMP_RESTORE = "preset_temp_restore"
CODEOWNERS = ["@kbx81"]
@ -116,6 +117,7 @@ PRESET_CONFIG_SCHEMA = cv.Schema(
cv.Optional(CONF_SWING_MODE): cv.templatable(
climate.validate_climate_swing_mode
),
cv.Optional(CONF_PRESET_TEMP_RESTORE, default=False): cv.boolean,
}
)
@ -925,6 +927,13 @@ async def to_code(config):
)
)
if CONF_PRESET_TEMP_RESTORE in preset_config:
cg.add(
preset_target_variable.set_preset_temp_restore(
preset_config[CONF_PRESET_TEMP_RESTORE]
)
)
if standard_preset is not None:
cg.add(var.set_preset_config(standard_preset, preset_target_variable))
else:

View File

@ -1041,18 +1041,48 @@ void ThermostatClimate::change_custom_preset_(const std::string &custom_preset)
bool ThermostatClimate::change_preset_internal_(const ThermostatClimateTargetTempConfig &config) {
bool something_changed = false;
ThermostatClimateTargetTempConfig *current_config = nullptr;
if (this->supports_two_points_) {
if (this->target_temperature_low != config.default_temperature_low) {
this->target_temperature_low = config.default_temperature_low;
something_changed = true;
if (this->preset.has_value()) {
climate::ClimatePreset preset = this->preset.value();
ThermostatClimateTargetTempConfig &current_preset_config = this->preset_config_.find(preset)->second;
current_config = &current_preset_config;
}
if (this->custom_preset.has_value()) {
const std::string &custom_preset = this->custom_preset.value();
ThermostatClimateTargetTempConfig &current_custom_preset_config =
this->custom_preset_config_.find(custom_preset)->second;
current_config = &current_custom_preset_config;
}
if (current_config != nullptr && current_config->preset_temp_restore_) {
if (supports_two_points_) {
current_config->stored_temperature_low = this->target_temperature_low;
current_config->stored_temperature_high = this->target_temperature_high;
} else {
current_config->stored_temperature = this->target_temperature;
}
if (this->target_temperature_high != config.default_temperature_high) {
this->target_temperature_high = config.default_temperature_high;
}
if (config.preset_temp_restore_) {
if (this->supports_two_points_) {
this->target_temperature_low =
std::isnan(config.stored_temperature_low) ? config.default_temperature_low : config.stored_temperature_low;
this->target_temperature_high =
std::isnan(config.stored_temperature_high) ? config.default_temperature_high : config.stored_temperature_high;
something_changed = true;
} else {
this->target_temperature =
std::isnan(config.stored_temperature) ? config.default_temperature : config.stored_temperature;
something_changed = true;
}
} else {
if (this->target_temperature != config.default_temperature) {
if (this->supports_two_points_) {
this->target_temperature_low = config.default_temperature_low;
this->target_temperature_high = config.default_temperature_high;
something_changed = true;
} else {
this->target_temperature = config.default_temperature;
something_changed = true;
}

View File

@ -43,10 +43,14 @@ struct ThermostatClimateTargetTempConfig {
void set_fan_mode(climate::ClimateFanMode fan_mode) { this->fan_mode_ = fan_mode; }
void set_swing_mode(climate::ClimateSwingMode swing_mode) { this->swing_mode_ = swing_mode; }
void set_mode(climate::ClimateMode mode) { this->mode_ = mode; }
void set_preset_temp_restore(bool preset_temp_restore) { this->preset_temp_restore_ = preset_temp_restore; }
float default_temperature{NAN};
float default_temperature_low{NAN};
float default_temperature_high{NAN};
float stored_temperature{NAN};
float stored_temperature_low{NAN};
float stored_temperature_high{NAN};
float cool_deadband_{NAN};
float cool_overrun_{NAN};
float heat_deadband_{NAN};
@ -54,6 +58,7 @@ struct ThermostatClimateTargetTempConfig {
optional<climate::ClimateFanMode> fan_mode_{};
optional<climate::ClimateSwingMode> swing_mode_{};
optional<climate::ClimateMode> mode_{};
bool preset_temp_restore_;
};
class ThermostatClimate : public climate::Climate, public Component {