diff --git a/androidtv/basetv.py b/androidtv/basetv.py index 333b29b1..48375e9a 100644 --- a/androidtv/basetv.py +++ b/androidtv/basetv.py @@ -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) + # ======================================================================= # # # diff --git a/tests/test_basetv.py b/tests/test_basetv.py index fd382e2b..ce1aaf4e 100644 --- a/tests/test_basetv.py +++ b/tests/test_basetv.py @@ -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):