Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Holiday integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from babel import Locale, UnknownLocaleError
8 from holidays import list_supported_countries
9 import voluptuous as vol
10 
11 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
12 from homeassistant.const import CONF_COUNTRY
14  CountrySelector,
15  CountrySelectorConfig,
16  SelectSelector,
17  SelectSelectorConfig,
18  SelectSelectorMode,
19 )
20 
21 from .const import CONF_PROVINCE, DOMAIN
22 
23 SUPPORTED_COUNTRIES = list_supported_countries(include_aliases=False)
24 
25 
26 class HolidayConfigFlow(ConfigFlow, domain=DOMAIN):
27  """Handle a config flow for Holiday."""
28 
29  VERSION = 1
30 
31  def __init__(self) -> None:
32  """Initialize the config flow."""
33  self.datadata: dict[str, Any] = {}
34 
35  async def async_step_user(
36  self, user_input: dict[str, Any] | None = None
37  ) -> ConfigFlowResult:
38  """Handle the initial step."""
39  if user_input is not None:
40  self.datadata = user_input
41 
42  selected_country = user_input[CONF_COUNTRY]
43 
44  if SUPPORTED_COUNTRIES[selected_country]:
45  return await self.async_step_provinceasync_step_province()
46 
47  self._async_abort_entries_match_async_abort_entries_match({CONF_COUNTRY: user_input[CONF_COUNTRY]})
48 
49  try:
50  locale = Locale.parse(self.hass.config.language, sep="-")
51  except UnknownLocaleError:
52  # Default to (US) English if language not recognized by babel
53  # Mainly an issue with English flavors such as "en-GB"
54  locale = Locale("en")
55  title = locale.territories[selected_country]
56  return self.async_create_entryasync_create_entryasync_create_entry(title=title, data=user_input)
57 
58  user_schema = vol.Schema(
59  {
60  vol.Optional(
61  CONF_COUNTRY, default=self.hass.config.country
62  ): CountrySelector(
64  countries=list(SUPPORTED_COUNTRIES),
65  )
66  ),
67  }
68  )
69 
70  return self.async_show_formasync_show_formasync_show_form(step_id="user", data_schema=user_schema)
71 
73  self, user_input: dict[str, Any] | None = None
74  ) -> ConfigFlowResult:
75  """Handle the province step."""
76  if user_input is not None:
77  combined_input: dict[str, Any] = {**self.datadata, **user_input}
78 
79  country = combined_input[CONF_COUNTRY]
80  province = combined_input.get(CONF_PROVINCE)
81 
82  self._async_abort_entries_match_async_abort_entries_match(
83  {
84  CONF_COUNTRY: country,
85  CONF_PROVINCE: province,
86  }
87  )
88 
89  try:
90  locale = Locale.parse(self.hass.config.language, sep="-")
91  except UnknownLocaleError:
92  # Default to (US) English if language not recognized by babel
93  # Mainly an issue with English flavors such as "en-GB"
94  locale = Locale("en")
95  province_str = f", {province}" if province else ""
96  name = f"{locale.territories[country]}{province_str}"
97 
98  return self.async_create_entryasync_create_entryasync_create_entry(title=name, data=combined_input)
99 
100  province_schema = vol.Schema(
101  {
102  vol.Optional(CONF_PROVINCE): SelectSelector(
104  options=SUPPORTED_COUNTRIES[self.datadata[CONF_COUNTRY]],
105  mode=SelectSelectorMode.DROPDOWN,
106  )
107  ),
108  }
109  )
110 
111  return self.async_show_formasync_show_formasync_show_form(step_id="province", data_schema=province_schema)
112 
114  self, user_input: dict[str, Any] | None = None
115  ) -> ConfigFlowResult:
116  """Handle the re-configuration of a province."""
117  reconfigure_entry = self._get_reconfigure_entry_get_reconfigure_entry()
118  if user_input is not None:
119  combined_input: dict[str, Any] = {**reconfigure_entry.data, **user_input}
120 
121  country = combined_input[CONF_COUNTRY]
122  province = combined_input.get(CONF_PROVINCE)
123 
124  self._async_abort_entries_match_async_abort_entries_match(
125  {
126  CONF_COUNTRY: country,
127  CONF_PROVINCE: province,
128  }
129  )
130 
131  try:
132  locale = Locale.parse(self.hass.config.language, sep="-")
133  except UnknownLocaleError:
134  # Default to (US) English if language not recognized by babel
135  # Mainly an issue with English flavors such as "en-GB"
136  locale = Locale("en")
137  province_str = f", {province}" if province else ""
138  name = f"{locale.territories[country]}{province_str}"
139 
140  return self.async_update_reload_and_abortasync_update_reload_and_abort(
141  reconfigure_entry, title=name, data=combined_input
142  )
143 
144  province_schema = vol.Schema(
145  {
146  vol.Optional(CONF_PROVINCE): SelectSelector(
148  options=SUPPORTED_COUNTRIES[
149  reconfigure_entry.data[CONF_COUNTRY]
150  ],
151  mode=SelectSelectorMode.DROPDOWN,
152  )
153  )
154  }
155  )
156 
157  return self.async_show_formasync_show_formasync_show_form(step_id="reconfigure", data_schema=province_schema)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:37
ConfigFlowResult async_step_province(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:74
ConfigFlowResult async_step_reconfigure(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:115
ConfigFlowResult async_create_entry(self, *str title, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None, Mapping[str, Any]|None options=None)
ConfigFlowResult async_update_reload_and_abort(self, ConfigEntry entry, *str|None|UndefinedType unique_id=UNDEFINED, str|UndefinedType title=UNDEFINED, Mapping[str, Any]|UndefinedType data=UNDEFINED, Mapping[str, Any]|UndefinedType data_updates=UNDEFINED, Mapping[str, Any]|UndefinedType options=UNDEFINED, str|UndefinedType reason=UNDEFINED, bool reload_even_if_entry_is_unchanged=True)
None _async_abort_entries_match(self, dict[str, Any]|None match_dict=None)
ConfigFlowResult 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_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)