Skip to content

Commit

Permalink
Merge pull request #383 from ikalchev/v4.2.1
Browse files Browse the repository at this point in the history
V4.2.1
  • Loading branch information
ikalchev authored Sep 6, 2021
2 parents b4343fa + 97a32e9 commit 52e7659
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ Sections
### Developers
-->

## [4.2.1] - 2021-09-6

### Fixed
- Fix floating point values with minStep. [#382](https://github.com/ikalchev/HAP-python/pull/382)

## [4.2.0] - 2021-09-04

### Changed
Expand Down
4 changes: 2 additions & 2 deletions pyhap/characteristic.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ def to_valid_value(self, value):
logger.error(error_msg)
raise ValueError(error_msg)
min_step = self.properties.get(PROP_MIN_STEP)
if min_step:
value = value - (value % min_step)
if value and min_step:
value = round(min_step * round(value / min_step), 14)
value = min(self.properties.get(PROP_MAX_VALUE, value), value)
value = max(self.properties.get(PROP_MIN_VALUE, value), value)
if self.properties[PROP_FORMAT] != HAP_FORMAT_FLOAT:
Expand Down
2 changes: 1 addition & 1 deletion pyhap/const.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""This module contains constants used by other modules."""
MAJOR_VERSION = 4
MINOR_VERSION = 2
PATCH_VERSION = 0
PATCH_VERSION = 1
__short_version__ = "{}.{}".format(MAJOR_VERSION, MINOR_VERSION)
__version__ = "{}.{}".format(__short_version__, PATCH_VERSION)
REQUIRED_PYTHON_VER = (3, 6)
Expand Down
55 changes: 53 additions & 2 deletions tests/test_characteristic.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,14 @@ def test_set_value_invalid_min_step(int_format):
with patch(path) as mock_notify:
char.set_value(5.55)
# Ensure floating point is dropped on an int property
# Ensure value is lowered to match minStep
assert char.value == 4
# Ensure value is rounded to match minStep
assert char.value == 6
assert mock_notify.called is False

char.set_value(6.00)
# Ensure floating point is dropped on an int property
# Ensure value is rounded to match minStep
assert char.value == 6
assert mock_notify.called is False

char.broker = Mock()
Expand All @@ -159,6 +165,51 @@ def test_set_value_invalid_min_step(int_format):
assert mock_notify.call_count == 1


def test_set_value_invalid_min_float():
"""Test setting the value of a characteristic that is outside the minStep."""
props = PROPERTIES.copy()
props["Format"] = HAP_FORMAT_FLOAT
props["minStep"] = 0.1
char = get_char(props, min_value=0, max_value=26)

char.set_value(5.55)
# Ensure value is rounded to match minStep
assert char.value == 5.5

char.set_value(22.2)
# Ensure value is rounded to match minStep
assert char.value == 22.2

char.set_value(22.200000)
# Ensure value is rounded to match minStep
assert char.value == 22.2

props = PROPERTIES.copy()
props["Format"] = HAP_FORMAT_FLOAT
props["minStep"] = 0.00001
char = get_char(props, min_value=0, max_value=26)

char.set_value(5.55)
# Ensure value is rounded to match minStep
assert char.value == 5.55

char.set_value(22.2)
# Ensure value is rounded to match minStep
assert char.value == 22.2

char.set_value(22.200000)
# Ensure value is rounded to match minStep
assert char.value == 22.2

char.set_value(22.12345678)
# Ensure value is rounded to match minStep
assert char.value == 22.12346

char.set_value(0)
# Ensure value is not modified
assert char.value == 0


@pytest.mark.parametrize("int_format", HAP_FORMAT_INTS)
def test_set_value_int(int_format):
"""Test setting the value of a characteristic."""
Expand Down

0 comments on commit 52e7659

Please sign in to comment.