2024-05-19 10:20:18 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-08-30 15:30:07 +00:00
|
|
|
import re
|
|
|
|
|
2020-05-27 14:55:18 +00:00
|
|
|
import pytest
|
2024-01-29 19:26:39 +00:00
|
|
|
from voluptuous import (
|
|
|
|
All,
|
|
|
|
Boolean,
|
|
|
|
Optional,
|
|
|
|
Range,
|
|
|
|
Schema,
|
|
|
|
)
|
2020-05-27 14:55:18 +00:00
|
|
|
|
2024-07-31 14:56:07 +00:00
|
|
|
from kasa import Device, DeviceType, IotLightPreset, KasaException, LightState, Module
|
2024-04-29 17:19:44 +00:00
|
|
|
from kasa.iot import IotBulb, IotDimmer
|
2024-11-13 16:10:06 +00:00
|
|
|
from kasa.iot.modules import LightPreset as IotLightPresetModule
|
2020-05-27 14:55:18 +00:00
|
|
|
|
|
|
|
from .conftest import (
|
|
|
|
bulb,
|
2023-12-05 19:07:10 +00:00
|
|
|
bulb_iot,
|
2020-05-27 14:55:18 +00:00
|
|
|
color_bulb,
|
2023-12-05 19:07:10 +00:00
|
|
|
color_bulb_iot,
|
2024-05-13 16:34:44 +00:00
|
|
|
dimmable_iot,
|
2020-05-27 14:55:18 +00:00
|
|
|
handle_turn_on,
|
|
|
|
non_color_bulb,
|
2024-05-13 16:34:44 +00:00
|
|
|
non_dimmable_iot,
|
2020-05-27 14:55:18 +00:00
|
|
|
non_variable_temp,
|
|
|
|
turn_on,
|
|
|
|
variable_temp,
|
2023-12-05 19:07:10 +00:00
|
|
|
variable_temp_iot,
|
2024-04-20 15:18:35 +00:00
|
|
|
variable_temp_smart,
|
2020-05-27 14:55:18 +00:00
|
|
|
)
|
2024-03-15 15:55:48 +00:00
|
|
|
from .test_iotdevice import SYSINFO_SCHEMA
|
2020-05-27 14:55:18 +00:00
|
|
|
|
|
|
|
|
2024-11-11 17:41:31 +00:00
|
|
|
@bulb_iot
|
2024-04-29 17:19:44 +00:00
|
|
|
async def test_bulb_sysinfo(dev: Device):
|
2020-05-27 14:55:18 +00:00
|
|
|
assert dev.sys_info is not None
|
2024-01-29 19:26:39 +00:00
|
|
|
SYSINFO_SCHEMA_BULB(dev.sys_info)
|
2020-05-27 14:55:18 +00:00
|
|
|
|
|
|
|
assert dev.model is not None
|
|
|
|
|
|
|
|
|
|
|
|
@bulb
|
2024-04-29 17:19:44 +00:00
|
|
|
async def test_state_attributes(dev: Device):
|
2024-03-26 18:28:39 +00:00
|
|
|
assert "Cloud connection" in dev.state_information
|
|
|
|
assert isinstance(dev.state_information["Cloud connection"], bool)
|
2020-05-27 14:55:18 +00:00
|
|
|
|
|
|
|
|
2023-12-05 19:07:10 +00:00
|
|
|
@bulb_iot
|
2024-02-04 15:20:08 +00:00
|
|
|
async def test_light_state_without_update(dev: IotBulb, monkeypatch):
|
2024-08-30 15:30:07 +00:00
|
|
|
monkeypatch.setitem(dev._last_update["system"]["get_sysinfo"], "light_state", None)
|
2024-02-21 15:52:55 +00:00
|
|
|
with pytest.raises(KasaException):
|
2020-05-27 14:55:18 +00:00
|
|
|
print(dev.light_state)
|
|
|
|
|
|
|
|
|
2023-12-05 19:07:10 +00:00
|
|
|
@bulb_iot
|
2024-02-04 15:20:08 +00:00
|
|
|
async def test_get_light_state(dev: IotBulb):
|
2020-05-27 14:55:18 +00:00
|
|
|
LIGHT_STATE_SCHEMA(await dev.get_light_state())
|
|
|
|
|
|
|
|
|
|
|
|
@color_bulb
|
|
|
|
@turn_on
|
2024-04-29 17:19:44 +00:00
|
|
|
async def test_hsv(dev: Device, turn_on):
|
2024-05-13 16:34:44 +00:00
|
|
|
light = dev.modules.get(Module.Light)
|
|
|
|
assert light
|
2020-05-27 14:55:18 +00:00
|
|
|
await handle_turn_on(dev, turn_on)
|
2024-05-13 16:34:44 +00:00
|
|
|
assert light.is_color
|
2020-05-27 14:55:18 +00:00
|
|
|
|
2024-05-13 16:34:44 +00:00
|
|
|
hue, saturation, brightness = light.hsv
|
2021-09-24 21:25:43 +00:00
|
|
|
assert 0 <= hue <= 360
|
2020-05-27 14:55:18 +00:00
|
|
|
assert 0 <= saturation <= 100
|
|
|
|
assert 0 <= brightness <= 100
|
|
|
|
|
2024-05-13 16:34:44 +00:00
|
|
|
await light.set_hsv(hue=1, saturation=1, value=1)
|
2020-05-27 14:55:18 +00:00
|
|
|
|
2021-09-19 21:45:48 +00:00
|
|
|
await dev.update()
|
2024-05-13 16:34:44 +00:00
|
|
|
hue, saturation, brightness = light.hsv
|
2020-05-27 14:55:18 +00:00
|
|
|
assert hue == 1
|
|
|
|
assert saturation == 1
|
|
|
|
assert brightness == 1
|
|
|
|
|
|
|
|
|
2023-12-05 19:07:10 +00:00
|
|
|
@color_bulb_iot
|
2024-02-04 15:20:08 +00:00
|
|
|
async def test_set_hsv_transition(dev: IotBulb, mocker):
|
2024-05-19 10:20:18 +00:00
|
|
|
set_light_state = mocker.patch("kasa.iot.IotBulb._set_light_state")
|
2024-11-13 16:10:06 +00:00
|
|
|
light = dev.modules.get(Module.Light)
|
|
|
|
assert light
|
|
|
|
await light.set_hsv(10, 10, 100, transition=1000)
|
2020-06-14 18:21:55 +00:00
|
|
|
|
|
|
|
set_light_state.assert_called_with(
|
|
|
|
{"hue": 10, "saturation": 10, "brightness": 100, "color_temp": 0},
|
|
|
|
transition=1000,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-07-31 14:56:07 +00:00
|
|
|
@bulb_iot
|
|
|
|
async def test_light_set_state(dev: IotBulb, mocker):
|
|
|
|
"""Testing setting LightState on the light module."""
|
|
|
|
light = dev.modules.get(Module.Light)
|
|
|
|
assert light
|
|
|
|
set_light_state = mocker.spy(dev, "_set_light_state")
|
|
|
|
state = LightState(light_on=True)
|
|
|
|
await light.set_state(state)
|
|
|
|
|
|
|
|
set_light_state.assert_called_with({"on_off": 1}, transition=None)
|
|
|
|
state = LightState(light_on=False)
|
|
|
|
await light.set_state(state)
|
|
|
|
|
|
|
|
set_light_state.assert_called_with({"on_off": 0}, transition=None)
|
|
|
|
|
|
|
|
|
2020-05-27 14:55:18 +00:00
|
|
|
@color_bulb
|
|
|
|
@turn_on
|
2024-08-30 15:30:07 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("hue", "sat", "brightness", "exception_cls", "error"),
|
|
|
|
[
|
|
|
|
pytest.param(-1, 0, 0, ValueError, "Invalid hue", id="hue out of range"),
|
|
|
|
pytest.param(361, 0, 0, ValueError, "Invalid hue", id="hue out of range"),
|
|
|
|
pytest.param(
|
|
|
|
0.5, 0, 0, TypeError, "Hue must be an integer", id="hue invalid type"
|
|
|
|
),
|
|
|
|
pytest.param(
|
|
|
|
"foo", 0, 0, TypeError, "Hue must be an integer", id="hue invalid type"
|
|
|
|
),
|
|
|
|
pytest.param(
|
|
|
|
0, -1, 0, ValueError, "Invalid saturation", id="saturation out of range"
|
|
|
|
),
|
|
|
|
pytest.param(
|
|
|
|
0, 101, 0, ValueError, "Invalid saturation", id="saturation out of range"
|
|
|
|
),
|
|
|
|
pytest.param(
|
|
|
|
0,
|
|
|
|
0.5,
|
|
|
|
0,
|
|
|
|
TypeError,
|
|
|
|
"Saturation must be an integer",
|
|
|
|
id="saturation invalid type",
|
|
|
|
),
|
|
|
|
pytest.param(
|
|
|
|
0,
|
|
|
|
"foo",
|
|
|
|
0,
|
|
|
|
TypeError,
|
|
|
|
"Saturation must be an integer",
|
|
|
|
id="saturation invalid type",
|
|
|
|
),
|
|
|
|
pytest.param(
|
|
|
|
0, 0, -1, ValueError, "Invalid brightness", id="brightness out of range"
|
|
|
|
),
|
|
|
|
pytest.param(
|
|
|
|
0, 0, 101, ValueError, "Invalid brightness", id="brightness out of range"
|
|
|
|
),
|
|
|
|
pytest.param(
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0.5,
|
|
|
|
TypeError,
|
|
|
|
"Brightness must be an integer",
|
|
|
|
id="brightness invalid type",
|
|
|
|
),
|
|
|
|
pytest.param(
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
"foo",
|
|
|
|
TypeError,
|
|
|
|
"Brightness must be an integer",
|
|
|
|
id="brightness invalid type",
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_invalid_hsv(
|
|
|
|
dev: Device, turn_on, hue, sat, brightness, exception_cls, error
|
|
|
|
):
|
2024-05-13 16:34:44 +00:00
|
|
|
light = dev.modules.get(Module.Light)
|
|
|
|
assert light
|
2020-05-27 14:55:18 +00:00
|
|
|
await handle_turn_on(dev, turn_on)
|
2024-05-13 16:34:44 +00:00
|
|
|
assert light.is_color
|
2024-08-30 15:30:07 +00:00
|
|
|
with pytest.raises(exception_cls, match=error):
|
|
|
|
await light.set_hsv(hue, sat, brightness)
|
2020-05-27 14:55:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
@color_bulb
|
2024-03-26 18:28:39 +00:00
|
|
|
@pytest.mark.skip("requires color feature")
|
2024-04-29 17:19:44 +00:00
|
|
|
async def test_color_state_information(dev: Device):
|
2024-05-13 16:34:44 +00:00
|
|
|
light = dev.modules.get(Module.Light)
|
|
|
|
assert light
|
2020-05-27 14:55:18 +00:00
|
|
|
assert "HSV" in dev.state_information
|
2024-05-13 16:34:44 +00:00
|
|
|
assert dev.state_information["HSV"] == light.hsv
|
2020-05-27 14:55:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
@non_color_bulb
|
2024-05-13 16:34:44 +00:00
|
|
|
async def test_hsv_on_non_color(dev: Device):
|
|
|
|
light = dev.modules.get(Module.Light)
|
|
|
|
assert light
|
|
|
|
assert not light.is_color
|
2020-05-27 14:55:18 +00:00
|
|
|
|
2024-02-21 15:52:55 +00:00
|
|
|
with pytest.raises(KasaException):
|
2024-05-13 16:34:44 +00:00
|
|
|
await light.set_hsv(0, 0, 0)
|
2024-02-21 15:52:55 +00:00
|
|
|
with pytest.raises(KasaException):
|
2024-05-13 16:34:44 +00:00
|
|
|
print(light.hsv)
|
2020-05-27 14:55:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
@variable_temp
|
2024-03-26 18:28:39 +00:00
|
|
|
@pytest.mark.skip("requires colortemp module")
|
2024-04-29 17:19:44 +00:00
|
|
|
async def test_variable_temp_state_information(dev: Device):
|
2024-05-13 16:34:44 +00:00
|
|
|
light = dev.modules.get(Module.Light)
|
|
|
|
assert light
|
2020-05-27 14:55:18 +00:00
|
|
|
assert "Color temperature" in dev.state_information
|
2024-05-13 16:34:44 +00:00
|
|
|
assert dev.state_information["Color temperature"] == light.color_temp
|
2020-05-27 14:55:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
@variable_temp
|
|
|
|
@turn_on
|
2024-04-29 17:19:44 +00:00
|
|
|
async def test_try_set_colortemp(dev: Device, turn_on):
|
2024-05-13 16:34:44 +00:00
|
|
|
light = dev.modules.get(Module.Light)
|
|
|
|
assert light
|
2020-05-27 14:55:18 +00:00
|
|
|
await handle_turn_on(dev, turn_on)
|
2024-05-13 16:34:44 +00:00
|
|
|
await light.set_color_temp(2700)
|
2021-09-19 21:45:48 +00:00
|
|
|
await dev.update()
|
2024-05-13 16:34:44 +00:00
|
|
|
assert light.color_temp == 2700
|
2020-05-27 14:55:18 +00:00
|
|
|
|
|
|
|
|
2023-12-05 19:07:10 +00:00
|
|
|
@variable_temp_iot
|
2024-02-04 15:20:08 +00:00
|
|
|
async def test_set_color_temp_transition(dev: IotBulb, mocker):
|
2024-05-19 10:20:18 +00:00
|
|
|
set_light_state = mocker.patch("kasa.iot.IotBulb._set_light_state")
|
2024-11-13 16:10:06 +00:00
|
|
|
light = dev.modules.get(Module.Light)
|
|
|
|
assert light
|
|
|
|
await light.set_color_temp(2700, transition=100)
|
2020-06-14 18:21:55 +00:00
|
|
|
|
|
|
|
set_light_state.assert_called_with({"color_temp": 2700}, transition=100)
|
|
|
|
|
|
|
|
|
2023-12-05 19:07:10 +00:00
|
|
|
@variable_temp_iot
|
2024-02-04 15:20:08 +00:00
|
|
|
async def test_unknown_temp_range(dev: IotBulb, monkeypatch, caplog):
|
2021-09-21 11:23:56 +00:00
|
|
|
monkeypatch.setitem(dev._sys_info, "model", "unknown bulb")
|
2024-11-13 16:10:06 +00:00
|
|
|
light = dev.modules.get(Module.Light)
|
|
|
|
assert light
|
|
|
|
assert light.valid_temperature_range == (2700, 5000)
|
2021-09-21 11:23:56 +00:00
|
|
|
assert "Unknown color temperature range, fallback to 2700-5000" in caplog.text
|
2020-05-27 14:55:18 +00:00
|
|
|
|
|
|
|
|
2024-04-20 15:18:35 +00:00
|
|
|
@variable_temp_smart
|
2024-05-13 16:34:44 +00:00
|
|
|
async def test_smart_temp_range(dev: Device):
|
|
|
|
light = dev.modules.get(Module.Light)
|
|
|
|
assert light
|
|
|
|
assert light.valid_temperature_range
|
2024-04-20 15:18:35 +00:00
|
|
|
|
|
|
|
|
2020-05-27 14:55:18 +00:00
|
|
|
@variable_temp
|
2024-05-13 16:34:44 +00:00
|
|
|
async def test_out_of_range_temperature(dev: Device):
|
|
|
|
light = dev.modules.get(Module.Light)
|
|
|
|
assert light
|
2024-08-30 15:30:07 +00:00
|
|
|
with pytest.raises(
|
2024-09-27 15:36:41 +00:00
|
|
|
ValueError, match=r"Temperature should be between \d+ and \d+, was 1000"
|
2024-08-30 15:30:07 +00:00
|
|
|
):
|
2024-05-13 16:34:44 +00:00
|
|
|
await light.set_color_temp(1000)
|
2024-08-30 15:30:07 +00:00
|
|
|
with pytest.raises(
|
2024-09-27 15:36:41 +00:00
|
|
|
ValueError, match=r"Temperature should be between \d+ and \d+, was 10000"
|
2024-08-30 15:30:07 +00:00
|
|
|
):
|
2024-05-13 16:34:44 +00:00
|
|
|
await light.set_color_temp(10000)
|
2020-05-27 14:55:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
@non_variable_temp
|
2024-05-13 16:34:44 +00:00
|
|
|
async def test_non_variable_temp(dev: Device):
|
|
|
|
light = dev.modules.get(Module.Light)
|
|
|
|
assert light
|
2024-02-21 15:52:55 +00:00
|
|
|
with pytest.raises(KasaException):
|
2024-05-13 16:34:44 +00:00
|
|
|
await light.set_color_temp(2700)
|
2020-05-27 14:55:18 +00:00
|
|
|
|
2024-02-21 15:52:55 +00:00
|
|
|
with pytest.raises(KasaException):
|
2024-05-13 16:34:44 +00:00
|
|
|
print(light.valid_temperature_range)
|
2020-05-27 14:55:18 +00:00
|
|
|
|
2024-02-21 15:52:55 +00:00
|
|
|
with pytest.raises(KasaException):
|
2024-05-13 16:34:44 +00:00
|
|
|
print(light.color_temp)
|
2020-05-27 14:55:18 +00:00
|
|
|
|
|
|
|
|
2024-05-13 16:34:44 +00:00
|
|
|
@dimmable_iot
|
2020-05-27 14:55:18 +00:00
|
|
|
@turn_on
|
2024-05-13 16:34:44 +00:00
|
|
|
async def test_dimmable_brightness(dev: IotBulb, turn_on):
|
2024-11-18 18:46:36 +00:00
|
|
|
assert isinstance(dev, IotBulb | IotDimmer)
|
2024-11-13 16:10:06 +00:00
|
|
|
light = dev.modules.get(Module.Light)
|
|
|
|
assert light
|
2020-05-27 14:55:18 +00:00
|
|
|
await handle_turn_on(dev, turn_on)
|
2024-05-13 17:52:08 +00:00
|
|
|
assert dev._is_dimmable
|
2020-05-27 14:55:18 +00:00
|
|
|
|
2024-11-13 16:10:06 +00:00
|
|
|
await light.set_brightness(50)
|
2021-09-19 21:45:48 +00:00
|
|
|
await dev.update()
|
2024-11-13 16:10:06 +00:00
|
|
|
assert light.brightness == 50
|
2020-05-27 14:55:18 +00:00
|
|
|
|
2024-11-13 16:10:06 +00:00
|
|
|
await light.set_brightness(10)
|
2021-09-19 21:45:48 +00:00
|
|
|
await dev.update()
|
2024-11-13 16:10:06 +00:00
|
|
|
assert light.brightness == 10
|
2020-05-27 14:55:18 +00:00
|
|
|
|
2024-08-30 15:30:07 +00:00
|
|
|
with pytest.raises(TypeError, match="Brightness must be an integer"):
|
2024-11-13 16:10:06 +00:00
|
|
|
await light.set_brightness("foo") # type: ignore[arg-type]
|
2020-05-27 14:55:18 +00:00
|
|
|
|
|
|
|
|
2023-12-05 19:07:10 +00:00
|
|
|
@bulb_iot
|
2024-02-04 15:20:08 +00:00
|
|
|
async def test_turn_on_transition(dev: IotBulb, mocker):
|
2024-05-19 10:20:18 +00:00
|
|
|
set_light_state = mocker.patch("kasa.iot.IotBulb._set_light_state")
|
2020-06-14 18:21:55 +00:00
|
|
|
await dev.turn_on(transition=1000)
|
|
|
|
|
|
|
|
set_light_state.assert_called_with({"on_off": 1}, transition=1000)
|
|
|
|
|
|
|
|
await dev.turn_off(transition=100)
|
|
|
|
|
|
|
|
set_light_state.assert_called_with({"on_off": 0}, transition=100)
|
|
|
|
|
|
|
|
|
2023-12-05 19:07:10 +00:00
|
|
|
@bulb_iot
|
2024-02-04 15:20:08 +00:00
|
|
|
async def test_dimmable_brightness_transition(dev: IotBulb, mocker):
|
2024-05-19 10:20:18 +00:00
|
|
|
set_light_state = mocker.patch("kasa.iot.IotBulb._set_light_state")
|
2024-11-13 16:10:06 +00:00
|
|
|
light = dev.modules.get(Module.Light)
|
|
|
|
assert light
|
|
|
|
await light.set_brightness(10, transition=1000)
|
2020-06-14 18:21:55 +00:00
|
|
|
|
2024-05-22 13:33:55 +00:00
|
|
|
set_light_state.assert_called_with({"brightness": 10, "on_off": 1}, transition=1000)
|
2020-06-14 18:21:55 +00:00
|
|
|
|
|
|
|
|
2024-05-13 16:34:44 +00:00
|
|
|
@dimmable_iot
|
|
|
|
async def test_invalid_brightness(dev: IotBulb):
|
2024-05-13 17:52:08 +00:00
|
|
|
assert dev._is_dimmable
|
2024-11-13 16:10:06 +00:00
|
|
|
light = dev.modules.get(Module.Light)
|
|
|
|
assert light
|
2024-08-30 15:30:07 +00:00
|
|
|
with pytest.raises(
|
|
|
|
ValueError,
|
|
|
|
match=re.escape("Invalid brightness value: 110 (valid range: 0-100%)"),
|
|
|
|
):
|
2024-11-13 16:10:06 +00:00
|
|
|
await light.set_brightness(110)
|
2020-05-27 14:55:18 +00:00
|
|
|
|
2024-08-30 15:30:07 +00:00
|
|
|
with pytest.raises(
|
|
|
|
ValueError,
|
|
|
|
match=re.escape("Invalid brightness value: -100 (valid range: 0-100%)"),
|
|
|
|
):
|
2024-11-13 16:10:06 +00:00
|
|
|
await light.set_brightness(-100)
|
2020-05-27 14:55:18 +00:00
|
|
|
|
|
|
|
|
2024-05-13 16:34:44 +00:00
|
|
|
@non_dimmable_iot
|
|
|
|
async def test_non_dimmable(dev: IotBulb):
|
2024-05-13 17:52:08 +00:00
|
|
|
assert not dev._is_dimmable
|
2024-11-13 16:10:06 +00:00
|
|
|
light = dev.modules.get(Module.Light)
|
|
|
|
assert light
|
2024-02-21 15:52:55 +00:00
|
|
|
with pytest.raises(KasaException):
|
2024-11-13 16:10:06 +00:00
|
|
|
assert light.brightness == 0
|
2024-02-21 15:52:55 +00:00
|
|
|
with pytest.raises(KasaException):
|
2024-11-13 16:10:06 +00:00
|
|
|
await light.set_brightness(100)
|
2022-04-05 16:51:36 +00:00
|
|
|
|
|
|
|
|
2023-12-05 19:07:10 +00:00
|
|
|
@bulb_iot
|
2022-10-22 22:15:47 +00:00
|
|
|
async def test_ignore_default_not_set_without_color_mode_change_turn_on(
|
2024-02-04 15:20:08 +00:00
|
|
|
dev: IotBulb, mocker
|
2022-10-22 22:15:47 +00:00
|
|
|
):
|
2024-02-04 15:20:08 +00:00
|
|
|
query_helper = mocker.patch("kasa.iot.IotBulb._query_helper")
|
2022-04-05 16:51:36 +00:00
|
|
|
# When turning back without settings, ignore default to restore the state
|
|
|
|
await dev.turn_on()
|
|
|
|
args, kwargs = query_helper.call_args_list[0]
|
|
|
|
assert args[2] == {"on_off": 1, "ignore_default": 0}
|
|
|
|
|
|
|
|
await dev.turn_off()
|
|
|
|
args, kwargs = query_helper.call_args_list[1]
|
|
|
|
assert args[2] == {"on_off": 0, "ignore_default": 1}
|
2022-10-22 22:15:47 +00:00
|
|
|
|
|
|
|
|
2023-12-05 19:07:10 +00:00
|
|
|
@bulb_iot
|
2024-02-04 15:20:08 +00:00
|
|
|
async def test_list_presets(dev: IotBulb):
|
2024-11-13 16:10:06 +00:00
|
|
|
light_preset = dev.modules.get(Module.LightPreset)
|
|
|
|
assert light_preset
|
|
|
|
assert isinstance(light_preset, IotLightPresetModule)
|
|
|
|
presets = light_preset._deprecated_presets
|
2024-06-04 17:24:53 +00:00
|
|
|
# Light strip devices may list some light effects along with normal presets but these
|
|
|
|
# are handled by the LightEffect module so exclude preferred states with id
|
|
|
|
raw_presets = [
|
|
|
|
pstate for pstate in dev.sys_info["preferred_state"] if "id" not in pstate
|
|
|
|
]
|
|
|
|
assert len(presets) == len(raw_presets)
|
|
|
|
|
2024-11-18 18:46:36 +00:00
|
|
|
for preset, raw in zip(presets, raw_presets, strict=False):
|
2022-10-22 22:15:47 +00:00
|
|
|
assert preset.index == raw["index"]
|
|
|
|
assert preset.brightness == raw["brightness"]
|
2024-06-04 17:24:53 +00:00
|
|
|
assert preset.hue == raw["hue"]
|
2022-10-22 22:15:47 +00:00
|
|
|
assert preset.saturation == raw["saturation"]
|
|
|
|
assert preset.color_temp == raw["color_temp"]
|
|
|
|
|
|
|
|
|
2023-12-05 19:07:10 +00:00
|
|
|
@bulb_iot
|
2024-02-04 15:20:08 +00:00
|
|
|
async def test_modify_preset(dev: IotBulb, mocker):
|
2023-04-01 14:15:58 +00:00
|
|
|
"""Verify that modifying preset calls the and exceptions are raised properly."""
|
2024-11-13 16:10:06 +00:00
|
|
|
if (
|
|
|
|
not (light_preset := dev.modules.get(Module.LightPreset))
|
|
|
|
or not light_preset._deprecated_presets
|
|
|
|
):
|
2022-10-22 22:15:47 +00:00
|
|
|
pytest.skip("Some strips do not support presets")
|
|
|
|
|
2024-11-13 16:10:06 +00:00
|
|
|
assert isinstance(light_preset, IotLightPresetModule)
|
2024-05-19 10:20:18 +00:00
|
|
|
data: dict[str, int | None] = {
|
2022-10-22 22:15:47 +00:00
|
|
|
"index": 0,
|
|
|
|
"brightness": 10,
|
|
|
|
"hue": 0,
|
|
|
|
"saturation": 0,
|
|
|
|
"color_temp": 0,
|
|
|
|
}
|
2024-05-19 10:20:18 +00:00
|
|
|
preset = IotLightPreset(**data) # type: ignore[call-arg, arg-type]
|
2022-10-22 22:15:47 +00:00
|
|
|
|
|
|
|
assert preset.index == 0
|
|
|
|
assert preset.brightness == 10
|
|
|
|
assert preset.hue == 0
|
|
|
|
assert preset.saturation == 0
|
|
|
|
assert preset.color_temp == 0
|
|
|
|
|
2024-11-13 16:10:06 +00:00
|
|
|
await light_preset._deprecated_save_preset(preset)
|
2024-04-24 11:25:16 +00:00
|
|
|
await dev.update()
|
2024-11-13 16:10:06 +00:00
|
|
|
assert light_preset._deprecated_presets[0].brightness == 10
|
2022-10-22 22:15:47 +00:00
|
|
|
|
2024-02-21 15:52:55 +00:00
|
|
|
with pytest.raises(KasaException):
|
2024-11-13 16:10:06 +00:00
|
|
|
await light_preset._deprecated_save_preset(
|
2024-05-19 10:20:18 +00:00
|
|
|
IotLightPreset(index=5, hue=0, brightness=0, saturation=0, color_temp=0) # type: ignore[call-arg]
|
2022-10-22 22:15:47 +00:00
|
|
|
)
|
2023-04-01 14:15:58 +00:00
|
|
|
|
|
|
|
|
2023-12-05 19:07:10 +00:00
|
|
|
@bulb_iot
|
2023-04-01 14:15:58 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("preset", "payload"),
|
|
|
|
[
|
|
|
|
(
|
2024-05-19 10:20:18 +00:00
|
|
|
IotLightPreset(index=0, hue=0, brightness=1, saturation=0), # type: ignore[call-arg]
|
2023-04-01 14:15:58 +00:00
|
|
|
{"index": 0, "hue": 0, "brightness": 1, "saturation": 0},
|
|
|
|
),
|
|
|
|
(
|
2024-05-19 10:20:18 +00:00
|
|
|
IotLightPreset(index=0, brightness=1, id="testid", mode=2, custom=0), # type: ignore[call-arg]
|
2023-04-01 14:15:58 +00:00
|
|
|
{"index": 0, "brightness": 1, "id": "testid", "mode": 2, "custom": 0},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2024-02-04 15:20:08 +00:00
|
|
|
async def test_modify_preset_payloads(dev: IotBulb, preset, payload, mocker):
|
2023-04-01 14:15:58 +00:00
|
|
|
"""Test that modify preset payloads ignore none values."""
|
2024-11-13 16:10:06 +00:00
|
|
|
if (
|
|
|
|
not (light_preset := dev.modules.get(Module.LightPreset))
|
|
|
|
or not light_preset._deprecated_presets
|
|
|
|
):
|
2023-04-01 14:15:58 +00:00
|
|
|
pytest.skip("Some strips do not support presets")
|
|
|
|
|
2024-02-04 15:20:08 +00:00
|
|
|
query_helper = mocker.patch("kasa.iot.IotBulb._query_helper")
|
2024-11-13 16:10:06 +00:00
|
|
|
await light_preset._deprecated_save_preset(preset)
|
2023-04-01 14:15:58 +00:00
|
|
|
query_helper.assert_called_with(dev.LIGHT_SERVICE, "set_preferred_state", payload)
|
2024-01-29 19:26:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
LIGHT_STATE_SCHEMA = Schema(
|
|
|
|
{
|
|
|
|
"brightness": All(int, Range(min=0, max=100)),
|
|
|
|
"color_temp": int,
|
|
|
|
"hue": All(int, Range(min=0, max=360)),
|
|
|
|
"mode": str,
|
|
|
|
"on_off": Boolean,
|
|
|
|
"saturation": All(int, Range(min=0, max=100)),
|
|
|
|
"dft_on_state": Optional(
|
|
|
|
{
|
|
|
|
"brightness": All(int, Range(min=0, max=100)),
|
|
|
|
"color_temp": All(int, Range(min=0, max=9000)),
|
|
|
|
"hue": All(int, Range(min=0, max=360)),
|
|
|
|
"mode": str,
|
|
|
|
"saturation": All(int, Range(min=0, max=100)),
|
|
|
|
}
|
|
|
|
),
|
|
|
|
"err_code": int,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
SYSINFO_SCHEMA_BULB = SYSINFO_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
"ctrl_protocols": Optional(dict),
|
|
|
|
"description": Optional(str), # Seen on LBxxx, similar to dev_name
|
|
|
|
"dev_state": str,
|
|
|
|
"disco_ver": str,
|
|
|
|
"heapsize": int,
|
|
|
|
"is_color": Boolean,
|
|
|
|
"is_dimmable": Boolean,
|
|
|
|
"is_factory": Boolean,
|
|
|
|
"is_variable_color_temp": Boolean,
|
|
|
|
"light_state": LIGHT_STATE_SCHEMA,
|
|
|
|
"preferred_state": [
|
|
|
|
{
|
|
|
|
"brightness": All(int, Range(min=0, max=100)),
|
|
|
|
"color_temp": int,
|
|
|
|
"hue": All(int, Range(min=0, max=360)),
|
|
|
|
"index": int,
|
|
|
|
"saturation": All(int, Range(min=0, max=100)),
|
|
|
|
}
|
|
|
|
],
|
|
|
|
}
|
|
|
|
)
|
2024-03-15 15:55:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
@bulb
|
2024-05-13 16:34:44 +00:00
|
|
|
def test_device_type_bulb(dev: Device):
|
2024-11-13 16:10:06 +00:00
|
|
|
assert dev.device_type in {DeviceType.Bulb, DeviceType.LightStrip}
|