Skip to content

Commit

Permalink
Merge pull request #128 from bdraco/speed
Browse files Browse the repository at this point in the history
Add set_speed method
  • Loading branch information
sbidy authored Feb 13, 2022
2 parents 19d9de8 + 881bc39 commit e7895fa
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
30 changes: 23 additions & 7 deletions pywizlight/bulb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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.
Expand Down
15 changes: 15 additions & 0 deletions pywizlight/tests/test_bulb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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."""
Expand Down

0 comments on commit e7895fa

Please sign in to comment.