Skip to content

Commit

Permalink
Separate host and port (#132)
Browse files Browse the repository at this point in the history
* Separate host and port in basetv.py

* Separate host and port in androidtv.py and firetv.py

* Separate host and port in firetv.py (part 2)

* Separate host and port in adb_manager.py

* Linting fix

* Separate host and port in __init__.py

* Separate host and port in tests/test_adb_manager.py

* Fix tests

* Remove commented out code
  • Loading branch information
JeffLIrion committed Dec 10, 2019
1 parent 6a4a908 commit 2b7229e
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 110 deletions.
12 changes: 7 additions & 5 deletions androidtv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
__version__ = '0.0.35'


def setup(host, adbkey='', adb_server_ip='', adb_server_port=5037, state_detection_rules=None, device_class='auto', auth_timeout_s=DEFAULT_AUTH_TIMEOUT_S):
def setup(host, port=5555, adbkey='', adb_server_ip='', adb_server_port=5037, state_detection_rules=None, device_class='auto', auth_timeout_s=DEFAULT_AUTH_TIMEOUT_S):
"""Connect to a device and determine whether it's an Android TV or an Amazon Fire TV.
Parameters
----------
host : str
The address of the device in the format ``<ip address>:<host>``
The address of the device; may be an IP address or a host name
port : int
The device port to which we are connecting (default is 5555)
adbkey : str
The path to the ``adbkey`` file for ADB authentication
adb_server_ip : str
Expand All @@ -39,15 +41,15 @@ def setup(host, adbkey='', adb_server_ip='', adb_server_port=5037, state_detecti
"""
if device_class == 'androidtv':
return AndroidTV(host, adbkey, adb_server_ip, adb_server_port, state_detection_rules, auth_timeout_s)
return AndroidTV(host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules, auth_timeout_s)

if device_class == 'firetv':
return FireTV(host, adbkey, adb_server_ip, adb_server_port, state_detection_rules, auth_timeout_s)
return FireTV(host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules, auth_timeout_s)

if device_class != 'auto':
raise ValueError("`device_class` must be 'androidtv', 'firetv', or 'auto'.")

aftv = BaseTV(host, adbkey, adb_server_ip, adb_server_port, state_detection_rules, auth_timeout_s)
aftv = BaseTV(host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules, auth_timeout_s)

# Fire TV
if aftv.device_properties.get('manufacturer') == 'Amazon':
Expand Down
75 changes: 40 additions & 35 deletions androidtv/adb_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,18 @@ class ADBPython(object):
Parameters
----------
host : str
The address of the device in the format ``<ip address>:<host>``
The address of the device; may be an IP address or a host name
port : int
The device port to which we are connecting (default is 5555)
adbkey : str
The path to the ``adbkey`` file for ADB authentication
"""
def __init__(self, host, adbkey=''):
def __init__(self, host, port, adbkey=''):
self.host = host
self.port = int(port)
self.adbkey = adbkey
self._adb = AdbDevice(serial=self.host, default_timeout_s=9.)
self._adb = AdbDevice(serial='{}:{}'.format(self.host, self.port), default_timeout_s=9.)

# keep track of whether the ADB connection is intact
self._available = False
Expand Down Expand Up @@ -108,15 +111,15 @@ def connect(self, always_log_errors=True, auth_timeout_s=DEFAULT_AUTH_TIMEOUT_S)
self._adb.connect(auth_timeout_s=auth_timeout_s)

# ADB connection successfully established
_LOGGER.debug("ADB connection to %s successfully established", self.host)
_LOGGER.debug("ADB connection to %s:%d successfully established", self.host, self.port)
self._available = True
return True

except OSError as exc:
if self._available or always_log_errors:
if exc.strerror is None:
exc.strerror = "Timed out trying to connect to ADB device."
_LOGGER.warning("Couldn't connect to host %s. %s: %s", self.host, exc.__class__.__name__, exc.strerror)
_LOGGER.warning("Couldn't connect to %s:%d. %s: %s", self.host, self.port, exc.__class__.__name__, exc.strerror)

# ADB connection attempt failed
self.close()
Expand All @@ -125,7 +128,7 @@ def connect(self, always_log_errors=True, auth_timeout_s=DEFAULT_AUTH_TIMEOUT_S)

except Exception as exc: # pylint: disable=broad-except
if self._available or always_log_errors:
_LOGGER.warning("Couldn't connect to host %s. %s: %s", self.host, exc.__class__.__name__, exc)
_LOGGER.warning("Couldn't connect to %s:%d. %s: %s", self.host, self.port, exc.__class__.__name__, exc)

# ADB connection attempt failed
self.close()
Expand All @@ -136,7 +139,7 @@ def connect(self, always_log_errors=True, auth_timeout_s=DEFAULT_AUTH_TIMEOUT_S)
self._adb_lock.release()

# Lock could not be acquired
_LOGGER.warning("Couldn't connect to host %s because adb-shell lock not acquired.", self.host)
_LOGGER.warning("Couldn't connect to %s:%d because adb-shell lock not acquired.", self.host, self.port)
self.close()
self._available = False
return False
Expand All @@ -153,18 +156,18 @@ def pull(self, local_path, device_path):
"""
if not self.available:
_LOGGER.debug("ADB command not sent to %s because adb-shell connection is not established: pull(%s, %s)", self.host, local_path, device_path)
_LOGGER.debug("ADB command not sent to %s:%d because adb-shell connection is not established: pull(%s, %s)", self.host, self.port, local_path, device_path)
return

if self._adb_lock.acquire(**LOCK_KWARGS): # pylint: disable=unexpected-keyword-arg
_LOGGER.debug("Sending command to %s via adb-shell: pull(%s, %s)", self.host, local_path, device_path)
_LOGGER.debug("Sending command to %s:%d via adb-shell: pull(%s, %s)", self.host, self.port, local_path, device_path)
try:
self._adb.pull(device_path, local_path)
finally:
self._adb_lock.release()

# Lock could not be acquired
_LOGGER.warning("ADB command not sent to %s because adb-shell lock not acquired: pull(%s, %s)", self.host, local_path, device_path)
_LOGGER.warning("ADB command not sent to %s:%d because adb-shell lock not acquired: pull(%s, %s)", self.host, self.port, local_path, device_path)
return

def push(self, local_path, device_path):
Expand All @@ -179,18 +182,18 @@ def push(self, local_path, device_path):
"""
if not self.available:
_LOGGER.debug("ADB command not sent to %s because adb-shell connection is not established: push(%s, %s)", self.host, local_path, device_path)
_LOGGER.debug("ADB command not sent to %s:%d because adb-shell connection is not established: push(%s, %s)", self.host, self.port, local_path, device_path)
return

if self._adb_lock.acquire(**LOCK_KWARGS): # pylint: disable=unexpected-keyword-arg
_LOGGER.debug("Sending command to %s via adb-shell: push(%s, %s)", self.host, local_path, device_path)
_LOGGER.debug("Sending command to %s:%d via adb-shell: push(%s, %s)", self.host, self.port, local_path, device_path)
try:
self._adb.push(local_path, device_path)
finally:
self._adb_lock.release()

# Lock could not be acquired
_LOGGER.warning("ADB command not sent to %s because adb-shell lock not acquired: push(%s, %s)", self.host, local_path, device_path)
_LOGGER.warning("ADB command not sent to %s:%d because adb-shell lock not acquired: push(%s, %s)", self.host, self.port, local_path, device_path)
return

def shell(self, cmd):
Expand All @@ -208,18 +211,18 @@ def shell(self, cmd):
"""
if not self.available:
_LOGGER.debug("ADB command not sent to %s because adb-shell connection is not established: %s", self.host, cmd)
_LOGGER.debug("ADB command not sent to %s:%d because adb-shell connection is not established: %s", self.host, self.port, cmd)
return None

if self._adb_lock.acquire(**LOCK_KWARGS): # pylint: disable=unexpected-keyword-arg
_LOGGER.debug("Sending command to %s via adb-shell: %s", self.host, cmd)
_LOGGER.debug("Sending command to %s:%d via adb-shell: %s", self.host, self.port, cmd)
try:
return self._adb.shell(cmd)
finally:
self._adb_lock.release()

# Lock could not be acquired
_LOGGER.warning("ADB command not sent to %s because adb-shell lock not acquired: %s", self.host, cmd)
_LOGGER.warning("ADB command not sent to %s:%d because adb-shell lock not acquired: %s", self.host, self.port, cmd)
return None


Expand All @@ -229,17 +232,18 @@ class ADBServer(object):
Parameters
----------
host : str
The address of the device in the format ``<ip address>:<host>``
adbkey : str
The path to the ``adbkey`` file for ADB authentication
The address of the device; may be an IP address or a host name
port : int
The device port to which we are connecting (default is 5555)
adb_server_ip : str
The IP address of the ADB server
adb_server_port : int
The port for the ADB server
"""
def __init__(self, host, adb_server_ip='', adb_server_port=5037):
def __init__(self, host, port=5555, adb_server_ip='', adb_server_port=5037):
self.host = host
self.port = int(port)
self.adb_server_ip = adb_server_ip
self.adb_server_port = adb_server_port
self._adb_client = None
Expand Down Expand Up @@ -271,7 +275,8 @@ def available(self):
# make sure the device is available
try:
# case 1: the device is currently available
if any([self.host in dev.get_serial_no() for dev in adb_devices]):
host_port = '{}:{}'.format(self.host, self.port)
if any([host_port in dev.get_serial_no() for dev in adb_devices]):
if not self._available:
self._available = True
return True
Expand Down Expand Up @@ -327,17 +332,17 @@ def connect(self, always_log_errors=True):
# Catch exceptions
try:
self._adb_client = Client(host=self.adb_server_ip, port=self.adb_server_port)
self._adb_device = self._adb_client.device(self.host)
self._adb_device = self._adb_client.device('{}:{}'.format(self.host, self.port))

# ADB connection successfully established
if self._adb_device:
_LOGGER.debug("ADB connection to %s via ADB server %s:%s successfully established", self.host, self.adb_server_ip, self.adb_server_port)
_LOGGER.debug("ADB connection to %s:%d via ADB server %s:%d successfully established", self.host, self.port, self.adb_server_ip, self.adb_server_port)
self._available = True
return True

# ADB connection attempt failed (without an exception)
if self._available or always_log_errors:
_LOGGER.warning("Couldn't connect to host %s via ADB server %s:%s", self.host, self.adb_server_ip, self.adb_server_port)
_LOGGER.warning("Couldn't connect to %s:%d via ADB server %s:%d", self.host, self.port, self.adb_server_ip, self.adb_server_port)

self.close()
self._available = False
Expand All @@ -346,7 +351,7 @@ def connect(self, always_log_errors=True):
# ADB connection attempt failed
except Exception as exc: # noqa pylint: disable=broad-except
if self._available or always_log_errors:
_LOGGER.warning("Couldn't connect to host %s via ADB server %s:%s, error: %s", self.host, self.adb_server_ip, self.adb_server_port, exc)
_LOGGER.warning("Couldn't connect to %s:%d via ADB server %s:%d, error: %s", self.host, self.port, self.adb_server_ip, self.adb_server_port, exc)

self.close()
self._available = False
Expand All @@ -356,7 +361,7 @@ def connect(self, always_log_errors=True):
self._adb_lock.release()

# Lock could not be acquired
_LOGGER.warning("Couldn't connect to host %s via ADB server %s:%s because pure-python-adb lock not acquired.", self.host, self.adb_server_ip, self.adb_server_port)
_LOGGER.warning("Couldn't connect to %s:%d via ADB server %s:%d because pure-python-adb lock not acquired.", self.host, self.port, self.adb_server_ip, self.adb_server_port)
self.close()
self._available = False
return False
Expand All @@ -373,18 +378,18 @@ def pull(self, local_path, device_path):
"""
if not self.available:
_LOGGER.debug("ADB command not sent to %s via ADB server %s:%s because pure-python-adb connection is not established: pull(%s, %s)", self.host, self.adb_server_ip, self.adb_server_port, local_path, device_path)
_LOGGER.debug("ADB command not sent to %s:%d via ADB server %s:%d because pure-python-adb connection is not established: pull(%s, %s)", self.host, self.port, self.adb_server_ip, self.adb_server_port, local_path, device_path)
return

if self._adb_lock.acquire(**LOCK_KWARGS): # pylint: disable=unexpected-keyword-arg
_LOGGER.debug("Sending command to %s via ADB server %s:%s: pull(%s, %s)", self.host, self.adb_server_ip, self.adb_server_port, local_path, device_path)
_LOGGER.debug("Sending command to %s:%d via ADB server %s:%d: pull(%s, %s)", self.host, self.port, self.adb_server_ip, self.adb_server_port, local_path, device_path)
try:
self._adb_device.pull(device_path, local_path)
finally:
self._adb_lock.release()

# Lock could not be acquired
_LOGGER.warning("ADB command not sent to %s via ADB server %s:%s: pull(%s, %s)", self.host, self.adb_server_ip, self.adb_server_port, local_path, device_path)
_LOGGER.warning("ADB command not sent to %s:%d via ADB server %s:%d: pull(%s, %s)", self.host, self.port, self.adb_server_ip, self.adb_server_port, local_path, device_path)
return

def push(self, local_path, device_path):
Expand All @@ -399,18 +404,18 @@ def push(self, local_path, device_path):
"""
if not self.available:
_LOGGER.debug("ADB command not sent to %s via ADB server %s:%s because pure-python-adb connection is not established: push(%s, %s)", self.host, self.adb_server_ip, self.adb_server_port, local_path, device_path)
_LOGGER.debug("ADB command not sent to %s:%d via ADB server %s:%d because pure-python-adb connection is not established: push(%s, %s)", self.host, self.port, self.adb_server_ip, self.adb_server_port, local_path, device_path)
return

if self._adb_lock.acquire(**LOCK_KWARGS): # pylint: disable=unexpected-keyword-arg
_LOGGER.debug("Sending command to %s via ADB server %s:%s: push(%s, %s)", self.host, self.adb_server_ip, self.adb_server_port, local_path, device_path)
_LOGGER.debug("Sending command to %s:%d via ADB server %s:%d: push(%s, %s)", self.host, self.port, self.adb_server_ip, self.adb_server_port, local_path, device_path)
try:
self._adb_device.push(local_path, device_path)
finally:
self._adb_lock.release()

# Lock could not be acquired
_LOGGER.warning("ADB command not sent to %s via ADB server %s:%s: push(%s, %s)", self.host, self.adb_server_ip, self.adb_server_port, local_path, device_path)
_LOGGER.warning("ADB command not sent to %s:%d via ADB server %s:%d: push(%s, %s)", self.host, self.port, self.adb_server_ip, self.adb_server_port, local_path, device_path)
return

def shell(self, cmd):
Expand All @@ -428,16 +433,16 @@ def shell(self, cmd):
"""
if not self._available:
_LOGGER.debug("ADB command not sent to %s via ADB server %s:%s because pure-python-adb connection is not established: %s", self.host, self.adb_server_ip, self.adb_server_port, cmd)
_LOGGER.debug("ADB command not sent to %s:%d via ADB server %s:%d because pure-python-adb connection is not established: %s", self.host, self.port, self.adb_server_ip, self.adb_server_port, cmd)
return None

if self._adb_lock.acquire(**LOCK_KWARGS): # pylint: disable=unexpected-keyword-arg
_LOGGER.debug("Sending command to %s via ADB server %s:%s: %s", self.host, self.adb_server_ip, self.adb_server_port, cmd)
_LOGGER.debug("Sending command to %s:%d via ADB server %s:%d: %s", self.host, self.port, self.adb_server_ip, self.adb_server_port, cmd)
try:
return self._adb_device.shell(cmd)
finally:
self._adb_lock.release()

# Lock could not be acquired
_LOGGER.warning("ADB command not sent to %s via ADB server %s:%s because pure-python-adb lock not acquired: %s", self.host, self.adb_server_ip, self.adb_server_port, cmd)
_LOGGER.warning("ADB command not sent to %s:%d via ADB server %s:%d because pure-python-adb lock not acquired: %s", self.host, self.port, self.adb_server_ip, self.adb_server_port, cmd)
return None
10 changes: 6 additions & 4 deletions androidtv/androidtv.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ class AndroidTV(BaseTV):
Parameters
----------
host : str
The address of the device in the format ``<ip address>:<host>``
The address of the device; may be an IP address or a host name
port : int
The device port to which we are connecting (default is 5555)
adbkey : str
The path to the ``adbkey`` file for ADB authentication
adb_server_ip : str
Expand All @@ -34,8 +36,8 @@ class AndroidTV(BaseTV):

DEVICE_CLASS = 'androidtv'

def __init__(self, host, adbkey='', adb_server_ip='', adb_server_port=5037, state_detection_rules=None, auth_timeout_s=constants.DEFAULT_AUTH_TIMEOUT_S):
BaseTV.__init__(self, host, adbkey, adb_server_ip, adb_server_port, state_detection_rules, auth_timeout_s)
def __init__(self, host, port=5555, adbkey='', adb_server_ip='', adb_server_port=5037, state_detection_rules=None, auth_timeout_s=constants.DEFAULT_AUTH_TIMEOUT_S):
BaseTV.__init__(self, host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules, auth_timeout_s)

# ======================================================================= #
# #
Expand Down Expand Up @@ -230,7 +232,7 @@ def get_properties(self, get_running_apps=True, lazy=False):
output = self._adb.shell(constants.CMD_ANDROIDTV_PROPERTIES_NOT_LAZY_RUNNING_APPS)
else:
output = self._adb.shell(constants.CMD_ANDROIDTV_PROPERTIES_NOT_LAZY_NO_RUNNING_APPS)
_LOGGER.debug("Android TV %s `get_properties` response: %s", self.host, output)
_LOGGER.debug("Android TV %s:%d `get_properties` response: %s", self.host, self.port, output)

# ADB command was unsuccessful
if output is None:
Expand Down
Loading

0 comments on commit 2b7229e

Please sign in to comment.