From 71207e112ece14161ee65bdf8b9ff937697b0b16 Mon Sep 17 00:00:00 2001 From: jan iversen Date: Tue, 12 Sep 2023 10:59:50 +0200 Subject: [PATCH] Bring modbus naming in sync with standard (#99285) --- homeassistant/components/modbus/modbus.py | 28 +++++++++++++---------- tests/components/modbus/test_init.py | 10 ++++---- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/homeassistant/components/modbus/modbus.py b/homeassistant/components/modbus/modbus.py index 238df4466c432c..a503b71593c5aa 100644 --- a/homeassistant/components/modbus/modbus.py +++ b/homeassistant/components/modbus/modbus.py @@ -168,11 +168,12 @@ 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[ @@ -180,29 +181,32 @@ async def async_write_register(service: ServiceCall) -> None: ] 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 @@ 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) diff --git a/tests/components/modbus/test_init.py b/tests/components/modbus/test_init.py index 6f88a4b7399c68..5d419ed28d587e 100644 --- a/tests/components/modbus/test_init.py +++ b/tests/components/modbus/test_init.py @@ -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: @@ -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, }