Skip to content

Commit

Permalink
Add diagnostics (#254)
Browse files Browse the repository at this point in the history
* Add diagnostics

* Add stats to diagnostics

* Add redactions of sensitive info

* Add tests

* Setup config entry

* Cleanup getting config entry

* Remove redundant comments

* Assert against test config and stats

* Redact data from tests to get good comparison
  • Loading branch information
NickM-27 committed May 13, 2022
1 parent 5ad9ab1 commit aaf7e13
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
2 changes: 2 additions & 0 deletions custom_components/frigate/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
# Configuration and options
CONF_CAMERA_STATIC_IMAGE_HEIGHT = "camera_image_height"
CONF_NOTIFICATION_PROXY_ENABLE = "notification_proxy_enable"
CONF_PASSWORD = "password"
CONF_PATH = "path"
CONF_RTMP_URL_TEMPLATE = "rtmp_url_template"

# Defaults
Expand Down
35 changes: 35 additions & 0 deletions custom_components/frigate/diagnostics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""Diagnostics support for Frigate."""

from typing import Any, Dict

from homeassistant.components.diagnostics import async_redact_data
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant

from .const import ATTR_CLIENT, ATTR_CONFIG, CONF_PASSWORD, CONF_PATH, DOMAIN

REDACT_CONFIG = {CONF_PASSWORD, CONF_PATH}


def get_redacted_data(data: Dict[str, Any]) -> Any:
"""Redact sensitive vales from data."""
return async_redact_data(data, REDACT_CONFIG)


async def async_get_config_entry_diagnostics(
hass: HomeAssistant,
entry: ConfigEntry,
) -> Dict[str, Any]:
"""Return diagnostics for a config entry."""

config = hass.data[DOMAIN][entry.entry_id][ATTR_CONFIG]
redacted_config = get_redacted_data(config)

stats = await hass.data[DOMAIN][entry.entry_id][ATTR_CLIENT].async_get_stats()
redacted_stats = get_redacted_data(stats)

data = {
"frigate_config": redacted_config,
"frigate_stats": redacted_stats,
}
return data
39 changes: 39 additions & 0 deletions tests/test_diagnostics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""Tests frigate diagnostics."""

from http import HTTPStatus
from typing import Any, Dict

from custom_components.frigate.diagnostics import get_redacted_data
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component

from . import TEST_CONFIG, TEST_STATS, setup_mock_frigate_config_entry


async def _get_diagnostics_for_config_entry(
hass: HomeAssistant, hass_client: Any, config_entry: ConfigEntry
) -> Any:
"""Return the diagnostics config entry for the specified domain."""
assert await async_setup_component(hass, "diagnostics", {})

client = await hass_client()
response = await client.get(
f"/api/diagnostics/config_entry/{config_entry.entry_id}"
)
assert response.status == HTTPStatus.OK
return await response.json()


async def test_diagnostics(hass: HomeAssistant, hass_client: Any) -> None:
"""Test the diagnostics."""
config_entry: ConfigEntry = await setup_mock_frigate_config_entry(hass)
diagnostic_config: Dict[str, Any] = await _get_diagnostics_for_config_entry(
hass, hass_client, config_entry
)

redacted_test_config = get_redacted_data(TEST_CONFIG)
redacted_test_stats = get_redacted_data(TEST_STATS)

assert diagnostic_config["data"]["frigate_config"] == redacted_test_config
assert diagnostic_config["data"]["frigate_stats"] == redacted_test_stats

0 comments on commit aaf7e13

Please sign in to comment.