Merge branch 'integration' into memory_api

This commit is contained in:
J. Nick Koston
2025-11-08 22:57:40 -06:00
19 changed files with 255 additions and 67 deletions

View File

@@ -4,7 +4,7 @@ from esphome import automation
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.const import CONF_DATA, CONF_ID, CONF_TRIGGER_ID
from esphome.core import CORE
from esphome.core import CORE, ID
CODEOWNERS = ["@mvturnho", "@danielschramm"]
IS_PLATFORM_COMPONENT = True
@@ -176,5 +176,8 @@ async def canbus_action_to_code(config, action_id, template_arg, args):
else:
if isinstance(data, bytes):
data = [int(x) for x in data]
cg.add(var.set_data_static(data))
# Generate static array in flash to avoid RAM copy
arr_id = ID(f"{action_id}_data", is_declaration=True, type=cg.uint8)
arr = cg.static_const_array(arr_id, cg.ArrayInitializer(*data))
cg.add(var.set_data_static(arr, len(data)))
return var

View File

@@ -112,12 +112,16 @@ class Canbus : public Component {
template<typename... Ts> class CanbusSendAction : public Action<Ts...>, public Parented<Canbus> {
public:
void set_data_template(const std::function<std::vector<uint8_t>(Ts...)> func) {
this->data_func_ = func;
void set_data_template(std::vector<uint8_t> (*func)(Ts...)) {
// Stateless lambdas (generated by ESPHome) implicitly convert to function pointers
this->data_.func = func;
this->static_ = false;
}
void set_data_static(const std::vector<uint8_t> &data) {
this->data_static_ = data;
// 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;
}
@@ -133,21 +137,27 @@ template<typename... Ts> class CanbusSendAction : public Action<Ts...>, public P
auto can_id = this->can_id_.has_value() ? *this->can_id_ : this->parent_->can_id_;
auto use_extended_id =
this->use_extended_id_.has_value() ? *this->use_extended_id_ : this->parent_->use_extended_id_;
std::vector<uint8_t> data;
if (this->static_) {
this->parent_->send_data(can_id, use_extended_id, this->remote_transmission_request_, this->data_static_);
data.assign(this->data_.static_data.ptr, this->data_.static_data.ptr + this->data_.static_data.len);
} else {
auto val = this->data_func_(x...);
this->parent_->send_data(can_id, use_extended_id, this->remote_transmission_request_, val);
data = this->data_.func(x...);
}
this->parent_->send_data(can_id, use_extended_id, this->remote_transmission_request_, data);
}
protected:
optional<uint32_t> can_id_{};
optional<bool> use_extended_id_{};
bool remote_transmission_request_{false};
bool static_{false};
std::function<std::vector<uint8_t>(Ts...)> data_func_{};
std::vector<uint8_t> data_static_{};
bool static_{true}; // Default to static mode (most common case)
union Data {
std::vector<uint8_t> (*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)
};
class CanbusTrigger : public Trigger<std::vector<uint8_t>, uint32_t, bool>, public Component {

View File

@@ -39,7 +39,7 @@ from esphome.const import (
CONF_WAND_ID,
CONF_ZERO,
)
from esphome.core import coroutine
from esphome.core import ID, coroutine
from esphome.schema_extractors import SCHEMA_EXTRACT, schema_extractor
from esphome.util import Registry, SimpleRegistry
@@ -2104,7 +2104,9 @@ async def abbwelcome_action(var, config, args):
)
cg.add(var.set_data_template(template_))
else:
cg.add(var.set_data_static(data_))
arr_id = ID(f"{var.base}_data", is_declaration=True, type=cg.uint8)
arr = cg.static_const_array(arr_id, cg.ArrayInitializer(*data_))
cg.add(var.set_data_static(arr, len(data_)))
# Mirage

View File

@@ -214,10 +214,14 @@ template<typename... Ts> class ABBWelcomeAction : public RemoteTransmitterAction
TEMPLATABLE_VALUE(uint8_t, message_type)
TEMPLATABLE_VALUE(uint8_t, message_id)
TEMPLATABLE_VALUE(bool, auto_message_id)
void set_data_static(std::vector<uint8_t> data) { data_static_ = std::move(data); }
void set_data_template(std::function<std::vector<uint8_t>(Ts...)> func) {
this->data_func_ = func;
has_data_func_ = true;
void set_data_template(std::vector<uint8_t> (*func)(Ts...)) {
this->data_.func = func;
this->static_ = false;
}
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;
}
void encode(RemoteTransmitData *dst, Ts... x) override {
ABBWelcomeData data;
@@ -228,19 +232,26 @@ template<typename... Ts> class ABBWelcomeAction : public RemoteTransmitterAction
data.set_message_type(this->message_type_.value(x...));
data.set_message_id(this->message_id_.value(x...));
data.auto_message_id = this->auto_message_id_.value(x...);
if (has_data_func_) {
data.set_data(this->data_func_(x...));
std::vector<uint8_t> data_vec;
if (this->static_) {
data_vec.assign(this->data_.static_data.ptr, this->data_.static_data.ptr + this->data_.static_data.len);
} else {
data.set_data(this->data_static_);
data_vec = this->data_.func(x...);
}
data.set_data(data_vec);
data.finalize();
ABBWelcomeProtocol().encode(dst, data);
}
protected:
std::function<std::vector<uint8_t>(Ts...)> data_func_{};
std::vector<uint8_t> data_static_{};
bool has_data_func_{false};
bool static_{true};
union Data {
std::vector<uint8_t> (*func)(Ts...);
struct {
const uint8_t *ptr;
size_t len;
} static_data;
} data_;
};
} // namespace remote_base

View File

@@ -3,7 +3,7 @@ import esphome.codegen as cg
from esphome.components import audio, audio_dac
import esphome.config_validation as cv
from esphome.const import CONF_DATA, CONF_ID, CONF_VOLUME
from esphome.core import CORE
from esphome.core import CORE, ID
from esphome.coroutine import CoroPriority, coroutine_with_priority
AUTO_LOAD = ["audio"]
@@ -90,7 +90,10 @@ async def speaker_play_action(config, action_id, template_arg, args):
templ = await cg.templatable(data, args, cg.std_vector.template(cg.uint8))
cg.add(var.set_data_template(templ))
else:
cg.add(var.set_data_static(data))
# Generate static array in flash to avoid RAM copy
arr_id = ID(f"{action_id}_data", is_declaration=True, type=cg.uint8)
arr = cg.static_const_array(arr_id, cg.ArrayInitializer(*data))
cg.add(var.set_data_static(arr, len(data)))
return var

View File

@@ -10,28 +10,35 @@ namespace speaker {
template<typename... Ts> class PlayAction : public Action<Ts...>, public Parented<Speaker> {
public:
void set_data_template(std::function<std::vector<uint8_t>(Ts...)> func) {
this->data_func_ = func;
void set_data_template(std::vector<uint8_t> (*func)(Ts...)) {
this->data_.func = func;
this->static_ = false;
}
void set_data_static(const std::vector<uint8_t> &data) {
this->data_static_ = data;
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;
}
void play(const Ts &...x) override {
if (this->static_) {
this->parent_->play(this->data_static_);
this->parent_->play(this->data_.static_data.ptr, this->data_.static_data.len);
} else {
auto val = this->data_func_(x...);
auto val = this->data_.func(x...);
this->parent_->play(val);
}
}
protected:
bool static_{false};
std::function<std::vector<uint8_t>(Ts...)> data_func_{};
std::vector<uint8_t> data_static_{};
bool static_{true};
union Data {
std::vector<uint8_t> (*func)(Ts...);
struct {
const uint8_t *ptr;
size_t len;
} static_data;
} data_;
};
template<typename... Ts> class VolumeSetAction : public Action<Ts...>, public Parented<Speaker> {

View File

@@ -3,7 +3,7 @@ import esphome.codegen as cg
from esphome.components import spi
import esphome.config_validation as cv
from esphome.const import CONF_BUSY_PIN, CONF_DATA, CONF_FREQUENCY, CONF_ID
from esphome.core import TimePeriod
from esphome.core import ID, TimePeriod
MULTI_CONF = True
CODEOWNERS = ["@swoboda1337"]
@@ -329,5 +329,8 @@ async def send_packet_action_to_code(config, action_id, template_arg, args):
templ = await cg.templatable(data, args, cg.std_vector.template(cg.uint8))
cg.add(var.set_data_template(templ))
else:
cg.add(var.set_data_static(data))
# Generate static array in flash to avoid RAM copy
arr_id = ID(f"{action_id}_data", is_declaration=True, type=cg.uint8)
arr = cg.static_const_array(arr_id, cg.ArrayInitializer(*data))
cg.add(var.set_data_static(arr, len(data)))
return var

View File

@@ -14,28 +14,36 @@ template<typename... Ts> class RunImageCalAction : public Action<Ts...>, public
template<typename... Ts> class SendPacketAction : public Action<Ts...>, public Parented<SX126x> {
public:
void set_data_template(std::function<std::vector<uint8_t>(Ts...)> func) {
this->data_func_ = func;
void set_data_template(std::vector<uint8_t> (*func)(Ts...)) {
this->data_.func = func;
this->static_ = false;
}
void set_data_static(const std::vector<uint8_t> &data) {
this->data_static_ = data;
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;
}
void play(const Ts &...x) override {
std::vector<uint8_t> data;
if (this->static_) {
this->parent_->transmit_packet(this->data_static_);
data.assign(this->data_.static_data.ptr, this->data_.static_data.ptr + this->data_.static_data.len);
} else {
this->parent_->transmit_packet(this->data_func_(x...));
data = this->data_.func(x...);
}
this->parent_->transmit_packet(data);
}
protected:
bool static_{false};
std::function<std::vector<uint8_t>(Ts...)> data_func_{};
std::vector<uint8_t> data_static_{};
bool static_{true};
union Data {
std::vector<uint8_t> (*func)(Ts...);
struct {
const uint8_t *ptr;
size_t len;
} static_data;
} data_;
};
template<typename... Ts> class SetModeTxAction : public Action<Ts...>, public Parented<SX126x> {

View File

@@ -3,6 +3,7 @@ import esphome.codegen as cg
from esphome.components import spi
import esphome.config_validation as cv
from esphome.const import CONF_DATA, CONF_FREQUENCY, CONF_ID
from esphome.core import ID
MULTI_CONF = True
CODEOWNERS = ["@swoboda1337"]
@@ -321,5 +322,8 @@ async def send_packet_action_to_code(config, action_id, template_arg, args):
templ = await cg.templatable(data, args, cg.std_vector.template(cg.uint8))
cg.add(var.set_data_template(templ))
else:
cg.add(var.set_data_static(data))
# Generate static array in flash to avoid RAM copy
arr_id = ID(f"{action_id}_data", is_declaration=True, type=cg.uint8)
arr = cg.static_const_array(arr_id, cg.ArrayInitializer(*data))
cg.add(var.set_data_static(arr, len(data)))
return var

View File

@@ -14,28 +14,36 @@ template<typename... Ts> class RunImageCalAction : public Action<Ts...>, public
template<typename... Ts> class SendPacketAction : public Action<Ts...>, public Parented<SX127x> {
public:
void set_data_template(std::function<std::vector<uint8_t>(Ts...)> func) {
this->data_func_ = func;
void set_data_template(std::vector<uint8_t> (*func)(Ts...)) {
this->data_.func = func;
this->static_ = false;
}
void set_data_static(const std::vector<uint8_t> &data) {
this->data_static_ = data;
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;
}
void play(const Ts &...x) override {
std::vector<uint8_t> data;
if (this->static_) {
this->parent_->transmit_packet(this->data_static_);
data.assign(this->data_.static_data.ptr, this->data_.static_data.ptr + this->data_.static_data.len);
} else {
this->parent_->transmit_packet(this->data_func_(x...));
data = this->data_.func(x...);
}
this->parent_->transmit_packet(data);
}
protected:
bool static_{false};
std::function<std::vector<uint8_t>(Ts...)> data_func_{};
std::vector<uint8_t> data_static_{};
bool static_{true};
union Data {
std::vector<uint8_t> (*func)(Ts...);
struct {
const uint8_t *ptr;
size_t len;
} static_data;
} data_;
};
template<typename... Ts> class SetModeTxAction : public Action<Ts...>, public Parented<SX127x> {

View File

@@ -12,7 +12,7 @@ from esphome.components.packet_transport import (
)
import esphome.config_validation as cv
from esphome.const import CONF_DATA, CONF_ID, CONF_PORT, CONF_TRIGGER_ID
from esphome.core import Lambda
from esphome.core import ID, Lambda
from esphome.cpp_generator import ExpressionStatement, MockObj
CODEOWNERS = ["@clydebarrow"]
@@ -158,5 +158,8 @@ async def udp_write_to_code(config, action_id, template_arg, args):
templ = await cg.templatable(data, args, cg.std_vector.template(cg.uint8))
cg.add(var.set_data_template(templ))
else:
cg.add(var.set_data_static(data))
# Generate static array in flash to avoid RAM copy
arr_id = ID(f"{action_id}_data", is_declaration=True, type=cg.uint8)
arr = cg.static_const_array(arr_id, cg.ArrayInitializer(*data))
cg.add(var.set_data_static(arr, len(data)))
return var

View File

@@ -11,28 +11,35 @@ namespace udp {
template<typename... Ts> class UDPWriteAction : public Action<Ts...>, public Parented<UDPComponent> {
public:
void set_data_template(std::function<std::vector<uint8_t>(Ts...)> func) {
this->data_func_ = func;
void set_data_template(std::vector<uint8_t> (*func)(Ts...)) {
this->data_.func = func;
this->static_ = false;
}
void set_data_static(const std::vector<uint8_t> &data) {
this->data_static_ = data;
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;
}
void play(const Ts &...x) override {
if (this->static_) {
this->parent_->send_packet(this->data_static_);
this->parent_->send_packet(this->data_.static_data.ptr, this->data_.static_data.len);
} else {
auto val = this->data_func_(x...);
auto val = this->data_.func(x...);
this->parent_->send_packet(val);
}
}
protected:
bool static_{false};
std::function<std::vector<uint8_t>(Ts...)> data_func_{};
std::vector<uint8_t> data_static_{};
bool static_{true};
union Data {
std::vector<uint8_t> (*func)(Ts...);
struct {
const uint8_t *ptr;
size_t len;
} static_data;
} data_;
};
} // namespace udp

View File

@@ -52,3 +52,25 @@ sensor:
name: "BLE Sensor without Lambda"
service_uuid: "abcd1234-abcd-1234-abcd-abcd12345678"
characteristic_uuid: "abcd1237-abcd-1234-abcd-abcd12345678"
number:
- platform: template
name: "Test Number"
id: test_number
optimistic: true
min_value: 0
max_value: 255
step: 1
button:
# Test ble_write with lambda that references a component (function pointer)
- platform: template
name: "BLE Write Lambda Test"
on_press:
- ble_client.ble_write:
id: test_blec
service_uuid: "abcd1234-abcd-1234-abcd-abcd12345678"
characteristic_uuid: "abcd1235-abcd-1234-abcd-abcd12345678"
value: !lambda |-
uint8_t val = (uint8_t)id(test_number).state;
return std::vector<uint8_t>{0xAA, val, 0xBB};

View File

@@ -37,6 +37,15 @@ canbus:
break;
}
number:
- platform: template
name: "Test Number"
id: test_number
optimistic: true
min_value: 0
max_value: 255
step: 1
button:
- platform: template
name: Canbus Actions
@@ -44,3 +53,7 @@ button:
- canbus.send: "abc"
- canbus.send: [0, 1, 2]
- canbus.send: !lambda return {0, 1, 2};
# Test canbus.send with lambda that references a component (function pointer)
- canbus.send: !lambda |-
uint8_t val = (uint8_t)id(test_number).state;
return std::vector<uint8_t>{0xAA, val, 0xBB};

View File

@@ -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:

View File

@@ -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}

View File

@@ -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};

View File

@@ -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};

View File

@@ -17,3 +17,22 @@ udp:
id: my_udp
data: !lambda |-
return std::vector<uint8_t>{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};