[api] Keep Home Assistant action fields in a flash table (#20237)

This commit is contained in:
J. Nick Koston
2026-10-07 10:09:49 -10:00
committed by GitHub
parent 3296bccd3c
commit 6db2e781ce
7 changed files with 268 additions and 262 deletions
@@ -0,0 +1,33 @@
"""Tests for the Home Assistant action field tables."""
from collections.abc import Callable
from pathlib import Path
import pytest
from esphome.components.api import DATA_FIELDS_SCHEMA, VARIABLES_FIELDS_SCHEMA
import esphome.config_validation as cv
CONFIG = "tests/component_tests/api/test_homeassistant_fields.yaml"
def test_field_tables(generate_main: Callable[[str | Path], str]) -> None:
"""Strings are single PROGMEM arrays on ESP8266; only tables without lambdas are shared."""
main_cpp = generate_main(CONFIG)
assert main_cpp.count('PROGMEM = "message";') == 1
assert main_cpp.count('PROGMEM = "notify.notify";') == 1
assert 'ESPHOME_F("message")' not in main_cpp
assert main_cpp.count("(api_apiserver_id, false, ha_action_fields, 1, 0, 0);") == 2
assert "(api_apiserver_id, true, ha_action_fields_2, 1, 0, 0);" in main_cpp
assert "(api_apiserver_id, true, ha_action_fields_3, 1, 0, 0);" in main_cpp
@pytest.mark.parametrize("schema", [DATA_FIELDS_SCHEMA, VARIABLES_FIELDS_SCHEMA])
def test_field_map_limit(schema: cv.Schema) -> None:
"""Each map is counted in a uint8_t, so 256 entries fail validation."""
schema({f"key{i}": "value" for i in range(255)})
with pytest.raises(cv.Invalid):
schema({f"key{i}": "value" for i in range(256)})
@@ -0,0 +1,29 @@
esphome:
name: test
on_boot:
then:
- homeassistant.action:
action: notify.notify
data:
message: Booted
- homeassistant.action:
action: notify.notify
data:
message: Booted
- homeassistant.event:
event: esphome.booted
data:
message: !lambda 'static int n = 0; return ++n;'
- homeassistant.event:
event: esphome.booted
data:
message: !lambda 'static int n = 0; return ++n;'
esp8266:
board: esp01_1m
wifi:
ssid: SomeNetwork
password: SomePassword
api:
@@ -17,7 +17,7 @@ def test_plain_string_with_return_is_compiled_as_lambda_with_warning(
with caplog.at_level(logging.WARNING):
main_cpp = generate_main(CONFIG)
assert main_cpp.count('add_variable(ESPHOME_F("lambda_var"), []() {') == 2
assert main_cpp.count('from_lambda("lambda_var", []() {') == 2
assert "return millis();" in main_cpp
# The source text must not be sent as a static string value.
assert '"return millis();"' not in main_cpp
@@ -28,16 +28,11 @@ def test_static_string_is_kept_as_static_value(
generate_main: Callable[[str | Path], str],
caplog: pytest.LogCaptureFixture,
) -> None:
"""A static string stays static, PROGMEM wrapped, with no warning."""
"""A static string stays a constant table entry, with no warning."""
with caplog.at_level(logging.WARNING):
main_cpp = generate_main(CONFIG)
assert (
main_cpp.count(
'add_variable(ESPHOME_F("static_var"), ESPHOME_F("static value"));'
)
== 2
)
assert main_cpp.count('{"static_var", "static value", nullptr}') == 2
assert "static value" not in caplog.text
@@ -49,7 +44,7 @@ def test_static_id_value_stays_literal_with_hint(
with caplog.at_level(logging.WARNING):
main_cpp = generate_main(CONFIG)
assert 'ESPHOME_F("id(test_sensor).state")' in main_cpp
assert '{"hint_var", "id(test_sensor).state", nullptr}' in main_cpp
assert "sent as literal text" in caplog.text
@@ -59,5 +54,5 @@ def test_explicit_lambda_tag_is_compiled_as_lambda(
"""A !lambda value keeps working unchanged."""
main_cpp = generate_main(CONFIG)
assert 'add_variable(ESPHOME_F("tagged_var"), []() {' in main_cpp
assert 'from_lambda("tagged_var", []() {' in main_cpp
assert "return App.get_name();" in main_cpp