Merge remote-tracking branch 'upstream/dev' into integration

This commit is contained in:
J. Nick Koston
2026-03-26 19:48:45 -10:00
21 changed files with 882 additions and 28 deletions
@@ -0,0 +1,5 @@
from tests.testing_helpers import ComponentManifestOverride
def override_manifest(manifest: ComponentManifestOverride) -> None:
manifest.enable_codegen()
@@ -0,0 +1,122 @@
#include <benchmark/benchmark.h>
#include "esphome/components/fan/fan.h"
namespace esphome::benchmarks {
// Inner iteration count to amortize CodSpeed instrumentation overhead.
static constexpr int kInnerIterations = 2000;
// Minimal Fan for benchmarking — control() is a no-op.
class BenchFan : public fan::Fan {
public:
void configure(const char *name) { this->configure_entity_(name, 0x12345678, 0); }
fan::FanTraits get_traits() override { return this->traits_; }
fan::FanTraits traits_;
protected:
void control(const fan::FanCall & /*call*/) override {}
};
// Helper to create a typical fan device for benchmarks.
// Note: setup() is not called (no preferences backend), so save_state_()
// is effectively a no-op. This benchmarks the call/validation path, not persistence.
static void setup_fan(BenchFan &fan) {
fan.configure("test_fan");
fan.traits_.set_oscillation(true);
fan.traits_.set_speed(true);
fan.traits_.set_supported_speed_count(6);
fan.traits_.set_direction(true);
fan.set_restore_mode(fan::FanRestoreMode::NO_RESTORE);
fan.traits_.set_supported_preset_modes({
"auto",
"sleep",
"nature",
"turbo",
});
}
// --- Fan::publish_state() with speed update ---
// Measures the publish path for a fan reporting state —
// the hot path during fan operation.
static void FanPublish_State(benchmark::State &state) {
BenchFan fan;
setup_fan(fan);
fan.state = true;
fan.direction = fan::FanDirection::FORWARD;
for (auto _ : state) {
for (int i = 0; i < kInnerIterations; i++) {
fan.speed = (i % 6) + 1;
fan.publish_state();
}
benchmark::DoNotOptimize(fan.speed);
}
state.SetItemsProcessed(state.iterations() * kInnerIterations);
}
BENCHMARK(FanPublish_State);
// --- Fan::publish_state() with callback ---
// Measures callback dispatch overhead.
static void FanPublish_WithCallback(benchmark::State &state) {
BenchFan fan;
setup_fan(fan);
fan.state = true;
uint64_t callback_count = 0;
fan.add_on_state_callback([&callback_count]() { callback_count++; });
for (auto _ : state) {
for (int i = 0; i < kInnerIterations; i++) {
fan.speed = (i % 6) + 1;
fan.publish_state();
}
benchmark::DoNotOptimize(callback_count);
}
state.SetItemsProcessed(state.iterations() * kInnerIterations);
}
BENCHMARK(FanPublish_WithCallback);
// --- FanCall::perform() set speed ---
// The most common fan call — adjusting the speed level.
static void FanCall_SetSpeed(benchmark::State &state) {
BenchFan fan;
setup_fan(fan);
fan.state = true;
for (auto _ : state) {
for (int i = 0; i < kInnerIterations; i++) {
int speed = (i % 6) + 1;
fan.make_call().set_speed(speed).perform();
}
benchmark::DoNotOptimize(fan.speed);
}
state.SetItemsProcessed(state.iterations() * kInnerIterations);
}
BENCHMARK(FanCall_SetSpeed);
// --- FanCall::perform() with multiple fields ---
// Exercises the validation path with state, speed, oscillation, and direction.
static void FanCall_MultiField(benchmark::State &state) {
BenchFan fan;
setup_fan(fan);
for (auto _ : state) {
for (int i = 0; i < kInnerIterations; i++) {
auto dir = (i % 2 == 0) ? fan::FanDirection::FORWARD : fan::FanDirection::REVERSE;
int speed = (i % 6) + 1;
fan.make_call().set_state(true).set_speed(speed).set_oscillating(i % 2 == 0).set_direction(dir).perform();
}
benchmark::DoNotOptimize(fan.state);
}
state.SetItemsProcessed(state.iterations() * kInnerIterations);
}
BENCHMARK(FanCall_MultiField);
} // namespace esphome::benchmarks
@@ -0,0 +1 @@
fan:
@@ -0,0 +1,2 @@
*.pcf -text
*.ttf -text
Binary file not shown.
+337
View File
@@ -0,0 +1,337 @@
"""Tests for the font component.
Focuses on verifying that long multi-byte (Chinese/CJK) glyph strings
are correctly processed through the font configuration pipeline.
"""
import functools
from pathlib import Path
from unittest.mock import MagicMock, patch
import pytest
from esphome.components.font import (
CONF_BPP,
CONF_EXTRAS,
CONF_GLYPHSETS,
CONF_IGNORE_MISSING_GLYPHS,
CONF_RAW_GLYPH_ID,
FONT_CACHE,
flatten,
glyph_comparator,
to_code,
validate_font_config,
)
import esphome.config_validation as cv
from esphome.const import (
CONF_FILE,
CONF_GLYPHS,
CONF_ID,
CONF_PATH,
CONF_RAW_DATA_ID,
CONF_SIZE,
CONF_TYPE,
)
FONT_DIR = Path(__file__).parent
FONT_PATH = FONT_DIR / "NotoSans-Regular.ttf"
# 200 unique CJK Unified Ideograph characters (U+4E00..U+4EC7)
CHINESE_200 = "".join(chr(cp) for cp in range(0x4E00, 0x4EC8))
def _file_conf() -> dict:
return {CONF_PATH: str(FONT_PATH), CONF_TYPE: "local"}
def _make_config(
glyphs: list[str],
*,
ignore_missing: bool = False,
size: int = 20,
bpp: int = 1,
extras: list | None = None,
glyphsets: list | None = None,
) -> dict:
"""Build a config dict matching what FONT_SCHEMA produces."""
return {
CONF_FILE: _file_conf(),
CONF_GLYPHS: glyphs,
CONF_GLYPHSETS: glyphsets or [],
CONF_IGNORE_MISSING_GLYPHS: ignore_missing,
CONF_SIZE: size,
CONF_BPP: bpp,
CONF_EXTRAS: extras or [],
}
@pytest.fixture(autouse=True)
def _load_font():
"""Load the test font into FONT_CACHE and clean up afterwards."""
fc = _file_conf()
FONT_CACHE[fc] = FONT_PATH
yield
FONT_CACHE.store.clear()
# ---------- flatten / glyph_comparator helpers ----------
def test_flatten_splits_chinese_string_into_chars():
"""A single string of 200 Chinese characters must become 200 individual chars."""
result = flatten([CHINESE_200])
assert len(result) == 200
assert all(len(c) == 1 for c in result)
assert result[0] == "\u4e00"
assert result[-1] == "\u4ec7"
def test_flatten_multiple_chinese_strings():
"""Multiple glyph strings are concatenated then split correctly."""
s1 = CHINESE_200[:100]
s2 = CHINESE_200[100:]
result = flatten([list(s1), list(s2)])
assert len(result) == 200
def test_glyph_comparator_orders_chinese_by_utf8():
"""glyph_comparator must order CJK characters by their UTF-8 byte sequence."""
chars = list(CHINESE_200[:10])
sorted_chars = sorted(chars, key=functools.cmp_to_key(glyph_comparator))
# CJK block is contiguous and UTF-8 order matches codepoint order here
assert sorted_chars == chars
def test_glyph_comparator_mixed_ascii_and_chinese():
"""ASCII characters sort before CJK characters (lower UTF-8 bytes)."""
assert glyph_comparator("A", "\u4e00") == -1
assert glyph_comparator("\u4e00", "A") == 1
assert glyph_comparator("\u4e00", "\u4e00") == 0
# ---------- validate_font_config ----------
def test_long_chinese_glyphs_raises_missing_error():
"""200 Chinese chars not present in NotoSans must raise Invalid with the correct count."""
config = _make_config([CHINESE_200])
with pytest.raises(cv.Invalid, match=r"missing 200 glyphs"):
validate_font_config(config)
def test_long_chinese_glyphs_error_mentions_overflow():
"""When more than 10 glyphs are missing the error should mention the remainder."""
config = _make_config([CHINESE_200])
with pytest.raises(cv.Invalid, match=r"and 190 more"):
validate_font_config(config)
def test_duplicate_chinese_glyphs_detected():
"""Duplicate CJK characters within a single glyph string must be caught."""
duped = "\u4e00\u4e01\u4e00" # first char repeated
config = _make_config([duped])
with pytest.raises(cv.Invalid, match="duplicate"):
validate_font_config(config)
def test_duplicate_chinese_across_strings():
"""Duplicates across separate glyph strings are also caught."""
config = _make_config(["\u4e00\u4e01", "\u4e01\u4e02"])
with pytest.raises(cv.Invalid, match="duplicate"):
validate_font_config(config)
def test_no_false_duplicates_in_200_unique_chinese():
"""200 unique CJK characters must not trigger the duplicate check."""
config = _make_config([CHINESE_200])
# Should not raise duplicate error — it should reach the missing-glyph check instead
with pytest.raises(cv.Invalid, match="missing"):
validate_font_config(config)
def test_valid_latin_glyphs_pass_validation():
"""Latin characters present in NotoSans-Regular pass validation without error."""
config = _make_config(["ABCabc123"])
result = validate_font_config(config)
assert result is not None
assert result[CONF_SIZE] == 20
def test_long_latin_glyphs_pass_validation():
"""A long string of supported Latin glyphs passes validation."""
# 95 printable ASCII characters that NotoSans supports
latin = "".join(chr(cp) for cp in range(0x21, 0x7F))
config = _make_config([latin])
result = validate_font_config(config)
assert result is not None
def test_mixed_latin_and_chinese_glyphs_error():
"""Mixing valid Latin and invalid Chinese chars reports missing Chinese glyphs."""
chinese_10 = CHINESE_200[:10]
config = _make_config(["ABC", chinese_10])
with pytest.raises(cv.Invalid, match=r"missing 10 glyphs"):
validate_font_config(config)
def test_single_chinese_char_glyph():
"""A single Chinese character is correctly handled as one glyph."""
config = _make_config(["\u4e00"])
with pytest.raises(cv.Invalid, match=r"missing 1 glyph[^s]"):
validate_font_config(config)
def test_chinese_glyphs_as_individual_list_items():
"""Chinese chars provided as separate list items are handled the same as a single string."""
chars_as_list = list(CHINESE_200[:50])
config = _make_config(chars_as_list)
with pytest.raises(cv.Invalid, match=r"missing 50 glyphs"):
validate_font_config(config)
# ---------- YAML parsing ----------
def test_yaml_long_latin_glyphs_parsed_and_validated(tmp_path):
"""200 Latin Extended chars on a single YAML line are parsed intact and pass validation."""
from esphome.yaml_util import load_yaml
latin_long = "".join(chr(cp) for cp in range(0x100, 0x1C8))
yaml_file = tmp_path / "font_test.yaml"
yaml_file.write_text(
f'font:\n - file: "NotoSans-Regular.ttf"\n glyphs: "{latin_long}"\n',
encoding="utf-8",
)
parsed = load_yaml(yaml_file)
raw_glyphs = parsed["font"][0]["glyphs"]
# YAML must preserve every Unicode character on the single line
assert raw_glyphs == latin_long
assert len(raw_glyphs) == 200
# Feed through validate_font_config to confirm all glyphs are accepted
config = _make_config([raw_glyphs])
result = validate_font_config(config)
assert result is not None
@pytest.mark.parametrize(
"glyphs_str",
[
" ABC", # space at start
"AB CD", # space in middle
"ABC ", # space at end
],
ids=["start", "middle", "end"],
)
def test_yaml_space_in_glyphs_preserved(tmp_path, glyphs_str):
"""A space character in a glyphs string must survive YAML round-trip and validation."""
from esphome.yaml_util import load_yaml
yaml_file = tmp_path / "font_test.yaml"
yaml_file.write_text(
f'font:\n - file: "NotoSans-Regular.ttf"\n glyphs: "{glyphs_str}"\n',
encoding="utf-8",
)
parsed = load_yaml(yaml_file)
raw_glyphs = parsed["font"][0]["glyphs"]
assert raw_glyphs == glyphs_str
assert " " in raw_glyphs
# Space and ASCII letters are all in NotoSans — validation must pass
config = _make_config([raw_glyphs])
result = validate_font_config(config)
assert result is not None
# ---------- to_code generation ----------
# 200 unique Latin Extended characters (U+0100..U+01C7), all present in NotoSans
LATIN_LONG = "".join(chr(cp) for cp in range(0x100, 0x1C8))
@pytest.fixture
def mock_cg():
"""Mock all cg codegen functions used by to_code."""
with (
patch("esphome.components.font.cg.add_define") as mock_define,
patch("esphome.components.font.cg.progmem_array") as mock_progmem,
patch("esphome.components.font.cg.static_const_array") as mock_static,
patch("esphome.components.font.cg.new_Pvariable") as mock_new_pvar,
):
mock_progmem.return_value = MagicMock()
mock_static.return_value = MagicMock()
yield {
"add_define": mock_define,
"progmem_array": mock_progmem,
"static_const_array": mock_static,
"new_Pvariable": mock_new_pvar,
}
@pytest.mark.asyncio
async def test_to_code_long_latin_generates_all_glyphs(mock_cg):
"""to_code must generate glyph data for every character in a long Latin string."""
glyph_count = len(LATIN_LONG) # 200
config = _make_config([LATIN_LONG])
config[CONF_ID] = MagicMock()
config[CONF_RAW_DATA_ID] = MagicMock()
config[CONF_RAW_GLYPH_ID] = MagicMock()
await to_code(config)
# USE_FONT define must be emitted
mock_cg["add_define"].assert_any_call("USE_FONT")
# progmem_array receives the combined bitmap data (non-empty)
mock_cg["progmem_array"].assert_called_once()
bitmap_data = mock_cg["progmem_array"].call_args.args[1]
assert len(bitmap_data) > 0
# static_const_array receives one entry per unique glyph
mock_cg["static_const_array"].assert_called_once()
glyph_initializer = mock_cg["static_const_array"].call_args.args[1]
assert len(glyph_initializer) == glyph_count
# new_Pvariable is called with the correct glyph count
mock_cg["new_Pvariable"].assert_called_once()
pvar_args = mock_cg["new_Pvariable"].call_args.args
assert pvar_args[2] == glyph_count # len(glyph_initializer)
assert pvar_args[8] == 1 # bpp
@pytest.mark.asyncio
async def test_to_code_glyph_entries_contain_expected_fields(mock_cg):
"""Each glyph initializer entry must have 7 fields: codepoint, data ptr, advance, offset_x, offset_y, w, h."""
config = _make_config([LATIN_LONG])
config[CONF_ID] = MagicMock()
config[CONF_RAW_DATA_ID] = MagicMock()
config[CONF_RAW_GLYPH_ID] = MagicMock()
await to_code(config)
glyph_initializer = mock_cg["static_const_array"].call_args.args[1]
for entry in glyph_initializer:
assert len(entry) == 7, f"Glyph entry should have 7 fields, got {len(entry)}"
codepoint = entry[0]
assert isinstance(codepoint, int)
assert 0x100 <= codepoint <= 0x1C7
@pytest.mark.asyncio
async def test_to_code_glyphs_sorted_by_utf8(mock_cg):
"""Glyphs in the initializer must be sorted by UTF-8 byte order."""
config = _make_config([LATIN_LONG])
config[CONF_ID] = MagicMock()
config[CONF_RAW_DATA_ID] = MagicMock()
config[CONF_RAW_GLYPH_ID] = MagicMock()
await to_code(config)
glyph_initializer = mock_cg["static_const_array"].call_args.args[1]
codepoints = [entry[0] for entry in glyph_initializer]
assert codepoints == sorted(codepoints)
@@ -0,0 +1,280 @@
"""Tests for light effect name validation."""
from __future__ import annotations
from collections.abc import Generator
from contextvars import Token
import pytest
from esphome import config_validation as cv
from esphome.components.light import (
EffectRef,
_final_validate,
_get_data,
available_effects_str,
find_effect_index,
)
from esphome.components.light.automation import _record_effect_ref
from esphome.config import Config, path_context
from esphome.const import CONF_EFFECT, CONF_EFFECTS, CONF_ID, CONF_NAME
from esphome.core import ID, Lambda
import esphome.final_validate as fv
from esphome.types import ConfigType
def _make_effects(*names: str) -> list[dict[str, dict[str, str]]]:
"""Create a list of effect config dicts from names."""
return [{f"effect_{i}": {CONF_NAME: name}} for i, name in enumerate(names)]
# --- find_effect_index ---
def test_find_effect_index_found() -> None:
effects = _make_effects("Fast Pulse", "Slow Pulse")
assert find_effect_index(effects, "Fast Pulse") == 1
assert find_effect_index(effects, "Slow Pulse") == 2
def test_find_effect_index_case_insensitive() -> None:
effects = _make_effects("Fast Pulse")
assert find_effect_index(effects, "fast pulse") == 1
assert find_effect_index(effects, "FAST PULSE") == 1
def test_find_effect_index_not_found() -> None:
effects = _make_effects("Fast Pulse", "Slow Pulse")
assert find_effect_index(effects, "Missing") is None
def test_find_effect_index_empty() -> None:
assert find_effect_index([], "anything") is None
# --- available_effects_str ---
def test_available_effects_str_multiple() -> None:
effects = _make_effects("Fast Pulse", "Slow Pulse")
assert available_effects_str(effects) == "'Fast Pulse', 'Slow Pulse'"
def test_available_effects_str_single() -> None:
effects = _make_effects("Fast Pulse")
assert available_effects_str(effects) == "'Fast Pulse'"
def test_available_effects_str_empty() -> None:
assert available_effects_str([]) == "none"
# --- _final_validate ---
def _setup_final_validate(
effect_refs: list[EffectRef],
light_configs: list[ConfigType],
declare_ids: list[tuple[ID, list[str | int]]],
) -> Token:
"""Set up CORE.data and fv.full_config for _final_validate tests."""
data = _get_data()
data.effect_refs = effect_refs
full_conf = Config()
full_conf["light"] = light_configs
for id_, path in declare_ids:
full_conf.declare_ids.append((id_, path))
return fv.full_config.set(full_conf)
def test_final_validate_valid_effect() -> None:
"""Valid effect name should not raise."""
light_id = ID("led1", is_declaration=True)
token = _setup_final_validate(
effect_refs=[
EffectRef(
light_id=light_id, effect_name="Fast Pulse", component_path=["esphome"]
),
],
light_configs=[
{CONF_ID: light_id, CONF_EFFECTS: _make_effects("Fast Pulse", "Slow Pulse")}
],
declare_ids=[(light_id, ["light", 0, CONF_ID])],
)
try:
_final_validate({})
finally:
fv.full_config.reset(token)
def test_final_validate_invalid_effect_raises() -> None:
"""Invalid effect name should raise FinalExternalInvalid."""
light_id = ID("led1", is_declaration=True)
token = _setup_final_validate(
effect_refs=[
EffectRef(
light_id=light_id, effect_name="Nonexistent", component_path=["esphome"]
),
],
light_configs=[
{CONF_ID: light_id, CONF_EFFECTS: _make_effects("Fast Pulse", "Slow Pulse")}
],
declare_ids=[(light_id, ["light", 0, CONF_ID])],
)
try:
with pytest.raises(cv.FinalExternalInvalid, match="Nonexistent"):
_final_validate({})
finally:
fv.full_config.reset(token)
def test_final_validate_lists_available_effects() -> None:
"""Error message should list available effects."""
light_id = ID("led1", is_declaration=True)
token = _setup_final_validate(
effect_refs=[
EffectRef(
light_id=light_id, effect_name="Missing", component_path=["esphome"]
),
],
light_configs=[
{CONF_ID: light_id, CONF_EFFECTS: _make_effects("Fast Pulse", "Slow Pulse")}
],
declare_ids=[(light_id, ["light", 0, CONF_ID])],
)
try:
with pytest.raises(cv.FinalExternalInvalid, match="'Fast Pulse', 'Slow Pulse'"):
_final_validate({})
finally:
fv.full_config.reset(token)
def test_final_validate_no_effects_on_light() -> None:
"""Light with no effects should report 'none' as available."""
light_id = ID("led1", is_declaration=True)
token = _setup_final_validate(
effect_refs=[
EffectRef(
light_id=light_id, effect_name="Missing", component_path=["esphome"]
),
],
light_configs=[{CONF_ID: light_id}],
declare_ids=[(light_id, ["light", 0, CONF_ID])],
)
try:
with pytest.raises(cv.FinalExternalInvalid, match="Available effects: none"):
_final_validate({})
finally:
fv.full_config.reset(token)
def test_final_validate_no_refs_is_noop() -> None:
"""No stored refs should pass without error."""
data = _get_data()
data.effect_refs = []
_final_validate({})
def test_final_validate_unknown_light_id_skipped() -> None:
"""Refs to unknown light IDs should be silently skipped."""
data = _get_data()
data.effect_refs = [
EffectRef(
light_id=ID("nonexistent", is_declaration=True),
effect_name="Missing",
component_path=["esphome"],
)
]
full_conf = Config()
token = fv.full_config.set(full_conf)
try:
_final_validate({})
finally:
fv.full_config.reset(token)
def test_final_validate_drains_refs() -> None:
"""Refs should be drained after validation to avoid redundant runs."""
light_id = ID("led1", is_declaration=True)
token = _setup_final_validate(
effect_refs=[
EffectRef(
light_id=light_id, effect_name="Fast Pulse", component_path=["esphome"]
),
],
light_configs=[{CONF_ID: light_id, CONF_EFFECTS: _make_effects("Fast Pulse")}],
declare_ids=[(light_id, ["light", 0, CONF_ID])],
)
try:
_final_validate({})
assert _get_data().effect_refs == []
finally:
fv.full_config.reset(token)
# --- _record_effect_ref ---
@pytest.fixture
def _path_ctx() -> Generator[None]:
"""Set path_context for _record_effect_ref tests."""
token = path_context.set(["esphome"])
yield
path_context.reset(token)
@pytest.mark.usefixtures("_path_ctx")
def test_record_effect_ref_static() -> None:
"""Static effect name should be recorded."""
light_id = ID("led1", is_declaration=True)
config: ConfigType = {CONF_ID: light_id, CONF_EFFECT: "Fast Pulse"}
result = _record_effect_ref(config)
assert result is config
data = _get_data()
assert len(data.effect_refs) == 1
assert data.effect_refs[0].effect_name == "Fast Pulse"
assert data.effect_refs[0].light_id is light_id
assert data.effect_refs[0].component_path == ["esphome"]
@pytest.mark.usefixtures("_path_ctx")
def test_record_effect_ref_skips_lambda() -> None:
"""Lambda effect should not be recorded."""
config: ConfigType = {
CONF_ID: ID("led1", is_declaration=True),
CONF_EFFECT: Lambda("return effect;"),
}
_record_effect_ref(config)
assert _get_data().effect_refs == []
@pytest.mark.usefixtures("_path_ctx")
def test_record_effect_ref_skips_none() -> None:
"""Effect 'None' should not be recorded."""
config: ConfigType = {
CONF_ID: ID("led1", is_declaration=True),
CONF_EFFECT: "None",
}
_record_effect_ref(config)
assert _get_data().effect_refs == []
@pytest.mark.usefixtures("_path_ctx")
def test_record_effect_ref_skips_none_case_insensitive() -> None:
"""Effect 'none' (lowercase) should not be recorded."""
config: ConfigType = {
CONF_ID: ID("led1", is_declaration=True),
CONF_EFFECT: "none",
}
_record_effect_ref(config)
assert _get_data().effect_refs == []
def test_record_effect_ref_skips_no_effect_key() -> None:
"""Config without effect key should be a no-op."""
config: ConfigType = {CONF_ID: ID("led1", is_declaration=True)}
_record_effect_ref(config)
assert _get_data().effect_refs == []
+2 -1
View File
@@ -1 +1,2 @@
*.pcf -text
*.pcf -text
*.ttf -text