[pid] Use register_apply_action for climate.pid.autotune (#19708)

This commit is contained in:
J. Nick Koston
2026-09-25 20:34:16 -04:00
committed by GitHub
parent a1d8ea6072
commit 0b2e5bfbbe
3 changed files with 20 additions and 41 deletions
+10 -18
View File
@@ -3,13 +3,11 @@ import esphome.codegen as cg
from esphome.components import climate, output, sensor
import esphome.config_validation as cv
from esphome.const import CONF_HUMIDITY_SENSOR, CONF_ID, CONF_SENSOR
from esphome.core import ID, Lambda
from esphome.cpp_generator import MockObj, TemplateArgsType
from esphome.core import Lambda
from esphome.types import ConfigType
pid_ns = cg.esphome_ns.namespace("pid")
PIDClimate = pid_ns.class_("PIDClimate", climate.Climate, cg.Component)
PIDAutotuneAction = pid_ns.class_("PIDAutotuneAction", automation.Action)
CONF_DEFAULT_TARGET_TEMPERATURE = "default_target_temperature"
@@ -158,9 +156,8 @@ automation.register_apply_action(
)
@automation.register_action(
automation.register_apply_action(
"climate.pid.autotune",
PIDAutotuneAction,
automation.maybe_simple_id(
{
cv.Required(CONF_ID): cv.use_id(PIDClimate),
@@ -173,20 +170,15 @@ automation.register_apply_action(
): cv.possibly_negative_percentage,
}
),
synchronous=True,
automation.ApplyCall(
"start_autotune({}, {}, {})",
(
(CONF_NOISEBAND, cg.float_),
(CONF_POSITIVE_OUTPUT, cg.float_),
(CONF_NEGATIVE_OUTPUT, cg.float_),
),
),
)
async def esp8266_set_frequency_to_code(
config: ConfigType,
action_id: ID,
template_arg: cg.TemplateArguments,
args: TemplateArgsType,
) -> MockObj:
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)
cg.add(var.set_noiseband(config[CONF_NOISEBAND]))
cg.add(var.set_positive_output(config[CONF_POSITIVE_OUTPUT]))
cg.add(var.set_negative_output(config[CONF_NEGATIVE_OUTPUT]))
return var
automation.register_apply_action(
+8
View File
@@ -169,6 +169,14 @@ void PIDClimate::update_pid_() {
if (this->do_publish_)
this->publish_state();
}
void PIDClimate::start_autotune(float noiseband, float positive_output, float negative_output) {
auto tuner = make_unique<PIDAutotuner>();
tuner->set_noiseband(noiseband);
tuner->set_output_positive(positive_output);
tuner->set_output_negative(negative_output);
this->start_autotune(std::move(tuner));
}
void PIDClimate::start_autotune(std::unique_ptr<PIDAutotuner> &&autotune) {
this->autotuner_ = std::move(autotune);
float min_value = this->supports_cool_() ? -1.0f : 0.0f;
+2 -23
View File
@@ -79,6 +79,8 @@ class PIDClimate final : public climate::Climate, public Component {
default_target_temperature_ = default_target_temperature;
}
void start_autotune(std::unique_ptr<PIDAutotuner> &&autotune);
/// Build a tuner from the climate.pid.autotune settings and start it.
void start_autotune(float noiseband, float positive_output, float negative_output);
void reset_integral_term();
protected:
@@ -109,27 +111,4 @@ class PIDClimate final : public climate::Climate, public Component {
bool do_publish_ = false;
};
template<typename... Ts> class PIDAutotuneAction final : public Action<Ts...> {
public:
PIDAutotuneAction(PIDClimate *parent) : parent_(parent) {}
void set_noiseband(float noiseband) { noiseband_ = noiseband; }
void set_positive_output(float positive_output) { positive_output_ = positive_output; }
void set_negative_output(float negative_output) { negative_output_ = negative_output; }
void play(const Ts &...x) {
auto tuner = make_unique<PIDAutotuner>();
tuner->set_noiseband(this->noiseband_);
tuner->set_output_negative(this->negative_output_);
tuner->set_output_positive(this->positive_output_);
this->parent_->start_autotune(std::move(tuner));
}
protected:
float noiseband_;
float positive_output_;
float negative_output_;
PIDClimate *parent_;
};
} // namespace esphome::pid