FHEM & reconnecting client fixes

This commit is contained in:
Martin Bauer
2019-07-07 17:30:45 +02:00
parent 06f96cb2dc
commit e6f5504181
8 changed files with 34 additions and 13 deletions

View File

@@ -41,14 +41,14 @@ class ReconnectingClient:
self._writer.write(line.encode())
else:
_LOGGER.warning(f"Skipping line '{line}'' because _writer is None")
except RuntimeError:
except RuntimeError as e:
_LOGGER.error("Writing failed " + str(e))
self._connection_task.cancel()
self._connection_task = self.hass.loop.create_task(self._connection())
async def _connection(self):
try:
reader, writer = await asyncio.open_connection(self._host, self._port)
_LOGGER.info("Connected to {} {}:{}".format(self._connection_name, self._host, self._port))
self._connection_last_state = 'CONNECTED'
self._writer = writer
@@ -61,17 +61,16 @@ class ReconnectingClient:
if not line:
raise OSError("Disconnect")
line = line.decode()
_LOGGER.debug("{} received line: {}".format(self._connection_name, line))
_LOGGER.warning("{} received line: {}".format(self._connection_name, line))
await self._receive_line_callback(line)
except OSError:
except OSError as e:
if self._connection_last_state != 'FAILED':
notification_text = "{} connection to {}:{} failed".format(self._connection_name,self._host, self._port)
self.hass.components.persistent_notification.async_create(notification_text, title="No connection")
_LOGGER.error("Connection to {} failed {}:{}".format(self._connection_name, self._host, self._port))
await self._connection_status_changed_callback('disconnected')
self._connection_last_state = 'FAILED'
self.connected = False
await asyncio.sleep(self.reconnect_time)
self.reconnect_time = min(2 * self.reconnect_time, self.reconnect_time_max)
self.hass.loop.create_task(self._connection())
self._connection_task = self.hass.loop.create_task(self._connection())