Skip to content

Commit

Permalink
Merge 3e8d4ca into f35b466
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffLIrion committed Sep 6, 2019
2 parents f35b466 + 3e8d4ca commit 56ad260
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 18 deletions.
5 changes: 5 additions & 0 deletions androidtv/androidtv.py
Expand Up @@ -181,6 +181,11 @@ def update(self):
def get_properties(self, lazy=False):
"""Get the properties needed for Home Assistant updates.
This will send one of the following ADB commands:
* :py:const:`androidtv.constants.CMD_ANDROIDTV_PROPERTIES_LAZY`
* :py:const:`androidtv.constants.CMD_ANDROIDTV_PROPERTIES_NOT_LAZY`
Parameters
----------
lazy : bool
Expand Down
18 changes: 9 additions & 9 deletions androidtv/basetv.py
Expand Up @@ -1118,8 +1118,8 @@ def state_detection_rules_validator(rules, exc=KeyError):

# If a rule is a string, check that it is valid
if isinstance(rule, str):
if rule not in constants.VALID_PROPERTIES + constants.VALID_STATES:
raise exc("Invalid rule '{0}' is not in {1}".format(rule, constants.VALID_PROPERTIES + constants.VALID_STATES))
if rule not in constants.VALID_STATE_PROPERTIES + constants.VALID_STATES:
raise exc("Invalid rule '{0}' is not in {1}".format(rule, constants.VALID_STATE_PROPERTIES + constants.VALID_STATES))

# If a rule is a dictionary, check that it is valid
else:
Expand All @@ -1132,17 +1132,17 @@ def state_detection_rules_validator(rules, exc=KeyError):
if not isinstance(conditions, dict):
raise exc("Expected a map for entry '{0}' in 'state_detection_rules', got {1}".format(state, type(conditions).__name__))

for condition, value in conditions.items():
# The keys of the dictionary must be valid conditions that can be checked
if condition not in constants.VALID_CONDITIONS:
raise exc("Invalid property '{0}' is not in {1}".format(condition, constants.VALID_CONDITIONS))
for prop, value in conditions.items():
# The keys of the dictionary must be valid properties that can be checked
if prop not in constants.VALID_PROPERTIES:
raise exc("Invalid property '{0}' is not in {1}".format(prop, constants.VALID_PROPERTIES))

# The value for the `audio_state` property must be a string
if condition == "audio_state" and not isinstance(value, str):
if prop == "audio_state" and not isinstance(value, str):
raise exc("Conditional value for property 'audio_state' must be a string, not {}".format(type(value).__name__))

# The value for the `media_session_state` and `wake_lock_size` properties must be an int
if condition != "audio_state" and not isinstance(value, int):
raise exc("Conditional value for property '{0}' must be an int, not {1}".format(condition, type(value).__name__))
if prop != "audio_state" and not isinstance(value, int):
raise exc("Conditional value for property '{0}' must be an int, not {1}".format(prop, type(value).__name__))

return rules
18 changes: 9 additions & 9 deletions androidtv/constants.py
Expand Up @@ -44,22 +44,22 @@
#: 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``)
#: Get the properties for an :py:class:`~androidtv.androidtv.AndroidTV` device (``lazy=True``); see :py:meth:`androidtv.androidtv.AndroidTV.get_properties`
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``)
#: Get the properties for an :py:class:`~androidtv.androidtv.AndroidTV` device (``lazy=False``); see :py:meth:`androidtv.androidtv.AndroidTV.get_properties`
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``)
#: Get the properties for a :py:class:`~androidtv.firetv.FireTV` device (``lazy=True, get_running_apps=True``); see :py:meth:`androidtv.firetv.FireTV.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_RUNNING_APPS

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

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

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

# `getprop` commands
Expand Down Expand Up @@ -228,10 +228,10 @@
VALID_STATES = (STATE_IDLE, STATE_OFF, STATE_PLAYING, STATE_PAUSED, STATE_STANDBY)

#: Properties that can be used to determine the current state
VALID_PROPERTIES = ("audio_state", "media_session_state")
VALID_STATE_PROPERTIES = ("audio_state", "media_session_state")

#: Conditions that can be checked by the :meth:`~androidtv.basetv.BaseTV._conditions_are_true` method
VALID_CONDITIONS = VALID_PROPERTIES + ("wake_lock_size",)
#: Properties that can be checked by the :meth:`~androidtv.basetv.BaseTV._conditions_are_true` method
VALID_PROPERTIES = VALID_STATE_PROPERTIES + ("wake_lock_size",)

# https://developer.android.com/reference/android/media/session/PlaybackState.html
#: States for the :attr:`~androidtv.basetv.BaseTV.media_session_state` property
Expand Down
7 changes: 7 additions & 0 deletions androidtv/firetv.py
Expand Up @@ -280,6 +280,13 @@ def stop_app(self, app):
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 Down

0 comments on commit 56ad260

Please sign in to comment.