Skip to content

Commit

Permalink
Enable custom Audio State command (#306)
Browse files Browse the repository at this point in the history
* Add Option for Custom Audio State command

* Add Option for Custom Audio State command

* Add Option for Custom Audio State command

* Add Option for Custom Audio State command

* Add Option for Custom Audio State command

Correction to default return value

* Formatting

* Formatting

* Formatting Fix

* Add test for CUSTOM_AUDIO_STATE

* Test correction

* PR Suggestions (#1)

* Reorder _cmd function

* Code suggestion change

* Code suggestion change

* Re-Ordering

* Update docs to refer to function

* Update basetv.py

Co-authored-by: Jeff Irion <JeffLIrion@users.noreply.github.com>
  • Loading branch information
StephenGoodall and JeffLIrion committed Feb 18, 2022
1 parent 813cfbf commit 4562db2
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
18 changes: 16 additions & 2 deletions androidtv/basetv/basetv.py
Expand Up @@ -116,6 +116,20 @@ def customize_command(self, custom_command, value):
elif custom_command in self._custom_commands:
del self._custom_commands[custom_command]

def _cmd_audio_state(self):
"""Get the command used to retrieve the current audio state for this device.
Returns
-------
str
The device-specific ADB shell command used to determine the current audio state
"""
if constants.CUSTOM_AUDIO_STATE in self._custom_commands:
return self._custom_commands[constants.CUSTOM_AUDIO_STATE]

return constants.CMD_AUDIO_STATE

def _cmd_current_app(self):
"""Get the command used to retrieve the current app for this device.
Expand Down Expand Up @@ -468,12 +482,12 @@ def _audio_output_device(stream_music):

@staticmethod
def _audio_state(audio_state_response):
"""Parse the :meth:`audio_state` property from the output of the command :py:const:`androidtv.constants.CMD_AUDIO_STATE`.
"""Parse the :meth:`audio_state` property from the ADB shell output.
Parameters
----------
audio_state_response : str, None
The output of the command :py:const:`androidtv.constants.CMD_AUDIO_STATE`
The output from the ADB command `androidtv.basetv.basetv.BaseTV._cmd_audio_state``
Returns
-------
Expand Down
4 changes: 2 additions & 2 deletions androidtv/basetv/basetv_async.py
Expand Up @@ -242,10 +242,10 @@ async def audio_state(self):
Returns
-------
str, None
The audio state, as determined from the ADB shell command :py:const:`androidtv.constants.CMD_AUDIO_STATE`, or ``None`` if it could not be determined
The audio state, or ``None`` if it could not be determined
"""
audio_state_response = await self._adb.shell(constants.CMD_AUDIO_STATE)
audio_state_response = await self._adb.shell(self._cmd_audio_state())
return self._audio_state(audio_state_response)

async def awake(self):
Expand Down
4 changes: 2 additions & 2 deletions androidtv/basetv/basetv_sync.py
Expand Up @@ -242,10 +242,10 @@ def audio_state(self):
Returns
-------
str, None
The audio state, as determined from the ADB shell command :py:const:`androidtv.constants.CMD_AUDIO_STATE`, or ``None`` if it could not be determined
The audio state, or ``None`` if it could not be determined
"""
audio_state_response = self._adb.shell(constants.CMD_AUDIO_STATE)
audio_state_response = self._adb.shell(self._cmd_audio_state())
return self._audio_state(audio_state_response)

def awake(self):
Expand Down
3 changes: 3 additions & 0 deletions androidtv/constants.py
Expand Up @@ -35,13 +35,15 @@ class DeviceEnum(IntEnum):
INTENT_HOME = "android.intent.category.HOME"

# Customizable commands
CUSTOM_AUDIO_STATE = "audio_state"
CUSTOM_CURRENT_APP = "current_app"
CUSTOM_CURRENT_APP_MEDIA_SESSION_STATE = "current_app_media_session_state"
CUSTOM_LAUNCH_APP = "launch_app"
CUSTOM_RUNNING_APPS = "running_apps"
CUSTOM_TURN_OFF = "turn_off"
CUSTOM_TURN_ON = "turn_on"
CUSTOMIZABLE_COMMANDS = {
CUSTOM_AUDIO_STATE,
CUSTOM_CURRENT_APP,
CUSTOM_CURRENT_APP_MEDIA_SESSION_STATE,
CUSTOM_LAUNCH_APP,
Expand All @@ -52,6 +54,7 @@ class DeviceEnum(IntEnum):

#: The subset of `CUSTOMIZABLE_COMMANDS` that is potentially used in the ``update()`` method
HA_CUSTOMIZABLE_COMMANDS = (
CUSTOM_AUDIO_STATE,
CUSTOM_CURRENT_APP_MEDIA_SESSION_STATE,
CUSTOM_LAUNCH_APP,
CUSTOM_RUNNING_APPS,
Expand Down
5 changes: 5 additions & 0 deletions tests/test_androidtv_sync.py
Expand Up @@ -622,6 +622,11 @@ def test_customize_command(self):
self.atv.turn_on()
patched.assert_called_with("5")

self.atv.customize_command(constants.CUSTOM_AUDIO_STATE, "6")
with patch.object(self.atv._adb, "shell") as patched:
self.atv.audio_state()
patched.assert_called_with("6")

# Delete a custom command
self.atv.customize_command(constants.CUSTOM_TURN_ON, None)
with patch.object(self.atv._adb, "shell") as patched:
Expand Down

0 comments on commit 4562db2

Please sign in to comment.