diff --git a/homeassistant/components/unifi/__init__.py b/homeassistant/components/unifi/__init__.py index 4d11a690f35..10959b8965c 100644 --- a/homeassistant/components/unifi/__init__.py +++ b/homeassistant/components/unifi/__init__.py @@ -50,7 +50,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b hass.data[UNIFI_DOMAIN][config_entry.entry_id] = controller await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS) - await controller.async_update_device_registry() + controller.async_update_device_registry() if len(hass.data[UNIFI_DOMAIN]) == 1: async_setup_services(hass) diff --git a/homeassistant/components/unifi/controller.py b/homeassistant/components/unifi/controller.py index 0b0caf3add6..ba188f80135 100644 --- a/homeassistant/components/unifi/controller.py +++ b/homeassistant/components/unifi/controller.py @@ -29,7 +29,11 @@ from homeassistant.helpers import ( device_registry as dr, entity_registry as er, ) -from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC +from homeassistant.helpers.device_registry import ( + DeviceEntry, + DeviceEntryType, + DeviceInfo, +) from homeassistant.helpers.dispatcher import ( async_dispatcher_connect, async_dispatcher_send, @@ -158,14 +162,6 @@ class UniFiController: host: str = self.config_entry.data[CONF_HOST] return host - @property - def mac(self) -> str | None: - """Return the mac address of this controller.""" - for client in self.api.clients.values(): - if self.host == client.ip: - return client.mac - return None - @callback def register_platform_add_entities( self, @@ -341,19 +337,30 @@ class UniFiController: self._cancel_poe_command = async_call_later(self.hass, 5, async_execute_command) - async def async_update_device_registry(self) -> None: + @property + def device_info(self) -> DeviceInfo: + """UniFi controller device info.""" + assert self.config_entry.unique_id is not None + + version: str | None = None + if sysinfo := next(iter(self.api.system_information.values()), None): + version = sysinfo.version + + return DeviceInfo( + entry_type=DeviceEntryType.SERVICE, + identifiers={(UNIFI_DOMAIN, self.config_entry.unique_id)}, + manufacturer=ATTR_MANUFACTURER, + model="UniFi Network Application", + name="UniFi Network", + sw_version=version, + ) + + @callback + def async_update_device_registry(self) -> DeviceEntry: """Update device registry.""" - if self.mac is None: - return - device_registry = dr.async_get(self.hass) - - device_registry.async_get_or_create( - config_entry_id=self.config_entry.entry_id, - connections={(CONNECTION_NETWORK_MAC, self.mac)}, - default_manufacturer=ATTR_MANUFACTURER, - default_model="UniFi Network", - default_name="UniFi Network", + return device_registry.async_get_or_create( + config_entry_id=self.config_entry.entry_id, **self.device_info ) @staticmethod diff --git a/tests/components/unifi/test_controller.py b/tests/components/unifi/test_controller.py index 18fe92e6d64..f4738862aef 100644 --- a/tests/components/unifi/test_controller.py +++ b/tests/components/unifi/test_controller.py @@ -40,7 +40,6 @@ from homeassistant.const import ( ) from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr -from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.setup import async_setup_component import homeassistant.util.dt as dt_util @@ -81,6 +80,24 @@ CONFIGURATION = [] SITE = [{"desc": "Site name", "name": "site_id", "role": "admin", "_id": "1"}] +SYSTEM_INFORMATION = [ + { + "anonymous_controller_id": "24f81231-a456-4c32-abcd-f5612345385f", + "build": "atag_7.4.162_21057", + "console_display_version": "3.1.15", + "hostname": "UDMP", + "name": "UDMP", + "previous_version": "7.4.156", + "timezone": "Europe/Stockholm", + "ubnt_device_type": "UDMPRO", + "udm_version": "3.0.20.9281", + "update_available": False, + "update_downloaded": False, + "uptime": 1196290, + "version": "7.4.162", + } +] + def mock_default_unifi_requests( aioclient_mock, @@ -224,7 +241,9 @@ async def test_controller_setup( "homeassistant.config_entries.ConfigEntries.async_forward_entry_setup", return_value=True, ) as forward_entry_setup: - config_entry = await setup_unifi_integration(hass, aioclient_mock) + config_entry = await setup_unifi_integration( + hass, aioclient_mock, system_information_response=SYSTEM_INFORMATION + ) controller = hass.data[UNIFI_DOMAIN][config_entry.entry_id] entry = controller.config_entry @@ -247,29 +266,16 @@ async def test_controller_setup( assert controller.option_detection_time == timedelta(seconds=DEFAULT_DETECTION_TIME) assert isinstance(controller.option_ssid_filter, set) - assert controller.mac is None - assert controller.signal_reachable == "unifi-reachable-1" assert controller.signal_options_update == "unifi-options-1" assert controller.signal_heartbeat_missed == "unifi-heartbeat-missed" - -async def test_controller_mac( - hass: HomeAssistant, aioclient_mock: AiohttpClientMocker -) -> None: - """Test that it is possible to identify controller mac.""" - config_entry = await setup_unifi_integration( - hass, aioclient_mock, clients_response=[CONTROLLER_HOST] - ) - controller = hass.data[UNIFI_DOMAIN][config_entry.entry_id] - assert controller.mac == CONTROLLER_HOST["mac"] - - device_registry = dr.async_get(hass) - device_entry = device_registry.async_get_or_create( + device_entry = dr.async_get(hass).async_get_or_create( config_entry_id=config_entry.entry_id, - connections={(CONNECTION_NETWORK_MAC, controller.mac)}, + identifiers={(UNIFI_DOMAIN, config_entry.unique_id)}, ) - assert device_entry + + assert device_entry.sw_version == "7.4.162" async def test_controller_not_accessible(hass: HomeAssistant) -> None: