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 = [
|
ATTR_DEVICE_TRACKER: Final = [
|
||||||
"comment",
|
"comment",
|
||||||
"mac-address",
|
|
||||||
"ssid",
|
"ssid",
|
||||||
"interface",
|
"interface",
|
||||||
"signal-strength",
|
"signal-strength",
|
||||||
|
|
|
@ -18,10 +18,6 @@ import homeassistant.util.dt as dt_util
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .hub import Device, MikrotikDataUpdateCoordinator
|
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(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
|
@ -90,6 +86,8 @@ class MikrotikDataUpdateCoordinatorTracker(
|
||||||
"""Initialize the tracked device."""
|
"""Initialize the tracked device."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
self.device = device
|
self.device = device
|
||||||
|
self._attr_name = str(device.name)
|
||||||
|
self._attr_unique_id = device.mac
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_connected(self) -> bool:
|
def is_connected(self) -> bool:
|
||||||
|
@ -107,12 +105,6 @@ class MikrotikDataUpdateCoordinatorTracker(
|
||||||
"""Return the source type of the client."""
|
"""Return the source type of the client."""
|
||||||
return SOURCE_TYPE_ROUTER
|
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
|
@property
|
||||||
def hostname(self) -> str:
|
def hostname(self) -> str:
|
||||||
"""Return the hostname of the client."""
|
"""Return the hostname of the client."""
|
||||||
|
@ -128,14 +120,7 @@ class MikrotikDataUpdateCoordinatorTracker(
|
||||||
"""Return the mac address of the client."""
|
"""Return the mac address of the client."""
|
||||||
return self.device.ip_address
|
return self.device.ip_address
|
||||||
|
|
||||||
@property
|
|
||||||
def unique_id(self) -> str:
|
|
||||||
"""Return a unique identifier for this device."""
|
|
||||||
return self.device.mac
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def extra_state_attributes(self) -> dict[str, Any] | None:
|
def extra_state_attributes(self) -> dict[str, Any] | None:
|
||||||
"""Return the device state attributes."""
|
"""Return the device state attributes."""
|
||||||
if self.is_connected:
|
return self.device.attrs if self.is_connected else None
|
||||||
return {k: v for k, v in self.device.attrs.items() if k not in FILTER_ATTRS}
|
|
||||||
return None
|
|
||||||
|
|
|
@ -77,11 +77,10 @@ class Device:
|
||||||
@property
|
@property
|
||||||
def attrs(self) -> dict[str, Any]:
|
def attrs(self) -> dict[str, Any]:
|
||||||
"""Return device attributes."""
|
"""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:
|
for attr in ATTR_DEVICE_TRACKER:
|
||||||
if attr in attr_data:
|
if attr in attr_data:
|
||||||
self._attrs[slugify(attr)] = attr_data[attr]
|
self._attrs[slugify(attr)] = attr_data[attr]
|
||||||
self._attrs["ip_address"] = self._params.get("active-address")
|
|
||||||
return self._attrs
|
return self._attrs
|
||||||
|
|
||||||
def update(
|
def update(
|
||||||
|
@ -250,7 +249,7 @@ class MikrotikData:
|
||||||
) -> list[dict[str, Any]]:
|
) -> list[dict[str, Any]]:
|
||||||
"""Retrieve data from Mikrotik API."""
|
"""Retrieve data from Mikrotik API."""
|
||||||
try:
|
try:
|
||||||
_LOGGER.info("Running command %s", cmd)
|
_LOGGER.debug("Running command %s", cmd)
|
||||||
if params:
|
if params:
|
||||||
return list(self.api(cmd=cmd, **params))
|
return list(self.api(cmd=cmd, **params))
|
||||||
return list(self.api(cmd=cmd))
|
return list(self.api(cmd=cmd))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue