From 6bceb8ec485ab9c7ec9f0a85e669ea35d5932368 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Wed, 26 Jun 2024 22:44:43 +0200 Subject: [PATCH] Add some more VolDictType annotations (#120610) --- homeassistant/components/elkm1/config_flow.py | 4 ++-- homeassistant/components/fritz/config_flow.py | 5 +++-- .../components/generic_thermostat/climate.py | 8 ++++++-- .../components/homeworks/config_flow.py | 5 +++-- .../components/keenetic_ndms2/config_flow.py | 3 ++- homeassistant/components/lcn/schemas.py | 17 +++++++++-------- homeassistant/components/light/__init__.py | 8 +++----- .../components/monoprice/config_flow.py | 3 ++- .../components/motioneye/config_flow.py | 3 ++- homeassistant/components/nina/config_flow.py | 11 +++++++---- homeassistant/components/vera/config_flow.py | 5 ++--- homeassistant/helpers/config_validation.py | 4 ++-- 12 files changed, 43 insertions(+), 33 deletions(-) diff --git a/homeassistant/components/elkm1/config_flow.py b/homeassistant/components/elkm1/config_flow.py index 972b38d2ae9..4ab8d1fe181 100644 --- a/homeassistant/components/elkm1/config_flow.py +++ b/homeassistant/components/elkm1/config_flow.py @@ -21,7 +21,7 @@ from homeassistant.const import ( ) from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import device_registry as dr -from homeassistant.helpers.typing import DiscoveryInfoType +from homeassistant.helpers.typing import DiscoveryInfoType, VolDictType from homeassistant.util import slugify from homeassistant.util.network import is_ip_address @@ -52,7 +52,7 @@ PROTOCOL_MAP = { VALIDATE_TIMEOUT = 35 -BASE_SCHEMA = { +BASE_SCHEMA: VolDictType = { vol.Optional(CONF_USERNAME, default=""): str, vol.Optional(CONF_PASSWORD, default=""): str, } diff --git a/homeassistant/components/fritz/config_flow.py b/homeassistant/components/fritz/config_flow.py index 4cdd4c19c1b..fbc324fde2b 100644 --- a/homeassistant/components/fritz/config_flow.py +++ b/homeassistant/components/fritz/config_flow.py @@ -33,6 +33,7 @@ from homeassistant.const import ( CONF_USERNAME, ) from homeassistant.core import callback +from homeassistant.helpers.typing import VolDictType from .const import ( CONF_OLD_DISCOVERY, @@ -210,7 +211,7 @@ class FritzBoxToolsFlowHandler(ConfigFlow, domain=DOMAIN): ) -> ConfigFlowResult: """Show the setup form to the user.""" - advanced_data_schema = {} + advanced_data_schema: VolDictType = {} if self.show_advanced_options: advanced_data_schema = { vol.Optional(CONF_PORT): vol.Coerce(int), @@ -348,7 +349,7 @@ class FritzBoxToolsFlowHandler(ConfigFlow, domain=DOMAIN): self, user_input: dict[str, Any], errors: dict[str, str] | None = None ) -> ConfigFlowResult: """Show the reconfigure form to the user.""" - advanced_data_schema = {} + advanced_data_schema: VolDictType = {} if self.show_advanced_options: advanced_data_schema = { vol.Optional(CONF_PORT, default=user_input[CONF_PORT]): vol.Coerce(int), diff --git a/homeassistant/components/generic_thermostat/climate.py b/homeassistant/components/generic_thermostat/climate.py index c080e8b82d7..d284c7d7772 100644 --- a/homeassistant/components/generic_thermostat/climate.py +++ b/homeassistant/components/generic_thermostat/climate.py @@ -61,7 +61,7 @@ from homeassistant.helpers.event import ( ) from homeassistant.helpers.reload import async_setup_reload_service from homeassistant.helpers.restore_state import RestoreEntity -from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType +from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType, VolDictType from . import DOMAIN, PLATFORMS @@ -96,6 +96,10 @@ CONF_PRESETS = { ) } +PRESETS_SCHEMA: VolDictType = { + vol.Optional(v): vol.Coerce(float) for v in CONF_PRESETS.values() +} + PLATFORM_SCHEMA_COMMON = vol.Schema( { vol.Required(CONF_HEATER): cv.entity_id, @@ -120,7 +124,7 @@ PLATFORM_SCHEMA_COMMON = vol.Schema( vol.In([PRECISION_TENTHS, PRECISION_HALVES, PRECISION_WHOLE]) ), vol.Optional(CONF_UNIQUE_ID): cv.string, - **{vol.Optional(v): vol.Coerce(float) for v in CONF_PRESETS.values()}, + **PRESETS_SCHEMA, } ) diff --git a/homeassistant/components/homeworks/config_flow.py b/homeassistant/components/homeworks/config_flow.py index 4b91018036a..81b31e4644e 100644 --- a/homeassistant/components/homeworks/config_flow.py +++ b/homeassistant/components/homeworks/config_flow.py @@ -29,6 +29,7 @@ from homeassistant.helpers.schema_config_entry_flow import ( SchemaOptionsFlowHandler, ) from homeassistant.helpers.selector import TextSelector +from homeassistant.helpers.typing import VolDictType from homeassistant.util import slugify from . import DEFAULT_FADE_RATE, calculate_unique_id @@ -62,7 +63,7 @@ CONTROLLER_EDIT = { ), } -LIGHT_EDIT = { +LIGHT_EDIT: VolDictType = { vol.Optional(CONF_RATE, default=DEFAULT_FADE_RATE): selector.NumberSelector( selector.NumberSelectorConfig( min=0, @@ -73,7 +74,7 @@ LIGHT_EDIT = { ), } -BUTTON_EDIT = { +BUTTON_EDIT: VolDictType = { vol.Optional(CONF_LED, default=False): selector.BooleanSelector(), vol.Optional(CONF_RELEASE_DELAY, default=0): selector.NumberSelector( selector.NumberSelectorConfig( diff --git a/homeassistant/components/keenetic_ndms2/config_flow.py b/homeassistant/components/keenetic_ndms2/config_flow.py index f00bbe22939..9e3c6728338 100644 --- a/homeassistant/components/keenetic_ndms2/config_flow.py +++ b/homeassistant/components/keenetic_ndms2/config_flow.py @@ -24,6 +24,7 @@ from homeassistant.const import ( ) from homeassistant.core import callback from homeassistant.helpers import config_validation as cv +from homeassistant.helpers.typing import VolDictType from .const import ( CONF_CONSIDER_HOME, @@ -84,7 +85,7 @@ class KeeneticFlowHandler(ConfigFlow, domain=DOMAIN): title=router_info.name, data={CONF_HOST: host, **user_input} ) - host_schema = ( + host_schema: VolDictType = ( {vol.Required(CONF_HOST): str} if CONF_HOST not in self.context else {} ) diff --git a/homeassistant/components/lcn/schemas.py b/homeassistant/components/lcn/schemas.py index b907525747d..9927ea5908d 100644 --- a/homeassistant/components/lcn/schemas.py +++ b/homeassistant/components/lcn/schemas.py @@ -21,6 +21,7 @@ from homeassistant.const import ( UnitOfTemperature, ) import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.typing import VolDictType from .const import ( BINSENSOR_PORTS, @@ -61,14 +62,14 @@ from .helpers import has_unique_host_names, is_address # Domain data # -DOMAIN_DATA_BINARY_SENSOR = { +DOMAIN_DATA_BINARY_SENSOR: VolDictType = { vol.Required(CONF_SOURCE): vol.All( vol.Upper, vol.In(SETPOINTS + KEYS + BINSENSOR_PORTS) ), } -DOMAIN_DATA_CLIMATE = { +DOMAIN_DATA_CLIMATE: VolDictType = { vol.Required(CONF_SOURCE): vol.All(vol.Upper, vol.In(VARIABLES)), vol.Required(CONF_SETPOINT): vol.All(vol.Upper, vol.In(VARIABLES + SETPOINTS)), vol.Optional(CONF_MAX_TEMP, default=DEFAULT_MAX_TEMP): vol.Coerce(float), @@ -80,7 +81,7 @@ DOMAIN_DATA_CLIMATE = { } -DOMAIN_DATA_COVER = { +DOMAIN_DATA_COVER: VolDictType = { vol.Required(CONF_MOTOR): vol.All(vol.Upper, vol.In(MOTOR_PORTS)), vol.Optional(CONF_REVERSE_TIME, default="rt1200"): vol.All( vol.Upper, vol.In(MOTOR_REVERSE_TIME) @@ -88,7 +89,7 @@ DOMAIN_DATA_COVER = { } -DOMAIN_DATA_LIGHT = { +DOMAIN_DATA_LIGHT: VolDictType = { vol.Required(CONF_OUTPUT): vol.All(vol.Upper, vol.In(OUTPUT_PORTS + RELAY_PORTS)), vol.Optional(CONF_DIMMABLE, default=False): vol.Coerce(bool), vol.Optional(CONF_TRANSITION, default=0): vol.All( @@ -97,7 +98,7 @@ DOMAIN_DATA_LIGHT = { } -DOMAIN_DATA_SCENE = { +DOMAIN_DATA_SCENE: VolDictType = { vol.Required(CONF_REGISTER): vol.All(vol.Coerce(int), vol.Range(0, 9)), vol.Required(CONF_SCENE): vol.All(vol.Coerce(int), vol.Range(0, 9)), vol.Optional(CONF_OUTPUTS, default=[]): vol.All( @@ -113,7 +114,7 @@ DOMAIN_DATA_SCENE = { ), } -DOMAIN_DATA_SENSOR = { +DOMAIN_DATA_SENSOR: VolDictType = { vol.Required(CONF_SOURCE): vol.All( vol.Upper, vol.In( @@ -126,7 +127,7 @@ DOMAIN_DATA_SENSOR = { } -DOMAIN_DATA_SWITCH = { +DOMAIN_DATA_SWITCH: VolDictType = { vol.Required(CONF_OUTPUT): vol.All(vol.Upper, vol.In(OUTPUT_PORTS + RELAY_PORTS)), } @@ -134,7 +135,7 @@ DOMAIN_DATA_SWITCH = { # Configuration # -DOMAIN_DATA_BASE = { +DOMAIN_DATA_BASE: VolDictType = { vol.Required(CONF_NAME): cv.string, vol.Required(CONF_ADDRESS): is_address, } diff --git a/homeassistant/components/light/__init__.py b/homeassistant/components/light/__init__.py index 67000b6aaaf..077071e6735 100644 --- a/homeassistant/components/light/__init__.py +++ b/homeassistant/components/light/__init__.py @@ -299,7 +299,7 @@ def is_on(hass: HomeAssistant, entity_id: str) -> bool: def preprocess_turn_on_alternatives( - hass: HomeAssistant, params: dict[str, Any] | dict[str | vol.Optional, Any] + hass: HomeAssistant, params: dict[str, Any] | VolDictType ) -> None: """Process extra data for turn light on request. @@ -403,11 +403,9 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: # noqa: # of the light base platform. hass.async_create_task(profiles.async_initialize(), eager_start=True) - def preprocess_data( - data: dict[str | vol.Optional, Any], - ) -> dict[str | vol.Optional, Any]: + def preprocess_data(data: VolDictType) -> VolDictType: """Preprocess the service data.""" - base: dict[str | vol.Optional, Any] = { + base: VolDictType = { entity_field: data.pop(entity_field) for entity_field in cv.ENTITY_SERVICE_FIELDS if entity_field in data diff --git a/homeassistant/components/monoprice/config_flow.py b/homeassistant/components/monoprice/config_flow.py index 542e729dbd2..2c7163123b6 100644 --- a/homeassistant/components/monoprice/config_flow.py +++ b/homeassistant/components/monoprice/config_flow.py @@ -12,6 +12,7 @@ from homeassistant.config_entries import ConfigEntry, ConfigFlow, OptionsFlow from homeassistant.const import CONF_PORT from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import HomeAssistantError +from homeassistant.helpers.typing import VolDictType from .const import ( CONF_SOURCE_1, @@ -35,7 +36,7 @@ SOURCES = [ CONF_SOURCE_6, ] -OPTIONS_FOR_DATA = {vol.Optional(source): str for source in SOURCES} +OPTIONS_FOR_DATA: VolDictType = {vol.Optional(source): str for source in SOURCES} DATA_SCHEMA = vol.Schema({vol.Required(CONF_PORT): str, **OPTIONS_FOR_DATA}) diff --git a/homeassistant/components/motioneye/config_flow.py b/homeassistant/components/motioneye/config_flow.py index 49059b528db..8107ca760cb 100644 --- a/homeassistant/components/motioneye/config_flow.py +++ b/homeassistant/components/motioneye/config_flow.py @@ -24,6 +24,7 @@ from homeassistant.const import CONF_SOURCE, CONF_URL, CONF_WEBHOOK_ID from homeassistant.core import callback from homeassistant.helpers import config_validation as cv from homeassistant.helpers.aiohttp_client import async_get_clientsession +from homeassistant.helpers.typing import VolDictType from . import create_motioneye_client from .const import ( @@ -55,7 +56,7 @@ class MotionEyeConfigFlow(ConfigFlow, domain=DOMAIN): user_input: dict[str, Any], errors: dict[str, str] | None = None ) -> ConfigFlowResult: """Show the form to the user.""" - url_schema: dict[vol.Required, type[str]] = {} + url_schema: VolDictType = {} if not self._hassio_discovery: # Only ask for URL when not discovered url_schema[ diff --git a/homeassistant/components/nina/config_flow.py b/homeassistant/components/nina/config_flow.py index 3a665bfe987..1fee6430ffc 100644 --- a/homeassistant/components/nina/config_flow.py +++ b/homeassistant/components/nina/config_flow.py @@ -17,6 +17,7 @@ from homeassistant.core import callback from homeassistant.helpers import entity_registry as er from homeassistant.helpers.aiohttp_client import async_get_clientsession import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.typing import VolDictType from .const import ( _LOGGER, @@ -137,14 +138,16 @@ class NinaConfigFlow(ConfigFlow, domain=DOMAIN): errors["base"] = "no_selection" + regions_schema: VolDictType = { + vol.Optional(region): cv.multi_select(self.regions[region]) + for region in CONST_REGIONS + } + return self.async_show_form( step_id="user", data_schema=vol.Schema( { - **{ - vol.Optional(region): cv.multi_select(self.regions[region]) - for region in CONST_REGIONS - }, + **regions_schema, vol.Required(CONF_MESSAGE_SLOTS, default=5): vol.All( int, vol.Range(min=1, max=20) ), diff --git a/homeassistant/components/vera/config_flow.py b/homeassistant/components/vera/config_flow.py index fcb1e5f013e..181849f46a1 100644 --- a/homeassistant/components/vera/config_flow.py +++ b/homeassistant/components/vera/config_flow.py @@ -22,6 +22,7 @@ from homeassistant.config_entries import ( from homeassistant.const import CONF_EXCLUDE, CONF_LIGHTS, CONF_SOURCE from homeassistant.core import callback from homeassistant.helpers import entity_registry as er +from homeassistant.helpers.typing import VolDictType from .const import CONF_CONTROLLER, CONF_LEGACY_UNIQUE_ID, DOMAIN @@ -49,9 +50,7 @@ def new_options(lights: list[int], exclude: list[int]) -> dict[str, list[int]]: return {CONF_LIGHTS: lights, CONF_EXCLUDE: exclude} -def options_schema( - options: Mapping[str, Any] | None = None, -) -> dict[vol.Optional, type[str]]: +def options_schema(options: Mapping[str, Any] | None = None) -> VolDictType: """Return options schema.""" options = options or {} return { diff --git a/homeassistant/helpers/config_validation.py b/homeassistant/helpers/config_validation.py index 58c76a40c8e..a28c81e6da9 100644 --- a/homeassistant/helpers/config_validation.py +++ b/homeassistant/helpers/config_validation.py @@ -1204,7 +1204,7 @@ PLATFORM_SCHEMA = vol.Schema( PLATFORM_SCHEMA_BASE = PLATFORM_SCHEMA.extend({}, extra=vol.ALLOW_EXTRA) -ENTITY_SERVICE_FIELDS = { +ENTITY_SERVICE_FIELDS: VolDictType = { # Either accept static entity IDs, a single dynamic template or a mixed list # of static and dynamic templates. While this could be solved with a single # complex template, handling it like this, keeps config validation useful. @@ -1310,7 +1310,7 @@ def script_action(value: Any) -> dict: SCRIPT_SCHEMA = vol.All(ensure_list, [script_action]) -SCRIPT_ACTION_BASE_SCHEMA = { +SCRIPT_ACTION_BASE_SCHEMA: VolDictType = { vol.Optional(CONF_ALIAS): string, vol.Optional(CONF_CONTINUE_ON_ERROR): boolean, vol.Optional(CONF_ENABLED): vol.Any(boolean, template),