__init__.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. """Register_commands."""
  2. from __future__ import annotations
  3. from typing import TYPE_CHECKING, Any
  4. from homeassistant.components import websocket_api
  5. from homeassistant.core import HomeAssistant, callback
  6. from homeassistant.helpers.dispatcher import async_dispatcher_connect
  7. import voluptuous as vol
  8. from ..const import DOMAIN
  9. from .critical import hacs_critical_acknowledge, hacs_critical_list
  10. from .repositories import (
  11. hacs_repositories_add,
  12. hacs_repositories_clear_new,
  13. hacs_repositories_list,
  14. hacs_repositories_remove,
  15. hacs_repositories_removed,
  16. )
  17. from .repository import (
  18. hacs_repository_beta,
  19. hacs_repository_download,
  20. hacs_repository_ignore,
  21. hacs_repository_info,
  22. hacs_repository_refresh,
  23. hacs_repository_release_notes,
  24. hacs_repository_remove,
  25. hacs_repository_state,
  26. hacs_repository_version,
  27. )
  28. if TYPE_CHECKING:
  29. from ..base import HacsBase
  30. @callback
  31. def async_register_websocket_commands(hass: HomeAssistant) -> None:
  32. """Register_commands."""
  33. websocket_api.async_register_command(hass, hacs_info)
  34. websocket_api.async_register_command(hass, hacs_subscribe)
  35. websocket_api.async_register_command(hass, hacs_repository_info)
  36. websocket_api.async_register_command(hass, hacs_repository_download)
  37. websocket_api.async_register_command(hass, hacs_repository_ignore)
  38. websocket_api.async_register_command(hass, hacs_repository_state)
  39. websocket_api.async_register_command(hass, hacs_repository_version)
  40. websocket_api.async_register_command(hass, hacs_repository_beta)
  41. websocket_api.async_register_command(hass, hacs_repository_refresh)
  42. websocket_api.async_register_command(hass, hacs_repository_release_notes)
  43. websocket_api.async_register_command(hass, hacs_repository_remove)
  44. websocket_api.async_register_command(hass, hacs_critical_acknowledge)
  45. websocket_api.async_register_command(hass, hacs_critical_list)
  46. websocket_api.async_register_command(hass, hacs_repositories_list)
  47. websocket_api.async_register_command(hass, hacs_repositories_add)
  48. websocket_api.async_register_command(hass, hacs_repositories_clear_new)
  49. websocket_api.async_register_command(hass, hacs_repositories_removed)
  50. websocket_api.async_register_command(hass, hacs_repositories_remove)
  51. @websocket_api.websocket_command(
  52. {
  53. vol.Required("type"): "hacs/subscribe",
  54. vol.Required("signal"): str,
  55. }
  56. )
  57. @websocket_api.require_admin
  58. @websocket_api.async_response
  59. async def hacs_subscribe(
  60. hass: HomeAssistant,
  61. connection: websocket_api.ActiveConnection,
  62. msg: dict,
  63. ) -> None:
  64. """Handle websocket subscriptions."""
  65. @callback
  66. def forward_messages(data: dict | None = None):
  67. """Forward events to websocket."""
  68. connection.send_message(websocket_api.event_message(msg["id"], data))
  69. connection.subscriptions[msg["id"]] = async_dispatcher_connect(
  70. hass,
  71. msg["signal"],
  72. forward_messages,
  73. )
  74. connection.send_message(websocket_api.result_message(msg["id"]))
  75. @websocket_api.websocket_command(
  76. {
  77. vol.Required("type"): "hacs/info",
  78. }
  79. )
  80. @websocket_api.require_admin
  81. @websocket_api.async_response
  82. async def hacs_info(
  83. hass: HomeAssistant,
  84. connection: websocket_api.ActiveConnection,
  85. msg: dict[str, Any],
  86. ) -> None:
  87. """Return information about HACS."""
  88. hacs: HacsBase = hass.data.get(DOMAIN)
  89. connection.send_message(
  90. websocket_api.result_message(
  91. msg["id"],
  92. {
  93. "categories": hacs.common.categories,
  94. "country": hacs.configuration.country,
  95. "debug": hacs.configuration.debug,
  96. "dev": hacs.configuration.dev,
  97. "disabled_reason": hacs.system.disabled_reason,
  98. "experimental": hacs.configuration.experimental,
  99. "has_pending_tasks": hacs.queue.has_pending_tasks,
  100. "lovelace_mode": hacs.core.lovelace_mode,
  101. "stage": hacs.stage,
  102. "startup": hacs.status.startup,
  103. "version": hacs.version,
  104. },
  105. )
  106. )