Skip to content

Commit

Permalink
Add constants for the AndroidTV and FireTV 'get_properties' shell com…
Browse files Browse the repository at this point in the history
…mands (#88)

* Add constants for the AndroidTV and FireTV 'get_properties' shell commands

* Fix Fire TV lazy 'get_properties' shell commands

* Fix documentation

* Remove shell command comments from the tests
  • Loading branch information
JeffLIrion committed Sep 6, 2019
1 parent 06b06f7 commit f35b466
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 54 deletions.
11 changes: 4 additions & 7 deletions androidtv/androidtv.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,10 @@ def get_properties(self, lazy=False):
The absolute volume level, or ``None`` if it was not determined
"""
output = self.adb.shell(constants.CMD_SCREEN_ON + (constants.CMD_SUCCESS1 if lazy else constants.CMD_SUCCESS1_FAILURE0) + " && " +
constants.CMD_AUDIO_STATE + " && " +
constants.CMD_AWAKE + (constants.CMD_SUCCESS1 if lazy else constants.CMD_SUCCESS1_FAILURE0) + " && " +
constants.CMD_WAKE_LOCK_SIZE + " && " +
constants.CMD_CURRENT_APP + " && (" +
constants.CMD_MEDIA_SESSION_STATE + " || echo) && " +
constants.CMD_STREAM_MUSIC)
if lazy:
output = self.adb.shell(constants.CMD_ANDROIDTV_PROPERTIES_LAZY)
else:
output = self.adb.shell(constants.CMD_ANDROIDTV_PROPERTIES_NOT_LAZY)
_LOGGER.debug("Android TV %s update response: %s", self.host, output)

# ADB command was unsuccessful
Expand Down
18 changes: 18 additions & 0 deletions androidtv/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@
#: Get the wake lock size
CMD_WAKE_LOCK_SIZE = "dumpsys power | grep Locks | grep 'size='"

#: Get the properties for an :py:class:`~androidtv.androidtv.AndroidTV` device (``lazy=True``)
CMD_ANDROIDTV_PROPERTIES_LAZY = CMD_SCREEN_ON + CMD_SUCCESS1 + " && " + CMD_AWAKE + CMD_SUCCESS1 + " && " + CMD_AUDIO_STATE + " && " + CMD_WAKE_LOCK_SIZE + " && " + CMD_CURRENT_APP + " && (" + CMD_MEDIA_SESSION_STATE + " || echo) && " + CMD_STREAM_MUSIC

#: Get the properties for an :py:class:`~androidtv.androidtv.AndroidTV` device (``lazy=False``)
CMD_ANDROIDTV_PROPERTIES_NOT_LAZY = CMD_SCREEN_ON + CMD_SUCCESS1_FAILURE0 + " && " + CMD_AWAKE + CMD_SUCCESS1_FAILURE0 + " && " + CMD_AUDIO_STATE + " && " + CMD_WAKE_LOCK_SIZE + " && " + CMD_CURRENT_APP + " && (" + CMD_MEDIA_SESSION_STATE + " || echo) && " + CMD_STREAM_MUSIC

#: Get the properties for a :py:class:`~androidtv.firetv.FireTV` device (``lazy=True, get_running_apps=True``)
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_RUNNING_APPS

#: Get the properties for a :py:class:`~androidtv.firetv.FireTV` device (``lazy=True, get_running_apps=False``)
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)"

#: Get the properties for a :py:class:`~androidtv.firetv.FireTV` device (``lazy=False, get_running_apps=True``)
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_RUNNING_APPS

#: Get the properties for a :py:class:`~androidtv.firetv.FireTV` device (``lazy=False, get_running_apps=False``)
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)"

# `getprop` commands
CMD_MANUFACTURER = "getprop ro.product.manufacturer"
CMD_MODEL = "getprop ro.product.model"
Expand Down
21 changes: 9 additions & 12 deletions androidtv/firetv.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,19 +303,16 @@ def get_properties(self, get_running_apps=True, lazy=False):
A list of the running apps, or ``None`` if it was not determined
"""
if get_running_apps:
output = self.adb.shell(constants.CMD_SCREEN_ON + (constants.CMD_SUCCESS1 if lazy else constants.CMD_SUCCESS1_FAILURE0) + " && " +
constants.CMD_AWAKE + (constants.CMD_SUCCESS1 if lazy else constants.CMD_SUCCESS1_FAILURE0) + " && " +
constants.CMD_WAKE_LOCK_SIZE + " && " +
constants.CMD_CURRENT_APP + " && (" +
constants.CMD_MEDIA_SESSION_STATE + " || echo) && " +
constants.CMD_RUNNING_APPS)
if lazy:
if get_running_apps:
output = self.adb.shell(constants.CMD_FIRETV_PROPERTIES_LAZY_RUNNING_APPS)
else:
output = self.adb.shell(constants.CMD_FIRETV_PROPERTIES_LAZY_NO_RUNNING_APPS)
else:
output = self.adb.shell(constants.CMD_SCREEN_ON + (constants.CMD_SUCCESS1 if lazy else constants.CMD_SUCCESS1_FAILURE0) + " && " +
constants.CMD_AWAKE + (constants.CMD_SUCCESS1 if lazy else constants.CMD_SUCCESS1_FAILURE0) + " && " +
constants.CMD_WAKE_LOCK_SIZE + " && " +
constants.CMD_CURRENT_APP + " && (" +
constants.CMD_MEDIA_SESSION_STATE + " || echo)")
if get_running_apps:
output = self.adb.shell(constants.CMD_FIRETV_PROPERTIES_NOT_LAZY_RUNNING_APPS)
else:
output = self.adb.shell(constants.CMD_FIRETV_PROPERTIES_NOT_LAZY_NO_RUNNING_APPS)
_LOGGER.debug("Fire TV %s update response: %s", self.host, output)

# ADB command was unsuccessful
Expand Down
10 changes: 4 additions & 6 deletions tests/test_androidtv.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from . import patchers


# `adb shell dumpsys audio`
DUMPSYS_AUDIO_OFF = """MediaFocusControl dump time: 9:00:59 AM
Audio Focus stack entries (last is top of stack):
Expand Down Expand Up @@ -140,7 +139,6 @@
muted player piids:"""


# `dumpsys audio | grep '\- STREAM_MUSIC:' -A 12`
STREAM_MUSIC_OFF = """- STREAM_MUSIC:
Muted: false
Min: 0
Expand Down Expand Up @@ -278,7 +276,6 @@
muted player piids:"""


# `dumpsys audio | grep '\- STREAM_MUSIC:' -A 12`
STREAM_MUSIC_ON = """- STREAM_MUSIC:
Muted: false
Min: 0
Expand All @@ -293,7 +290,6 @@
Devices: speaker"""


# `dumpsys power | grep 'Display Power' | grep -q 'state=ON' && 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 mCurrentFocus) && 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 audio`
GET_PROPERTIES_OUTPUT1 = ""
GET_PROPERTIES_DICT1 = {'screen_on': False,
'awake': False,
Expand All @@ -306,7 +302,6 @@
'volume': None}
STATE1 = (constants.STATE_OFF, None, None, None, None)

# `dumpsys power | grep 'Display Power' | grep -q 'state=ON' && 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 mCurrentFocus) && 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 audio`
GET_PROPERTIES_OUTPUT2 = "1"
GET_PROPERTIES_DICT2 = {'screen_on': True,
'awake': False,
Expand All @@ -319,7 +314,6 @@
'volume': None}
STATE2 = (constants.STATE_IDLE, None, None, None, None)

# `dumpsys power | grep 'Display Power' | grep -q 'state=ON' && 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 mCurrentFocus) && 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 audio`
GET_PROPERTIES_OUTPUT3 = """110Wake Locks: size=2
com.amazon.tv.launcher
Expand Down Expand Up @@ -748,6 +742,10 @@ def test_get_properties(self):
properties = self.atv.get_properties_dict(lazy=True)
self.assertEqual(properties, GET_PROPERTIES_DICT4)

with patchers.patch_shell(GET_PROPERTIES_OUTPUT4)[self.PATCH_KEY]:
properties = self.atv.get_properties_dict(lazy=False)
self.assertEqual(properties, GET_PROPERTIES_DICT4)

with patchers.patch_shell(GET_PROPERTIES_OUTPUT_PLEX_STANDBY)[self.PATCH_KEY]:
properties = self.atv.get_properties_dict(lazy=True)
self.assertEqual(properties, GET_PROPERTIES_DICT_PLEX_STANDBY)
Expand Down
3 changes: 0 additions & 3 deletions tests/test_basetv.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from . import patchers


# `adb shell getprop ro.product.manufacturer && getprop ro.product.model && getprop ro.serialno && getprop ro.build.version.release && ip addr show wlan0 | grep -m 1 ether && ip addr show eth0 | grep -m 1 ether`
DEVICE_PROPERTIES_OUTPUT1 = """Amazon
AFTT
SERIALNO
Expand Down Expand Up @@ -57,10 +56,8 @@
'wifimac': None,
'ethmac': 'ab:cd:ef:gh:ij:kl'}

