androidtv.basetv.basetv_sync 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_sync.BaseTVSync(host, port=5555, adbkey='', adb_server_ip='', adb_server_port=5037, state_detection_rules=None, signer=None)[source]
Bases:
BaseTV
Base class for representing an Android TV / Fire TV device.
The
state_detection_rules
parameter is of the format:state_detection_rules = {'com.amazon.tv.launcher': ['idle'], 'com.netflix.ninja': ['media_session_state'], 'com.ellation.vrv': ['audio_state'], 'com.hulu.plus': [{'playing': {'wake_lock_size' : 4}}, {'paused': {'wake_lock_size': 2}}], 'com.plexapp.android': [{'paused': {'media_session_state': 3, 'wake_lock_size': 1}}, {'playing': {'media_session_state': 3}}, 'idle']}
The keys are app IDs, and the values are lists of rules that are evaluated in order.
VALID_STATES = ('idle', 'off', 'playing', 'paused', 'standby')
Valid rules:
'idle'
,'playing'
,'paused'
,'standby'
, or'off'
= always report the specified state when this app is open'media_session_state'
= try to use themedia_session_state()
property to determine the state'audio_state'
= try to use theaudio_state()
property to determine the state{'<VALID_STATE>': {'<PROPERTY1>': VALUE1, '<PROPERTY2>': VALUE2, ...}}
= check if each of the properties is equal to the specified value, and if so return the stateThe valid properties are
'media_session_state'
,'audio_state'
, and'wake_lock_size'
- Parameters:
host (str) – The address of the device; may be an IP address or a host name
port (int) – The device port to which we are connecting (default is 5555)
adbkey (str) – The path to the
adbkey
file for ADB authenticationadb_server_ip (str) – The IP address of the ADB server
adb_server_port (int) – The port for the ADB server
state_detection_rules (dict, None) – A dictionary of rules for determining the state (see above)
signer (PythonRSASigner, None) – The signer for the ADB keys, as loaded by
androidtv.adb_manager.adb_manager_sync.ADBPythonSync.load_adbkey()
- _get_stream_music(stream_music_raw=None)[source]
Get the
STREAM_MUSIC
block from the output of the commandandroidtv.constants.CMD_STREAM_MUSIC
.- Parameters:
stream_music_raw (str, None) – The output of the command
androidtv.constants.CMD_STREAM_MUSIC
- Returns:
The
STREAM_MUSIC
block from the output ofandroidtv.constants.CMD_STREAM_MUSIC
, orNone
if it could not be determined- Return type:
str, None
- _send_intent(pkg, intent, count=1)[source]
Send an intent to the device.
- Parameters:
pkg (str) – The command that will be sent is
monkey -p <pkg> -c <intent> <count>; echo $?
intent (str) – The command that will be sent is
monkey -p <pkg> -c <intent> <count>; echo $?
count (int, str) – The command that will be sent is
monkey -p <pkg> -c <intent> <count>; echo $?
- Returns:
A dictionary with keys
'output'
and'retcode'
, if they could be determined; otherwise, an empty dictionary- Return type:
dict
- adb_close()[source]
Close the ADB connection.
This only works for the Python ADB implementation (see
androidtv.adb_manager.adb_manager_sync.ADBPythonSync.close()
). For the ADB server approach, this doesn’t do anything (seeandroidtv.adb_manager.adb_manager_sync.ADBServerSync.close()
).
- adb_connect(log_errors=True, auth_timeout_s=10.0, transport_timeout_s=1.0)[source]
Connect to an Android TV / Fire TV device.
- Parameters:
log_errors (bool) – Whether errors should be logged
auth_timeout_s (float) – Authentication timeout (in seconds)
transport_timeout_s (float) – Transport timeout (in seconds)
- Returns:
Whether or not the connection was successfully established and the device is available
- Return type:
bool
- adb_pull(local_path, device_path)[source]
Pull a file from the device.
This calls
androidtv.adb_manager.adb_manager_sync.ADBPythonSync.pull()
orandroidtv.adb_manager.adb_manager_sync.ADBServerSync.pull()
, depending on whether the Python ADB implementation or an ADB server is used for communicating with the device.- Parameters:
local_path (str) – The path where the file will be saved
device_path (str) – The file on the device that will be pulled
- adb_push(local_path, device_path)[source]
Push a file to the device.
This calls
androidtv.adb_manager.adb_manager_sync.ADBPythonSync.push()
orandroidtv.adb_manager.adb_manager_sync.ADBServerSync.push()
, depending on whether the Python ADB implementation or an ADB server is used for communicating with the device.- Parameters:
local_path (str) – The file that will be pushed to the device
device_path (str) – The path where the file will be saved on the device
- adb_screencap()[source]
Take a screencap.
This calls
androidtv.adb_manager.adb_manager_sync.ADBPythonSync.screencap()
orandroidtv.adb_manager.adb_manager_sync.ADBServerSync.screencap()
, depending on whether the Python ADB implementation or an ADB server is used for communicating with the device.- Returns:
The screencap as a binary .png image
- Return type:
bytes
- adb_shell(cmd)[source]
Send an ADB command.
This calls
androidtv.adb_manager.adb_manager_sync.ADBPythonSync.shell()
orandroidtv.adb_manager.adb_manager_sync.ADBServerSync.shell()
, depending on whether the Python ADB implementation or an ADB server is used for communicating with the device.- Parameters:
cmd (str) – The ADB command to be sent
- Returns:
The response from the device, if there is a response
- Return type:
str, None
- audio_output_device()[source]
Get the current audio playback device.
- Returns:
The current audio playback device, or
None
if it could not be determined- Return type:
str, None
- audio_state()[source]
Check if audio is playing, paused, or idle.
- Returns:
The audio state, or
None
if it could not be determined- Return type:
str, None
- awake()[source]
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
- current_app()[source]
Return the current app.
- Returns:
The ID of the current app, or
None
if it could not be determined- Return type:
str, None
- current_app_media_session_state()[source]
Get the current app and the state from the output of
dumpsys media_session
.- Returns:
str, None – The current app, or
None
if it could not be determinedint, None – The state from the output of the ADB shell command
dumpsys media_session
, orNone
if it could not be determined
- 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
- get_hdmi_input()[source]
Get the HDMI input from the output of
androidtv.constants.CMD_HDMI_INPUT
.- Returns:
The HDMI input, or
None
if it could not be determined- Return type:
str, None
- get_installed_apps()[source]
Return a list of installed applications.
- Returns:
A list of the installed apps, or
None
if it could not be determined- Return type:
list, None
- is_volume_muted()[source]
Whether or not the volume is muted.
- Returns:
Whether or not the volume is muted, or
None
if it could not be determined- Return type:
bool, None
- launch_app(app)[source]
Launch an app.
- Parameters:
app (str) – The ID of the app that will be launched
- learn_sendevent(timeout_s=8)[source]
Capture an event (e.g., a button press) via
getevent
and convert it intosendevent
commands.For more info, see:
- Parameters:
timeout_s (int) – The timeout in seconds to wait for events
- Returns:
The events converted to
sendevent
commands- Return type:
str
- media_session_state()[source]
Get the state from the output of
dumpsys media_session
.- Returns:
The state from the output of the ADB shell command
dumpsys media_session
, orNone
if it could not be determined- Return type:
int, None
Send menu action.
- running_apps()[source]
Return a list of running user applications.
- Returns:
A list of the running apps
- Return type:
list
- screen_on()[source]
Check if the screen is on.
- Returns:
Whether or not the device is on
- Return type:
bool
- screen_on_awake_wake_lock_size()[source]
Check if the screen is on and the device is awake, and get the wake lock size.
- Returns:
bool – Whether or not the device is on
bool – Whether or not the device is awake (screensaver is not running)
int, None – The size of the current wake lock, or
None
if it could not be determined
- set_volume_level(volume_level)[source]
Set the volume to the desired level.
- Parameters:
volume_level (float) – The new volume level (between 0 and 1)
- Returns:
The new volume level (between 0 and 1), or
None
ifself.max_volume
could not be determined- Return type:
float, None
- start_intent(uri)[source]
Start an intent on the device.
- Parameters:
uri (str) – The intent that will be sent is
am start -a android.intent.action.VIEW -d <uri>
- stop_app(app)[source]
Stop an app.
- Parameters:
app (str) – The ID of the app that will be stopped
- Returns:
The output of the
am force-stop
ADB shell command, orNone
if the device is unavailable- Return type:
str, None
- stream_music_properties()[source]
Get various properties from the “STREAM_MUSIC” block from
dumpsys audio
..- Returns:
audio_output_device (str, None) – The current audio playback device, or
None
if it could not be determinedis_volume_muted (bool, None) – Whether or not the volume is muted, or
None
if it could not be determinedvolume (int, None) – The absolute volume level, or
None
if it could not be determinedvolume_level (float, None) – The volume level (between 0 and 1), or
None
if it could not be determined
- volume()[source]
Get the absolute volume level.
- Returns:
The absolute volume level, or
None
if it could not be determined- Return 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
None
ifself.max_volume
could not be determined- Return type:
float, None
- volume_level()[source]
Get the relative volume level.
- Returns:
The volume level (between 0 and 1), or
None
if it could not be determined- Return 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
None
ifself.max_volume
could not be determined- Return type:
float, None