Minor cleanup config flow Workday (#92163)

This commit is contained in:
G Johansson 2023-05-07 00:55:06 +02:00 committed by GitHub
parent c624e50b60
commit 7e19bb4ee0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,8 +3,7 @@ from __future__ import annotations
from typing import Any from typing import Any
import holidays from holidays import country_holidays, list_supported_countries
from holidays import HolidayBase
import voluptuous as vol import voluptuous as vol
from homeassistant.config_entries import ( from homeassistant.config_entries import (
@ -48,15 +47,14 @@ NONE_SENTINEL = "none"
def add_province_to_schema( def add_province_to_schema(
schema: vol.Schema, schema: vol.Schema,
options: dict[str, Any], country: str,
) -> vol.Schema: ) -> vol.Schema:
"""Update schema with province from country.""" """Update schema with province from country."""
year: int = dt.now().year all_countries = list_supported_countries()
obj_holidays: HolidayBase = getattr(holidays, options[CONF_COUNTRY])(years=year) if not all_countries[country]:
if not obj_holidays.subdivisions:
return schema return schema
province_list = [NONE_SENTINEL, *obj_holidays.subdivisions] province_list = [NONE_SENTINEL, *all_countries[country]]
add_schema = { add_schema = {
vol.Optional(CONF_PROVINCE, default=NONE_SENTINEL): SelectSelector( vol.Optional(CONF_PROVINCE, default=NONE_SENTINEL): SelectSelector(
SelectSelectorConfig( SelectSelectorConfig(
@ -78,11 +76,9 @@ def validate_custom_dates(user_input: dict[str, Any]) -> None:
raise AddDatesError("Incorrect date") raise AddDatesError("Incorrect date")
year: int = dt.now().year year: int = dt.now().year
obj_holidays: HolidayBase = getattr(holidays, user_input[CONF_COUNTRY])(years=year) obj_holidays = country_holidays(
if user_input.get(CONF_PROVINCE): user_input[CONF_COUNTRY], user_input.get(CONF_PROVINCE), year
obj_holidays = getattr(holidays, user_input[CONF_COUNTRY])( )
subdiv=user_input[CONF_PROVINCE], years=year
)
for remove_date in user_input[CONF_REMOVE_HOLIDAYS]: for remove_date in user_input[CONF_REMOVE_HOLIDAYS]:
if dt.parse_date(remove_date) is None: if dt.parse_date(remove_date) is None:
@ -95,7 +91,7 @@ DATA_SCHEMA_SETUP = vol.Schema(
vol.Required(CONF_NAME, default=DEFAULT_NAME): TextSelector(), vol.Required(CONF_NAME, default=DEFAULT_NAME): TextSelector(),
vol.Required(CONF_COUNTRY): SelectSelector( vol.Required(CONF_COUNTRY): SelectSelector(
SelectSelectorConfig( SelectSelectorConfig(
options=list(holidays.list_supported_countries()), options=list(list_supported_countries()),
mode=SelectSelectorMode.DROPDOWN, mode=SelectSelectorMode.DROPDOWN,
) )
), ),
@ -231,7 +227,7 @@ class WorkdayConfigFlow(ConfigFlow, domain=DOMAIN):
) )
schema = await self.hass.async_add_executor_job( 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) new_schema = self.add_suggested_values_to_schema(schema, user_input)
return self.async_show_form( return self.async_show_form(
@ -282,7 +278,7 @@ class WorkdayOptionsFlowHandler(OptionsFlowWithConfigEntry):
return self.async_create_entry(data=combined_input) return self.async_create_entry(data=combined_input)
schema: vol.Schema = await self.hass.async_add_executor_job( 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( new_schema = self.add_suggested_values_to_schema(