From 49d07ca14742ec09ad4ed8b13b8c1368d94dcfda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Sun, 23 Jan 2022 12:59:25 +0100 Subject: [PATCH] Add more data to diagnostics platform (#2466) --- custom_components/hacs/diagnostics.py | 32 ++++++++++++++++++--- custom_components/hacs/repositories/base.py | 2 +- tests/conftest.py | 16 +++++++++++ tests/test_diagnostics.py | 18 ++++++++---- 4 files changed, 58 insertions(+), 10 deletions(-) diff --git a/custom_components/hacs/diagnostics.py b/custom_components/hacs/diagnostics.py index bc12519ae59..2480d9265ce 100644 --- a/custom_components/hacs/diagnostics.py +++ b/custom_components/hacs/diagnostics.py @@ -4,11 +4,13 @@ from typing import Any from aiogithubapi import GitHubException +from homeassistant.components.diagnostics import async_redact_data from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from .base import HacsBase from .const import DOMAIN +from .utils.configuration_schema import TOKEN async def async_get_config_entry_diagnostics( @@ -19,6 +21,7 @@ async def async_get_config_entry_diagnostics( hacs: HacsBase = hass.data[DOMAIN] data = { + "entry": entry.as_dict(), "hacs": { "stage": hacs.stage, "version": hacs.version, @@ -31,7 +34,13 @@ async def async_get_config_entry_diagnostics( "archived_repositories": hacs.common.archived_repositories, "lovelace_mode": hacs.core.lovelace_mode, "configuration": {}, - } + }, + "custom_repositories": [ + repo.data.full_name + for repo in hacs.repositories.list_all + if not hacs.repositories.is_default(str(repo.data.id)) + ], + "repositories": [], } for key in ( @@ -47,12 +56,27 @@ async def async_get_config_entry_diagnostics( ): data["hacs"]["configuration"][key] = getattr(hacs.configuration, key, None) - data["repositories"] = [repo.data.to_json() for repo in hacs.repositories.list_downloaded] + for repository in hacs.repositories.list_downloaded: + data["repositories"].append( + { + "data": repository.data.to_json(), + "integration_manifest": repository.integration_manifest, + "repository_manifest": repository.repository_manifest.to_dict(), + "ref": repository.ref, + "paths": { + "localpath": repository.localpath.replace(hacs.core.config_path, "/config"), + "local": repository.content.path.local.replace( + hacs.core.config_path, "/config" + ), + "remote": repository.content.path.remote, + }, + } + ) try: rate_limit_response = await hacs.githubapi.rate_limit() data["rate_limit"] = rate_limit_response.data.as_dict except GitHubException as exception: - data["rate_limit"] = {"error": str(exception)} + data["rate_limit"] = str(exception) - return data + return async_redact_data(data, (TOKEN,)) diff --git a/custom_components/hacs/repositories/base.py b/custom_components/hacs/repositories/base.py index dad73d55cfa..f1042773d01 100644 --- a/custom_components/hacs/repositories/base.py +++ b/custom_components/hacs/repositories/base.py @@ -685,7 +685,7 @@ def remove(self) -> None: async def uninstall(self) -> None: """Run uninstall tasks.""" - self.logger.info("%s Uninstalling", self) + self.logger.info("%s Removing", self) if not await self.remove_local_directory(): raise HacsException("Could not uninstall") self.data.installed = False diff --git a/tests/conftest.py b/tests/conftest.py index 00d133d3ee0..ac55d21abb4 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -8,6 +8,7 @@ from aiogithubapi import GitHubAPI from awesomeversion import AwesomeVersion +from homeassistant.config_entries import ConfigEntry from homeassistant.const import __version__ as HAVERSION from homeassistant.core import HomeAssistant from homeassistant.exceptions import ServiceNotFound @@ -33,6 +34,7 @@ HacsThemeRepository, ) from custom_components.hacs.tasks.manager import HacsTaskManager +from custom_components.hacs.utils.configuration_schema import TOKEN as CONF_TOKEN from custom_components.hacs.utils.queue_manager import QueueManager from custom_components.hacs.validate.manager import ValidationManager @@ -189,3 +191,17 @@ def repository_netdaemon(hacs): """Fixtrue for HACS netdaemon repository object""" repository_obj = HacsNetdaemonRepository(hacs, "test/test") yield dummy_repository_base(hacs, repository_obj) + + +@pytest.fixture +def config_entry() -> ConfigEntry: + """Fixture for a config entry.""" + yield ConfigEntry( + version=1, + domain=DOMAIN, + title="", + data={CONF_TOKEN: TOKEN}, + source="user", + options={}, + unique_id="12345", + ) diff --git a/tests/test_diagnostics.py b/tests/test_diagnostics.py index ae92bcee35c..b502ec0e1e2 100644 --- a/tests/test_diagnostics.py +++ b/tests/test_diagnostics.py @@ -2,32 +2,40 @@ from unittest.mock import MagicMock, patch from aiogithubapi import GitHubException, GitHubRateLimitModel, GitHubResponseModel +from homeassistant.components.diagnostics import REDACTED +from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant import pytest from custom_components.hacs.base import HacsBase from custom_components.hacs.diagnostics import async_get_config_entry_diagnostics +from tests.common import TOKEN + @pytest.mark.asyncio -async def test_diagnostics(hacs: HacsBase, hass: HomeAssistant): +async def test_diagnostics(hacs: HacsBase, hass: HomeAssistant, config_entry: ConfigEntry): """Test the base result.""" response = GitHubResponseModel(MagicMock(headers={})) response.data = GitHubRateLimitModel({"resources": {"core": {"remaining": 0}}}) with patch("aiogithubapi.github.GitHub.rate_limit", return_value=response): - diagnostics = await async_get_config_entry_diagnostics(hass, None) + diagnostics = await async_get_config_entry_diagnostics(hass, config_entry) assert diagnostics["hacs"]["version"] == "0.0.0" assert diagnostics["rate_limit"]["resources"]["core"]["remaining"] == 0 + assert TOKEN not in str(diagnostics) + assert diagnostics["entry"]["data"]["token"] == REDACTED @pytest.mark.asyncio -async def test_diagnostics_with_exception(hacs: HacsBase, hass: HomeAssistant): +async def test_diagnostics_with_exception( + hacs: HacsBase, hass: HomeAssistant, config_entry: ConfigEntry +): """test the result with issues getting the ratelimit.""" with patch( "aiogithubapi.github.GitHub.rate_limit", side_effect=GitHubException("Something went wrong") ): - diagnostics = await async_get_config_entry_diagnostics(hass, None) + diagnostics = await async_get_config_entry_diagnostics(hass, config_entry) assert diagnostics["hacs"]["version"] == "0.0.0" - assert diagnostics["rate_limit"]["error"] == "Something went wrong" + assert diagnostics["rate_limit"] == "Something went wrong"