From 90386bc036f02872579fc71bc7a0e3319feaeb70 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 21 Jun 2023 21:41:06 +0200 Subject: [PATCH] Reduce overhead to fetch unifiprotect attributes (#94976) --- homeassistant/components/unifiprotect/utils.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/unifiprotect/utils.py b/homeassistant/components/unifiprotect/utils.py index 061f6745f32..e0c56cfd5fc 100644 --- a/homeassistant/components/unifiprotect/utils.py +++ b/homeassistant/components/unifiprotect/utils.py @@ -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