Skip to content

Commit

Permalink
Bring modbus naming in sync with standard (#99285)
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen authored Sep 12, 2023
1 parent fead9d3 commit 71207e1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
28 changes: 16 additions & 12 deletions homeassistant/components/modbus/modbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,41 +168,45 @@ async def async_stop_modbus(event: Event) -> None:

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

if ATTR_SLAVE in service.data:
unit = int(float(service.data[ATTR_SLAVE]))
slave = int(float(service.data[ATTR_SLAVE]))
address = int(float(service.data[ATTR_ADDRESS]))
value = service.data[ATTR_VALUE]
hub = hub_collect[
service.data[ATTR_HUB] if ATTR_HUB in service.data else DEFAULT_HUB
]
if isinstance(value, list):
await hub.async_pb_call(
unit, address, [int(float(i)) for i in value], CALL_TYPE_WRITE_REGISTERS
slave,
address,
[int(float(i)) for i in value],
CALL_TYPE_WRITE_REGISTERS,
)
else:
await hub.async_pb_call(
unit, address, int(float(value)), CALL_TYPE_WRITE_REGISTER
slave, address, int(float(value)), CALL_TYPE_WRITE_REGISTER
)

async def async_write_coil(service: ServiceCall) -> None:
"""Write Modbus coil."""
unit = 0
slave = 0
if ATTR_UNIT in service.data:
unit = int(float(service.data[ATTR_UNIT]))
slave = int(float(service.data[ATTR_UNIT]))
if ATTR_SLAVE in service.data:
unit = int(float(service.data[ATTR_SLAVE]))
slave = int(float(service.data[ATTR_SLAVE]))
address = service.data[ATTR_ADDRESS]
state = service.data[ATTR_STATE]
hub = hub_collect[
service.data[ATTR_HUB] if ATTR_HUB in service.data else DEFAULT_HUB
]
if isinstance(state, list):
await hub.async_pb_call(unit, address, state, CALL_TYPE_WRITE_COILS)
await hub.async_pb_call(slave, address, state, CALL_TYPE_WRITE_COILS)
else:
await hub.async_pb_call(unit, address, state, CALL_TYPE_WRITE_COIL)
await hub.async_pb_call(slave, address, state, CALL_TYPE_WRITE_COIL)

for x_write in (
(SERVICE_WRITE_REGISTER, async_write_register, ATTR_VALUE, cv.positive_int),
Expand Down Expand Up @@ -405,10 +409,10 @@ def pb_connect(self) -> bool:
return True

def pb_call(
self, unit: int | None, address: int, value: int | list[int], use_call: str
self, slave: int | None, address: int, value: int | list[int], use_call: str
) -> ModbusResponse | None:
"""Call sync. pymodbus."""
kwargs = {"slave": unit} if unit else {}
kwargs = {"slave": slave} if slave else {}
entry = self._pb_request[use_call]
try:
result: ModbusResponse = entry.func(address, value, **kwargs)
Expand Down
10 changes: 5 additions & 5 deletions tests/components/modbus/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,17 +566,17 @@ async def test_config_modbus(
],
)
@pytest.mark.parametrize(
"do_unit",
"do_slave",
[
ATTR_UNIT,
ATTR_SLAVE,
ATTR_UNIT,
],
)
async def test_pb_service_write(
hass: HomeAssistant,
do_write,
do_return,
do_unit,
do_slave,
caplog: pytest.LogCaptureFixture,
mock_modbus_with_pymodbus,
) -> None:
Expand All @@ -591,7 +591,7 @@ async def test_pb_service_write(

data = {
ATTR_HUB: TEST_MODBUS_NAME,
do_unit: 17,
do_slave: 17,
ATTR_ADDRESS: 16,
do_write[DATA]: do_write[VALUE],
}
Expand Down Expand Up @@ -932,7 +932,7 @@ async def test_write_no_client(hass: HomeAssistant, mock_modbus) -> None:

data = {
ATTR_HUB: TEST_MODBUS_NAME,
ATTR_UNIT: 17,
ATTR_SLAVE: 17,
ATTR_ADDRESS: 16,
ATTR_STATE: True,
}
Expand Down

0 comments on commit 71207e1

Please sign in to comment.