mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-01-24 13:47:05 +00:00
Add range to alarm_volume feature
This commit is contained in:
parent
a621761dd4
commit
b1f7754f36
@ -10,6 +10,16 @@ from ..smartmodule import SmartModule
|
||||
|
||||
DURATION_MAX = 10 * 60
|
||||
|
||||
VOLUME_INT_TO_STR = {
|
||||
0: "mute",
|
||||
1: "low",
|
||||
2: "normal",
|
||||
3: "high",
|
||||
}
|
||||
|
||||
VOLUME_STR_LIST = [v for v in VOLUME_INT_TO_STR.values()]
|
||||
VOLUME_INT_RANGE = (min(VOLUME_INT_TO_STR.keys()), max(VOLUME_INT_TO_STR.keys()))
|
||||
|
||||
AlarmVolume: TypeAlias = Literal["mute", "low", "normal", "high"]
|
||||
|
||||
|
||||
@ -73,7 +83,8 @@ class Alarm(SmartModule):
|
||||
attribute_setter="set_alarm_volume",
|
||||
category=Feature.Category.Config,
|
||||
type=Feature.Type.Choice,
|
||||
choices_getter=lambda: ["mute", "low", "normal", "high"],
|
||||
choices_getter=lambda: VOLUME_STR_LIST,
|
||||
range_getter=lambda: VOLUME_INT_RANGE,
|
||||
)
|
||||
)
|
||||
self._add_feature(
|
||||
@ -205,22 +216,16 @@ class Alarm(SmartModule):
|
||||
|
||||
def _check_and_convert_volume(self, volume: str | int) -> str:
|
||||
"""Raise an exception on invalid volume."""
|
||||
volume_int_to_str = {
|
||||
0: "mute",
|
||||
1: "low",
|
||||
2: "normal",
|
||||
3: "high",
|
||||
}
|
||||
if isinstance(volume, int):
|
||||
volume = volume_int_to_str.get(volume, "invalid")
|
||||
volume = VOLUME_INT_TO_STR.get(volume, "invalid")
|
||||
|
||||
if TYPE_CHECKING:
|
||||
assert isinstance(volume, str)
|
||||
|
||||
if volume not in volume_int_to_str.values():
|
||||
if volume not in VOLUME_INT_TO_STR.values():
|
||||
raise ValueError(
|
||||
f"Invalid volume {volume} "
|
||||
f"available: {volume_int_to_str.keys()}, {volume_int_to_str.values()}"
|
||||
f"available: {VOLUME_INT_TO_STR.keys()}, {VOLUME_INT_TO_STR.values()}"
|
||||
)
|
||||
|
||||
return volume
|
||||
|
@ -35,6 +35,21 @@ async def test_features(dev: SmartDevice, feature: str, prop_name: str, type: ty
|
||||
assert isinstance(feat.value, type)
|
||||
|
||||
|
||||
@alarm
|
||||
async def test_volume_feature(dev: SmartDevice):
|
||||
"""Test that volume feature has correct choices and range."""
|
||||
alarm = next(get_parent_and_child_modules(dev, Module.Alarm))
|
||||
assert alarm is not None
|
||||
|
||||
volume_feat = alarm.get_feature("alarm_volume")
|
||||
assert volume_feat
|
||||
|
||||
assert volume_feat.minimum_value == 0
|
||||
assert volume_feat.maximum_value == 3
|
||||
|
||||
assert volume_feat.choices == ["mute", "low", "normal", "high"]
|
||||
|
||||
|
||||
@alarm
|
||||
@pytest.mark.parametrize(
|
||||
("kwargs", "request_params"),
|
||||
|
Loading…
Reference in New Issue
Block a user