Add Fan interface for SMART devices (#873)

Enables the Fan interface for devices supporting that component.
Currently the only device with a fan is the ks240 which implements it as
a child device. This PR adds a method `get_module` to search the child
device for modules if it is a WallSwitch device type.
This commit is contained in:
Steven B
2024-04-30 17:42:53 +01:00
committed by GitHub
parent 7db989e2ec
commit 16f17a7729
6 changed files with 124 additions and 18 deletions

View File

@@ -10,7 +10,7 @@ brightness = parametrize("brightness smart", component_filter="brightness")
@brightness
async def test_brightness_component(dev: SmartDevice):
"""Test brightness feature."""
brightness = dev.modules.get("Brightness")
brightness = dev.get_module("Brightness")
assert brightness
assert isinstance(dev, SmartDevice)
assert "brightness" in dev._components

View File

@@ -1,8 +1,9 @@
from typing import cast
import pytest
from pytest_mock import MockerFixture
from kasa import SmartDevice
from kasa.smart import SmartDevice
from kasa.smart.modules import FanModule
from kasa.tests.device_fixtures import parametrize
@@ -12,7 +13,7 @@ fan = parametrize("has fan", component_filter="fan_control", protocol_filter={"S
@fan
async def test_fan_speed(dev: SmartDevice, mocker: MockerFixture):
"""Test fan speed feature."""
fan = cast(FanModule, dev.modules.get("FanModule"))
fan = cast(FanModule, dev.get_module("FanModule"))
assert fan
level_feature = fan._module_features["fan_speed_level"]
@@ -24,7 +25,9 @@ async def test_fan_speed(dev: SmartDevice, mocker: MockerFixture):
call = mocker.spy(fan, "call")
await fan.set_fan_speed_level(3)
call.assert_called_with("set_device_info", {"fan_speed_level": 3})
call.assert_called_with(
"set_device_info", {"device_on": True, "fan_speed_level": 3}
)
await dev.update()
@@ -35,7 +38,7 @@ async def test_fan_speed(dev: SmartDevice, mocker: MockerFixture):
@fan
async def test_sleep_mode(dev: SmartDevice, mocker: MockerFixture):
"""Test sleep mode feature."""
fan = cast(FanModule, dev.modules.get("FanModule"))
fan = cast(FanModule, dev.get_module("FanModule"))
assert fan
sleep_feature = fan._module_features["fan_sleep_mode"]
assert isinstance(sleep_feature.value, bool)
@@ -48,3 +51,31 @@ async def test_sleep_mode(dev: SmartDevice, mocker: MockerFixture):
assert fan.sleep_mode is True
assert sleep_feature.value is True
@fan
async def test_fan_interface(dev: SmartDevice, mocker: MockerFixture):
"""Test fan speed on device interface."""
assert isinstance(dev, SmartDevice)
fan = cast(FanModule, dev.get_module("FanModule"))
device = fan._device
assert device.is_fan
await device.set_fan_speed_level(1)
await dev.update()
assert device.fan_speed_level == 1
assert device.is_on
await device.set_fan_speed_level(4)
await dev.update()
assert device.fan_speed_level == 4
await device.set_fan_speed_level(0)
await dev.update()
assert not device.is_on
with pytest.raises(ValueError):
await device.set_fan_speed_level(-1)
with pytest.raises(ValueError):
await device.set_fan_speed_level(5)