Remove NONE_SENTINEL in favor of optional select in workday (#101280)

pull/101815/head
Robert Resch 2023-10-11 13:29:42 +02:00 committed by GitHub
parent 0b2b486754
commit 87c82fb00f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 51 deletions

View File

@ -43,23 +43,23 @@ from .const import (
LOGGER,
)
NONE_SENTINEL = "none"
def add_province_to_schema(
schema: vol.Schema,
country: str,
country: str | None,
) -> vol.Schema:
"""Update schema with province from country."""
if not country:
return schema
all_countries = list_supported_countries()
if not all_countries.get(country):
return schema
province_list = [NONE_SENTINEL, *all_countries[country]]
add_schema = {
vol.Optional(CONF_PROVINCE, default=NONE_SENTINEL): SelectSelector(
vol.Optional(CONF_PROVINCE): SelectSelector(
SelectSelectorConfig(
options=province_list,
options=all_countries[country],
mode=SelectSelectorMode.DROPDOWN,
translation_key=CONF_PROVINCE,
)
@ -90,7 +90,7 @@ def validate_custom_dates(user_input: dict[str, Any]) -> None:
raise AddDatesError("Incorrect date")
year: int = dt_util.now().year
if country := user_input[CONF_COUNTRY]:
if country := user_input.get(CONF_COUNTRY):
cls = country_holidays(country)
obj_holidays = country_holidays(
country=country,
@ -113,9 +113,9 @@ def validate_custom_dates(user_input: dict[str, Any]) -> None:
DATA_SCHEMA_SETUP = vol.Schema(
{
vol.Required(CONF_NAME, default=DEFAULT_NAME): TextSelector(),
vol.Optional(CONF_COUNTRY, default=NONE_SENTINEL): SelectSelector(
vol.Optional(CONF_COUNTRY): SelectSelector(
SelectSelectorConfig(
options=[NONE_SENTINEL, *list(list_supported_countries())],
options=list(list_supported_countries()),
mode=SelectSelectorMode.DROPDOWN,
translation_key=CONF_COUNTRY,
)
@ -202,11 +202,6 @@ class WorkdayConfigFlow(ConfigFlow, domain=DOMAIN):
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
try:
await self.hass.async_add_executor_job(
validate_custom_dates, combined_input
@ -221,13 +216,13 @@ class WorkdayConfigFlow(ConfigFlow, domain=DOMAIN):
errors["remove_holidays"] = "remove_holiday_range_error"
abort_match = {
CONF_COUNTRY: combined_input[CONF_COUNTRY],
CONF_COUNTRY: combined_input.get(CONF_COUNTRY),
CONF_EXCLUDES: combined_input[CONF_EXCLUDES],
CONF_OFFSET: combined_input[CONF_OFFSET],
CONF_WORKDAYS: combined_input[CONF_WORKDAYS],
CONF_ADD_HOLIDAYS: combined_input[CONF_ADD_HOLIDAYS],
CONF_REMOVE_HOLIDAYS: combined_input[CONF_REMOVE_HOLIDAYS],
CONF_PROVINCE: combined_input[CONF_PROVINCE],
CONF_PROVINCE: combined_input.get(CONF_PROVINCE),
}
LOGGER.debug("abort_check in options with %s", combined_input)
self._async_abort_entries_match(abort_match)
@ -242,7 +237,7 @@ class WorkdayConfigFlow(ConfigFlow, domain=DOMAIN):
)
schema = await self.hass.async_add_executor_job(
add_province_to_schema, DATA_SCHEMA_OPT, self.data[CONF_COUNTRY]
add_province_to_schema, DATA_SCHEMA_OPT, self.data.get(CONF_COUNTRY)
)
new_schema = self.add_suggested_values_to_schema(schema, user_input)
return self.async_show_form(
@ -251,7 +246,7 @@ class WorkdayConfigFlow(ConfigFlow, domain=DOMAIN):
errors=errors,
description_placeholders={
"name": self.data[CONF_NAME],
"country": self.data[CONF_COUNTRY],
"country": self.data.get(CONF_COUNTRY),
},
)
@ -267,8 +262,9 @@ class WorkdayOptionsFlowHandler(OptionsFlowWithConfigEntry):
if user_input is not None:
combined_input: dict[str, Any] = {**self.options, **user_input}
if combined_input.get(CONF_PROVINCE, NONE_SENTINEL) == NONE_SENTINEL:
combined_input[CONF_PROVINCE] = None
if CONF_PROVINCE not in user_input:
# Province not present, delete old value (if present) too
combined_input.pop(CONF_PROVINCE, None)
try:
await self.hass.async_add_executor_job(
@ -287,13 +283,13 @@ class WorkdayOptionsFlowHandler(OptionsFlowWithConfigEntry):
try:
self._async_abort_entries_match(
{
CONF_COUNTRY: self._config_entry.options[CONF_COUNTRY],
CONF_COUNTRY: self._config_entry.options.get(CONF_COUNTRY),
CONF_EXCLUDES: combined_input[CONF_EXCLUDES],
CONF_OFFSET: combined_input[CONF_OFFSET],
CONF_WORKDAYS: combined_input[CONF_WORKDAYS],
CONF_ADD_HOLIDAYS: combined_input[CONF_ADD_HOLIDAYS],
CONF_REMOVE_HOLIDAYS: combined_input[CONF_REMOVE_HOLIDAYS],
CONF_PROVINCE: combined_input[CONF_PROVINCE],
CONF_PROVINCE: combined_input.get(CONF_PROVINCE),
}
)
except AbortFlow as err:
@ -302,7 +298,7 @@ class WorkdayOptionsFlowHandler(OptionsFlowWithConfigEntry):
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[CONF_COUNTRY]
add_province_to_schema, DATA_SCHEMA_OPT, self.options.get(CONF_COUNTRY)
)
new_schema = self.add_suggested_values_to_schema(
@ -315,7 +311,7 @@ class WorkdayOptionsFlowHandler(OptionsFlowWithConfigEntry):
errors=errors,
description_placeholders={
"name": self.options[CONF_NAME],
"country": self.options[CONF_COUNTRY],
"country": self.options.get(CONF_COUNTRY),
},
)

View File

@ -18,7 +18,6 @@ from homeassistant.helpers.selector import (
SelectSelectorMode,
)
from .config_flow import NONE_SENTINEL
from .const import CONF_PROVINCE
@ -75,9 +74,8 @@ class CountryFixFlow(RepairsFlow):
self, user_input: dict[str, Any] | None = None
) -> data_entry_flow.FlowResult:
"""Handle the province step of a fix flow."""
if user_input and user_input.get(CONF_PROVINCE):
if user_input.get(CONF_PROVINCE, NONE_SENTINEL) == NONE_SENTINEL:
user_input[CONF_PROVINCE] = None
if user_input is not None:
user_input.setdefault(CONF_PROVINCE, None)
options = dict(self.entry.options)
new_options = {**options, **user_input, CONF_COUNTRY: self.country}
self.hass.config_entries.async_update_entry(self.entry, options=new_options)
@ -90,9 +88,9 @@ class CountryFixFlow(RepairsFlow):
step_id="province",
data_schema=vol.Schema(
{
vol.Optional(CONF_PROVINCE, default=NONE_SENTINEL): SelectSelector(
vol.Optional(CONF_PROVINCE): SelectSelector(
SelectSelectorConfig(
options=[NONE_SENTINEL, *country_provinces],
options=country_provinces,
mode=SelectSelectorMode.DROPDOWN,
translation_key=CONF_PROVINCE,
)

View File

@ -9,7 +9,6 @@ from homeassistant.components.workday.const import (
CONF_COUNTRY,
CONF_EXCLUDES,
CONF_OFFSET,
CONF_PROVINCE,
CONF_REMOVE_HOLIDAYS,
CONF_WORKDAYS,
DEFAULT_EXCLUDES,
@ -50,7 +49,6 @@ async def test_form(hass: HomeAssistant) -> None:
CONF_WORKDAYS: DEFAULT_WORKDAYS,
CONF_ADD_HOLIDAYS: [],
CONF_REMOVE_HOLIDAYS: [],
CONF_PROVINCE: "none",
},
)
await hass.async_block_till_done()
@ -65,7 +63,6 @@ async def test_form(hass: HomeAssistant) -> None:
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": [],
"remove_holidays": [],
"province": None,
}
@ -81,7 +78,6 @@ async def test_form_no_country(hass: HomeAssistant) -> None:
result["flow_id"],
{
CONF_NAME: "Workday Sensor",
CONF_COUNTRY: "none",
},
)
await hass.async_block_till_done()
@ -101,13 +97,11 @@ async def test_form_no_country(hass: HomeAssistant) -> None:
assert result3["title"] == "Workday Sensor"
assert result3["options"] == {
"name": "Workday Sensor",
"country": None,
"excludes": ["sat", "sun", "holiday"],
"days_offset": 0,
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": [],
"remove_holidays": [],
"province": None,
}
@ -149,7 +143,6 @@ async def test_form_no_subdivision(hass: HomeAssistant) -> None:
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": [],
"remove_holidays": [],
"province": None,
}
@ -166,7 +159,6 @@ async def test_options_form(hass: HomeAssistant) -> None:
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": [],
"remove_holidays": [],
"province": None,
},
)
@ -221,7 +213,6 @@ async def test_form_incorrect_dates(hass: HomeAssistant) -> None:
CONF_WORKDAYS: DEFAULT_WORKDAYS,
CONF_ADD_HOLIDAYS: ["2022-xx-12"],
CONF_REMOVE_HOLIDAYS: [],
CONF_PROVINCE: "none",
},
)
await hass.async_block_till_done()
@ -235,7 +226,6 @@ async def test_form_incorrect_dates(hass: HomeAssistant) -> None:
CONF_WORKDAYS: DEFAULT_WORKDAYS,
CONF_ADD_HOLIDAYS: ["2022-12-12"],
CONF_REMOVE_HOLIDAYS: ["Does not exist"],
CONF_PROVINCE: "none",
},
)
await hass.async_block_till_done()
@ -250,7 +240,6 @@ async def test_form_incorrect_dates(hass: HomeAssistant) -> None:
CONF_WORKDAYS: DEFAULT_WORKDAYS,
CONF_ADD_HOLIDAYS: ["2022-12-12"],
CONF_REMOVE_HOLIDAYS: ["Weihnachtstag"],
CONF_PROVINCE: "none",
},
)
await hass.async_block_till_done()
@ -265,7 +254,6 @@ async def test_form_incorrect_dates(hass: HomeAssistant) -> None:
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": ["2022-12-12"],
"remove_holidays": ["Weihnachtstag"],
"province": None,
}
@ -282,7 +270,6 @@ async def test_options_form_incorrect_dates(hass: HomeAssistant) -> None:
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": [],
"remove_holidays": [],
"province": None,
},
)
@ -383,7 +370,6 @@ async def test_options_form_abort_duplicate(hass: HomeAssistant) -> None:
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": [],
"remove_holidays": [],
"province": "none",
},
)
@ -415,7 +401,6 @@ async def test_form_incorrect_date_range(hass: HomeAssistant) -> None:
CONF_WORKDAYS: DEFAULT_WORKDAYS,
CONF_ADD_HOLIDAYS: ["2022-12-12", "2022-12-30,2022-12-32"],
CONF_REMOVE_HOLIDAYS: [],
CONF_PROVINCE: "none",
},
)
await hass.async_block_till_done()
@ -429,7 +414,6 @@ async def test_form_incorrect_date_range(hass: HomeAssistant) -> None:
CONF_WORKDAYS: DEFAULT_WORKDAYS,
CONF_ADD_HOLIDAYS: ["2022-12-12"],
CONF_REMOVE_HOLIDAYS: ["2022-12-25", "2022-12-30,2022-12-32"],
CONF_PROVINCE: "none",
},
)
await hass.async_block_till_done()
@ -444,7 +428,6 @@ async def test_form_incorrect_date_range(hass: HomeAssistant) -> None:
CONF_WORKDAYS: DEFAULT_WORKDAYS,
CONF_ADD_HOLIDAYS: ["2022-12-12", "2022-12-01,2022-12-10"],
CONF_REMOVE_HOLIDAYS: ["2022-12-25", "2022-12-30,2022-12-31"],
CONF_PROVINCE: "none",
},
)
await hass.async_block_till_done()
@ -459,7 +442,6 @@ async def test_form_incorrect_date_range(hass: HomeAssistant) -> None:
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": ["2022-12-12", "2022-12-01,2022-12-10"],
"remove_holidays": ["2022-12-25", "2022-12-30,2022-12-31"],
"province": None,
}
@ -476,7 +458,6 @@ async def test_options_form_incorrect_date_ranges(hass: HomeAssistant) -> None:
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": [],
"remove_holidays": [],
"province": None,
},
)

View File

@ -126,7 +126,7 @@ async def test_bad_country_none(
data = await resp.json()
url = RepairsFlowResourceView.url.format(flow_id=flow_id)
resp = await client.post(url, json={"province": "none"})
resp = await client.post(url, json={})
assert resp.status == HTTPStatus.OK
data = await resp.json()
@ -303,7 +303,7 @@ async def test_bad_province_none(
assert data["step_id"] == "province"
url = RepairsFlowResourceView.url.format(flow_id=flow_id)
resp = await client.post(url, json={"province": "none"})
resp = await client.post(url, json={})
assert resp.status == HTTPStatus.OK
data = await resp.json()