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 19, 2024
1 parent 72e3399 commit 270683d
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 13 deletions.
4 changes: 4 additions & 0 deletions pymodbus/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class ModbusBaseClient(ModbusClientMixin[Awaitable[ModbusResponse]]):

def __init__( # pylint: disable=too-many-arguments
self,
comm_params: CommParams,
framer: FramerType,
timeout: float = 3,
retries: int = 3,
Expand All @@ -66,6 +67,7 @@ def __init__( # pylint: disable=too-many-arguments
) -> None:
"""Initialize a client instance."""
ModbusClientMixin.__init__(self) # type: ignore[arg-type]
_jix = comm_params
self.ctx = ModbusClientProtocol(
framer,
CommParams(
Expand Down Expand Up @@ -266,6 +268,7 @@ class _params:

def __init__( # pylint: disable=too-many-arguments
self,
comm_params: CommParams,
framer: FramerType,
timeout: float = 3,
retries: int = 3,
Expand All @@ -280,6 +283,7 @@ def __init__( # pylint: disable=too-many-arguments
) -> None:
"""Initialize a client instance."""
ModbusClientMixin.__init__(self) # type: ignore[arg-type]
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 @@ -83,6 +83,7 @@ def __init__(
)
ModbusBaseClient.__init__(
self,
CommParams(),
framer,
comm_type=CommType.SERIAL,
host=port,
Expand Down Expand Up @@ -160,6 +161,7 @@ def __init__(
) -> None:
"""Initialize Modbus Serial Client."""
super().__init__(
CommParams(),
framer,
comm_type=CommType.SERIAL,
host=port,
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 @@ -69,6 +69,7 @@ def __init__(
kwargs["source_address"] = source_address
ModbusBaseClient.__init__(
self,
CommParams(),
framer,
host=host,
port=port,
Expand Down Expand Up @@ -135,6 +136,7 @@ def __init__(
if "comm_type" not in kwargs:
kwargs["comm_type"] = CommType.TCP
super().__init__(
CommParams(),
framer,
host=host,
port=port,
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 @@ -64,6 +64,7 @@ def __init__(
"""Initialize Asyncio Modbus UDP Client."""
ModbusBaseClient.__init__(
self,
CommParams(),
framer,
comm_type=CommType.UDP,
host=host,
Expand Down Expand Up @@ -131,6 +132,7 @@ def __init__(
) -> None:
"""Initialize Modbus UDP Client."""
super().__init__(
CommParams(),
framer,
port=port,
host=host,
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 @@ -326,6 +326,7 @@ async def test_send_packet(self, rtu_framer):
"""Test send packet."""
message = TEST_MESSAGE
client = ModbusBaseClient(
CommParams(),
FramerType.ASCII,
host="localhost",
port=BASE_PORT + 1,
Expand Down
20 changes: 11 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 @@ -277,6 +277,7 @@ async def test_serial_not_installed():
async def test_client_modbusbaseclient():
"""Test modbus base client class."""
client = ModbusBaseClient(
CommParams(),
FramerType.ASCII,
host="localhost",
port=BASE_PORT + 1,
Expand Down Expand Up @@ -312,6 +313,7 @@ async def test_client_base_async():
p_close.return_value = asyncio.Future()
p_close.return_value.set_result(True)
async with ModbusBaseClient(
CommParams(),
FramerType.ASCII,
host="localhost",
port=BASE_PORT + 2,
Expand All @@ -327,7 +329,7 @@ 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(CommParams(), FramerType.SOCKET)
transport = mock.MagicMock()
base.ctx.connection_made(transport)
assert base.transport == transport
Expand All @@ -349,7 +351,7 @@ 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(CommParams(), FramerType.SOCKET)
response = base.build_response(0x00) # pylint: disable=protected-access
excp = response.exception()
assert isinstance(excp, ConnectionException)
Expand All @@ -363,7 +365,7 @@ 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
CommParams(), FramerType.ASCII, host="localhost", port=+3, CommType=CommType.TCP
)
transport = mock.MagicMock()
base.ctx.connection_made(transport=transport)
Expand Down Expand Up @@ -411,7 +413,7 @@ 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(CommParams(), FramerType.SOCKET, host="127.0.0.1")
request = pdu_bit_read.ReadCoilsRequest(1, 1)
transport = MockTransport(base, request)
base.ctx.connection_made(transport=transport)
Expand All @@ -422,7 +424,7 @@ 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(CommParams(), FramerType.SOCKET, host="127.0.0.1")
base.broadcast_enable = True
request = pdu_bit_read.ReadCoilsRequest(1, 1)
transport = MockTransport(base, request)
Expand All @@ -432,7 +434,7 @@ 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(CommParams(), FramerType.SOCKET, host="127.0.0.1", timeout=0.1)
request = pdu_bit_read.ReadCoilsRequest(1, 1)
transport = MockTransport(base, request, retries=2)
base.ctx.connection_made(transport=transport)
Expand All @@ -445,7 +447,7 @@ 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(CommParams(), FramerType.SOCKET, host="127.0.0.1", timeout=0.1, retries=2)
# Avoid creating do_reconnect() task
base.ctx.connection_lost = mock.MagicMock()
request = pdu_bit_read.ReadCoilsRequest(1, 1)
Expand Down Expand Up @@ -645,7 +647,7 @@ def test_client_mixin_convert_fail():

async def test_client_build_response():
"""Test fail of build_response."""
client = ModbusBaseClient(FramerType.RTU)
client = ModbusBaseClient(CommParams(), FramerType.RTU)
with pytest.raises(ConnectionException):
await client.build_response(ModbusRequest(0, 0, 0, False))

Expand Down

0 comments on commit 270683d

Please sign in to comment.