Skip to content

Commit 52e7659

Browse files
authored
Merge pull request #383 from ikalchev/v4.2.1
V4.2.1
2 parents b4343fa + 97a32e9 commit 52e7659

4 files changed

Lines changed: 61 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ Sections
1616
### Developers
1717
-->
1818

19+
## [4.2.1] - 2021-09-6
20+
21+
### Fixed
22+
- Fix floating point values with minStep. [#382](https://github.com/ikalchev/HAP-python/pull/382)
23+
1924
## [4.2.0] - 2021-09-04
2025

2126
### Changed

pyhap/characteristic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ def to_valid_value(self, value):
210210
logger.error(error_msg)
211211
raise ValueError(error_msg)
212212
min_step = self.properties.get(PROP_MIN_STEP)
213-
if min_step:
214-
value = value - (value % min_step)
213+
if value and min_step:
214+
value = round(min_step * round(value / min_step), 14)
215215
value = min(self.properties.get(PROP_MAX_VALUE, value), value)
216216
value = max(self.properties.get(PROP_MIN_VALUE, value), value)
217217
if self.properties[PROP_FORMAT] != HAP_FORMAT_FLOAT:

pyhap/const.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""This module contains constants used by other modules."""
22
MAJOR_VERSION = 4
33
MINOR_VERSION = 2
4-
PATCH_VERSION = 0
4+
PATCH_VERSION = 1
55
__short_version__ = "{}.{}".format(MAJOR_VERSION, MINOR_VERSION)
66
__version__ = "{}.{}".format(__short_version__, PATCH_VERSION)
77
REQUIRED_PYTHON_VER = (3, 6)

tests/test_characteristic.py

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,14 @@ def test_set_value_invalid_min_step(int_format):
139139
with patch(path) as mock_notify:
140140
char.set_value(5.55)
141141
# Ensure floating point is dropped on an int property
142-
# Ensure value is lowered to match minStep
143-
assert char.value == 4
142+
# Ensure value is rounded to match minStep
143+
assert char.value == 6
144+
assert mock_notify.called is False
145+
146+
char.set_value(6.00)
147+
# Ensure floating point is dropped on an int property
148+
# Ensure value is rounded to match minStep
149+
assert char.value == 6
144150
assert mock_notify.called is False
145151

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

161167

168+
def test_set_value_invalid_min_float():
169+
"""Test setting the value of a characteristic that is outside the minStep."""
170+
props = PROPERTIES.copy()
171+
props["Format"] = HAP_FORMAT_FLOAT
172+
props["minStep"] = 0.1
173+
char = get_char(props, min_value=0, max_value=26)
174+
175+
char.set_value(5.55)
176+
# Ensure value is rounded to match minStep
177+
assert char.value == 5.5
178+
179+
char.set_value(22.2)
180+
# Ensure value is rounded to match minStep
181+
assert char.value == 22.2
182+
183+
char.set_value(22.200000)
184+
# Ensure value is rounded to match minStep
185+
assert char.value == 22.2
186+
187+
props = PROPERTIES.copy()
188+
props["Format"] = HAP_FORMAT_FLOAT
189+
props["minStep"] = 0.00001
190+
char = get_char(props, min_value=0, max_value=26)
191+
192+
char.set_value(5.55)
193+
# Ensure value is rounded to match minStep
194+
assert char.value == 5.55
195+
196+
char.set_value(22.2)
197+
# Ensure value is rounded to match minStep
198+
assert char.value == 22.2
199+
200+
char.set_value(22.200000)
201+
# Ensure value is rounded to match minStep
202+
assert char.value == 22.2
203+
204+
char.set_value(22.12345678)
205+
# Ensure value is rounded to match minStep
206+
assert char.value == 22.12346
207+
208+
char.set_value(0)
209+
# Ensure value is not modified
210+
assert char.value == 0
211+
212+
162213
@pytest.mark.parametrize("int_format", HAP_FORMAT_INTS)
163214
def test_set_value_int(int_format):
164215
"""Test setting the value of a characteristic."""

0 commit comments

Comments
 (0)