From a6feea5415a1109f7d175c5a3b2c9429233b57c6 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 8 Nov 2025 22:19:47 -0600 Subject: [PATCH 01/13] Add additional sx126x lambda tests --- tests/components/sx126x/common.yaml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/components/sx126x/common.yaml b/tests/components/sx126x/common.yaml index 3f540a4bae..659550cc01 100644 --- a/tests/components/sx126x/common.yaml +++ b/tests/components/sx126x/common.yaml @@ -26,6 +26,15 @@ sx126x: - lambda: |- ESP_LOGD("lambda", "packet %.2f %.2f %s", rssi, snr, format_hex(x).c_str()); +number: + - platform: template + name: "SX126x Number" + id: my_number + optimistic: true + min_value: 0 + max_value: 100 + step: 1 + button: - platform: template name: "SX126x Button" @@ -37,3 +46,5 @@ button: - sx126x.set_mode_rx - sx126x.send_packet: data: [0xC5, 0x51, 0x78, 0x82, 0xB7, 0xF9, 0x9C, 0x5C] + - sx126x.send_packet: !lambda |- + return {0x01, 0x02, (uint8_t)id(my_number).state}; From a67a433627448edfdccb48b1a88dd5bc9623731b Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 8 Nov 2025 22:28:32 -0600 Subject: [PATCH 02/13] Add additional sx127x lambda tests --- tests/components/sx127x/common.yaml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/components/sx127x/common.yaml b/tests/components/sx127x/common.yaml index 540381fc08..6e48952fcc 100644 --- a/tests/components/sx127x/common.yaml +++ b/tests/components/sx127x/common.yaml @@ -26,6 +26,15 @@ sx127x: - sx127x.send_packet: data: [0xC5, 0x51, 0x78, 0x82, 0xB7, 0xF9, 0x9C, 0x5C] +number: + - platform: template + name: "SX127x Number" + id: my_number + optimistic: true + min_value: 0 + max_value: 100 + step: 1 + button: - platform: template name: "SX127x Button" @@ -38,3 +47,5 @@ button: - sx127x.set_mode_rx - sx127x.send_packet: data: [0xC5, 0x51, 0x78, 0x82, 0xB7, 0xF9, 0x9C, 0x5C] + - sx127x.send_packet: !lambda |- + return {0x01, 0x02, (uint8_t)id(my_number).state}; From 5310512123ae59868969240786e6fd0806c88ef5 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 8 Nov 2025 22:33:44 -0600 Subject: [PATCH 03/13] Add additional udp lambda tests --- tests/components/udp/common.yaml | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/components/udp/common.yaml b/tests/components/udp/common.yaml index 96224d0d1f..98546d49ef 100644 --- a/tests/components/udp/common.yaml +++ b/tests/components/udp/common.yaml @@ -17,3 +17,22 @@ udp: id: my_udp data: !lambda |- return std::vector{1,3,4,5,6}; + +number: + - platform: template + name: "UDP Number" + id: my_number + optimistic: true + min_value: 0 + max_value: 100 + step: 1 + +button: + - platform: template + name: "UDP Button" + on_press: + then: + - udp.write: + data: [0x01, 0x02, 0x03] + - udp.write: !lambda |- + return {0x10, 0x20, (uint8_t)id(my_number).state}; From 5b8827d47acec9732a191d5b2f900610ad7fcf92 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 8 Nov 2025 23:07:43 -0600 Subject: [PATCH 04/13] [remote_base] Optimize raw transmit action memory usage - use function pointers --- esphome/components/remote_base/raw_protocol.h | 29 ++++++++++++------- .../remote_transmitter/common-buttons.yaml | 16 +++++++++- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/esphome/components/remote_base/raw_protocol.h b/esphome/components/remote_base/raw_protocol.h index 9b671e611f..4f50a85908 100644 --- a/esphome/components/remote_base/raw_protocol.h +++ b/esphome/components/remote_base/raw_protocol.h @@ -42,17 +42,21 @@ class RawTrigger : public Trigger, public Component, public RemoteRe template class RawAction : public RemoteTransmitterActionBase { public: - void set_code_template(std::function func) { this->code_func_ = func; } + void set_code_template(RawTimings (*func)(Ts...)) { + this->code_.func = func; + this->static_ = false; + } void set_code_static(const int32_t *code, size_t len) { - this->code_static_ = code; - this->code_static_len_ = len; + this->code_.static_code.data = code; + this->code_.static_code.len = len; + this->static_ = true; } TEMPLATABLE_VALUE(uint32_t, carrier_frequency); void encode(RemoteTransmitData *dst, Ts... x) override { - if (this->code_static_ != nullptr) { - for (size_t i = 0; i < this->code_static_len_; i++) { - auto val = this->code_static_[i]; + if (this->static_) { + for (size_t i = 0; i < this->code_.static_code.len; i++) { + auto val = this->code_.static_code.data[i]; if (val < 0) { dst->space(static_cast(-val)); } else { @@ -60,15 +64,20 @@ template class RawAction : public RemoteTransmitterActionBaseset_data(this->code_func_(x...)); + dst->set_data(this->code_.func(x...)); } dst->set_carrier_frequency(this->carrier_frequency_.value(x...)); } protected: - std::function code_func_{nullptr}; - const int32_t *code_static_{nullptr}; - int32_t code_static_len_{0}; + bool static_{true}; + union Code { + RawTimings (*func)(Ts...); + struct { + const int32_t *data; + size_t len; + } static_code; + } code_; }; class RawDumper : public RemoteReceiverDumperBase { diff --git a/tests/components/remote_transmitter/common-buttons.yaml b/tests/components/remote_transmitter/common-buttons.yaml index e9593cc97c..cab28d813b 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 @@ -128,10 +136,16 @@ button: address: 0x00 command: 0x0B - platform: template - name: RC5 Raw + name: RC5 Raw static on_press: remote_transmitter.transmit_raw: code: [1000, -1000] + - platform: template + name: RC5 Raw lambda + on_press: + remote_transmitter.transmit_raw: + code: !lambda |- + return {(int32_t)id(test_number).state * 100, -1000}; - platform: template name: AEHA id: eaha_hitachi_climate_power_on From 353ea5674dd2243f4e0bfae2958c2da14d551665 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 8 Nov 2025 23:09:31 -0600 Subject: [PATCH 05/13] Add additional tests for remote_transmitter raw --- .../remote_transmitter/common-buttons.yaml | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/components/remote_transmitter/common-buttons.yaml b/tests/components/remote_transmitter/common-buttons.yaml index e9593cc97c..cab28d813b 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 @@ -128,10 +136,16 @@ button: address: 0x00 command: 0x0B - platform: template - name: RC5 Raw + name: RC5 Raw static on_press: remote_transmitter.transmit_raw: code: [1000, -1000] + - platform: template + name: RC5 Raw lambda + on_press: + remote_transmitter.transmit_raw: + code: !lambda |- + return {(int32_t)id(test_number).state * 100, -1000}; - platform: template name: AEHA id: eaha_hitachi_climate_power_on From 59485c1d2b9090dfe2e30ff41e8111ad0466a6e8 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 8 Nov 2025 23:14:57 -0600 Subject: [PATCH 06/13] save 4 bytes --- esphome/components/remote_base/raw_protocol.h | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/esphome/components/remote_base/raw_protocol.h b/esphome/components/remote_base/raw_protocol.h index 4f50a85908..f59431c88a 100644 --- a/esphome/components/remote_base/raw_protocol.h +++ b/esphome/components/remote_base/raw_protocol.h @@ -44,19 +44,18 @@ template class RawAction : public RemoteTransmitterActionBasecode_.func = func; - this->static_ = false; + this->len_ = -1; } void set_code_static(const int32_t *code, size_t len) { - this->code_.static_code.data = code; - this->code_.static_code.len = len; - this->static_ = true; + this->code_.data = code; + this->len_ = len; } TEMPLATABLE_VALUE(uint32_t, carrier_frequency); void encode(RemoteTransmitData *dst, Ts... x) override { - if (this->static_) { - for (size_t i = 0; i < this->code_.static_code.len; i++) { - auto val = this->code_.static_code.data[i]; + if (this->len_ >= 0) { + for (size_t i = 0; i < static_cast(this->len_); i++) { + auto val = this->code_.data[i]; if (val < 0) { dst->space(static_cast(-val)); } else { @@ -70,13 +69,10 @@ template class RawAction : public RemoteTransmitterActionBase Date: Sat, 8 Nov 2025 23:20:27 -0600 Subject: [PATCH 07/13] optimize --- esphome/components/uart/automation.h | 29 +++++++++++++--------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/esphome/components/uart/automation.h b/esphome/components/uart/automation.h index 7a3344c2f1..c2eb308eb8 100644 --- a/esphome/components/uart/automation.h +++ b/esphome/components/uart/automation.h @@ -12,36 +12,33 @@ template class UARTWriteAction : public Action, public Pa public: void set_data_template(std::vector (*func)(Ts...)) { // Stateless lambdas (generated by ESPHome) implicitly convert to function pointers - this->data_.func = func; - this->static_ = false; + this->code_.func = func; + this->len_ = -1; // Sentinel value indicates template mode } // Store pointer to static data in flash (no RAM copy) void set_data_static(const uint8_t *data, size_t len) { - // Simply set pointer and length - no construction needed for POD types - this->data_.static_data.ptr = data; - this->data_.static_data.len = len; - this->static_ = true; + this->code_.data = data; + this->len_ = len; // Length >= 0 indicates static mode } void play(const Ts &...x) override { - if (this->static_) { - this->parent_->write_array(this->data_.static_data.ptr, this->data_.static_data.len); + if (this->len_ >= 0) { + // Static mode: use pointer and length + this->parent_->write_array(this->code_.data, static_cast(this->len_)); } else { - auto val = this->data_.func(x...); + // Template mode: call function + auto val = this->code_.func(x...); this->parent_->write_array(val); } } protected: - bool static_{true}; // Default to static mode (most common case) - union Data { + ssize_t len_{-1}; // -1 = template mode, >=0 = static mode with length + union Code { std::vector (*func)(Ts...); // Function pointer (stateless lambdas) - struct { - const uint8_t *ptr; - size_t len; - } static_data; - } data_; + const uint8_t *data; // Pointer to static data in flash + } code_; }; } // namespace uart From db8b96f257685808ddd61aedebbc72c529552e17 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 8 Nov 2025 23:21:57 -0600 Subject: [PATCH 08/13] tweak --- esphome/components/remote_base/raw_protocol.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/esphome/components/remote_base/raw_protocol.h b/esphome/components/remote_base/raw_protocol.h index f59431c88a..941b6aab42 100644 --- a/esphome/components/remote_base/raw_protocol.h +++ b/esphome/components/remote_base/raw_protocol.h @@ -44,11 +44,11 @@ template class RawAction : public RemoteTransmitterActionBasecode_.func = func; - this->len_ = -1; + this->len_ = -1; // Sentinel value indicates template mode } void set_code_static(const int32_t *code, size_t len) { this->code_.data = code; - this->len_ = len; + this->len_ = len; // Length >= 0 indicates static mode } TEMPLATABLE_VALUE(uint32_t, carrier_frequency); @@ -69,7 +69,7 @@ template class RawAction : public RemoteTransmitterActionBase=0 = static mode with length union Code { RawTimings (*func)(Ts...); const int32_t *data; From 729304af01898a319f643417b257ad049409d1d1 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 8 Nov 2025 23:28:23 -0600 Subject: [PATCH 09/13] optimize --- esphome/components/ble_client/automation.h | 26 ++++++++++------------ 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/esphome/components/ble_client/automation.h b/esphome/components/ble_client/automation.h index 46915be9ed..9c5646b3d1 100644 --- a/esphome/components/ble_client/automation.h +++ b/esphome/components/ble_client/automation.h @@ -107,15 +107,14 @@ template class BLEClientWriteAction : public Action, publ void set_char_uuid128(uint8_t *uuid) { this->char_uuid_ = espbt::ESPBTUUID::from_raw(uuid); } void set_value_template(std::vector (*func)(Ts...)) { - this->value_.template_func = func; - this->has_simple_value_ = false; + this->value_.func = func; + this->len_ = -1; // Sentinel value indicates template mode } // Store pointer to static data in flash (no RAM copy) void set_value_simple(const uint8_t *data, size_t len) { - this->value_.simple_data.ptr = data; - this->value_.simple_data.len = len; - this->has_simple_value_ = true; + this->value_.data = data; + this->len_ = len; // Length >= 0 indicates static mode } void play(const Ts &...x) override {} @@ -124,10 +123,12 @@ template class BLEClientWriteAction : public Action, publ this->num_running_++; this->var_ = std::make_tuple(x...); std::vector value; - if (this->has_simple_value_) { - value.assign(this->value_.simple_data.ptr, this->value_.simple_data.ptr + this->value_.simple_data.len); + if (this->len_ >= 0) { + // Static mode: copy from flash to vector + value.assign(this->value_.data, this->value_.data + this->len_); } else { - value = this->value_.template_func(x...); + // Template mode: call function + value = this->value_.func(x...); } // on write failure, continue the automation chain rather than stopping so that e.g. disconnect can work. if (!write(value)) @@ -202,13 +203,10 @@ template class BLEClientWriteAction : public Action, publ private: BLEClient *ble_client_; - bool has_simple_value_{true}; + ssize_t len_{-1}; // -1 = template mode, >=0 = static mode with length union Value { - std::vector (*template_func)(Ts...); - struct { - const uint8_t *ptr; - size_t len; - } simple_data; + std::vector (*func)(Ts...); // Function pointer (stateless lambdas) + const uint8_t *data; // Pointer to static data in flash } value_; espbt::ESPBTUUID service_uuid_; espbt::ESPBTUUID char_uuid_; From 845fae77166454c011e782967a3d9f622475be3e Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 8 Nov 2025 23:30:53 -0600 Subject: [PATCH 10/13] optimize --- esphome/components/canbus/canbus.h | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/esphome/components/canbus/canbus.h b/esphome/components/canbus/canbus.h index 122ccfe39e..f7b84111bd 100644 --- a/esphome/components/canbus/canbus.h +++ b/esphome/components/canbus/canbus.h @@ -115,14 +115,13 @@ template class CanbusSendAction : public Action, public P void set_data_template(std::vector (*func)(Ts...)) { // Stateless lambdas (generated by ESPHome) implicitly convert to function pointers this->data_.func = func; - this->static_ = false; + this->len_ = -1; // Sentinel value indicates template mode } // Store pointer to static data in flash (no RAM copy) 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 set_can_id(uint32_t can_id) { this->can_id_ = can_id; } @@ -138,9 +137,11 @@ template class CanbusSendAction : public Action, public P auto use_extended_id = this->use_extended_id_.has_value() ? *this->use_extended_id_ : this->parent_->use_extended_id_; std::vector data; - if (this->static_) { - data.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.assign(this->data_.data, this->data_.data + this->len_); } else { + // Template mode: call function data = this->data_.func(x...); } this->parent_->send_data(can_id, use_extended_id, this->remote_transmission_request_, data); @@ -150,14 +151,11 @@ template class CanbusSendAction : public Action, public P optional can_id_{}; optional use_extended_id_{}; bool remote_transmission_request_{false}; - bool static_{true}; // Default to static mode (most common case) + ssize_t len_{-1}; // -1 = template mode, >=0 = static mode with length union Data { - std::vector (*func)(Ts...); // 4 bytes on 32-bit - struct { - const uint8_t *ptr; // 4 bytes on 32-bit - size_t len; // 4 bytes on 32-bit - } static_data; // 8 bytes total on 32-bit - } data_; // Union size = 8 bytes (max of 4 and 8) + std::vector (*func)(Ts...); // Function pointer (stateless lambdas) + const uint8_t *data; // Pointer to static data in flash + } data_; }; class CanbusTrigger : public Trigger, uint32_t, bool>, public Component { From 21d0c8b54903fdaf74f402acd25bee8d6e7795b8 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 8 Nov 2025 23:36:06 -0600 Subject: [PATCH 11/13] optimize --- esphome/components/sx126x/automation.h | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/esphome/components/sx126x/automation.h b/esphome/components/sx126x/automation.h index 41f0a888e5..2282c583cb 100644 --- a/esphome/components/sx126x/automation.h +++ b/esphome/components/sx126x/automation.h @@ -16,33 +16,31 @@ template class SendPacketAction : public Action, public P 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 { std::vector data; - if (this->static_) { - data.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.assign(this->data_.data, this->data_.data + this->len_); } else { + // Template mode: call function data = this->data_.func(x...); } this->parent_->transmit_packet(data); } 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 bdaeb2cf2e1235c6a69902440853b28d45402a7d Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 8 Nov 2025 23:39:39 -0600 Subject: [PATCH 12/13] optimize --- esphome/components/sx127x/automation.h | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/esphome/components/sx127x/automation.h b/esphome/components/sx127x/automation.h index 52dbf37e09..fb0367fcca 100644 --- a/esphome/components/sx127x/automation.h +++ b/esphome/components/sx127x/automation.h @@ -16,33 +16,31 @@ template class SendPacketAction : public Action, public P 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 { std::vector data; - if (this->static_) { - data.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.assign(this->data_.data, this->data_.data + this->len_); } else { + // Template mode: call function data = this->data_.func(x...); } this->parent_->transmit_packet(data); } 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 c16cd3bab5e2da864099dbf01bf400302d5ec676 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 8 Nov 2025 23:41:24 -0600 Subject: [PATCH 13/13] optimize --- esphome/components/udp/automation.h | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/esphome/components/udp/automation.h b/esphome/components/udp/automation.h index a3b76fb4ea..b66c2a9892 100644 --- a/esphome/components/udp/automation.h +++ b/esphome/components/udp/automation.h @@ -13,32 +13,30 @@ template class UDPWriteAction : public Action, public Par 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_->send_packet(this->data_.static_data.ptr, this->data_.static_data.len); + if (this->len_ >= 0) { + // Static mode: pass pointer directly to send_packet(const uint8_t *, size_t) + this->parent_->send_packet(this->data_.data, static_cast(this->len_)); } else { + // Template mode: call function and pass vector to send_packet(const std::vector &) auto val = this->data_.func(x...); this->parent_->send_packet(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_; };