Create helper for File config flow step handling (#117371)
This commit is contained in:
parent
f23419ed35
commit
85e651fd5a
1 changed files with 27 additions and 32 deletions
|
@ -31,20 +31,21 @@ BOOLEAN_SELECTOR = BooleanSelector(BooleanSelectorConfig())
|
||||||
TEMPLATE_SELECTOR = TemplateSelector(TemplateSelectorConfig())
|
TEMPLATE_SELECTOR = TemplateSelector(TemplateSelectorConfig())
|
||||||
TEXT_SELECTOR = TextSelector(TextSelectorConfig(type=TextSelectorType.TEXT))
|
TEXT_SELECTOR = TextSelector(TextSelectorConfig(type=TextSelectorType.TEXT))
|
||||||
|
|
||||||
FILE_SENSOR_SCHEMA = vol.Schema(
|
FILE_FLOW_SCHEMAS = {
|
||||||
{
|
Platform.SENSOR.value: vol.Schema(
|
||||||
vol.Required(CONF_FILE_PATH): TEXT_SELECTOR,
|
{
|
||||||
vol.Optional(CONF_VALUE_TEMPLATE): TEMPLATE_SELECTOR,
|
vol.Required(CONF_FILE_PATH): TEXT_SELECTOR,
|
||||||
vol.Optional(CONF_UNIT_OF_MEASUREMENT): TEXT_SELECTOR,
|
vol.Optional(CONF_VALUE_TEMPLATE): TEMPLATE_SELECTOR,
|
||||||
}
|
vol.Optional(CONF_UNIT_OF_MEASUREMENT): TEXT_SELECTOR,
|
||||||
)
|
}
|
||||||
|
),
|
||||||
FILE_NOTIFY_SCHEMA = vol.Schema(
|
Platform.NOTIFY.value: vol.Schema(
|
||||||
{
|
{
|
||||||
vol.Required(CONF_FILE_PATH): TEXT_SELECTOR,
|
vol.Required(CONF_FILE_PATH): TEXT_SELECTOR,
|
||||||
vol.Optional(CONF_TIMESTAMP, default=False): BOOLEAN_SELECTOR,
|
vol.Optional(CONF_TIMESTAMP, default=False): BOOLEAN_SELECTOR,
|
||||||
}
|
}
|
||||||
)
|
),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class FileConfigFlowHandler(ConfigFlow, domain=DOMAIN):
|
class FileConfigFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||||
|
@ -67,13 +68,13 @@ class FileConfigFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||||
menu_options=["notify", "sensor"],
|
menu_options=["notify", "sensor"],
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_step_notify(
|
async def _async_handle_step(
|
||||||
self, user_input: dict[str, Any] | None = None
|
self, platform: str, user_input: dict[str, Any] | None = None
|
||||||
) -> ConfigFlowResult:
|
) -> ConfigFlowResult:
|
||||||
"""Handle file notifier config flow."""
|
"""Handle file config flow step."""
|
||||||
errors: dict[str, str] = {}
|
errors: dict[str, str] = {}
|
||||||
if user_input:
|
if user_input:
|
||||||
user_input[CONF_PLATFORM] = "notify"
|
user_input[CONF_PLATFORM] = platform
|
||||||
self._async_abort_entries_match(user_input)
|
self._async_abort_entries_match(user_input)
|
||||||
if not await self.validate_file_path(user_input[CONF_FILE_PATH]):
|
if not await self.validate_file_path(user_input[CONF_FILE_PATH]):
|
||||||
errors[CONF_FILE_PATH] = "not_allowed"
|
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_create_entry(data=user_input, title=title)
|
||||||
|
|
||||||
return self.async_show_form(
|
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(
|
async def async_step_sensor(
|
||||||
self, user_input: dict[str, Any] | None = None
|
self, user_input: dict[str, Any] | None = None
|
||||||
) -> ConfigFlowResult:
|
) -> ConfigFlowResult:
|
||||||
"""Handle file sensor config flow."""
|
"""Handle file sensor config flow."""
|
||||||
errors: dict[str, str] = {}
|
return await self._async_handle_step(Platform.SENSOR.value, user_input)
|
||||||
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
|
|
||||||
)
|
|
||||||
|
|
||||||
async def async_step_import(
|
async def async_step_import(
|
||||||
self, import_data: dict[str, Any] | None = None
|
self, import_data: dict[str, Any] | None = None
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue