entity.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. """HACS Base entities."""
  2. from __future__ import annotations
  3. from typing import TYPE_CHECKING, Any
  4. from homeassistant.core import callback
  5. from homeassistant.helpers.device_registry import DeviceEntryType
  6. from homeassistant.helpers.dispatcher import async_dispatcher_connect
  7. from homeassistant.helpers.entity import Entity
  8. from .const import DOMAIN, HACS_SYSTEM_ID, NAME_SHORT
  9. from .enums import HacsDispatchEvent, HacsGitHubRepo
  10. if TYPE_CHECKING:
  11. from .base import HacsBase
  12. from .repositories.base import HacsRepository
  13. def system_info(hacs: HacsBase) -> dict:
  14. """Return system info."""
  15. return {
  16. "identifiers": {(DOMAIN, HACS_SYSTEM_ID)},
  17. "name": NAME_SHORT,
  18. "manufacturer": "hacs.xyz",
  19. "model": "",
  20. "sw_version": str(hacs.version),
  21. "configuration_url": "homeassistant://hacs",
  22. "entry_type": DeviceEntryType.SERVICE,
  23. }
  24. class HacsBaseEntity(Entity):
  25. """Base HACS entity."""
  26. repository: HacsRepository | None = None
  27. _attr_should_poll = False
  28. def __init__(self, hacs: HacsBase) -> None:
  29. """Initialize."""
  30. self.hacs = hacs
  31. async def async_added_to_hass(self) -> None:
  32. """Register for status events."""
  33. self.async_on_remove(
  34. async_dispatcher_connect(
  35. self.hass,
  36. HacsDispatchEvent.REPOSITORY,
  37. self._update_and_write_state,
  38. )
  39. )
  40. @callback
  41. def _update(self) -> None:
  42. """Update the sensor."""
  43. async def async_update(self) -> None:
  44. """Manual updates of the sensor."""
  45. self._update()
  46. @callback
  47. def _update_and_write_state(self, _: Any) -> None:
  48. """Update the entity and write state."""
  49. self._update()
  50. self.async_write_ha_state()
  51. class HacsSystemEntity(HacsBaseEntity):
  52. """Base system entity."""
  53. _attr_icon = "hacs:hacs"
  54. _attr_unique_id = HACS_SYSTEM_ID
  55. @property
  56. def device_info(self) -> dict[str, any]:
  57. """Return device information about HACS."""
  58. return system_info(self.hacs)
  59. class HacsRepositoryEntity(HacsBaseEntity):
  60. """Base repository entity."""
  61. def __init__(
  62. self,
  63. hacs: HacsBase,
  64. repository: HacsRepository,
  65. ) -> None:
  66. """Initialize."""
  67. super().__init__(hacs=hacs)
  68. self.repository = repository
  69. self._attr_unique_id = str(repository.data.id)
  70. @property
  71. def available(self) -> bool:
  72. """Return True if entity is available."""
  73. return self.hacs.repositories.is_downloaded(repository_id=str(self.repository.data.id))
  74. @property
  75. def device_info(self) -> dict[str, any]:
  76. """Return device information about HACS."""
  77. if self.repository.data.full_name == HacsGitHubRepo.INTEGRATION:
  78. return system_info(self.hacs)
  79. return {
  80. "identifiers": {(DOMAIN, str(self.repository.data.id))},
  81. "name": self.repository.display_name,
  82. "model": self.repository.data.category,
  83. "manufacturer": ", ".join(
  84. author.replace("@", "") for author in self.repository.data.authors
  85. ),
  86. "configuration_url": "homeassistant://hacs",
  87. "entry_type": DeviceEntryType.SERVICE,
  88. }
  89. @callback
  90. def _update_and_write_state(self, data: dict) -> None:
  91. """Update the entity and write state."""
  92. if data.get("repository_id") == self.repository.data.id:
  93. self._update()
  94. self.async_write_ha_state()