Home Assistant Unofficial Reference 2024.12.1
repairs.py
Go to the documentation of this file.
1 """Repairs platform for the Workday integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Any, cast
6 
7 from holidays import list_supported_countries
8 import voluptuous as vol
9 
10 from homeassistant import data_entry_flow
11 from homeassistant.components.repairs import ConfirmRepairFlow, RepairsFlow
12 from homeassistant.config_entries import ConfigEntry
13 from homeassistant.const import CONF_COUNTRY
14 from homeassistant.core import HomeAssistant
16  SelectSelector,
17  SelectSelectorConfig,
18  SelectSelectorMode,
19 )
20 
21 from .config_flow import validate_custom_dates
22 from .const import CONF_PROVINCE, CONF_REMOVE_HOLIDAYS
23 
24 
26  """Handler for an issue fixing flow."""
27 
28  def __init__(self, entry: ConfigEntry, country: str | None) -> None:
29  """Create flow."""
30  self.entryentry = entry
31  self.countrycountry: str | None = country
32  super().__init__()
33 
34  async def async_step_init(
35  self, user_input: dict[str, str] | None = None
37  """Handle the first step of a fix flow."""
38  if self.countrycountry:
39  return await self.async_step_provinceasync_step_province()
40  return await self.async_step_countryasync_step_country()
41 
42  async def async_step_country(
43  self, user_input: dict[str, Any] | None = None
45  """Handle the country step of a fix flow."""
46  if user_input is not None:
47  all_countries = list_supported_countries(include_aliases=False)
48  if not all_countries[user_input[CONF_COUNTRY]]:
49  options = dict(self.entryentry.options)
50  new_options = {**options, **user_input, CONF_PROVINCE: None}
51  self.hass.config_entries.async_update_entry(
52  self.entryentry, options=new_options
53  )
54  await self.hass.config_entries.async_reload(self.entryentry.entry_id)
55  return self.async_create_entryasync_create_entry(data={})
56  self.countrycountry = user_input[CONF_COUNTRY]
57  return await self.async_step_provinceasync_step_province()
58 
59  return self.async_show_formasync_show_form(
60  step_id="country",
61  data_schema=vol.Schema(
62  {
63  vol.Required(CONF_COUNTRY): SelectSelector(
65  options=sorted(
66  list_supported_countries(include_aliases=False)
67  ),
68  mode=SelectSelectorMode.DROPDOWN,
69  )
70  )
71  }
72  ),
73  description_placeholders={"title": self.entryentry.title},
74  )
75 
77  self, user_input: dict[str, Any] | None = None
79  """Handle the province step of a fix flow."""
80  if user_input is not None:
81  user_input.setdefault(CONF_PROVINCE, None)
82  options = dict(self.entryentry.options)
83  new_options = {**options, **user_input, CONF_COUNTRY: self.countrycountry}
84  self.hass.config_entries.async_update_entry(self.entryentry, options=new_options)
85  await self.hass.config_entries.async_reload(self.entryentry.entry_id)
86  return self.async_create_entryasync_create_entry(data={})
87 
88  assert self.countrycountry
89  country_provinces = list_supported_countries(include_aliases=False)[
90  self.countrycountry
91  ]
92  return self.async_show_formasync_show_form(
93  step_id="province",
94  data_schema=vol.Schema(
95  {
96  vol.Optional(CONF_PROVINCE): SelectSelector(
98  options=country_provinces,
99  mode=SelectSelectorMode.DROPDOWN,
100  translation_key=CONF_PROVINCE,
101  )
102  ),
103  }
104  ),
105  description_placeholders={
106  CONF_COUNTRY: self.countrycountry,
107  "title": self.entryentry.title,
108  },
109  )
110 
111 
113  """Handler for an issue fixing flow."""
114 
115  def __init__(
116  self, entry: ConfigEntry, country: str | None, named_holiday: str
117  ) -> None:
118  """Create flow."""
119  self.entryentry = entry
120  self.country: str | None = country
121  self.named_holiday: str = named_holiday
122  super().__init__()
123 
124  async def async_step_init(
125  self, user_input: dict[str, str] | None = None
127  """Handle the first step of a fix flow."""
128  return await self.async_step_fix_remove_holidayasync_step_fix_remove_holiday()
129 
131  self, user_input: dict[str, Any] | None = None
133  """Handle the options step of a fix flow."""
134  errors: dict[str, str] = {}
135  if user_input:
136  options = dict(self.entryentry.options)
137  new_options = {**options, **user_input}
138  try:
139  await self.hass.async_add_executor_job(
140  validate_custom_dates, new_options
141  )
142  except Exception: # noqa: BLE001
143  errors["remove_holidays"] = "remove_holiday_error"
144  else:
145  self.hass.config_entries.async_update_entry(
146  self.entryentry, options=new_options
147  )
148  await self.hass.config_entries.async_reload(self.entryentry.entry_id)
149  return self.async_create_entryasync_create_entry(data={})
150 
151  remove_holidays = self.entryentry.options[CONF_REMOVE_HOLIDAYS]
152  removed_named_holiday = [
153  value for value in remove_holidays if value != self.named_holiday
154  ]
155  new_schema = self.add_suggested_values_to_schemaadd_suggested_values_to_schema(
156  vol.Schema(
157  {
158  vol.Optional(CONF_REMOVE_HOLIDAYS, default=[]): SelectSelector(
160  options=[],
161  multiple=True,
162  custom_value=True,
163  mode=SelectSelectorMode.DROPDOWN,
164  )
165  ),
166  }
167  ),
168  {CONF_REMOVE_HOLIDAYS: removed_named_holiday},
169  )
170  return self.async_show_formasync_show_form(
171  step_id="fix_remove_holiday",
172  data_schema=new_schema,
173  description_placeholders={
174  CONF_COUNTRY: self.country if self.country else "-",
175  CONF_REMOVE_HOLIDAYS: self.named_holiday,
176  "title": self.entryentry.title,
177  },
178  errors=errors,
179  )
180 
181 
183  hass: HomeAssistant,
184  issue_id: str,
185  data: dict[str, Any] | None,
186 ) -> RepairsFlow:
187  """Create flow."""
188  entry = None
189  if data and (entry_id := data.get("entry_id")):
190  entry_id = cast(str, entry_id)
191  entry = hass.config_entries.async_get_entry(entry_id)
192 
193  if data and (holiday := data.get("named_holiday")) and entry:
194  # Bad named holiday in configuration
195  return HolidayFixFlow(entry, data.get("country"), holiday)
196 
197  if data and entry:
198  # Country or province does not exist
199  return CountryFixFlow(entry, data.get("country"))
200 
201  return ConfirmRepairFlow()
data_entry_flow.FlowResult async_step_province(self, dict[str, Any]|None user_input=None)
Definition: repairs.py:78
None __init__(self, ConfigEntry entry, str|None country)
Definition: repairs.py:28
data_entry_flow.FlowResult async_step_country(self, dict[str, Any]|None user_input=None)
Definition: repairs.py:44
data_entry_flow.FlowResult async_step_init(self, dict[str, str]|None user_input=None)
Definition: repairs.py:36
None __init__(self, ConfigEntry entry, str|None country, str named_holiday)
Definition: repairs.py:117
data_entry_flow.FlowResult async_step_fix_remove_holiday(self, dict[str, Any]|None user_input=None)
Definition: repairs.py:132
data_entry_flow.FlowResult async_step_init(self, dict[str, str]|None user_input=None)
Definition: repairs.py:126
vol.Schema add_suggested_values_to_schema(self, vol.Schema data_schema, Mapping[str, Any]|None suggested_values)
_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)
RepairsFlow async_create_fix_flow(HomeAssistant hass, str issue_id, dict[str, Any]|None data)
Definition: repairs.py:186