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