[rotary_encoder] Use register_apply_action for the set_value action (#19545)

This commit is contained in:
J. Nick Koston
2026-09-24 08:50:33 -04:00
committed by GitHub
parent 4cbced407d
commit f24e1f31f2
3 changed files with 3 additions and 32 deletions
@@ -4,7 +4,6 @@
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
#include "esphome/core/automation.h"
#include "esphome/components/sensor/sensor.h"
namespace esphome::rotary_encoder {
@@ -106,15 +105,4 @@ class RotaryEncoderSensor final : public sensor::Sensor, public Component {
CallbackManager<void(int32_t)> listeners_{};
};
template<typename... Ts> class RotaryEncoderSetValueAction final : public Action<Ts...> {
public:
RotaryEncoderSetValueAction(RotaryEncoderSensor *encoder) : encoder_(encoder) {}
TEMPLATABLE_VALUE(int, value)
void play(const Ts &...x) override { this->encoder_->set_value(this->value_.value(x...)); }
protected:
RotaryEncoderSensor *encoder_;
};
} // namespace esphome::rotary_encoder
+2 -19
View File
@@ -15,8 +15,6 @@ from esphome.const import (
STATE_CLASS_MEASUREMENT,
UNIT_STEPS,
)
from esphome.core import ID
from esphome.cpp_generator import MockObj, TemplateArgsType
from esphome.types import ConfigType
rotary_encoder_ns = cg.esphome_ns.namespace("rotary_encoder")
@@ -42,9 +40,6 @@ CONF_PUBLISH_INITIAL_VALUE = "publish_initial_value"
RotaryEncoderSensor = rotary_encoder_ns.class_(
"RotaryEncoderSensor", sensor.Sensor, cg.Component
)
RotaryEncoderSetValueAction = rotary_encoder_ns.class_(
"RotaryEncoderSetValueAction", automation.Action
)
def validate_min_max_value(config: ConfigType) -> ConfigType:
@@ -118,25 +113,13 @@ async def to_code(config: ConfigType) -> None:
await automation.build_callback_automations(var, config, _CALLBACK_AUTOMATIONS)
@automation.register_action(
automation.register_apply_action(
"sensor.rotary_encoder.set_value",
RotaryEncoderSetValueAction,
cv.Schema(
{
cv.Required(CONF_ID): cv.use_id(RotaryEncoderSensor),
cv.Required(CONF_VALUE): cv.templatable(cv.int_),
}
),
synchronous=True,
automation.ApplyField(CONF_VALUE, "set_value", cg.int_),
)
async def sensor_template_publish_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)
template_ = await cg.templatable(config[CONF_VALUE], args, cg.int_)
cg.add(var.set_value(template_))
return var
+1 -1
View File
@@ -18,7 +18,7 @@ sensor:
value: 10
- sensor.rotary_encoder.set_value:
id: rotary_encoder1
value: !lambda "return -1;"
value: !lambda "return -static_cast<int>(x);"
on_clockwise:
- logger.log: Clockwise
on_anticlockwise: