From 9f9114cb4ae4674c87309b363495f18e9a42dd81 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Thu, 24 Nov 2022 21:59:41 +0100 Subject: [PATCH] Simplify SchemaFlowStep typing (#82661) * Simplify SchemaFlowStep typing * Adjust accuweather --- .../components/accuweather/config_flow.py | 5 ++- .../components/derivative/config_flow.py | 9 +++-- homeassistant/components/group/config_flow.py | 4 +-- .../components/integration/config_flow.py | 9 +++-- .../components/min_max/config_flow.py | 9 +++-- .../components/scrape/config_flow.py | 5 ++- .../components/switch_as_x/config_flow.py | 3 +- .../components/threshold/config_flow.py | 5 ++- homeassistant/components/tod/config_flow.py | 9 +++-- .../components/utility_meter/config_flow.py | 7 ++-- .../helpers/schema_config_entry_flow.py | 33 +++++++++++-------- 11 files changed, 48 insertions(+), 50 deletions(-) diff --git a/homeassistant/components/accuweather/config_flow.py b/homeassistant/components/accuweather/config_flow.py index 357327b298b..1480f6c1352 100644 --- a/homeassistant/components/accuweather/config_flow.py +++ b/homeassistant/components/accuweather/config_flow.py @@ -19,7 +19,6 @@ from homeassistant.helpers.aiohttp_client import async_get_clientsession import homeassistant.helpers.config_validation as cv from homeassistant.helpers.schema_config_entry_flow import ( SchemaFlowFormStep, - SchemaFlowMenuStep, SchemaOptionsFlowHandler, ) @@ -30,8 +29,8 @@ OPTIONS_SCHEMA = vol.Schema( vol.Optional(CONF_FORECAST, default=False): bool, } ) -OPTIONS_FLOW: dict[str, SchemaFlowFormStep | SchemaFlowMenuStep] = { - "init": SchemaFlowFormStep(OPTIONS_SCHEMA) +OPTIONS_FLOW = { + "init": SchemaFlowFormStep(OPTIONS_SCHEMA), } diff --git a/homeassistant/components/derivative/config_flow.py b/homeassistant/components/derivative/config_flow.py index eea2b303a12..1b98f4af352 100644 --- a/homeassistant/components/derivative/config_flow.py +++ b/homeassistant/components/derivative/config_flow.py @@ -18,7 +18,6 @@ from homeassistant.helpers import selector from homeassistant.helpers.schema_config_entry_flow import ( SchemaConfigFlowHandler, SchemaFlowFormStep, - SchemaFlowMenuStep, ) from .const import ( @@ -76,12 +75,12 @@ CONFIG_SCHEMA = vol.Schema( } ).extend(OPTIONS_SCHEMA.schema) -CONFIG_FLOW: dict[str, SchemaFlowFormStep | SchemaFlowMenuStep] = { - "user": SchemaFlowFormStep(CONFIG_SCHEMA) +CONFIG_FLOW = { + "user": SchemaFlowFormStep(CONFIG_SCHEMA), } -OPTIONS_FLOW: dict[str, SchemaFlowFormStep | SchemaFlowMenuStep] = { - "init": SchemaFlowFormStep(OPTIONS_SCHEMA) +OPTIONS_FLOW = { + "init": SchemaFlowFormStep(OPTIONS_SCHEMA), } diff --git a/homeassistant/components/group/config_flow.py b/homeassistant/components/group/config_flow.py index f47dbcb3b44..5453d3024f5 100644 --- a/homeassistant/components/group/config_flow.py +++ b/homeassistant/components/group/config_flow.py @@ -115,7 +115,7 @@ def set_group_type(group_type: str) -> Callable[[dict[str, Any]], dict[str, Any] return _set_group_type -CONFIG_FLOW: dict[str, SchemaFlowFormStep | SchemaFlowMenuStep] = { +CONFIG_FLOW = { "user": SchemaFlowMenuStep(GROUP_TYPES), "binary_sensor": SchemaFlowFormStep( BINARY_SENSOR_CONFIG_SCHEMA, set_group_type("binary_sensor") @@ -139,7 +139,7 @@ CONFIG_FLOW: dict[str, SchemaFlowFormStep | SchemaFlowMenuStep] = { } -OPTIONS_FLOW: dict[str, SchemaFlowFormStep | SchemaFlowMenuStep] = { +OPTIONS_FLOW = { "init": SchemaFlowFormStep(None, next_step=choose_options_step), "binary_sensor": SchemaFlowFormStep(binary_sensor_options_schema), "cover": SchemaFlowFormStep(partial(basic_group_options_schema, "cover")), diff --git a/homeassistant/components/integration/config_flow.py b/homeassistant/components/integration/config_flow.py index aab5671f64b..dba3103906f 100644 --- a/homeassistant/components/integration/config_flow.py +++ b/homeassistant/components/integration/config_flow.py @@ -18,7 +18,6 @@ from homeassistant.helpers import selector from homeassistant.helpers.schema_config_entry_flow import ( SchemaConfigFlowHandler, SchemaFlowFormStep, - SchemaFlowMenuStep, ) from .const import ( @@ -89,12 +88,12 @@ CONFIG_SCHEMA = vol.Schema( } ) -CONFIG_FLOW: dict[str, SchemaFlowFormStep | SchemaFlowMenuStep] = { - "user": SchemaFlowFormStep(CONFIG_SCHEMA) +CONFIG_FLOW = { + "user": SchemaFlowFormStep(CONFIG_SCHEMA), } -OPTIONS_FLOW: dict[str, SchemaFlowFormStep | SchemaFlowMenuStep] = { - "init": SchemaFlowFormStep(OPTIONS_SCHEMA) +OPTIONS_FLOW = { + "init": SchemaFlowFormStep(OPTIONS_SCHEMA), } diff --git a/homeassistant/components/min_max/config_flow.py b/homeassistant/components/min_max/config_flow.py index d5c9ce43a99..ecf47dc0c63 100644 --- a/homeassistant/components/min_max/config_flow.py +++ b/homeassistant/components/min_max/config_flow.py @@ -11,7 +11,6 @@ from homeassistant.helpers import selector from homeassistant.helpers.schema_config_entry_flow import ( SchemaConfigFlowHandler, SchemaFlowFormStep, - SchemaFlowMenuStep, ) from .const import CONF_ENTITY_IDS, CONF_ROUND_DIGITS, DOMAIN @@ -49,12 +48,12 @@ CONFIG_SCHEMA = vol.Schema( } ).extend(OPTIONS_SCHEMA.schema) -CONFIG_FLOW: dict[str, SchemaFlowFormStep | SchemaFlowMenuStep] = { - "user": SchemaFlowFormStep(CONFIG_SCHEMA) +CONFIG_FLOW = { + "user": SchemaFlowFormStep(CONFIG_SCHEMA), } -OPTIONS_FLOW: dict[str, SchemaFlowFormStep | SchemaFlowMenuStep] = { - "init": SchemaFlowFormStep(OPTIONS_SCHEMA) +OPTIONS_FLOW = { + "init": SchemaFlowFormStep(OPTIONS_SCHEMA), } diff --git a/homeassistant/components/scrape/config_flow.py b/homeassistant/components/scrape/config_flow.py index b5557daff15..eedc584a394 100644 --- a/homeassistant/components/scrape/config_flow.py +++ b/homeassistant/components/scrape/config_flow.py @@ -39,7 +39,6 @@ from homeassistant.helpers.schema_config_entry_flow import ( SchemaConfigFlowHandler, SchemaFlowError, SchemaFlowFormStep, - SchemaFlowMenuStep, SchemaOptionsFlowHandler, ) from homeassistant.helpers.selector import ( @@ -141,7 +140,7 @@ def validate_sensor_setup(user_input: dict[str, Any]) -> dict[str, Any]: DATA_SCHEMA_RESOURCE = vol.Schema(RESOURCE_SETUP) DATA_SCHEMA_SENSOR = vol.Schema(SENSOR_SETUP) -CONFIG_FLOW: dict[str, SchemaFlowFormStep | SchemaFlowMenuStep] = { +CONFIG_FLOW = { "user": SchemaFlowFormStep( schema=DATA_SCHEMA_RESOURCE, next_step="sensor", @@ -152,7 +151,7 @@ CONFIG_FLOW: dict[str, SchemaFlowFormStep | SchemaFlowMenuStep] = { validate_user_input=validate_sensor_setup, ), } -OPTIONS_FLOW: dict[str, SchemaFlowFormStep | SchemaFlowMenuStep] = { +OPTIONS_FLOW = { "init": SchemaFlowFormStep(DATA_SCHEMA_RESOURCE), } diff --git a/homeassistant/components/switch_as_x/config_flow.py b/homeassistant/components/switch_as_x/config_flow.py index 991f4f33a6b..8b6527eb49e 100644 --- a/homeassistant/components/switch_as_x/config_flow.py +++ b/homeassistant/components/switch_as_x/config_flow.py @@ -11,7 +11,6 @@ from homeassistant.helpers import entity_registry as er, selector from homeassistant.helpers.schema_config_entry_flow import ( SchemaConfigFlowHandler, SchemaFlowFormStep, - SchemaFlowMenuStep, wrapped_entity_config_entry_title, ) @@ -25,7 +24,7 @@ TARGET_DOMAIN_OPTIONS = [ selector.SelectOptionDict(value=Platform.SIREN, label="Siren"), ] -CONFIG_FLOW: dict[str, SchemaFlowFormStep | SchemaFlowMenuStep] = { +CONFIG_FLOW = { "user": SchemaFlowFormStep( vol.Schema( { diff --git a/homeassistant/components/threshold/config_flow.py b/homeassistant/components/threshold/config_flow.py index e3af2e9c567..20cedcfca6b 100644 --- a/homeassistant/components/threshold/config_flow.py +++ b/homeassistant/components/threshold/config_flow.py @@ -12,7 +12,6 @@ from homeassistant.helpers.schema_config_entry_flow import ( SchemaConfigFlowHandler, SchemaFlowError, SchemaFlowFormStep, - SchemaFlowMenuStep, ) from .const import CONF_HYSTERESIS, CONF_LOWER, CONF_UPPER, DEFAULT_HYSTERESIS, DOMAIN @@ -56,11 +55,11 @@ CONFIG_SCHEMA = vol.Schema( } ).extend(OPTIONS_SCHEMA.schema) -CONFIG_FLOW: dict[str, SchemaFlowFormStep | SchemaFlowMenuStep] = { +CONFIG_FLOW = { "user": SchemaFlowFormStep(CONFIG_SCHEMA, validate_user_input=_validate_mode) } -OPTIONS_FLOW: dict[str, SchemaFlowFormStep | SchemaFlowMenuStep] = { +OPTIONS_FLOW = { "init": SchemaFlowFormStep(OPTIONS_SCHEMA, validate_user_input=_validate_mode) } diff --git a/homeassistant/components/tod/config_flow.py b/homeassistant/components/tod/config_flow.py index 5155d15561b..6e21b8046a1 100644 --- a/homeassistant/components/tod/config_flow.py +++ b/homeassistant/components/tod/config_flow.py @@ -11,7 +11,6 @@ from homeassistant.helpers import selector from homeassistant.helpers.schema_config_entry_flow import ( SchemaConfigFlowHandler, SchemaFlowFormStep, - SchemaFlowMenuStep, ) from .const import CONF_AFTER_TIME, CONF_BEFORE_TIME, DOMAIN @@ -29,12 +28,12 @@ CONFIG_SCHEMA = vol.Schema( } ).extend(OPTIONS_SCHEMA.schema) -CONFIG_FLOW: dict[str, SchemaFlowFormStep | SchemaFlowMenuStep] = { - "user": SchemaFlowFormStep(CONFIG_SCHEMA) +CONFIG_FLOW = { + "user": SchemaFlowFormStep(CONFIG_SCHEMA), } -OPTIONS_FLOW: dict[str, SchemaFlowFormStep | SchemaFlowMenuStep] = { - "init": SchemaFlowFormStep(OPTIONS_SCHEMA) +OPTIONS_FLOW = { + "init": SchemaFlowFormStep(OPTIONS_SCHEMA), } diff --git a/homeassistant/components/utility_meter/config_flow.py b/homeassistant/components/utility_meter/config_flow.py index cabd90ba8bd..fbdf302e9b7 100644 --- a/homeassistant/components/utility_meter/config_flow.py +++ b/homeassistant/components/utility_meter/config_flow.py @@ -12,7 +12,6 @@ from homeassistant.helpers.schema_config_entry_flow import ( SchemaConfigFlowHandler, SchemaFlowError, SchemaFlowFormStep, - SchemaFlowMenuStep, ) from .const import ( @@ -93,12 +92,12 @@ CONFIG_SCHEMA = vol.Schema( } ) -CONFIG_FLOW: dict[str, SchemaFlowFormStep | SchemaFlowMenuStep] = { +CONFIG_FLOW = { "user": SchemaFlowFormStep(CONFIG_SCHEMA, validate_user_input=_validate_config) } -OPTIONS_FLOW: dict[str, SchemaFlowFormStep | SchemaFlowMenuStep] = { - "init": SchemaFlowFormStep(OPTIONS_SCHEMA) +OPTIONS_FLOW = { + "init": SchemaFlowFormStep(OPTIONS_SCHEMA), } diff --git a/homeassistant/helpers/schema_config_entry_flow.py b/homeassistant/helpers/schema_config_entry_flow.py index b132e3edd86..526f59bd103 100644 --- a/homeassistant/helpers/schema_config_entry_flow.py +++ b/homeassistant/helpers/schema_config_entry_flow.py @@ -22,20 +22,27 @@ class SchemaFlowError(Exception): @dataclass -class SchemaFlowFormStep: +class SchemaFlowStep: """Define a config or options flow step.""" - # Optional voluptuous schema, or function which returns a schema or None, for - # requesting and validating user input. - # If a function is specified, the function will be passed the handler, which is - # either an instance of SchemaConfigFlowHandler or SchemaOptionsFlowHandler, and the - # union of config entry options and user input from previous steps. - # If schema validation fails, the step will be retried. If the schema is None, no - # user input is requested. + +@dataclass +class SchemaFlowFormStep(SchemaFlowStep): + """Define a config or options flow form step.""" + schema: vol.Schema | Callable[ [SchemaConfigFlowHandler | SchemaOptionsFlowHandler, dict[str, Any]], vol.Schema | None, ] | None + """Optional voluptuous schema, or function which returns a schema or None, for + requesting and validating user input. + + - If a function is specified, the function will be passed the handler, which is + either an instance of SchemaConfigFlowHandler or SchemaOptionsFlowHandler, and the + union of config entry options and user input from previous steps. + - If schema validation fails, the step will be retried. If the schema is None, no + user input is requested. + """ validate_user_input: Callable[[dict[str, Any]], dict[str, Any]] = lambda x: x """Optional function to validate user input. @@ -56,7 +63,7 @@ class SchemaFlowFormStep: @dataclass -class SchemaFlowMenuStep: +class SchemaFlowMenuStep(SchemaFlowStep): """Define a config or options flow menu step.""" # Menu options @@ -69,7 +76,7 @@ class SchemaCommonFlowHandler: def __init__( self, handler: SchemaConfigFlowHandler | SchemaOptionsFlowHandler, - flow: dict[str, SchemaFlowFormStep | SchemaFlowMenuStep], + flow: Mapping[str, SchemaFlowStep], options: dict[str, Any] | None, ) -> None: """Initialize a common handler.""" @@ -210,8 +217,8 @@ class SchemaCommonFlowHandler: class SchemaConfigFlowHandler(config_entries.ConfigFlow): """Handle a schema based config flow.""" - config_flow: dict[str, SchemaFlowFormStep | SchemaFlowMenuStep] - options_flow: dict[str, SchemaFlowFormStep | SchemaFlowMenuStep] | None = None + config_flow: Mapping[str, SchemaFlowStep] + options_flow: Mapping[str, SchemaFlowStep] | None = None VERSION = 1 @@ -311,7 +318,7 @@ class SchemaOptionsFlowHandler(config_entries.OptionsFlowWithConfigEntry): def __init__( self, config_entry: config_entries.ConfigEntry, - options_flow: dict[str, SchemaFlowFormStep | SchemaFlowMenuStep], + options_flow: Mapping[str, SchemaFlowStep], async_options_flow_finished: Callable[[HomeAssistant, Mapping[str, Any]], None] | None = None, ) -> None: