Small speedup to unifiprotect attribute lookups (#93507)

This commit is contained in:
J. Nick Koston 2023-05-25 00:51:45 -05:00 committed by GitHub
parent c63e3c3bf1
commit f8d918ca4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 11 deletions

View File

@ -41,18 +41,16 @@ from .const import (
def get_nested_attr(obj: Any, attr: str) -> Any:
"""Fetch a nested attribute."""
attrs = attr.split(".")
if "." not in attr:
value = getattr(obj, attr, None)
else:
value = obj
for key in attr.split("."):
if not hasattr(value, key):
return None
value = getattr(value, key)
value = obj
for key in attrs:
if not hasattr(value, key):
return None
value = getattr(value, key)
if isinstance(value, Enum):
value = value.value
return value
return value.value if isinstance(value, Enum) else value
@callback