mirror of
https://github.com/esphome/esphome.git
synced 2026-10-07 19:44:08 +00:00
[uart] Skip the parity setter when it matches the default (#20069)
This commit is contained in:
@@ -402,7 +402,9 @@ async def to_code(config):
|
||||
cg.add(var.set_clock_source(UART_CLOCK_SOURCES[clock_source]))
|
||||
cg.add(var.set_stop_bits(config[CONF_STOP_BITS]))
|
||||
cg.add(var.set_data_bits(config[CONF_DATA_BITS]))
|
||||
cg.add(var.set_parity(config[CONF_PARITY]))
|
||||
# Skip the setter when the config matches the C++ initializer (UART_CONFIG_PARITY_NONE).
|
||||
if (parity := config[CONF_PARITY]) != "NONE":
|
||||
cg.add(var.set_parity(parity))
|
||||
|
||||
if CONF_DEBUG in config:
|
||||
await debug_to_code(config[CONF_DEBUG], var)
|
||||
|
||||
@@ -217,7 +217,7 @@ class UARTComponent {
|
||||
uint32_t baud_rate_{0};
|
||||
uint8_t stop_bits_{0};
|
||||
uint8_t data_bits_{0};
|
||||
UARTParityOptions parity_{UART_CONFIG_PARITY_NONE};
|
||||
UARTParityOptions parity_{UART_CONFIG_PARITY_NONE}; // Must match the parity check in __init__.py
|
||||
#ifdef USE_UART_DEBUGGER
|
||||
CallbackManager<void(UARTDirection, uint8_t)> debug_callback_{};
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
---
|
||||
esphome:
|
||||
name: test
|
||||
|
||||
esp32:
|
||||
board: esp32dev
|
||||
|
||||
uart:
|
||||
- id: default_uart
|
||||
tx_pin: GPIO17
|
||||
rx_pin: GPIO16
|
||||
baud_rate: 9600
|
||||
@@ -0,0 +1,13 @@
|
||||
---
|
||||
esphome:
|
||||
name: test
|
||||
|
||||
esp32:
|
||||
board: esp32dev
|
||||
|
||||
uart:
|
||||
- id: even_uart
|
||||
tx_pin: GPIO17
|
||||
rx_pin: GPIO16
|
||||
baud_rate: 9600
|
||||
parity: EVEN
|
||||
@@ -0,0 +1,24 @@
|
||||
"""Tests for UART parity code generation."""
|
||||
|
||||
from collections.abc import Callable
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def test_default_parity_is_not_emitted(
|
||||
generate_main: Callable[[str | Path], str],
|
||||
component_config_path: Callable[[str], Path],
|
||||
) -> None:
|
||||
"""Parity NONE is the C++ initializer, so the setter is skipped."""
|
||||
main_cpp = generate_main(component_config_path("parity_default.yaml"))
|
||||
|
||||
assert "default_uart->set_parity(" not in main_cpp
|
||||
|
||||
|
||||
def test_custom_parity_is_emitted(
|
||||
generate_main: Callable[[str | Path], str],
|
||||
component_config_path: Callable[[str], Path],
|
||||
) -> None:
|
||||
"""A non default parity still reaches the setter."""
|
||||
main_cpp = generate_main(component_config_path("parity_even.yaml"))
|
||||
|
||||
assert "even_uart->set_parity(uart::UART_CONFIG_PARITY_EVEN);" in main_cpp
|
||||
Reference in New Issue
Block a user