Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/tv.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import os
import socket
import time
from asyncio import AbstractEventLoop, timeout
from asyncio import AbstractEventLoop, Lock, timeout
from enum import IntEnum
from functools import wraps
from typing import Any, Awaitable, Callable, Concatenate, Coroutine, ParamSpec, TypeVar
Expand Down Expand Up @@ -232,6 +232,7 @@ def __init__(
self._use_app_url = not device_config.use_chromecast
self._player_state = media_player.States.ON
self._muted = False
self._connect_lock = Lock()

def __del__(self):
"""Destructs instance, disconnect AndroidTVRemote."""
Expand Down Expand Up @@ -422,14 +423,17 @@ async def connect(self, max_timeout: int | None = None) -> bool:
:return: True if connected or connecting, False if timeout or authentication error occurred.
"""
# if we are already connecting, simply ignore further connect calls
if self._state == DeviceState.CONNECTING:
if self._connect_lock.locked():
_LOG.debug("[%s] Connection task already running", self.log_id)
return True

await self._connect_lock.acquire()

if isinstance(self._atv.is_on, bool) and self._atv.is_on:
_LOG.debug("[%s] Android TV is already connected", self.log_id)
# just to make sure the state is up-to-date
self.events.emit(Events.CONNECTED, self._identifier)
self._connect_lock.release()
return True

self._state = DeviceState.CONNECTING
Expand Down Expand Up @@ -485,6 +489,7 @@ async def connect(self, max_timeout: int | None = None) -> bool:
)
break

self._connect_lock.release()
if not success:
if self._state == DeviceState.CONNECTING:
self._state = DeviceState.ERROR
Expand Down