mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 16:57:10 +00:00
Add diagnostics for UniFi Protect (#72280)
This commit is contained in:
parent
abf9aab18f
commit
654c59c194
21
homeassistant/components/unifiprotect/diagnostics.py
Normal file
21
homeassistant/components/unifiprotect/diagnostics.py
Normal file
@ -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()))
|
@ -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:
|
||||
|
56
tests/components/unifiprotect/test_diagnostics.py
Normal file
56
tests/components/unifiprotect/test_diagnostics.py
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user