Skip to content

Commit

Permalink
remove kwargs client.
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen committed Jul 20, 2024
1 parent dbcb4a4 commit 72a7175
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 13 deletions.
6 changes: 6 additions & 0 deletions pymodbus/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion pymodbus/client/serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -90,6 +90,7 @@ def __init__(
bytesize=bytesize,
parity=parity,
stopbits=stopbits,
comm_params = CommParams(),
**kwargs,
)

Expand Down Expand Up @@ -167,6 +168,7 @@ def __init__(
bytesize=bytesize,
parity=parity,
stopbits=stopbits,
comm_params = CommParams(),
**kwargs,
)
self.socket: serial.Serial | None = None
Expand Down
4 changes: 3 additions & 1 deletion pymodbus/client/tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -72,6 +72,7 @@ def __init__(
framer,
host=host,
port=port,
comm_params = CommParams(),
**kwargs,
)

Expand Down Expand Up @@ -138,6 +139,7 @@ def __init__(
framer,
host=host,
port=port,
comm_params = CommParams(),
**kwargs,
)
self.params.source_address = source_address
Expand Down
4 changes: 3 additions & 1 deletion pymodbus/client/udp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -68,6 +68,7 @@ def __init__(
comm_type=CommType.UDP,
host=host,
port=port,
comm_params = CommParams(),
**kwargs,
)
self.source_address = source_address
Expand Down Expand Up @@ -135,6 +136,7 @@ def __init__(
port=port,
host=host,
comm_type=CommType.UDP,
comm_params = CommParams(),
**kwargs,
)
self.params.source_address = source_address
Expand Down
3 changes: 2 additions & 1 deletion test/framers/test_old_framers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand Down
28 changes: 19 additions & 9 deletions test/sub_client/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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))

Expand Down

0 comments on commit 72a7175

Please sign in to comment.