Skip to content

Commit

Permalink
Merge b9a6e1a into 27f5063
Browse files Browse the repository at this point in the history
  • Loading branch information
imduffy15 committed Nov 29, 2020
2 parents 27f5063 + b9a6e1a commit f92bf9e
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 0 deletions.
21 changes: 21 additions & 0 deletions androidtv/basetv/basetv.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def __init__(self, adb, host, port=5555, adbkey='', adb_server_ip='', adb_server
self.adb_server_port = adb_server_port
self._state_detection_rules = state_detection_rules
self.device_properties = {}
self.installed_apps = []
self._is_google_tv = False

# make sure the rules are valid
Expand Down Expand Up @@ -376,6 +377,26 @@ def _get_hdmi_input(hdmi_response):
"""
return hdmi_response.strip() if hdmi_response and hdmi_response.strip() else None

@staticmethod
def _installed_apps(installed_apps_response):
"""Get the installed apps from the output of :py:const:`androidtv.constants.CMD_INSTALLED_APPS`.
Parameters
----------
installed_apps_response : str, None
The output of :py:const:`androidtv.constants.CMD_INSTALLED_APPS`
Returns
-------
list, None
A list of the installed apps, or ``None`` if it could not be determined
"""
if installed_apps_response is not None:
return [line.strip().rsplit("package:", 1)[-1] for line in installed_apps_response.splitlines() if line.strip()]

return None

@staticmethod
def _is_volume_muted(stream_music):
"""Determine whether or not the volume is muted from the ``STREAM_MUSIC`` block from ``adb shell dumpsys audio``.
Expand Down
12 changes: 12 additions & 0 deletions androidtv/basetv/basetv_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,18 @@ async def get_hdmi_input(self):
"""
return self._get_hdmi_input(await self._adb.shell(constants.CMD_HDMI_INPUT))

async def get_installed_apps(self):
"""Return a list of installed applications.
Returns
-------
list, None
A list of the installed apps, or ``None`` if it could not be determined
"""
installed_apps_response = await self._adb.shell(constants.CMD_INSTALLED_APPS)
return self._installed_apps(installed_apps_response)

async def is_volume_muted(self):
"""Whether or not the volume is muted.
Expand Down
12 changes: 12 additions & 0 deletions androidtv/basetv/basetv_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,18 @@ def get_hdmi_input(self):
"""
return self._get_hdmi_input(self._adb.shell(constants.CMD_HDMI_INPUT))

def get_installed_apps(self):
"""Return a list of installed applications.
Returns
-------
list, None
A list of the installed apps, or ``None`` if it could not be determined
"""
installed_apps_response = self._adb.shell(constants.CMD_INSTALLED_APPS)
return self._installed_apps(installed_apps_response)

def is_volume_muted(self):
"""Whether or not the volume is muted.
Expand Down
3 changes: 3 additions & 0 deletions androidtv/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
#: Get the running apps for a Fire TV device
CMD_RUNNING_APPS_FIRETV = "ps | grep u0_a"

#: Get installed apps
CMD_INSTALLED_APPS = "pm list packages"

#: Determine if the device is on
CMD_SCREEN_ON = "(dumpsys power | grep 'Display Power' | grep -q 'state=ON' || dumpsys power | grep -q 'mScreenOn=true')"

Expand Down
2 changes: 2 additions & 0 deletions androidtv/setup_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ async def setup(host, port=5555, adbkey='', adb_server_ip='', adb_server_port=50
atv = AndroidTVAsync(host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules, signer)
await atv.adb_connect(auth_timeout_s=auth_timeout_s)
atv.device_properties = await atv.get_device_properties()
atv.installed_apps = await atv.get_installed_apps()
return atv

if device_class == 'firetv':
ftv = FireTVAsync(host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules, signer)
await ftv.adb_connect(auth_timeout_s=auth_timeout_s)
ftv.device_properties = await ftv.get_device_properties()
ftv.installed_apps = await ftv.get_installed_apps()
return ftv

if device_class != 'auto':
Expand Down
22 changes: 22 additions & 0 deletions tests/test_basetv_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@
Device "eth0" does not exist.
"""

INSTALLED_APPS_OUTPUT_1 = """package:org.example.app
package:org.example.launcher
"""

INSTALLED_APPS_LIST = [
"org.example.app",
"org.example.launcher",
]

MEDIA_SESSION_STATE_OUTPUT = "com.amazon.tv.launcher\nstate=PlaybackState {state=2, position=0, buffered position=0, speed=0.0, updated=65749, actions=240640, custom actions=[], active item id=-1, error=null}"

STATE_DETECTION_RULES1 = {'com.amazon.tv.launcher': ['off']}
Expand Down Expand Up @@ -343,6 +352,19 @@ async def test_get_device_properties(self):
device_properties = await self.btv.get_device_properties()
self.assertTrue(self.btv._is_google_tv)

@awaiter
async def test_get_installed_apps(self):
""""Check that `get_installed_apps` works correctly.
"""
with async_patchers.patch_shell(INSTALLED_APPS_OUTPUT_1)[self.PATCH_KEY]:
installed_apps = await self.btv.get_installed_apps()
self.assertListEqual(INSTALLED_APPS_LIST, installed_apps)

with async_patchers.patch_shell(None)[self.PATCH_KEY]:
installed_apps = await self.btv.get_installed_apps()
self.assertEqual(None, installed_apps)

@awaiter
async def test_awake(self):
"""Check that the ``awake`` property works correctly.
Expand Down
21 changes: 21 additions & 0 deletions tests/test_basetv_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@
Device "eth0" does not exist.
"""

INSTALLED_APPS_OUTPUT_1 = """package:org.example.app
package:org.example.launcher
"""

INSTALLED_APPS_LIST = [
"org.example.app",
"org.example.launcher",
]

DEVICE_PROPERTIES_DICT3 = {'manufacturer': 'Not Amazon',
'model': 'AFTT',
'serialno': 'SERIALNO',
Expand Down Expand Up @@ -337,6 +346,18 @@ def test_get_device_properties(self):
device_properties = self.btv.get_device_properties()
self.assertTrue(self.btv._is_google_tv)

def test_get_installed_apps(self):
""""Check that `get_installed_apps` works correctly.
"""
with patchers.patch_shell(INSTALLED_APPS_OUTPUT_1)[self.PATCH_KEY]:
installed_apps = self.btv.get_installed_apps()
self.assertListEqual(INSTALLED_APPS_LIST, installed_apps)

with patchers.patch_shell(None)[self.PATCH_KEY]:
installed_apps = self.btv.get_installed_apps()
self.assertEqual(None, installed_apps)

def test_awake(self):
"""Check that the ``awake`` property works correctly.
Expand Down

0 comments on commit f92bf9e

Please sign in to comment.