XaF / qolsysgw

Qolsys IQ Panel 2+ gateway to an Home Assistant Alarm Control Panel
MIT License
128 stars 12 forks source link

Integration's socket doesn't reconnect upon [some?] failures #2

Closed kevin-david closed 2 years ago

kevin-david commented 2 years ago

When I changed some wifi settings earlier, the sensors became Unavailable and never came back. Restarting AppDaemon fixed this, but it seems like a good idea to be resilient to transient network issues inside the integration itself

2022-01-01 00:28:38.165051 DEBUG qolsys_panel: Sending keep-alive
2022-01-01 00:28:38.303066 ERROR qolsys_panel: error while listening
Traceback (most recent call last):
  File "/config/appdaemon/apps/qolsysgw/qolsys/socket.py", line 81, in listen
    line = await reader.readline()
  File "/usr/lib/python3.9/asyncio/streams.py", line 540, in readline
    line = await self.readuntil(sep)
  File "/usr/lib/python3.9/asyncio/streams.py", line 632, in readuntil
    await self._wait_for_data('readuntil')
  File "/usr/lib/python3.9/asyncio/streams.py", line 517, in _wait_for_data
    await self._waiter
  File "/usr/lib/python3.9/asyncio/selector_events.py", line 856, in _read_ready__data_received
    data = self._sock.recv(self.max_size)
ConnectionResetError: [Errno 104] Connection reset by peer
2022-01-01 00:28:38.308925 DEBUG qolsys_panel: Qolsys callback for disconnection event
2022-01-01 00:28:38.311177 DEBUG qolsys_panel: call_service: mqtt/publish, {'namespace': 'mqtt', 'retain': True, 'topic': 'homeassistant/alarm_control_panel/qolsys_panel/availability', 'payload': 'offline'}
2022-01-01 00:28:38.316187 ERROR qolsys_panel: Task exception was never retrieved
future: <Task finished name='Task-335' coro=<QolsysSocket.listen() done, defined at /config/appdaemon/apps/qolsysgw/qolsys/socket.py:59> exception=ConnectionResetError(104, 'Connection reset by peer')>
Traceback (most recent call last):
  File "/config/appdaemon/apps/qolsysgw/qolsys/socket.py", line 121, in listen
    await writer.wait_closed()
  File "/usr/lib/python3.9/asyncio/streams.py", line 359, in wait_closed
    await self._protocol._get_close_waiter(self)
  File "/config/appdaemon/apps/qolsysgw/qolsys/socket.py", line 81, in listen
    line = await reader.readline()
  File "/usr/lib/python3.9/asyncio/streams.py", line 540, in readline
    line = await self.readuntil(sep)
  File "/usr/lib/python3.9/asyncio/streams.py", line 632, in readuntil
    await self._wait_for_data('readuntil')
  File "/usr/lib/python3.9/asyncio/streams.py", line 517, in _wait_for_data
    await self._waiter
  File "/usr/lib/python3.9/asyncio/selector_events.py", line 856, in _read_ready__data_received
    data = self._sock.recv(self.max_size)
ConnectionResetError: [Errno 104] Connection reset by peer
XaF commented 2 years ago

Thanks for the report! If you can reproduce the error, please try again with the dev branch, as I just pushed a small fix that should resolve that issue! :) Seemed to have been caused by the fact that wait_closed will fail if the connection has been closed on the other side.

kevin-david commented 2 years ago

I know it's probably not the same - but I attempted to repro this with your changes by blocking the panel from connecting to wifi for a while and eventually allowing it back in. When it was allowed back in and rebooted, it reconnected automatically. So that was good!

XaF commented 2 years ago

I'll consider it's good enough a test :) I merged this to the main branch too! Thanks again!