2323 DOMAIN ,
2424 METER_REG_BASE ,
2525 ModbusDefaults ,
26+ RetrySettings ,
2627 SolarEdgeTimeouts ,
2728 SunSpecNotImpl ,
2829)
@@ -148,6 +149,7 @@ def __init__(
148149
149150 self ._initalized = False
150151 self ._online = True
152+ self ._timeout_counter = 0
151153
152154 self ._client = None
153155
@@ -316,9 +318,10 @@ async def async_refresh_modbus_data(self) -> bool:
316318 if not self .initalized :
317319 try :
318320 async with self ._lock :
319- await self ._async_init_solaredge ()
321+ async with asyncio .timeout (self .coordinator_timeout ):
322+ await self ._async_init_solaredge ()
320323
321- except (ConnectionException , ModbusIOException ) as e :
324+ except (ConnectionException , ModbusIOException , TimeoutError ) as e :
322325 self .disconnect ()
323326 ir .async_create_issue (
324327 self ._hass ,
@@ -357,20 +360,20 @@ async def async_refresh_modbus_data(self) -> bool:
357360
358361 try :
359362 async with self ._lock :
360- for inverter in self .inverters :
361- await inverter .read_modbus_data ()
362- for meter in self .meters :
363- await meter .read_modbus_data ()
364- for battery in self .batteries :
365- await battery .read_modbus_data ()
363+ async with asyncio .timeout (self .coordinator_timeout ):
364+ for inverter in self .inverters :
365+ await inverter .read_modbus_data ()
366+ for meter in self .meters :
367+ await meter .read_modbus_data ()
368+ for battery in self .batteries :
369+ await battery .read_modbus_data ()
366370
367371 except ModbusReadError as e :
368372 self .disconnect ()
369373 raise DataUpdateFailed (f"Update failed: { e } " )
370374
371375 except DeviceInvalid as e :
372- if not self ._keep_modbus_open :
373- self .disconnect ()
376+ self .disconnect ()
374377 raise DataUpdateFailed (f"Invalid device: { e } " )
375378
376379 except ConnectionException as e :
@@ -381,9 +384,29 @@ async def async_refresh_modbus_data(self) -> bool:
381384 self .disconnect ()
382385 raise DataUpdateFailed (f"Modbus error: { e } " )
383386
387+ except TimeoutError as e :
388+ self .disconnect ()
389+ self ._timeout_counter += 1
390+
391+ _LOGGER .warning (
392+ f"Refresh timeout { self ._timeout_counter } limit { RetrySettings .Limit } "
393+ )
394+
395+ if self ._timeout_counter >= RetrySettings .Limit :
396+ self ._timeout_counter = 0
397+ raise TimeoutError
398+
399+ raise DataUpdateFailed (f"Timeout error: { e } " )
400+
384401 if not self ._keep_modbus_open :
385402 self .disconnect ()
386403
404+ if self ._timeout_counter > 0 :
405+ _LOGGER .warning (
406+ f"Timeout count { self ._timeout_counter } limit { RetrySettings .Limit } "
407+ )
408+ self ._timeout_counter = 0
409+
387410 return True
388411
389412 async def connect (self ) -> None :
0 commit comments