Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class AndroidTV:
- """ Represents an Android TV device. """
- def __init__(self, host, adbkey=None):
- """ Initialize AndroidTV object.
- :param host: Host in format <address>:port.
- :param adbkey: The path to the "adbkey" file
- """
- self.host = host
- self.adbkey = adbkey
- self._adb = None
- self.state = STATE_UNKNOWN
- self.muted = False
- self.device = 'hdmi'
- self.volume = 0.
- self.app_id = None
- self.package_launcher = None
- self.package_settings = 'android.settings.SETTINGS'
- self._adb_error = False
- self._adb_lock = False
- self._exceptions = (TypeError, ValueError, AttributeError,
- InvalidCommandError, InvalidResponseError,
- InvalidChecksumError, BrokenPipeError)
- async def connect(self):
- """ Connect to an Android TV device.
- Will attempt to establish ADB connection to the given host.
- Failure sets state to UNKNOWN and disables sending actions.
- """
- if self.adbkey:
- self._adb = AsyncADBConnection(self.host, self.adbkey)
- else:
- self._adb = AsyncADBConnection(self.host)
- """
- try:
- if self.adbkey:
- signer = Signer(self.adbkey)
- # Connect to the device
- self._adb = adb_commands.AdbCommands().ConnectDevice(serial=self.host, rsa_keys=[signer])
- else:
- self._adb = adb_commands.AdbCommands().ConnectDevice(serial=self.host)
- if not self.package_launcher:
- await self.home()
- logging.info("Getting Launcher App Package")
- self.package_launcher = await self.current_app
- except socket_error as serr:
- logging.warning("Couldn't connect to host: %s, error: %s", self.host, serr.strerror)
- """
- async def update(self):
- """ Update the device status. """
- # Check if device is disconnected.
- if not self._adb:
- self.state = STATE_UNKNOWN
- self.app_id = None
- # Check if device is off.
- elif not await self._screen_on:
- self.state = STATE_OFF
- self.app_id = None
- else:
- self.app_id = await self.current_app
- if await self._wake_lock:
- self.state = STATE_PLAYING
- elif self.app_id not in (self.package_launcher, self.package_settings):
- # Check if state was playing on last update
- if self.state == STATE_PLAYING:
- self.state = STATE_PAUSED
- elif self.state != STATE_PAUSED:
- self.state = STATE_IDLE
- else:
- # We're on either the launcher or in settings
- self.state = STATE_ON
- # Get information from the audio status.
- audio_output = await self._dump('audio')
- stream_block = re.findall(BLOCK_REGEX, audio_output,
- re.DOTALL | re.MULTILINE)[0]
- self.muted = re.findall(MUTED_REGEX, stream_block,
- re.DOTALL | re.MULTILINE)[0] == 'true'
- # ======================================================================= #
- # #
- # properties #
- # #
- # ======================================================================= #
- @property
- async def current_app(self):
- filtered_dump = await self._dump("window windows", "mCurrentFocus")
- current_focus = filtered_dump.replace("\r", "")
- matches = WINDOW_REGEX.search(current_focus)
- if matches:
- (pkg, activity) = matches.group('package', 'activity')
- return pkg
- else:
- logging.warning("Couldn't get current app, reply was %s", current_focus)
- return None
- @property
- async def _screen_on(self):
- """ Check if the screen is on. """
- return await self._dump_has('power', 'Display Power', 'state=ON')
- @property
- async def _awake(self):
- """ Check if the device is awake (screen saver is not running). """
- return await self._dump_has('power', 'mWakefulness', 'Awake')
- @property
- async def _wake_lock(self):
- """ Check for wake locks (device is playing). """
- return not await self._dump_has('power', 'Locks', 'size=0')
- # ======================================================================= #
- # #
- # ADB methods #
- # #
- # ======================================================================= #
- @adb_wrapper
- async def _input(self, cmd):
- """ Send input to the device.
- :param cmd: Input command.
- """
- if not self._adb:
- return
- self._adb.Shell('input {0}'.format(cmd))
- @adb_wrapper
- async def _dump(self, service, grep=None):
- """ Perform a service dump.
- :param service: Service to dump.
- :param grep: Grep for this string.
- :returns: Dump, optionally grepped.
- """
- if not self._adb:
- return
- if grep:
- return self._adb.Shell('dumpsys {0} | grep "{1}"'.format(service, grep))
- return self._adb.Shell('dumpsys {0}'.format(service))
- async def _dump_has(self, service, grep, search):
- """ Check if a dump has particular content.
- :param service: Service to dump.
- :param grep: Grep for this string.
- :param search: Check for this substring.
- :returns: Found or not.
- """
- dump_result = await self._dump(service, grep=grep)
- return dump_result.strip().find(search) > -1
- # ======================================================================= #
- # #
- # turn on/off methods #
- # #
- # ======================================================================= #
- async def turn_on(self):
- """ Send power action if device is off. """
- if self._adb and not self._screen_on:
- await self._power()
- async def turn_off(self):
- """ Send power action if device is not off. """
- if self._adb and self._screen_on:
- await self._power()
- # ======================================================================= #
- # #
- # "key" methods: basic commands #
- # #
- # ======================================================================= #
- @adb_wrapper
- async def _key(self, key):
- """ Send a key event to device.
- :param key: Key constant.
- """
- await self._input('keyevent {0}'.format(key))
- async def _power(self):
- """ Send power action. """
- await self._key(POWER)
- async def home(self):
- """ Send home action. """
- await self._key(HOME)
- async def up(self):
- """ Send up action. """
- await self._key(UP)
- async def down(self):
- """ Send down action. """
- await self._key(DOWN)
- async def left(self):
- """ Send left action. """
- await self._key(LEFT)
- async def right(self):
- """ Send right action. """
- await self._key(RIGHT)
- async def enter(self):
- """ Send enter action. """
- await self._key(ENTER)
- async def back(self):
- """ Send back action. """
- await self._key(BACK)
- async def menu(self):
- """ Send menu action. """
- await self._key(MENU)
- async def volume_up(self):
- """ Send volume up action. """
- await self._key(VOLUME_UP)
- async def volume_down(self):
- """ Send volume down action. """
- await self._key(VOLUME_DOWN)
- async def mute_volume(self):
- """ Mute the volume. """
- await self._key(MUTE)
- # ======================================================================= #
- # #
- # "key" methods: media commands #
- # #
- # ======================================================================= #
- async def media_play_pause(self):
- """ Send media play/pause action. """
- await self._key(PLAY_PAUSE)
- async def media_play(self):
- """ Send media play action. """
- await self._key(PLAY)
- async def media_pause(self):
- """ Send media pause action. """
- await self._key(PAUSE)
- async def media_stop(self):
- """ Send media stop action. """
- await self._key(STOP)
- async def media_next(self):
- """ Send media next action (results in fast-forward). """
- await self._key(NEXT)
- async def media_previous(self):
- """ Send media previous action (results in rewind). """
- await self._key(PREVIOUS)
- # ======================================================================= #
- # #
- # "key" methods: key commands #
- # #
- # ======================================================================= #
- async def space(self):
- """ Send space keypress. """
- await self._key(SPACE)
- async def key_0(self):
- """ Send 0 keypress. """
- await self._key(KEY_0)
- async def key_1(self):
- """ Send 1 keypress. """
- await self._key(KEY_1)
- async def key_2(self):
- """ Send 2 keypress. """
- await self._key(KEY_2)
- async def key_3(self):
- """ Send 3 keypress. """
- await self._key(KEY_3)
- async def key_4(self):
- """ Send 4 keypress. """
- await self._key(KEY_4)
- async def key_5(self):
- """ Send 5 keypress. """
- await self._key(KEY_5)
- async def key_6(self):
- """ Send 6 keypress. """
- await self._key(KEY_6)
- async def key_7(self):
- """ Send 7 keypress. """
- await self._key(KEY_7)
- async def key_8(self):
- """ Send 8 keypress. """
- await self._key(KEY_8)
- async def key_9(self):
- """ Send 9 keypress. """
- await self._key(KEY_9)
- async def key_a(self):
- """ Send a keypress. """
- await self._key(KEY_A)
- async def key_b(self):
- """ Send b keypress. """
- await self._key(KEY_B)
- async def key_c(self):
- """ Send c keypress. """
- await self._key(KEY_C)
- async def key_d(self):
- """ Send d keypress. """
- await self._key(KEY_D)
- async def key_e(self):
- """ Send e keypress. """
- await self._key(KEY_E)
- async def key_f(self):
- """ Send f keypress. """
- await self._key(KEY_F)
- async def key_g(self):
- """ Send g keypress. """
- await self._key(KEY_G)
- async def key_h(self):
- """ Send h keypress. """
- await self._key(KEY_H)
- async def key_i(self):
- """ Send i keypress. """
- await self._key(KEY_I)
- async def key_j(self):
- """ Send j keypress. """
- await self._key(KEY_J)
- async def key_k(self):
- """ Send k keypress. """
- await self._key(KEY_K)
- async def key_l(self):
- """ Send l keypress. """
- await self._key(KEY_L)
- async def key_m(self):
- """ Send m keypress. """
- await self._key(KEY_M)
- async def key_n(self):
- """ Send n keypress. """
- await self._key(KEY_N)
- async def key_o(self):
- """ Send o keypress. """
- await self._key(KEY_O)
- async def key_p(self):
- """ Send p keypress. """
- await self._key(KEY_P)
- async def key_q(self):
- """ Send q keypress. """
- await self._key(KEY_Q)
- async def key_r(self):
- """ Send r keypress. """
- await self._key(KEY_R)
- async def key_s(self):
- """ Send s keypress. """
- await self._key(KEY_S)
- async def key_t(self):
- """ Send t keypress. """
- await self._key(KEY_T)
- async def key_u(self):
- """ Send u keypress. """
- await self._key(KEY_U)
- async def key_v(self):
- """ Send v keypress. """
- await self._key(KEY_V)
- async def key_w(self):
- """ Send w keypress. """
- await self._key(KEY_W)
- async def key_x(self):
- """ Send x keypress. """
- await self._key(KEY_X)
- async def key_y(self):
- """ Send y keypress. """
- await self._key(KEY_Y)
- async def key_z(self):
- """ Send z keypress. """
- await self._key(KEY_Z)
Advertisement
Add Comment
Please, Sign In to add comment