mirror of
https://github.com/esphome/esphome.git
synced 2026-09-28 07:20:24 +00:00
[voice_assistant] Use register_apply_action for start, start_continuous and stop (#19536)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
from esphome import automation
|
||||
from esphome.automation import register_action, register_condition
|
||||
from esphome.automation import register_condition
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import media_player, micro_wake_word, microphone, speaker
|
||||
import esphome.config_validation as cv
|
||||
@@ -62,15 +62,6 @@ MAX_MICROPHONE_SOURCES = 2
|
||||
voice_assistant_ns = cg.esphome_ns.namespace("voice_assistant")
|
||||
VoiceAssistant = voice_assistant_ns.class_("VoiceAssistant", cg.Component)
|
||||
|
||||
StartAction = voice_assistant_ns.class_(
|
||||
"StartAction", automation.Action, cg.Parented.template(VoiceAssistant)
|
||||
)
|
||||
StartContinuousAction = voice_assistant_ns.class_(
|
||||
"StartContinuousAction", automation.Action, cg.Parented.template(VoiceAssistant)
|
||||
)
|
||||
StopAction = voice_assistant_ns.class_(
|
||||
"StopAction", automation.Action, cg.Parented.template(VoiceAssistant)
|
||||
)
|
||||
IsRunningCondition = voice_assistant_ns.class_(
|
||||
"IsRunningCondition", automation.Condition, cg.Parented.template(VoiceAssistant)
|
||||
)
|
||||
@@ -406,51 +397,31 @@ async def to_code(config: ConfigType) -> None:
|
||||
VOICE_ASSISTANT_ACTION_SCHEMA = cv.Schema({cv.GenerateID(): cv.use_id(VoiceAssistant)})
|
||||
|
||||
|
||||
@register_action(
|
||||
automation.register_apply_action(
|
||||
"voice_assistant.start_continuous",
|
||||
StartContinuousAction,
|
||||
VOICE_ASSISTANT_ACTION_SCHEMA,
|
||||
synchronous=True,
|
||||
automation.ApplyCall("request_start(true, true)"),
|
||||
)
|
||||
@register_action(
|
||||
# wake_word defaults to "" so a start without one clears the previous wake word,
|
||||
# as the old action did.
|
||||
automation.register_apply_action(
|
||||
"voice_assistant.start",
|
||||
StartAction,
|
||||
VOICE_ASSISTANT_ACTION_SCHEMA.extend(
|
||||
{
|
||||
cv.Optional(CONF_SILENCE_DETECTION, default=True): cv.boolean,
|
||||
cv.Optional(CONF_WAKE_WORD): cv.templatable(cv.string),
|
||||
cv.Optional(CONF_WAKE_WORD, default=""): cv.templatable(cv.string),
|
||||
}
|
||||
),
|
||||
synchronous=True,
|
||||
automation.ApplyField(CONF_WAKE_WORD, "set_wake_word", cg.std_string),
|
||||
automation.ApplyCall(
|
||||
"request_start(false, {})", ((CONF_SILENCE_DETECTION, cg.bool_),)
|
||||
),
|
||||
)
|
||||
async def voice_assistant_listen_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])
|
||||
if CONF_SILENCE_DETECTION in config:
|
||||
cg.add(var.set_silence_detection(config[CONF_SILENCE_DETECTION]))
|
||||
if wake_word := config.get(CONF_WAKE_WORD):
|
||||
templ = await cg.templatable(wake_word, args, cg.std_string)
|
||||
cg.add(var.set_wake_word(templ))
|
||||
return var
|
||||
|
||||
|
||||
@register_action(
|
||||
"voice_assistant.stop", StopAction, VOICE_ASSISTANT_ACTION_SCHEMA, synchronous=True
|
||||
automation.register_apply_action(
|
||||
"voice_assistant.stop",
|
||||
VOICE_ASSISTANT_ACTION_SCHEMA,
|
||||
automation.ApplyCall("request_stop()"),
|
||||
)
|
||||
async def voice_assistant_stop_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])
|
||||
return var
|
||||
|
||||
|
||||
@register_condition(
|
||||
|
||||
@@ -347,31 +347,6 @@ class VoiceAssistant final : public Component {
|
||||
#endif
|
||||
};
|
||||
|
||||
template<typename... Ts> class StartAction final : public Action<Ts...>, public Parented<VoiceAssistant> {
|
||||
TEMPLATABLE_VALUE(std::string, wake_word);
|
||||
|
||||
public:
|
||||
void play(const Ts &...x) override {
|
||||
this->parent_->set_wake_word(this->wake_word_.value(x...));
|
||||
this->parent_->request_start(false, this->silence_detection_);
|
||||
}
|
||||
|
||||
void set_silence_detection(bool silence_detection) { this->silence_detection_ = silence_detection; }
|
||||
|
||||
protected:
|
||||
bool silence_detection_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class StartContinuousAction final : public Action<Ts...>, public Parented<VoiceAssistant> {
|
||||
public:
|
||||
void play(const Ts &...x) override { this->parent_->request_start(true, true); }
|
||||
};
|
||||
|
||||
template<typename... Ts> class StopAction final : public Action<Ts...>, public Parented<VoiceAssistant> {
|
||||
public:
|
||||
void play(const Ts &...x) override { this->parent_->request_stop(); }
|
||||
};
|
||||
|
||||
template<typename... Ts> class IsRunningCondition final : public Condition<Ts...>, public Parented<VoiceAssistant> {
|
||||
public:
|
||||
bool check(const Ts &...x) override { return this->parent_->is_running() || this->parent_->is_continuous(); }
|
||||
|
||||
@@ -2,6 +2,9 @@ esphome:
|
||||
on_boot:
|
||||
then:
|
||||
- voice_assistant.start
|
||||
- voice_assistant.start:
|
||||
wake_word: okay_nabu
|
||||
silence_detection: false
|
||||
- voice_assistant.start_continuous
|
||||
- voice_assistant.stop
|
||||
|
||||
|
||||
@@ -2,6 +2,9 @@ esphome:
|
||||
on_boot:
|
||||
then:
|
||||
- voice_assistant.start
|
||||
- voice_assistant.start:
|
||||
wake_word: okay_nabu
|
||||
silence_detection: false
|
||||
- voice_assistant.start_continuous
|
||||
- voice_assistant.stop
|
||||
|
||||
|
||||
Reference in New Issue
Block a user