From 31939918dc8d4c59bc5d22e2dbacc83d82fa38f0 Mon Sep 17 00:00:00 2001 From: Anton Sergunov Date: Wed, 13 Nov 2024 13:57:07 +0000 Subject: [PATCH] Make pid cimate respect mode --- esphome/components/pid/pid_climate.cpp | 18 +++++++++++++----- esphome/components/pid/pid_climate.h | 3 +++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/esphome/components/pid/pid_climate.cpp b/esphome/components/pid/pid_climate.cpp index 93b6999a00..118742b470 100644 --- a/esphome/components/pid/pid_climate.cpp +++ b/esphome/components/pid/pid_climate.cpp @@ -90,6 +90,18 @@ void PIDClimate::dump_config() { this->autotuner_->dump_config(); } } +float PIDClimate::value_by_mode_(float value) { + switch (this->mode) { + case climate::CLIMATE_MODE_OFF: + return 0.0f; + case climate::CLIMATE_MODE_HEAT: + return std::max(0.0f, value); + case climate::CLIMATE_MODE_COOL: + return std::min(0.0f, value); + default: + return value; + } +} void PIDClimate::write_output_(float value) { this->output_value_ = value; @@ -147,11 +159,7 @@ void PIDClimate::update_pid_() { } } - if (this->mode == climate::CLIMATE_MODE_OFF) { - this->write_output_(0.0); - } else { - this->write_output_(value); - } + this->write_output_(this->value_by_mode_(value)); if (this->do_publish_) this->publish_state(); diff --git a/esphome/components/pid/pid_climate.h b/esphome/components/pid/pid_climate.h index b5275e9775..d8fe8f0f6c 100644 --- a/esphome/components/pid/pid_climate.h +++ b/esphome/components/pid/pid_climate.h @@ -84,6 +84,9 @@ class PIDClimate : public climate::Climate, public Component { bool supports_cool_() const { return this->cool_output_ != nullptr; } bool supports_heat_() const { return this->heat_output_ != nullptr; } + /// Return cloest possible output value with respect to mode + float value_by_mode_(float value) const; + void write_output_(float value); /// The sensor used for getting the current temperature