Create helper for File config flow step handling (#117371)

This commit is contained in:
Jan Bouwhuis 2024-05-13 22:40:01 +02:00 committed by GitHub
parent f23419ed35
commit 85e651fd5a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -31,20 +31,21 @@ BOOLEAN_SELECTOR = BooleanSelector(BooleanSelectorConfig())
TEMPLATE_SELECTOR = TemplateSelector(TemplateSelectorConfig())
TEXT_SELECTOR = TextSelector(TextSelectorConfig(type=TextSelectorType.TEXT))
FILE_SENSOR_SCHEMA = vol.Schema(
{
vol.Required(CONF_FILE_PATH): TEXT_SELECTOR,
vol.Optional(CONF_VALUE_TEMPLATE): TEMPLATE_SELECTOR,
vol.Optional(CONF_UNIT_OF_MEASUREMENT): TEXT_SELECTOR,
}
)
FILE_NOTIFY_SCHEMA = vol.Schema(
{
vol.Required(CONF_FILE_PATH): TEXT_SELECTOR,
vol.Optional(CONF_TIMESTAMP, default=False): BOOLEAN_SELECTOR,
}
)
FILE_FLOW_SCHEMAS = {
Platform.SENSOR.value: vol.Schema(
{
vol.Required(CONF_FILE_PATH): TEXT_SELECTOR,
vol.Optional(CONF_VALUE_TEMPLATE): TEMPLATE_SELECTOR,
vol.Optional(CONF_UNIT_OF_MEASUREMENT): TEXT_SELECTOR,
}
),
Platform.NOTIFY.value: vol.Schema(
{
vol.Required(CONF_FILE_PATH): TEXT_SELECTOR,
vol.Optional(CONF_TIMESTAMP, default=False): BOOLEAN_SELECTOR,
}
),
}
class FileConfigFlowHandler(ConfigFlow, domain=DOMAIN):
@ -67,13 +68,13 @@ class FileConfigFlowHandler(ConfigFlow, domain=DOMAIN):
menu_options=["notify", "sensor"],
)
async def async_step_notify(
self, user_input: dict[str, Any] | None = None
async def _async_handle_step(
self, platform: str, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle file notifier config flow."""
"""Handle file config flow step."""
errors: dict[str, str] = {}
if user_input:
user_input[CONF_PLATFORM] = "notify"
user_input[CONF_PLATFORM] = platform
self._async_abort_entries_match(user_input)
if not await self.validate_file_path(user_input[CONF_FILE_PATH]):
errors[CONF_FILE_PATH] = "not_allowed"
@ -82,26 +83,20 @@ class FileConfigFlowHandler(ConfigFlow, domain=DOMAIN):
return self.async_create_entry(data=user_input, title=title)
return self.async_show_form(
step_id="notify", data_schema=FILE_NOTIFY_SCHEMA, errors=errors
step_id=platform, data_schema=FILE_FLOW_SCHEMAS[platform], errors=errors
)
async def async_step_notify(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle file notifier config flow."""
return await self._async_handle_step(Platform.NOTIFY.value, user_input)
async def async_step_sensor(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle file sensor config flow."""
errors: dict[str, str] = {}
if user_input:
user_input[CONF_PLATFORM] = "sensor"
self._async_abort_entries_match(user_input)
if not await self.validate_file_path(user_input[CONF_FILE_PATH]):
errors[CONF_FILE_PATH] = "not_allowed"
else:
title = f"{DEFAULT_NAME} [{user_input[CONF_FILE_PATH]}]"
return self.async_create_entry(data=user_input, title=title)
return self.async_show_form(
step_id="sensor", data_schema=FILE_SENSOR_SCHEMA, errors=errors
)
return await self._async_handle_step(Platform.SENSOR.value, user_input)
async def async_step_import(
self, import_data: dict[str, Any] | None = None