python-kasa/tests/smart/modules/test_powerprotection.py

77 lines
2.1 KiB
Python
Raw Normal View History

2024-12-05 16:52:48 +00:00
import pytest
from pytest_mock import MockerFixture
from kasa import Module, SmartDevice
from kasa.smart.modules import PowerProtection
from ...device_fixtures import parametrize
powerprotection = parametrize(
"has powerprotection",
component_filter="power_protection",
protocol_filter={"SMART"},
)
def _skip_on_unavailable(dev: SmartDevice):
if Module.PowerProtection not in dev.modules:
pytest.skip(f"No powerprotection module on {dev}, maybe a strip parent?")
@powerprotection
@pytest.mark.parametrize(
("feature", "prop_name", "type"),
[
2024-12-05 16:59:50 +00:00
("overloaded", "overloaded", bool),
2024-12-05 16:52:48 +00:00
("power_protection_enabled", "enabled", bool),
("power_protection_threshold", "protection_threshold", int),
],
)
async def test_features(dev, feature, prop_name, type):
"""Test that features are registered and work as expected."""
_skip_on_unavailable(dev)
powerprot: PowerProtection = dev.modules[Module.PowerProtection]
prop = getattr(powerprot, prop_name)
assert isinstance(prop, type)
feat = dev.features[feature]
assert feat.value == prop
assert isinstance(feat.value, type)
@powerprotection
async def test_set_enable(dev: SmartDevice, mocker: MockerFixture):
"""Test enable."""
_skip_on_unavailable(dev)
powerprot: PowerProtection = dev.modules[Module.PowerProtection]
call_spy = mocker.spy(powerprot, "call")
await powerprot.set_enabled(True)
params = {
"enabled": mocker.ANY,
"protection_power": mocker.ANY,
}
call_spy.assert_called_with("set_protection_power", params)
@powerprotection
async def test_set_threshold(dev: SmartDevice, mocker: MockerFixture):
"""Test enable."""
_skip_on_unavailable(dev)
powerprot: PowerProtection = dev.modules[Module.PowerProtection]
call_spy = mocker.spy(powerprot, "call")
await powerprot.set_protection_threshold(123)
params = {
"enabled": mocker.ANY,
"protection_power": 123,
}
call_spy.assert_called_with("set_protection_power", params)
with pytest.raises(ValueError, match="Threshold out of range"):
await powerprot.set_protection_threshold(-10)