From 7c4a509c46a0f54fb639761a9453ec667447f0a4 Mon Sep 17 00:00:00 2001 From: WillCodeForCats <48533968+WillCodeForCats@users.noreply.github.com> Date: Sat, 4 Jan 2025 11:24:16 -0800 Subject: [PATCH 1/5] Sleep for 1 second on disconnect --- custom_components/solaredge_modbus_multi/hub.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/custom_components/solaredge_modbus_multi/hub.py b/custom_components/solaredge_modbus_multi/hub.py index f7808a38..a890b68a 100644 --- a/custom_components/solaredge_modbus_multi/hub.py +++ b/custom_components/solaredge_modbus_multi/hub.py @@ -488,6 +488,8 @@ def disconnect(self, clear_client: bool = False) -> None: if clear_client: self._client = None + await asyncio.sleep(1) + async def shutdown(self) -> None: """Shut down the hub and disconnect.""" From 775227932cc3940098205fe34b714b13d84e8cbd Mon Sep 17 00:00:00 2001 From: WillCodeForCats <48533968+WillCodeForCats@users.noreply.github.com> Date: Sat, 4 Jan 2025 11:26:48 -0800 Subject: [PATCH 2/5] Catch exceptions from close() --- custom_components/solaredge_modbus_multi/hub.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/custom_components/solaredge_modbus_multi/hub.py b/custom_components/solaredge_modbus_multi/hub.py index a890b68a..20888e77 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,7 +483,11 @@ def disconnect(self, clear_client: bool = False) -> None: f"(clear_client={clear_client})." ) ) - self._client.close() + + try: + self._client.close() + except ModbusException as exception_error: + _LOGGER.error(str(exception_error)) if clear_client: self._client = None From d303c7224900810d631b171e502a226e37bd6095 Mon Sep 17 00:00:00 2001 From: WillCodeForCats <48533968+WillCodeForCats@users.noreply.github.com> Date: Sat, 4 Jan 2025 11:27:04 -0800 Subject: [PATCH 3/5] Delete client object on clear --- custom_components/solaredge_modbus_multi/hub.py | 1 + 1 file changed, 1 insertion(+) diff --git a/custom_components/solaredge_modbus_multi/hub.py b/custom_components/solaredge_modbus_multi/hub.py index 20888e77..2e9b2212 100644 --- a/custom_components/solaredge_modbus_multi/hub.py +++ b/custom_components/solaredge_modbus_multi/hub.py @@ -490,6 +490,7 @@ def disconnect(self, clear_client: bool = False) -> None: _LOGGER.error(str(exception_error)) if clear_client: + del self._client self._client = None await asyncio.sleep(1) From e4f5faf866410a15eddffef784ac738c0ef0778d Mon Sep 17 00:00:00 2001 From: WillCodeForCats <48533968+WillCodeForCats@users.noreply.github.com> Date: Sat, 4 Jan 2025 11:28:31 -0800 Subject: [PATCH 4/5] Update hub.py --- custom_components/solaredge_modbus_multi/hub.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/custom_components/solaredge_modbus_multi/hub.py b/custom_components/solaredge_modbus_multi/hub.py index 2e9b2212..11c9bc54 100644 --- a/custom_components/solaredge_modbus_multi/hub.py +++ b/custom_components/solaredge_modbus_multi/hub.py @@ -486,8 +486,8 @@ def disconnect(self, clear_client: bool = False) -> None: try: self._client.close() - except ModbusException as exception_error: - _LOGGER.error(str(exception_error)) + except ModbusException as e: + _LOGGER.error(f"Modbus error: {e}") if clear_client: del self._client From 617c9117c2b96d99335c9da9ecee89008e1c0197 Mon Sep 17 00:00:00 2001 From: WillCodeForCats <48533968+WillCodeForCats@users.noreply.github.com> Date: Sat, 4 Jan 2025 11:29:29 -0800 Subject: [PATCH 5/5] Update hub.py --- custom_components/solaredge_modbus_multi/hub.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/solaredge_modbus_multi/hub.py b/custom_components/solaredge_modbus_multi/hub.py index 11c9bc54..85a4bd95 100644 --- a/custom_components/solaredge_modbus_multi/hub.py +++ b/custom_components/solaredge_modbus_multi/hub.py @@ -487,7 +487,7 @@ def disconnect(self, clear_client: bool = False) -> None: try: self._client.close() except ModbusException as e: - _LOGGER.error(f"Modbus error: {e}") + _LOGGER.error(f"ModbusException on close: {e}") if clear_client: del self._client