Skip to content

Commit

Permalink
Cleanups in telnet API (#293)
Browse files Browse the repository at this point in the history
  • Loading branch information
ol-iver committed May 25, 2024
1 parent 0a6cb3d commit 7efe628
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions denonavr/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,6 @@ class DenonAVRTelnetApi:
host: str = attr.ib(converter=str, default="localhost")
timeout: float = attr.ib(converter=float, default=2.0)
_connection_enabled: bool = attr.ib(default=False)
_healthy: Optional[bool] = attr.ib(
converter=attr.converters.optional(bool), default=None
)
_last_message_time: float = attr.ib(default=-1.0)
_connect_lock: asyncio.Lock = attr.ib(default=attr.Factory(asyncio.Lock))
_reconnect_task: asyncio.Task = attr.ib(default=None)
Expand Down Expand Up @@ -484,7 +481,7 @@ async def _async_establish_connection(self) -> None:
"%s: Connection failed on telnet reconnect", self.host, exc_info=True
)
raise AvrNetworkError(f"OSError: {err}", "telnet connect") from err
_LOGGER.debug("%s: telnet connection complete", self.host)
_LOGGER.debug("%s: telnet connection established", self.host)
self._protocol = cast(DenonAVRTelnetProtocol, transport_protocol[1])
self._connection_enabled = True
self._last_message_time = time.monotonic()
Expand Down Expand Up @@ -527,8 +524,6 @@ def _monitor(self) -> None:
_LOGGER.info(
"%s: Keep alive failed, disconnecting and reconnecting", self.host
)
if self._protocol is not None:
self._protocol.close()
self._handle_disconnected()
return

Expand All @@ -540,8 +535,10 @@ def _monitor(self) -> None:

def _handle_disconnected(self) -> None:
"""Handle disconnected."""
_LOGGER.debug("%s: disconnected", self.host)
self._protocol = None
_LOGGER.debug("%s: handle disconnected", self.host)
if self._protocol is not None:
self._protocol.close()
self._protocol = None
self._stop_monitor()
if not self._connection_enabled:
return
Expand All @@ -550,6 +547,7 @@ def _handle_disconnected(self) -> None:
async def async_disconnect(self) -> None:
"""Close the connection to the receiver asynchronously."""
async with self._connect_lock:
_LOGGER.debug("%s: telnet disconnecting", self.host)
self._connection_enabled = False
self._stop_monitor()
reconnect_task = self._reconnect_task
Expand All @@ -565,13 +563,15 @@ async def async_disconnect(self) -> None:
await reconnect_task
except asyncio.CancelledError:
pass
_LOGGER.debug("%s: telnet disconnected", self.host)

async def _async_reconnect(self) -> None:
"""Reconnect to the receiver asynchronously."""
backoff = 0.5

while self._connection_enabled and not self.healthy:
async with self._connect_lock:
_LOGGER.debug("%s: Telnet reconnecting", self.host)
try:
await self._async_establish_connection()
except AvrTimoutError:
Expand Down

0 comments on commit 7efe628

Please sign in to comment.