From 7207b9734f067032c5c289f674bca77133b9300f Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Thu, 20 Apr 2023 08:56:37 +1200 Subject: [PATCH] Call on_error if no api client connected that handles voice (#4709) --- esphome/components/api/api_server.cpp | 6 ++++-- esphome/components/api/api_server.h | 2 +- esphome/components/voice_assistant/voice_assistant.cpp | 5 ++++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/esphome/components/api/api_server.cpp b/esphome/components/api/api_server.cpp index fbef4b253f..97a7d6fbf6 100644 --- a/esphome/components/api/api_server.cpp +++ b/esphome/components/api/api_server.cpp @@ -428,10 +428,12 @@ void APIServer::on_shutdown() { } #ifdef USE_VOICE_ASSISTANT -void APIServer::start_voice_assistant() { +bool APIServer::start_voice_assistant() { + bool result = false; for (auto &c : this->clients_) { - c->request_voice_assistant(true); + result |= c->request_voice_assistant(true); } + return result; } void APIServer::stop_voice_assistant() { for (auto &c : this->clients_) { diff --git a/esphome/components/api/api_server.h b/esphome/components/api/api_server.h index 30103b2e3f..a1bec2802f 100644 --- a/esphome/components/api/api_server.h +++ b/esphome/components/api/api_server.h @@ -96,7 +96,7 @@ class APIServer : public Component, public Controller { #endif #ifdef USE_VOICE_ASSISTANT - void start_voice_assistant(); + bool start_voice_assistant(); void stop_voice_assistant(); #endif diff --git a/esphome/components/voice_assistant/voice_assistant.cpp b/esphome/components/voice_assistant/voice_assistant.cpp index 777bef4edb..e2d5bea90a 100644 --- a/esphome/components/voice_assistant/voice_assistant.cpp +++ b/esphome/components/voice_assistant/voice_assistant.cpp @@ -63,7 +63,10 @@ void VoiceAssistant::start(struct sockaddr_storage *addr, uint16_t port) { void VoiceAssistant::request_start() { ESP_LOGD(TAG, "Requesting start..."); - api::global_api_server->start_voice_assistant(); + if (!api::global_api_server->start_voice_assistant()) { + ESP_LOGW(TAG, "Could not request start."); + this->error_trigger_->trigger("not-connected", "Could not request start."); + } } void VoiceAssistant::signal_stop() {