[uart_mux] New component to share a UART between a CDC-ACM bridge and local consumers (#19066)

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
Keith Burzinski
2026-09-15 20:32:15 -05:00
committed by GitHub
co-authored by Claude Fable 5.1 J. Nick Koston
parent ec1d8fa953
commit d824a32ef1
12 changed files with 420 additions and 0 deletions
@@ -0,0 +1,42 @@
"""Tests for the uart_mux component's final validation."""
import pytest
from esphome import config_validation as cv
from esphome.const import CONF_ID, PlatformFramework
from esphome.core import ID
from esphome.types import ConfigType
from tests.component_tests.types import SetCoreConfigCallable
CONF_BRIDGE_ID = "bridge_id"
def _set_esp32_s3(set_core_config: SetCoreConfigCallable) -> None:
from esphome.components.esp32 import KEY_VARIANT, VARIANT_ESP32S3
set_core_config(
PlatformFramework.ESP32_IDF, platform_data={KEY_VARIANT: VARIANT_ESP32S3}
)
def _mux_conf(mux_id: str, bridge_id: str) -> ConfigType:
return {CONF_ID: ID(mux_id), CONF_BRIDGE_ID: ID(bridge_id)}
def test_accepts_one_mux_per_bridge(set_core_config: SetCoreConfigCallable) -> None:
_set_esp32_s3(set_core_config)
from esphome.components import uart_mux
uart_mux._final_validate(_mux_conf("mux_0", "bridge_0"))
uart_mux._final_validate(_mux_conf("mux_1", "bridge_1"))
def test_rejects_two_muxes_on_one_bridge(
set_core_config: SetCoreConfigCallable,
) -> None:
_set_esp32_s3(set_core_config)
from esphome.components import uart_mux
uart_mux._final_validate(_mux_conf("mux_0", "bridge_0"))
with pytest.raises(cv.Invalid, match="already routed by another 'uart_mux'"):
uart_mux._final_validate(_mux_conf("mux_1", "bridge_0"))
+44
View File
@@ -0,0 +1,44 @@
tinyusb:
id: tinyusb_test
on_mount:
- uart_mux.select_bridge: mux_0
on_unmount:
- uart_mux.select_local: mux_0
usb_manufacturer_str: ESPHomeTestManufacturer
usb_product_id: 0x1234
usb_product_str: ESPHomeTestProduct
usb_vendor_id: 0x2345
uart:
- id: uart_0
tx_pin: 14
rx_pin: 13
baud_rate: 115200
usb_cdc_acm:
interfaces:
- id: cdc_acm_1
bridge:
- platform: cdc_acm_uart
id: bridge_0
uart_id: uart_0
usb_cdc_acm_id: cdc_acm_1
uart_mux:
- id: mux_0
bridge_id: bridge_0
initial_route: local
interval:
- interval: 60s
then:
- if:
condition:
uart_mux.is_local: mux_0
then:
- lambda: |-
uint8_t byte;
if (id(mux_0).available() && id(mux_0).read_byte(&byte)) {
id(mux_0).write_byte(byte);
}
@@ -0,0 +1,2 @@
packages:
uart_mux: !include common.yaml
@@ -0,0 +1,7 @@
# ESP32-S2 has no USB_SERIAL_JTAG, so the logger defaults to USB_CDC, which shares
# the USB OTG peripheral with tinyusb. Use a hardware UART for logging instead.
logger:
hardware_uart: UART0
packages:
uart_mux: !include common.yaml
@@ -0,0 +1,2 @@
packages:
uart_mux: !include common.yaml