From 98aad834077f5938d75fae35d9d4189280323be8 Mon Sep 17 00:00:00 2001 From: clydebarrow <2366188+clydebarrow@users.noreply.github.com> Date: Mon, 19 Oct 2026 09:01:09 +1100 Subject: [PATCH] [packet_interface] Check the layers in the link schema Co-Authored-By: Claude Opus 5.5 --- .../components/packet_interface/__init__.py | 35 +++++++++---------- tests/unit_tests/test_packet_interface.py | 12 ++----- 2 files changed, 19 insertions(+), 28 deletions(-) diff --git a/esphome/components/packet_interface/__init__.py b/esphome/components/packet_interface/__init__.py index 362deb51126..ac51b7eb47a 100644 --- a/esphome/components/packet_interface/__init__.py +++ b/esphome/components/packet_interface/__init__.py @@ -54,16 +54,9 @@ def AUTO_LOAD(config: list[ConfigType] | None = None) -> list[str]: return sorted(loads) -def _final_validate(config: list[ConfigType]) -> None: - for index, interface in enumerate(config): - try: - check_session(interface) - except cv.Invalid as err: - err.prepend([index]) - raise - - -FINAL_VALIDATE_SCHEMA = _final_validate +def _validate_layers(config: ConfigType) -> ConfigType: + check_session(config) + return config def packet_interface_schema( @@ -72,15 +65,19 @@ def packet_interface_schema( max_packet_size_limit: int = 4096, ) -> cv.Schema: """Schema for a link platform, with the layers that can be stacked on it.""" - return cv.Schema( - { - cv.GenerateID(): cv.declare_id(class_), - cv.Optional( - CONF_MAX_PACKET_SIZE, default=default_max_packet_size - ): cv.int_range(16, max_packet_size_limit), - **LINK_LAYER_SCHEMAS, - } - ).extend(cv.COMPONENT_SCHEMA) + return ( + cv.Schema( + { + cv.GenerateID(): cv.declare_id(class_), + cv.Optional( + CONF_MAX_PACKET_SIZE, default=default_max_packet_size + ): cv.int_range(16, max_packet_size_limit), + **LINK_LAYER_SCHEMAS, + } + ) + .extend(cv.COMPONENT_SCHEMA) + .add_extra(_validate_layers) + ) async def new_packet_interface(config: ConfigType, *args: Any) -> MockObj: diff --git a/tests/unit_tests/test_packet_interface.py b/tests/unit_tests/test_packet_interface.py index 16a203c38a5..f7fc8d4b5a2 100644 --- a/tests/unit_tests/test_packet_interface.py +++ b/tests/unit_tests/test_packet_interface.py @@ -166,15 +166,9 @@ def test_rolling_code_requires_session(setup_core) -> None: """The rolling code needs the session's keep-alives, so the peer sends this device's boot number back.""" rolling = {CONF_ENCRYPTION: {"key": "secret", "rolling_code_enable": True}} with pytest.raises(cv.Invalid, match="session"): - packet_interface.FINAL_VALIDATE_SCHEMA( - [uart_packet_interface.CONFIG_SCHEMA(rolling)] - ) - packet_interface.FINAL_VALIDATE_SCHEMA( - [ - uart_packet_interface.CONFIG_SCHEMA({**rolling, CONF_SESSION: None}), - uart_packet_interface.CONFIG_SCHEMA({CONF_ENCRYPTION: "secret"}), - ] - ) + uart_packet_interface.CONFIG_SCHEMA(rolling) + uart_packet_interface.CONFIG_SCHEMA({**rolling, CONF_SESSION: None}) + uart_packet_interface.CONFIG_SCHEMA({CONF_ENCRYPTION: "secret"}) @pytest.mark.parametrize(