2020-06-14 16:09:28 +00:00
|
|
|
import pytest
|
|
|
|
|
2024-11-13 16:10:06 +00:00
|
|
|
from kasa import DeviceType, Module
|
2024-02-04 15:20:08 +00:00
|
|
|
from kasa.iot import IotDimmer
|
2020-06-14 16:09:28 +00:00
|
|
|
|
2024-05-13 16:34:44 +00:00
|
|
|
from .conftest import dimmer_iot, handle_turn_on, turn_on
|
2020-06-14 16:09:28 +00:00
|
|
|
|
|
|
|
|
2024-05-13 16:34:44 +00:00
|
|
|
@dimmer_iot
|
2024-05-22 13:33:55 +00:00
|
|
|
async def test_set_brightness(dev):
|
2024-11-13 16:10:06 +00:00
|
|
|
light = dev.modules.get(Module.Light)
|
|
|
|
assert light
|
2024-05-22 13:33:55 +00:00
|
|
|
await handle_turn_on(dev, False)
|
2024-11-11 09:31:13 +00:00
|
|
|
await dev.update()
|
2024-05-22 13:33:55 +00:00
|
|
|
assert dev.is_on is False
|
2020-06-14 16:09:28 +00:00
|
|
|
|
2024-11-13 16:10:06 +00:00
|
|
|
await light.set_brightness(99)
|
2024-04-24 11:25:16 +00:00
|
|
|
await dev.update()
|
2024-11-13 16:10:06 +00:00
|
|
|
assert light.brightness == 99
|
2024-05-22 13:33:55 +00:00
|
|
|
assert dev.is_on is True
|
2020-06-14 16:09:28 +00:00
|
|
|
|
2024-11-13 16:10:06 +00:00
|
|
|
await light.set_brightness(0)
|
2024-04-24 11:25:16 +00:00
|
|
|
await dev.update()
|
2024-11-13 16:10:06 +00:00
|
|
|
assert light.brightness == 99
|
2024-05-22 13:33:55 +00:00
|
|
|
assert dev.is_on is False
|
2020-06-14 16:09:28 +00:00
|
|
|
|
|
|
|
|
2024-05-13 16:34:44 +00:00
|
|
|
@dimmer_iot
|
2020-06-14 16:09:28 +00:00
|
|
|
@turn_on
|
|
|
|
async def test_set_brightness_transition(dev, turn_on, mocker):
|
2024-11-13 16:10:06 +00:00
|
|
|
light = dev.modules.get(Module.Light)
|
|
|
|
assert light
|
2020-06-14 16:09:28 +00:00
|
|
|
await handle_turn_on(dev, turn_on)
|
2024-02-04 15:20:08 +00:00
|
|
|
query_helper = mocker.spy(IotDimmer, "_query_helper")
|
2020-06-14 16:09:28 +00:00
|
|
|
|
2024-11-13 16:10:06 +00:00
|
|
|
await light.set_brightness(99, transition=1000)
|
2020-06-14 16:09:28 +00:00
|
|
|
query_helper.assert_called_with(
|
|
|
|
mocker.ANY,
|
|
|
|
"smartlife.iot.dimmer",
|
|
|
|
"set_dimmer_transition",
|
|
|
|
{"brightness": 99, "duration": 1000},
|
|
|
|
)
|
2024-04-24 11:25:16 +00:00
|
|
|
await dev.update()
|
2024-11-13 16:10:06 +00:00
|
|
|
assert light.brightness == 99
|
2024-04-24 11:25:16 +00:00
|
|
|
assert dev.is_on
|
2020-06-14 16:09:28 +00:00
|
|
|
|
2024-11-13 16:10:06 +00:00
|
|
|
await light.set_brightness(0, transition=1000)
|
2024-04-24 11:25:16 +00:00
|
|
|
await dev.update()
|
2024-05-22 13:33:55 +00:00
|
|
|
assert dev.is_on is False
|
2020-06-14 16:09:28 +00:00
|
|
|
|
|
|
|
|
2024-05-13 16:34:44 +00:00
|
|
|
@dimmer_iot
|
2020-06-14 16:09:28 +00:00
|
|
|
async def test_set_brightness_invalid(dev):
|
2024-11-13 16:10:06 +00:00
|
|
|
light = dev.modules.get(Module.Light)
|
|
|
|
assert light
|
2024-08-30 15:30:07 +00:00
|
|
|
for invalid_brightness in [-1, 101]:
|
|
|
|
with pytest.raises(ValueError, match="Invalid brightness"):
|
2024-11-13 16:10:06 +00:00
|
|
|
await light.set_brightness(invalid_brightness)
|
2020-06-14 16:09:28 +00:00
|
|
|
|
2024-08-30 15:30:07 +00:00
|
|
|
for invalid_type in [0.5, "foo"]:
|
|
|
|
with pytest.raises(TypeError, match="Brightness must be an integer"):
|
2024-11-13 16:10:06 +00:00
|
|
|
await light.set_brightness(invalid_type)
|
2024-08-30 15:30:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
@dimmer_iot
|
|
|
|
async def test_set_brightness_invalid_transition(dev):
|
2024-11-13 16:10:06 +00:00
|
|
|
light = dev.modules.get(Module.Light)
|
|
|
|
assert light
|
2024-08-30 15:30:07 +00:00
|
|
|
for invalid_transition in [-1]:
|
|
|
|
with pytest.raises(ValueError, match="Transition value .+? is not valid."):
|
2024-11-13 16:10:06 +00:00
|
|
|
await light.set_brightness(1, transition=invalid_transition)
|
2024-08-30 15:30:07 +00:00
|
|
|
for invalid_type in [0.5, "foo"]:
|
|
|
|
with pytest.raises(TypeError, match="Transition must be integer"):
|
2024-11-13 16:10:06 +00:00
|
|
|
await light.set_brightness(1, transition=invalid_type)
|
2020-06-14 16:09:28 +00:00
|
|
|
|
|
|
|
|
2024-05-13 16:34:44 +00:00
|
|
|
@dimmer_iot
|
2020-06-14 16:09:28 +00:00
|
|
|
async def test_turn_on_transition(dev, mocker):
|
2024-11-13 16:10:06 +00:00
|
|
|
light = dev.modules.get(Module.Light)
|
|
|
|
assert light
|
2024-02-04 15:20:08 +00:00
|
|
|
query_helper = mocker.spy(IotDimmer, "_query_helper")
|
2024-11-13 16:10:06 +00:00
|
|
|
original_brightness = light.brightness
|
2020-06-14 16:09:28 +00:00
|
|
|
|
|
|
|
await dev.turn_on(transition=1000)
|
|
|
|
query_helper.assert_called_with(
|
|
|
|
mocker.ANY,
|
|
|
|
"smartlife.iot.dimmer",
|
|
|
|
"set_dimmer_transition",
|
|
|
|
{"brightness": original_brightness, "duration": 1000},
|
|
|
|
)
|
2024-04-24 11:25:16 +00:00
|
|
|
await dev.update()
|
|
|
|
assert dev.is_on
|
2024-11-13 16:10:06 +00:00
|
|
|
assert light.brightness == original_brightness
|
2020-06-14 16:09:28 +00:00
|
|
|
|
|
|
|
|
2024-05-13 16:34:44 +00:00
|
|
|
@dimmer_iot
|
2020-06-14 16:09:28 +00:00
|
|
|
async def test_turn_off_transition(dev, mocker):
|
2024-11-13 16:10:06 +00:00
|
|
|
light = dev.modules.get(Module.Light)
|
|
|
|
assert light
|
2020-06-14 16:09:28 +00:00
|
|
|
await handle_turn_on(dev, True)
|
2024-02-04 15:20:08 +00:00
|
|
|
query_helper = mocker.spy(IotDimmer, "_query_helper")
|
2024-11-13 16:10:06 +00:00
|
|
|
original_brightness = light.brightness
|
2020-06-14 16:09:28 +00:00
|
|
|
|
|
|
|
await dev.turn_off(transition=1000)
|
2024-11-11 09:31:13 +00:00
|
|
|
await dev.update()
|
2020-06-14 16:09:28 +00:00
|
|
|
|
|
|
|
assert dev.is_off
|
2024-11-13 16:10:06 +00:00
|
|
|
assert light.brightness == original_brightness
|
2020-06-14 16:09:28 +00:00
|
|
|
query_helper.assert_called_with(
|
|
|
|
mocker.ANY,
|
|
|
|
"smartlife.iot.dimmer",
|
|
|
|
"set_dimmer_transition",
|
|
|
|
{"brightness": 0, "duration": 1000},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-05-13 16:34:44 +00:00
|
|
|
@dimmer_iot
|
2020-06-14 16:09:28 +00:00
|
|
|
@turn_on
|
|
|
|
async def test_set_dimmer_transition(dev, turn_on, mocker):
|
2024-11-13 16:10:06 +00:00
|
|
|
light = dev.modules.get(Module.Light)
|
|
|
|
assert light
|
2020-06-14 16:09:28 +00:00
|
|
|
await handle_turn_on(dev, turn_on)
|
2024-02-04 15:20:08 +00:00
|
|
|
query_helper = mocker.spy(IotDimmer, "_query_helper")
|
2020-06-14 16:09:28 +00:00
|
|
|
|
|
|
|
await dev.set_dimmer_transition(99, 1000)
|
|
|
|
query_helper.assert_called_with(
|
|
|
|
mocker.ANY,
|
|
|
|
"smartlife.iot.dimmer",
|
|
|
|
"set_dimmer_transition",
|
|
|
|
{"brightness": 99, "duration": 1000},
|
|
|
|
)
|
2024-04-24 11:25:16 +00:00
|
|
|
await dev.update()
|
|
|
|
assert dev.is_on
|
2024-11-13 16:10:06 +00:00
|
|
|
assert light.brightness == 99
|
2020-06-14 16:09:28 +00:00
|
|
|
|
|
|
|
|
2024-05-13 16:34:44 +00:00
|
|
|
@dimmer_iot
|
2020-06-14 16:09:28 +00:00
|
|
|
@turn_on
|
|
|
|
async def test_set_dimmer_transition_to_off(dev, turn_on, mocker):
|
2024-11-13 16:10:06 +00:00
|
|
|
light = dev.modules.get(Module.Light)
|
|
|
|
assert light
|
2020-06-14 16:09:28 +00:00
|
|
|
await handle_turn_on(dev, turn_on)
|
2024-11-13 16:10:06 +00:00
|
|
|
original_brightness = light.brightness
|
2024-02-04 15:20:08 +00:00
|
|
|
query_helper = mocker.spy(IotDimmer, "_query_helper")
|
2020-06-14 16:09:28 +00:00
|
|
|
|
|
|
|
await dev.set_dimmer_transition(0, 1000)
|
2024-11-11 09:31:13 +00:00
|
|
|
await dev.update()
|
2020-06-14 16:09:28 +00:00
|
|
|
|
|
|
|
assert dev.is_off
|
2024-11-13 16:10:06 +00:00
|
|
|
assert light.brightness == original_brightness
|
2020-06-14 16:09:28 +00:00
|
|
|
query_helper.assert_called_with(
|
|
|
|
mocker.ANY,
|
|
|
|
"smartlife.iot.dimmer",
|
|
|
|
"set_dimmer_transition",
|
|
|
|
{"brightness": 0, "duration": 1000},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-05-13 16:34:44 +00:00
|
|
|
@dimmer_iot
|
2024-08-30 15:30:07 +00:00
|
|
|
async def test_set_dimmer_transition_invalid_brightness(dev):
|
|
|
|
for invalid_brightness in [-1, 101]:
|
|
|
|
with pytest.raises(ValueError, match="Invalid brightness value: "):
|
2020-06-14 16:09:28 +00:00
|
|
|
await dev.set_dimmer_transition(invalid_brightness, 1000)
|
|
|
|
|
2024-08-30 15:30:07 +00:00
|
|
|
for invalid_type in [0.5, "foo"]:
|
|
|
|
with pytest.raises(TypeError, match="Transition must be integer"):
|
|
|
|
await dev.set_dimmer_transition(1, invalid_type)
|
|
|
|
|
|
|
|
|
|
|
|
@dimmer_iot
|
|
|
|
async def test_set_dimmer_transition_invalid_transition(dev):
|
|
|
|
for invalid_transition in [-1]:
|
|
|
|
with pytest.raises(ValueError, match="Transition value .+? is not valid."):
|
|
|
|
await dev.set_dimmer_transition(1, transition=invalid_transition)
|
|
|
|
for invalid_type in [0.5, "foo"]:
|
|
|
|
with pytest.raises(TypeError, match="Transition must be integer"):
|
|
|
|
await dev.set_dimmer_transition(1, transition=invalid_type)
|
2024-03-15 15:55:48 +00:00
|
|
|
|
|
|
|
|
2024-05-13 16:34:44 +00:00
|
|
|
@dimmer_iot
|
2024-03-15 15:55:48 +00:00
|
|
|
def test_device_type_dimmer(dev):
|
|
|
|
assert dev.device_type == DeviceType.Dimmer
|