| 1 |
import asyncio |
| 2 |
from prompt_toolkit import PromptSession |
| 3 |
from prompt_toolkit.patch_stdout import patch_stdout |
| 4 |
from prompt_toolkit.shortcuts import clear |
| 5 |
|
| 6 |
from govee_local_api import GoveeController, GoveeDevice, GoveeLightFeatures |
| 7 |
|
| 8 |
|
| 9 |
def update_device_callback(device: GoveeDevice) -> None: |
| 10 |
# print(f"Goveee device update callback: {device}") |
| 11 |
pass |
| 12 |
|
| 13 |
|
| 14 |
def discovered_callback(device: GoveeDevice, is_new: bool) -> bool: |
| 15 |
if is_new: |
| 16 |
# print(f"Discovered: {device}. New: {is_new}") |
| 17 |
device.set_update_callback(update_device_callback) |
| 18 |
return True |
| 19 |
|
| 20 |
|
| 21 |
async def create_controller( |
| 22 |
discovery_enabled: bool, manual_device_ip: str | None = None |
| 23 |
) -> GoveeController: |
| 24 |
controller = GoveeController( |
| 25 |
loop=asyncio.get_event_loop(), |
| 26 |
listening_addresses="0.0.0.0", |
| 27 |
discovery_enabled=discovery_enabled, |
| 28 |
discovered_callback=discovered_callback, |
| 29 |
evicted_callback=lambda device: print(f"Evicted {device}"), |
| 30 |
) |
| 31 |
|
| 32 |
await controller.start() |
| 33 |
|
| 34 |
if discovery_enabled: |
| 35 |
while not controller.devices: |
| 36 |
print("Waiting for devices... ") |
| 37 |
await asyncio.sleep(1) |
| 38 |
else: |
| 39 |
if not manual_device_ip: |
| 40 |
raise ValueError( |
| 41 |
"Manual device IP must be provided if discovery is disabled." |
| 42 |
) |
| 43 |
print(f"Discovery not enabled. Adding {manual_device_ip} to discovery.") |
| 44 |
controller.add_device_to_discovery_queue(manual_device_ip) |
| 45 |
while not controller.devices: |
| 46 |
print(f"Waiting for device {manual_device_ip} to be discovered...") |
| 47 |
await asyncio.sleep(1) |
| 48 |
return controller |
| 49 |
|
| 50 |
|
| 51 |
async def handle_turn_on(device: GoveeDevice) -> None: |
| 52 |
print("Turning on the LED strip...") |
| 53 |
await device.turn_on() |
| 54 |
|
| 55 |
|
| 56 |
async def handle_turn_off(device: GoveeDevice) -> None: |
| 57 |
print("Turning off the LED strip...") |
| 58 |
await device.turn_off() |
| 59 |
|
| 60 |
|
| 61 |
async def handle_set_brightness(device: GoveeDevice, session: PromptSession) -> None: |
| 62 |
while True: |
| 63 |
brightness = await session.prompt_async("Enter brightness (0-100): ") |
| 64 |
try: |
| 65 |
brightness = int(brightness) |
| 66 |
if 0 <= brightness <= 100: |
| 67 |
print(f"Setting brightness to {brightness}%") |
| 68 |
await device.set_brightness(brightness) |
| 69 |
break |
| 70 |
else: |
| 71 |
print("Please enter a value between 0 and 100.") |
| 72 |
except ValueError: |
| 73 |
print("Invalid input, please enter an integer.") |
| 74 |
|
| 75 |
|
| 76 |
async def handle_set_color(device: GoveeDevice, session: PromptSession) -> None: |
| 77 |
while True: |
| 78 |
color = await session.prompt_async("Enter color (R G B, values 0-255): ") |
| 79 |
try: |
| 80 |
r, g, b = map(int, color.split()) |
| 81 |
if all(0 <= v <= 255 for v in [r, g, b]): |
| 82 |
print(f"Setting color to RGB({r}, {g}, {b})") |
| 83 |
await device.set_rgb_color(r, g, b) |
| 84 |
break |
| 85 |
else: |
| 86 |
print("RGB values must be between 0 and 255.") |
| 87 |
except ValueError: |
| 88 |
print("Invalid input, please enter three integers separated by spaces.") |
| 89 |
|
| 90 |
|
| 91 |
async def handle_set_segment_color(device: GoveeDevice, session: PromptSession) -> None: |
| 92 |
if device.capabilities.features & GoveeLightFeatures.SEGMENT_CONTROL == 0: |
| 93 |
print("This device does not support segment control.") |
| 94 |
return |
| 95 |
while True: |
| 96 |
segment = await session.prompt_async( |
| 97 |
f"Enter segment number (1-{device.capabilities.segments_count}): " |
| 98 |
) |
| 99 |
try: |
| 100 |
segment = int(segment) |
| 101 |
if 1 <= segment <= device.capabilities.segments_count: |
| 102 |
color = await session.prompt_async( |
| 103 |
"Enter segment color (R G B, values 0-255): " |
| 104 |
) |
| 105 |
r, g, b = map(int, color.split()) |
| 106 |
if all(0 <= v <= 255 for v in [r, g, b]): |
| 107 |
print(f"Setting segment {segment} color to RGB({r}, {g}, {b})") |
| 108 |
await device.set_segment_rgb_color(segment, r, g, b) |
| 109 |
break |
| 110 |
else: |
| 111 |
print("RGB values must be between 0 and 255.") |
| 112 |
else: |
| 113 |
print( |
| 114 |
f"Segment number must be between 1 and {device.capabilities.segments_count}." |
| 115 |
) |
| 116 |
except ValueError: |
| 117 |
print( |
| 118 |
"Invalid input. Please enter an integer for the segment and three integers for the color." |
| 119 |
) |
| 120 |
|
| 121 |
|
| 122 |
async def handle_set_scene(device: GoveeDevice, session: PromptSession): |
| 123 |
while True: |
| 124 |
scenes = device.capabilities.available_scenes |
| 125 |
for idx, scene in enumerate(scenes): |
| 126 |
print(f"{idx}: {scene}") |
| 127 |
|
| 128 |
scene = await session.prompt_async(f"Enter scene number (0-{len(scenes)}): ") |
| 129 |
scene_name = scenes[int(scene)] |
| 130 |
if scene_name in device.capabilities.available_scenes: |
| 131 |
print(f"Setting scene to {scene}") |
| 132 |
await device.set_scene(scene_name) |
| 133 |
break |
| 134 |
else: |
| 135 |
print("Invalid scene. Please choose from the available scenes.") |
| 136 |
|
| 137 |
|
| 138 |
async def handle_clear_screen(device): |
| 139 |
clear() |
| 140 |
|
| 141 |
|
| 142 |
async def handle_exit(device): |
| 143 |
print("Exiting...") |
| 144 |
raise SystemExit() |
| 145 |
|
| 146 |
|
| 147 |
async def devices_menu(session, devices: list[GoveeDevice]) -> GoveeDevice: |
| 148 |
selection = None |
| 149 |
devices = sorted(devices, key=lambda d: d.sku) |
| 150 |
while ( |
| 151 |
selection is None |
| 152 |
or selection == "" |
| 153 |
or not selection.isnumeric() |
| 154 |
or int(selection) >= len(devices) |
| 155 |
): |
| 156 |
for idx, device in enumerate(devices): |
| 157 |
print(f"{idx}: {device.ip} - {device.sku}") |
| 158 |
selection = await session.prompt_async( |
| 159 |
f"Choose an option (0-{len(devices) - 1}): " |
| 160 |
) |
| 161 |
if not selection.isnumeric(): |
| 162 |
continue |
| 163 |
print(f"Selected device: {selection}") |
| 164 |
return devices[int(selection)] |
| 165 |
|
| 166 |
|
| 167 |
async def handle_send_hex(device: GoveeDevice, session: PromptSession) -> None: |
| 168 |
hex_data = await session.prompt_async("Enter hex data: ") |
| 169 |
await device.send_raw_command(hex_data) |
| 170 |
|
| 171 |
|
| 172 |
async def handle_manual_device( |
| 173 |
device: GoveeDevice, controller: GoveeController, session: PromptSession |
| 174 |
) -> None: |
| 175 |
ip = await session.prompt_async("Enter device IP: ") |
| 176 |
controller.add_device_to_discovery_queue(ip) |
| 177 |
|
| 178 |
|
| 179 |
async def menu(device: GoveeDevice) -> None: |
| 180 |
print("\nDevice: ", device) |
| 181 |
print("Select an option:") |
| 182 |
print("0. Exit") |
| 183 |
print("1. Turn on") |
| 184 |
print("2. Turn off") |
| 185 |
print("3. Set brightness (0-100)") |
| 186 |
print("4. Set color (R G B)") |
| 187 |
print("5. Set segment color (Segment, R G B)") |
| 188 |
print("6. Set scene") |
| 189 |
print("7. Send raw hex") |
| 190 |
print("8. Clear screen") |
| 191 |
print("9. Clear Device") |
| 192 |
print("10. Add device") |
| 193 |
|
| 194 |
|
| 195 |
async def repl() -> None: |
| 196 |
session = PromptSession() |
| 197 |
print("Welcome to the LED Control REPL.") |
| 198 |
|
| 199 |
# Dictionary of command handlers |
| 200 |
command_handlers = { |
| 201 |
"0": handle_exit, |
| 202 |
"1": handle_turn_on, |
| 203 |
"2": handle_turn_off, |
| 204 |
"3": lambda device: handle_set_brightness(device, session), |
| 205 |
"4": lambda device: handle_set_color(device, session), |
| 206 |
"5": lambda device: handle_set_segment_color(device, session), |
| 207 |
"6": lambda device: handle_set_scene(device, session), |
| 208 |
"7": lambda device: handle_send_hex(device, session), |
| 209 |
"8": handle_clear_screen, |
| 210 |
"10": lambda device: handle_manual_device(device, controller, session), |
| 211 |
} |
| 212 |
|
| 213 |
enable_discovery = await session.prompt_async("Enable device discovery? (y/n): ") |
| 214 |
discovery_enabled: bool = enable_discovery.lower() == "y" |
| 215 |
selected_device: GoveeDevice | None = None |
| 216 |
|
| 217 |
manual_device_ip = None |
| 218 |
if not discovery_enabled: |
| 219 |
manual_device_ip = await session.prompt_async("Enter device IP: ") |
| 220 |
controller: GoveeController = await create_controller( |
| 221 |
discovery_enabled, manual_device_ip |
| 222 |
) |
| 223 |
|
| 224 |
while True: |
| 225 |
if not selected_device: |
| 226 |
selected_device = await devices_menu(session, controller.devices) |
| 227 |
|
| 228 |
await menu(selected_device) |
| 229 |
with patch_stdout(): |
| 230 |
user_choice = await session.prompt_async( |
| 231 |
f"Choose an option (0-{len(command_handlers)}): " |
| 232 |
) |
| 233 |
if user_choice == "9": |
| 234 |
selected_device = None |
| 235 |
continue |
| 236 |
|
| 237 |
# Get the command handler from the dictionary, or return an invalid message if not found |
| 238 |
handler = command_handlers.get(user_choice) |
| 239 |
if handler: |
| 240 |
await handler(selected_device) # Call the appropriate command handler |
| 241 |
else: |
| 242 |
print("Invalid option, please try again.") |
| 243 |
|
| 244 |
|
| 245 |
async def main(): |
| 246 |
await repl() |
| 247 |
|
| 248 |
|
| 249 |
if __name__ == "__main__": |
| 250 |
try: |
| 251 |
asyncio.run(main()) |
| 252 |
except (EOFError, KeyboardInterrupt): |
| 253 |
print("REPL exited.") |