Skip to content

Commit

Permalink
Fix type checks/linting
Browse files Browse the repository at this point in the history
  • Loading branch information
OCopping committed Mar 8, 2024
1 parent 9aff887 commit 52143a3
Showing 1 changed file with 22 additions and 23 deletions.
45 changes: 22 additions & 23 deletions src/fastcs/connections/modbus_connection.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import asyncio

from dataclasses import dataclass
from typing import Optional

from pymodbus.client.base import ModbusBaseClient
from pymodbus.client import (
AsyncModbusSerialClient,
AsyncModbusTcpClient,
AsyncModbusUdpClient,
ModbusBaseClient,
)
from pymodbus.exceptions import ModbusException

from pymodbus.exceptions import ModbusException
from pymodbus.framer import Framer
from pymodbus.pdu import ExceptionResponse

from pymodbus.pdu import ExceptionResponse, ModbusResponse

# Constants
CR = "\r"
Expand All @@ -27,29 +24,27 @@ class ModbusConnectionSettings:
port: int = 7001
slave: int = 0

class ModbusConnection:

class ModbusConnection:
def __init__(self, settings: ModbusConnectionSettings) -> None:

self.host, self.port, self.slave = settings.host, settings.port, settings.slave
self.running: bool = False

Check warning on line 31 in src/fastcs/connections/modbus_connection.py

View check run for this annotation

Codecov / codecov/patch

src/fastcs/connections/modbus_connection.py#L30-L31

Added lines #L30 - L31 were not covered by tests

self._client: ModbusBaseClient

Check warning on line 33 in src/fastcs/connections/modbus_connection.py

View check run for this annotation

Codecov / codecov/patch

src/fastcs/connections/modbus_connection.py#L33

Added line #L33 was not covered by tests

async def connect(self, framer=Framer.SOCKET):
async def connect(self) -> None:
raise NotImplementedError

Check warning on line 36 in src/fastcs/connections/modbus_connection.py

View check run for this annotation

Codecov / codecov/patch

src/fastcs/connections/modbus_connection.py#L36

Added line #L36 was not covered by tests


def disconnect(self):
self._client.close()

Check warning on line 39 in src/fastcs/connections/modbus_connection.py

View check run for this annotation

Codecov / codecov/patch

src/fastcs/connections/modbus_connection.py#L39

Added line #L39 was not covered by tests

async def _read(self, address: int, count: int = 2) -> Optional[str]:
async def _read(self, address: int, count: int = 2) -> Optional[ModbusResponse]:
# address -= 1 # modbus spec starts from 0 not 1
try:

Check warning on line 43 in src/fastcs/connections/modbus_connection.py

View check run for this annotation

Codecov / codecov/patch

src/fastcs/connections/modbus_connection.py#L43

Added line #L43 was not covered by tests
# address_hex = hex(address)
rr = await self._client.read_holding_registers(address, count=count, slave=self.slave) # type: ignore
print(f"Response: {rr}")

Check warning on line 46 in src/fastcs/connections/modbus_connection.py

View check run for this annotation

Codecov / codecov/patch

src/fastcs/connections/modbus_connection.py#L45-L46

Added lines #L45 - L46 were not covered by tests
except ModbusException as exc: # pragma no cover
except ModbusException: # pragma no cover
# Received ModbusException from library
self.disconnect()
return
Expand All @@ -61,8 +56,11 @@ async def _read(self, address: int, count: int = 2) -> Optional[str]:
# Received Modbus library exception
# THIS IS NOT A PYTHON EXCEPTION, but a valid modbus message
self.disconnect()
else:
return rr
return None

Check warning on line 61 in src/fastcs/connections/modbus_connection.py

View check run for this annotation

Codecov / codecov/patch

src/fastcs/connections/modbus_connection.py#L60-L61

Added lines #L60 - L61 were not covered by tests

async def send(self, address: int, value: int) -> None:
async def send(self, address: int, value: int) -> ModbusResponse | None:
"""Send a request.
Args:
Expand All @@ -71,14 +69,15 @@ async def send(self, address: int, value: int) -> None:
"""
await self._client.write_registers(address, value, slave=self.slave)
resp = await self._read(address, 2)
return resp

