[ethernet] Skip setters that pass the default PHY address, MDC, MDIO, clock and SPI interface (#20070)

This commit is contained in:
J. Nick Koston
2026-10-12 09:56:53 -04:00
committed by GitHub
parent 780ccfc462
commit 00872d4336
6 changed files with 128 additions and 17 deletions
@@ -0,0 +1,14 @@
esphome:
name: test
esp32:
board: esp32dev
ethernet:
type: LAN8720
mdc_pin: GPIO4
mdio_pin: GPIO5
phy_addr: 1
clk:
mode: CLK_OUT
pin: GPIO17
@@ -0,0 +1,13 @@
esphome:
name: test
esp32:
board: esp32dev
ethernet:
type: LAN8720
mdc_pin: GPIO23
mdio_pin: GPIO18
clk:
mode: CLK_EXT_IN
pin: GPIO0
@@ -0,0 +1,14 @@
esphome:
name: test
esp32:
board: esp32-s3-devkitc-1
ethernet:
type: W5500
clk_pin: GPIO12
mosi_pin: GPIO11
miso_pin: GPIO13
cs_pin: GPIO10
interrupt_pin: GPIO4
clock_speed: 20MHz
@@ -0,0 +1,49 @@
"""Ethernet setters are skipped when the config matches the C++ initializers."""
from collections.abc import Callable
from pathlib import Path
_MDIO_SETTERS = ("set_phy_addr(", "set_mdc_pin(", "set_mdio_pin(")
def test_rmii_defaults_are_not_emitted(
generate_main: Callable[[str | Path], str],
component_config_path: Callable[[str], Path],
) -> None:
main_cpp = generate_main(component_config_path("rmii_defaults.yaml"))
for setter in (*_MDIO_SETTERS, "set_clk_mode(", "set_clk_pin("):
assert setter not in main_cpp
def test_rmii_custom_values_are_emitted(
generate_main: Callable[[str | Path], str],
component_config_path: Callable[[str], Path],
) -> None:
main_cpp = generate_main(component_config_path("rmii_custom.yaml"))
assert "set_phy_addr(1);" in main_cpp
assert "set_mdc_pin(4);" in main_cpp
assert "set_mdio_pin(5);" in main_cpp
assert "set_clk_mode(::EMAC_CLK_OUT);" in main_cpp
assert "set_clk_pin(17);" in main_cpp
def test_spi2_interface_is_not_emitted(
generate_main: Callable[[str | Path], str],
component_config_path: Callable[[str], Path],
) -> None:
"""ESP32-S3 defaults to spi2, which is the C++ initializer."""
main_cpp = generate_main(component_config_path("spi_s3_own_bus.yaml"))
assert "set_interface(" not in main_cpp
def test_spi3_interface_is_emitted(
generate_main: Callable[[str | Path], str],
component_config_path: Callable[[str], Path],
) -> None:
"""Classic ESP32 defaults to spi3, so the setter is still needed."""
main_cpp = generate_main(component_config_path("spi_own_bus.yaml"))
assert "set_interface(::SPI3_HOST);" in main_cpp