[router] Use register_apply_action for router.speaker.switch_output (#19695)

This commit is contained in:
J. Nick Koston
2026-09-26 02:16:32 +01:00
committed by GitHub
parent 76de6a672c
commit 0d445fb3bc
2 changed files with 4 additions and 33 deletions
+4 -22
View File
@@ -1,4 +1,4 @@
from esphome import automation, core
from esphome import automation
import esphome.codegen as cg
from esphome.components import audio, speaker
import esphome.config_validation as cv
@@ -9,9 +9,7 @@ from esphome.const import (
CONF_OUTPUT_SPEAKER,
CONF_SAMPLE_RATE,
)
from esphome.core import ID
from esphome.cpp_generator import MockObj
from esphome.types import ConfigType, TemplateArgsType
from esphome.types import ConfigType
CODEOWNERS = ["@kahrendt"]
@@ -20,7 +18,6 @@ CONF_TARGET_SPEAKER = "target_speaker"
router_ns = cg.esphome_ns.namespace("router")
Router = router_ns.class_("Router", cg.Component, speaker.Speaker)
SwitchOutputAction = router_ns.class_("SwitchOutputAction", automation.Action)
SpeakerPtr = speaker.Speaker.operator("ptr")
@@ -93,9 +90,8 @@ async def to_code(config: ConfigType) -> None:
cg.add(var.add_output(spk))
@automation.register_action(
automation.register_apply_action(
"router.speaker.switch_output",
SwitchOutputAction,
cv.Schema(
{
cv.GenerateID(CONF_ID): cv.use_id(Router),
@@ -104,19 +100,5 @@ async def to_code(config: ConfigType) -> None:
),
}
),
synchronous=True,
automation.ApplyField(CONF_TARGET_SPEAKER, "switch_to_output", SpeakerPtr),
)
async def switch_output_to_code(
config: ConfigType,
action_id: ID,
template_arg: cg.TemplateArguments,
args: TemplateArgsType,
) -> MockObj:
parent = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, parent)
target = config[CONF_TARGET_SPEAKER]
if not isinstance(target, core.Lambda):
target = await cg.get_variable(target)
template_ = await cg.templatable(target, args, SpeakerPtr)
cg.add(var.set_target(template_))
return var
@@ -3,7 +3,6 @@
#ifdef USE_ESP32
#include "esphome/components/speaker/speaker.h"
#include "esphome/core/automation.h"
#include "esphome/core/component.h"
#include "esphome/core/helpers.h"
@@ -80,16 +79,6 @@ class Router final : public Component, public speaker::Speaker {
std::atomic<int8_t> active_output_idx_{0};
};
template<typename... Ts> class SwitchOutputAction final : public Action<Ts...> {
public:
explicit SwitchOutputAction(Router *parent) : parent_(parent) {}
TEMPLATABLE_VALUE(speaker::Speaker *, target)
void play(const Ts &...x) override { this->parent_->switch_to_output(this->target_.value(x...)); }
protected:
Router *parent_;
};
} // namespace esphome::router
#endif // USE_ESP32