mirror of
https://github.com/esphome/esphome.git
synced 2026-09-28 15:30:21 +00:00
[mixer] Use register_apply_action for mixer_speaker.apply_ducking (#19689)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/automation.h"
|
||||
#include "mixer_speaker.h"
|
||||
|
||||
#ifdef USE_ESP32
|
||||
|
||||
namespace esphome::mixer_speaker {
|
||||
template<typename... Ts> class DuckingApplyAction final : public Action<Ts...>, public Parented<SourceSpeaker> {
|
||||
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
|
||||
@@ -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;
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user