Skip to content

Commit 52143a3

Browse files
committed
Fix type checks/linting
1 parent 9aff887 commit 52143a3

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

src/fastcs/connections/modbus_connection.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
import asyncio
2-
31
from dataclasses import dataclass
42
from typing import Optional
53

6-
from pymodbus.client.base import ModbusBaseClient
74
from pymodbus.client import (
85
AsyncModbusSerialClient,
96
AsyncModbusTcpClient,
107
AsyncModbusUdpClient,
8+
ModbusBaseClient,
119
)
12-
from pymodbus.exceptions import ModbusException
1310

11+
from pymodbus.exceptions import ModbusException
1412
from pymodbus.framer import Framer
15-
from pymodbus.pdu import ExceptionResponse
16-
13+
from pymodbus.pdu import ExceptionResponse, ModbusResponse
1714

1815
# Constants
1916
CR = "\r"
@@ -27,29 +24,27 @@ class ModbusConnectionSettings:
2724
port: int = 7001
2825
slave: int = 0
2926

30-
class ModbusConnection:
3127

28+
class ModbusConnection:
3229
def __init__(self, settings: ModbusConnectionSettings) -> None:
33-
3430
self.host, self.port, self.slave = settings.host, settings.port, settings.slave
3531
self.running: bool = False
3632

3733
self._client: ModbusBaseClient
3834

39-
async def connect(self, framer=Framer.SOCKET):
35+
async def connect(self) -> None:
4036
raise NotImplementedError
4137

42-
4338
def disconnect(self):
4439
self._client.close()
4540

46-
async def _read(self, address: int, count: int = 2) -> Optional[str]:
41+
async def _read(self, address: int, count: int = 2) -> Optional[ModbusResponse]:
4742
# address -= 1 # modbus spec starts from 0 not 1
4843
try:
4944
# address_hex = hex(address)
5045
rr = await self._client.read_holding_registers(address, count=count, slave=self.slave) # type: ignore
5146
print(f"Response: {rr}")
52-
except ModbusException as exc: # pragma no cover
47+
except ModbusException: # pragma no cover
5348
# Received ModbusException from library
5449
self.disconnect()
5550
return
@@ -61,8 +56,11 @@ async def _read(self, address: int, count: int = 2) -> Optional[str]:
6156
# Received Modbus library exception
6257
# THIS IS NOT A PYTHON EXCEPTION, but a valid modbus message
6358
self.disconnect()
59+
else:
60+
return rr
61+
return None
6462

65-
async def send(self, address: int, value: int) -> None:
63+
async def send(self, address: int, value: int) -> ModbusResponse | None:
6664
"""Send a request.
6765
6866
Args:
@@ -71,14 +69,15 @@ async def send(self, address: int, value: int) -> None:
7169
"""
7270
await self._client.write_registers(address, value, slave=self.slave)
7371
resp = await self._read(address, 2)
72+
return resp
7473

75-
class ModbusSerialConnection(ModbusConnection):
7674

75+
class ModbusSerialConnection(ModbusConnection):
7776
def __init__(self, settings: ModbusConnectionSettings) -> None:
7877
super().__init__(settings)
7978

80-
async def connect(self, framer=Framer.SOCKET):
81-
self._client: AsyncModbusSerialClient = AsyncModbusSerialClient(
79+
async def connect(self, framer: Framer=Framer.SOCKET):
80+
self._client = AsyncModbusSerialClient(
8281
str(self.port),
8382
framer=framer,
8483
timeout=10,
@@ -95,13 +94,13 @@ async def connect(self, framer=Framer.SOCKET):
9594
await self._client.connect()
9695
assert self._client.connected
9796

98-
class ModbusTcpConnection(ModbusConnection):
9997

98+
class ModbusTcpConnection(ModbusConnection):
10099
def __init__(self, settings: ModbusConnectionSettings) -> None:
101100
super().__init__(settings)
102101

103-
async def connect(self, framer=Framer.SOCKET):
104-
self._client: AsyncModbusTcpClient = AsyncModbusTcpClient(
102+
async def connect(self, framer: Framer=Framer.SOCKET):
103+
self._client = AsyncModbusTcpClient(
105104
self.host,
106105
self.port,
107106
framer=framer,
@@ -116,13 +115,13 @@ async def connect(self, framer=Framer.SOCKET):
116115
await self._client.connect()
117116
assert self._client.connected
118117

119-
class ModbusUdpConnection(ModbusConnection):
120118

119+
class ModbusUdpConnection(ModbusConnection):
121120
def __init__(self, settings: ModbusConnectionSettings) -> None:
122121
super().__init__(settings)
123122

124-
async def connect(self, framer=Framer.SOCKET):
125-
self._client: AsyncModbusUdpClient = AsyncModbusUdpClient(
123+
async def connect(self, framer: Framer=Framer.SOCKET):
124+
self._client = AsyncModbusUdpClient(
126125
self.host,
127126
self.port,
128127
framer=framer,
@@ -135,4 +134,4 @@ async def connect(self, framer=Framer.SOCKET):
135134
)
136135

137136
await self._client.connect()
138-
assert self._client.connected
137+
assert self._client.connected

0 commit comments

Comments
 (0)