Skip to content

Commit

Permalink
Merge 3ed2b06 into 1acd752
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffLIrion committed May 15, 2020
2 parents 1acd752 + 3ed2b06 commit 41f7d9e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
47 changes: 47 additions & 0 deletions androidtv/basetv.py
Expand Up @@ -1172,6 +1172,53 @@ def volume_down(self, current_volume_level=None):
# return the new volume level
return max(current_volume - 1, 0.) / self.max_volume

# ======================================================================= #
# #
# Miscellaneous methods #
# #
# ======================================================================= #
def get_sendevent(self, timeout_s=8):
"""Capture an event (e.g., a button press) via ``getevent`` and convert it into ``sendevent`` commands.
For more info, see:
* http://ktnr74.blogspot.com/2013/06/emulating-touchscreen-interaction-with.html?m=1
* https://qatesttech.wordpress.com/2012/06/21/turning-the-output-from-getevent-into-something-something-that-can-be-used/
Parameters
----------
timeout_s : int
The timeout in seconds to wait for events
Returns
-------
str
The events converted to ``sendevent`` commands
"""
getevent = self.adb_shell("( getevent ) & pid=$!; ( sleep {} && kill -HUP $pid ) 2>/dev/null & watcher=$!; if wait $pid 2>/dev/null; then echo 'your command finished'; kill -HUP -P $watcher; wait $watcher; else echo 'your command was interrupted'; fi".format(timeout_s))

return " && ".join([self._parse_getevent_line(line) for line in getevent.splitlines() if line.startswith("/") and ":" in line])

@staticmethod
def _parse_getevent_line(line):
"""Parse a line of the output received in ``get_sendevent``.
Parameters
----------
line : str
A line of output from ``get_sendevent``
Returns
-------
str
The properly formatted ``sendevent`` command
"""
device_name, event_info = line.split(":", 1)
integers = [int(x, 16) for x in event_info.strip().split()[:3]]
return "sendevent {} {} {} {}".format(device_name, *integers)


# ======================================================================= #
# #
Expand Down
7 changes: 7 additions & 0 deletions tests/test_basetv.py
Expand Up @@ -447,6 +447,13 @@ def test_wake_lock_size(self):
with patchers.patch_shell('INVALID')[self.PATCH_KEY]:
self.assertIsNone(self.btv.wake_lock_size())

def test_get_sendevent(self):
"""Check that the ``get_sendevent`` method works correctly.
"""
with patchers.patch_shell("add device 1: /dev/input/event4\r\n name: \"Amazon Fire TV Remote\"\r\nadd device 2: /dev/input/event3\r\n name: \"kcmouse\"\r\ncould not get driver version for /dev/input/mouse0, Not a typewriter\r\nadd device 3: /dev/input/event2\r\n name: \"amazon_touch\"\r\nadd device 4: /dev/input/event1\r\n name: \"hdmipower\"\r\nadd device 5: /dev/input/event0\r\n name: \"mtk-kpd\"\r\ncould not get driver version for /dev/input/mice, Not a typewriter\r\n/dev/input/event4: 0004 0004 00070051\r\n/dev/input/event4: 0001 006c 00000001\r\n/dev/input/event4: 0000 0000 00000000\r\n/dev/input/event4: 0004 0004 00070051\r\n/dev/input/event4: 0001 006c 00000000\r\n/dev/input/event4: 0000 0000 00000000\r\nyour command was interrupted")[self.PATCH_KEY]:
self.assertEqual(self.btv.get_sendevent(), "sendevent /dev/input/event4 4 4 458833 && sendevent /dev/input/event4 1 108 1 && sendevent /dev/input/event4 0 0 0 && sendevent /dev/input/event4 4 4 458833 && sendevent /dev/input/event4 1 108 0 && sendevent /dev/input/event4 0 0 0")


class TestHAStateDetectionRulesValidator(unittest.TestCase):
def test_ha_state_detection_rules_validator(self):
Expand Down

0 comments on commit 41f7d9e

Please sign in to comment.