Skip to content

Commit

Permalink
Make client type annotations compatible with async client usage (#1842)
Browse files Browse the repository at this point in the history
  • Loading branch information
jvelo authored Oct 19, 2023
1 parent d445d90 commit 01ad131
Showing 1 changed file with 57 additions and 36 deletions.
93 changes: 57 additions & 36 deletions pymodbus/client/mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations

import struct
from collections.abc import Awaitable
from enum import Enum
from typing import Any

Expand Down Expand Up @@ -47,7 +48,9 @@ class ModbusClientMixin: # pylint: disable=too-many-public-methods
def __init__(self):
"""Initialize."""

def execute(self, request: ModbusRequest) -> ModbusResponse:
def execute(
self, request: ModbusRequest
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Execute request (code ???).
:param request: Request to send
Expand All @@ -62,7 +65,7 @@ def execute(self, request: ModbusRequest) -> ModbusResponse:

def read_coils(
self, address: int, count: int = 1, slave: int = 0, **kwargs: Any
) -> ModbusResponse:
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Read coils (code 0x01).
:param address: Start address to read from
Expand All @@ -77,7 +80,7 @@ def read_coils(

def read_discrete_inputs(
self, address: int, count: int = 1, slave: int = 0, **kwargs: Any
) -> ModbusResponse:
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Read discrete inputs (code 0x02).
:param address: Start address to read from
Expand All @@ -92,7 +95,7 @@ def read_discrete_inputs(

def read_holding_registers(
self, address: int, count: int = 1, slave: int = 0, **kwargs: Any
) -> ModbusResponse:
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Read holding registers (code 0x03).
:param address: Start address to read from
Expand All @@ -107,7 +110,7 @@ def read_holding_registers(

def read_input_registers(
self, address: int, count: int = 1, slave: int = 0, **kwargs: Any
) -> ModbusResponse:
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Read input registers (code 0x04).
:param address: Start address to read from
Expand All @@ -122,7 +125,7 @@ def read_input_registers(

def write_coil(
self, address: int, value: bool, slave: int = 0, **kwargs: Any
) -> ModbusResponse:
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Write single coil (code 0x05).
:param address: Address to write to
Expand All @@ -137,7 +140,7 @@ def write_coil(

def write_register(
self, address: int, value: int, slave: int = 0, **kwargs: Any
) -> ModbusResponse:
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Write register (code 0x06).
:param address: Address to write to
Expand All @@ -150,7 +153,9 @@ def write_register(
pdu_req_write.WriteSingleRegisterRequest(address, value, slave, **kwargs)
)

def read_exception_status(self, slave: int = 0, **kwargs: Any) -> ModbusResponse:
def read_exception_status(
self, slave: int = 0, **kwargs: Any
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Read Exception Status (code 0x07).
:param slave: (optional) Modbus slave ID
Expand All @@ -161,7 +166,7 @@ def read_exception_status(self, slave: int = 0, **kwargs: Any) -> ModbusResponse

def diag_query_data(
self, msg: bytearray, slave: int = 0, **kwargs: Any
) -> ModbusResponse:
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Diagnose query data (code 0x08 sub 0x00).
:param msg: Message to be returned
Expand All @@ -173,7 +178,7 @@ def diag_query_data(

def diag_restart_communication(
self, toggle: bool, slave: int = 0, **kwargs: Any
) -> ModbusResponse:
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Diagnose restart communication (code 0x08 sub 0x01).
:param toggle: True if toggled.
Expand All @@ -187,7 +192,7 @@ def diag_restart_communication(

def diag_read_diagnostic_register(
self, slave: int = 0, **kwargs: Any
) -> ModbusResponse:
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Diagnose read diagnostic register (code 0x08 sub 0x02).
:param slave: (optional) Modbus slave ID
Expand All @@ -200,7 +205,7 @@ def diag_read_diagnostic_register(

def diag_change_ascii_input_delimeter(
self, slave: int = 0, **kwargs: Any
) -> ModbusResponse:
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Diagnose change ASCII input delimiter (code 0x08 sub 0x03).
:param slave: (optional) Modbus slave ID
Expand All @@ -211,7 +216,9 @@ def diag_change_ascii_input_delimeter(
pdu_diag.ChangeAsciiInputDelimiterRequest(slave=slave, **kwargs)
)

def diag_force_listen_only(self, slave: int = 0, **kwargs: Any) -> ModbusResponse:
def diag_force_listen_only(
self, slave: int = 0, **kwargs: Any
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Diagnose force listen only (code 0x08 sub 0x04).
:param slave: (optional) Modbus slave ID
Expand All @@ -220,7 +227,9 @@ def diag_force_listen_only(self, slave: int = 0, **kwargs: Any) -> ModbusRespons
"""
return self.execute(pdu_diag.ForceListenOnlyModeRequest(slave=slave, **kwargs))

def diag_clear_counters(self, slave: int = 0, **kwargs: Any) -> ModbusResponse:
def diag_clear_counters(
self, slave: int = 0, **kwargs: Any
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Diagnose clear counters (code 0x08 sub 0x0A).
:param slave: (optional) Modbus slave ID
Expand All @@ -231,7 +240,7 @@ def diag_clear_counters(self, slave: int = 0, **kwargs: Any) -> ModbusResponse:

def diag_read_bus_message_count(
self, slave: int = 0, **kwargs: Any
) -> ModbusResponse:
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Diagnose read bus message count (code 0x08 sub 0x0B).
:param slave: (optional) Modbus slave ID
Expand All @@ -244,7 +253,7 @@ def diag_read_bus_message_count(

def diag_read_bus_comm_error_count(
self, slave: int = 0, **kwargs: Any
) -> ModbusResponse:
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Diagnose read Bus Communication Error Count (code 0x08 sub 0x0C).
:param slave: (optional) Modbus slave ID
Expand All @@ -257,7 +266,7 @@ def diag_read_bus_comm_error_count(

def diag_read_bus_exception_error_count(
self, slave: int = 0, **kwargs: Any
) -> ModbusResponse:
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Diagnose read Bus Exception Error Count (code 0x08 sub 0x0D).
:param slave: (optional) Modbus slave ID
Expand All @@ -270,7 +279,7 @@ def diag_read_bus_exception_error_count(

def diag_read_slave_message_count(
self, slave: int = 0, **kwargs: Any
) -> ModbusResponse:
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Diagnose read Slave Message Count (code 0x08 sub 0x0E).
:param slave: (optional) Modbus slave ID
Expand All @@ -283,7 +292,7 @@ def diag_read_slave_message_count(

def diag_read_slave_no_response_count(
self, slave: int = 0, **kwargs: Any
) -> ModbusResponse:
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Diagnose read Slave No Response Count (code 0x08 sub 0x0F).
:param slave: (optional) Modbus slave ID
Expand All @@ -296,7 +305,7 @@ def diag_read_slave_no_response_count(

def diag_read_slave_nak_count(
self, slave: int = 0, **kwargs: Any
) -> ModbusResponse:
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Diagnose read Slave NAK Count (code 0x08 sub 0x10).
:param slave: (optional) Modbus slave ID
Expand All @@ -307,7 +316,7 @@ def diag_read_slave_nak_count(

def diag_read_slave_busy_count(
self, slave: int = 0, **kwargs: Any
) -> ModbusResponse:
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Diagnose read Slave Busy Count (code 0x08 sub 0x11).
:param slave: (optional) Modbus slave ID
Expand All @@ -318,7 +327,7 @@ def diag_read_slave_busy_count(

def diag_read_bus_char_overrun_count(
self, slave: int = 0, **kwargs: Any
) -> ModbusResponse:
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Diagnose read Bus Character Overrun Count (code 0x08 sub 0x12).
:param slave: (optional) Modbus slave ID
Expand All @@ -331,7 +340,7 @@ def diag_read_bus_char_overrun_count(

def diag_read_iop_overrun_count(
self, slave: int = 0, **kwargs: Any
) -> ModbusResponse:
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Diagnose read Iop overrun count (code 0x08 sub 0x13).
:param slave: (optional) Modbus slave ID
Expand All @@ -344,7 +353,7 @@ def diag_read_iop_overrun_count(

def diag_clear_overrun_counter(
self, slave: int = 0, **kwargs: Any
) -> ModbusResponse:
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Diagnose Clear Overrun Counter and Flag (code 0x08 sub 0x14).
:param slave: (optional) Modbus slave ID
Expand All @@ -355,7 +364,7 @@ def diag_clear_overrun_counter(

def diag_getclear_modbus_response(
self, slave: int = 0, **kwargs: Any
) -> ModbusResponse:
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Diagnose Get/Clear modbus plus (code 0x08 sub 0x15).
:param slave: (optional) Modbus slave ID
Expand All @@ -364,15 +373,19 @@ def diag_getclear_modbus_response(
"""
return self.execute(pdu_diag.GetClearModbusPlusRequest(slave=slave, **kwargs))

def diag_get_comm_event_counter(self, **kwargs: Any) -> ModbusResponse:
def diag_get_comm_event_counter(
self, **kwargs: Any
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Diagnose get event counter (code 0x0B).
:param kwargs: (optional) Experimental parameters.
:raises ModbusException:
"""
return self.execute(pdu_other_msg.GetCommEventCounterRequest(**kwargs))

def diag_get_comm_event_log(self, **kwargs: Any) -> ModbusResponse:
def diag_get_comm_event_log(
self, **kwargs: Any
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Diagnose get event counter (code 0x0C).
:param kwargs: (optional) Experimental parameters.
Expand All @@ -386,7 +399,7 @@ def write_coils(
values: list[bool] | bool,
slave: int = 0,
**kwargs: Any,
) -> ModbusResponse:
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Write coils (code 0x0F).
:param address: Start address to write to
Expand All @@ -401,7 +414,7 @@ def write_coils(

def write_registers(
self, address: int, values: list[int] | int, slave: int = 0, **kwargs: Any
) -> ModbusResponse:
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Write registers (code 0x10).
:param address: Start address to write to
Expand All @@ -416,7 +429,9 @@ def write_registers(
)
)

def report_slave_id(self, slave: int = 0, **kwargs: Any) -> ModbusResponse:
def report_slave_id(
self, slave: int = 0, **kwargs: Any
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Report slave ID (code 0x11).
:param slave: (optional) Modbus slave ID
Expand All @@ -425,7 +440,9 @@ def report_slave_id(self, slave: int = 0, **kwargs: Any) -> ModbusResponse:
"""
return self.execute(pdu_other_msg.ReportSlaveIdRequest(slave, **kwargs))

def read_file_record(self, records: list[tuple], **kwargs: Any) -> ModbusResponse:
def read_file_record(
self, records: list[tuple], **kwargs: Any
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Read file record (code 0x14).
:param records: List of (Reference type, File number, Record Number, Record Length)
Expand All @@ -434,7 +451,9 @@ def read_file_record(self, records: list[tuple], **kwargs: Any) -> ModbusRespons
"""
return self.execute(pdu_file_msg.ReadFileRecordRequest(records, **kwargs))

def write_file_record(self, records: list[tuple], **kwargs: Any) -> ModbusResponse:
def write_file_record(
self, records: list[tuple], **kwargs: Any
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Write file record (code 0x15).
:param records: List of (Reference type, File number, Record Number, Record Length)
Expand All @@ -449,7 +468,7 @@ def mask_write_register(
and_mask: int = 0xFFFF,
or_mask: int = 0x0000,
**kwargs: Any,
) -> ModbusResponse:
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Mask write register (code 0x16).
:param address: The mask pointer address (0x0000 to 0xffff)
Expand All @@ -470,7 +489,7 @@ def readwrite_registers(
values: list[int] | int = 0,
slave: int = 0,
**kwargs,
) -> ModbusResponse:
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Read/Write registers (code 0x17).
:param read_address: The address to start reading from
Expand All @@ -492,7 +511,9 @@ def readwrite_registers(
)
)

def read_fifo_queue(self, address: int = 0x0000, **kwargs: Any) -> ModbusResponse:
def read_fifo_queue(
self, address: int = 0x0000, **kwargs: Any
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Read FIFO queue (code 0x18).
:param address: The address to start reading from
Expand All @@ -505,7 +526,7 @@ def read_fifo_queue(self, address: int = 0x0000, **kwargs: Any) -> ModbusRespons

def read_device_information(
self, read_code: int = None, object_id: int = 0x00, **kwargs: Any
) -> ModbusResponse:
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Read FIFO queue (code 0x2B sub 0x0E).
:param read_code: The device information read code
Expand Down

0 comments on commit 01ad131

Please sign in to comment.