Home Assistant Unofficial Reference 2024.12.1
repairs.py
Go to the documentation of this file.
1 """Issues for OpenWeatherMap."""
2 
3 from typing import cast
4 
5 from homeassistant import data_entry_flow
6 from homeassistant.components.repairs import RepairsFlow
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.const import CONF_API_KEY, CONF_MODE
9 from homeassistant.core import HomeAssistant, callback
10 from homeassistant.helpers import issue_registry as ir
11 
12 from .const import DOMAIN, OWM_MODE_V30
13 from .utils import validate_api_key
14 
15 
16 class DeprecatedV25RepairFlow(RepairsFlow):
17  """Handler for an issue fixing flow."""
18 
19  def __init__(self, entry: ConfigEntry) -> None:
20  """Create flow."""
21  super().__init__()
22  self.entryentry = entry
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 self.async_show_form(step_id="migrate")
29 
30  async def async_step_migrate(
31  self, user_input: dict[str, str] | None = None
33  """Handle the migrate step of a fix flow."""
34  errors, description_placeholders = {}, {}
35  new_options = {**self.entryentry.options, CONF_MODE: OWM_MODE_V30}
36 
37  errors, description_placeholders = await validate_api_key(
38  self.entryentry.data[CONF_API_KEY], OWM_MODE_V30
39  )
40  if not errors:
41  self.hass.config_entries.async_update_entry(self.entryentry, options=new_options)
42  await self.hass.config_entries.async_reload(self.entryentry.entry_id)
43  return self.async_create_entry(data={})
44 
45  return self.async_show_form(
46  step_id="migrate",
47  errors=errors,
48  description_placeholders=description_placeholders,
49  )
50 
51 
53  hass: HomeAssistant,
54  issue_id: str,
55  data: dict[str, str | int | float | None],
56 ) -> RepairsFlow:
57  """Create single repair flow."""
58  entry_id = cast(str, data.get("entry_id"))
59  entry = hass.config_entries.async_get_entry(entry_id)
60  assert entry
61  return DeprecatedV25RepairFlow(entry)
62 
63 
64 def _get_issue_id(entry_id: str) -> str:
65  return f"deprecated_v25_{entry_id}"
66 
67 
68 @callback
69 def async_create_issue(hass: HomeAssistant, entry_id: str) -> None:
70  """Create issue for V2.5 deprecation."""
71  ir.async_create_issue(
72  hass=hass,
73  domain=DOMAIN,
74  issue_id=_get_issue_id(entry_id),
75  is_fixable=True,
76  is_persistent=False,
77  severity=ir.IssueSeverity.WARNING,
78  learn_more_url="https://www.home-assistant.io/integrations/openweathermap/",
79  translation_key="deprecated_v25",
80  data={"entry_id": entry_id},
81  )
82 
83 
84 @callback
85 def async_delete_issue(hass: HomeAssistant, entry_id: str) -> None:
86  """Remove issue for V2.5 deprecation."""
87  ir.async_delete_issue(hass=hass, domain=DOMAIN, issue_id=_get_issue_id(entry_id))
data_entry_flow.FlowResult async_step_init(self, dict[str, str]|None user_input=None)
Definition: repairs.py:26
data_entry_flow.FlowResult async_step_migrate(self, dict[str, str]|None user_input=None)
Definition: repairs.py:32
RepairsFlow async_create_fix_flow(HomeAssistant hass, str issue_id, dict[str, str|int|float|None] data)
Definition: repairs.py:56
None async_create_issue(HomeAssistant hass, str entry_id)
Definition: repairs.py:69
None async_delete_issue(HomeAssistant hass, str entry_id)
Definition: repairs.py:85