generated from ludeeus/integration_blueprint
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |