-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwizlight_cli
More file actions
executable file
·81 lines (63 loc) · 2.89 KB
/
wizlight_cli
File metadata and controls
executable file
·81 lines (63 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python3
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()