Allow workday sensor to be configured without a country (#93048)

* Merge branch 'dev' into workday_without_country

* ruff

* remove province check

* Remove not needed test

* Mod config flow

---------

Co-authored-by: G Johansson <goran.johansson@shiftit.se>
This commit is contained in:
Scott Colby 2023-09-24 10:13:45 -04:00 committed by GitHub
parent b19a0fb2e9
commit 49715f300a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 126 additions and 22 deletions

View file

@ -52,7 +52,7 @@ def add_province_to_schema(
) -> vol.Schema:
"""Update schema with province from country."""
all_countries = list_supported_countries()
if not all_countries[country]:
if not all_countries.get(country):
return schema
province_list = [NONE_SENTINEL, *all_countries[country]]
@ -71,19 +71,21 @@ def add_province_to_schema(
def validate_custom_dates(user_input: dict[str, Any]) -> None:
"""Validate custom dates for add/remove holidays."""
for add_date in user_input[CONF_ADD_HOLIDAYS]:
if dt_util.parse_date(add_date) is None:
raise AddDatesError("Incorrect date")
cls: HolidayBase = country_holidays(user_input[CONF_COUNTRY])
year: int = dt_util.now().year
obj_holidays: HolidayBase = country_holidays(
user_input[CONF_COUNTRY],
subdiv=user_input.get(CONF_PROVINCE),
years=year,
language=cls.default_language,
)
if country := user_input[CONF_COUNTRY]:
cls = country_holidays(country)
obj_holidays = country_holidays(
country=country,
subdiv=user_input.get(CONF_PROVINCE),
years=year,
language=cls.default_language,
)
else:
obj_holidays = HolidayBase(years=year)
for remove_date in user_input[CONF_REMOVE_HOLIDAYS]:
if dt_util.parse_date(remove_date) is None:
@ -94,10 +96,11 @@ def validate_custom_dates(user_input: dict[str, Any]) -> None:
DATA_SCHEMA_SETUP = vol.Schema(
{
vol.Required(CONF_NAME, default=DEFAULT_NAME): TextSelector(),
vol.Required(CONF_COUNTRY): SelectSelector(
vol.Optional(CONF_COUNTRY, default=NONE_SENTINEL): SelectSelector(
SelectSelectorConfig(
options=list(list_supported_countries()),
options=[NONE_SENTINEL, *list(list_supported_countries())],
mode=SelectSelectorMode.DROPDOWN,
translation_key=CONF_COUNTRY,
)
),
}
@ -208,6 +211,9 @@ class WorkdayConfigFlow(ConfigFlow, domain=DOMAIN):
errors: dict[str, str] = {}
if user_input is not None:
combined_input: dict[str, Any] = {**self.data, **user_input}
if combined_input.get(CONF_COUNTRY, NONE_SENTINEL) == NONE_SENTINEL:
combined_input[CONF_COUNTRY] = None
if combined_input.get(CONF_PROVINCE, NONE_SENTINEL) == NONE_SENTINEL:
combined_input[CONF_PROVINCE] = None