Skip to content

Commit

Permalink
add usage_text to functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Bacher, Dominik committed Jul 12, 2023
1 parent a375f21 commit ee74f76
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions test/resources/stubs/testprojekt-ets6-functions.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"identifier": "P-05C0-0_F-1",
"name": "LivingroomLight",
"function_type": "FT-1",
"usage_text": "switchable light",
"project_uid": 11,
"group_addresses": [
{
Expand Down
9 changes: 7 additions & 2 deletions xknxproject/loader/knx_master_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ def load(
knx_proj_contents: KNXProjContents,
knx_master_file: Path,
language: str | None,
) -> tuple[dict[str, str], dict[str, str], str | None]:
) -> tuple[dict[str, str], dict[str, str], str | None, dict[str, str]]:
"""Load KNX master data."""
manufacturer_mapping: dict[str, str] = {}
space_usage_mapping: dict[str, str] = {}
product_languages: list[str] = [] # eg. "en-US", "de-DE", "fr-FR"
language_code: str | None = None
function_type_mapping: dict[str, str] = {}

with knx_master_file.open(mode="rb") as master_xml:
tree = ElementTree.parse(master_xml)
Expand All @@ -44,6 +45,10 @@ def load(
for language_node in tree.findall(".//{*}ProductLanguages/{*}Language"):
product_languages.append(language_node.get("Identifier", ""))

for function_type_node in tree.findall(".//{*}FunctionTypes/{*}FunctionType"):
identifier = function_type_node.get("Id", "")
function_type_mapping[identifier] = function_type_node.get("Text", "")

if language is not None:
language_code = KNXMasterLoader.get_language_code(
language, product_languages
Expand All @@ -65,7 +70,7 @@ def load(
) is not None:
space_usage_mapping[_ref_id] = translation_node.get("Text", "")

return manufacturer_mapping, space_usage_mapping, language_code
return manufacturer_mapping, space_usage_mapping, language_code, function_type_mapping

@staticmethod
def get_language_code(language: str, product_languages: list[str]) -> str | None:
Expand Down
14 changes: 10 additions & 4 deletions xknxproject/loader/project_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class ProjectLoader:
def load(
knx_proj_contents: KNXProjContents,
space_usage_names: dict[str, str],
function_type_names: dict[str, str],
) -> tuple[
list[XMLGroupAddress],
list[XMLArea],
Expand Down Expand Up @@ -64,7 +65,7 @@ def load(
)

location_loader = _LocationLoader(
knx_proj_contents, devices, space_usage_names
knx_proj_contents, devices, space_usage_names, function_type_names
)
for location_element in tree.findall(
f"{{*}}Project/{{*}}Installations/{{*}}Installation/{{*}}{element_name}"
Expand Down Expand Up @@ -237,6 +238,7 @@ def __init__(
knx_proj_contents: KNXProjContents,
devices: list[DeviceInstance],
space_usage_names: dict[str, str],
function_type_names: dict[str, str],
):
"""Initialize the LocationLoader."""
self._element_name = (
Expand All @@ -246,6 +248,7 @@ def __init__(
device.identifier: device.individual_address for device in devices
}
self.space_usage_names = space_usage_names
self.function_type_names = function_type_names

def load(self, location_element: ElementTree.Element) -> list[XMLSpace]:
"""Load Location mappings."""
Expand Down Expand Up @@ -288,17 +291,20 @@ def parse_space(self, node: ElementTree.Element) -> XMLSpace:
def parse_functions(self, node: ElementTree.Element) -> XMLFunction:
"""Parse a functions from the document."""
project_uid = node.get("Puid")
function_type=node.get("Type") # type: ignore[arg-type]
usage_text = self.function_type_names.get(function_type, "") if function_type else ""
functions: XMLFunction = XMLFunction(
identifier=node.get("Id"), # type: ignore[arg-type]
name=node.get("Name"), # type: ignore[arg-type]
function_type=node.get("Type"), # type: ignore[arg-type]
function_type=function_type,
project_uid=int(project_uid) if project_uid else None,
group_addresses=[],
usage_text=usage_text,
)

for sub_node in node:
if sub_node.tag.endswith("GroupAddressRef"):
project_uid = sub_node.get("Puid")
project_uid = sub_node.get("Puid")
group_address_ref: XMLGroupAddressRef = XMLGroupAddressRef(
identifier=sub_node.get("Id"), # type: ignore[arg-type]
name=sub_node.get("Name"), # type: ignore[arg-type]
Expand Down
1 change: 1 addition & 0 deletions xknxproject/models/knxproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class Function(TypedDict):
identifier: str
name: str
function_type: str
usage_text: str
project_uid: int | None
group_addresses: list[GroupAddressRef]

Expand Down
1 change: 1 addition & 0 deletions xknxproject/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ class XMLFunction:
identifier: str
name: str
function_type: str
usage_text: str
project_uid: int | None
group_addresses: list[XMLGroupAddressRef]

Expand Down
3 changes: 3 additions & 0 deletions xknxproject/xml/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ def convert_functions(self, functions: list[XMLFunction]) -> list[Function]:
identifier=f.identifier,
name=f.name,
function_type=f.function_type,
usage_text=f.usage_text,
project_uid=f.project_uid,
group_addresses=self.convert_group_address_ref(f.group_addresses),
)
Expand Down Expand Up @@ -221,6 +222,7 @@ def load(self, language: str | None) -> None:
manufacturer_names,
space_usage_names,
self.language_code,
function_type_names,
) = KNXMasterLoader.load(
knx_proj_contents=self.knx_proj_contents,
knx_master_file=self.knx_proj_contents.root_path / "knx_master.xml",
Expand All @@ -235,6 +237,7 @@ def load(self, language: str | None) -> None:
) = ProjectLoader.load(
knx_proj_contents=self.knx_proj_contents,
space_usage_names=space_usage_names,
function_type_names=function_type_names,
)

products_dict: dict[str, Product] = {}
Expand Down

0 comments on commit ee74f76

Please sign in to comment.