From 72a71759648764849165196f9c0254da537e71f7 Mon Sep 17 00:00:00 2001 From: jan iversen Date: Fri, 19 Jul 2024 20:13:48 +0200 Subject: [PATCH] remove kwargs client. --- pymodbus/client/base.py | 6 ++++++ pymodbus/client/serial.py | 4 +++- pymodbus/client/tcp.py | 4 +++- pymodbus/client/udp.py | 4 +++- test/framers/test_old_framers.py | 3 ++- test/sub_client/test_client.py | 28 +++++++++++++++++++--------- 6 files changed, 36 insertions(+), 13 deletions(-) diff --git a/pymodbus/client/base.py b/pymodbus/client/base.py index 51602641b0..70a36e1a18 100644 --- a/pymodbus/client/base.py +++ b/pymodbus/client/base.py @@ -62,10 +62,13 @@ def __init__( # pylint: disable=too-many-arguments no_resend_on_retry: bool = False, comm_type: CommType | None = None, source_address: tuple[str, int] | None = None, + comm_params: CommParams | None = None, **kwargs: Any, ) -> None: """Initialize a client instance.""" ModbusClientMixin.__init__(self) # type: ignore[arg-type] + if comm_params: + self.comm_params = comm_params self.ctx = ModbusClientProtocol( framer, CommParams( @@ -276,10 +279,13 @@ def __init__( # pylint: disable=too-many-arguments no_resend_on_retry: bool = False, comm_type: CommType | None = None, source_address: tuple[str, int] | None = None, + comm_params: CommParams | None = None, **kwargs: Any, ) -> None: """Initialize a client instance.""" ModbusClientMixin.__init__(self) # type: ignore[arg-type] + if comm_params: + self.comm_params = comm_params self.comm_params = CommParams( comm_type=comm_type, comm_name="comm", diff --git a/pymodbus/client/serial.py b/pymodbus/client/serial.py index 2972bcde0e..e88c8f1966 100644 --- a/pymodbus/client/serial.py +++ b/pymodbus/client/serial.py @@ -9,7 +9,7 @@ from pymodbus.exceptions import ConnectionException from pymodbus.framer import FramerType from pymodbus.logging import Log -from pymodbus.transport import CommType +from pymodbus.transport import CommParams, CommType from pymodbus.utilities import ModbusTransactionState @@ -90,6 +90,7 @@ def __init__( bytesize=bytesize, parity=parity, stopbits=stopbits, + comm_params = CommParams(), **kwargs, ) @@ -167,6 +168,7 @@ def __init__( bytesize=bytesize, parity=parity, stopbits=stopbits, + comm_params = CommParams(), **kwargs, ) self.socket: serial.Serial | None = None diff --git a/pymodbus/client/tcp.py b/pymodbus/client/tcp.py index e6ff7bf1f3..65137dcd70 100644 --- a/pymodbus/client/tcp.py +++ b/pymodbus/client/tcp.py @@ -10,7 +10,7 @@ from pymodbus.exceptions import ConnectionException from pymodbus.framer import FramerType from pymodbus.logging import Log -from pymodbus.transport import CommType +from pymodbus.transport import CommParams, CommType class AsyncModbusTcpClient(ModbusBaseClient): @@ -72,6 +72,7 @@ def __init__( framer, host=host, port=port, + comm_params = CommParams(), **kwargs, ) @@ -138,6 +139,7 @@ def __init__( framer, host=host, port=port, + comm_params = CommParams(), **kwargs, ) self.params.source_address = source_address diff --git a/pymodbus/client/udp.py b/pymodbus/client/udp.py index 4d71e240e4..403af189b7 100644 --- a/pymodbus/client/udp.py +++ b/pymodbus/client/udp.py @@ -8,7 +8,7 @@ from pymodbus.exceptions import ConnectionException from pymodbus.framer import FramerType from pymodbus.logging import Log -from pymodbus.transport import CommType +from pymodbus.transport import CommParams, CommType DGRAM_TYPE = socket.SOCK_DGRAM @@ -68,6 +68,7 @@ def __init__( comm_type=CommType.UDP, host=host, port=port, + comm_params = CommParams(), **kwargs, ) self.source_address = source_address @@ -135,6 +136,7 @@ def __init__( port=port, host=host, comm_type=CommType.UDP, + comm_params = CommParams(), **kwargs, ) self.params.source_address = source_address diff --git a/test/framers/test_old_framers.py b/test/framers/test_old_framers.py index 39bc5035b0..2690fe8693 100644 --- a/test/framers/test_old_framers.py +++ b/test/framers/test_old_framers.py @@ -14,7 +14,7 @@ ModbusTlsFramer, ) from pymodbus.pdu.bit_read_message import ReadCoilsRequest -from pymodbus.transport import CommType +from pymodbus.transport import CommParams, CommType from pymodbus.utilities import ModbusTransactionState @@ -330,6 +330,7 @@ async def test_send_packet(self, rtu_framer): host="localhost", port=BASE_PORT + 1, CommType=CommType.TCP, + comm_params=CommParams(), ) client.state = ModbusTransactionState.TRANSACTION_COMPLETE client.silent_interval = 1 diff --git a/test/sub_client/test_client.py b/test/sub_client/test_client.py index ec9405009f..d17b3a820b 100755 --- a/test/sub_client/test_client.py +++ b/test/sub_client/test_client.py @@ -22,7 +22,7 @@ from pymodbus.datastore.store import ModbusSequentialDataBlock from pymodbus.exceptions import ConnectionException, ModbusException, ModbusIOException from pymodbus.pdu import ModbusRequest -from pymodbus.transport import CommType +from pymodbus.transport import CommParams, CommType BASE_PORT = 6500 @@ -281,6 +281,7 @@ async def test_client_modbusbaseclient(): host="localhost", port=BASE_PORT + 1, CommType=CommType.TCP, + comm_params=CommParams(), ) client.register(pdu_bit_read.ReadCoilsResponse) assert str(client) @@ -316,6 +317,7 @@ async def test_client_base_async(): host="localhost", port=BASE_PORT + 2, CommType=CommType.TCP, + comm_params=CommParams(), ) as client: str(client) p_connect.return_value = asyncio.Future() @@ -327,7 +329,8 @@ async def test_client_base_async(): @pytest.mark.skip() async def test_client_protocol_receiver(): """Test the client protocol data received.""" - base = ModbusBaseClient(FramerType.SOCKET) + base = ModbusBaseClient(FramerType.SOCKET, comm_params=CommParams(), +) transport = mock.MagicMock() base.ctx.connection_made(transport) assert base.transport == transport @@ -349,7 +352,8 @@ async def test_client_protocol_receiver(): @pytest.mark.skip() async def test_client_protocol_response(): """Test the udp client protocol builds responses.""" - base = ModbusBaseClient(FramerType.SOCKET) + base = ModbusBaseClient(FramerType.SOCKET, comm_params=CommParams(), +) response = base.build_response(0x00) # pylint: disable=protected-access excp = response.exception() assert isinstance(excp, ConnectionException) @@ -363,7 +367,8 @@ async def test_client_protocol_response(): async def test_client_protocol_handler(): """Test the client protocol handles responses.""" base = ModbusBaseClient( - FramerType.ASCII, host="localhost", port=+3, CommType=CommType.TCP + FramerType.ASCII, host="localhost", port=+3, CommType=CommType.TCP, comm_params=CommParams(), + ) transport = mock.MagicMock() base.ctx.connection_made(transport=transport) @@ -411,7 +416,8 @@ def close(self): async def test_client_protocol_execute(): """Test the client protocol execute method.""" - base = ModbusBaseClient(FramerType.SOCKET, host="127.0.0.1") + base = ModbusBaseClient(FramerType.SOCKET, host="127.0.0.1", comm_params=CommParams(), +) request = pdu_bit_read.ReadCoilsRequest(1, 1) transport = MockTransport(base, request) base.ctx.connection_made(transport=transport) @@ -422,7 +428,8 @@ async def test_client_protocol_execute(): async def test_client_execute_broadcast(): """Test the client protocol execute method.""" - base = ModbusBaseClient(FramerType.SOCKET, host="127.0.0.1") + base = ModbusBaseClient(FramerType.SOCKET, host="127.0.0.1", comm_params=CommParams(), +) base.broadcast_enable = True request = pdu_bit_read.ReadCoilsRequest(1, 1) transport = MockTransport(base, request) @@ -432,7 +439,8 @@ async def test_client_execute_broadcast(): async def test_client_protocol_retry(): """Test the client protocol execute method with retries.""" - base = ModbusBaseClient(FramerType.SOCKET, host="127.0.0.1", timeout=0.1) + base = ModbusBaseClient(FramerType.SOCKET, host="127.0.0.1", timeout=0.1, comm_params=CommParams(), +) request = pdu_bit_read.ReadCoilsRequest(1, 1) transport = MockTransport(base, request, retries=2) base.ctx.connection_made(transport=transport) @@ -445,7 +453,8 @@ async def test_client_protocol_retry(): async def test_client_protocol_timeout(): """Test the client protocol execute method with timeout.""" - base = ModbusBaseClient(FramerType.SOCKET, host="127.0.0.1", timeout=0.1, retries=2) + base = ModbusBaseClient(FramerType.SOCKET, host="127.0.0.1", timeout=0.1, retries=2, comm_params=CommParams(), +) # Avoid creating do_reconnect() task base.ctx.connection_lost = mock.MagicMock() request = pdu_bit_read.ReadCoilsRequest(1, 1) @@ -645,7 +654,8 @@ def test_client_mixin_convert_fail(): async def test_client_build_response(): """Test fail of build_response.""" - client = ModbusBaseClient(FramerType.RTU) + client = ModbusBaseClient(FramerType.RTU, comm_params=CommParams(), +) with pytest.raises(ConnectionException): await client.build_response(ModbusRequest(0, 0, 0, False))