Rework UniFi Network Controller device and add software version (#99136)

Rework Network Controller device and add software version
This commit is contained in:
Robert Svensson 2023-08-27 17:07:38 +02:00 committed by GitHub
parent 71bf782b22
commit 5e5193eeb5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 40 deletions

View file

@ -50,7 +50,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
hass.data[UNIFI_DOMAIN][config_entry.entry_id] = controller hass.data[UNIFI_DOMAIN][config_entry.entry_id] = controller
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS) 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: if len(hass.data[UNIFI_DOMAIN]) == 1:
async_setup_services(hass) async_setup_services(hass)

View file

@ -29,7 +29,11 @@ from homeassistant.helpers import (
device_registry as dr, device_registry as dr,
entity_registry as er, 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 ( from homeassistant.helpers.dispatcher import (
async_dispatcher_connect, async_dispatcher_connect,
async_dispatcher_send, async_dispatcher_send,
@ -158,14 +162,6 @@ class UniFiController:
host: str = self.config_entry.data[CONF_HOST] host: str = self.config_entry.data[CONF_HOST]
return 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 @callback
def register_platform_add_entities( def register_platform_add_entities(
self, self,
@ -341,19 +337,30 @@ class UniFiController:
self._cancel_poe_command = async_call_later(self.hass, 5, async_execute_command) 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.""" """Update device registry."""
if self.mac is None:
return
device_registry = dr.async_get(self.hass) device_registry = dr.async_get(self.hass)
return device_registry.async_get_or_create(
device_registry.async_get_or_create( config_entry_id=self.config_entry.entry_id, **self.device_info
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",
) )
@staticmethod @staticmethod

View file

@ -40,7 +40,6 @@ from homeassistant.const import (
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
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
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
@ -81,6 +80,24 @@ CONFIGURATION = []
SITE = [{"desc": "Site name", "name": "site_id", "role": "admin", "_id": "1"}] 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( def mock_default_unifi_requests(
aioclient_mock, aioclient_mock,
@ -224,7 +241,9 @@ async def test_controller_setup(
"homeassistant.config_entries.ConfigEntries.async_forward_entry_setup", "homeassistant.config_entries.ConfigEntries.async_forward_entry_setup",
return_value=True, return_value=True,
) as forward_entry_setup: ) 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] controller = hass.data[UNIFI_DOMAIN][config_entry.entry_id]
entry = controller.config_entry entry = controller.config_entry
@ -247,29 +266,16 @@ async def test_controller_setup(
assert controller.option_detection_time == timedelta(seconds=DEFAULT_DETECTION_TIME) assert controller.option_detection_time == timedelta(seconds=DEFAULT_DETECTION_TIME)
assert isinstance(controller.option_ssid_filter, set) assert isinstance(controller.option_ssid_filter, set)
assert controller.mac is None
assert controller.signal_reachable == "unifi-reachable-1" assert controller.signal_reachable == "unifi-reachable-1"
assert controller.signal_options_update == "unifi-options-1" assert controller.signal_options_update == "unifi-options-1"
assert controller.signal_heartbeat_missed == "unifi-heartbeat-missed" assert controller.signal_heartbeat_missed == "unifi-heartbeat-missed"
device_entry = dr.async_get(hass).async_get_or_create(
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(
config_entry_id=config_entry.entry_id, 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: async def test_controller_not_accessible(hass: HomeAssistant) -> None: