[esp8266_pwm] Use register_apply_action for the set_frequency action (#19516)

This commit is contained in:
J. Nick Koston
2026-09-24 10:30:19 +12:00
committed by GitHub
parent 438307f548
commit e3951f2ab4
3 changed files with 12 additions and 31 deletions
@@ -4,7 +4,6 @@
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
#include "esphome/core/automation.h"
#include "esphome/components/output/float_output.h"
namespace esphome::esp8266_pwm {
@@ -37,19 +36,6 @@ class ESP8266PWM final : public output::FloatOutput, public Component {
float last_output_{0.0};
};
template<typename... Ts> class SetFrequencyAction final : public Action<Ts...> {
public:
SetFrequencyAction(ESP8266PWM *parent) : parent_(parent) {}
TEMPLATABLE_VALUE(float, frequency);
void play(const Ts &...x) {
float freq = this->frequency_.value(x...);
this->parent_->update_frequency(freq);
}
ESP8266PWM *parent_;
};
} // namespace esphome::esp8266_pwm
#endif
+2 -17
View File
@@ -4,8 +4,6 @@ from esphome.components import output
from esphome.components.esp8266.const import require_waveform
import esphome.config_validation as cv
from esphome.const import CONF_FREQUENCY, CONF_ID, CONF_NUMBER, CONF_PIN
from esphome.core import ID
from esphome.cpp_generator import MockObj, TemplateArgsType
from esphome.types import ConfigType
DEPENDENCIES = ["esp8266"]
@@ -19,7 +17,6 @@ def valid_pwm_pin(value: ConfigType) -> ConfigType:
esp8266_pwm_ns = cg.esphome_ns.namespace("esp8266_pwm")
ESP8266PWM = esp8266_pwm_ns.class_("ESP8266PWM", output.FloatOutput, cg.Component)
SetFrequencyAction = esp8266_pwm_ns.class_("SetFrequencyAction", automation.Action)
validate_frequency = cv.All(cv.frequency, cv.float_range(min=1.0e-6))
# Schema default that also matches the C++ initializer in esp8266_pwm.h; codegen
@@ -57,25 +54,13 @@ async def to_code(config: ConfigType) -> None:
cg.add(var.set_frequency(frequency))
@automation.register_action(
automation.register_apply_action(
"output.esp8266_pwm.set_frequency",
SetFrequencyAction,
cv.Schema(
{
cv.Required(CONF_ID): cv.use_id(ESP8266PWM),
cv.Required(CONF_FREQUENCY): cv.templatable(validate_frequency),
}
),
synchronous=True,
automation.ApplyField(CONF_FREQUENCY, "update_frequency", 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)
template_ = await cg.templatable(config[CONF_FREQUENCY], args, cg.float_)
cg.add(var.set_frequency(template_))
return var
+10
View File
@@ -1,3 +1,13 @@
esphome:
on_boot:
then:
- output.esp8266_pwm.set_frequency:
id: out
frequency: 100Hz
- output.esp8266_pwm.set_frequency:
id: out2
frequency: !lambda return 200.0f;
output:
- platform: esp8266_pwm
id: out