diff --git a/luxtronik/datatypes.py b/luxtronik/datatypes.py index d8834351..2543e011 100755 --- a/luxtronik/datatypes.py +++ b/luxtronik/datatypes.py @@ -150,6 +150,13 @@ def options(cls): """Return list of all available options.""" return [value for _, value in cls.codes.items()] + @classmethod + def sanitize_option(cls, option): + is_string = isinstance(option, str) + if is_string: + option = option.lower().replace('-', '_').strip() + return option, is_string + @classmethod def from_heatpump(cls, value): if value is None: @@ -160,12 +167,14 @@ def from_heatpump(cls, value): @classmethod def to_heatpump(cls, value): + value, value_is_str = cls.sanitize_option(value) for index, code in cls.codes.items(): + code, _ = cls.sanitize_option(code) if code == value: return index - if isinstance(value, str) and value.startswith(cls.unknown_prefix): - return int(value.split(cls.unknown_delimiter)[1]) - if isinstance(value, (int, float)) or (isinstance(value, str) and value.isdigit()): + if value_is_str and value.startswith(cls.unknown_prefix.lower()): + return int(value.split(cls.unknown_delimiter.lower())[1]) + if isinstance(value, (int, float)) or (value_is_str and value.isdigit()): return int(value) return None diff --git a/tests/test_datatypes.py b/tests/test_datatypes.py index 79ba895e..5da5cc7e 100644 --- a/tests/test_datatypes.py +++ b/tests/test_datatypes.py @@ -233,6 +233,7 @@ def test_to_heatpump(self): a = SelectionBase("") assert a.to_heatpump("a") is None assert a.to_heatpump("Unknown_214") == 214 + assert a.to_heatpump("unknown_215") == 215 assert a.to_heatpump(0) == 0 assert a.to_heatpump("1") == 1 assert a.to_heatpump(2.3) == 2 @@ -244,8 +245,8 @@ class SelectionBaseChild(SelectionBase): codes = { 0: "a", - 1: "b", - 2: "c", + 1: "B", + 2: "c_d", } @@ -272,8 +273,8 @@ def test_from_heatpump(self): a = SelectionBaseChild("") assert a.from_heatpump(0) == "a" - assert a.from_heatpump(1) == "b" - assert a.from_heatpump(2) == "c" + assert a.from_heatpump(1) == "B" + assert a.from_heatpump(2) == "c_d" assert a.from_heatpump(3) == "Unknown_3" assert a.from_heatpump(None) is None @@ -281,9 +282,9 @@ def test_to_heatpump(self): """Test cases for to_heatpump function""" a = SelectionBaseChild("") - assert a.to_heatpump("a") == 0 - assert a.to_heatpump("b") == 1 - assert a.to_heatpump("c") == 2 + assert a.to_heatpump(" a") == 0 + assert a.to_heatpump("b ") == 1 + assert a.to_heatpump("c-D ") == 2 assert a.to_heatpump("d") is None assert a.to_heatpump(None) is None assert a.to_heatpump(2) == 2