Skip to content

Commit

Permalink
Merge 88d5e40 into e6dbd3d
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffLIrion committed Jan 15, 2022
2 parents e6dbd3d + 88d5e40 commit e080544
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 488 deletions.
13 changes: 8 additions & 5 deletions androidtv/basetv/basetv.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,15 +543,18 @@ def _screen_on_awake_wake_lock_size(output):
Returns
-------
bool
Whether or not the device is on
bool
Whether or not the device is awake (screensaver is not running)
bool, None
Whether or not the device is on, or ``None`` if it could not be determined
bool, None
Whether or not the device is awake (screensaver is not running), or ``None`` if it could not be determined
int, None
The size of the current wake lock, or ``None`` if it could not be determined
"""
if not output:
if output is None:
return None, None, None

if output == "":
return False, False, None

screen_on = output[0] == "1"
Expand Down
74 changes: 0 additions & 74 deletions androidtv/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,80 +274,6 @@
+ CMD_STREAM_MUSIC
)

#: Get the properties for a Fire TV device (``lazy=True, get_running_apps=True``); see :py:meth:`androidtv.firetv.firetv_sync.FireTVSync.get_properties` and :py:meth:`androidtv.firetv.firetv_async.FireTVAsync.get_properties`
CMD_FIRETV_PROPERTIES_LAZY_RUNNING_APPS = (
CMD_SCREEN_ON
+ CMD_SUCCESS1
+ " && "
+ CMD_AWAKE
+ CMD_SUCCESS1
+ " && "
+ CMD_WAKE_LOCK_SIZE
+ " && "
+ CMD_CURRENT_APP
+ " && ("
+ CMD_MEDIA_SESSION_STATE
+ " || echo) && ("
+ CMD_HDMI_INPUT
+ " || echo) && "
+ CMD_RUNNING_APPS_FIRETV
)

#: Get the properties for a Fire TV device (``lazy=True, get_running_apps=False``); see :py:meth:`androidtv.firetv.firetv_sync.FireTVSync.get_properties` and :py:meth:`androidtv.firetv.firetv_async.FireTVAsync.get_properties`
CMD_FIRETV_PROPERTIES_LAZY_NO_RUNNING_APPS = (
CMD_SCREEN_ON
+ CMD_SUCCESS1
+ " && "
+ CMD_AWAKE
+ CMD_SUCCESS1
+ " && "
+ CMD_WAKE_LOCK_SIZE
+ " && "
+ CMD_CURRENT_APP
+ " && ("
+ CMD_MEDIA_SESSION_STATE
+ " || echo) && ("
+ CMD_HDMI_INPUT
+ " || echo)"
)

#: Get the properties for a Fire TV device (``lazy=False, get_running_apps=True``); see :py:meth:`androidtv.firetv.firetv_sync.FireTVSync.get_properties` and :py:meth:`androidtv.firetv.firetv_async.FireTVAsync.get_properties`
CMD_FIRETV_PROPERTIES_NOT_LAZY_RUNNING_APPS = (
CMD_SCREEN_ON
+ CMD_SUCCESS1_FAILURE0
+ " && "
+ CMD_AWAKE
+ CMD_SUCCESS1_FAILURE0
+ " && "
+ CMD_WAKE_LOCK_SIZE
+ " && "
+ CMD_CURRENT_APP
+ " && ("
+ CMD_MEDIA_SESSION_STATE
+ " || echo) && ("
+ CMD_HDMI_INPUT
+ " || echo) && "
+ CMD_RUNNING_APPS_FIRETV
)

#: Get the properties for a Fire TV device (``lazy=False, get_running_apps=False``); see :py:meth:`androidtv.firetv.firetv_sync.FireTVSync.get_properties` and :py:meth:`androidtv.firetv.firetv_async.FireTVAsync.get_properties`
CMD_FIRETV_PROPERTIES_NOT_LAZY_NO_RUNNING_APPS = (
CMD_SCREEN_ON
+ CMD_SUCCESS1_FAILURE0
+ " && "
+ CMD_AWAKE
+ CMD_SUCCESS1_FAILURE0
+ " && "
+ CMD_WAKE_LOCK_SIZE
+ " && "
+ CMD_CURRENT_APP
+ " && ("
+ CMD_MEDIA_SESSION_STATE
+ " || echo) && ("
+ CMD_HDMI_INPUT
+ " || echo)"
)

# `getprop` commands
CMD_MANUFACTURER = "getprop ro.product.manufacturer"
CMD_MODEL = "getprop ro.product.model"
Expand Down
87 changes: 0 additions & 87 deletions androidtv/firetv/base_firetv.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ def __init__(self, host, port=5555, adbkey="", adb_server_ip="", adb_server_port

def _fill_in_commands(self):
"""Fill in commands that are specific to Fire TV devices."""
self._cmd_get_properties_lazy_running_apps = constants.CMD_FIRETV_PROPERTIES_LAZY_RUNNING_APPS
self._cmd_get_properties_lazy_no_running_apps = constants.CMD_FIRETV_PROPERTIES_LAZY_NO_RUNNING_APPS
self._cmd_get_properties_not_lazy_running_apps = constants.CMD_FIRETV_PROPERTIES_NOT_LAZY_RUNNING_APPS
self._cmd_get_properties_not_lazy_no_running_apps = constants.CMD_FIRETV_PROPERTIES_NOT_LAZY_NO_RUNNING_APPS
self._cmd_current_app = constants.CMD_CURRENT_APP
self._cmd_launch_app = constants.CMD_LAUNCH_APP

Expand Down Expand Up @@ -223,86 +219,3 @@ def _update(self, screen_on, awake, wake_lock_size, current_app, media_session_s
state = constants.STATE_PAUSED

return state, current_app, running_apps, hdmi_input

# ======================================================================= #
# #
# Properties #
# #
# ======================================================================= #
def _get_properties(self, output, get_running_apps=True):
"""Get the properties needed for Home Assistant updates.
This will send one of the following ADB commands:
* :py:const:`androidtv.constants.CMD_FIRETV_PROPERTIES_LAZY_RUNNING_APPS`
* :py:const:`androidtv.constants.CMD_FIRETV_PROPERTIES_LAZY_NO_RUNNING_APPS`
* :py:const:`androidtv.constants.CMD_FIRETV_PROPERTIES_NOT_LAZY_RUNNING_APPS`
* :py:const:`androidtv.constants.CMD_FIRETV_PROPERTIES_NOT_LAZY_NO_RUNNING_APPS`
Parameters
----------
output : str, None
The output of the ADB command used to retrieve the properties
get_running_apps : bool
Whether or not to get the ``running_apps`` property
Returns
-------
screen_on : bool, None
Whether or not the device is on, or ``None`` if it was not determined
awake : bool, None
Whether or not the device is awake (screensaver is not running), or ``None`` if it was not determined
wake_lock_size : int, None
The size of the current wake lock, or ``None`` if it was not determined
current_app : str, None
The current app property, or ``None`` if it was not determined
media_session_state : int, None
The state from the output of ``dumpsys media_session``, or ``None`` if it was not determined
running_apps : list, None
A list of the running apps, or ``None`` if it was not determined
hdmi_input : str, None
The HDMI input, or ``None`` if it could not be determined
"""
# ADB command was unsuccessful
if output is None:
return None, None, None, None, None, None, None

# `screen_on` property
if not output:
return False, False, -1, None, None, None, None
screen_on = output[0] == "1"

# `awake` property
if len(output) < 2:
return screen_on, False, -1, None, None, None, None
awake = output[1] == "1"

lines = output.strip().splitlines()

# `wake_lock_size` property
if len(lines[0]) < 3:
return screen_on, awake, -1, None, None, None, None
wake_lock_size = self._wake_lock_size(lines[0])

# `current_app` property
if len(lines) < 2:
return screen_on, awake, wake_lock_size, None, None, None, None
current_app = self._current_app(lines[1])

# `media_session_state` property
if len(lines) < 3:
return screen_on, awake, wake_lock_size, current_app, None, None, None
media_session_state = self._media_session_state(lines[2], current_app)

# HDMI input property
if len(lines) < 4:
return screen_on, awake, wake_lock_size, current_app, media_session_state, None, None
hdmi_input = self._get_hdmi_input(lines[3])

# `running_apps` property
if not get_running_apps or len(lines) < 5:
return screen_on, awake, wake_lock_size, current_app, media_session_state, None, hdmi_input
running_apps = self._running_apps(lines[4:])

return screen_on, awake, wake_lock_size, current_app, media_session_state, running_apps, hdmi_input
30 changes: 12 additions & 18 deletions androidtv/firetv/firetv_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,6 @@ async def update(self, get_running_apps=True, lazy=True):
async def get_properties(self, get_running_apps=True, lazy=False):
"""Get the properties needed for Home Assistant updates.
This will send one of the following ADB commands:
* :py:const:`androidtv.constants.CMD_FIRETV_PROPERTIES_LAZY_RUNNING_APPS`
* :py:const:`androidtv.constants.CMD_FIRETV_PROPERTIES_LAZY_NO_RUNNING_APPS`
* :py:const:`androidtv.constants.CMD_FIRETV_PROPERTIES_NOT_LAZY_RUNNING_APPS`
* :py:const:`androidtv.constants.CMD_FIRETV_PROPERTIES_NOT_LAZY_NO_RUNNING_APPS`
Parameters
----------
get_running_apps : bool
Expand All @@ -132,19 +125,20 @@ async def get_properties(self, get_running_apps=True, lazy=False):
The HDMI input, or ``None`` if it could not be determined
"""
if lazy:
if get_running_apps:
output = await self._adb.shell(self._cmd_get_properties_lazy_running_apps)
else:
output = await self._adb.shell(self._cmd_get_properties_lazy_no_running_apps)
screen_on, awake, wake_lock_size = await self.screen_on_awake_wake_lock_size()
if lazy and not (screen_on and awake):
return screen_on, awake, wake_lock_size, None, None, None, None

current_app, media_session_state = await self.current_app_media_session_state()

if get_running_apps:
running_apps = await self.running_apps()
else:
if get_running_apps:
output = await self._adb.shell(self._cmd_get_properties_not_lazy_running_apps)
else:
output = await self._adb.shell(self._cmd_get_properties_not_lazy_no_running_apps)
_LOGGER.debug("Fire TV %s:%d `get_properties` response: %s", self.host, self.port, output)
running_apps = None

hdmi_input = await self.get_hdmi_input()

return self._get_properties(output, get_running_apps)
return screen_on, awake, wake_lock_size, current_app, media_session_state, running_apps, hdmi_input

async def get_properties_dict(self, get_running_apps=True, lazy=True):
"""Get the properties needed for Home Assistant updates and return them as a dictionary.
Expand Down
30 changes: 12 additions & 18 deletions androidtv/firetv/firetv_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,6 @@ def update(self, get_running_apps=True, lazy=True):
def get_properties(self, get_running_apps=True, lazy=False):
"""Get the properties needed for Home Assistant updates.
This will send one of the following ADB commands:
* :py:const:`androidtv.constants.CMD_FIRETV_PROPERTIES_LAZY_RUNNING_APPS`
* :py:const:`androidtv.constants.CMD_FIRETV_PROPERTIES_LAZY_NO_RUNNING_APPS`
* :py:const:`androidtv.constants.CMD_FIRETV_PROPERTIES_NOT_LAZY_RUNNING_APPS`
* :py:const:`androidtv.constants.CMD_FIRETV_PROPERTIES_NOT_LAZY_NO_RUNNING_APPS`
Parameters
----------
get_running_apps : bool
Expand All @@ -132,19 +125,20 @@ def get_properties(self, get_running_apps=True, lazy=False):
The HDMI input, or ``None`` if it could not be determined
"""
if lazy:
if get_running_apps:
output = self._adb.shell(self._cmd_get_properties_lazy_running_apps)
else:
output = self._adb.shell(self._cmd_get_properties_lazy_no_running_apps)
screen_on, awake, wake_lock_size = self.screen_on_awake_wake_lock_size()
if lazy and not (screen_on and awake):
return screen_on, awake, wake_lock_size, None, None, None, None

