@@ -177,7 +177,6 @@ def __init__(
177177 "retries" , ModbusDefaults .Retries
178178 )
179179 self ._id = entry_data [CONF_NAME ].lower ()
180- self ._lock = asyncio .Lock ()
181180 self .inverters = []
182181 self .meters = []
183182 self .batteries = []
@@ -380,37 +379,16 @@ async def _async_init_solaredge(self) -> None:
380379 async def async_refresh_modbus_data (self ) -> bool :
381380 """Refresh modbus data from inverters."""
382381
383- async with self ._lock :
384- if not self .is_connected :
385- await self .connect ()
386-
387- if not self .initalized :
388- try :
389- async with asyncio .timeout (self .coordinator_timeout ):
390- await self ._async_init_solaredge ()
391-
392- except (ConnectionException , ModbusIOException , TimeoutError ) as e :
393- self .disconnect ()
394- ir .async_create_issue (
395- self ._hass ,
396- DOMAIN ,
397- "check_configuration" ,
398- is_fixable = True ,
399- severity = ir .IssueSeverity .ERROR ,
400- translation_key = "check_configuration" ,
401- data = {"entry_id" : self ._entry_id },
402- )
403- raise HubInitFailed (f"Setup failed: { e } " )
404-
405- ir .async_delete_issue (self ._hass , DOMAIN , "check_configuration" )
406-
407- if not self .keep_modbus_open :
408- self .disconnect ()
382+ if not self .is_connected :
383+ await self .connect ()
409384
410- return True
385+ if not self .initalized :
386+ try :
387+ async with asyncio .timeout (self .coordinator_timeout ):
388+ await self ._async_init_solaredge ()
411389
412- if not self . is_connected :
413- self .online = False
390+ except ( ConnectionException , ModbusIOException , TimeoutError ) as e :
391+ self .disconnect ()
414392 ir .async_create_issue (
415393 self ._hass ,
416394 DOMAIN ,
@@ -420,65 +398,85 @@ async def async_refresh_modbus_data(self) -> bool:
420398 translation_key = "check_configuration" ,
421399 data = {"entry_id" : self ._entry_id },
422400 )
423- raise DataUpdateFailed (
424- f"Modbus/TCP connect to { self .hub_host } :{ self .hub_port } failed."
425- )
401+ raise HubInitFailed (f"Setup failed: { e } " )
426402
427- if not self .online :
428- ir .async_delete_issue (self ._hass , DOMAIN , "check_configuration" )
403+ ir .async_delete_issue (self ._hass , DOMAIN , "check_configuration" )
429404
430- self .online = True
431-
432- try :
433- async with asyncio .timeout (self .coordinator_timeout ):
434- for inverter in self .inverters :
435- await inverter .read_modbus_data ()
436- for meter in self .meters :
437- await meter .read_modbus_data ()
438- for battery in self .batteries :
439- await battery .read_modbus_data ()
440-
441- except ModbusReadError as e :
405+ if not self .keep_modbus_open :
442406 self .disconnect ()
443- raise DataUpdateFailed (f"Update failed: { e } " )
444407
445- except DeviceInvalid as e :
446- self .disconnect ()
447- raise DataUpdateFailed (f"Invalid device: { e } " )
408+ return True
448409
449- except ConnectionException as e :
450- self .disconnect ()
451- raise DataUpdateFailed (f"Connection failed: { e } " )
410+ if not self .is_connected :
411+ self .online = False
412+ ir .async_create_issue (
413+ self ._hass ,
414+ DOMAIN ,
415+ "check_configuration" ,
416+ is_fixable = True ,
417+ severity = ir .IssueSeverity .ERROR ,
418+ translation_key = "check_configuration" ,
419+ data = {"entry_id" : self ._entry_id },
420+ )
421+ raise DataUpdateFailed (
422+ f"Modbus/TCP connect to { self .hub_host } :{ self .hub_port } failed."
423+ )
452424
453- except ModbusIOException as e :
454- self .disconnect ()
455- raise DataUpdateFailed (f"Modbus error: { e } " )
425+ if not self .online :
426+ ir .async_delete_issue (self ._hass , DOMAIN , "check_configuration" )
456427
457- except TimeoutError as e :
458- self .disconnect (clear_client = True )
459- self ._timeout_counter += 1
428+ self .online = True
460429
461- _LOGGER .debug (
462- f"Refresh timeout { self ._timeout_counter } "
463- f"limit { self ._retry_limit } "
464- )
430+ try :
431+ async with asyncio .timeout (self .coordinator_timeout ):
432+ for inverter in self .inverters :
433+ await inverter .read_modbus_data ()
434+ for meter in self .meters :
435+ await meter .read_modbus_data ()
436+ for battery in self .batteries :
437+ await battery .read_modbus_data ()
465438
466- if self . _timeout_counter >= self . _retry_limit :
467- self ._timeout_counter = 0
468- raise TimeoutError
439+ except ModbusReadError as e :
440+ self .disconnect ()
441+ raise DataUpdateFailed ( f"Update failed: { e } " )
469442
470- raise DataUpdateFailed (f"Timeout error: { e } " )
443+ except DeviceInvalid as e :
444+ self .disconnect ()
445+ raise DataUpdateFailed (f"Invalid device: { e } " )
471446
472- if self ._timeout_counter > 0 :
473- _LOGGER .debug (
474- f"Timeout count { self ._timeout_counter } limit { self ._retry_limit } "
475- )
447+ except ConnectionException as e :
448+ self .disconnect ()
449+ raise DataUpdateFailed (f"Connection failed: { e } " )
450+
451+ except ModbusIOException as e :
452+ self .disconnect ()
453+ raise DataUpdateFailed (f"Modbus error: { e } " )
454+
455+ except TimeoutError as e :
456+ self .disconnect (clear_client = True )
457+ self ._timeout_counter += 1
458+
459+ _LOGGER .debug (
460+ f"Refresh timeout { self ._timeout_counter } "
461+ f"limit { self ._retry_limit } "
462+ )
463+
464+ if self ._timeout_counter >= self ._retry_limit :
476465 self ._timeout_counter = 0
466+ raise TimeoutError
477467
478- if not self .keep_modbus_open :
479- self .disconnect ()
468+ raise DataUpdateFailed (f"Timeout error: { e } " )
480469
481- return True
470+ if self ._timeout_counter > 0 :
471+ _LOGGER .debug (
472+ f"Timeout count { self ._timeout_counter } limit { self ._retry_limit } "
473+ )
474+ self ._timeout_counter = 0
475+
476+ if not self .keep_modbus_open :
477+ self .disconnect ()
478+
479+ return True
482480
483481 async def connect (self ) -> None :
484482 """Connect to inverter."""
@@ -521,9 +519,8 @@ def disconnect(self, clear_client: bool = False) -> None:
521519 async def shutdown (self ) -> None :
522520 """Shut down the hub and disconnect."""
523521
524- async with self ._lock :
525- self .online = False
526- self .disconnect (clear_client = True )
522+ self .online = False
523+ self .disconnect (clear_client = True )
527524
528525 async def modbus_read_holding_registers (self , unit , address , rcount ):
529526 """Read modbus registers from inverter."""
@@ -588,93 +585,92 @@ async def modbus_read_holding_registers(self, unit, address, rcount):
588585 async def write_registers (self , unit : int , address : int , payload ) -> None :
589586 """Write modbus registers to inverter."""
590587
591- async with self ._lock :
592- self ._wr_unit = unit
593- self ._wr_address = address
594- self ._wr_payload = payload
588+ self ._wr_unit = unit
589+ self ._wr_address = address
590+ self ._wr_payload = payload
595591
596- try :
597- if not self .is_connected :
598- await self .connect ()
592+ try :
593+ if not self .is_connected :
594+ await self .connect ()
599595
600- sig = inspect .signature (self ._client .write_registers )
596+ sig = inspect .signature (self ._client .write_registers )
601597
602- if "device_id" in sig .parameters :
603- result = await self ._client .write_registers (
604- address = self ._wr_address ,
605- values = self ._wr_payload ,
606- device_id = self ._wr_unit ,
607- )
608- else :
609- result = await self ._client .write_registers (
610- address = self ._wr_address ,
611- values = self ._wr_payload ,
612- slave = self ._wr_unit ,
613- )
598+ if "device_id" in sig .parameters :
599+ result = await self ._client .write_registers (
600+ address = self ._wr_address ,
601+ values = self ._wr_payload ,
602+ device_id = self ._wr_unit ,
603+ )
604+ else :
605+ result = await self ._client .write_registers (
606+ address = self ._wr_address ,
607+ values = self ._wr_payload ,
608+ slave = self ._wr_unit ,
609+ )
614610
615- self .has_write = address
611+ self .has_write = address
616612
617- if self .sleep_after_write > 0 :
618- _LOGGER .debug (
619- f"Sleep { self .sleep_after_write } seconds after write { address } ."
620- )
621- await asyncio .sleep (self .sleep_after_write )
613+ if self .sleep_after_write > 0 :
614+ _LOGGER .debug (
615+ f"Sleep { self .sleep_after_write } seconds after write { address } ."
616+ )
617+ await asyncio .sleep (self .sleep_after_write )
622618
623- self .has_write = None
624- _LOGGER .debug (f"Finished with write { address } ." )
619+ self .has_write = None
620+ _LOGGER .debug (f"Finished with write { address } ." )
625621
626- except ModbusIOException as e :
627- self .disconnect ()
622+ except ModbusIOException as e :
623+ self .disconnect ()
628624
629- raise HomeAssistantError (
630- f"Error sending command to inverter ID { self ._wr_unit } : { e } ."
631- )
625+ raise HomeAssistantError (
626+ f"Error sending command to inverter ID { self ._wr_unit } : { e } ."
627+ )
632628
633- except ConnectionException as e :
634- self .disconnect ()
629+ except ConnectionException as e :
630+ self .disconnect ()
635631
636- _LOGGER .error (f"Connection failed: { e } " )
632+ _LOGGER .error (f"Connection failed: { e } " )
633+ raise HomeAssistantError (
634+ f"Connection to inverter ID { self ._wr_unit } failed."
635+ )
636+
637+ if result .isError ():
638+ if type (result ) is ModbusIOException :
639+ self .disconnect ()
640+ _LOGGER .error (
641+ f"Write failed: No response from inverter ID { self ._wr_unit } ."
642+ )
637643 raise HomeAssistantError (
638- f"Connection to inverter ID { self ._wr_unit } failed ."
644+ "No response from inverter ID {self._wr_unit}."
639645 )
640646
641- if result .isError ():
642- if type (result ) is ModbusIOException :
643- self .disconnect ()
644- _LOGGER .error (
645- f"Write failed: No response from inverter ID { self ._wr_unit } ."
647+ if type (result ) is ExceptionResponse :
648+ if result .exception_code == ModbusExceptions .IllegalAddress :
649+ _LOGGER .debug (
650+ f"Unit { self ._wr_unit } Write IllegalAddress: { result } "
646651 )
647652 raise HomeAssistantError (
648- "No response from inverter ID {self._wr_unit}."
653+ "Address not supported at device at ID {self._wr_unit}."
649654 )
650655
651- if type (result ) is ExceptionResponse :
652- if result .exception_code == ModbusExceptions .IllegalAddress :
653- _LOGGER .debug (
654- f"Unit { self ._wr_unit } Write IllegalAddress: { result } "
655- )
656- raise HomeAssistantError (
657- "Address not supported at device at ID {self._wr_unit}."
658- )
659-
660- if result .exception_code == ModbusExceptions .IllegalFunction :
661- _LOGGER .debug (
662- f"Unit { self ._wr_unit } Write IllegalFunction: { result } "
663- )
664- raise HomeAssistantError (
665- "Function not supported by device at ID {self._wr_unit}."
666- )
656+ if result .exception_code == ModbusExceptions .IllegalFunction :
657+ _LOGGER .debug (
658+ f"Unit { self ._wr_unit } Write IllegalFunction: { result } "
659+ )
660+ raise HomeAssistantError (
661+ "Function not supported by device at ID {self._wr_unit}."
662+ )
667663
668- if result .exception_code == ModbusExceptions .IllegalValue :
669- _LOGGER .debug (
670- f"Unit { self ._wr_unit } Write IllegalValue: { result } "
671- )
672- raise HomeAssistantError (
673- "Value invalid for device at ID {self._wr_unit}."
674- )
664+ if result .exception_code == ModbusExceptions .IllegalValue :
665+ _LOGGER .debug (
666+ f"Unit { self ._wr_unit } Write IllegalValue: { result } "
667+ )
668+ raise HomeAssistantError (
669+ "Value invalid for device at ID {self._wr_unit}."
670+ )
675671
676- self .disconnect ()
677- raise ModbusWriteError (result )
672+ self .disconnect ()
673+ raise ModbusWriteError (result )
678674
679675 @staticmethod
680676 def _safe_version_tuple (version_str : str ) -> tuple [int , ...]:
0 commit comments