From 654c59c19414c52f95f56603c460f63edeb60959 Mon Sep 17 00:00:00 2001 From: Christopher Bailey Date: Sat, 21 May 2022 19:35:27 -0400 Subject: [PATCH] Add diagnostics for UniFi Protect (#72280) --- .../components/unifiprotect/diagnostics.py | 21 +++++++ tests/components/unifiprotect/conftest.py | 13 +++++ .../unifiprotect/test_diagnostics.py | 56 +++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 homeassistant/components/unifiprotect/diagnostics.py create mode 100644 tests/components/unifiprotect/test_diagnostics.py diff --git a/homeassistant/components/unifiprotect/diagnostics.py b/homeassistant/components/unifiprotect/diagnostics.py new file mode 100644 index 00000000000..b76c9eba1e7 --- /dev/null +++ b/homeassistant/components/unifiprotect/diagnostics.py @@ -0,0 +1,21 @@ +"""Diagnostics support for UniFi Network.""" +from __future__ import annotations + +from typing import Any, cast + +from pyunifiprotect.test_util.anonymize import anonymize_data + +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant + +from .const import DOMAIN +from .data import ProtectData + + +async def async_get_config_entry_diagnostics( + hass: HomeAssistant, config_entry: ConfigEntry +) -> dict[str, Any]: + """Return diagnostics for a config entry.""" + + data: ProtectData = hass.data[DOMAIN][config_entry.entry_id] + return cast(dict[str, Any], anonymize_data(data.api.bootstrap.unifi_dict())) diff --git a/tests/components/unifiprotect/conftest.py b/tests/components/unifiprotect/conftest.py index 6eeef02e817..3986e4cd5a3 100644 --- a/tests/components/unifiprotect/conftest.py +++ b/tests/components/unifiprotect/conftest.py @@ -67,6 +67,19 @@ class MockBootstrap: """Fake process method for tests.""" pass + def unifi_dict(self) -> dict[str, Any]: + """Return UniFi formatted dict representation of the NVR.""" + return { + "nvr": self.nvr.unifi_dict(), + "cameras": [c.unifi_dict() for c in self.cameras.values()], + "lights": [c.unifi_dict() for c in self.lights.values()], + "sensors": [c.unifi_dict() for c in self.sensors.values()], + "viewers": [c.unifi_dict() for c in self.viewers.values()], + "liveviews": [c.unifi_dict() for c in self.liveviews.values()], + "doorlocks": [c.unifi_dict() for c in self.doorlocks.values()], + "chimes": [c.unifi_dict() for c in self.chimes.values()], + } + @dataclass class MockEntityFixture: diff --git a/tests/components/unifiprotect/test_diagnostics.py b/tests/components/unifiprotect/test_diagnostics.py new file mode 100644 index 00000000000..b58e164e913 --- /dev/null +++ b/tests/components/unifiprotect/test_diagnostics.py @@ -0,0 +1,56 @@ +"""Test UniFi Protect diagnostics.""" + +from pyunifiprotect.data import NVR, Light + +from homeassistant.core import HomeAssistant + +from .conftest import MockEntityFixture + +from tests.components.diagnostics import get_diagnostics_for_config_entry + + +async def test_diagnostics( + hass: HomeAssistant, mock_entry: MockEntityFixture, mock_light: Light, hass_client +): + """Test generating diagnostics for a config entry.""" + + light1 = mock_light.copy() + light1._api = mock_entry.api + light1.name = "Test Light 1" + light1.id = "lightid1" + + mock_entry.api.bootstrap.lights = { + light1.id: light1, + } + await hass.config_entries.async_setup(mock_entry.entry.entry_id) + await hass.async_block_till_done() + + diag = await get_diagnostics_for_config_entry(hass, hass_client, mock_entry.entry) + + nvr_obj: NVR = mock_entry.api.bootstrap.nvr + # validate some of the data + assert "nvr" in diag and isinstance(diag["nvr"], dict) + nvr = diag["nvr"] + # should have been anonymized + assert nvr["id"] != nvr_obj.id + assert nvr["mac"] != nvr_obj.mac + assert nvr["host"] != str(nvr_obj.host) + # should have been kept + assert nvr["firmwareVersion"] == nvr_obj.firmware_version + assert nvr["version"] == str(nvr_obj.version) + assert nvr["type"] == nvr_obj.type + + assert ( + "lights" in diag + and isinstance(diag["lights"], list) + and len(diag["lights"]) == 1 + ) + light = diag["lights"][0] + # should have been anonymized + assert light["id"] != light1.id + assert light["name"] != light1.mac + assert light["mac"] != light1.mac + assert light["host"] != str(light1.host) + # should have been kept + assert light["firmwareVersion"] == light1.firmware_version + assert light["type"] == light1.type