[rtttl] Gate on_finished_playback callback storage behind define (#16003)

This commit is contained in:
J. Nick Koston
2026-04-26 00:03:59 -05:00
committed by GitHub
parent 58f6ad2d0c
commit f092e619d8
5 changed files with 17 additions and 1 deletions

View File

@@ -93,7 +93,9 @@ async def to_code(config):
cg.add(var.set_gain(config[CONF_GAIN]))
await automation.build_callback_automations(var, config, _CALLBACK_AUTOMATIONS)
if config.get(CONF_ON_FINISHED_PLAYBACK):
cg.add_define("USE_RTTTL_FINISHED_PLAYBACK_CALLBACK")
await automation.build_callback_automations(var, config, _CALLBACK_AUTOMATIONS)
@automation.register_action(

View File

@@ -424,7 +424,9 @@ void Rtttl::set_state_(State state) {
// Clear loop_done when transitioning from `State::STOPPED` to any other state
if (state == State::STOPPED) {
this->disable_loop();
#ifdef USE_RTTTL_FINISHED_PLAYBACK_CALLBACK
this->on_finished_playback_callback_.call();
#endif
ESP_LOGD(TAG, "Playback finished");
} else if (old_state == State::STOPPED) {
this->enable_loop();

View File

@@ -2,6 +2,8 @@
#include "esphome/core/automation.h"
#include "esphome/core/component.h"
#include "esphome/core/defines.h"
#include "esphome/core/helpers.h"
#ifdef USE_OUTPUT
#include "esphome/components/output/float_output.h"
@@ -45,9 +47,11 @@ class Rtttl : public Component {
bool is_playing() { return this->state_ != State::STOPPED; }
#ifdef USE_RTTTL_FINISHED_PLAYBACK_CALLBACK
template<typename F> void add_on_finished_playback_callback(F &&callback) {
this->on_finished_playback_callback_.add(std::forward<F>(callback));
}
#endif
protected:
inline uint16_t get_integer_() {
@@ -106,8 +110,10 @@ class Rtttl : public Component {
uint32_t samples_gap_{0};
#endif // USE_SPEAKER
#ifdef USE_RTTTL_FINISHED_PLAYBACK_CALLBACK
/// The callback to call when playback is finished.
CallbackManager<void()> on_finished_playback_callback_;
#endif
};
template<typename... Ts> class PlayAction : public Action<Ts...> {

View File

@@ -186,6 +186,7 @@
#define USE_MQTT
#define USE_MQTT_COVER_JSON
#define USE_NETWORK
#define USE_RTTTL_FINISHED_PLAYBACK_CALLBACK
#define USE_RUNTIME_IMAGE_BMP
#define USE_RUNTIME_IMAGE_PNG
#define USE_RUNTIME_IMAGE_JPEG

View File

@@ -29,3 +29,8 @@ output:
rtttl:
output: rtttl_output
on_finished_playback:
- then:
- logger.log: "Playback finished 1"
- then:
- logger.log: "Playback finished 2"