Bring modbus naming in sync with standard (#99285)

This commit is contained in:
jan iversen 2023-09-12 10:59:50 +02:00 committed by GitHub
parent fead9d3a92
commit 71207e112e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 17 deletions

View file

@ -168,11 +168,12 @@ async def async_modbus_setup(
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[
@ -180,29 +181,32 @@ async def async_modbus_setup(
]
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),
@ -405,10 +409,10 @@ class ModbusHub:
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)

View file

@ -566,17 +566,17 @@ SERVICE = "service"
],
)
@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:
@ -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],
}
@ -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,
}