From 29507782e28e06c996fbc3874112f51b8cbf5d92 Mon Sep 17 00:00:00 2001 From: treaki Date: Sun, 4 Aug 2024 21:38:49 +0200 Subject: [PATCH 1/2] Create wizlight_cli better cli without annoying interactive requests for every value --- wizlight_cli | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 wizlight_cli diff --git a/wizlight_cli b/wizlight_cli new file mode 100644 index 0000000..3174ed7 --- /dev/null +++ b/wizlight_cli @@ -0,0 +1,77 @@ +import argparse +import asyncio +from pywizlight import wizlight, PilotBuilder, discovery + +async def discover_lights_cli(): + bulbs = await discovery.discover_lights() + for bulb in bulbs: + print(f"Found bulb: {bulb.ip}") + +async def turn_on_light(ip): + bulb = wizlight(ip) + await bulb.turn_on() + +async def turn_off_light(ip): + bulb = wizlight(ip) + await bulb.turn_off() + +async def get_state(ip): + bulb = wizlight(ip) + state = await bulb.updateState() + print("Current state of the bulb:") + print(f" - Power: {'On' if state.get_state() else 'Off'}") + print(f" - Brightness: {state.get_brightness()}") + print(f" - RGB: {state.get_rgb()}") + print(f" - Color Temp: {state.get_colortemp()}") + print(f" - Scene: {state.get_scene()}") + +async def set_state(ip, brightness=None, r=None, g=None, b=None): + bulb = wizlight(ip) + pilot = PilotBuilder() + if brightness is not None: + pilot = PilotBuilder(brightness=brightness) + if r is not None and g is not None and b is not None: + pilot = PilotBuilder(rgb=(r, g, b)) + await bulb.turn_on(pilot) + +def main(): + parser = argparse.ArgumentParser(description="Control WizLight bulbs via command line.") + subparsers = parser.add_subparsers(dest="command", required=True) + + parser_discover = subparsers.add_parser("discover", help="Discover bulbs in the local network") + + parser_on = subparsers.add_parser("on", help="Turn a given bulb on") + parser_on.add_argument("ip", help="IP address of the WizLight bulb") + + parser_off = subparsers.add_parser("off", help="Turn a given bulb off") + parser_off.add_argument("ip", help="IP address of the WizLight bulb") + + parser_state = subparsers.add_parser("state", help="Get the current state of a given bulb") + parser_state.add_argument("ip", help="IP address of the WizLight bulb") + + parser_set_state = subparsers.add_parser("set-state", help="Set the current state of a given bulb") + parser_set_state.add_argument("ip", help="IP address of the WizLight bulb") + parser_set_state.add_argument("--brightness", type=int, help="Brightness value (0-255)") + parser_set_state.add_argument("--color", type=str, help="Color value in the format r,g,b") + + args = parser.parse_args() + + loop = asyncio.get_event_loop() + + if args.command == "discover": + loop.run_until_complete(discover_lights_cli()) + elif args.command == "on": + loop.run_until_complete(turn_on_light(args.ip)) + elif args.command == "off": + loop.run_until_complete(turn_off_light(args.ip)) + elif args.command == "state": + loop.run_until_complete(get_state(args.ip)) + elif args.command == "set-state": + brightness = args.brightness + color = None + if args.color: + color = tuple(map(int, args.color.split(','))) + loop.run_until_complete(set_state(args.ip, brightness, *color)) + +if __name__ == "__main__": + main() From 37988fc2ed5f6fe0b78274874ca66d2a9e9d48ae Mon Sep 17 00:00:00 2001 From: "Mr. T" Date: Sun, 4 Aug 2024 21:40:22 +0200 Subject: [PATCH 2/2] made wizlight_cli executable --- wizlight_cli | 1 + 1 file changed, 1 insertion(+) mode change 100644 => 100755 wizlight_cli diff --git a/wizlight_cli b/wizlight_cli old mode 100644 new mode 100755 index 3174ed7..44c1575 --- a/wizlight_cli +++ b/wizlight_cli @@ -1,3 +1,4 @@ +#!/usr/bin/env python3 import argparse import asyncio from pywizlight import wizlight, PilotBuilder, discovery