Reduce overhead to fetch unifiprotect attributes (#94976)

pull/95026/head
J. Nick Koston 2023-06-21 21:41:06 +02:00 committed by GitHub
parent 31f845bfe0
commit 90386bc036
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 3 additions and 2 deletions

View File

@ -38,6 +38,8 @@ from .const import (
ModelType,
)
_SENTINEL = object()
def get_nested_attr(obj: Any, attr: str) -> Any:
"""Fetch a nested attribute."""
@ -46,9 +48,8 @@ def get_nested_attr(obj: Any, attr: str) -> Any:
else:
value = obj
for key in attr.split("."):
if not hasattr(value, key):
if (value := getattr(value, key, _SENTINEL)) is _SENTINEL:
return None
value = getattr(value, key)
return value.value if isinstance(value, Enum) else value