[rtttl] Use register_apply_action for rtttl.play and rtttl.stop (#19482)

This commit is contained in:
J. Nick Koston
2026-09-23 12:22:05 +01:00
committed by GitHub
parent 3b570d84ae
commit 1a8a661b56
3 changed files with 19 additions and 57 deletions
+5 -41
View File
@@ -6,8 +6,6 @@ from esphome.components.output import FloatOutput
from esphome.components.speaker import Speaker
import esphome.config_validation as cv
from esphome.const import CONF_GAIN, CONF_ID, CONF_OUTPUT, CONF_PLATFORM, CONF_SPEAKER
from esphome.core import ID
from esphome.cpp_generator import MockObj, TemplateArgsType
import esphome.final_validate as fv
from esphome.types import ConfigType
@@ -20,8 +18,6 @@ CONF_ON_FINISHED_PLAYBACK = "on_finished_playback"
rtttl_ns = cg.esphome_ns.namespace("rtttl")
Rtttl = rtttl_ns.class_("Rtttl", cg.Component)
PlayAction = rtttl_ns.class_("PlayAction", automation.Action)
StopAction = rtttl_ns.class_("StopAction", automation.Action)
IsPlayingCondition = rtttl_ns.class_("IsPlayingCondition", automation.Condition)
MULTI_CONF = True
@@ -101,9 +97,8 @@ async def to_code(config: ConfigType) -> None:
await automation.build_callback_automations(var, config, _CALLBACK_AUTOMATIONS)
@automation.register_action(
automation.register_apply_action(
"rtttl.play",
PlayAction,
cv.maybe_simple_value(
{
cv.GenerateID(CONF_ID): cv.use_id(Rtttl),
@@ -111,43 +106,21 @@ async def to_code(config: ConfigType) -> None:
},
key=CONF_RTTTL,
),
synchronous=True,
automation.ApplyField(CONF_RTTTL, "play", cg.std_string),
)
async def rtttl_play_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_RTTTL], args, cg.std_string)
cg.add(var.set_value(template_))
return var
@automation.register_action(
automation.register_apply_action(
"rtttl.stop",
StopAction,
cv.Schema(
{
cv.GenerateID(): cv.use_id(Rtttl),
}
),
synchronous=True,
automation.ApplyCall("stop()"),
)
async def rtttl_stop_to_code(
config: ConfigType,
action_id: ID,
template_arg: cg.TemplateArguments,
args: TemplateArgsType,
) -> MockObj:
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
return var
@automation.register_condition(
automation.register_parented_condition(
"rtttl.is_playing",
IsPlayingCondition,
cv.Schema(
@@ -156,12 +129,3 @@ async def rtttl_stop_to_code(
}
),
)
async def rtttl_is_playing_to_code(
config: ConfigType,
condition_id: ID,
template_arg: cg.TemplateArguments,
args: TemplateArgsType,
) -> MockObj:
var = cg.new_Pvariable(condition_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
return var
-16
View File
@@ -116,22 +116,6 @@ class Rtttl final : public Component {
#endif
};
template<typename... Ts> class PlayAction final : public Action<Ts...> {
public:
PlayAction(Rtttl *rtttl) : rtttl_(rtttl) {}
TEMPLATABLE_VALUE(std::string, value)
void play(const Ts &...x) override { this->rtttl_->play(this->value_.value(x...)); }
protected:
Rtttl *rtttl_;
};
template<typename... Ts> class StopAction final : public Action<Ts...>, public Parented<Rtttl> {
public:
void play(const Ts &...x) override { this->parent_->stop(); }
};
template<typename... Ts> class IsPlayingCondition final : public Condition<Ts...>, public Parented<Rtttl> {
public:
bool check(const Ts &...x) override { return this->parent_->is_playing(); }
+14
View File
@@ -19,6 +19,19 @@ esphome:
- rtttl.play: 'test_only_o:o=5:c'
- rtttl.play: 'test_only_b:b=100:c'
- rtttl.play: 'test_empty::c'
# Long form with an explicit id, a lambda song, and the is_playing condition
- rtttl.play:
id: rtttl_player
rtttl: 'test_long_form:d=8,o=5,b=100:c'
- rtttl.play:
rtttl: !lambda return std::string("test_lambda:d=8,o=5,b=100:c");
- if:
condition:
rtttl.is_playing:
id: rtttl_player
then:
- rtttl.stop:
id: rtttl_player
output:
- platform: ${output_platform}
@@ -28,6 +41,7 @@ output:
max_power: 0.5
rtttl:
id: rtttl_player
output: rtttl_output
on_finished_playback:
- then: