Simplify SchemaFlowStep typing (#82661)

* Simplify SchemaFlowStep typing

* Adjust accuweather
pull/82670/head
epenet 2022-11-24 21:59:41 +01:00 committed by GitHub
parent 6c024c8875
commit 9f9114cb4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 48 additions and 50 deletions

View File

@ -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),
}

View File

@ -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),
}

View File

@ -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")),

View File

@ -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),
}

View File

@ -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),
}

View File

@ -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),
}

View File

@ -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(
{

View File

@ -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)
}

View File

@ -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),
}

View File

@ -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),
}

View File

@ -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: