From ecca3dff58e9b83cf22f0b40371fd8d9d8c967e6 Mon Sep 17 00:00:00 2001 From: David Rapan Date: Sun, 11 Aug 2024 11:53:44 +0200 Subject: [PATCH] fix: Silencing Errno 111 during reconnects --- custom_components/solarman/api.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/custom_components/solarman/api.py b/custom_components/solarman/api.py index 8994606a..918528b4 100644 --- a/custom_components/solarman/api.py +++ b/custom_components/solarman/api.py @@ -24,6 +24,25 @@ def __init__(self, address, serial, port, mb_slave_id, passthrough): super().__init__(address, serial, port = port, mb_slave_id = mb_slave_id, logger = _LOGGER, auto_reconnect = AUTO_RECONNECT, socket_timeout = TIMINGS_SOCKET_TIMEOUT) self._passthrough = passthrough + async def reconnect(self) -> None: + """ + Overridden to silence [ConnectionRefusedError: [Errno 111] Connect call failed] during reconnects + + """ + try: + if self.reader_task: + self.reader_task.cancel() + self.reader, self.writer = await asyncio.open_connection(self.address, self.port) + loop = asyncio.get_running_loop() + self.reader_task = loop.create_task(self._conn_keeper(), name = "ConnKeeper") + self.log.debug("[%s] Successful reconnect", self.serial) + if self.data_wanted_ev.is_set(): + self.log.debug("[%s] Data expected. Will retry the last request", self.serial) + self.writer.write(self._last_frame) + await self.writer.drain() + except Exception as e: + self.log.debug(f"Cannot open connection to {self.address}. [{type(e).__name__}{f': {e}' if f'{e}' else ''}]") + def _received_frame_is_valid(self, frame): return super()._received_frame_is_valid(frame) if not self._passthrough else True