44import logging
55import threading
66
7- from pymodbus .client import ModbusTcpClient
7+ from pymodbus .client import AsyncModbusTcpClient
88
99from .const import (
1010 INT32 ,
@@ -27,49 +27,54 @@ def __init__(self, host: str, port: int) -> None:
2727 self .host = host
2828 self .port = port
2929 # Fail more quickly and only retry once before executing retry logic
30- self ._client = ModbusTcpClient (
31- host = self .host , port = self .port , timeout = 10 , retries = 1
30+ self ._client = AsyncModbusTcpClient (
31+ host = self .host , port = self .port , timeout = 30 , retries = 10
3232 )
3333 self ._lock = threading .Lock ()
3434
3535 def is_still_connected (self ):
3636 """Check if the connection is still open."""
3737 return self ._client .is_socket_open ()
3838
39- def connect (self ):
39+ async def connect (self ):
4040 """Connect to the Modbus TCP server."""
41- return self ._client .connect ()
41+ return await self ._client .connect ()
4242
43- def disconnect (self ):
43+ async def disconnect (self ):
4444 """Disconnect from the Modbus TCP server."""
4545 if self ._client .is_socket_open ():
4646 return self ._client .close ()
4747 return None
4848
49- def write_register (self , unit , address , value ):
49+ async def write_register (self , unit , address , value ):
5050 """Write a register."""
5151 try :
5252 slave = int (unit )
53- if not unit :
53+ if not ( 0 <= slave <= 254 ) :
5454 _LOGGER .error (
5555 "Unit for this device (%s) isn't set correctly. Cannot write (%s) to register (%s). Ensure that the config was migrated to latest state by forcing a rescan" ,
56- unit ,
56+ slave ,
5757 value ,
5858 address ,
5959 )
6060 return
61- self ._client .write_register (address = address , value = value , slave = slave )
61+ await self ._client .write_register (address = address , value = value , slave = slave )
6262 except BrokenPipeError :
6363 self .__handle_broken_pipe_error ()
6464
65- def read_holding_registers (self , unit , address , count ):
65+ async def read_holding_registers (self , unit , address , count ):
6666 """Read holding registers."""
6767 try :
68- if not unit :
68+ slave = int (unit )
69+ if not (0 <= slave <= 254 ):
70+ _LOGGER .error (
71+ "Unit for this device (%s) isn't set correctly. Cannot read register (%s). Ensure that the config was migrated to latest state by forcing a rescan" ,
72+ slave ,
73+ address ,
74+ )
6975 return None
7076
71- slave = int (unit )
72- return self ._client .read_holding_registers (
77+ return await self ._client .read_holding_registers (
7378 address = address , count = count , slave = slave
7479 )
7580 except BrokenPipeError :
@@ -111,7 +116,7 @@ def get_first_register_id(self, registerInfoDict: OrderedDict):
111116 first_register = next (iter (registerInfoDict ))
112117 return registerInfoDict [first_register ].register
113118
114- def determine_present_devices (self ):
119+ async def determine_present_devices (self ):
115120 """Determine which devices are present."""
116121 valid_devices = {}
117122
@@ -124,7 +129,7 @@ def determine_present_devices(self):
124129
125130 address = self .get_first_register_id (register_definition )
126131 count = self .calculate_register_count (register_definition )
127- result = self .read_holding_registers (unit , address , count )
132+ result = await self .read_holding_registers (unit , address , count )
128133 if result is None :
129134 _LOGGER .debug (
130135 "result is error for unit: %s address: %s count: %s and result: %s" ,
0 commit comments