Set device connection MAC address for networked devices in NUT (#141856)

* Set device connection MAC address for networked devices

* Change variable name for consistency
This commit is contained in:
tdfountain 2025-03-30 14:21:11 -07:00 committed by GitHub
parent 5057343b6a
commit 3ab2cd3fb7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -23,6 +23,7 @@ from homeassistant.const import (
from homeassistant.core import Event, HomeAssistant, callback from homeassistant.core import Event, HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryAuthFailed, HomeAssistantError from homeassistant.exceptions import ConfigEntryAuthFailed, HomeAssistantError
from homeassistant.helpers import device_registry as dr from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, format_mac
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .const import DOMAIN, INTEGRATION_SUPPORTED_COMMANDS, PLATFORMS from .const import DOMAIN, INTEGRATION_SUPPORTED_COMMANDS, PLATFORMS
@ -153,10 +154,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: NutConfigEntry) -> bool:
coordinator, data, unique_id, user_available_commands coordinator, data, unique_id, user_available_commands
) )
connections: set[tuple[str, str]] | None = None
if data.device_info.mac_address is not None:
connections = {(CONNECTION_NETWORK_MAC, data.device_info.mac_address)}
device_registry = dr.async_get(hass) device_registry = dr.async_get(hass)
device_registry.async_get_or_create( device_registry.async_get_or_create(
config_entry_id=entry.entry_id, config_entry_id=entry.entry_id,
identifiers={(DOMAIN, unique_id)}, identifiers={(DOMAIN, unique_id)},
connections=connections,
name=data.name.title(), name=data.name.title(),
manufacturer=data.device_info.manufacturer, manufacturer=data.device_info.manufacturer,
model=data.device_info.model, model=data.device_info.model,
@ -244,6 +250,7 @@ class NUTDeviceInfo:
model_id: str | None = None model_id: str | None = None
firmware: str | None = None firmware: str | None = None
serial: str | None = None serial: str | None = None
mac_address: str | None = None
device_location: str | None = None device_location: str | None = None
@ -307,9 +314,18 @@ class PyNUTData:
model_id: str | None = self._status.get("device.part") model_id: str | None = self._status.get("device.part")
firmware = _firmware_from_status(self._status) firmware = _firmware_from_status(self._status)
serial = _serial_from_status(self._status) serial = _serial_from_status(self._status)
mac_address: str | None = self._status.get("device.macaddr")
if mac_address is not None:
mac_address = format_mac(mac_address.rstrip().replace(" ", ":"))
device_location: str | None = self._status.get("device.location") device_location: str | None = self._status.get("device.location")
return NUTDeviceInfo( return NUTDeviceInfo(
manufacturer, model, model_id, firmware, serial, device_location manufacturer,
model,
model_id,
firmware,
serial,
mac_address,
device_location,
) )
async def _async_get_status(self) -> dict[str, str]: async def _async_get_status(self) -> dict[str, str]: