[modbus] Only apply turnaround delay after broadcasts (#17209)

This commit is contained in:
Jonathan Swoboda
2026-06-25 11:38:34 -04:00
committed by GitHub
parent 18c7f60410
commit 1d5490fd91
4 changed files with 93 additions and 5 deletions
@@ -0,0 +1,56 @@
esphome:
name: uart-mock-modbus-bcast
host:
api:
logger:
level: VERBOSE
external_components:
- source:
type: local
path: EXTERNAL_COMPONENT_PATH
# Dummy uart entry to satisfy modbus's DEPENDENCIES = ["uart"]
# The actual UART bus used is the uart_mock component below
uart:
baud_rate: 115200
port: /dev/null
# No on_tx injection: a broadcast (address 0) gets no reply on a real bus.
uart_mock:
- id: virtual_uart
baud_rate: 9600
auto_start: true
debug:
modbus:
- uart_id: virtual_uart
id: virtual_modbus
role: client
send_wait_time: 200ms
turnaround_time: 10ms
modbus_controller:
- address: 0
modbus_id: virtual_modbus
update_interval: 60s
id: modbus_controller_bcast
number:
- platform: modbus_controller
modbus_controller_id: modbus_controller_bcast
id: bcast_write
name: "bcast_write"
address: 0x01
register_type: holding
value_type: U_WORD
min_value: 0
max_value: 65535
interval:
- interval: 400ms
then:
- number.set:
id: bcast_write
value: 42
@@ -330,3 +330,28 @@ async def test_uart_mock_modbus_server_controller_multiple(
await tracker.setup_and_start_scenario(client)
await tracker.await_all(futures)
_assert_no_modbus_errors(error_log_lines, warning_log_lines)
@pytest.mark.asyncio
async def test_uart_mock_modbus_broadcast(
yaml_config: str,
run_compiled: RunCompiledFunction,
api_client_connected: APIClientConnectedFactory,
) -> None:
"""Test that broadcast writes (address 0) don't wait for a response.
A controller at address 0 sends broadcast writes that get no reply. The
client must not arm the response timeout for them: otherwise every write
blocks for send_wait_time and logs a spurious "no response from 0" warning.
"""
line_callback, error_log_lines, warning_log_lines = _make_modbus_line_callback()
async with (
run_compiled(yaml_config, line_callback=line_callback),
api_client_connected(),
):
# Several broadcast writes fire on the 400ms interval; send_wait_time is
# 200ms, so the old behaviour would have warned on each one by now.
await asyncio.sleep(3.0)
_assert_no_modbus_errors(error_log_lines, warning_log_lines)