Skip to content

Commit 8de1ced

Browse files
authored
Add on, off, and brightness commands to CLI (#2043)
1 parent 85162a2 commit 8de1ced

3 files changed

Lines changed: 133 additions & 0 deletions

File tree

src/wled/cli/__init__.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,67 @@ def unsupported_version_error_handler(
6464
sys.exit(1)
6565

6666

67+
@cli.command("on")
68+
async def command_on(
69+
host: Annotated[
70+
str,
71+
typer.Option(
72+
help="WLED device IP address or hostname",
73+
prompt="Host address",
74+
show_default=False,
75+
),
76+
],
77+
) -> None:
78+
"""Turn on a WLED device."""
79+
async with WLED(host) as led:
80+
await led.master(on=True)
81+
console.print("[green]WLED device turned on!")
82+
83+
84+
@cli.command("off")
85+
async def command_off(
86+
host: Annotated[
87+
str,
88+
typer.Option(
89+
help="WLED device IP address or hostname",
90+
prompt="Host address",
91+
show_default=False,
92+
),
93+
],
94+
) -> None:
95+
"""Turn off a WLED device."""
96+
async with WLED(host) as led:
97+
await led.master(on=False)
98+
console.print("[green]WLED device turned off!")
99+
100+
101+
@cli.command("brightness")
102+
async def command_brightness(
103+
host: Annotated[
104+
str,
105+
typer.Option(
106+
help="WLED device IP address or hostname",
107+
prompt="Host address",
108+
show_default=False,
109+
),
110+
],
111+
brightness: Annotated[
112+
int,
113+
typer.Option(
114+
help="Brightness level (0-255)",
115+
prompt="Brightness",
116+
show_default=False,
117+
min=0,
118+
max=255,
119+
),
120+
],
121+
) -> None:
122+
"""Set the brightness of a WLED device."""
123+
async with WLED(host) as led:
124+
await led.master(brightness=brightness)
125+
console.print(f"[green]Brightness set to {brightness}!")
126+
127+
67128
@cli.command("info")
68129
async def command_info(
69130
host: Annotated[

tests/__snapshots__/test_cli.ambr

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
11
# serializer version: 1
2+
# name: test_brightness_command
3+
'''
4+
Brightness set to 200!
5+
6+
'''
7+
# ---
28
# name: test_cli_structure
39
dict({
10+
'brightness': list([
11+
'brightness',
12+
'host',
13+
]),
414
'effects': list([
515
'host',
616
]),
717
'info': list([
818
'host',
919
]),
20+
'off': list([
21+
'host',
22+
]),
23+
'on': list([
24+
'host',
25+
]),
1026
'palettes': list([
1127
'host',
1228
]),
@@ -194,6 +210,18 @@
194210

195211
'''
196212
# ---
213+
# name: test_off_command
214+
'''
215+
WLED device turned off!
216+
217+
'''
218+
# ---
219+
# name: test_on_command
220+
'''
221+
WLED device turned on!
222+
223+
'''
224+
# ---
197225
# name: test_palettes_command
198226
'''
199227

tests/test_cli.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,50 @@ def test_cli_structure(snapshot: SnapshotAssertion) -> None:
262262
# ---------------------------------------------------------------------------
263263

264264

265+
@pytest.mark.usefixtures("stable_terminal")
266+
def test_on_command(
267+
runner: CliRunner,
268+
snapshot: SnapshotAssertion,
269+
) -> None:
270+
"""On command turns on the device."""
271+
mock = _mock_wled(_device())
272+
with patch("wled.cli.WLED", mock):
273+
result = runner.invoke(cli, ["on", "--host", "example.com"])
274+
assert result.exit_code == 0
275+
assert result.output == snapshot
276+
mock.return_value.master.assert_awaited_once_with(on=True)
277+
278+
279+
@pytest.mark.usefixtures("stable_terminal")
280+
def test_off_command(
281+
runner: CliRunner,
282+
snapshot: SnapshotAssertion,
283+
) -> None:
284+
"""Off command turns off the device."""
285+
mock = _mock_wled(_device())
286+
with patch("wled.cli.WLED", mock):
287+
result = runner.invoke(cli, ["off", "--host", "example.com"])
288+
assert result.exit_code == 0
289+
assert result.output == snapshot
290+
mock.return_value.master.assert_awaited_once_with(on=False)
291+
292+
293+
@pytest.mark.usefixtures("stable_terminal")
294+
def test_brightness_command(
295+
runner: CliRunner,
296+
snapshot: SnapshotAssertion,
297+
) -> None:
298+
"""Brightness command sets device brightness."""
299+
mock = _mock_wled(_device())
300+
with patch("wled.cli.WLED", mock):
301+
result = runner.invoke(
302+
cli, ["brightness", "--host", "example.com", "--brightness", "200"]
303+
)
304+
assert result.exit_code == 0
305+
assert result.output == snapshot
306+
mock.return_value.master.assert_awaited_once_with(brightness=200)
307+
308+
265309
@pytest.mark.usefixtures("stable_terminal")
266310
def test_info_command(
267311
runner: CliRunner,

0 commit comments

Comments
 (0)