-
-
Notifications
You must be signed in to change notification settings - Fork 228
Allow passing alarm parameter overrides #1340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
0f7e007
Allow passing alarm parameter overrides
rytilahti 8323753
Expose alarm_duration, fix play setter signature
rytilahti ee82c99
Add tests
rytilahti 8a2ed72
De-nest get_alarm_configure in fake protocol
rytilahti 000ae50
Add value checks
rytilahti 2fed0ea
Fix tests
rytilahti 5a7ad6e
One more try
rytilahti f6119e3
update
rytilahti a2b1193
test exceptions
rytilahti 0c58b5e
constify max duration
rytilahti dbd80e6
Add mute volume and allow passing volume as integer
rytilahti a621761
Merge remote-tracking branch 'upstream/master' into feat/improve_alarm
sdb9696 b1f7754
Add range to alarm_volume feature
sdb9696 5bc843c
Merge branch 'master' into feat/improve_alarm
sdb9696 3d1a6c3
Merge branch 'master' into feat/improve_alarm
sdb9696 07a4e43
Merge branch 'master' into feat/improve_alarm
sdb9696 7f5046b
Add alarm_volume_number feature
sdb9696 233940a
Fix tests
sdb9696 5be4375
Rename feature
sdb9696 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
from __future__ import annotations | ||
|
||
import pytest | ||
from pytest_mock import MockerFixture | ||
|
||
from kasa import Module | ||
from kasa.smart import SmartDevice | ||
from kasa.smart.modules import Alarm | ||
|
||
from ...device_fixtures import get_parent_and_child_modules, parametrize | ||
|
||
alarm = parametrize("has alarm", component_filter="alarm", protocol_filter={"SMART"}) | ||
|
||
|
||
@alarm | ||
@pytest.mark.parametrize( | ||
("feature", "prop_name", "type"), | ||
[ | ||
("alarm", "active", bool), | ||
("alarm_source", "source", str | None), | ||
("alarm_sound", "alarm_sound", str), | ||
("alarm_volume", "_alarm_volume_str", str), | ||
("alarm_volume_number", "alarm_volume", int), | ||
], | ||
) | ||
async def test_features(dev: SmartDevice, feature: str, prop_name: str, type: type): | ||
"""Test that featu F438 res are registered and work as expected.""" | ||
alarm = next(get_parent_and_child_modules(dev, Module.Alarm)) | ||
assert alarm is not None | ||
|
||
prop = getattr(alarm, prop_name) | ||
assert isinstance(prop, type) | ||
|
||
feat = alarm._device.features[feature] | ||
assert feat.value == prop | ||
assert isinstance(feat.value, type) | ||
|
||
|
||
@alarm | ||
async def test_volume_feature(dev: SmartDevice): | ||
"""Test that volume features have correct choices and range.""" | ||
alarm = next(get_parent_and_child_modules(dev, Module.Alarm)) | ||
assert alarm is not None | ||
|
||
volume_str_feat = alarm.get_feature("_alarm_volume_str") | ||
assert volume_str_feat | ||
|
||
assert volume_str_feat.choices == ["mute", "low", "normal", "high"] | ||
|
||
volume_int_feat = alarm.get_feature("alarm_volume") | ||
assert volume_int_feat.minimum_value == 0 | ||
assert volume_int_feat.maximum_value == 3 | ||
|
||
|
||
@alarm | ||
@pytest.mark.parametrize( | ||
("kwargs", "request_params"), | ||
[ | ||
pytest.param({"volume": "low"}, {"alarm_volume": "low"}, id="volume"), | ||
pytest.param({"volume": 0}, {"alarm_volume": "mute"}, id="volume-integer"), | ||
pytest.param({"duration": 1}, {"alarm_duration": 1}, id="duration"), | ||
pytest.param( | ||
{"sound": "Doorbell Ring 1"}, {"alarm_type": "Doorbell Ring 1"}, id="sound" | ||
), | ||
], | ||
) | ||
async def test_play(dev: SmartDevice, kwargs, request_params, mocker: MockerFixture): | ||
"""Test that play parameters are handled correctly.""" | ||
alarm: Alarm = next(get_parent_and_child_modules(dev, Module.Alarm)) | ||
call_spy = mocker.spy(alarm, "call") | ||
await alarm.play(**kwargs) | ||
|
||
call_spy.assert_called_with("play_alarm", request_params) | ||
|
||
with pytest.raises(ValueError, match="Invalid duration"): | ||
await alarm.play(duration=-1) | ||
|
||
with pytest.raises(ValueError, match="Invalid sound"): | ||
await alarm.play(sound="unknown") | ||
|
||
with pytest.raises(ValueError, match="Invalid volume"): | ||
await alarm.play(volume="unknown") # type: ignore[arg-type] | ||
|
||
with pytest.raises(ValueError, match="Invalid volume"): | ||
await alarm.play(volume=-1) | ||
|
||
|
||
@alarm | ||
async def test_stop(dev: SmartDevice, mocker: MockerFixture): | ||
"""Test that stop creates the correct call.""" | ||
alarm: Alarm = next(get_parent_and_child_modules(dev, Module.Alarm)) | ||
call_spy = mocker.spy(alarm, "call") | ||
await alarm.stop() | ||
|
||
call_spy.assert_called_with("stop_alarm") | ||
|
||
|
||
@alarm | ||
@pytest.mark.parametrize( | ||
("method", "value", "target_key"), | ||
[ | ||
pytest.param( | ||
"set_alarm_sound", "Doorbell Ring 1", "type", id="set_alarm_sound" | ||
), | ||
pytest.param("set_alarm_volume", "low", "volume", id="set_alarm_volume"), | ||
pytest.param("set_alarm_duration", 10, "duration", id="set_alarm_duration"), | ||
], | ||
) | ||
async def test_set_alarm_configure( | ||
dev: SmartDevice, | ||
mocker: MockerFixture, | ||
method: str, | ||
value: str | int, | ||
target_key: str, | ||
): | ||
"""Test that set_alarm_sound creates the correct call.""" | ||
alarm: Alarm = next(get_parent_and_child_modules(dev, Module.Alarm)) | ||
call_spy = mocker.spy(alarm, "call") | ||
await getattr(alarm, method)(value) | ||
|
||
expected_params = {"duration": mocker.ANY, "type": mocker.ANY, "volume": mocker.ANY} | ||
expected_params[target_key] = value | ||
|
||
call_spy.assert_called_with("set_alarm_configure", expected_params) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.