|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from datetime import datetime |
| 4 | + |
| 5 | +import pytest |
| 6 | +from pytest_mock import MockerFixture |
| 7 | + |
| 8 | +from kasa import Module |
| 9 | +from kasa.smart import SmartDevice |
| 10 | +from kasa.tests.device_fixtures import parametrize |
| 11 | + |
| 12 | +autooff = parametrize( |
| 13 | + "has autooff", component_filter="auto_off", protocol_filter={"SMART"} |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +@autooff |
| 18 | +@pytest.mark.parametrize( |
| 19 | + "feature, prop_name, type", |
| 20 | + [ |
| 21 | + ("auto_off_enabled", "enabled", bool), |
| 22 | + ("auto_off_minutes", "delay", int), |
| 23 | + ("auto_off_at", "auto_off_at", datetime | None), |
| 24 | + ], |
| 25 | +) |
| 26 | +async def test_autooff_features( |
| 27 | + dev: SmartDevice, feature: str, prop_name: str, type: type |
| 28 | +): |
| 29 | + """Test that features are registered and work as expected.""" |
| 30 | + autooff = dev.modules.get(Module.AutoOff) |
| 31 | + assert autooff is not None |
| 32 | + |
| 33 | + prop = getattr(autooff, prop_name) |
| 34 | + assert isinstance(prop, type) |
| 35 | + |
| 36 | + feat = dev.features[feature] |
| 37 | + assert feat.value == prop |
| 38 | + assert isinstance(feat.value, type) |
| 39 | + |
| 40 | + |
| 41 | +@autooff |
| 42 | +async def test_settings(dev: SmartDevice, mocker: MockerFixture): |
| 43 | + """Test autooff settings.""" |
| 44 | + autooff = dev.modules.get(Module.AutoOff) |
| 45 | + assert autooff |
| 46 | + |
| 47 | + enabled = dev.features["auto_off_enabled"] |
| 48 | + assert autooff.enabled == enabled.value |
| 49 | + |
| 50 | + delay = dev.features["auto_off_minutes"] |
| 51 | + assert autooff.delay == delay.value |
| 52 | + |
| 53 | + call = mocker.spy(autooff, "call") |
| 54 | + new_state = True |
| 55 | + |
| 56 | + await autooff.set_enabled(new_state) |
| 57 | + call.assert_called_with( |
| 58 | + "set_auto_off_config", {"enable": new_state, "delay_min": delay.value} |
| 59 | + ) |
| 60 | + call.reset_mock() |
| 61 | + await dev.update() |
| 62 | + |
| 63 | + new_delay = 123 |
| 64 | + |
| 65 | + await autooff.set_delay(new_delay) |
| 66 | + |
| 67 | + call.assert_called_with( |
| 68 | + "set_auto_off_config", {"enable": new_state, "delay_min": new_delay} |
| 69 | + ) |
| 70 | + |
| 71 | + await dev.update() |
| 72 | + |
| 73 | + assert autooff.enabled == new_state |
| 74 | + assert autooff.delay == new_delay |
| 75 | + |
| 76 | + |
| 77 | +@autooff |
| 78 | +@pytest.mark.parametrize("is_timer_active", [True, False]) |
| 79 | +# @pytest.skip("skip") |
| 80 | +async def test_auto_off_at( |
| 81 | + dev: SmartDevice, mocker: MockerFixture, is_timer_active: bool |
| 82 | +): |
| 83 | + """Test auto-off at sensor.""" |
| 84 | + autooff = dev.modules.get(Module.AutoOff) |
| 85 | + assert autooff |
| 86 | + |
| 87 | + autooff_at = dev.features["auto_off_at"] |
| 88 | + |
| 89 | + mocker.patch.object( |
| 90 | + type(autooff), |
| 91 | + "is_timer_active", |
| 92 | + new_callable=mocker.PropertyMock, |
| 93 | + return_value=is_timer_active, |
| 94 | + ) |
| 95 | + if is_timer_active: |
| 96 | + assert isinstance(autooff_at.value, datetime) |
| 97 | + else: |
| 98 | + assert autooff_at.value is None |
0 commit comments