Add tests for register_component_source, remove redundant index check

- Test empty name returns 0
- Test deduplication returns same index
- Test overflow warns and returns 0
- Remove duplicate index==0 check in get_component_log_str() since
  component_source_lookup() already handles it
This commit is contained in:
J. Nick Koston
2026-03-22 21:24:13 -10:00
parent 0ec2a288a3
commit a92281f899
2 changed files with 32 additions and 2 deletions
+1 -2
View File
@@ -275,8 +275,7 @@ void Component::call() {
}
}
const LogString *Component::get_component_log_str() const {
return this->component_source_index_ == 0 ? LOG_STR("<unknown>")
: component_source_lookup(this->component_source_index_);
return component_source_lookup(this->component_source_index_);
}
bool Component::should_warn_of_blocking(uint32_t blocking_time) {
// Convert centisecond threshold to milliseconds for comparison
+31
View File
@@ -1,8 +1,10 @@
import logging
from unittest.mock import Mock
import pytest
from esphome import const, cpp_helpers as ch
from esphome.cpp_helpers import ComponentSourcePool, register_component_source
@pytest.mark.asyncio
@@ -78,3 +80,32 @@ async def test_register_component__with_setup_priority(monkeypatch):
assert add_mock.call_count == 4
app_mock.register_component_.assert_called_with(var)
assert core_mock.component_ids == []
def test_register_component_source_empty_name(monkeypatch):
monkeypatch.setattr(ch, "CORE", Mock(data={}))
assert register_component_source("") == 0
def test_register_component_source_deduplicates(monkeypatch):
monkeypatch.setattr(ch, "CORE", Mock(data={}))
idx1 = register_component_source("wifi")
idx2 = register_component_source("api")
idx3 = register_component_source("wifi")
assert idx1 == 1
assert idx2 == 2
assert idx3 == 1 # deduplicated
def test_register_component_source_overflow_warns(monkeypatch, caplog):
# Pre-fill pool to max
pool = ComponentSourcePool(
sources={f"comp_{i}": i + 1 for i in range(0xFF)},
table_registered=True,
)
monkeypatch.setattr(ch, "CORE", Mock(data={ch._COMPONENT_SOURCE_DOMAIN: pool}))
with caplog.at_level(logging.WARNING):
idx = register_component_source("overflow_component")
assert idx == 0
assert "Too many unique component source names" in caplog.text
assert "overflow_component" in caplog.text