# `adb shell CURRENT_APP=$(dumpsys window windows | grep mCurrentFocus) && 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 {'`
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}"

# `adb shell ps | grep u0_a`
RUNNING_APPS_OUTPUT = """u0_a18 316 197 1189204 115000 ffffffff 00000000 S com.netflix.ninja
u0_a2 15121 197 998628 24628 ffffffff 00000000 S com.amazon.device.controllermanager"""

Expand Down
54 changes: 28 additions & 26 deletions tests/test_firetv.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@
from . import patchers




# `adb shell CURRENT_APP=$(dumpsys window windows | grep mCurrentFocus) && CURRENT_APP=${CURRENT_APP#*{* * } && CURRENT_APP=${CURRENT_APP%%/*} && echo $CURRENT_APP`
CURRENT_APP_OUTPUT = "com.amazon.tv.launcher"

# `adb shell dumpsys power | grep 'Display Power' | grep -q 'state=ON' && echo -e '1\c' && dumpsys power | grep mWakefulness | grep -q Awake && echo -e '1\c' && dumpsys power | grep Locks | grep 'size=' && (dumpsys media_session | grep -m 1 'state=PlaybackState {' || echo) && dumpsys window windows | grep mCurrentFocus && ps | grep u0_a`
GET_PROPERTIES_OUTPUT1 = ""
GET_PROPERTIES_DICT1 = {'screen_on': False,
'awake': False,
Expand All @@ -31,7 +27,6 @@
'running_apps': None}
STATE1 = (constants.STATE_OFF, None, None)

