diff --git a/custom_components/solaredge_modbus_multi/hub.py b/custom_components/solaredge_modbus_multi/hub.py index f7808a38..85a4bd95 100644 --- a/custom_components/solaredge_modbus_multi/hub.py +++ b/custom_components/solaredge_modbus_multi/hub.py @@ -12,7 +12,7 @@ from homeassistant.helpers.entity import DeviceInfo from pymodbus.client import AsyncModbusTcpClient from pymodbus.constants import Endian -from pymodbus.exceptions import ConnectionException, ModbusIOException +from pymodbus.exceptions import ConnectionException, ModbusException, ModbusIOException from pymodbus.payload import BinaryPayloadDecoder from pymodbus.pdu import ExceptionResponse @@ -483,11 +483,18 @@ def disconnect(self, clear_client: bool = False) -> None: f"(clear_client={clear_client})." ) ) - self._client.close() + + try: + self._client.close() + except ModbusException as e: + _LOGGER.error(f"ModbusException on close: {e}") if clear_client: + del self._client self._client = None + await asyncio.sleep(1) + async def shutdown(self) -> None: """Shut down the hub and disconnect."""