From aae7b6e466f824b4bc068a7b274f351a5dddfe98 Mon Sep 17 00:00:00 2001 From: clydebarrow <2366188+clydebarrow@users.noreply.github.com> Date: Tue, 20 Oct 2026 09:35:03 +1100 Subject: [PATCH] [packet_interface] Name a peer in the error for a link used with peers The link may have only one peer, so the error no longer says several. Co-Authored-By: Claude Opus 5.5 --- esphome/components/packet_interface/__init__.py | 8 +++++--- tests/unit_tests/test_packet_interface.py | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/esphome/components/packet_interface/__init__.py b/esphome/components/packet_interface/__init__.py index e1894f34144..8f456525ad5 100644 --- a/esphome/components/packet_interface/__init__.py +++ b/esphome/components/packet_interface/__init__.py @@ -115,14 +115,16 @@ def interface_layers(interface_id: ID) -> ConfigType | None: def final_validate_interface(key: str) -> Callable[[ConfigType], ConfigType]: - """Final validation for an application: a link with several peers must be used through one of the peers.""" + """Final validation for an application: a link with peers must be used through one of the peers.""" def validator(config: ConfigType) -> ConfigType: interface_id: ID = config[key] interface = interface_layers(interface_id) - if interface is not None and CONF_PEERS in interface.get(CONF_ADDRESSING, {}): + if interface is not None and ( + peers := interface.get(CONF_ADDRESSING, {}).get(CONF_PEERS) + ): raise cv.Invalid( - f"'{interface_id.id}' has several peers, so use the id of one of them", + f"'{interface_id.id}' has peers, so use the id of one of them, such as '{peers[0][CONF_ID].id}'", path=[key], ) return config diff --git a/tests/unit_tests/test_packet_interface.py b/tests/unit_tests/test_packet_interface.py index 3209346c89c..97352002c4b 100644 --- a/tests/unit_tests/test_packet_interface.py +++ b/tests/unit_tests/test_packet_interface.py @@ -368,8 +368,8 @@ def test_addressing_own_address_rejected(setup_core, addressing: dict) -> None: uart_packet_interface.CONFIG_SCHEMA({CONF_ADDRESSING: addressing}) -def test_application_needs_peer_id_with_several_peers(setup_core) -> None: - """An application must use one peer's id, not the id of a link with several peers.""" +def test_application_needs_peer_id_with_peers(setup_core) -> None: + """An application must use one peer's id, not the id of a link with peers.""" validator = packet_interface.final_validate_interface("packet_interface") token = fv.full_config.set( { @@ -380,7 +380,9 @@ def test_application_needs_peer_id_with_several_peers(setup_core) -> None: } ) try: - with pytest.raises(cv.Invalid, match="several peers"): + with pytest.raises( + cv.Invalid, match="has peers, so use the id of one of them, such as 'a'" + ): validator({"packet_interface": ID("bus")}) assert validator({"packet_interface": ID("single")}) assert validator({"packet_interface": ID("a")})