diff --git a/pyvlx/parameter.py b/pyvlx/parameter.py index 7df725a6..23454dd9 100644 --- a/pyvlx/parameter.py +++ b/pyvlx/parameter.py @@ -193,7 +193,8 @@ def from_percent(position_percent): def to_percent(raw): """Create percent position value out of raw.""" # The first byte has the vlue from 0 to 200. Ignoring the second one. - return int(raw[0] / 2) + # Adding 0.5 allows a slight tolerance for devices (e.g. Velux SML) that do not return exactly 51200 as final position when closed. + return int(raw[0] / 2 + 0.5) def __str__(self): """Return string representation of object.""" diff --git a/test/parameter_test.py b/test/parameter_test.py new file mode 100644 index 00000000..570a55f4 --- /dev/null +++ b/test/parameter_test.py @@ -0,0 +1,34 @@ +"""Test for Position class.""" +import unittest + +from pyvlx import Parameter, Position + + +class TestParameterPosition(unittest.TestCase): + """Test class for Position class.""" + + def test_to_percent(self): + """Test position percent output from position int input""" + + self.assertEqual(Position.to_percent(Parameter.from_int(0)), 0) + self.assertEqual(Position.to_percent(Parameter.from_int(1)), 0) + self.assertEqual(Position.to_percent(Parameter.from_int(3)), 0) + self.assertEqual(Position.to_percent(Parameter.from_int(256)), 1) + self.assertEqual(Position.to_percent(Parameter.from_int(512)), 1) + self.assertEqual(Position.to_percent(Parameter.from_int(5120)), 10) + self.assertEqual(Position.to_percent(Parameter.from_int(12800)), 25) + self.assertEqual(Position.to_percent(Parameter.from_int(25600)), 50) + self.assertEqual(Position.to_percent(Parameter.from_int(38400)), 75) + self.assertEqual(Position.to_percent(Parameter.from_int(50688)), 99) + self.assertEqual(Position.to_percent(Parameter.from_int(51050)), 100) + self.assertEqual(Position.to_percent(Parameter.from_int(51200)), 100) + + def test_from_to_percent(self): + """Test position percent output from position percent input""" + self.assertEqual(Position.to_percent(Position.from_percent(0)), 0) + self.assertEqual(Position.to_percent(Position.from_percent(1)), 1) + self.assertEqual(Position.to_percent(Position.from_percent(25)), 25) + self.assertEqual(Position.to_percent(Position.from_percent(50)), 50) + self.assertEqual(Position.to_percent(Position.from_percent(75)), 75) + self.assertEqual(Position.to_percent(Position.from_percent(99)), 99) + self.assertEqual(Position.to_percent(Position.from_percent(100)), 100)