Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 32 additions & 18 deletions kasa/cli/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,46 +127,60 @@
def presets_list(dev: Device):
"""List presets."""
if not (light_preset := dev.modules.get(Module.LightPreset)):
error("Presets not supported on device")
error("Device does not support light presets")
return

for idx, preset in enumerate(light_preset.preset_states_list):
echo(
f"[{idx}] Hue: {preset.hue:3} Saturation: {preset.saturation:3} "
f"Brightness/Value: {preset.brightness:3} Temp: {preset.color_temp:4}"
f"[{idx}] Hue: {preset.hue or '':3} "
f"Saturation: {preset.saturation or '':3} "
f"Brightness/Value: {preset.brightness or '':3} "
f"Temp: {preset.color_temp or '':4}"
)

return light_preset.preset_states_list


@presets.command(name="modify")
@click.argument("index", type=int)
@click.option("--brightness", type=int)
@click.option("--hue", type=int)
@click.option("--saturation", type=int)
@click.option("--temperature", type=int)
@click.option("--brightness", type=int, required=False, default=None)
@click.option("--hue", type=int, required=False, default=None)
@click.option("--saturation", type=int, required=False, default=None)
@click.option("--temperature", type=int, required=False, default=None)
@pass_dev_or_child
async def presets_modify(dev: Device, index, brightness, hue, saturation, temperature):
"""Modify a preset."""
for preset in dev.presets:
if preset.index == index:
break
else:
error(f"No preset found for index {index}")
if not (light_preset := dev.modules.get(Module.LightPreset)):
error("Device does not support light presets")
return

Check warning on line 155 in kasa/cli/light.py

View check run for this annotation

Codecov / codecov/patch

kasa/cli/light.py#L154-L155

Added lines #L154 - L155 were not covered by tests

max_index = len(light_preset.preset_states_list) - 1
if index > len(light_preset.preset_states_list) - 1:
error(f"Invalid index, must be between 0 and {max_index}")

Check warning on line 159 in kasa/cli/light.py

View check run for this annotation

Codecov / codecov/patch

kasa/cli/light.py#L159

Added line #L159 was not covered by tests
return

if brightness is not None:
if all([val is None for val in {brightness, hue, saturation, temperature}]):
error("Need to supply at least one option to modify.")
return

Check warning on line 164 in kasa/cli/light.py

View check run for this annotation

Codecov / codecov/patch

kasa/cli/light.py#L164

Added line #L164 was not covered by tests

# Preset names have `Not set`` as the first value
preset_name = light_preset.preset_list[index + 1]
preset = light_preset.preset_states_list[index]

echo(f"Preset {preset_name} currently: {preset}")

if brightness is not None and preset.brightness is not None:
preset.brightness = brightness
if hue is not None:
if hue is not None and preset.hue is not None:
preset.hue = hue
if saturation is not None:
if saturation is not None and preset.saturation is not None:
preset.saturation = saturation
if temperature is not None:
if temperature is not None and preset.temperature is not None:
preset.color_temp = temperature

echo(f"Going to save preset: {preset}")
echo(f"Updating preset {preset_name} to: {preset}")

return await dev.save_preset(preset)
return await light_preset.save_preset(preset_name, preset)


@light.command()
Expand Down
46 changes: 46 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
brightness,
effect,
hsv,
presets,
presets_modify,
temperature,
)
from kasa.cli.main import TYPES, _legacy_type_to_class, cli, cmd_command, raw_command
Expand Down Expand Up @@ -575,6 +577,50 @@ async def test_light_effect(dev: Device, runner: CliRunner):
assert res.exit_code == 2


async def test_light_preset(dev: Device, runner: CliRunner):
res = await runner.invoke(presets, obj=dev)
if not (light_preset := dev.modules.get(Module.LightPreset)):
assert "Device does not support light presets" in res.output
return

if len(light_preset.preset_states_list) == 0:
pytest.skip(
"Some fixtures do not have presets and"
" the api doesn'tsupport creating them"
)
# Start off with a known state
first_name = light_preset.preset_list[1]
await light_preset.set_preset(first_name)
await dev.update()
assert light_preset.preset == first_name

res = await runner.invoke(presets, obj=dev)
assert "Brightness" in res.output
assert res.exit_code == 0

res = await runner.invoke(
presets_modify,
[
"0",
"--brightness",
"12",
],
obj=dev,
)
await dev.update()
assert light_preset.preset_states_list[0].brightness == 12

res = await runner.invoke(
presets_modify,
[
"0",
],
obj=dev,
)
await dev.update()
assert "Need to supply at least one option to modify." in res.output


async def test_led(dev: Device, runner: CliRunner):
res = await runner.invoke(led, obj=dev)
if not (led_module := dev.modules.get(Module.Led)):
Expand Down
Loading