From f8d918ca4ad65d5f30949ca381538cb070043aa8 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 25 May 2023 00:51:45 -0500 Subject: [PATCH] Small speedup to unifiprotect attribute lookups (#93507) --- .../components/unifiprotect/utils.py | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/unifiprotect/utils.py b/homeassistant/components/unifiprotect/utils.py index 3152213cce8..061f6745f32 100644 --- a/homeassistant/components/unifiprotect/utils.py +++ b/homeassistant/components/unifiprotect/utils.py @@ -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