Use VolDictType to improve schema typing (#120417)

This commit is contained in:
Marc Mueller 2024-06-25 15:15:59 +02:00 committed by GitHub
parent aa05f73210
commit bcd1243686
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
19 changed files with 54 additions and 37 deletions

View File

@ -32,6 +32,7 @@ from homeassistant.helpers.schema_config_entry_flow import (
SchemaOptionsFlowHandler,
)
from homeassistant.helpers.selector import SelectSelector, SelectSelectorConfig
from homeassistant.helpers.typing import VolDictType
from .bridge import AsusWrtBridge
from .const import (
@ -143,6 +144,7 @@ class AsusWrtFlowHandler(ConfigFlow, domain=DOMAIN):
user_input = self._config_data
add_schema: VolDictType
if self.show_advanced_options:
add_schema = {
vol.Exclusive(CONF_PASSWORD, PASS_KEY, PASS_KEY_MSG): str,

View File

@ -30,6 +30,7 @@ from homeassistant.const import (
)
from homeassistant.core import callback
from homeassistant.helpers.device_registry import format_mac
from homeassistant.helpers.typing import VolDictType
from homeassistant.util.network import is_link_local
from . import AxisConfigEntry
@ -63,7 +64,7 @@ class AxisFlowHandler(ConfigFlow, domain=AXIS_DOMAIN):
def __init__(self) -> None:
"""Initialize the Axis config flow."""
self.config: dict[str, Any] = {}
self.discovery_schema: dict[vol.Required, type[str | int]] | None = None
self.discovery_schema: VolDictType | None = None
async def async_step_user(
self, user_input: dict[str, Any] | None = None

View File

@ -27,6 +27,7 @@ from homeassistant.const import CONF_DEVICE_ID, CONF_HOST, CONF_MAC, CONF_TYPE,
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import IntegrationError
from homeassistant.helpers import config_validation as cv, device_registry as dr
from homeassistant.helpers.typing import VolDictType
from .const import (
CONF_BROWSE_UNFILTERED,
@ -382,7 +383,7 @@ class DlnaDmrOptionsFlowHandler(OptionsFlow):
if not errors:
return self.async_create_entry(title="", data=options)
fields = {}
fields: VolDictType = {}
def _add_with_suggestion(key: str, validator: Callable | type[bool]) -> None:
"""Add a field to with a suggested value.

View File

@ -22,6 +22,7 @@ from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant
from homeassistant.data_entry_flow import AbortFlow
from homeassistant.helpers import aiohttp_client, selector
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from homeassistant.helpers.typing import VolDictType
from homeassistant.loader import async_get_issue_tracker
from homeassistant.util.ssl import get_default_no_verify_context
@ -181,7 +182,7 @@ class EcovacsConfigFlow(ConfigFlow, domain=DOMAIN):
title=user_input[CONF_USERNAME], data=user_input
)
schema = {
schema: VolDictType = {
vol.Required(CONF_USERNAME): selector.TextSelector(
selector.TextSelectorConfig(type=selector.TextSelectorType.TEXT)
),

View File

@ -21,6 +21,7 @@ from homeassistant.config_entries import (
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.httpx_client import get_async_client
from homeassistant.helpers.typing import VolDictType
from .const import (
DOMAIN,
@ -69,7 +70,7 @@ class EnphaseConfigFlow(ConfigFlow, domain=DOMAIN):
@callback
def _async_generate_schema(self) -> vol.Schema:
"""Generate schema."""
schema = {}
schema: VolDictType = {}
if self.ip_address:
schema[vol.Required(CONF_HOST, default=self.ip_address)] = vol.In(

View File

@ -24,6 +24,7 @@ from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.core import callback
from homeassistant.data_entry_flow import AbortFlow
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.typing import VolDictType
from .const import DOMAIN, KNOWN_DEVICES
from .storage import async_get_entity_storage
@ -555,7 +556,7 @@ class HomekitControllerFlowHandler(ConfigFlow, domain=DOMAIN):
"category": formatted_category(self.category),
}
schema = {vol.Required("pairing_code"): vol.All(str, vol.Strip)}
schema: VolDictType = {vol.Required("pairing_code"): vol.All(str, vol.Strip)}
if errors and errors.get("pairing_code") == "insecure_setup_code":
schema[vol.Optional("allow_insecure_setup_codes")] = bool

View File

@ -11,6 +11,8 @@ from typing import Any, Concatenate
from aiohttp import web
import voluptuous as vol
from homeassistant.helpers.typing import VolDictType
from .view import HomeAssistantView
_LOGGER = logging.getLogger(__name__)
@ -25,7 +27,9 @@ class RequestDataValidator:
Will return a 400 if no JSON provided or doesn't match schema.
"""
def __init__(self, schema: vol.Schema, allow_empty: bool = False) -> None:
def __init__(
self, schema: VolDictType | vol.Schema, allow_empty: bool = False
) -> None:
"""Initialize the decorator."""
if isinstance(schema, dict):
schema = vol.Schema(schema)

View File

@ -22,7 +22,7 @@ from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import get_capability, get_supported_features
from homeassistant.helpers.typing import ConfigType, TemplateVarsType
from homeassistant.helpers.typing import ConfigType, TemplateVarsType, VolDictType
from . import DOMAIN, const
@ -114,7 +114,7 @@ async def async_get_action_capabilities(
"""List action capabilities."""
action_type = config[CONF_TYPE]
fields = {}
fields: VolDictType = {}
if action_type == "set_humidity":
fields[vol.Required(const.ATTR_HUMIDITY)] = vol.Coerce(int)

View File

@ -16,6 +16,7 @@ from homeassistant.config_entries import (
)
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.typing import VolDictType
from .const import CONF_ALLOW_NAMELESS_UUIDS, DOMAIN
@ -81,7 +82,7 @@ class IBeaconOptionsFlow(OptionsFlow):
data = {CONF_ALLOW_NAMELESS_UUIDS: list(updated_uuids)}
return self.async_create_entry(title="", data=data)
schema = {
schema: VolDictType = {
vol.Optional(
"new_uuid",
description={"suggested_value": new_uuid},

View File

@ -29,7 +29,7 @@ from homeassistant.config_entries import (
from homeassistant.const import CONF_HOST, CONF_PORT
from homeassistant.core import callback
from homeassistant.helpers import selector
from homeassistant.helpers.typing import UNDEFINED
from homeassistant.helpers.typing import UNDEFINED, VolDictType
from .const import (
CONF_KNX_AUTOMATIC,
@ -368,7 +368,7 @@ class KNXCommonFlow(ABC, ConfigEntryBaseFlow):
CONF_KNX_ROUTE_BACK, not bool(self._selected_tunnel)
)
fields = {
fields: VolDictType = {
vol.Required(CONF_KNX_TUNNELING_TYPE, default=default_type): vol.In(
CONF_KNX_TUNNELING_TYPE_LABELS
),
@ -694,7 +694,7 @@ class KNXCommonFlow(ABC, ConfigEntryBaseFlow):
router for router in routers if router.routing_requires_secure
)
fields = {
fields: VolDictType = {
vol.Required(
CONF_KNX_INDIVIDUAL_ADDRESS, default=_individual_address
): _IA_SELECTOR,

View File

@ -21,7 +21,7 @@ from homeassistant.core import Context, HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import config_validation as cv, entity_registry as er
from homeassistant.helpers.entity import get_supported_features
from homeassistant.helpers.typing import ConfigType, TemplateVarsType
from homeassistant.helpers.typing import ConfigType, TemplateVarsType, VolDictType
from . import (
ATTR_BRIGHTNESS_PCT,
@ -150,7 +150,7 @@ async def async_get_action_capabilities(
supported_color_modes = None
supported_features = 0
extra_fields = {}
extra_fields: VolDictType = {}
if brightness_supported(supported_color_modes):
extra_fields[vol.Optional(ATTR_BRIGHTNESS_PCT)] = VALID_BRIGHTNESS_PCT

View File

@ -22,6 +22,7 @@ from homeassistant.const import CONF_DEVICE
from homeassistant.core import callback
from homeassistant.helpers import selector
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.typing import VolDictType
from .const import (
CONF_BAUD_RATE,
@ -153,7 +154,7 @@ class MySensorsConfigFlowHandler(ConfigFlow, domain=DOMAIN):
return self._async_create_entry(user_input)
user_input = user_input or {}
schema = {
schema: VolDictType = {
vol.Required(
CONF_DEVICE, default=user_input.get(CONF_DEVICE, "/dev/ttyACM0")
): str,
@ -164,9 +165,8 @@ class MySensorsConfigFlowHandler(ConfigFlow, domain=DOMAIN):
}
schema.update(_get_schema_common(user_input))
schema = vol.Schema(schema)
return self.async_show_form(
step_id="gw_serial", data_schema=schema, errors=errors
step_id="gw_serial", data_schema=vol.Schema(schema), errors=errors
)
async def async_step_gw_tcp(
@ -182,7 +182,7 @@ class MySensorsConfigFlowHandler(ConfigFlow, domain=DOMAIN):
return self._async_create_entry(user_input)
user_input = user_input or {}
schema = {
schema: VolDictType = {
vol.Required(
CONF_DEVICE, default=user_input.get(CONF_DEVICE, "127.0.0.1")
): str,
@ -192,8 +192,9 @@ class MySensorsConfigFlowHandler(ConfigFlow, domain=DOMAIN):
}
schema.update(_get_schema_common(user_input))
schema = vol.Schema(schema)
return self.async_show_form(step_id="gw_tcp", data_schema=schema, errors=errors)
return self.async_show_form(
step_id="gw_tcp", data_schema=vol.Schema(schema), errors=errors
)
def _check_topic_exists(self, topic: str) -> bool:
for other_config in self._async_current_entries():
@ -243,7 +244,7 @@ class MySensorsConfigFlowHandler(ConfigFlow, domain=DOMAIN):
return self._async_create_entry(user_input)
user_input = user_input or {}
schema = {
schema: VolDictType = {
vol.Required(
CONF_TOPIC_IN_PREFIX, default=user_input.get(CONF_TOPIC_IN_PREFIX, "")
): str,
@ -254,9 +255,8 @@ class MySensorsConfigFlowHandler(ConfigFlow, domain=DOMAIN):
}
schema.update(_get_schema_common(user_input))
schema = vol.Schema(schema)
return self.async_show_form(
step_id="gw_mqtt", data_schema=schema, errors=errors
step_id="gw_mqtt", data_schema=vol.Schema(schema), errors=errors
)
@callback

View File

@ -23,6 +23,7 @@ from homeassistant.config_entries import (
from homeassistant.const import CONF_EXCLUDE, CONF_HOSTS
from homeassistant.core import HomeAssistant, callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.typing import VolDictType
from .const import (
CONF_HOME_INTERVAL,
@ -110,7 +111,7 @@ async def _async_build_schema_with_user_input(
exclude = user_input.get(
CONF_EXCLUDE, await network.async_get_source_ip(hass, MDNS_TARGET_IP)
)
schema = {
schema: VolDictType = {
vol.Required(CONF_HOSTS, default=hosts): str,
vol.Required(
CONF_HOME_INTERVAL, default=user_input.get(CONF_HOME_INTERVAL, 0)

View File

@ -20,6 +20,7 @@ from homeassistant.config_entries import ConfigEntry, ConfigFlow, ConfigFlowResu
from homeassistant.const import CONF_NAME, CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.aiohttp_client import async_create_clientsession
from homeassistant.helpers.typing import VolDictType
from .const import CONF_TOTP_SECRET, CONF_UTILITY, DOMAIN
@ -151,7 +152,7 @@ class OpowerConfigFlow(ConfigFlow, domain=DOMAIN):
)
await self.hass.config_entries.async_reload(self.reauth_entry.entry_id)
return self.async_abort(reason="reauth_successful")
schema = {
schema: VolDictType = {
vol.Required(CONF_USERNAME): self.reauth_entry.data[CONF_USERNAME],
vol.Required(CONF_PASSWORD): str,
}

View File

@ -23,6 +23,7 @@ from homeassistant.helpers.selector import (
NumberSelector,
NumberSelectorConfig,
)
from homeassistant.helpers.typing import VolDictType
from homeassistant.util import slugify
from .const import (
@ -37,7 +38,7 @@ from .const import (
RESULT_SUCCESS = "success"
def _base_schema(user_input: dict[str, Any]) -> vol.Schema:
def _base_schema(user_input: dict[str, Any]) -> VolDictType:
return {
vol.Required(
CONF_TRACKED_ENTITIES, default=user_input.get(CONF_TRACKED_ENTITIES, [])

View File

@ -38,6 +38,7 @@ from homeassistant.helpers import (
entity_registry as er,
)
from homeassistant.helpers.event import async_track_state_change_event
from homeassistant.helpers.typing import VolDictType
from . import (
DOMAIN,
@ -245,9 +246,10 @@ class RfxtrxOptionsFlow(OptionsFlow):
device_data = self._selected_device
data_schema = {}
data_schema: VolDictType = {}
if binary_supported(self._selected_device_object):
off_delay_schema: VolDictType
if device_data.get(CONF_OFF_DELAY):
off_delay_schema = {
vol.Optional(

View File

@ -41,7 +41,7 @@ from homeassistant.core import callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.typing import DiscoveryInfoType
from homeassistant.helpers.typing import DiscoveryInfoType, VolDictType
from homeassistant.util.network import is_ip_address as is_ip
from .const import (
@ -79,7 +79,7 @@ def _reauth_schema() -> vol.Schema:
def _user_schema_with_defaults(user_input: dict[str, Any]) -> vol.Schema:
user_schema = {
user_schema: VolDictType = {
vol.Required(CONF_HOST, default=user_input.get(CONF_HOST, "")): str,
}
user_schema.update(_ordered_shared_schema(user_input))
@ -87,9 +87,7 @@ def _user_schema_with_defaults(user_input: dict[str, Any]) -> vol.Schema:
return vol.Schema(user_schema)
def _ordered_shared_schema(
schema_input: dict[str, Any],
) -> dict[vol.Required | vol.Optional, Any]:
def _ordered_shared_schema(schema_input: dict[str, Any]) -> VolDictType:
return {
vol.Required(CONF_USERNAME, default=schema_input.get(CONF_USERNAME, "")): str,
vol.Required(CONF_PASSWORD, default=schema_input.get(CONF_PASSWORD, "")): str,

View File

@ -4,7 +4,7 @@ from __future__ import annotations
from collections.abc import Sequence
import logging
from typing import Any, Final, cast
from typing import Any, cast
from kasa import SmartBulb, SmartLightStrip
import voluptuous as vol
@ -25,6 +25,7 @@ from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import entity_platform
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import VolDictType
from . import legacy_device_id
from .const import DOMAIN
@ -43,7 +44,7 @@ VAL = vol.Range(min=0, max=100)
TRANSITION = vol.Range(min=0, max=6000)
HSV_SEQUENCE = vol.ExactSequence((HUE, SAT, VAL))
BASE_EFFECT_DICT: Final = {
BASE_EFFECT_DICT: VolDictType = {
vol.Optional("brightness", default=100): vol.All(
vol.Coerce(int), vol.Range(min=0, max=100)
),
@ -58,7 +59,7 @@ BASE_EFFECT_DICT: Final = {
),
}
SEQUENCE_EFFECT_DICT: Final = {
SEQUENCE_EFFECT_DICT: VolDictType = {
**BASE_EFFECT_DICT,
vol.Required("sequence"): vol.All(
cv.ensure_list,
@ -76,7 +77,7 @@ SEQUENCE_EFFECT_DICT: Final = {
),
}
RANDOM_EFFECT_DICT: Final = {
RANDOM_EFFECT_DICT: VolDictType = {
**BASE_EFFECT_DICT,
vol.Optional("fadeoff", default=0): vol.All(
vol.Coerce(int), vol.Range(min=0, max=3000)

View File

@ -38,6 +38,7 @@ from homeassistant.core import HomeAssistant, callback
from homeassistant.data_entry_flow import AbortFlow, FlowManager
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.typing import VolDictType
from . import disconnect_client
from .addon import get_addon_manager
@ -639,7 +640,7 @@ class ZWaveJSConfigFlow(BaseZwaveJSFlow, ConfigFlow, domain=DOMAIN):
CONF_ADDON_LR_S2_AUTHENTICATED_KEY, self.lr_s2_authenticated_key or ""
)
schema = {
schema: VolDictType = {
vol.Optional(CONF_S0_LEGACY_KEY, default=s0_legacy_key): str,
vol.Optional(
CONF_S2_ACCESS_CONTROL_KEY, default=s2_access_control_key