Check warning on line 72 in src/fastcs/connections/modbus_connection.py

View check run for this annotation

Codecov / codecov/patch

src/fastcs/connections/modbus_connection.py#L70-L72

Added lines #L70 - L72 were not covered by tests

class ModbusSerialConnection(ModbusConnection):

class ModbusSerialConnection(ModbusConnection):
def __init__(self, settings: ModbusConnectionSettings) -> None:
super().__init__(settings)

Check warning on line 77 in src/fastcs/connections/modbus_connection.py

View check run for this annotation

Codecov / codecov/patch

src/fastcs/connections/modbus_connection.py#L77

Added line #L77 was not covered by tests

async def connect(self, framer=Framer.SOCKET):
self._client: AsyncModbusSerialClient = AsyncModbusSerialClient(
async def connect(self, framer: Framer=Framer.SOCKET):
self._client = AsyncModbusSerialClient(

Check warning on line 80 in src/fastcs/connections/modbus_connection.py

View check run for this annotation

Codecov / codecov/patch

src/fastcs/connections/modbus_connection.py#L80

Added line #L80 was not covered by tests
str(self.port),
framer=framer,
timeout=10,
Expand All @@ -95,13 +94,13 @@ async def connect(self, framer=Framer.SOCKET):
await self._client.connect()
assert self._client.connected

Check warning on line 95 in src/fastcs/connections/modbus_connection.py

View check run for this annotation

Codecov / codecov/patch

src/fastcs/connections/modbus_connection.py#L94-L95

Added lines #L94 - L95 were not covered by tests

class ModbusTcpConnection(ModbusConnection):

class ModbusTcpConnection(ModbusConnection):
def __init__(self, settings: ModbusConnectionSettings) -> None:
super().__init__(settings)

Check warning on line 100 in src/fastcs/connections/modbus_connection.py

View check run for this annotation

Codecov / codecov/patch

src/fastcs/connections/modbus_connection.py#L100

Added line #L100 was not covered by tests

async def connect(self, framer=Framer.SOCKET):
self._client: AsyncModbusTcpClient = AsyncModbusTcpClient(
async def connect(self, framer: Framer=Framer.SOCKET):
self._client = AsyncModbusTcpClient(

Check warning on line 103 in src/fastcs/connections/modbus_connection.py

View check run for this annotation

Codecov / codecov/patch

src/fastcs/connections/modbus_connection.py#L103

Added line #L103 was not covered by tests
self.host,
self.port,
framer=framer,
Expand All @@ -116,13 +115,13 @@ async def connect(self, framer=Framer.SOCKET):
await self._client.connect()
assert self._client.connected

Check warning on line 116 in src/fastcs/connections/modbus_connection.py

View check run for this annotation

Codecov / codecov/patch

src/fastcs/connections/modbus_connection.py#L115-L116

Added lines #L115 - L116 were not covered by tests

class ModbusUdpConnection(ModbusConnection):

class ModbusUdpConnection(ModbusConnection):
def __init__(self, settings: ModbusConnectionSettings) -> None:
super().__init__(settings)

Check warning on line 121 in src/fastcs/connections/modbus_connection.py

View check run for this annotation

Codecov / codecov/patch

src/fastcs/connections/modbus_connection.py#L121

Added line #L121 was not covered by tests

async def connect(self, framer=Framer.SOCKET):
self._client: AsyncModbusUdpClient = AsyncModbusUdpClient(
async def connect(self, framer: Framer=Framer.SOCKET):
self._client = AsyncModbusUdpClient(

Check warning on line 124 in src/fastcs/connections/modbus_connection.py

View check run for this annotation

Codecov / codecov/patch

src/fastcs/connections/modbus_connection.py#L124

Added line #L124 was not covered by tests
self.host,
self.port,
framer=framer,
Expand All @@ -135,4 +134,4 @@ async def connect(self, framer=Framer.SOCKET):
)

await self._client.connect()
assert self._client.connected
assert self._client.connected

Check warning on line 137 in src/fastcs/connections/modbus_connection.py

View check run for this annotation

Codecov / codecov/patch

src/fastcs/connections/modbus_connection.py#L136-L137

Added lines #L136 - L137 were not covered by tests

0 comments on commit 52143a3

Please sign in to comment.