mirror of
https://github.com/esphome/esphome.git
synced 2026-09-28 07:20:24 +00:00
[ledc] Use register_apply_action for the set_frequency action (#19515)
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"
|
||||
#include <cstdint>
|
||||
|
||||
@@ -43,20 +42,6 @@ class LEDCOutput final : public output::FloatOutput, public Component {
|
||||
bool initialized_ = false;
|
||||
};
|
||||
|
||||
template<typename... Ts> class SetFrequencyAction final : public Action<Ts...> {
|
||||
public:
|
||||
SetFrequencyAction(LEDCOutput *parent) : parent_(parent) {}
|
||||
TEMPLATABLE_VALUE(float, frequency);
|
||||
|
||||
void play(const Ts &...x) {
|
||||
float freq = this->frequency_.value(x...);
|
||||
this->parent_->update_frequency(freq);
|
||||
}
|
||||
|
||||
protected:
|
||||
LEDCOutput *parent_;
|
||||
};
|
||||
|
||||
} // namespace esphome::ledc
|
||||
|
||||
#endif
|
||||
|
||||
@@ -12,8 +12,6 @@ from esphome.const import (
|
||||
CONF_PHASE_ANGLE,
|
||||
CONF_PIN,
|
||||
)
|
||||
from esphome.core import ID
|
||||
from esphome.cpp_generator import MockObj, TemplateArgsType
|
||||
from esphome.types import ConfigType
|
||||
|
||||
DEPENDENCIES = ["esp32"]
|
||||
@@ -45,7 +43,6 @@ def validate_frequency(value: Any) -> float:
|
||||
|
||||
ledc_ns = cg.esphome_ns.namespace("ledc")
|
||||
LEDCOutput = ledc_ns.class_("LEDCOutput", output.FloatOutput, cg.Component)
|
||||
SetFrequencyAction = ledc_ns.class_("SetFrequencyAction", automation.Action)
|
||||
|
||||
CONFIG_SCHEMA = output.FLOAT_OUTPUT_SCHEMA.extend(
|
||||
{
|
||||
@@ -77,25 +74,13 @@ async def to_code(config: ConfigType) -> None:
|
||||
cg.add(var.set_phase_angle(config[CONF_PHASE_ANGLE]))
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
automation.register_apply_action(
|
||||
"output.ledc.set_frequency",
|
||||
SetFrequencyAction,
|
||||
cv.Schema(
|
||||
{
|
||||
cv.Required(CONF_ID): cv.use_id(LEDCOutput),
|
||||
cv.Required(CONF_FREQUENCY): cv.templatable(validate_frequency),
|
||||
}
|
||||
),
|
||||
synchronous=True,
|
||||
automation.ApplyField(CONF_FREQUENCY, "update_frequency", cg.float_),
|
||||
)
|
||||
async def ledc_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
|
||||
|
||||
@@ -4,6 +4,9 @@ esphome:
|
||||
- output.ledc.set_frequency:
|
||||
id: test_ledc
|
||||
frequency: 100Hz
|
||||
- output.ledc.set_frequency:
|
||||
id: test_ledc
|
||||
frequency: !lambda return 200.0f;
|
||||
|
||||
output:
|
||||
- platform: ledc
|
||||
|
||||
Reference in New Issue
Block a user