├── README.md └── android-tv-remote.py /README.md: -------------------------------------------------------------------------------- 1 | # Android TV remote 2 | A simple wrapper around ADB to send commands to Android TVs. 3 | 4 | Currently it only allows switching input sources (since this functionality is not exposed in the [Philips TV API](https://github.com/eslavnov/pylips/wiki)). 5 | 6 | ## Prerequisites 7 | 8 | You need Android Debug Bridge (standard Android debugging app) installed for this tool to work, since it's just a wrapper around `adb` to send specific key inputs to the TV. Just google `install adb %your_os%` for installation instructions for your OS. 9 | 10 | **Make sure `adb` is added to PATH!** 11 | 12 | Install on Ubuntu: 13 | ``` 14 | sudo apt install adb 15 | ``` 16 | 17 | ## Enabling remote debugging on your TV 18 | This has to be enabled to allow `adb` to talk to the TV: 19 | 1. On your TV find Android Settings > Device > About > Build and press it 7 times until you see a notification. 20 | 2. Go back to Android Settings > Preferences > Developer options 21 | 3. In Developer options enable 'USB debugging'. If you have a separate option for LAN/WI-FI debugging, enable it instead. 22 | 23 | ## Using the tool 24 | 25 | Set the TV's ip address with a `--host` parameter and a desired input source with an `--input` parameter (number from 1 to 6). 26 | The first time you connect to your TV it will display a promt. Answer 'yes' so it does not show up at every request. 27 | 28 | **Examples:** 29 | 30 | Select HDMI1: 31 | 32 | ```python android-tv-remote.py --host %tv_ip_address% --input 1``` 33 | 34 | Select SCART: 35 | 36 | ```python android-tv-remote.py --host %tv_ip_address% --input 6``` 37 | -------------------------------------------------------------------------------- /android-tv-remote.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import subprocess 3 | 4 | def handle_connection(ip): 5 | if check_connection(ip) is False: 6 | if connect(ip) is False: 7 | return 8 | 9 | def check_connection(ip): 10 | if ip in str(subprocess.check_output(['adb', 'devices'])): 11 | return True 12 | return False 13 | 14 | def connect(ip, err_count=0): 15 | if "unable" in str(subprocess.check_output(['adb', 'connect', ip])): 16 | err_count += 1 17 | if err_count >= 3: 18 | print("Unable to connect to", ip, "after", err_count, "retries") 19 | return False 20 | else: 21 | return connect(ip, err_count) 22 | print("Connected to", ip) 23 | return True 24 | 25 | def main(): 26 | parser = argparse.ArgumentParser(description='Control Philips Android TVs') 27 | parser.add_argument("--host", dest='host', help="TV's ip address") 28 | parser.add_argument("--input", dest='input', help="TV's input source (1-6)") 29 | args = parser.parse_args() 30 | 31 | if args is None: 32 | return print("Please set your TV's IP-address with a --host parameter and an input mode (1-6) with an --input parameter") 33 | 34 | if args.host is None: 35 | return print("Please set your TV's IP-address with a --host parameter") 36 | 37 | if args.input is None: 38 | return print("Please set an input mode (1-6) with an --input parameter") 39 | 40 | sources={ 41 | 1: "KEYCODE_F1", 42 | 2: "KEYCODE_F2", 43 | 3: "KEYCODE_F3", 44 | 4: "KEYCODE_F4", 45 | 5: "KEYCODE_F5", 46 | 6: "KEYCODE_F6", 47 | } 48 | 49 | handle_connection(args.host) 50 | 51 | if int(args.input) in sources: 52 | try: 53 | subprocess.check_output(['adb', 'shell', 'input', 'keyevent', sources[int(args.input)]]) 54 | except: 55 | print("Error connecting to TV") 56 | else: 57 | print("Unknown input (should be a value between 1-6):", args.input) 58 | 59 | if __name__ == '__main__': 60 | main() --------------------------------------------------------------------------------