From b1f7754f36d0a16248ca483e8576f092d8c9eb4d Mon Sep 17 00:00:00 2001 From: Steven B <51370195+sdb9696@users.noreply.github.com> Date: Thu, 23 Jan 2025 12:40:06 +0000 Subject: [PATCH] Add range to alarm_volume feature --- kasa/smart/modules/alarm.py | 25 +++++++++++++++---------- tests/smart/modules/test_alarm.py | 15 +++++++++++++++ 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/kasa/smart/modules/alarm.py b/kasa/smart/modules/alarm.py index 2209742e..1297f954 100644 --- a/kasa/smart/modules/alarm.py +++ b/kasa/smart/modules/alarm.py @@ -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 diff --git a/tests/smart/modules/test_alarm.py b/tests/smart/modules/test_alarm.py index 2879c8e0..6b551c99 100644 --- a/tests/smart/modules/test_alarm.py +++ b/tests/smart/modules/test_alarm.py @@ -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"),