Home Assistant Unofficial Reference 2024.12.1
issue_handler.py
Go to the documentation of this file.
1 """The repairs integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 import voluptuous as vol
8 
9 from homeassistant import data_entry_flow
10 from homeassistant.core import HomeAssistant, callback
11 from homeassistant.exceptions import HomeAssistantError
12 from homeassistant.helpers import issue_registry as ir
14  async_process_integration_platforms,
15 )
16 
17 from .const import DOMAIN
18 from .models import RepairsFlow, RepairsProtocol
19 
20 
22  """Handler for an issue fixing flow without any side effects."""
23 
24  async def async_step_init(
25  self, user_input: dict[str, str] | None = None
27  """Handle the first step of a fix flow."""
28  return await self.async_step_confirm()
29 
30  async def async_step_confirm(
31  self, user_input: dict[str, str] | None = None
33  """Handle the confirm step of a fix flow."""
34  if user_input is not None:
35  return self.async_create_entryasync_create_entry(data={})
36 
37  issue_registry = ir.async_get(self.hass)
38  description_placeholders = None
39  if issue := issue_registry.async_get_issue(self.handler, self.issue_id):
40  description_placeholders = issue.translation_placeholders
41 
42  return self.async_show_formasync_show_form(
43  step_id="confirm",
44  data_schema=vol.Schema({}),
45  description_placeholders=description_placeholders,
46  )
47 
48 
50  """Manage repairs flows."""
51 
52  async def async_create_flow(
53  self,
54  handler_key: str,
55  *,
56  context: data_entry_flow.FlowContext | None = None,
57  data: dict[str, Any] | None = None,
58  ) -> RepairsFlow:
59  """Create a flow. platform is a repairs module."""
60  assert data and "issue_id" in data
61  issue_id = data["issue_id"]
62 
63  issue_registry = ir.async_get(self.hasshass)
64  issue = issue_registry.async_get_issue(handler_key, issue_id)
65  if issue is None or not issue.is_fixable:
67 
68  if "platforms" not in self.hasshass.data[DOMAIN]:
70 
71  platforms: dict[str, RepairsProtocol] = self.hasshass.data[DOMAIN]["platforms"]
72  if handler_key not in platforms:
73  flow: RepairsFlow = ConfirmRepairFlow()
74  else:
75  platform = platforms[handler_key]
76  flow = await platform.async_create_fix_flow(self.hasshass, issue_id, issue.data)
77 
78  flow.issue_id = issue_id
79  flow.data = issue.data
80  return flow
81 
82  async def async_finish_flow(
83  self, flow: data_entry_flow.FlowHandler, result: data_entry_flow.FlowResult
85  """Complete a fix flow.
86 
87  This method is called when a flow step returns FlowResultType.ABORT or
88  FlowResultType.CREATE_ENTRY.
89  """
90  if result.get("type") != data_entry_flow.FlowResultType.ABORT:
91  ir.async_delete_issue(self.hasshass, flow.handler, flow.init_data["issue_id"])
92  if "result" not in result:
93  result["result"] = None
94  return result
95 
96 
97 @callback
98 def async_setup(hass: HomeAssistant) -> None:
99  """Initialize repairs."""
100  hass.data[DOMAIN]["flow_manager"] = RepairsFlowManager(hass)
101 
102 
103 async def async_process_repairs_platforms(hass: HomeAssistant) -> None:
104  """Start processing repairs platforms."""
105  hass.data[DOMAIN]["platforms"] = {}
106 
108  hass, DOMAIN, _register_repairs_platform, wait_for_platforms=True
109  )
110 
111 
112 @callback
114  hass: HomeAssistant, integration_domain: str, platform: RepairsProtocol
115 ) -> None:
116  """Register a repairs platform."""
117  if not hasattr(platform, "async_create_fix_flow"):
118  raise HomeAssistantError(f"Invalid repairs platform {platform}")
119  hass.data[DOMAIN]["platforms"][integration_domain] = platform
data_entry_flow.FlowResult async_step_init(self, dict[str, str]|None user_input=None)
RepairsFlow async_create_flow(self, str handler_key, *data_entry_flow.FlowContext|None context=None, dict[str, Any]|None data=None)
_FlowResultT async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_create_entry(self, *str|None title=None, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None)
_FlowResultT async_finish_flow(self, FlowHandler[_FlowContextT, _FlowResultT, _HandlerT] flow, _FlowResultT result)
hass
None _register_repairs_platform(HomeAssistant hass, str integration_domain, RepairsProtocol platform)
None async_process_repairs_platforms(HomeAssistant hass)
config_entries.ConfigFlowResult async_step_confirm(self, dict[str, Any]|None user_input=None)
None async_process_integration_platforms(HomeAssistant hass, str platform_name, Callable[[HomeAssistant, str, Any], Awaitable[None]|None] process_platform, bool wait_for_platforms=False)