[rtttl] Migrate FinishedPlaybackTrigger to callback automation (#15202)

This commit is contained in:
J. Nick Koston
2026-03-27 08:21:16 -10:00
committed by GitHub
parent a2d452684a
commit 83b3187126
2 changed files with 5 additions and 27 deletions
+5 -20
View File
@@ -5,14 +5,7 @@ import esphome.codegen as cg
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,
CONF_TRIGGER_ID,
)
from esphome.const import CONF_GAIN, CONF_ID, CONF_OUTPUT, CONF_PLATFORM, CONF_SPEAKER
import esphome.final_validate as fv
_LOGGER = logging.getLogger(__name__)
@@ -26,9 +19,6 @@ 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)
FinishedPlaybackTrigger = rtttl_ns.class_(
"FinishedPlaybackTrigger", automation.Trigger.template()
)
IsPlayingCondition = rtttl_ns.class_("IsPlayingCondition", automation.Condition)
MULTI_CONF = True
@@ -40,13 +30,7 @@ CONFIG_SCHEMA = cv.All(
cv.Optional(CONF_OUTPUT): cv.use_id(FloatOutput),
cv.Optional(CONF_SPEAKER): cv.use_id(Speaker),
cv.Optional(CONF_GAIN, default="0.6"): cv.percentage,
cv.Optional(CONF_ON_FINISHED_PLAYBACK): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(
FinishedPlaybackTrigger
),
}
),
cv.Optional(CONF_ON_FINISHED_PLAYBACK): automation.validate_automation({}),
}
).extend(cv.COMPONENT_SCHEMA),
cv.has_exactly_one_key(CONF_OUTPUT, CONF_SPEAKER),
@@ -103,8 +87,9 @@ async def to_code(config):
cg.add(var.set_gain(config[CONF_GAIN]))
for conf in config.get(CONF_ON_FINISHED_PLAYBACK, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [], conf)
await automation.build_callback_automation(
var, "add_on_finished_playback_callback", [], conf
)
@automation.register_action(
-7
View File
@@ -131,11 +131,4 @@ template<typename... Ts> class IsPlayingCondition : public Condition<Ts...>, pub
bool check(const Ts &...x) override { return this->parent_->is_playing(); }
};
class FinishedPlaybackTrigger : public Trigger<> {
public:
explicit FinishedPlaybackTrigger(Rtttl *parent) {
parent->add_on_finished_playback_callback([this]() { this->trigger(); });
}
};
} // namespace esphome::rtttl