Home Assistant Unofficial Reference 2024.12.1
websocket_api.py
Go to the documentation of this file.
1 """The repairs websocket API."""
2 
3 from __future__ import annotations
4 
5 from http import HTTPStatus
6 from typing import Any
7 
8 from aiohttp import web
9 import voluptuous as vol
10 
11 from homeassistant import data_entry_flow
12 from homeassistant.auth.permissions.const import POLICY_EDIT
13 from homeassistant.components import websocket_api
14 from homeassistant.components.http.data_validator import RequestDataValidator
15 from homeassistant.components.http.decorators import require_admin
16 from homeassistant.core import HomeAssistant, callback
17 from homeassistant.exceptions import Unauthorized
18 from homeassistant.helpers import issue_registry as ir
20  FlowManagerIndexView,
21  FlowManagerResourceView,
22 )
23 
24 from .const import DOMAIN
25 
26 
27 @callback
28 def async_setup(hass: HomeAssistant) -> None:
29  """Set up the repairs websocket API."""
30  websocket_api.async_register_command(hass, ws_get_issue_data)
31  websocket_api.async_register_command(hass, ws_ignore_issue)
32  websocket_api.async_register_command(hass, ws_list_issues)
33 
34  hass.http.register_view(RepairsFlowIndexView(hass.data[DOMAIN]["flow_manager"]))
35  hass.http.register_view(RepairsFlowResourceView(hass.data[DOMAIN]["flow_manager"]))
36 
37 
38 @callback
39 @websocket_api.websocket_command( { vol.Required("type"): "repairs/get_issue_data",
40  vol.Required("domain"): str,
41  vol.Required("issue_id"): str,
42  }
43 )
45  hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict[str, Any]
46 ) -> None:
47  """Fix an issue."""
48  issue_registry = ir.async_get(hass)
49  if not (issue := issue_registry.async_get_issue(msg["domain"], msg["issue_id"])):
50  connection.send_error(
51  msg["id"],
52  "unknown_issue",
53  f"Issue '{msg['issue_id']}' not found",
54  )
55  return
56  connection.send_result(msg["id"], {"issue_data": issue.data})
57 
58 
59 @callback
60 @websocket_api.websocket_command( { vol.Required("type"): "repairs/ignore_issue",
61  vol.Required("domain"): str,
62  vol.Required("issue_id"): str,
63  vol.Required("ignore"): bool,
64  }
65 )
66 def ws_ignore_issue(
67  hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict[str, Any]
68 ) -> None:
69  """Fix an issue."""
70  ir.async_ignore_issue(hass, msg["domain"], msg["issue_id"], msg["ignore"])
71 
72  connection.send_result(msg["id"])
73 
74 
75 @websocket_api.websocket_command( { vol.Required("type"): "repairs/list_issues",
76  }
77 )
78 @callback
79 def ws_list_issues(
80  hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict[str, Any]
81 ) -> None:
82  """Return a list of issues."""
83  issue_registry = ir.async_get(hass)
84  issues = [
85  {
86  "breaks_in_ha_version": issue.breaks_in_ha_version,
87  "created": issue.created,
88  "dismissed_version": issue.dismissed_version,
89  "ignored": issue.dismissed_version is not None,
90  "domain": issue.domain,
91  "is_fixable": issue.is_fixable,
92  "issue_domain": issue.issue_domain,
93  "issue_id": issue.issue_id,
94  "learn_more_url": issue.learn_more_url,
95  "severity": issue.severity,
96  "translation_key": issue.translation_key,
97  "translation_placeholders": issue.translation_placeholders,
98  }
99  for issue in issue_registry.issues.values()
100  if issue.active
101  ]
102  connection.send_result(msg["id"], {"issues": issues})
103 
104 
106  """View to create issue fix flows."""
107 
108  url = "/api/repairs/issues/fix"
109  name = "api:repairs:issues:fix"
110 
111  @require_admin(error=Unauthorized(permission=POLICY_EDIT))
112  @RequestDataValidator( vol.Schema( { vol.Required("handler"): str,
113  vol.Required("issue_id"): str,
114  },
115  extra=vol.ALLOW_EXTRA,
116  )
117  )
118  async def post(self, request: web.Request, data: dict[str, Any]) -> web.Response:
119  """Handle a POST request."""
120  try:
121  result = await self._flow_mgr_flow_mgr.async_init(
122  data["handler"],
123  data={"issue_id": data["issue_id"]},
124  )
126  return self.json_message("Invalid handler specified", HTTPStatus.NOT_FOUND)
128  return self.json_message(
129  "Handler does not support user", HTTPStatus.BAD_REQUEST
130  )
131 
132  result = self._prepare_result_json_prepare_result_json(result)
133 
134  return self.json(result)
135 
136 
138  """View to interact with the option flow manager."""
139 
140  url = "/api/repairs/issues/fix/{flow_id}"
141  name = "api:repairs:issues:fix:resource"
142 
143  @require_admin(error=Unauthorized(permission=POLICY_EDIT))
144  async def get(self, request: web.Request, /, flow_id: str) -> web.Response:
145  """Get the current state of a data_entry_flow."""
146  return await super().get(request, flow_id)
147 
148  @require_admin(error=Unauthorized(permission=POLICY_EDIT))
149  async def post(self, request: web.Request, flow_id: str) -> web.Response:
150  """Handle a POST request."""
151  return await super().post(request, flow_id)
152 
web.Response post(self, web.Request request, dict[str, Any] data)
web.Response post(self, web.Request request, str flow_id)
web.Response get(self, web.Request request, str flow_id)
data_entry_flow.FlowResult _prepare_result_json(self, data_entry_flow.FlowResult result)
_flow_mgr
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None ws_get_issue_data(HomeAssistant hass, websocket_api.ActiveConnection connection, dict[str, Any] msg)
None ws_ignore_issue(HomeAssistant hass, websocket_api.ActiveConnection connection, dict[str, Any] msg)
None ws_list_issues(HomeAssistant hass, websocket_api.ActiveConnection connection, dict[str, Any] msg)