[uart] Keep constant switch, button and write payloads in flash (#20082)

This commit is contained in:
J. Nick Koston
2026-10-12 01:16:34 +00:00
committed by GitHub
parent ed6ab4565d
commit 0cd239dfd1
14 changed files with 137 additions and 80 deletions
@@ -0,0 +1,34 @@
---
esphome:
name: test
on_boot:
then:
- uart.write: [0x01, 0x02, 0x03]
- uart.write: !lambda return {0x09};
- uart.write: ""
esp32:
board: esp32dev
uart:
tx_pin: 17
rx_pin: 16
baud_rate: 9600
switch:
- platform: uart
id: shared_switch
name: Shared Switch
data: [0x01, 0x02, 0x03]
- platform: uart
id: on_off_switch
name: On Off Switch
data:
turn_on: "ON"
turn_off: [0x04]
button:
- platform: uart
id: shared_button
name: Shared Button
data: [0x01, 0x02, 0x03]
@@ -0,0 +1,28 @@
"""Tests for UART constant payloads in shared PROGMEM tables."""
from collections.abc import Callable
from pathlib import Path
import re
def test_constant_payloads_share_progmem_tables(
generate_main: Callable[[str | Path], str],
component_config_path: Callable[[str], Path],
) -> None:
"""Equal payloads share one table across switch, button and write action."""
main_cpp = generate_main(component_config_path("payload_tables.yaml"))
tables = dict(
re.findall(
r"static constexpr uint8_t (\w+)\[\] PROGMEM = (\{[^}]*\});", main_cpp
)
)
shared = next(k for k, v in tables.items() if v == "{0x01, 0x02, 0x03}")
assert sorted(tables.values()) == sorted(
["{0x01, 0x02, 0x03}", "{0x4F, 0x4E}", "{0x04}"]
)
assert f"shared_switch->set_data_on({shared}, 3);" in main_cpp
assert f"shared_button->set_data({shared}, 3);" in main_cpp
assert f"set_data_static({shared}, 3);" in main_cpp
assert "set_data_static(nullptr, 0);" in main_cpp
assert "set_data_template(" in main_cpp