Cleanup mikrotik device extra_attributes (#74491)
This commit is contained in:
parent
e529ef7b0e
commit
d33779d3a0
3 changed files with 5 additions and 22 deletions
|
@ -44,7 +44,6 @@ PLATFORMS: Final = [Platform.DEVICE_TRACKER]
|
|||
|
||||
ATTR_DEVICE_TRACKER: Final = [
|
||||
"comment",
|
||||
"mac-address",
|
||||
"ssid",
|
||||
"interface",
|
||||
"signal-strength",
|
||||
|
|
|
@ -18,10 +18,6 @@ import homeassistant.util.dt as dt_util
|
|||
from .const import DOMAIN
|
||||
from .hub import Device, MikrotikDataUpdateCoordinator
|
||||
|
||||
# These are normalized to ATTR_IP and ATTR_MAC to conform
|
||||
# to device_tracker
|
||||
FILTER_ATTRS = ("ip_address", "mac_address")
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
|
@ -90,6 +86,8 @@ class MikrotikDataUpdateCoordinatorTracker(
|
|||
"""Initialize the tracked device."""
|
||||
super().__init__(coordinator)
|
||||
self.device = device
|
||||
self._attr_name = str(device.name)
|
||||
self._attr_unique_id = device.mac
|
||||
|
||||
@property
|
||||
def is_connected(self) -> bool:
|
||||
|
@ -107,12 +105,6 @@ class MikrotikDataUpdateCoordinatorTracker(
|
|||
"""Return the source type of the client."""
|
||||
return SOURCE_TYPE_ROUTER
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""Return the name of the client."""
|
||||
# Stringify to ensure we return a string
|
||||
return str(self.device.name)
|
||||
|
||||
@property
|
||||
def hostname(self) -> str:
|
||||
"""Return the hostname of the client."""
|
||||
|
@ -128,14 +120,7 @@ class MikrotikDataUpdateCoordinatorTracker(
|
|||
"""Return the mac address of the client."""
|
||||
return self.device.ip_address
|
||||
|
||||
@property
|
||||
def unique_id(self) -> str:
|
||||
"""Return a unique identifier for this device."""
|
||||
return self.device.mac
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self) -> dict[str, Any] | None:
|
||||
"""Return the device state attributes."""
|
||||
if self.is_connected:
|
||||
return {k: v for k, v in self.device.attrs.items() if k not in FILTER_ATTRS}
|
||||
return None
|
||||
return self.device.attrs if self.is_connected else None
|
||||
|
|
|
@ -77,11 +77,10 @@ class Device:
|
|||
@property
|
||||
def attrs(self) -> dict[str, Any]:
|
||||
"""Return device attributes."""
|
||||
attr_data = self._wireless_params if self._wireless_params else self._params
|
||||
attr_data = self._wireless_params | self._params
|
||||
for attr in ATTR_DEVICE_TRACKER:
|
||||
if attr in attr_data:
|
||||
self._attrs[slugify(attr)] = attr_data[attr]
|
||||
self._attrs["ip_address"] = self._params.get("active-address")
|
||||
return self._attrs
|
||||
|
||||
def update(
|
||||
|
@ -250,7 +249,7 @@ class MikrotikData:
|
|||
) -> list[dict[str, Any]]:
|
||||
"""Retrieve data from Mikrotik API."""
|
||||
try:
|
||||
_LOGGER.info("Running command %s", cmd)
|
||||
_LOGGER.debug("Running command %s", cmd)
|
||||
if params:
|
||||
return list(self.api(cmd=cmd, **params))
|
||||
return list(self.api(cmd=cmd))
|
||||
|
|
Loading…
Add table
Reference in a new issue