appdaemon.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. """Class for appdaemon apps in HACS."""
  2. from __future__ import annotations
  3. from typing import TYPE_CHECKING
  4. from aiogithubapi import AIOGitHubAPIException
  5. from ..enums import HacsCategory, HacsDispatchEvent
  6. from ..exceptions import HacsException
  7. from ..utils.decorator import concurrent
  8. from .base import HacsRepository
  9. if TYPE_CHECKING:
  10. from ..base import HacsBase
  11. class HacsAppdaemonRepository(HacsRepository):
  12. """Appdaemon apps in HACS."""
  13. def __init__(self, hacs: HacsBase, full_name: str):
  14. """Initialize."""
  15. super().__init__(hacs=hacs)
  16. self.data.full_name = full_name
  17. self.data.full_name_lower = full_name.lower()
  18. self.data.category = HacsCategory.APPDAEMON
  19. self.content.path.local = self.localpath
  20. self.content.path.remote = "apps"
  21. @property
  22. def localpath(self):
  23. """Return localpath."""
  24. return f"{self.hacs.core.config_path}/appdaemon/apps/{self.data.name}"
  25. async def validate_repository(self):
  26. """Validate."""
  27. await self.common_validate()
  28. # Custom step 1: Validate content.
  29. try:
  30. addir = await self.repository_object.get_contents("apps", self.ref)
  31. except AIOGitHubAPIException:
  32. raise HacsException(
  33. f"{self.string} Repository structure for {self.ref.replace('tags/','')} is not compliant"
  34. ) from None
  35. if not isinstance(addir, list):
  36. self.validate.errors.append(f"{self.string} Repository structure not compliant")
  37. self.content.path.remote = addir[0].path
  38. self.content.objects = await self.repository_object.get_contents(
  39. self.content.path.remote, self.ref
  40. )
  41. # Handle potential errors
  42. if self.validate.errors:
  43. for error in self.validate.errors:
  44. if not self.hacs.status.startup:
  45. self.logger.error("%s %s", self.string, error)
  46. return self.validate.success
  47. @concurrent(concurrenttasks=10, backoff_time=5)
  48. async def update_repository(self, ignore_issues=False, force=False):
  49. """Update."""
  50. if not await self.common_update(ignore_issues, force) and not force:
  51. return
  52. # Get appdaemon objects.
  53. if self.repository_manifest:
  54. if self.repository_manifest.content_in_root:
  55. self.content.path.remote = ""
  56. if self.content.path.remote == "apps":
  57. addir = await self.repository_object.get_contents(self.content.path.remote, self.ref)
  58. self.content.path.remote = addir[0].path
  59. self.content.objects = await self.repository_object.get_contents(
  60. self.content.path.remote, self.ref
  61. )
  62. # Set local path
  63. self.content.path.local = self.localpath
  64. # Signal entities to refresh
  65. if self.data.installed:
  66. self.hacs.async_dispatch(
  67. HacsDispatchEvent.REPOSITORY,
  68. {
  69. "id": 1337,
  70. "action": "update",
  71. "repository": self.data.full_name,
  72. "repository_id": self.data.id,
  73. },
  74. )