Cover the report deprecation warning and lazy validator paths

This commit is contained in:
J. Nick Koston
2026-08-13 21:21:17 -05:00
parent 663f462877
commit 10d232dc40
@@ -1,9 +1,11 @@
"""Test get_base_entity_object_id function matches C++ behavior."""
from collections.abc import Callable, Generator
import logging
from pathlib import Path
import re
from typing import Any
from unittest.mock import patch
import pytest
@@ -21,11 +23,13 @@ from esphome.const import (
)
from esphome.core import CORE, ID, entity_helpers
from esphome.core.entity_helpers import (
_check_report_deprecation,
_register_string,
_setup_entity_impl,
entity_duplicate_validator,
finalize_entity_strings,
get_base_entity_object_id,
lazy_load_validator,
register_device_class,
register_icon,
register_unit_of_measurement,
@@ -1236,3 +1240,45 @@ async def test_finalize_comment_sanitization(
# Newline must be replaced to prevent breaking out of comment
assert "\n" not in comment_line
assert "INJECTED_CODE" in comment_line # still visible but safe in comment
@pytest.mark.parametrize(
("value", "warns"),
[
("coordinator", True),
("enable", True),
("force", False),
("default", False),
],
)
def test_check_report_deprecation(
value: str, warns: bool, caplog: pytest.LogCaptureFixture
) -> None:
"""Deprecated zigbee report options warn; the value always passes through."""
with caplog.at_level(logging.WARNING):
assert _check_report_deprecation(value) == value
assert ("deprecated" in caplog.text) is warns
def test_lazy_load_validator_defers_import() -> None:
"""The validator no-ops without importing unless the component is loaded."""
validator = lazy_load_validator("zigbee", "validate_binary_sensor")
config = {CONF_NAME: "test"}
original = CORE.loaded_integrations
CORE.loaded_integrations = set()
try:
with patch(
"esphome.core.entity_helpers.importlib.import_module"
) as import_mock:
assert validator(config) is config
import_mock.assert_not_called()
CORE.loaded_integrations.add("zigbee")
delegate = import_mock.return_value.validate_binary_sensor
delegate.return_value = {CONF_NAME: "validated"}
assert validator(config) == {CONF_NAME: "validated"}
import_mock.assert_called_once_with("esphome.components.zigbee")
delegate.assert_called_once_with(config)
finally:
CORE.loaded_integrations = original