diff --git a/pywizlight/bulb.py b/pywizlight/bulb.py index 09daeaf..d483a97 100755 --- a/pywizlight/bulb.py +++ b/pywizlight/bulb.py @@ -96,6 +96,11 @@ def _rgb_in_range_or_raise(rgb: Tuple[Union[float, int], ...]) -> None: raise ValueError("Blue is not in range between 0-255.") +def _validate_speed_or_raise(speed: int) -> None: + if not 10 <= speed <= 200: + raise ValueError("Value must be between 10 and 200") + + class PilotBuilder: """Get information from the bulb.""" @@ -158,10 +163,9 @@ def _set_cold_white(self, value: int) -> None: self.pilot_params["c"] = value def _set_speed(self, value: int) -> None: - """Set the color changing speed value (20-200).""" + """Set the effect changing speed (10-200).""" # This applies only to changing effects. - if not 19 < value < 201: - raise ValueError("Value must be between 20 and 200") + _validate_speed_or_raise(value) self.pilot_params["speed"] = value def _set_scene(self, scene_id: int) -> None: @@ -280,13 +284,17 @@ def get_speed(self) -> Optional[int]: """Get the color changing speed.""" return _extract_int(self.pilotResult, "speed") + def get_scene_id(self) -> Optional[int]: + if "schdPsetId" in self.pilotResult: # rhythm + return 1000 + return self.pilotResult.get("sceneId") + def get_scene(self) -> Optional[str]: """Get the current scene name.""" - if "schdPsetId" in self.pilotResult: # rhythm - return SCENES[1000] - if "sceneId" not in self.pilotResult: + scene_id = self.get_scene_id() + if scene_id is None: return None - return SCENES.get(self.pilotResult["sceneId"]) + return SCENES.get(scene_id) def get_cold_white(self) -> Optional[int]: """Get the value of the cold white led.""" @@ -541,6 +549,14 @@ async def reset(self) -> None: """Reset the bulb to factory defaults.""" await self.sendUDPMessage(r'{"method":"reset","params":{}}') + async def set_speed(self, speed: int) -> None: + """Set the effect speed.""" + # If we have state: True in the setPilot, the speed does not change + _validate_speed_or_raise(speed) + await self.sendUDPMessage( + to_wiz_json({"method": "setPilot", "params": {"speed": speed}}) + ) + async def turn_on(self, pilot_builder: PilotBuilder = PilotBuilder()) -> None: """Turn the light on with defined message. diff --git a/pywizlight/tests/test_bulb.py b/pywizlight/tests/test_bulb.py index 3b12a6e..a6b17d8 100644 --- a/pywizlight/tests/test_bulb.py +++ b/pywizlight/tests/test_bulb.py @@ -146,6 +146,14 @@ async def test_PilotBuilder_speed(correct_bulb: wizlight) -> None: assert state and state.get_speed() == 50 +@pytest.mark.asyncio +async def test_set_speed(correct_bulb: wizlight) -> None: + """Set speed.""" + await correct_bulb.set_speed(125) + state = await correct_bulb.updateState() + assert state and state.get_speed() == 125 + + @pytest.mark.asyncio async def test_get_source(correct_bulb: wizlight) -> None: """Test getting the source.""" @@ -226,6 +234,13 @@ async def test_error_PilotBuilder_speed(correct_bulb: wizlight) -> None: await correct_bulb.turn_on(PilotBuilder(speed=532)) +@pytest.mark.asyncio +async def test_error_set_speed(correct_bulb: wizlight) -> None: + """Error speed.""" + with pytest.raises(ValueError): + await correct_bulb.set_speed(532) + + @pytest.mark.asyncio async def test_fw_version(correct_bulb: wizlight) -> None: """Test fetching the firmware version."""