mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-06-03 19:38:30 +02:00
Continuous voice_assistant and silence detection (#4892)
This commit is contained in:
@@ -1,16 +1,23 @@
|
||||
import esphome.config_validation as cv
|
||||
import esphome.codegen as cg
|
||||
|
||||
from esphome.const import CONF_ID, CONF_MICROPHONE, CONF_SPEAKER
|
||||
from esphome.const import (
|
||||
CONF_ID,
|
||||
CONF_MICROPHONE,
|
||||
CONF_SPEAKER,
|
||||
CONF_MEDIA_PLAYER,
|
||||
)
|
||||
from esphome import automation
|
||||
from esphome.automation import register_action
|
||||
from esphome.components import microphone, speaker
|
||||
from esphome.automation import register_action, register_condition
|
||||
from esphome.components import microphone, speaker, media_player
|
||||
|
||||
AUTO_LOAD = ["socket"]
|
||||
DEPENDENCIES = ["api", "microphone"]
|
||||
|
||||
CODEOWNERS = ["@jesserockz"]
|
||||
|
||||
CONF_SILENCE_DETECTION = "silence_detection"
|
||||
CONF_ON_LISTENING = "on_listening"
|
||||
CONF_ON_START = "on_start"
|
||||
CONF_ON_STT_END = "on_stt_end"
|
||||
CONF_ON_TTS_START = "on_tts_start"
|
||||
@@ -25,16 +32,25 @@ 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)
|
||||
)
|
||||
|
||||
|
||||
CONFIG_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(VoiceAssistant),
|
||||
cv.GenerateID(CONF_MICROPHONE): cv.use_id(microphone.Microphone),
|
||||
cv.Optional(CONF_SPEAKER): cv.use_id(speaker.Speaker),
|
||||
cv.Exclusive(CONF_SPEAKER, "output"): cv.use_id(speaker.Speaker),
|
||||
cv.Exclusive(CONF_MEDIA_PLAYER, "output"): cv.use_id(media_player.MediaPlayer),
|
||||
cv.Optional(CONF_SILENCE_DETECTION, default=True): cv.boolean,
|
||||
cv.Optional(CONF_ON_LISTENING): automation.validate_automation(single=True),
|
||||
cv.Optional(CONF_ON_START): automation.validate_automation(single=True),
|
||||
cv.Optional(CONF_ON_STT_END): automation.validate_automation(single=True),
|
||||
cv.Optional(CONF_ON_TTS_START): automation.validate_automation(single=True),
|
||||
@@ -56,6 +72,17 @@ async def to_code(config):
|
||||
spkr = await cg.get_variable(config[CONF_SPEAKER])
|
||||
cg.add(var.set_speaker(spkr))
|
||||
|
||||
if CONF_MEDIA_PLAYER in config:
|
||||
mp = await cg.get_variable(config[CONF_MEDIA_PLAYER])
|
||||
cg.add(var.set_media_player(mp))
|
||||
|
||||
cg.add(var.set_silence_detection(config[CONF_SILENCE_DETECTION]))
|
||||
|
||||
if CONF_ON_LISTENING in config:
|
||||
await automation.build_automation(
|
||||
var.get_listening_trigger(), [], config[CONF_ON_LISTENING]
|
||||
)
|
||||
|
||||
if CONF_ON_START in config:
|
||||
await automation.build_automation(
|
||||
var.get_start_trigger(), [], config[CONF_ON_START]
|
||||
@@ -96,6 +123,11 @@ async def to_code(config):
|
||||
VOICE_ASSISTANT_ACTION_SCHEMA = cv.Schema({cv.GenerateID(): cv.use_id(VoiceAssistant)})
|
||||
|
||||
|
||||
@register_action(
|
||||
"voice_assistant.start_continuous",
|
||||
StartContinuousAction,
|
||||
VOICE_ASSISTANT_ACTION_SCHEMA,
|
||||
)
|
||||
@register_action("voice_assistant.start", StartAction, VOICE_ASSISTANT_ACTION_SCHEMA)
|
||||
async def voice_assistant_listen_to_code(config, action_id, template_arg, args):
|
||||
var = cg.new_Pvariable(action_id, template_arg)
|
||||
@@ -108,3 +140,12 @@ async def voice_assistant_stop_to_code(config, action_id, template_arg, args):
|
||||
var = cg.new_Pvariable(action_id, template_arg)
|
||||
await cg.register_parented(var, config[CONF_ID])
|
||||
return var
|
||||
|
||||
|
||||
@register_condition(
|
||||
"voice_assistant.is_running", IsRunningCondition, VOICE_ASSISTANT_ACTION_SCHEMA
|
||||
)
|
||||
async def voice_assistant_is_running_to_code(config, condition_id, template_arg, args):
|
||||
var = cg.new_Pvariable(condition_id, template_arg)
|
||||
await cg.register_parented(var, config[CONF_ID])
|
||||
return var
|
||||
|
||||
Reference in New Issue
Block a user