Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement of percent calculation #72

Merged
merged 7 commits into from
Aug 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pyvlx/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
34 changes: 34 additions & 0 deletions test/parameter_test.py
Original file line number Diff line number Diff line change
@@ -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)