mirror of
https://github.com/esphome/esphome.git
synced 2026-09-28 15:30:21 +00:00
[voice_assistant] Use register_apply_condition for is_running and connected (#19703)
This commit is contained in:
@@ -59,12 +59,6 @@ MAX_MICROPHONE_SOURCES = 2
|
||||
voice_assistant_ns = cg.esphome_ns.namespace("voice_assistant")
|
||||
VoiceAssistant = voice_assistant_ns.class_("VoiceAssistant", cg.Component)
|
||||
|
||||
IsRunningCondition = voice_assistant_ns.class_(
|
||||
"IsRunningCondition", automation.Condition, cg.Parented.template(VoiceAssistant)
|
||||
)
|
||||
ConnectedCondition = voice_assistant_ns.class_(
|
||||
"ConnectedCondition", automation.Condition, cg.Parented.template(VoiceAssistant)
|
||||
)
|
||||
|
||||
Timer = voice_assistant_ns.struct("Timer")
|
||||
|
||||
@@ -421,15 +415,14 @@ automation.register_apply_action(
|
||||
)
|
||||
|
||||
|
||||
automation.register_parented_condition(
|
||||
automation.register_apply_condition(
|
||||
"voice_assistant.is_running",
|
||||
IsRunningCondition,
|
||||
VOICE_ASSISTANT_ACTION_SCHEMA,
|
||||
"is_running_or_continuous()",
|
||||
)
|
||||
|
||||
|
||||
automation.register_parented_condition(
|
||||
automation.register_apply_condition(
|
||||
"voice_assistant.connected",
|
||||
ConnectedCondition,
|
||||
VOICE_ASSISTANT_ACTION_SCHEMA,
|
||||
"get_api_connection() != nullptr",
|
||||
)
|
||||
|
||||
@@ -182,6 +182,8 @@ class VoiceAssistant final : public Component {
|
||||
bool is_running() const { return this->state_ != State::IDLE; }
|
||||
void set_continuous(bool continuous) { this->continuous_ = continuous; }
|
||||
bool is_continuous() const { return this->continuous_; }
|
||||
/// The voice_assistant.is_running condition: a pipeline is running or continuous mode keeps one coming.
|
||||
bool is_running_or_continuous() const { return this->is_running() || this->is_continuous(); }
|
||||
|
||||
void set_use_wake_word(bool use_wake_word) { this->use_wake_word_ = use_wake_word; }
|
||||
|
||||
@@ -347,16 +349,6 @@ class VoiceAssistant final : public Component {
|
||||
#endif
|
||||
};
|
||||
|
||||
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(); }
|
||||
};
|
||||
|
||||
template<typename... Ts> class ConnectedCondition final : public Condition<Ts...>, public Parented<VoiceAssistant> {
|
||||
public:
|
||||
bool check(const Ts &...x) override { return this->parent_->get_api_connection() != nullptr; }
|
||||
};
|
||||
|
||||
extern VoiceAssistant *global_voice_assistant; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
|
||||
} // namespace esphome::voice_assistant
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
button:
|
||||
- platform: template
|
||||
name: Voice assistant state
|
||||
on_press:
|
||||
- if:
|
||||
condition:
|
||||
voice_assistant.is_running:
|
||||
then:
|
||||
- logger.log: Running
|
||||
- if:
|
||||
condition:
|
||||
voice_assistant.connected:
|
||||
then:
|
||||
- logger.log: Connected
|
||||
@@ -0,0 +1,9 @@
|
||||
substitutions:
|
||||
i2s_din_pin: GPIO34
|
||||
i2s_din_pin2: GPIO35
|
||||
i2s_dout_pin: GPIO32
|
||||
|
||||
packages:
|
||||
i2s_audio: !include ../../test_build_components/common/i2s_audio/esp32-idf.yaml
|
||||
voice_assistant: !include common-idf.yaml
|
||||
actions: !include common-actions.yaml
|
||||
Reference in New Issue
Block a user