Skip to content
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
15 changes: 12 additions & 3 deletions luxtronik/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

Expand Down
15 changes: 8 additions & 7 deletions tests/test_datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -244,8 +245,8 @@ class SelectionBaseChild(SelectionBase):

codes = {
0: "a",
1: "b",
2: "c",
1: "B",
2: "c_d",
}


Expand All @@ -272,18 +273,18 @@ 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

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
Expand Down