Make Light and Fan a common module interface (#911)

This commit is contained in:
Steven B
2024-05-13 17:34:44 +01:00
committed by GitHub
parent d7b00336f4
commit 33d839866e
32 changed files with 544 additions and 342 deletions

View File

@@ -203,14 +203,14 @@ wallswitch_iot = parametrize(
"wall switches iot", model_filter=SWITCHES, protocol_filter={"IOT"}
)
strip = parametrize("strips", model_filter=STRIPS, protocol_filter={"SMART", "IOT"})
dimmer = parametrize("dimmers", model_filter=DIMMERS, protocol_filter={"IOT"})
lightstrip = parametrize(
dimmer_iot = parametrize("dimmers", model_filter=DIMMERS, protocol_filter={"IOT"})
lightstrip_iot = parametrize(
"lightstrips", model_filter=LIGHT_STRIPS, protocol_filter={"IOT"}
)
# bulb types
dimmable = parametrize("dimmable", model_filter=DIMMABLE, protocol_filter={"IOT"})
non_dimmable = parametrize(
dimmable_iot = parametrize("dimmable", model_filter=DIMMABLE, protocol_filter={"IOT"})
non_dimmable_iot = parametrize(
"non-dimmable", model_filter=BULBS - DIMMABLE, protocol_filter={"IOT"}
)
variable_temp = parametrize(
@@ -292,12 +292,12 @@ device_iot = parametrize(
def check_categories():
"""Check that every fixture file is categorized."""
categorized_fixtures = set(
dimmer.args[1]
dimmer_iot.args[1]
+ strip.args[1]
+ plug.args[1]
+ bulb.args[1]
+ wallswitch.args[1]
+ lightstrip.args[1]
+ lightstrip_iot.args[1]
+ bulb_smart.args[1]
+ dimmers_smart.args[1]
+ hubs_smart.args[1]

View File

@@ -2,7 +2,7 @@ import pytest
from kasa.iot import IotDevice
from kasa.smart import SmartDevice
from kasa.tests.conftest import dimmable, parametrize
from kasa.tests.conftest import dimmable_iot, parametrize
brightness = parametrize("brightness smart", component_filter="brightness")
@@ -16,7 +16,7 @@ async def test_brightness_component(dev: SmartDevice):
assert "brightness" in dev._components
# Test getting the value
feature = brightness._module_features["brightness"]
feature = dev.features["brightness"]
assert isinstance(feature.value, int)
assert feature.value > 1 and feature.value <= 100
@@ -32,7 +32,7 @@ async def test_brightness_component(dev: SmartDevice):
await feature.set_value(feature.maximum_value + 10)
@dimmable
@dimmable_iot
async def test_brightness_dimmable(dev: IotDevice):
"""Test brightness feature."""
assert isinstance(dev, IotDevice)

View File

@@ -23,6 +23,6 @@ async def test_contact_features(dev: SmartDevice, feature, type):
prop = getattr(contact, feature)
assert isinstance(prop, type)
feat = contact._module_features[feature]
feat = dev.features[feature]
assert feat.value == prop
assert isinstance(feat.value, type)

View File

@@ -14,7 +14,7 @@ async def test_fan_speed(dev: SmartDevice, mocker: MockerFixture):
fan = dev.modules.get(Module.Fan)
assert fan
level_feature = fan._module_features["fan_speed_level"]
level_feature = dev.features["fan_speed_level"]
assert (
level_feature.minimum_value
<= level_feature.value
@@ -38,7 +38,7 @@ async def test_sleep_mode(dev: SmartDevice, mocker: MockerFixture):
"""Test sleep mode feature."""
fan = dev.modules.get(Module.Fan)
assert fan
sleep_feature = fan._module_features["fan_sleep_mode"]
sleep_feature = dev.features["fan_sleep_mode"]
assert isinstance(sleep_feature.value, bool)
call = mocker.spy(fan, "call")
@@ -52,7 +52,7 @@ async def test_sleep_mode(dev: SmartDevice, mocker: MockerFixture):
@fan
async def test_fan_interface(dev: SmartDevice, mocker: MockerFixture):
async def test_fan_module(dev: SmartDevice, mocker: MockerFixture):
"""Test fan speed on device interface."""
assert isinstance(dev, SmartDevice)
fan = dev.modules.get(Module.Fan)
@@ -60,21 +60,21 @@ async def test_fan_interface(dev: SmartDevice, mocker: MockerFixture):
device = fan._device
assert device.is_fan
await device.set_fan_speed_level(1)
await fan.set_fan_speed_level(1)
await dev.update()
assert device.fan_speed_level == 1
assert fan.fan_speed_level == 1
assert device.is_on
await device.set_fan_speed_level(4)
await fan.set_fan_speed_level(4)
await dev.update()
assert device.fan_speed_level == 4
assert fan.fan_speed_level == 4
await device.set_fan_speed_level(0)
await fan.set_fan_speed_level(0)
await dev.update()
assert not device.is_on
with pytest.raises(ValueError):
await device.set_fan_speed_level(-1)
await fan.set_fan_speed_level(-1)
with pytest.raises(ValueError):
await device.set_fan_speed_level(5)
await fan.set_fan_speed_level(5)

View File

@@ -43,7 +43,7 @@ async def test_firmware_features(
prop = getattr(fw, prop_name)
assert isinstance(prop, type)
feat = fw._module_features[feature]
feat = dev.features[feature]
assert feat.value == prop
assert isinstance(feat.value, type)

View File

@@ -23,6 +23,6 @@ async def test_humidity_features(dev, feature, type):
prop = getattr(humidity, feature)
assert isinstance(prop, type)
feat = humidity._module_features[feature]
feat = dev.features[feature]
assert feat.value == prop
assert isinstance(feat.value, type)

View File

@@ -20,7 +20,7 @@ async def test_light_effect(dev: Device, mocker: MockerFixture):
light_effect = dev.modules.get(Module.LightEffect)
assert isinstance(light_effect, LightEffect)
feature = light_effect._module_features["light_effect"]
feature = dev.features["light_effect"]
assert feature.type == Feature.Type.Choice
call = mocker.spy(light_effect, "call")

View File

@@ -29,7 +29,7 @@ async def test_temperature_features(dev, feature, type):
prop = getattr(temp_module, feature)
assert isinstance(prop, type)
feat = temp_module._module_features[feature]
feat = dev.features[feature]
assert feat.value == prop
assert isinstance(feat.value, type)
@@ -42,6 +42,6 @@ async def test_temperature_warning(dev):
assert hasattr(temp_module, "temperature_warning")
assert isinstance(temp_module.temperature_warning, bool)
feat = temp_module._module_features["temperature_warning"]
feat = dev.features["temperature_warning"]
assert feat.value == temp_module.temperature_warning
assert isinstance(feat.value, bool)

View File

@@ -28,7 +28,7 @@ async def test_temperature_control_features(dev, feature, type):
prop = getattr(temp_module, feature)
assert isinstance(prop, type)
feat = temp_module._module_features[feature]
feat = dev.features[feature]
assert feat.value == prop
assert isinstance(feat.value, type)

View File

@@ -25,7 +25,7 @@ async def test_waterleak_properties(dev, feature, prop_name, type):
prop = getattr(waterleak, prop_name)
assert isinstance(prop, type)
feat = waterleak._module_features[feature]
feat = dev.features[feature]
assert feat.value == prop
assert isinstance(feat.value, type)

View File

@@ -7,19 +7,18 @@ from voluptuous import (
Schema,
)
from kasa import Device, DeviceType, KasaException, Light, LightPreset
from kasa import Device, DeviceType, KasaException, LightPreset, Module
from kasa.iot import IotBulb, IotDimmer
from kasa.smart import SmartDevice
from .conftest import (
bulb,
bulb_iot,
color_bulb,
color_bulb_iot,
dimmable,
dimmable_iot,
handle_turn_on,
non_color_bulb,
non_dimmable,
non_dimmable_iot,
non_variable_temp,
turn_on,
variable_temp,
@@ -65,19 +64,20 @@ async def test_get_light_state(dev: IotBulb):
@color_bulb
@turn_on
async def test_hsv(dev: Device, turn_on):
assert isinstance(dev, Light)
light = dev.modules.get(Module.Light)
assert light
await handle_turn_on(dev, turn_on)
assert dev.is_color
assert light.is_color
hue, saturation, brightness = dev.hsv
hue, saturation, brightness = light.hsv
assert 0 <= hue <= 360
assert 0 <= saturation <= 100
assert 0 <= brightness <= 100
await dev.set_hsv(hue=1, saturation=1, value=1)
await light.set_hsv(hue=1, saturation=1, value=1)
await dev.update()
hue, saturation, brightness = dev.hsv
hue, saturation, brightness = light.hsv
assert hue == 1
assert saturation == 1
assert brightness == 1
@@ -96,57 +96,64 @@ async def test_set_hsv_transition(dev: IotBulb, mocker):
@color_bulb
@turn_on
async def test_invalid_hsv(dev: Light, turn_on):
async def test_invalid_hsv(dev: Device, turn_on):
light = dev.modules.get(Module.Light)
assert light
await handle_turn_on(dev, turn_on)
assert dev.is_color
assert light.is_color
for invalid_hue in [-1, 361, 0.5]:
with pytest.raises(ValueError):
await dev.set_hsv(invalid_hue, 0, 0) # type: ignore[arg-type]
await light.set_hsv(invalid_hue, 0, 0) # type: ignore[arg-type]
for invalid_saturation in [-1, 101, 0.5]:
with pytest.raises(ValueError):
await dev.set_hsv(0, invalid_saturation, 0) # type: ignore[arg-type]
await light.set_hsv(0, invalid_saturation, 0) # type: ignore[arg-type]
for invalid_brightness in [-1, 101, 0.5]:
with pytest.raises(ValueError):
await dev.set_hsv(0, 0, invalid_brightness) # type: ignore[arg-type]
await light.set_hsv(0, 0, invalid_brightness) # type: ignore[arg-type]
@color_bulb
@pytest.mark.skip("requires color feature")
async def test_color_state_information(dev: Device):
assert isinstance(dev, Light)
light = dev.modules.get(Module.Light)
assert light
assert "HSV" in dev.state_information
assert dev.state_information["HSV"] == dev.hsv
assert dev.state_information["HSV"] == light.hsv
@non_color_bulb
async def test_hsv_on_non_color(dev: Light):
assert not dev.is_color
async def test_hsv_on_non_color(dev: Device):
light = dev.modules.get(Module.Light)
assert light
assert not light.is_color
with pytest.raises(KasaException):
await dev.set_hsv(0, 0, 0)
await light.set_hsv(0, 0, 0)
with pytest.raises(KasaException):
print(dev.hsv)
print(light.hsv)
@variable_temp
@pytest.mark.skip("requires colortemp module")
async def test_variable_temp_state_information(dev: Device):
assert isinstance(dev, Light)
light = dev.modules.get(Module.Light)
assert light
assert "Color temperature" in dev.state_information
assert dev.state_information["Color temperature"] == dev.color_temp
assert dev.state_information["Color temperature"] == light.color_temp
@variable_temp
@turn_on
async def test_try_set_colortemp(dev: Device, turn_on):
assert isinstance(dev, Light)
light = dev.modules.get(Module.Light)
assert light
await handle_turn_on(dev, turn_on)
await dev.set_color_temp(2700)
await light.set_color_temp(2700)
await dev.update()
assert dev.color_temp == 2700
assert light.color_temp == 2700
@variable_temp_iot
@@ -166,34 +173,40 @@ async def test_unknown_temp_range(dev: IotBulb, monkeypatch, caplog):
@variable_temp_smart
async def test_smart_temp_range(dev: SmartDevice):
assert dev.valid_temperature_range
async def test_smart_temp_range(dev: Device):
light = dev.modules.get(Module.Light)
assert light
assert light.valid_temperature_range
@variable_temp
async def test_out_of_range_temperature(dev: Light):
async def test_out_of_range_temperature(dev: Device):
light = dev.modules.get(Module.Light)
assert light
with pytest.raises(ValueError):
await dev.set_color_temp(1000)
await light.set_color_temp(1000)
with pytest.raises(ValueError):
await dev.set_color_temp(10000)
await light.set_color_temp(10000)
@non_variable_temp
async def test_non_variable_temp(dev: Light):
async def test_non_variable_temp(dev: Device):
light = dev.modules.get(Module.Light)
assert light
with pytest.raises(KasaException):
await dev.set_color_temp(2700)
await light.set_color_temp(2700)
with pytest.raises(KasaException):
print(dev.valid_temperature_range)
print(light.valid_temperature_range)
with pytest.raises(KasaException):
print(dev.color_temp)
print(light.color_temp)
@dimmable
@dimmable_iot
@turn_on
async def test_dimmable_brightness(dev: Device, turn_on):
assert isinstance(dev, (Light, IotDimmer))
async def test_dimmable_brightness(dev: IotBulb, turn_on):
assert isinstance(dev, (IotBulb, IotDimmer))
await handle_turn_on(dev, turn_on)
assert dev.is_dimmable
@@ -229,8 +242,8 @@ async def test_dimmable_brightness_transition(dev: IotBulb, mocker):
set_light_state.assert_called_with({"brightness": 10}, transition=1000)
@dimmable
async def test_invalid_brightness(dev: Light):
@dimmable_iot
async def test_invalid_brightness(dev: IotBulb):
assert dev.is_dimmable
with pytest.raises(ValueError):
@@ -240,8 +253,8 @@ async def test_invalid_brightness(dev: Light):
await dev.set_brightness(-100)
@non_dimmable
async def test_non_dimmable(dev: Light):
@non_dimmable_iot
async def test_non_dimmable(dev: IotBulb):
assert not dev.is_dimmable
with pytest.raises(KasaException):
@@ -380,7 +393,7 @@ SYSINFO_SCHEMA_BULB = SYSINFO_SCHEMA.extend(
@bulb
def test_device_type_bulb(dev):
def test_device_type_bulb(dev: Device):
if dev.is_light_strip:
pytest.skip("bulb has also lightstrips to test the api")
assert dev.device_type == DeviceType.Bulb

View File

@@ -3,7 +3,9 @@ from pytest_mock import MockerFixture
from kasa import Device, Module
from kasa.tests.device_fixtures import (
lightstrip,
dimmable_iot,
dimmer_iot,
lightstrip_iot,
parametrize,
parametrize_combine,
plug_iot,
@@ -17,7 +19,12 @@ led = parametrize_combine([led_smart, plug_iot])
light_effect_smart = parametrize(
"has light effect smart", component_filter="light_effect", protocol_filter={"SMART"}
)
light_effect = parametrize_combine([light_effect_smart, lightstrip])
light_effect = parametrize_combine([light_effect_smart, lightstrip_iot])
dimmable_smart = parametrize(
"dimmable smart", component_filter="brightness", protocol_filter={"SMART"}
)
dimmable = parametrize_combine([dimmable_smart, dimmer_iot, dimmable_iot])
@led
@@ -25,7 +32,7 @@ async def test_led_module(dev: Device, mocker: MockerFixture):
"""Test fan speed feature."""
led_module = dev.modules.get(Module.Led)
assert led_module
feat = led_module._module_features["led"]
feat = dev.features["led"]
call = mocker.spy(led_module, "call")
await led_module.set_led(True)
@@ -52,7 +59,7 @@ async def test_light_effect_module(dev: Device, mocker: MockerFixture):
"""Test fan speed feature."""
light_effect_module = dev.modules[Module.LightEffect]
assert light_effect_module
feat = light_effect_module._module_features["light_effect"]
feat = dev.features["light_effect"]
call = mocker.spy(light_effect_module, "call")
effect_list = light_effect_module.effect_list
@@ -93,3 +100,26 @@ async def test_light_effect_module(dev: Device, mocker: MockerFixture):
with pytest.raises(ValueError):
await light_effect_module.set_effect("foobar")
assert call.call_count == 4
@dimmable
async def test_light_brightness(dev: Device):
"""Test brightness setter and getter."""
assert isinstance(dev, Device)
light = dev.modules.get(Module.Light)
assert light
# Test getting the value
feature = dev.features["brightness"]
assert feature.minimum_value == 0
assert feature.maximum_value == 100
await light.set_brightness(10)
await dev.update()
assert light.brightness == 10
with pytest.raises(ValueError):
await light.set_brightness(feature.minimum_value - 10)
with pytest.raises(ValueError):
await light.set_brightness(feature.maximum_value + 10)

View File

@@ -3,10 +3,10 @@ import pytest
from kasa import DeviceType
from kasa.iot import IotDimmer
from .conftest import dimmer, handle_turn_on, turn_on
from .conftest import dimmer_iot, handle_turn_on, turn_on
@dimmer
@dimmer_iot
@turn_on
async def test_set_brightness(dev, turn_on):
await handle_turn_on(dev, turn_on)
@@ -22,7 +22,7 @@ async def test_set_brightness(dev, turn_on):
assert dev.is_on == turn_on
@dimmer
@dimmer_iot
@turn_on
async def test_set_brightness_transition(dev, turn_on, mocker):
await handle_turn_on(dev, turn_on)
@@ -44,7 +44,7 @@ async def test_set_brightness_transition(dev, turn_on, mocker):
assert dev.brightness == 1
@dimmer
@dimmer_iot
async def test_set_brightness_invalid(dev):
for invalid_brightness in [-1, 101, 0.5]:
with pytest.raises(ValueError):
@@ -55,7 +55,7 @@ async def test_set_brightness_invalid(dev):
await dev.set_brightness(1, transition=invalid_transition)
@dimmer
@dimmer_iot
async def test_turn_on_transition(dev, mocker):
query_helper = mocker.spy(IotDimmer, "_query_helper")
original_brightness = dev.brightness
@@ -72,7 +72,7 @@ async def test_turn_on_transition(dev, mocker):
assert dev.brightness == original_brightness
@dimmer
@dimmer_iot
async def test_turn_off_transition(dev, mocker):
await handle_turn_on(dev, True)
query_helper = mocker.spy(IotDimmer, "_query_helper")
@@ -90,7 +90,7 @@ async def test_turn_off_transition(dev, mocker):
)
@dimmer
@dimmer_iot
@turn_on
async def test_set_dimmer_transition(dev, turn_on, mocker):
await handle_turn_on(dev, turn_on)
@@ -108,7 +108,7 @@ async def test_set_dimmer_transition(dev, turn_on, mocker):
assert dev.brightness == 99
@dimmer
@dimmer_iot
@turn_on
async def test_set_dimmer_transition_to_off(dev, turn_on, mocker):
await handle_turn_on(dev, turn_on)
@@ -127,7 +127,7 @@ async def test_set_dimmer_transition_to_off(dev, turn_on, mocker):
)
@dimmer
@dimmer_iot
async def test_set_dimmer_transition_invalid(dev):
for invalid_brightness in [-1, 101, 0.5]:
with pytest.raises(ValueError):
@@ -138,6 +138,6 @@ async def test_set_dimmer_transition_invalid(dev):
await dev.set_dimmer_transition(1, invalid_transition)
@dimmer
@dimmer_iot
def test_device_type_dimmer(dev):
assert dev.device_type == DeviceType.Dimmer

View File

@@ -26,8 +26,8 @@ from kasa.xortransport import XorEncryption
from .conftest import (
bulb_iot,
dimmer,
lightstrip,
dimmer_iot,
lightstrip_iot,
new_discovery,
plug_iot,
strip_iot,
@@ -86,14 +86,14 @@ async def test_type_detection_strip(dev: Device):
assert d.device_type == DeviceType.Strip
@dimmer
@dimmer_iot
async def test_type_detection_dimmer(dev: Device):
d = Discover._get_device_class(dev._last_update)("localhost")
assert d.is_dimmer
assert d.device_type == DeviceType.Dimmer
@lightstrip
@lightstrip_iot
async def test_type_detection_lightstrip(dev: Device):
d = Discover._get_device_class(dev._last_update)("localhost")
assert d.is_light_strip

View File

@@ -1,5 +1,6 @@
import logging
import sys
from unittest.mock import patch
import pytest
from pytest_mock import MockerFixture
@@ -180,11 +181,10 @@ async def test_feature_setters(dev: Device, mocker: MockerFixture):
async def _test_features(dev):
exceptions = []
query = mocker.patch.object(dev.protocol, "query")
for feat in dev.features.values():
query.reset_mock()
try:
await _test_feature(feat, query)
with patch.object(feat.device.protocol, "query") as query:
await _test_feature(feat, query)
# we allow our own exceptions to avoid mocking valid responses
except KasaException:
pass

View File

@@ -3,24 +3,24 @@ import pytest
from kasa import DeviceType
from kasa.iot import IotLightStrip
from .conftest import lightstrip
from .conftest import lightstrip_iot
@lightstrip
@lightstrip_iot
async def test_lightstrip_length(dev: IotLightStrip):
assert dev.is_light_strip
assert dev.device_type == DeviceType.LightStrip
assert dev.length == dev.sys_info["length"]
@lightstrip
@lightstrip_iot
async def test_lightstrip_effect(dev: IotLightStrip):
assert isinstance(dev.effect, dict)
for k in ["brightness", "custom", "enable", "id", "name"]:
assert k in dev.effect
@lightstrip
@lightstrip_iot
async def test_effects_lightstrip_set_effect(dev: IotLightStrip):
with pytest.raises(ValueError):
await dev.set_effect("Not real")
@@ -30,7 +30,7 @@ async def test_effects_lightstrip_set_effect(dev: IotLightStrip):
assert dev.effect["name"] == "Candy Cane"
@lightstrip
@lightstrip_iot
@pytest.mark.parametrize("brightness", [100, 50])
async def test_effects_lightstrip_set_effect_brightness(
dev: IotLightStrip, brightness, mocker
@@ -48,7 +48,7 @@ async def test_effects_lightstrip_set_effect_brightness(
assert payload["brightness"] == brightness
@lightstrip
@lightstrip_iot
@pytest.mark.parametrize("transition", [500, 1000])
async def test_effects_lightstrip_set_effect_transition(
dev: IotLightStrip, transition, mocker
@@ -66,12 +66,12 @@ async def test_effects_lightstrip_set_effect_transition(
assert payload["transition"] == transition
@lightstrip
@lightstrip_iot
async def test_effects_lightstrip_has_effects(dev: IotLightStrip):
assert dev.has_effects is True
assert dev.effect_list
@lightstrip
@lightstrip_iot
def test_device_type_lightstrip(dev):
assert dev.device_type == DeviceType.LightStrip

View File

@@ -14,7 +14,6 @@ from kasa.exceptions import SmartErrorCode
from kasa.smart import SmartDevice
from .conftest import (
bulb_smart,
device_smart,
get_device_for_fixture_protocol,
)
@@ -159,28 +158,6 @@ async def test_get_modules():
assert module is None
@bulb_smart
async def test_smartdevice_brightness(dev: SmartDevice):
"""Test brightness setter and getter."""
assert isinstance(dev, SmartDevice)
assert "brightness" in dev._components
# Test getting the value
feature = dev.features["brightness"]
assert feature.minimum_value == 1
assert feature.maximum_value == 100
await dev.set_brightness(10)
await dev.update()
assert dev.brightness == 10
with pytest.raises(ValueError):
await dev.set_brightness(feature.minimum_value - 10)
with pytest.raises(ValueError):
await dev.set_brightness(feature.maximum_value + 10)
@device_smart
async def test_smartdevice_cloud_connection(dev: SmartDevice, mocker: MockerFixture):
"""Test is_cloud_connected property."""