From 532a6ed2391b41dd36d53bed44ac7782bf12c5e5 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 26 Sep 2026 01:30:56 +0100 Subject: [PATCH] [mixer] Use register_apply_action for mixer_speaker.apply_ducking (#19689) --- esphome/components/mixer/speaker/__init__.py | 29 ++++--------------- esphome/components/mixer/speaker/automation.h | 18 ------------ tests/components/mixer/common-actions.yaml | 8 +++++ .../mixer/test-actions.esp32-idf.yaml | 8 +++++ 4 files changed, 21 insertions(+), 42 deletions(-) delete mode 100644 esphome/components/mixer/speaker/automation.h create mode 100644 tests/components/mixer/common-actions.yaml create mode 100644 tests/components/mixer/test-actions.esp32-idf.yaml diff --git a/esphome/components/mixer/speaker/__init__.py b/esphome/components/mixer/speaker/__init__.py index 26619f35a7..44473b12c3 100644 --- a/esphome/components/mixer/speaker/__init__.py +++ b/esphome/components/mixer/speaker/__init__.py @@ -15,9 +15,7 @@ from esphome.const import ( CONF_TIMEOUT, PLATFORM_ESP32, ) -from esphome.core import ID from esphome.core.entity_helpers import inherit_property_from -from esphome.cpp_generator import MockObj, TemplateArgsType import esphome.final_validate as fv from esphome.types import ConfigType @@ -32,10 +30,6 @@ CONF_DECIBEL_REDUCTION = "decibel_reduction" CONF_QUEUE_MODE = "queue_mode" CONF_SOURCE_SPEAKERS = "source_speakers" -DuckingApplyAction = mixer_speaker_ns.class_( - "DuckingApplyAction", automation.Action, cg.Parented.template(SourceSpeaker) -) - SOURCE_SPEAKER_SCHEMA = speaker.SPEAKER_SCHEMA.extend( { @@ -148,9 +142,8 @@ async def to_code(config: ConfigType) -> None: cg.add(var.add_source_speaker(source_speaker)) -@automation.register_action( +automation.register_apply_action( "mixer_speaker.apply_ducking", - DuckingApplyAction, cv.Schema( { cv.GenerateID(): cv.use_id(SourceSpeaker), @@ -162,20 +155,8 @@ async def to_code(config: ConfigType) -> None: ), } ), - synchronous=True, + automation.ApplyCall( + "apply_ducking({}, {})", + ((CONF_DECIBEL_REDUCTION, cg.uint8), (CONF_DURATION, cg.uint32)), + ), ) -async def ducking_set_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]) - decibel_reduction = await cg.templatable( - config[CONF_DECIBEL_REDUCTION], args, cg.uint8 - ) - cg.add(var.set_decibel_reduction(decibel_reduction)) - duration = await cg.templatable(config[CONF_DURATION], args, cg.uint32) - cg.add(var.set_duration(duration)) - return var diff --git a/esphome/components/mixer/speaker/automation.h b/esphome/components/mixer/speaker/automation.h deleted file mode 100644 index ea51b6b889..0000000000 --- a/esphome/components/mixer/speaker/automation.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - -#include "esphome/core/automation.h" -#include "mixer_speaker.h" - -#ifdef USE_ESP32 - -namespace esphome::mixer_speaker { -template class DuckingApplyAction final : public Action, public Parented { - TEMPLATABLE_VALUE(uint8_t, decibel_reduction); - TEMPLATABLE_VALUE(uint32_t, duration); - void play(const Ts &...x) override { - this->parent_->apply_ducking(this->decibel_reduction_.value(x...), this->duration_.value(x...)); - } -}; -} // namespace esphome::mixer_speaker - -#endif diff --git a/tests/components/mixer/common-actions.yaml b/tests/components/mixer/common-actions.yaml new file mode 100644 index 0000000000..f86009e628 --- /dev/null +++ b/tests/components/mixer/common-actions.yaml @@ -0,0 +1,8 @@ +button: + - platform: template + name: Duck media + on_press: + - mixer_speaker.apply_ducking: + id: source_speaker_1_id + decibel_reduction: !lambda return 20; + duration: !lambda return 250; diff --git a/tests/components/mixer/test-actions.esp32-idf.yaml b/tests/components/mixer/test-actions.esp32-idf.yaml new file mode 100644 index 0000000000..7c214bf864 --- /dev/null +++ b/tests/components/mixer/test-actions.esp32-idf.yaml @@ -0,0 +1,8 @@ +substitutions: + dout_pin: GPIO14 + +packages: + spi: !include ../../test_build_components/common/spi/esp32-idf.yaml + i2s_audio: !include ../../test_build_components/common/i2s_audio/esp32-idf.yaml + mixer: !include common.yaml + actions: !include common-actions.yaml