mirror of
https://github.com/esphome/esphome.git
synced 2026-09-28 07:20:24 +00:00
[libretiny_pwm] Use register_apply_action for the set_frequency action (#19518)
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/components/output/float_output.h"
|
||||
|
||||
#ifdef USE_LIBRETINY
|
||||
@@ -34,20 +33,6 @@ class LibreTinyPWM final : public output::FloatOutput, public Component {
|
||||
bool initialized_ = false;
|
||||
};
|
||||
|
||||
template<typename... Ts> class SetFrequencyAction final : public Action<Ts...> {
|
||||
public:
|
||||
SetFrequencyAction(LibreTinyPWM *parent) : parent_(parent) {}
|
||||
TEMPLATABLE_VALUE(float, frequency);
|
||||
|
||||
void play(const Ts &...x) {
|
||||
float freq = this->frequency_.value(x...);
|
||||
this->parent_->update_frequency(freq);
|
||||
}
|
||||
|
||||
protected:
|
||||
LibreTinyPWM *parent_;
|
||||
};
|
||||
|
||||
} // namespace esphome::libretiny_pwm
|
||||
|
||||
#endif
|
||||
|
||||
@@ -3,15 +3,12 @@ import esphome.codegen as cg
|
||||
from esphome.components import output
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_FREQUENCY, CONF_ID, CONF_PIN
|
||||
from esphome.core import ID
|
||||
from esphome.cpp_generator import MockObj, TemplateArgsType
|
||||
from esphome.types import ConfigType
|
||||
|
||||
DEPENDENCIES = ["libretiny"]
|
||||
|
||||
libretinypwm_ns = cg.esphome_ns.namespace("libretiny_pwm")
|
||||
LibreTinyPWM = libretinypwm_ns.class_("LibreTinyPWM", output.FloatOutput, cg.Component)
|
||||
SetFrequencyAction = libretinypwm_ns.class_("SetFrequencyAction", automation.Action)
|
||||
|
||||
CONFIG_SCHEMA = output.FLOAT_OUTPUT_SCHEMA.extend(
|
||||
{
|
||||
@@ -32,25 +29,13 @@ async def to_code(config: ConfigType) -> None:
|
||||
cg.add(var.set_frequency(config[CONF_FREQUENCY]))
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
automation.register_apply_action(
|
||||
"output.libretiny_pwm.set_frequency",
|
||||
SetFrequencyAction,
|
||||
cv.Schema(
|
||||
{
|
||||
cv.Required(CONF_ID): cv.use_id(LibreTinyPWM),
|
||||
cv.Required(CONF_FREQUENCY): cv.templatable(cv.int_),
|
||||
}
|
||||
),
|
||||
synchronous=True,
|
||||
automation.ApplyField(CONF_FREQUENCY, "update_frequency", cg.float_),
|
||||
)
|
||||
async def libretiny_pwm_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)
|
||||
template_ = await cg.templatable(config[CONF_FREQUENCY], args, cg.float_)
|
||||
cg.add(var.set_frequency(template_))
|
||||
return var
|
||||
|
||||
Reference in New Issue
Block a user