current_app, media_session_state = self.current_app_media_session_state()

if get_running_apps:
running_apps = self.running_apps()
else:
if get_running_apps:
output = self._adb.shell(self._cmd_get_properties_not_lazy_running_apps)
else:
output = self._adb.shell(self._cmd_get_properties_not_lazy_no_running_apps)
_LOGGER.debug("Fire TV %s:%d `get_properties` response: %s", self.host, self.port, output)
running_apps = None

hdmi_input = self.get_hdmi_input()

return self._get_properties(output, get_running_apps)
return screen_on, awake, wake_lock_size, current_app, media_session_state, running_apps, hdmi_input

def get_properties_dict(self, get_running_apps=True, lazy=True):
"""Get the properties needed for Home Assistant updates and return them as a dictionary.
Expand Down
2 changes: 1 addition & 1 deletion tests/test_basetv_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ async def test_screen_on(self):
async def test_screen_on_awake_wake_lock_size(self):
"""Check that the ``screen_on_awake_wake_lock_size`` property works correctly."""
with async_patchers.patch_shell(None)[self.PATCH_KEY]:
self.assertTupleEqual(await self.btv.screen_on_awake_wake_lock_size(), (False, False, None))
self.assertTupleEqual(await self.btv.screen_on_awake_wake_lock_size(), (None, None, None))

with async_patchers.patch_shell("")[self.PATCH_KEY]:
self.assertTupleEqual(await self.btv.screen_on_awake_wake_lock_size(), (False, False, None))
Expand Down
2 changes: 1 addition & 1 deletion tests/test_basetv_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ def test_screen_on(self):
def test_screen_on_awake_wake_lock_size(self):
"""Check that the ``screen_on_awake_wake_lock_size`` property works correctly."""
with patchers.patch_shell(None)[self.PATCH_KEY]:
self.assertTupleEqual(self.btv.screen_on_awake_wake_lock_size(), (False, False, None))
self.assertTupleEqual(self.btv.screen_on_awake_wake_lock_size(), (None, None, None))

with patchers.patch_shell("")[self.PATCH_KEY]:
self.assertTupleEqual(self.btv.screen_on_awake_wake_lock_size(), (False, False, None))
Expand Down
24 changes: 0 additions & 24 deletions tests/test_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,30 +119,6 @@ def test_constants(self):
r"(dumpsys power | grep 'Display Power' | grep -q 'state=ON' || dumpsys power | grep -q 'mScreenOn=true') && echo -e '1\c' || echo -e '0\c' && dumpsys power | grep mWakefulness | grep -q Awake && echo -e '1\c' || echo -e '0\c' && (dumpsys audio | grep paused | grep -qv 'Buffer Queue' && echo -e '1\c' || (dumpsys audio | grep started | grep -qv 'Buffer Queue' && echo '2\c' || echo '0\c')) && dumpsys power | grep Locks | grep 'size=' && CURRENT_APP=$(dumpsys activity a . | grep mResumedActivity) && CURRENT_APP=${CURRENT_APP#*ActivityRecord{* * } && CURRENT_APP=${CURRENT_APP#*{* * } && CURRENT_APP=${CURRENT_APP%%/*} && CURRENT_APP=${CURRENT_APP%\}*} && echo $CURRENT_APP && (dumpsys media_session | grep -A 100 'Sessions Stack' | grep -A 100 $CURRENT_APP | grep -m 1 'state=PlaybackState {' || echo) && (dumpsys activity starter | grep -E -o '(ExternalTv|HDMI)InputService/HW[0-9]' -m 1 | grep -o 'HW[0-9]' || echo) && dumpsys audio | grep '\- STREAM_MUSIC:' -A 11",
)

# CMD_FIRETV_PROPERTIES_LAZY_RUNNING_APPS
self.assertEqual(
constants.CMD_FIRETV_PROPERTIES_LAZY_RUNNING_APPS,
r"(dumpsys power | grep 'Display Power' | grep -q 'state=ON' || dumpsys power | grep -q 'mScreenOn=true') && echo -e '1\c' && dumpsys power | grep mWakefulness | grep -q Awake && echo -e '1\c' && dumpsys power | grep Locks | grep 'size=' && CURRENT_APP=$(dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp') && CURRENT_APP=${CURRENT_APP#*ActivityRecord{* * } && CURRENT_APP=${CURRENT_APP#*{* * } && CURRENT_APP=${CURRENT_APP%%/*} && CURRENT_APP=${CURRENT_APP%\}*} && echo $CURRENT_APP && (dumpsys media_session | grep -A 100 'Sessions Stack' | grep -A 100 $CURRENT_APP | grep -m 1 'state=PlaybackState {' || echo) && (dumpsys activity starter | grep -E -o '(ExternalTv|HDMI)InputService/HW[0-9]' -m 1 | grep -o 'HW[0-9]' || echo) && ps | grep u0_a",
)

# CMD_FIRETV_PROPERTIES_LAZY_NO_RUNNING_APPS
self.assertEqual(
constants.CMD_FIRETV_PROPERTIES_LAZY_NO_RUNNING_APPS,
r"(dumpsys power | grep 'Display Power' | grep -q 'state=ON' || dumpsys power | grep -q 'mScreenOn=true') && echo -e '1\c' && dumpsys power | grep mWakefulness | grep -q Awake && echo -e '1\c' && dumpsys power | grep Locks | grep 'size=' && CURRENT_APP=$(dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp') && CURRENT_APP=${CURRENT_APP#*ActivityRecord{* * } && CURRENT_APP=${CURRENT_APP#*{* * } && CURRENT_APP=${CURRENT_APP%%/*} && CURRENT_APP=${CURRENT_APP%\}*} && echo $CURRENT_APP && (dumpsys media_session | grep -A 100 'Sessions Stack' | grep -A 100 $CURRENT_APP | grep -m 1 'state=PlaybackState {' || echo) && (dumpsys activity starter | grep -E -o '(ExternalTv|HDMI)InputService/HW[0-9]' -m 1 | grep -o 'HW[0-9]' || echo)",
)

# CMD_FIRETV_PROPERTIES_NOT_LAZY_RUNNING_APPS
self.assertEqual(
constants.CMD_FIRETV_PROPERTIES_NOT_LAZY_RUNNING_APPS,
r"(dumpsys power | grep 'Display Power' | grep -q 'state=ON' || dumpsys power | grep -q 'mScreenOn=true') && echo -e '1\c' || echo -e '0\c' && dumpsys power | grep mWakefulness | grep -q Awake && echo -e '1\c' || echo -e '0\c' && dumpsys power | grep Locks | grep 'size=' && CURRENT_APP=$(dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp') && CURRENT_APP=${CURRENT_APP#*ActivityRecord{* * } && CURRENT_APP=${CURRENT_APP#*{* * } && CURRENT_APP=${CURRENT_APP%%/*} && CURRENT_APP=${CURRENT_APP%\}*} && echo $CURRENT_APP && (dumpsys media_session | grep -A 100 'Sessions Stack' | grep -A 100 $CURRENT_APP | grep -m 1 'state=PlaybackState {' || echo) && (dumpsys activity starter | grep -E -o '(ExternalTv|HDMI)InputService/HW[0-9]' -m 1 | grep -o 'HW[0-9]' || echo) && ps | grep u0_a",
)

# CMD_FIRETV_PROPERTIES_NOT_LAZY_NO_RUNNING_APPS
self.assertEqual(
constants.CMD_FIRETV_PROPERTIES_NOT_LAZY_NO_RUNNING_APPS,
r"(dumpsys power | grep 'Display Power' | grep -q 'state=ON' || dumpsys power | grep -q 'mScreenOn=true') && echo -e '1\c' || echo -e '0\c' && dumpsys power | grep mWakefulness | grep -q Awake && echo -e '1\c' || echo -e '0\c' && dumpsys power | grep Locks | grep 'size=' && CURRENT_APP=$(dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp') && CURRENT_APP=${CURRENT_APP#*ActivityRecord{* * } && CURRENT_APP=${CURRENT_APP#*{* * } && CURRENT_APP=${CURRENT_APP%%/*} && CURRENT_APP=${CURRENT_APP%\}*} && echo $CURRENT_APP && (dumpsys media_session | grep -A 100 'Sessions Stack' | grep -A 100 $CURRENT_APP | grep -m 1 'state=PlaybackState {' || echo) && (dumpsys activity starter | grep -E -o '(ExternalTv|HDMI)InputService/HW[0-9]' -m 1 | grep -o 'HW[0-9]' || echo)",
)

def test_current_app_extraction_atv_launcher(self):
dumpsys_output = """
mCurrentFocus=Window{e74bb23 u0 com.google.android.tvlauncher/com.google.android.tvlauncher.MainActivity}
Expand Down
Loading

0 comments on commit e080544

Please sign in to comment.