mirror of
https://github.com/esphome/esphome.git
synced 2026-09-28 15:30:21 +00:00
[servo] Use register_apply_action for servo.write and servo.detach (#19531)
This commit is contained in:
@@ -13,14 +13,10 @@ from esphome.const import (
|
||||
CONF_RESTORE,
|
||||
CONF_TRANSITION_LENGTH,
|
||||
)
|
||||
from esphome.core import ID
|
||||
from esphome.cpp_generator import MockObj, TemplateArgsType
|
||||
from esphome.types import ConfigType
|
||||
|
||||
servo_ns = cg.esphome_ns.namespace("servo")
|
||||
Servo = servo_ns.class_("Servo", cg.Component)
|
||||
ServoWriteAction = servo_ns.class_("ServoWriteAction", automation.Action)
|
||||
ServoDetachAction = servo_ns.class_("ServoDetachAction", automation.Action)
|
||||
|
||||
CONF_AUTO_DETACH_TIME = "auto_detach_time"
|
||||
MULTI_CONF = True
|
||||
@@ -56,45 +52,23 @@ async def to_code(config: ConfigType) -> None:
|
||||
cg.add(var.set_transition_length(config[CONF_TRANSITION_LENGTH]))
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
automation.register_apply_action(
|
||||
"servo.write",
|
||||
ServoWriteAction,
|
||||
cv.Schema(
|
||||
{
|
||||
cv.Required(CONF_ID): cv.use_id(Servo),
|
||||
cv.Required(CONF_LEVEL): cv.templatable(cv.possibly_negative_percentage),
|
||||
}
|
||||
),
|
||||
synchronous=True,
|
||||
automation.ApplyField(CONF_LEVEL, "write", cg.float_),
|
||||
)
|
||||
async def servo_write_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_LEVEL], args, cg.float_)
|
||||
cg.add(var.set_value(template_))
|
||||
return var
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
automation.register_apply_action(
|
||||
"servo.detach",
|
||||
ServoDetachAction,
|
||||
maybe_simple_id(
|
||||
{
|
||||
cv.Required(CONF_ID): cv.use_id(Servo),
|
||||
}
|
||||
),
|
||||
synchronous=True,
|
||||
automation.ApplyCall("detach()"),
|
||||
)
|
||||
async def servo_detach_to_code(
|
||||
config: ConfigType,
|
||||
action_id: ID,
|
||||
template_arg: cg.TemplateArguments,
|
||||
args: TemplateArgsType,
|
||||
) -> MockObj:
|
||||
paren = await cg.get_variable(config[CONF_ID])
|
||||
return cg.new_Pvariable(action_id, template_arg, paren)
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/preferences.h"
|
||||
@@ -51,25 +50,4 @@ class Servo final : public Component {
|
||||
};
|
||||
};
|
||||
|
||||
template<typename... Ts> class ServoWriteAction final : public Action<Ts...> {
|
||||
public:
|
||||
ServoWriteAction(Servo *servo) : servo_(servo) {}
|
||||
TEMPLATABLE_VALUE(float, value)
|
||||
|
||||
void play(const Ts &...x) override { this->servo_->write(this->value_.value(x...)); }
|
||||
|
||||
protected:
|
||||
Servo *servo_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class ServoDetachAction final : public Action<Ts...> {
|
||||
public:
|
||||
ServoDetachAction(Servo *servo) : servo_(servo) {}
|
||||
|
||||
void play(const Ts &...x) override { this->servo_->detach(); }
|
||||
|
||||
protected:
|
||||
Servo *servo_;
|
||||
};
|
||||
|
||||
} // namespace esphome::servo
|
||||
|
||||
@@ -4,6 +4,9 @@ esphome:
|
||||
- servo.write:
|
||||
id: test_servo
|
||||
level: -100.0%
|
||||
- servo.write:
|
||||
id: test_servo
|
||||
level: !lambda return 0.5;
|
||||
- servo.detach: test_servo
|
||||
|
||||
output:
|
||||
|
||||
Reference in New Issue
Block a user