# `adb shell dumpsys power | grep 'Display Power' | grep -q 'state=ON' && 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 mCurrentFocus) && 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) && ps | grep u0_a`
GET_PROPERTIES_OUTPUT2 = "1"
GET_PROPERTIES_DICT2 = {'screen_on': True,
'awake': False,
Expand All @@ -41,7 +36,6 @@
'running_apps': None}
STATE2 = (constants.STATE_IDLE, None, None)

# `adb shell dumpsys power | grep 'Display Power' | grep -q 'state=ON' && 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 mCurrentFocus) && 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) && ps | grep u0_a`
GET_PROPERTIES_OUTPUT3 = """11Wake Locks: size=2
com.amazon.tv.launcher
Expand Down Expand Up @@ -185,26 +179,6 @@ def test_get_properties(self):
properties = self.ftv.get_properties_dict(lazy=True)
self.assertDictEqual(properties, GET_PROPERTIES_DICT3)

self.ftv._state_detection_rules = STATE_DETECTION_RULES1
state = self.ftv.update()
self.assertEqual(state[0], constants.STATE_OFF)

self.ftv._state_detection_rules = STATE_DETECTION_RULES2
state = self.ftv.update()
self.assertEqual(state[0], constants.STATE_OFF)

self.ftv._state_detection_rules = STATE_DETECTION_RULES3
state = self.ftv.update()
self.assertEqual(state[0], constants.STATE_STANDBY)

self.ftv._state_detection_rules = STATE_DETECTION_RULES4
state = self.ftv.update()
self.assertEqual(state[0], constants.STATE_PAUSED)

self.ftv._state_detection_rules = STATE_DETECTION_RULES5
state = self.ftv.update()
self.assertEqual(state[0], constants.STATE_STANDBY)

with patchers.patch_shell(GET_PROPERTIES_OUTPUT3A)[self.PATCH_KEY]:
properties = self.ftv.get_properties_dict(lazy=True)
self.assertDictEqual(properties, GET_PROPERTIES_DICT3A)
Expand All @@ -225,6 +199,10 @@ def test_get_properties(self):
properties = self.ftv.get_properties_dict(lazy=True)
self.assertDictEqual(properties, GET_PROPERTIES_DICT3E)

with patchers.patch_shell(GET_PROPERTIES_OUTPUT3E)[self.PATCH_KEY]:
properties = self.ftv.get_properties_dict(lazy=True, get_running_apps=False)
self.assertDictEqual(properties, GET_PROPERTIES_DICT3E)

with patchers.patch_shell(GET_PROPERTIES_OUTPUT4)[self.PATCH_KEY]:
properties = self.ftv.get_properties_dict(lazy=True)
self.assertDictEqual(properties, GET_PROPERTIES_DICT4)
Expand All @@ -237,6 +215,10 @@ def test_get_properties(self):
properties = self.ftv.get_properties_dict(lazy=True)
self.assertDictEqual(properties, GET_PROPERTIES_DICT5)

with patchers.patch_shell(GET_PROPERTIES_OUTPUT5)[self.PATCH_KEY]:
properties = self.ftv.get_properties_dict(lazy=False)
self.assertDictEqual(properties, GET_PROPERTIES_DICT5)

def test_update(self):
"""Check that the ``update`` method works correctly.
Expand Down Expand Up @@ -265,6 +247,26 @@ def test_update(self):
state = self.ftv.update()
self.assertTupleEqual(state, STATE3)

self.ftv._state_detection_rules = STATE_DETECTION_RULES1
state = self.ftv.update()
self.assertEqual(state[0], constants.STATE_OFF)

self.ftv._state_detection_rules = STATE_DETECTION_RULES2
state = self.ftv.update()
self.assertEqual(state[0], constants.STATE_OFF)

self.ftv._state_detection_rules = STATE_DETECTION_RULES3
state = self.ftv.update()
self.assertEqual(state[0], constants.STATE_STANDBY)

self.ftv._state_detection_rules = STATE_DETECTION_RULES4
state = self.ftv.update()
self.assertEqual(state[0], constants.STATE_PAUSED)

self.ftv._state_detection_rules = STATE_DETECTION_RULES5
state = self.ftv.update()
self.assertEqual(state[0], constants.STATE_STANDBY)

def assertUpdate(self, get_properties, update):
"""Check that the results of the `update` method are as expected.
Expand Down

0 comments on commit f35b466

Please sign in to comment.