From 85e651fd5a2b146322fdf59d7398eb72afd92ad9 Mon Sep 17 00:00:00 2001 From: Jan Bouwhuis Date: Mon, 13 May 2024 22:40:01 +0200 Subject: [PATCH] Create helper for File config flow step handling (#117371) --- homeassistant/components/file/config_flow.py | 59 +++++++++----------- 1 file changed, 27 insertions(+), 32 deletions(-) diff --git a/homeassistant/components/file/config_flow.py b/homeassistant/components/file/config_flow.py index 3b63854b76b..2d729473929 100644 --- a/homeassistant/components/file/config_flow.py +++ b/homeassistant/components/file/config_flow.py @@ -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