from evdev import InputDevice, list_devices, ecodes from urllib.request import Request, urlopen from urllib.error import HTTPError, URLError from dotenv import load_dotenv import os import base64 SCANCODES = {0x11A: "up", 0x148: "down", 0x147: "left", 0x107: "right", 0x106: "ok", 0x141: "play/pause", 0x11c: "power", 0x103: "vol+", 0x142: "vol-", 0x101: "app", 0x154: "1", 0x110: "stop", 0x14b: "skip", 0x155: "epg", 0x119: "web", 0x15f: "setting", 0x15a: "dtv"} device = None last = 0x00 load_dotenv("/home/arslaan/remote-automation/.env") HOMELAB_USERNAME = os.environ["HOMELAB_USERNAME"] HOMELAB_PASSWORD = os.environ["HOMELAB_PASSWORD"] def homelab_request(request: str): url = f"http://192.168.1.96/automate/{request}" credentials = f"{HOMELAB_USERNAME}:{HOMELAB_PASSWORD}" encoded_credentials = base64.b64encode( credentials.encode() ).decode() req = Request( url, headers={ "Authorization": f"Basic {encoded_credentials}", }, ) try: with urlopen(req, timeout=5) as resp: print(f"homelab request {request} response {resp.status}") print(resp.read().decode()) except HTTPError as e: print(f"homelab request {request} response {e.code}") print(e.read().decode()) except URLError as e: print(f"homelab request failed: {e.reason}") def do_action(action: str): if action == "app": homelab_request("hack_this_club") # /j (test endpoint) elif action == "play/pause": homelab_request("music") elif action == "1": homelab_request("print_queue") elif action == "stop": homelab_request("music/stop") elif action == "skip": homelab_request("music/next") elif action == "epg": homelab_request("music/fav") elif action == "vol+": homelab_request("volup") elif action == "vol-": homelab_request("voldown") elif action == "web": homelab_request("zola") elif action == "setting": homelab_request("cgit") elif action == "dtv": homelab_request("agentic") for path in list_devices(): candidate = InputDevice(path) if candidate.name == "meson-ir": device = candidate break if device is None: raise RuntimeError("meson-ir input device not found") print(f"Listening on {device.path}: {device.name}") try: for event in device.read_loop(): if event.type == ecodes.EV_MSC and event.code == ecodes.MSC_SCAN: if (event.value != last): print(f"scancode {event.value:#x}") last = event.value action = SCANCODES.get(event.value) if action: print(f"got action {action}") do_action(action) else: print(f"repeat scancode {event.value:#x}") last = 0x00 except KeyboardInterrupt: print("\nExiting...") finally: device.close()