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
  • Loading branch information
JeffLIrion committed Sep 6, 2019
1 parent 06b06f7 commit c9c527f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 39 deletions.
11 changes: 4 additions & 7 deletions androidtv/androidtv.py
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
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 an ~:py:class:`androidtv.firetv.FireTV` device (``lazy=True, get_running_apps=True``)
CMD_FIRETV_PROPERTIES_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 an ~:py:class:`androidtv.firetv.FireTV` device (``lazy=True, get_running_apps=False``)
CMD_FIRETV_PROPERTIES_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)"

#: Get the properties for an ~: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 an ~: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
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
4 changes: 4 additions & 0 deletions tests/test_androidtv.py
Expand Up @@ -748,6 +748,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
48 changes: 28 additions & 20 deletions tests/test_firetv.py
Expand Up @@ -185,26 +185,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 +205,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 +221,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 +253,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 c9c527f

Please sign in to comment.