1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-29 22:24:26 +00:00

Thermostat enhancements and code clean-up (#2073)

This commit is contained in:
Keith Burzinski
2021-08-02 04:08:24 -05:00
committed by GitHub
parent 9b04e657db
commit 335210d788
4 changed files with 355 additions and 217 deletions

View File

@@ -7,6 +7,7 @@ namespace thermostat {
static const char *const TAG = "thermostat.climate";
void ThermostatClimate::setup() {
// add a callback so that whenever the sensor state changes we can take action
this->sensor_->add_on_state_callback([this](float state) {
this->current_temperature = state;
// required action may have changed, recompute, refresh
@@ -29,7 +30,12 @@ void ThermostatClimate::setup() {
this->setup_complete_ = true;
this->publish_state();
}
float ThermostatClimate::hysteresis() { return this->hysteresis_; }
float ThermostatClimate::cool_deadband() { return this->cool_deadband_; }
float ThermostatClimate::cool_overrun() { return this->cool_overrun_; }
float ThermostatClimate::heat_deadband() { return this->heat_deadband_; }
float ThermostatClimate::heat_overrun() { return this->heat_overrun_; }
void ThermostatClimate::refresh() {
this->switch_to_mode_(this->mode);
this->switch_to_action_(compute_action_());
@@ -38,6 +44,77 @@ void ThermostatClimate::refresh() {
this->check_temperature_change_trigger_();
this->publish_state();
}
bool ThermostatClimate::hysteresis_valid() {
if ((this->supports_cool_ || (this->supports_fan_only_ && this->supports_fan_only_cooling_)) &&
(isnan(this->cool_deadband_) || isnan(this->cool_overrun_)))
return false;
if (this->supports_heat_ && (isnan(this->heat_deadband_) || isnan(this->heat_overrun_)))
return false;
return true;
}
void ThermostatClimate::validate_target_temperature() {
if (isnan(this->target_temperature)) {
this->target_temperature =
((this->get_traits().get_visual_max_temperature() - this->get_traits().get_visual_min_temperature()) / 2) +
this->get_traits().get_visual_min_temperature();
} else {
// target_temperature must be between the visual minimum and the visual maximum
if (this->target_temperature < this->get_traits().get_visual_min_temperature())
this->target_temperature = this->get_traits().get_visual_min_temperature();
if (this->target_temperature > this->get_traits().get_visual_max_temperature())
this->target_temperature = this->get_traits().get_visual_max_temperature();
}
}
void ThermostatClimate::validate_target_temperatures() {
if (this->supports_two_points_) {
validate_target_temperature_low();
validate_target_temperature_high();
} else {
validate_target_temperature();
}
}
void ThermostatClimate::validate_target_temperature_low() {
if (isnan(this->target_temperature_low)) {
this->target_temperature_low = this->get_traits().get_visual_min_temperature();
} else {
// target_temperature_low must not be lower than the visual minimum
if (this->target_temperature_low < this->get_traits().get_visual_min_temperature())
this->target_temperature_low = this->get_traits().get_visual_min_temperature();
// target_temperature_low must not be greater than the visual maximum minus set_point_minimum_differential_
if (this->target_temperature_low >
this->get_traits().get_visual_max_temperature() - this->set_point_minimum_differential_)
this->target_temperature_low =
this->get_traits().get_visual_max_temperature() - this->set_point_minimum_differential_;
// if target_temperature_low is set greater than target_temperature_high, move up target_temperature_high
if (this->target_temperature_low > this->target_temperature_high - this->set_point_minimum_differential_)
this->target_temperature_high = this->target_temperature_low + this->set_point_minimum_differential_;
}
}
void ThermostatClimate::validate_target_temperature_high() {
if (isnan(this->target_temperature_high)) {
this->target_temperature_high = this->get_traits().get_visual_max_temperature();
} else {
// target_temperature_high must not be lower than the visual maximum
if (this->target_temperature_high > this->get_traits().get_visual_max_temperature())
this->target_temperature_high = this->get_traits().get_visual_max_temperature();
// target_temperature_high must not be lower than the visual minimum plus set_point_minimum_differential_
if (this->target_temperature_high <
this->get_traits().get_visual_min_temperature() + this->set_point_minimum_differential_)
this->target_temperature_high =
this->get_traits().get_visual_min_temperature() + this->set_point_minimum_differential_;
// if target_temperature_high is set less than target_temperature_low, move down target_temperature_low
if (this->target_temperature_high < this->target_temperature_low + this->set_point_minimum_differential_)
this->target_temperature_low = this->target_temperature_high - this->set_point_minimum_differential_;
}
}
void ThermostatClimate::control(const climate::ClimateCall &call) {
if (call.get_preset().has_value()) {
// setup_complete_ blocks modifying/resetting the temps immediately after boot
@@ -53,29 +130,25 @@ void ThermostatClimate::control(const climate::ClimateCall &call) {
this->fan_mode = *call.get_fan_mode();
if (call.get_swing_mode().has_value())
this->swing_mode = *call.get_swing_mode();
if (call.get_target_temperature().has_value())
this->target_temperature = *call.get_target_temperature();
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();
// set point validation
if (this->supports_two_points_) {
if (this->target_temperature_low < this->get_traits().get_visual_min_temperature())
this->target_temperature_low = this->get_traits().get_visual_min_temperature();
if (this->target_temperature_high > this->get_traits().get_visual_max_temperature())
this->target_temperature_high = this->get_traits().get_visual_max_temperature();
if (this->target_temperature_high < this->target_temperature_low)
this->target_temperature_high = this->target_temperature_low;
if (call.get_target_temperature_low().has_value()) {
this->target_temperature_low = *call.get_target_temperature_low();
validate_target_temperature_low();
}
if (call.get_target_temperature_high().has_value()) {
this->target_temperature_high = *call.get_target_temperature_high();
validate_target_temperature_high();
}
} else {
if (this->target_temperature < this->get_traits().get_visual_min_temperature())
this->target_temperature = this->get_traits().get_visual_min_temperature();
if (this->target_temperature > this->get_traits().get_visual_max_temperature())
this->target_temperature = this->get_traits().get_visual_max_temperature();
if (call.get_target_temperature().has_value()) {
this->target_temperature = *call.get_target_temperature();
validate_target_temperature();
}
}
// make any changes happen
refresh();
}
climate::ClimateTraits ThermostatClimate::traits() {
auto traits = climate::ClimateTraits();
traits.set_supports_current_temperature(true);
@@ -127,114 +200,54 @@ climate::ClimateTraits ThermostatClimate::traits() {
traits.set_supports_action(true);
return traits;
}
climate::ClimateAction ThermostatClimate::compute_action_() {
// we need to know the current climate action before anything else happens here
climate::ClimateAction target_action = this->action;
// if the climate mode is OFF then the climate action must be OFF
if (this->mode == climate::CLIMATE_MODE_OFF) {
auto target_action = climate::CLIMATE_ACTION_IDLE;
// if any hysteresis values or current_temperature is not valid, we go to OFF;
if (isnan(this->current_temperature) || !this->hysteresis_valid()) {
return climate::CLIMATE_ACTION_OFF;
} else if (this->action == climate::CLIMATE_ACTION_OFF) {
// ...but if the climate mode is NOT OFF then the climate action must not be OFF
target_action = climate::CLIMATE_ACTION_IDLE;
}
if (this->supports_two_points_) {
if (isnan(this->current_temperature) || isnan(this->target_temperature_low) ||
isnan(this->target_temperature_high) || isnan(this->hysteresis_))
// if any control parameters are nan, go to OFF action (not IDLE!)
return climate::CLIMATE_ACTION_OFF;
if (((this->action == climate::CLIMATE_ACTION_FAN) && (this->mode != climate::CLIMATE_MODE_FAN_ONLY)) ||
((this->action == climate::CLIMATE_ACTION_DRYING) && (this->mode != climate::CLIMATE_MODE_DRY))) {
target_action = climate::CLIMATE_ACTION_IDLE;
}
switch (this->mode) {
case climate::CLIMATE_MODE_FAN_ONLY:
if (this->supports_fan_only_) {
if (this->current_temperature > this->target_temperature_high + this->hysteresis_)
target_action = climate::CLIMATE_ACTION_FAN;
else if (this->current_temperature < this->target_temperature_high - this->hysteresis_)
if (this->action == climate::CLIMATE_ACTION_FAN)
target_action = climate::CLIMATE_ACTION_IDLE;
}
break;
case climate::CLIMATE_MODE_DRY:
target_action = climate::CLIMATE_ACTION_DRYING;
break;
case climate::CLIMATE_MODE_HEAT_COOL:
case climate::CLIMATE_MODE_COOL:
case climate::CLIMATE_MODE_HEAT:
if (this->supports_cool_) {
if (this->current_temperature > this->target_temperature_high + this->hysteresis_)
target_action = climate::CLIMATE_ACTION_COOLING;
else if (this->current_temperature < this->target_temperature_high - this->hysteresis_)
if (this->action == climate::CLIMATE_ACTION_COOLING)
target_action = climate::CLIMATE_ACTION_IDLE;
}
if (this->supports_heat_) {
if (this->current_temperature < this->target_temperature_low - this->hysteresis_)
target_action = climate::CLIMATE_ACTION_HEATING;
else if (this->current_temperature > this->target_temperature_low + this->hysteresis_)
if (this->action == climate::CLIMATE_ACTION_HEATING)
target_action = climate::CLIMATE_ACTION_IDLE;
}
break;
default:
break;
}
} else {
if (isnan(this->current_temperature) || isnan(this->target_temperature) || isnan(this->hysteresis_))
// if any control parameters are nan, go to OFF action (not IDLE!)
return climate::CLIMATE_ACTION_OFF;
if (((this->action == climate::CLIMATE_ACTION_FAN) && (this->mode != climate::CLIMATE_MODE_FAN_ONLY)) ||
((this->action == climate::CLIMATE_ACTION_DRYING) && (this->mode != climate::CLIMATE_MODE_DRY))) {
target_action = climate::CLIMATE_ACTION_IDLE;
}
switch (this->mode) {
case climate::CLIMATE_MODE_FAN_ONLY:
if (this->supports_fan_only_) {
if (this->current_temperature > this->target_temperature + this->hysteresis_)
target_action = climate::CLIMATE_ACTION_FAN;
else if (this->current_temperature < this->target_temperature - this->hysteresis_)
if (this->action == climate::CLIMATE_ACTION_FAN)
target_action = climate::CLIMATE_ACTION_IDLE;
}
break;
case climate::CLIMATE_MODE_DRY:
target_action = climate::CLIMATE_ACTION_DRYING;
break;
case climate::CLIMATE_MODE_COOL:
if (this->supports_cool_) {
if (this->current_temperature > this->target_temperature + this->hysteresis_)
target_action = climate::CLIMATE_ACTION_COOLING;
else if (this->current_temperature < this->target_temperature - this->hysteresis_)
if (this->action == climate::CLIMATE_ACTION_COOLING)
target_action = climate::CLIMATE_ACTION_IDLE;
}
case climate::CLIMATE_MODE_HEAT:
if (this->supports_heat_) {
if (this->current_temperature < this->target_temperature - this->hysteresis_)
target_action = climate::CLIMATE_ACTION_HEATING;
else if (this->current_temperature > this->target_temperature + this->hysteresis_)
if (this->action == climate::CLIMATE_ACTION_HEATING)
target_action = climate::CLIMATE_ACTION_IDLE;
}
break;
default:
break;
}
// ensure set point(s) is/are valid before computing the action
this->validate_target_temperatures();
// everything has been validated so we can now safely compute the action
switch (this->mode) {
// if the climate mode is OFF then the climate action must be OFF
case climate::CLIMATE_MODE_OFF:
target_action = climate::CLIMATE_ACTION_OFF;
break;
case climate::CLIMATE_MODE_FAN_ONLY:
if (this->fanning_required_())
target_action = climate::CLIMATE_ACTION_FAN;
break;
case climate::CLIMATE_MODE_DRY:
target_action = climate::CLIMATE_ACTION_DRYING;
break;
case climate::CLIMATE_MODE_HEAT_COOL:
if (this->cooling_required_() && this->heating_required_()) {
// this is bad and should never happen, so just stop.
// target_action = climate::CLIMATE_ACTION_IDLE;
} else if (this->cooling_required_()) {
target_action = climate::CLIMATE_ACTION_COOLING;
} else if (this->heating_required_()) {
target_action = climate::CLIMATE_ACTION_HEATING;
}
break;
case climate::CLIMATE_MODE_COOL:
if (this->cooling_required_()) {
target_action = climate::CLIMATE_ACTION_COOLING;
}
break;
case climate::CLIMATE_MODE_HEAT:
if (this->heating_required_()) {
target_action = climate::CLIMATE_ACTION_HEATING;
}
break;
default:
break;
}
// do not switch to an action that isn't enabled per the active climate mode
if ((this->mode == climate::CLIMATE_MODE_COOL) && (target_action == climate::CLIMATE_ACTION_HEATING))
target_action = climate::CLIMATE_ACTION_IDLE;
if ((this->mode == climate::CLIMATE_MODE_HEAT) && (target_action == climate::CLIMATE_ACTION_COOLING))
target_action = climate::CLIMATE_ACTION_IDLE;
return target_action;
}
void ThermostatClimate::switch_to_action_(climate::ClimateAction action) {
// setup_complete_ helps us ensure an action is called immediately after boot
if ((action == this->action) && this->setup_complete_)
@@ -284,6 +297,7 @@ void ThermostatClimate::switch_to_action_(climate::ClimateAction action) {
this->action = action;
this->prev_action_trigger_ = trig;
}
void ThermostatClimate::switch_to_fan_mode_(climate::ClimateFanMode fan_mode) {
// setup_complete_ helps us ensure an action is called immediately after boot
if ((fan_mode == this->prev_fan_mode_) && this->setup_complete_)
@@ -335,6 +349,7 @@ void ThermostatClimate::switch_to_fan_mode_(climate::ClimateFanMode fan_mode) {
this->prev_fan_mode_ = fan_mode;
this->prev_fan_mode_trigger_ = trig;
}
void ThermostatClimate::switch_to_mode_(climate::ClimateMode mode) {
// setup_complete_ helps us ensure an action is called immediately after boot
if ((mode == this->prev_mode_) && this->setup_complete_)
@@ -377,6 +392,7 @@ void ThermostatClimate::switch_to_mode_(climate::ClimateMode mode) {
this->prev_mode_ = mode;
this->prev_mode_trigger_ = trig;
}
void ThermostatClimate::switch_to_swing_mode_(climate::ClimateSwingMode swing_mode) {
// setup_complete_ helps us ensure an action is called immediately after boot
if ((swing_mode == this->prev_swing_mode_) && this->setup_complete_)
@@ -413,6 +429,7 @@ void ThermostatClimate::switch_to_swing_mode_(climate::ClimateSwingMode swing_mo
this->prev_swing_mode_ = swing_mode;
this->prev_swing_mode_trigger_ = trig;
}
void ThermostatClimate::check_temperature_change_trigger_() {
if (this->supports_two_points_) {
// setup_complete_ helps us ensure an action is called immediately after boot
@@ -437,6 +454,70 @@ void ThermostatClimate::check_temperature_change_trigger_() {
assert(trig != nullptr);
trig->trigger();
}
bool ThermostatClimate::cooling_required_() {
auto temperature = this->supports_two_points_ ? this->target_temperature_high : this->target_temperature;
if (this->supports_cool_) {
if (this->current_temperature > (temperature + this->cool_deadband_)) {
// if the current temperature exceeds the target + deadband, cooling is required
return true;
} else if (this->current_temperature < (temperature - this->cool_overrun_)) {
// if the current temperature is less than the target - overrun, cooling should stop
return false;
} else {
// if we get here, the current temperature is between target + deadband and target - overrun,
// so the action should not change unless it conflicts with the current mode
return (this->action == climate::CLIMATE_ACTION_COOLING) &&
((this->mode == climate::CLIMATE_MODE_HEAT_COOL) || (this->mode == climate::CLIMATE_MODE_COOL));
}
}
return false;
}
bool ThermostatClimate::fanning_required_() {
auto temperature = this->supports_two_points_ ? this->target_temperature_high : this->target_temperature;
if (this->supports_fan_only_) {
if (this->supports_fan_only_cooling_) {
if (this->current_temperature > (temperature + this->cool_deadband_)) {
// if the current temperature exceeds the target + deadband, fanning is required
return true;
} else if (this->current_temperature < (temperature - this->cool_overrun_)) {
// if the current temperature is less than the target - overrun, fanning should stop
return false;
} else {
// if we get here, the current temperature is between target + deadband and target - overrun,
// so the action should not change unless it conflicts with the current mode
return (this->action == climate::CLIMATE_ACTION_FAN) && (this->mode == climate::CLIMATE_MODE_FAN_ONLY);
}
} else {
return true;
}
}
return false;
}
bool ThermostatClimate::heating_required_() {
auto temperature = this->supports_two_points_ ? this->target_temperature_low : this->target_temperature;
if (this->supports_heat_) {
if (this->current_temperature < temperature - this->heat_deadband_) {
// if the current temperature is below the target - deadband, heating is required
return true;
} else if (this->current_temperature > temperature + this->heat_overrun_) {
// if the current temperature is above the target + overrun, heating should stop
return false;
} else {
// if we get here, the current temperature is between target - deadband and target + overrun,
// so the action should not change unless it conflicts with the current mode
return (this->action == climate::CLIMATE_ACTION_HEATING) &&
((this->mode == climate::CLIMATE_MODE_HEAT_COOL) || (this->mode == climate::CLIMATE_MODE_HEAT));
}
}
return false;
}
void ThermostatClimate::change_away_(bool away) {
if (!away) {
if (this->supports_two_points_) {
@@ -453,13 +534,16 @@ void ThermostatClimate::change_away_(bool away) {
}
this->preset = away ? climate::CLIMATE_PRESET_AWAY : climate::CLIMATE_PRESET_HOME;
}
void ThermostatClimate::set_normal_config(const ThermostatClimateTargetTempConfig &normal_config) {
this->normal_config_ = normal_config;
}
void ThermostatClimate::set_away_config(const ThermostatClimateTargetTempConfig &away_config) {
this->supports_away_ = true;
this->away_config_ = away_config;
}
ThermostatClimate::ThermostatClimate()
: cool_action_trigger_(new Trigger<>()),
cool_mode_trigger_(new Trigger<>()),
@@ -486,8 +570,15 @@ ThermostatClimate::ThermostatClimate()
swing_mode_horizontal_trigger_(new Trigger<>()),
swing_mode_vertical_trigger_(new Trigger<>()),
temperature_change_trigger_(new Trigger<>()) {}
void ThermostatClimate::set_default_mode(climate::ClimateMode default_mode) { this->default_mode_ = default_mode; }
void ThermostatClimate::set_hysteresis(float hysteresis) { this->hysteresis_ = hysteresis; }
void ThermostatClimate::set_set_point_minimum_differential(float differential) {
this->set_point_minimum_differential_ = differential;
}
void ThermostatClimate::set_cool_deadband(float deadband) { this->cool_deadband_ = deadband; }
void ThermostatClimate::set_cool_overrun(float overrun) { this->cool_overrun_ = overrun; }
void ThermostatClimate::set_heat_deadband(float deadband) { this->heat_deadband_ = deadband; }
void ThermostatClimate::set_heat_overrun(float overrun) { this->heat_overrun_ = overrun; }
void ThermostatClimate::set_sensor(sensor::Sensor *sensor) { this->sensor_ = sensor; }
void ThermostatClimate::set_supports_heat_cool(bool supports_heat_cool) {
this->supports_heat_cool_ = supports_heat_cool;
@@ -496,6 +587,9 @@ void ThermostatClimate::set_supports_auto(bool supports_auto) { this->supports_a
void ThermostatClimate::set_supports_cool(bool supports_cool) { this->supports_cool_ = supports_cool; }
void ThermostatClimate::set_supports_dry(bool supports_dry) { this->supports_dry_ = supports_dry; }
void ThermostatClimate::set_supports_fan_only(bool supports_fan_only) { this->supports_fan_only_ = supports_fan_only; }
void ThermostatClimate::set_supports_fan_only_cooling(bool supports_fan_only_cooling) {
this->supports_fan_only_cooling_ = supports_fan_only_cooling;
}
void ThermostatClimate::set_supports_heat(bool supports_heat) { this->supports_heat_ = supports_heat; }
void ThermostatClimate::set_supports_fan_mode_on(bool supports_fan_mode_on) {
this->supports_fan_mode_on_ = supports_fan_mode_on;
@@ -539,6 +633,7 @@ void ThermostatClimate::set_supports_swing_mode_vertical(bool supports_swing_mod
void ThermostatClimate::set_supports_two_points(bool supports_two_points) {
this->supports_two_points_ = supports_two_points;
}
Trigger<> *ThermostatClimate::get_cool_action_trigger() const { return this->cool_action_trigger_; }
Trigger<> *ThermostatClimate::get_dry_action_trigger() const { return this->dry_action_trigger_; }
Trigger<> *ThermostatClimate::get_fan_only_action_trigger() const { return this->fan_only_action_trigger_; }
@@ -564,6 +659,7 @@ Trigger<> *ThermostatClimate::get_swing_mode_off_trigger() const { return this->
Trigger<> *ThermostatClimate::get_swing_mode_horizontal_trigger() const { return this->swing_mode_horizontal_trigger_; }
Trigger<> *ThermostatClimate::get_swing_mode_vertical_trigger() const { return this->swing_mode_vertical_trigger_; }
Trigger<> *ThermostatClimate::get_temperature_change_trigger() const { return this->temperature_change_trigger_; }
void ThermostatClimate::dump_config() {
LOG_CLIMATE("", "Thermostat", this);
if (this->supports_heat_) {
@@ -578,12 +674,17 @@ void ThermostatClimate::dump_config() {
else
ESP_LOGCONFIG(TAG, " Default Target Temperature High: %.1f°C", this->normal_config_.default_temperature);
}
ESP_LOGCONFIG(TAG, " Hysteresis: %.1f°C", this->hysteresis_);
ESP_LOGCONFIG(TAG, " Minimum Set Point Differential: %.1f°C", this->set_point_minimum_differential_);
ESP_LOGCONFIG(TAG, " Cool Deadband: %.1f°C", this->cool_deadband_);
ESP_LOGCONFIG(TAG, " Cool Overrun: %.1f°C", this->cool_overrun_);
ESP_LOGCONFIG(TAG, " Heat Deadband: %.1f°C", this->heat_deadband_);
ESP_LOGCONFIG(TAG, " Heat Overrun: %.1f°C", this->heat_overrun_);
ESP_LOGCONFIG(TAG, " Supports AUTO: %s", YESNO(this->supports_auto_));
ESP_LOGCONFIG(TAG, " Supports HEAT/COOL: %s", YESNO(this->supports_heat_cool_));
ESP_LOGCONFIG(TAG, " Supports COOL: %s", YESNO(this->supports_cool_));
ESP_LOGCONFIG(TAG, " Supports DRY: %s", YESNO(this->supports_dry_));
ESP_LOGCONFIG(TAG, " Supports FAN_ONLY: %s", YESNO(this->supports_fan_only_));
ESP_LOGCONFIG(TAG, " Supports FAN_ONLY_COOLING: %s", YESNO(this->supports_fan_only_cooling_));
ESP_LOGCONFIG(TAG, " Supports HEAT: %s", YESNO(this->supports_heat_));
ESP_LOGCONFIG(TAG, " Supports FAN MODE ON: %s", YESNO(this->supports_fan_mode_on_));
ESP_LOGCONFIG(TAG, " Supports FAN MODE OFF: %s", YESNO(this->supports_fan_mode_off_));
@@ -619,8 +720,10 @@ void ThermostatClimate::dump_config() {
}
ThermostatClimateTargetTempConfig::ThermostatClimateTargetTempConfig() = default;
ThermostatClimateTargetTempConfig::ThermostatClimateTargetTempConfig(float default_temperature)
: default_temperature(default_temperature) {}
ThermostatClimateTargetTempConfig::ThermostatClimateTargetTempConfig(float default_temperature_low,
float default_temperature_high)
: default_temperature_low(default_temperature_low), default_temperature_high(default_temperature_high) {}