|
| 1 | +import argparse |
| 2 | +import asyncio |
| 3 | +import logging |
| 4 | +import sys |
| 5 | +import termios |
| 6 | +import tty |
| 7 | +from contextlib import contextmanager |
| 8 | +from typing import Iterator, Optional |
| 9 | + |
| 10 | +from aiovantage import Vantage |
| 11 | + |
| 12 | + |
| 13 | +# Grab connection info from command line arguments |
| 14 | +parser = argparse.ArgumentParser(description="aiovantage example") |
| 15 | +parser.add_argument("host", help="hostname of Vantage controller") |
| 16 | +parser.add_argument("--username", help="username for Vantage controller") |
| 17 | +parser.add_argument("--password", help="password for Vantage controller") |
| 18 | +parser.add_argument("--debug", help="enable debug logging", action="store_true") |
| 19 | +args = parser.parse_args() |
| 20 | + |
| 21 | + |
| 22 | +def parse_keypress() -> Optional[str]: |
| 23 | + # Rudimentary keypress parser |
| 24 | + |
| 25 | + c = sys.stdin.read(1) |
| 26 | + if c == "\x1b": |
| 27 | + seq = sys.stdin.read(2) |
| 28 | + if seq == "[A": |
| 29 | + return "KEY_UP" |
| 30 | + elif seq == "[B": |
| 31 | + return "KEY_DOWN" |
| 32 | + else: |
| 33 | + return None |
| 34 | + else: |
| 35 | + return c |
| 36 | + |
| 37 | + |
| 38 | +@contextmanager |
| 39 | +def cbreak_mode(fd: int) -> Iterator[None]: |
| 40 | + # Context manager to read terminal input character by character |
| 41 | + |
| 42 | + old_attrs = termios.tcgetattr(fd) |
| 43 | + try: |
| 44 | + tty.setcbreak(fd) |
| 45 | + yield |
| 46 | + finally: |
| 47 | + termios.tcsetattr(fd, termios.TCSADRAIN, old_attrs) |
| 48 | + |
| 49 | + |
| 50 | +async def main() -> None: |
| 51 | + if args.debug: |
| 52 | + logging.basicConfig(level=logging.DEBUG) |
| 53 | + |
| 54 | + async with Vantage(args.host, args.username, args.password) as vantage: |
| 55 | + # Print out the available loads |
| 56 | + print("Load ID Name") |
| 57 | + print("------- ----") |
| 58 | + async for load in vantage.loads: |
| 59 | + print(f"{load.id: ^7} {load.name}") |
| 60 | + print() |
| 61 | + |
| 62 | + # Ask which load to control |
| 63 | + while True: |
| 64 | + try: |
| 65 | + print("Enter a load ID to control:") |
| 66 | + print("> ", end="", flush=True) |
| 67 | + |
| 68 | + load_id = int(input()) |
| 69 | + load = vantage.loads[load_id] |
| 70 | + break |
| 71 | + except (ValueError, KeyError): |
| 72 | + print("Invalid load id") |
| 73 | + continue |
| 74 | + |
| 75 | + # Print control instructions |
| 76 | + print(f"\nControlling load '{load.name}'") |
| 77 | + print(" Use the arrow keys to increase or decrease the load's brightness.") |
| 78 | + print(" Press the spacebar to toggle the load.") |
| 79 | + print(" Press 'q' to quit.\n") |
| 80 | + |
| 81 | + # Listen for control keypresses |
| 82 | + with cbreak_mode(sys.stdin.fileno()): |
| 83 | + while True: |
| 84 | + key = parse_keypress() |
| 85 | + level = load.level or 0 |
| 86 | + |
| 87 | + if key == "KEY_UP": |
| 88 | + # Increase the load's brightness |
| 89 | + await vantage.loads.set_level(load.id, level + 10, transition=1) |
| 90 | + print(f"Increased '{load.name}' brightness to {load.level}%") |
| 91 | + |
| 92 | + elif key == "KEY_DOWN": |
| 93 | + # Decrease the load's brightness |
| 94 | + await vantage.loads.set_level(load.id, level - 10, transition=1) |
| 95 | + print(f"Decreased '{load.name}' brightness to {load.level}%") |
| 96 | + |
| 97 | + elif key == " ": |
| 98 | + # Toggle load |
| 99 | + if load.is_on: |
| 100 | + await vantage.loads.turn_off(load.id) |
| 101 | + print(f"Turned '{load.name}' load off") |
| 102 | + else: |
| 103 | + await vantage.loads.turn_on(load.id) |
| 104 | + print(f"Turned '{load.name}' load on") |
| 105 | + |
| 106 | + elif key == "q": |
| 107 | + break |
| 108 | + |
| 109 | + |
| 110 | +try: |
| 111 | + asyncio.run(main()) |
| 112 | +except KeyboardInterrupt: |
| 113 | + pass |
0 commit comments