2023-04-12 01:45:10 +02:00
|
|
|
#pragma once
|
|
|
|
|
2023-05-10 06:46:32 +02:00
|
|
|
#include "esphome/core/defines.h"
|
|
|
|
|
|
|
|
#ifdef USE_VOICE_ASSISTANT
|
|
|
|
|
2023-04-12 01:45:10 +02:00
|
|
|
#include "esphome/core/automation.h"
|
|
|
|
#include "esphome/core/component.h"
|
|
|
|
#include "esphome/core/helpers.h"
|
|
|
|
|
|
|
|
#include "esphome/components/api/api_pb2.h"
|
|
|
|
#include "esphome/components/api/api_server.h"
|
|
|
|
#include "esphome/components/microphone/microphone.h"
|
2023-05-08 00:36:17 +02:00
|
|
|
#ifdef USE_SPEAKER
|
|
|
|
#include "esphome/components/speaker/speaker.h"
|
|
|
|
#endif
|
2023-05-31 06:30:53 +02:00
|
|
|
#ifdef USE_MEDIA_PLAYER
|
|
|
|
#include "esphome/components/media_player/media_player.h"
|
|
|
|
#endif
|
2023-04-12 01:45:10 +02:00
|
|
|
#include "esphome/components/socket/socket.h"
|
|
|
|
|
|
|
|
namespace esphome {
|
|
|
|
namespace voice_assistant {
|
|
|
|
|
2023-05-08 00:36:17 +02:00
|
|
|
// Version 1: Initial version
|
|
|
|
// Version 2: Adds raw speaker support
|
2023-07-19 01:38:47 +02:00
|
|
|
// Version 3: Unused/skip
|
2023-05-08 00:36:17 +02:00
|
|
|
static const uint32_t INITIAL_VERSION = 1;
|
|
|
|
static const uint32_t SPEAKER_SUPPORT = 2;
|
|
|
|
|
2023-04-12 01:45:10 +02:00
|
|
|
class VoiceAssistant : public Component {
|
|
|
|
public:
|
|
|
|
void setup() override;
|
2023-05-08 00:36:17 +02:00
|
|
|
void loop() override;
|
2023-04-12 01:45:10 +02:00
|
|
|
float get_setup_priority() const override;
|
|
|
|
void start(struct sockaddr_storage *addr, uint16_t port);
|
|
|
|
|
|
|
|
void set_microphone(microphone::Microphone *mic) { this->mic_ = mic; }
|
2023-05-08 00:36:17 +02:00
|
|
|
#ifdef USE_SPEAKER
|
|
|
|
void set_speaker(speaker::Speaker *speaker) { this->speaker_ = speaker; }
|
|
|
|
#endif
|
2023-05-31 06:30:53 +02:00
|
|
|
#ifdef USE_MEDIA_PLAYER
|
|
|
|
void set_media_player(media_player::MediaPlayer *media_player) { this->media_player_ = media_player; }
|
|
|
|
#endif
|
2023-05-08 00:36:17 +02:00
|
|
|
|
|
|
|
uint32_t get_version() const {
|
|
|
|
#ifdef USE_SPEAKER
|
2023-05-31 06:30:53 +02:00
|
|
|
if (this->speaker_ != nullptr) {
|
2023-05-08 00:36:17 +02:00
|
|
|
return SPEAKER_SUPPORT;
|
2023-05-31 06:30:53 +02:00
|
|
|
}
|
2023-05-08 00:36:17 +02:00
|
|
|
#endif
|
|
|
|
return INITIAL_VERSION;
|
|
|
|
}
|
2023-04-12 01:45:10 +02:00
|
|
|
|
2023-05-31 06:30:53 +02:00
|
|
|
void request_start(bool continuous = false);
|
2023-04-12 01:45:10 +02:00
|
|
|
void signal_stop();
|
|
|
|
|
|
|
|
void on_event(const api::VoiceAssistantEventResponse &msg);
|
|
|
|
|
2023-05-31 06:30:53 +02:00
|
|
|
bool is_running() const { return this->running_; }
|
|
|
|
void set_continuous(bool continuous) { this->continuous_ = continuous; }
|
|
|
|
bool is_continuous() const { return this->continuous_; }
|
|
|
|
|
|
|
|
void set_silence_detection(bool silence_detection) { this->silence_detection_ = silence_detection; }
|
|
|
|
|
|
|
|
Trigger<> *get_listening_trigger() const { return this->listening_trigger_; }
|
2023-04-17 04:57:28 +02:00
|
|
|
Trigger<> *get_start_trigger() const { return this->start_trigger_; }
|
|
|
|
Trigger<std::string> *get_stt_end_trigger() const { return this->stt_end_trigger_; }
|
|
|
|
Trigger<std::string> *get_tts_start_trigger() const { return this->tts_start_trigger_; }
|
|
|
|
Trigger<std::string> *get_tts_end_trigger() const { return this->tts_end_trigger_; }
|
|
|
|
Trigger<> *get_end_trigger() const { return this->end_trigger_; }
|
|
|
|
Trigger<std::string, std::string> *get_error_trigger() const { return this->error_trigger_; }
|
|
|
|
|
2023-04-12 01:45:10 +02:00
|
|
|
protected:
|
|
|
|
std::unique_ptr<socket::Socket> socket_ = nullptr;
|
|
|
|
struct sockaddr_storage dest_addr_;
|
|
|
|
|
2023-05-31 06:30:53 +02:00
|
|
|
Trigger<> *listening_trigger_ = new Trigger<>();
|
2023-04-17 04:57:28 +02:00
|
|
|
Trigger<> *start_trigger_ = new Trigger<>();
|
|
|
|
Trigger<std::string> *stt_end_trigger_ = new Trigger<std::string>();
|
|
|
|
Trigger<std::string> *tts_start_trigger_ = new Trigger<std::string>();
|
|
|
|
Trigger<std::string> *tts_end_trigger_ = new Trigger<std::string>();
|
|
|
|
Trigger<> *end_trigger_ = new Trigger<>();
|
|
|
|
Trigger<std::string, std::string> *error_trigger_ = new Trigger<std::string, std::string>();
|
|
|
|
|
2023-04-12 01:45:10 +02:00
|
|
|
microphone::Microphone *mic_{nullptr};
|
2023-05-08 00:36:17 +02:00
|
|
|
#ifdef USE_SPEAKER
|
|
|
|
speaker::Speaker *speaker_{nullptr};
|
|
|
|
#endif
|
2023-05-31 06:30:53 +02:00
|
|
|
#ifdef USE_MEDIA_PLAYER
|
|
|
|
media_player::MediaPlayer *media_player_{nullptr};
|
|
|
|
bool playing_tts_{false};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
std::string conversation_id_{""};
|
2023-04-12 01:45:10 +02:00
|
|
|
|
|
|
|
bool running_{false};
|
2023-05-31 06:30:53 +02:00
|
|
|
bool continuous_{false};
|
|
|
|
bool silence_detection_;
|
2023-04-12 01:45:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename... Ts> class StartAction : public Action<Ts...>, public Parented<VoiceAssistant> {
|
|
|
|
public:
|
|
|
|
void play(Ts... x) override { this->parent_->request_start(); }
|
|
|
|
};
|
|
|
|
|
2023-05-31 06:30:53 +02:00
|
|
|
template<typename... Ts> class StartContinuousAction : public Action<Ts...>, public Parented<VoiceAssistant> {
|
|
|
|
public:
|
|
|
|
void play(Ts... x) override { this->parent_->request_start(true); }
|
|
|
|
};
|
|
|
|
|
2023-04-12 01:45:10 +02:00
|
|
|
template<typename... Ts> class StopAction : public Action<Ts...>, public Parented<VoiceAssistant> {
|
|
|
|
public:
|
2023-05-31 06:30:53 +02:00
|
|
|
void play(Ts... x) override {
|
|
|
|
this->parent_->set_continuous(false);
|
|
|
|
this->parent_->signal_stop();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename... Ts> class IsRunningCondition : public Condition<Ts...>, public Parented<VoiceAssistant> {
|
|
|
|
public:
|
|
|
|
bool check(Ts... x) override { return this->parent_->is_running() || this->parent_->is_continuous(); }
|
2023-04-12 01:45:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
extern VoiceAssistant *global_voice_assistant; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
|
|
|
|
|
|
|
} // namespace voice_assistant
|
|
|
|
} // namespace esphome
|
2023-05-10 06:46:32 +02:00
|
|
|
|
|
|
|
#endif // USE_VOICE_ASSISTANT
|