From 1d7349e6e448a9b33515ea4c01203b7483cf31c1 Mon Sep 17 00:00:00 2001 From: Alexander Leisentritt Date: Sat, 8 Oct 2022 07:38:05 +0000 Subject: [PATCH] add preset option to restore user set temperatures --- esphome/components/thermostat/climate.py | 9 ++++ .../thermostat/thermostat_climate.cpp | 44 ++++++++++++++++--- .../thermostat/thermostat_climate.h | 5 +++ 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/esphome/components/thermostat/climate.py b/esphome/components/thermostat/climate.py index 8aa61dbb93..c0ebf22159 100644 --- a/esphome/components/thermostat/climate.py +++ b/esphome/components/thermostat/climate.py @@ -71,6 +71,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"] @@ -114,6 +115,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, } ) @@ -907,6 +909,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: diff --git a/esphome/components/thermostat/thermostat_climate.cpp b/esphome/components/thermostat/thermostat_climate.cpp index 61e279d4a6..196d1621b6 100644 --- a/esphome/components/thermostat/thermostat_climate.cpp +++ b/esphome/components/thermostat/thermostat_climate.cpp @@ -1020,18 +1020,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 ¤t_preset_config = this->preset_config_.find(preset)->second; + current_config = ¤t_preset_config; + } + + if (this->custom_preset.has_value()) { + const std::string &custom_preset = this->custom_preset.value(); + ThermostatClimateTargetTempConfig ¤t_custom_preset_config = + this->custom_preset_config_.find(custom_preset)->second; + current_config = ¤t_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; } diff --git a/esphome/components/thermostat/thermostat_climate.h b/esphome/components/thermostat/thermostat_climate.h index 68cbb17e34..b08011af04 100644 --- a/esphome/components/thermostat/thermostat_climate.h +++ b/esphome/components/thermostat/thermostat_climate.h @@ -39,10 +39,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}; @@ -50,6 +54,7 @@ struct ThermostatClimateTargetTempConfig { optional fan_mode_{}; optional swing_mode_{}; optional mode_{}; + bool preset_temp_restore_; }; class ThermostatClimate : public climate::Climate, public Component {