1 """Adds config flow for Workday integration."""
3 from __future__
import annotations
5 from functools
import partial
8 from holidays
import PUBLIC, HolidayBase, country_holidays, list_supported_countries
9 import voluptuous
as vol
23 CountrySelectorConfig,
25 LanguageSelectorConfig,
58 """Update schema with province from country."""
62 all_countries = list_supported_countries(include_aliases=
False)
67 _country = country_holidays(country=country)
68 if country_default_language := (_country.default_language):
69 selectable_languages = _country.supported_languages
70 new_selectable_languages =
list(selectable_languages)
73 CONF_LANGUAGE, default=country_default_language
76 languages=new_selectable_languages, native_name=
True
81 if provinces := all_countries.get(country):
86 mode=SelectSelectorMode.DROPDOWN,
87 translation_key=CONF_PROVINCE,
94 _categories = [x
for x
in _country.supported_categories
if x != PUBLIC]
100 mode=SelectSelectorMode.DROPDOWN,
102 translation_key=CONF_CATEGORY,
109 **DATA_SCHEMA_OPT.schema,
118 """Validate date range."""
119 if check_date.find(
",") > 0:
120 dates = check_date.split(
",", maxsplit=1)
122 if dt_util.parse_date(date)
is None:
123 raise error(
"Incorrect date in range")
129 """Validate custom dates for add/remove holidays."""
130 for add_date
in user_input[CONF_ADD_HOLIDAYS]:
133 and dt_util.parse_date(add_date)
is None
137 year: int = dt_util.now().year
138 if country := user_input.get(CONF_COUNTRY):
139 language = user_input.get(CONF_LANGUAGE)
140 province = user_input.get(CONF_PROVINCE)
141 obj_holidays = country_holidays(
148 supported_languages := obj_holidays.supported_languages
149 )
and language ==
"en":
150 for lang
in supported_languages:
151 if lang.startswith(
"en"):
152 obj_holidays = country_holidays(
159 obj_holidays = HolidayBase(years=year)
161 for remove_date
in user_input[CONF_REMOVE_HOLIDAYS]:
164 and dt_util.parse_date(remove_date)
is None
165 and obj_holidays.get_named(remove_date) == []
170 DATA_SCHEMA_OPT = vol.Schema(
172 vol.Optional(CONF_WORKDAYS, default=DEFAULT_WORKDAYS):
SelectSelector(
174 options=ALLOWED_DAYS,
176 mode=SelectSelectorMode.DROPDOWN,
177 translation_key=
"days",
180 vol.Optional(CONF_EXCLUDES, default=DEFAULT_EXCLUDES):
SelectSelector(
182 options=ALLOWED_DAYS,
184 mode=SelectSelectorMode.DROPDOWN,
185 translation_key=
"days",
188 vol.Optional(CONF_OFFSET, default=DEFAULT_OFFSET):
NumberSelector(
196 mode=SelectSelectorMode.DROPDOWN,
204 mode=SelectSelectorMode.DROPDOWN,
212 """Handle a config flow for Workday integration."""
216 data: dict[str, Any] = {}
221 config_entry: ConfigEntry,
222 ) -> WorkdayOptionsFlowHandler:
223 """Get the options flow for this handler."""
227 self, user_input: dict[str, Any] |
None =
None
228 ) -> ConfigFlowResult:
229 """Handle the user initial step."""
230 errors: dict[str, str] = {}
232 supported_countries = await self.hass.async_add_executor_job(
233 partial(list_supported_countries, include_aliases=
False)
236 if user_input
is not None:
241 data_schema=vol.Schema(
243 vol.Required(CONF_NAME, default=DEFAULT_NAME):
TextSelector(),
246 countries=
list(supported_countries),
255 self, user_input: dict[str, Any] |
None =
None
256 ) -> ConfigFlowResult:
257 """Handle remaining flow."""
258 errors: dict[str, str] = {}
259 if user_input
is not None:
260 combined_input: dict[str, Any] = {**self.
datadata, **user_input}
263 await self.hass.async_add_executor_job(
264 validate_custom_dates, combined_input
266 except AddDatesError:
267 errors[
"add_holidays"] =
"add_holiday_error"
268 except AddDateRangeError:
269 errors[
"add_holidays"] =
"add_holiday_range_error"
270 except RemoveDatesError:
271 errors[
"remove_holidays"] =
"remove_holiday_error"
272 except RemoveDateRangeError:
273 errors[
"remove_holidays"] =
"remove_holiday_range_error"
276 CONF_COUNTRY: combined_input.get(CONF_COUNTRY),
277 CONF_EXCLUDES: combined_input[CONF_EXCLUDES],
278 CONF_OFFSET: combined_input[CONF_OFFSET],
279 CONF_WORKDAYS: combined_input[CONF_WORKDAYS],
280 CONF_ADD_HOLIDAYS: combined_input[CONF_ADD_HOLIDAYS],
281 CONF_REMOVE_HOLIDAYS: combined_input[CONF_REMOVE_HOLIDAYS],
282 CONF_PROVINCE: combined_input.get(CONF_PROVINCE),
284 if CONF_CATEGORY
in combined_input:
285 abort_match[CONF_CATEGORY] = combined_input[CONF_CATEGORY]
286 LOGGER.debug(
"abort_check in options with %s", combined_input)
289 LOGGER.debug(
"Errors have occurred %s", errors)
291 LOGGER.debug(
"No duplicate, no errors, creating entry")
293 title=combined_input[CONF_NAME],
295 options=combined_input,
298 schema = await self.hass.async_add_executor_job(
299 add_province_and_language_to_schema,
301 self.
datadata.
get(CONF_COUNTRY),
306 data_schema=new_schema,
308 description_placeholders={
309 "name": self.
datadata[CONF_NAME],
310 "country": self.
datadata.
get(CONF_COUNTRY,
"-"),
316 """Handle Workday options."""
319 self, user_input: dict[str, Any] |
None =
None
320 ) -> ConfigFlowResult:
321 """Manage Workday options."""
322 errors: dict[str, str] = {}
324 if user_input
is not None:
326 if CONF_PROVINCE
not in user_input:
328 combined_input.pop(CONF_PROVINCE,
None)
331 await self.hass.async_add_executor_job(
332 validate_custom_dates, combined_input
334 except AddDatesError:
335 errors[
"add_holidays"] =
"add_holiday_error"
336 except AddDateRangeError:
337 errors[
"add_holidays"] =
"add_holiday_range_error"
338 except RemoveDatesError:
339 errors[
"remove_holidays"] =
"remove_holiday_error"
340 except RemoveDateRangeError:
341 errors[
"remove_holidays"] =
"remove_holiday_range_error"
343 LOGGER.debug(
"abort_check in options with %s", combined_input)
346 CONF_EXCLUDES: combined_input[CONF_EXCLUDES],
347 CONF_OFFSET: combined_input[CONF_OFFSET],
348 CONF_WORKDAYS: combined_input[CONF_WORKDAYS],
349 CONF_ADD_HOLIDAYS: combined_input[CONF_ADD_HOLIDAYS],
350 CONF_REMOVE_HOLIDAYS: combined_input[CONF_REMOVE_HOLIDAYS],
351 CONF_PROVINCE: combined_input.get(CONF_PROVINCE),
353 if CONF_CATEGORY
in combined_input:
354 abort_match[CONF_CATEGORY] = combined_input[CONF_CATEGORY]
357 except AbortFlow
as err:
358 errors = {
"base": err.reason}
363 schema: vol.Schema = await self.hass.async_add_executor_job(
364 add_province_and_language_to_schema,
366 options.get(CONF_COUNTRY),
370 LOGGER.debug(
"Errors have occurred in options %s", errors)
373 data_schema=new_schema,
375 description_placeholders={
376 "name": options[CONF_NAME],
377 "country": options.get(CONF_COUNTRY,
"-"),
383 """Exception for error adding dates."""
386 class AddDateRangeError(HomeAssistantError):
387 """Exception for error adding dates."""
391 """Exception for error removing dates."""
395 """Exception for error removing dates."""
399 """Exception country does not exist error."""
WorkdayOptionsFlowHandler async_get_options_flow(ConfigEntry config_entry)
ConfigFlowResult async_step_options(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_init(self, dict[str, Any]|None user_input=None)
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)
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)
None _async_abort_entries_match(self, dict[str, Any]|None match_dict=None)
ConfigEntry config_entry(self)
None config_entry(self, ConfigEntry value)
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)
web.Response get(self, web.Request request, str config_key)
bool _is_valid_date_range(str check_date, type[HomeAssistantError] error)
None validate_custom_dates(dict[str, Any] user_input)
vol.Schema add_province_and_language_to_schema(vol.Schema schema, str|None country)