Skip to content

Commit

Permalink
Create helper for File config flow step handling (#117371)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbouwh committed May 13, 2024
1 parent f23419e commit 85e651f
Showing 1 changed file with 27 additions and 32 deletions.
59 changes: 27 additions & 32 deletions homeassistant/components/file/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,21 @@
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):
Expand All @@ -67,13 +68,13 @@ async def async_step_user(
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"
Expand All @@ -82,26 +83,20 @@ async def async_step_notify(
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
Expand Down

0 comments on commit 85e651f

Please sign in to comment.