Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Use VolDictType to improve schema typing #120417

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions homeassistant/components/asuswrt/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
SchemaOptionsFlowHandler,
)
from homeassistant.helpers.selector import SelectSelector, SelectSelectorConfig
from homeassistant.helpers.typing import VolDictType

from .bridge import AsusWrtBridge
from .const import (
Expand Down Expand Up @@ -143,6 +144,7 @@ def _show_setup_form(self, error: str | None = None) -> ConfigFlowResult:

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,
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/axis/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
)
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
Expand Down Expand Up @@ -63,7 +64,7 @@ def async_get_options_flow(config_entry: ConfigEntry) -> AxisOptionsFlowHandler:
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
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/dlna_dmr/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
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,
Expand Down Expand Up @@ -382,7 +383,7 @@ async def async_step_init(
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.
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/ecovacs/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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

Expand Down Expand Up @@ -181,7 +182,7 @@ async def async_step_auth(
title=user_input[CONF_USERNAME], data=user_input
)

schema = {
schema: VolDictType = {
vol.Required(CONF_USERNAME): selector.TextSelector(
selector.TextSelectorConfig(type=selector.TextSelectorType.TEXT)
),
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/enphase_envoy/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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,
Expand Down Expand Up @@ -69,7 +70,7 @@ def async_get_options_flow(config_entry: ConfigEntry) -> EnvoyOptionsFlowHandler
@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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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
Expand Down Expand Up @@ -555,7 +556,7 @@ def _async_step_pair_show_form(
"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

Expand Down
6 changes: 5 additions & 1 deletion homeassistant/components/http/data_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from aiohttp import web
import voluptuous as vol

from homeassistant.helpers.typing import VolDictType

from .view import HomeAssistantView

_LOGGER = logging.getLogger(__name__)
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/humidifier/device_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
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

Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/ibeacon/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
)
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

Expand Down Expand Up @@ -81,7 +82,7 @@ async def async_step_init(self, user_input: dict | None = None) -> ConfigFlowRes
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},
Expand Down
6 changes: 3 additions & 3 deletions homeassistant/components/knx/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
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,
Expand Down Expand Up @@ -368,7 +368,7 @@ async def async_step_manual_tunnel(
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
),
Expand Down Expand Up @@ -694,7 +694,7 @@ async def async_step_routing(
router for router in routers if router.routing_requires_secure
)

fields = {
fields: VolDictType = {
vol.Required(
CONF_KNX_INDIVIDUAL_ADDRESS, default=_individual_address
): _IA_SELECTOR,
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/light/device_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
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,
Expand Down Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions homeassistant/components/mysensors/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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,
Expand Down Expand Up @@ -153,7 +154,7 @@ async def async_step_gw_serial(
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,
Expand All @@ -164,9 +165,8 @@ async def async_step_gw_serial(
}
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(
Expand All @@ -182,7 +182,7 @@ async def async_step_gw_tcp(
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,
Expand All @@ -192,8 +192,9 @@ async def async_step_gw_tcp(
}
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():
Expand Down Expand Up @@ -243,7 +244,7 @@ async def async_step_gw_mqtt(
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,
Expand All @@ -254,9 +255,8 @@ async def async_step_gw_mqtt(
}
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
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/nmap_tracker/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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,
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/opower/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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

Expand Down Expand Up @@ -151,7 +152,7 @@ async def async_step_reauth_confirm(
)
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,
}
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/proximity/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
NumberSelector,
NumberSelectorConfig,
)
from homeassistant.helpers.typing import VolDictType
from homeassistant.util import slugify

from .const import (
Expand All @@ -37,7 +38,7 @@
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, [])
Expand Down
4 changes: 3 additions & 1 deletion homeassistant/components/rfxtrx/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
entity_registry as er,
)
from homeassistant.helpers.event import async_track_state_change_event
from homeassistant.helpers.typing import VolDictType

from . import (
DOMAIN,
Expand Down Expand Up @@ -245,9 +246,10 @@ async def async_step_set_device_options(

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(
Expand Down
8 changes: 3 additions & 5 deletions homeassistant/components/synology_dsm/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
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 (
Expand Down Expand Up @@ -79,17 +79,15 @@ 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))

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,
Expand Down
Loading