Skip to content

Commit

Permalink
Add more data to diagnostics platform (#2466)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus committed Jan 23, 2022
1 parent ec78d32 commit 49d07ca
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 10 deletions.
32 changes: 28 additions & 4 deletions custom_components/hacs/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
Expand All @@ -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 (
Expand All @@ -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,))
2 changes: 1 addition & 1 deletion custom_components/hacs/repositories/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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",
)
18 changes: 13 additions & 5 deletions tests/test_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 49d07ca

Please sign in to comment.