From 99c60bfa42260ce6c1c8e987b94fd2a02691c772 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 8 Nov 2025 22:41:05 -0600 Subject: [PATCH 1/4] Add additional speaker lambda tests --- tests/components/speaker/common.yaml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/components/speaker/common.yaml b/tests/components/speaker/common.yaml index c04674ee29..fa54fa7e39 100644 --- a/tests/components/speaker/common.yaml +++ b/tests/components/speaker/common.yaml @@ -1,3 +1,12 @@ +number: + - platform: template + name: "Speaker Number" + id: my_number + optimistic: true + min_value: 0 + max_value: 100 + step: 1 + esphome: on_boot: then: @@ -14,6 +23,15 @@ esphome: - speaker.finish: - speaker.stop: +button: + - platform: template + name: "Speaker Button" + on_press: + then: + - speaker.play: [0x10, 0x20, 0x30, 0x40] + - speaker.play: !lambda |- + return {0x01, 0x02, (uint8_t)id(my_number).state}; + i2s_audio: i2s_lrclk_pin: ${i2s_bclk_pin} i2s_bclk_pin: ${i2s_lrclk_pin} From d29882e4ad78d504d46cd46b44785c41adff0986 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 8 Nov 2025 22:51:11 -0600 Subject: [PATCH 2/4] Add additonal abbwelcome remote_base tests --- .../remote_transmitter/common-buttons.yaml | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/components/remote_transmitter/common-buttons.yaml b/tests/components/remote_transmitter/common-buttons.yaml index e9593cc97c..101d60a893 100644 --- a/tests/components/remote_transmitter/common-buttons.yaml +++ b/tests/components/remote_transmitter/common-buttons.yaml @@ -1,3 +1,11 @@ +number: + - platform: template + id: test_number + optimistic: true + min_value: 0 + max_value: 255 + step: 1 + button: - platform: template name: Beo4 audio mute @@ -217,6 +225,23 @@ button: command: 0xEC rc_code_1: 0x0D rc_code_2: 0x0D + - platform: template + name: ABBWelcome static + on_press: + remote_transmitter.transmit_abbwelcome: + source_address: 0x1234 + destination_address: 0x5678 + message_type: 0x01 + data: [0x10, 0x20, 0x30] + - platform: template + name: ABBWelcome lambda + on_press: + remote_transmitter.transmit_abbwelcome: + source_address: 0x1234 + destination_address: 0x5678 + message_type: 0x01 + data: !lambda |- + return {(uint8_t)id(test_number).state, 0x20, 0x30}; - platform: template name: Digital Write on_press: From 9abef44ac035a0ce8865d5b77c8e52aabceb8441 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 8 Nov 2025 23:44:11 -0600 Subject: [PATCH 3/4] optimize --- esphome/components/speaker/automation.h | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/esphome/components/speaker/automation.h b/esphome/components/speaker/automation.h index cae429cf8a..391c9e4c62 100644 --- a/esphome/components/speaker/automation.h +++ b/esphome/components/speaker/automation.h @@ -12,32 +12,30 @@ template class PlayAction : public Action, public Parente public: void set_data_template(std::vector (*func)(Ts...)) { this->data_.func = func; - this->static_ = false; + this->len_ = -1; // Sentinel value indicates template mode } void set_data_static(const uint8_t *data, size_t len) { - this->data_.static_data.ptr = data; - this->data_.static_data.len = len; - this->static_ = true; + this->data_.data = data; + this->len_ = len; // Length >= 0 indicates static mode } void play(const Ts &...x) override { - if (this->static_) { - this->parent_->play(this->data_.static_data.ptr, this->data_.static_data.len); + if (this->len_ >= 0) { + // Static mode: pass pointer directly to play(const uint8_t *, size_t) + this->parent_->play(this->data_.data, static_cast(this->len_)); } else { + // Template mode: call function and pass vector to play(const std::vector &) auto val = this->data_.func(x...); this->parent_->play(val); } } protected: - bool static_{true}; + ssize_t len_{-1}; // -1 = template mode, >=0 = static mode with length union Data { - std::vector (*func)(Ts...); - struct { - const uint8_t *ptr; - size_t len; - } static_data; + std::vector (*func)(Ts...); // Function pointer (stateless lambdas) + const uint8_t *data; // Pointer to static data in flash } data_; }; From ff04a6da4b9acc11d6d28752508f05c266a00a77 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 8 Nov 2025 23:45:42 -0600 Subject: [PATCH 4/4] optimize --- .../remote_base/abbwelcome_protocol.h | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/esphome/components/remote_base/abbwelcome_protocol.h b/esphome/components/remote_base/abbwelcome_protocol.h index 2ed90c502e..4b922eb2f1 100644 --- a/esphome/components/remote_base/abbwelcome_protocol.h +++ b/esphome/components/remote_base/abbwelcome_protocol.h @@ -216,12 +216,11 @@ template class ABBWelcomeAction : public RemoteTransmitterAction TEMPLATABLE_VALUE(bool, auto_message_id) void set_data_template(std::vector (*func)(Ts...)) { this->data_.func = func; - this->static_ = false; + this->len_ = -1; // Sentinel value indicates template mode } void set_data_static(const uint8_t *data, size_t len) { - this->data_.static_data.ptr = data; - this->data_.static_data.len = len; - this->static_ = true; + this->data_.data = data; + this->len_ = len; // Length >= 0 indicates static mode } void encode(RemoteTransmitData *dst, Ts... x) override { ABBWelcomeData data; @@ -233,9 +232,11 @@ template class ABBWelcomeAction : public RemoteTransmitterAction data.set_message_id(this->message_id_.value(x...)); data.auto_message_id = this->auto_message_id_.value(x...); std::vector data_vec; - if (this->static_) { - data_vec.assign(this->data_.static_data.ptr, this->data_.static_data.ptr + this->data_.static_data.len); + if (this->len_ >= 0) { + // Static mode: copy from flash to vector + data_vec.assign(this->data_.data, this->data_.data + this->len_); } else { + // Template mode: call function data_vec = this->data_.func(x...); } data.set_data(data_vec); @@ -244,13 +245,10 @@ template class ABBWelcomeAction : public RemoteTransmitterAction } protected: - bool static_{true}; + ssize_t len_{-1}; // -1 = template mode, >=0 = static mode with length union Data { - std::vector (*func)(Ts...); - struct { - const uint8_t *ptr; - size_t len; - } static_data; + std::vector (*func)(Ts...); // Function pointer (stateless lambdas) + const uint8_t *data; // Pointer to static data in flash } data_; };