2022-01-01 21:23:10 +00:00
|
|
|
"""UniFi Protect Integration utils."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from enum import Enum
|
|
|
|
from typing import Any
|
|
|
|
|
2022-01-18 22:38:16 +00:00
|
|
|
from homeassistant.core import callback
|
|
|
|
|
2022-01-01 21:23:10 +00:00
|
|
|
|
|
|
|
def get_nested_attr(obj: Any, attr: str) -> Any:
|
|
|
|
"""Fetch a nested attribute."""
|
|
|
|
attrs = attr.split(".")
|
|
|
|
|
|
|
|
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
|
2022-01-18 22:38:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _async_unifi_mac_from_hass(mac: str) -> str:
|
|
|
|
# MAC addresses in UFP are always caps
|
|
|
|
return mac.replace(":", "").upper()
|
|
|
|
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _async_short_mac(mac: str) -> str:
|
|
|
|
"""Get the short mac address from the full mac."""
|
|
|
|
return _async_unifi_mac_from_hass(mac)[-6:]
|