[microphone] Use the apply helpers for the capture, mute and condition automations (#19619)

This commit is contained in:
J. Nick Koston
2026-09-25 11:04:14 -04:00
committed by GitHub
parent 45bcff2c2c
commit db566b25d0
4 changed files with 37 additions and 83 deletions
+17 -58
View File
@@ -14,9 +14,9 @@ from esphome.const import (
CONF_ON_DATA,
CONF_TRIGGER_ID,
)
from esphome.core import CORE, ID
from esphome.core import CORE
from esphome.coroutine import CoroPriority, coroutine_with_priority
from esphome.cpp_generator import MockObj, TemplateArgsType
from esphome.cpp_generator import MockObj
from esphome.types import ConfigType
AUTO_LOAD = ["audio"]
@@ -29,30 +29,11 @@ microphone_ns = cg.esphome_ns.namespace("microphone")
Microphone = microphone_ns.class_("Microphone")
MicrophoneSource = microphone_ns.class_("MicrophoneSource")
CaptureAction = microphone_ns.class_(
"CaptureAction", automation.Action, cg.Parented.template(Microphone)
)
StopCaptureAction = microphone_ns.class_(
"StopCaptureAction", automation.Action, cg.Parented.template(Microphone)
)
MuteAction = microphone_ns.class_(
"MuteAction", automation.Action, cg.Parented.template(Microphone)
)
UnmuteAction = microphone_ns.class_(
"UnmuteAction", automation.Action, cg.Parented.template(Microphone)
)
DataTrigger = microphone_ns.class_(
"DataTrigger",
automation.Trigger.template(cg.std_vector.template(cg.uint8).operator("ref")),
)
IsCapturingCondition = microphone_ns.class_(
"IsCapturingCondition", automation.Condition
)
IsMutedCondition = microphone_ns.class_("IsMutedCondition", automation.Condition)
async def setup_microphone_core_(var: MockObj, config: ConfigType) -> None:
for conf in config.get(CONF_ON_DATA, []):
@@ -189,44 +170,22 @@ async def microphone_source_to_code(
return mic_source
async def microphone_action(
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
for _name, _call in (
("microphone.capture", "start()"),
("microphone.stop_capture", "stop()"),
("microphone.mute", "set_mute_state(true)"),
("microphone.unmute", "set_mute_state(false)"),
):
automation.register_apply_action(
_name, MICROPHONE_ACTION_SCHEMA, automation.ApplyCall(_call)
)
automation.register_action(
"microphone.capture",
CaptureAction,
MICROPHONE_ACTION_SCHEMA,
synchronous=True,
)(microphone_action)
automation.register_action(
"microphone.stop_capture",
StopCaptureAction,
MICROPHONE_ACTION_SCHEMA,
synchronous=True,
)(microphone_action)
automation.register_action(
"microphone.mute", MuteAction, MICROPHONE_ACTION_SCHEMA, synchronous=True
)(microphone_action)
automation.register_action(
"microphone.unmute", UnmuteAction, MICROPHONE_ACTION_SCHEMA, synchronous=True
)(microphone_action)
automation.register_condition(
"microphone.is_capturing", IsCapturingCondition, MICROPHONE_ACTION_SCHEMA
)(microphone_action)
automation.register_condition(
"microphone.is_muted", IsMutedCondition, MICROPHONE_ACTION_SCHEMA
)(microphone_action)
automation.register_apply_condition(
"microphone.is_capturing", MICROPHONE_ACTION_SCHEMA, "is_running()"
)
automation.register_apply_condition(
"microphone.is_muted", MICROPHONE_ACTION_SCHEMA, "get_mute_state()"
)
@coroutine_with_priority(CoroPriority.CORE)
@@ -7,21 +7,6 @@
namespace esphome::microphone {
template<typename... Ts> class CaptureAction final : public Action<Ts...>, public Parented<Microphone> {
void play(const Ts &...x) override { this->parent_->start(); }
};
template<typename... Ts> class StopCaptureAction final : public Action<Ts...>, public Parented<Microphone> {
void play(const Ts &...x) override { this->parent_->stop(); }
};
template<typename... Ts> class MuteAction final : public Action<Ts...>, public Parented<Microphone> {
void play(const Ts &...x) override { this->parent_->set_mute_state(true); }
};
template<typename... Ts> class UnmuteAction final : public Action<Ts...>, public Parented<Microphone> {
void play(const Ts &...x) override { this->parent_->set_mute_state(false); }
};
class DataTrigger final : public Trigger<const std::vector<uint8_t> &> {
public:
explicit DataTrigger(Microphone *mic) {
@@ -29,14 +14,4 @@ class DataTrigger final : public Trigger<const std::vector<uint8_t> &> {
}
};
template<typename... Ts> class IsCapturingCondition final : public Condition<Ts...>, public Parented<Microphone> {
public:
bool check(const Ts &...x) override { return this->parent_->is_running(); }
};
template<typename... Ts> class IsMutedCondition final : public Condition<Ts...>, public Parented<Microphone> {
public:
bool check(const Ts &...x) override { return this->parent_->get_mute_state(); }
};
} // namespace esphome::microphone
@@ -0,0 +1,12 @@
# capture, stop_capture and is_capturing on the external microphone.
button:
- platform: template
name: Microphone Capture
on_press:
- if:
condition:
microphone.is_capturing: mic_id_external
then:
- microphone.stop_capture: mic_id_external
else:
- microphone.capture: mic_id_external
@@ -0,0 +1,8 @@
substitutions:
i2s_din_pin1: GPIO33
i2s_din_pin2: GPIO34
packages:
i2s_audio: !include ../../test_build_components/common/i2s_audio/esp32-idf.yaml
microphone: !include common-pdm.yaml
actions: !include common-actions.yaml