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
36 changes: 26 additions & 10 deletions luxtronik/data_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,29 @@ def parse(self, raw_data):
entry.raw = data
self._data[index] = entry

def _name_lookup(self, name):
"""
Try to find the index using the given field name.

Args:
name (string): Field name.

Returns:
tuple[int | None, str | None]:
0: Index found or None
1: New preferred name, if available, otherwise None
"""
obsolete_entry = self._obsolete.get(name, None)
if obsolete_entry:
return None, obsolete_entry
for index, entry in self._data.items():
check_result = entry.check_name(name)
if check_result == LUXTRONIK_NAME_CHECK_PREFERRED:
return index, None
elif check_result == LUXTRONIK_NAME_CHECK_OBSOLETE:
return index, entry.name
return None, None

def _lookup(self, target, with_index=False):
"""
Lookup an entry
Expand All @@ -52,16 +75,9 @@ def _lookup(self, target, with_index=False):
target_index = int(target)
except ValueError:
# Get entry by name
target_index = None
obsolete_entry = self._obsolete.get(target, None)
if obsolete_entry:
raise KeyError(f"The name '{target}' is obsolete! Use '{obsolete_entry}' instead.")
for index, entry in self._data.items():
check_result = entry.check_name(target)
if check_result == LUXTRONIK_NAME_CHECK_PREFERRED:
target_index = index
elif check_result == LUXTRONIK_NAME_CHECK_OBSOLETE:
raise KeyError(f"The name '{target}' is obsolete! Use '{entry.name}' instead.")
target_index, new_name = self._name_lookup(target)
if new_name is not None:
raise KeyError(f"The name '{target}' is obsolete! Use '{new_name}' instead.")
elif isinstance(target, int):
# Get entry by id
target_index = target
Expand Down
38 changes: 38 additions & 0 deletions tests/test_LuxtronikData.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Test suite for LuxtronikData"""

import pytest

from luxtronik import (
LuxtronikData,
LuxtronikAllData,
Expand Down Expand Up @@ -58,6 +60,42 @@ def test_get_firmware_version(self):
a.calculations.get(84).raw = ord("1")
assert a.get_firmware_version() == "V3.1"

# Test of downward compatibility with outdated entry name
assert a.calculations.get("ID_WEB_SoftStand").value == "V3.1"


@pytest.mark.parametrize("vector, index, names", [
("para", 1106, ["ID_Einst_SilenceTimer_13", "Unknown_Parameter_1106"]),
("para", 1109, ["ID_Einst_SilenceTimer_16", "Unknown_Parameter_1109"]),
("calc", 232, ["Vapourisation_Temperature", "Unknown_Calculation_232"]),
("calc", 241, ["HUP_PWM", "Circulation_Pump", "Unknown_Calculation_241"]),
("visi", 182, ["ID_Visi_Heizung_Zeitschaltprogramm", "ID_Visi_Heizung_Zeitschlaltprogramm", "Unknown_Visibility_182"]),
("visi", 326, ["Unknown_Visibility_326"]),
])
def test_obsolete(self, vector, index, names):
"""Test data access with outdated names"""

data = LuxtronikData()

if vector == "para":
vector = data.parameters
elif vector == "calc":
vector = data.calculations
elif vector == "visi":
vector = data.visibilities

field_i = vector.get(index)
for idx, name in enumerate(names):
try:
# field should be found for index 0
field_n = vector.get(name)
assert idx == 0
assert field_n == field_i
assert field_n.name == names[0]
assert field_n._names == names
except Exception:
# KeyError should be thrown for all other indices
assert idx > 0


class TestLuxtronikAllData:
Expand Down
1 change: 1 addition & 0 deletions tests/test_calculations.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ def test_init(self):
"""Test cases for initialization"""
calculations = Calculations()
assert calculations.name == "Calculation"
assert calculations.calculations == calculations._data
Loading