Skip to content

Commit

Permalink
Minor cleanup config flow Workday (#92163)
Browse files Browse the repository at this point in the history
  • Loading branch information
gjohansson-ST committed May 6, 2023
1 parent c624e50 commit 7e19bb4
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions homeassistant/components/workday/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

from typing import Any

import holidays
from holidays import HolidayBase
from holidays import country_holidays, list_supported_countries
import voluptuous as vol

from homeassistant.config_entries import (
Expand Down Expand Up @@ -48,15 +47,14 @@

def add_province_to_schema(
schema: vol.Schema,
options: dict[str, Any],
country: str,
) -> vol.Schema:
"""Update schema with province from country."""
year: int = dt.now().year
obj_holidays: HolidayBase = getattr(holidays, options[CONF_COUNTRY])(years=year)
if not obj_holidays.subdivisions:
all_countries = list_supported_countries()
if not all_countries[country]:
return schema

province_list = [NONE_SENTINEL, *obj_holidays.subdivisions]
province_list = [NONE_SENTINEL, *all_countries[country]]
add_schema = {
vol.Optional(CONF_PROVINCE, default=NONE_SENTINEL): SelectSelector(
SelectSelectorConfig(
Expand All @@ -78,11 +76,9 @@ def validate_custom_dates(user_input: dict[str, Any]) -> None:
raise AddDatesError("Incorrect date")

year: int = dt.now().year
obj_holidays: HolidayBase = getattr(holidays, user_input[CONF_COUNTRY])(years=year)
if user_input.get(CONF_PROVINCE):
obj_holidays = getattr(holidays, user_input[CONF_COUNTRY])(
subdiv=user_input[CONF_PROVINCE], years=year
)
obj_holidays = country_holidays(
user_input[CONF_COUNTRY], user_input.get(CONF_PROVINCE), year
)

for remove_date in user_input[CONF_REMOVE_HOLIDAYS]:
if dt.parse_date(remove_date) is None:
Expand All @@ -95,7 +91,7 @@ def validate_custom_dates(user_input: dict[str, Any]) -> None:
vol.Required(CONF_NAME, default=DEFAULT_NAME): TextSelector(),
vol.Required(CONF_COUNTRY): SelectSelector(
SelectSelectorConfig(
options=list(holidays.list_supported_countries()),
options=list(list_supported_countries()),
mode=SelectSelectorMode.DROPDOWN,
)
),
Expand Down Expand Up @@ -231,7 +227,7 @@ async def async_step_options(
)

schema = await self.hass.async_add_executor_job(
add_province_to_schema, DATA_SCHEMA_OPT, self.data
add_province_to_schema, DATA_SCHEMA_OPT, self.data[CONF_COUNTRY]
)
new_schema = self.add_suggested_values_to_schema(schema, user_input)
return self.async_show_form(
Expand Down Expand Up @@ -282,7 +278,7 @@ async def async_step_init(
return self.async_create_entry(data=combined_input)

schema: vol.Schema = await self.hass.async_add_executor_job(
add_province_to_schema, DATA_SCHEMA_OPT, self.options
add_province_to_schema, DATA_SCHEMA_OPT, self.options[CONF_COUNTRY]
)

new_schema = self.add_suggested_values_to_schema(
Expand Down

0 comments on commit 7e19bb4

Please sign in to comment.