diagnostics.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. """Diagnostics support for HACS."""
  2. from __future__ import annotations
  3. from typing import Any
  4. from aiogithubapi import GitHubException
  5. from homeassistant.components.diagnostics import async_redact_data
  6. from homeassistant.config_entries import ConfigEntry
  7. from homeassistant.core import HomeAssistant
  8. from .base import HacsBase
  9. from .const import DOMAIN
  10. from .utils.configuration_schema import TOKEN
  11. async def async_get_config_entry_diagnostics(
  12. hass: HomeAssistant,
  13. entry: ConfigEntry,
  14. ) -> dict[str, Any]:
  15. """Return diagnostics for a config entry."""
  16. hacs: HacsBase = hass.data[DOMAIN]
  17. data = {
  18. "entry": entry.as_dict(),
  19. "hacs": {
  20. "stage": hacs.stage,
  21. "version": hacs.version,
  22. "disabled_reason": hacs.system.disabled_reason,
  23. "new": hacs.status.new,
  24. "startup": hacs.status.startup,
  25. "categories": hacs.common.categories,
  26. "renamed_repositories": hacs.common.renamed_repositories,
  27. "archived_repositories": hacs.common.archived_repositories,
  28. "ignored_repositories": hacs.common.ignored_repositories,
  29. "lovelace_mode": hacs.core.lovelace_mode,
  30. "configuration": {},
  31. },
  32. "custom_repositories": [
  33. repo.data.full_name
  34. for repo in hacs.repositories.list_all
  35. if not hacs.repositories.is_default(str(repo.data.id))
  36. ],
  37. "repositories": [],
  38. }
  39. for key in (
  40. "appdaemon",
  41. "country",
  42. "debug",
  43. "dev",
  44. "experimental",
  45. "netdaemon",
  46. "python_script",
  47. "release_limit",
  48. "theme",
  49. ):
  50. data["hacs"]["configuration"][key] = getattr(hacs.configuration, key, None)
  51. for repository in hacs.repositories.list_downloaded:
  52. data["repositories"].append(
  53. {
  54. "data": repository.data.to_json(),
  55. "integration_manifest": repository.integration_manifest,
  56. "repository_manifest": repository.repository_manifest.to_dict(),
  57. "ref": repository.ref,
  58. "paths": {
  59. "localpath": repository.localpath.replace(hacs.core.config_path, "/config"),
  60. "local": repository.content.path.local.replace(
  61. hacs.core.config_path, "/config"
  62. ),
  63. "remote": repository.content.path.remote,
  64. },
  65. }
  66. )
  67. try:
  68. rate_limit_response = await hacs.githubapi.rate_limit()
  69. data["rate_limit"] = rate_limit_response.data.as_dict
  70. except GitHubException as exception:
  71. data["rate_limit"] = str(exception)
  72. return async_redact_data(data, (TOKEN,))