Skip to content

Commit

Permalink
Improve parameter validation
Browse files Browse the repository at this point in the history
Refactor parameter validation to handle unchanged values and re-add tests for unchanged value scenarios.
  • Loading branch information
denpamusic committed Jan 25, 2025
1 parent 811f3de commit 1a4e0c5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
7 changes: 4 additions & 3 deletions pyplumio/helpers/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,6 @@ def __copy__(self) -> Parameter:
def validate(self, value: ParameterValue) -> int:
"""Validate a parameter value."""
value = _normalize_parameter_value(value)
if value == self.values.value:
raise ValueError("Parameter value is unchanged.")

if value < self.values.min_value or value > self.values.max_value:
raise ValueError(
f"Value must be between '{self.min_value}' and '{self.max_value}'"
Expand All @@ -209,6 +206,10 @@ async def _try_set(
self, value: Any, retries: int = 5, timeout: float = 5.0
) -> bool:
"""Try to set a parameter value."""
if value == self.values.value:
# Value is unchanged
return True

self._previous_value = self._values.value
self._values.value = value
self._pending_update = True
Expand Down
36 changes: 30 additions & 6 deletions tests/helpers/test_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,6 @@ async def test_number_validate(number: Number) -> None:
"""Test the number validation."""
assert number.validate(2)

with pytest.raises(ValueError):
number.validate(1)

with pytest.raises(ValueError):
number.validate(6)

Expand All @@ -135,9 +132,6 @@ async def test_switch_validate(switch: Switch) -> None:
"""Test the switch validation."""
assert switch.validate(STATE_ON)

with pytest.raises(ValueError):
assert switch.validate(STATE_OFF)

with pytest.raises(ValueError):
switch.validate(2)

Expand Down Expand Up @@ -264,6 +258,36 @@ def test_switch_repr(switch: Switch) -> None:
)


@patch("asyncio.Queue.put")
async def test_number_request_with_unchanged_value(
mock_put, number: Number, bypass_asyncio_sleep, caplog
) -> None:
"""Test that a frame doesn't get dispatched if it's value is unchanged."""
assert not number.pending_update
assert not await number.set(5, retries=3)
assert number.pending_update
assert mock_put.await_count == 3 # type: ignore [unreachable]
mock_put.reset_mock()
assert "Timed out while trying to set 'test_number' parameter" in caplog.text
await number.set(5)
mock_put.assert_not_awaited()


@patch("asyncio.Queue.put")
async def test_switch_request_with_unchanged_value(
mock_put, switch: Switch, bypass_asyncio_sleep, caplog
) -> None:
"""Test that a frame doesn't get dispatched if it's value is unchanged."""
assert not switch.pending_update
assert not await switch.set(True, retries=3)
assert switch.pending_update
assert mock_put.await_count == 3 # type: ignore [unreachable]
mock_put.reset_mock()
assert "Timed out while trying to set 'test_switch' parameter" in caplog.text
await switch.set(True)
mock_put.assert_not_awaited()


@patch("pyplumio.helpers.parameter.Switch.set")
async def test_switch_turn_on(mock_set, switch: Switch) -> None:
"""Test that switch can be turned on."""
Expand Down

0 comments on commit 1a4e0c5

Please sign in to comment.