8000 Change default slave id from 0 to 1 in modbus actions by crug80 · Pull Request #142865 · home-assistant/core · GitHub
[go: up one dir, main page]

Skip to content

Change default slave id from 0 to 1 in modbus actions #142865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions homeassistant/components/modbus/modbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ async def async_stop_modbus(event: Event) -> None:

async def async_write_register(service: ServiceCall) -> None:
"""Write Modbus registers."""
slave = 0
slave = 1
if ATTR_UNIT in service.data:
slave = int(float(service.data[ATTR_UNIT]))

Expand All @@ -195,7 +195,7 @@ async def async_write_register(service: ServiceCall) -> None:

async def async_write_coil(service: ServiceCall) -> None:
"""Write Modbus coil."""
slave = 0
slave = 1
if ATTR_UNIT in service.data:
slave = int(float(service.data[ATTR_UNIT]))
if ATTR_SLAVE in service.data:
Expand Down
86 changes: 86 additions & 0 deletions tests/components/modbus/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -1327,3 +1327,89 @@ async def test_check_default_slave(
assert mock_modbus.read_holding_registers.mock_calls
first_call = mock_modbus.read_holding_registers.mock_calls[0]
assert first_call.kwargs["slave"] == expected_slave_value


@pytest.mark.parametrize(
"do_config",
[
{
CONF_NAME: TEST_MODBUS_NAME,
CONF_TYPE: SERIAL,
CONF_BAUDRATE: 9600,
CONF_BYTESIZE: 8,
CONF_METHOD: "rtu",
CONF_PORT: TEST_PORT_SERIAL,
CONF_PARITY: "E",
CONF_STOPBITS: 1,
CONF_SENSORS: [
{
CONF_NAME: "dummy_noslave",
CONF_ADDRESS: 8888,
}
],
},
],
)
@pytest.mark.parametrize(
"do_write",
[
{
DATA: ATTR_VALUE,
VALUE: 15,
SERVICE: SERVICE_WRITE_REGISTER,
FUNC: CALL_TYPE_WRITE_REGISTER,
},
{
DATA: ATTR_STATE,
VALUE: False,
SERVICE: SERVICE_WRITE_COIL,
FUNC: CALL_TYPE_WRITE_COIL,
},
],
)
@pytest.mark.parametrize(
"do_return",
[
{VALUE: ReadResult([0x0001]), DATA: ""},
{VALUE: ExceptionResponse(0x06), DATA: "Pymodbus:"},
{VALUE: ModbusException("fail write_"), DATA: "Pymodbus:"},
],
)
async def test_pb_service_write_no_slave(
hass: HomeAssistant,
do_write,
do_return,
caplog: pytest.LogCaptureFixture,
mock_modbus_with_pymodbus,
) -> None:
"""Run test for service write_register in case of missing slave/unit parameter."""

func_name = {
CALL_TYPE_WRITE_COIL: mock_modbus_with_pymodbus.write_coil,
CALL_TYPE_WRITE_REGISTER: mock_modbus_with_pymodbus.write_register,
}

value_arg_name = {
CALL_TYPE_WRITE_COIL: "value",
CALL_TYPE_WRITE_REGISTER: "value",
}

data = {
ATTR_HUB: TEST_MODBUS_NAME,
ATTR_ADDRESS: 16,
do_write[DATA]: do_write[VALUE],
}
mock_modbus_with_pymodbus.reset_mock()
caplog.clear()
caplog.set_level(logging.DEBUG)
func_name[do_write[FUNC]].return_value = do_return[VALUE]
await hass.services.async_call(DOMAIN, do_write[SERVICE], data, blocking=True)
assert func_name[do_write[FUNC]].called
assert func_name[do_write[FUNC]].call_args.args == (data[ATTR_ADDRESS],)
assert func_name[do_write[FUNC]].call_args.kwargs == {
"slave": 1,
value_arg_name[do_write[FUNC]]: data[do_write[DATA]],
}

if do_return[DATA]:
assert any(message.startswith("Pymodbus:") for message in caplog.messages)
0