androidtv.basetv module¶
Communicate with an Android TV or Amazon Fire TV device via ADB over a network.
ADB Debugging must be enabled.
-
class
androidtv.basetv.BaseTV(host, adbkey='', adb_server_ip='', adb_server_port=5037)[source]¶ Bases:
objectBase class for representing an Android TV / Fire TV device.
-
_adb_shell_pure_python_adb(cmd)[source]¶ Send an ADB command using an ADB server.
Parameters: cmd (str) – The ADB command to be sent Returns: The response from the device, if there is a response Return type: str, None
-
_adb_shell_python_adb(cmd)[source]¶ Send an ADB command using the Python ADB implementation.
Parameters: cmd (str) – The ADB command to be sent Returns: The response from the device, if there is a response Return type: str, None
-
static
_audio_state(dumpsys_audio)[source]¶ Parse the
audio_stateproperty from the output ofadb shell dumpsys audio.Parameters: dumpsys_audio (str, None) – The output of adb shell dumpsys audioReturns: The audio state, or Noneif it could not be determinedReturn type: str, None
-
static
_current_app(current_window)[source]¶ Return the current app from the output of
adb shell dumpsys window windows | grep mCurrentFocus.Parameters: current_window (str, None) – The output of adb shell dumpsys window windows | grep mCurrentFocusReturns: The ID of the current app, or Noneif it could not be determinedReturn type: str, None
-
static
_device(stream_music)[source]¶ Get the current playback device from the
STREAM_MUSICblock fromadb shell dumpsys audio.Parameters: stream_music (str, None) – The STREAM_MUSICblock fromadb shell dumpsys audioReturns: The current playback device, or Noneif it could not be determinedReturn type: str, None
-
_get_stream_music(dumpsys_audio=None)[source]¶ Get the
STREAM_MUSICblock fromadb shell dumpsys audio.Parameters: dumpsys_audio (str, None) – The output of adb shell dumpsys audioReturns: The STREAM_MUSICblock fromadb shell dumpsys audio, orNoneif it could not be determinedReturn type: str, None
-
static
_is_volume_muted(stream_music)[source]¶ Determine whether or not the volume is muted from the
STREAM_MUSICblock fromadb shell dumpsys audio.Parameters: stream_music (str, None) – The STREAM_MUSICblock fromadb shell dumpsys audioReturns: Whether or not the volume is muted, or Noneif it could not be determinedReturn type: bool, None
-
static
_media_session_state(media_session)[source]¶ Get the state from the output of
adb shell dumpsys media_session | grep -m 1 'state=PlaybackState {'.Parameters: media_session (str, None) – The output of adb shell dumpsys media_session | grep -m 1 'state=PlaybackState {'Returns: The state from the output of the ADB shell command dumpsys media_session, orNoneif it could not be determinedReturn type: int, None
-
static
_running_apps(ps)[source]¶ Get the running apps from the output of
ps | grep u0_a.Parameters: ps (str, None) – The output of adb shell ps | grep u0_aReturns: A list of the running apps, or Noneif it could not be determinedReturn type: list, None
-
_volume(stream_music, device)[source]¶ Get the absolute volume level from the
STREAM_MUSICblock fromadb shell dumpsys audio.Parameters: - stream_music (str, None) – The
STREAM_MUSICblock fromadb shell dumpsys audio - device (str, None) – The current playback device
Returns: The absolute volume level, or
Noneif it could not be determinedReturn type: int, None
- stream_music (str, None) – The
-
_volume_level(volume)[source]¶ Get the relative volume level from the absolute volume level.
Parameters: volume (int, None) – The absolute volume level Returns: The volume level (between 0 and 1), or Noneif it could not be determinedReturn type: float, None
-
static
_wake_lock_size(locks_size)[source]¶ Get the size of the current wake lock from the output of
adb shell dumpsys power | grep Locks | grep 'size='.Parameters: locks_size (str, None) – The output of adb shell dumpsys power | grep Locks | grep 'size='.Returns: The size of the current wake lock, or Noneif it could not be determinedReturn type: int, None
-
audio_state¶ Check if audio is playing, paused, or idle.
Returns: The audio state, as determined from the ADB shell command dumpsys audio, orNoneif it could not be determinedReturn type: str, None
-
available¶ Check whether the ADB connection is intact.
Returns: Whether or not the ADB connection is intact Return type: bool
-
awake¶ Check if the device is awake (screensaver is not running).
Returns: Whether or not the device is awake (screensaver is not running) Return type: bool
-
connect(always_log_errors=True)[source]¶ Connect to an Android TV / Fire TV device.
Parameters: always_log_errors (bool) – If True, errors will always be logged; otherwise, errors will only be logged on the first failed reconnect attempt Returns: Whether or not the connection was successfully established and the device is available Return type: bool
-
current_app¶ Return the current app.
Returns: The ID of the current app, or Noneif it could not be determinedReturn type: str, None
-
device¶ Get the current playback device.
Returns: The current playback device, or Noneif it could not be determinedReturn type: str, None
-
get_device_properties()[source]¶ Return a dictionary of device properties.
Returns: props – A dictionary with keys 'wifimac','ethmac','serialno','manufacturer','model', and'sw_version'Return type: dict
-
is_volume_muted¶ Whether or not the volume is muted.
Returns: Whether or not the volume is muted, or Noneif it could not be determinedReturn type: bool, None
-
media_session_state¶ Get the state from the output of
dumpsys media_session.Returns: The state from the output of the ADB shell command dumpsys media_session, orNoneif it could not be determinedReturn type: int, None
Send menu action.
-
running_apps¶ Return a list of running user applications.
Returns: A list of the running apps Return type: list
-
screen_on¶ Check if the screen is on.
Returns: Whether or not the device is on Return type: bool
-
set_volume_level(volume_level, current_volume_level=None)[source]¶ Set the volume to the desired level.
Note
This method works by sending volume up/down commands with a 1 second pause in between. Without this pause, the device will do a quick power cycle. This is the most robust solution I’ve found so far.
Parameters: - volume_level (float) – The new volume level (between 0 and 1)
- current_volume_level (float, None) – The current volume level (between 0 and 1); if it is not provided, it will be determined
Returns: The new volume level (between 0 and 1), or
Noneifself.max_volumecould not be determinedReturn type: float, None
-
volume¶ Get the absolute volume level.
Returns: The absolute volume level, or Noneif it could not be determinedReturn type: int, None
-
volume_down(current_volume_level=None)[source]¶ Send volume down action.
Parameters: current_volume_level (float, None) – The current volume level (between 0 and 1); if it is not provided, it will be determined Returns: The new volume level (between 0 and 1), or Noneifself.max_volumecould not be determinedReturn type: float, None
-
volume_level¶ Get the relative volume level.
Returns: The volume level (between 0 and 1), or Noneif it could not be determinedReturn type: float, None
-
volume_up(current_volume_level=None)[source]¶ Send volume up action.
Parameters: current_volume_level (float, None) – The current volume level (between 0 and 1); if it is not provided, it will be determined Returns: The new volume level (between 0 and 1), or Noneifself.max_volumecould not be determinedReturn type: float, None
-
wake_lock_size¶ Get the size of the current wake lock.
Returns: The size of the current wake lock, or Noneif it could not be determinedReturn type: int, None
-