From 3fdce31dc59f2a94c59afdb64516ae1d4cbe0845 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Tue, 25 Jun 2024 18:13:14 +0200 Subject: [PATCH] Improve collection schema typing --- .../components/application_credentials/__init__.py | 6 +++--- homeassistant/components/assist_pipeline/pipeline.py | 4 ++-- homeassistant/components/counter/__init__.py | 4 ++-- homeassistant/components/image_upload/__init__.py | 6 +++--- homeassistant/components/input_boolean/__init__.py | 4 ++-- homeassistant/components/input_button/__init__.py | 4 ++-- homeassistant/components/input_datetime/__init__.py | 4 ++-- homeassistant/components/input_number/__init__.py | 4 ++-- homeassistant/components/input_select/__init__.py | 4 ++-- homeassistant/components/input_text/__init__.py | 4 ++-- homeassistant/components/lovelace/const.py | 11 ++++++----- homeassistant/components/person/__init__.py | 6 +++--- homeassistant/components/schedule/__init__.py | 10 +++++----- homeassistant/components/tag/__init__.py | 10 +++++----- homeassistant/components/timer/__init__.py | 4 ++-- homeassistant/components/zone/__init__.py | 6 +++--- homeassistant/helpers/collection.py | 6 +++--- 17 files changed, 49 insertions(+), 48 deletions(-) diff --git a/homeassistant/components/application_credentials/__init__.py b/homeassistant/components/application_credentials/__init__.py index aacd18fc79540c..22deb1248591c1 100644 --- a/homeassistant/components/application_credentials/__init__.py +++ b/homeassistant/components/application_credentials/__init__.py @@ -29,7 +29,7 @@ from homeassistant.helpers import collection, config_entry_oauth2_flow import homeassistant.helpers.config_validation as cv from homeassistant.helpers.storage import Store -from homeassistant.helpers.typing import ConfigType +from homeassistant.helpers.typing import ConfigType, VolDictType from homeassistant.loader import ( IntegrationNotFound, async_get_application_credentials, @@ -49,14 +49,14 @@ CONF_AUTH_DOMAIN = "auth_domain" DEFAULT_IMPORT_NAME = "Import from configuration.yaml" -CREATE_FIELDS = { +CREATE_FIELDS: VolDictType = { vol.Required(CONF_DOMAIN): cv.string, vol.Required(CONF_CLIENT_ID): vol.All(cv.string, vol.Strip), vol.Required(CONF_CLIENT_SECRET): vol.All(cv.string, vol.Strip), vol.Optional(CONF_AUTH_DOMAIN): cv.string, vol.Optional(CONF_NAME): cv.string, } -UPDATE_FIELDS: dict = {} # Not supported +UPDATE_FIELDS: VolDictType = {} # Not supported CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN) diff --git a/homeassistant/components/assist_pipeline/pipeline.py b/homeassistant/components/assist_pipeline/pipeline.py index 6c1b3ced470b88..56f88f601048da 100644 --- a/homeassistant/components/assist_pipeline/pipeline.py +++ b/homeassistant/components/assist_pipeline/pipeline.py @@ -45,7 +45,7 @@ ) from homeassistant.helpers.singleton import singleton from homeassistant.helpers.storage import Store -from homeassistant.helpers.typing import UNDEFINED, UndefinedType +from homeassistant.helpers.typing import UNDEFINED, UndefinedType, VolDictType from homeassistant.util import ( dt as dt_util, language as language_util, @@ -94,7 +94,7 @@ def validate_language(data: dict[str, Any]) -> Any: return data -PIPELINE_FIELDS = { +PIPELINE_FIELDS: VolDictType = { vol.Required("conversation_engine"): str, vol.Required("conversation_language"): str, vol.Required("language"): str, diff --git a/homeassistant/components/counter/__init__.py b/homeassistant/components/counter/__init__.py index 3d68d70e575da9..324668a63e240a 100644 --- a/homeassistant/components/counter/__init__.py +++ b/homeassistant/components/counter/__init__.py @@ -21,7 +21,7 @@ from homeassistant.helpers.entity_component import EntityComponent from homeassistant.helpers.restore_state import RestoreEntity from homeassistant.helpers.storage import Store -from homeassistant.helpers.typing import ConfigType +from homeassistant.helpers.typing import ConfigType, VolDictType _LOGGER = logging.getLogger(__name__) @@ -49,7 +49,7 @@ STORAGE_KEY = DOMAIN STORAGE_VERSION = 1 -STORAGE_FIELDS = { +STORAGE_FIELDS: VolDictType = { vol.Optional(CONF_ICON): cv.icon, vol.Optional(CONF_INITIAL, default=DEFAULT_INITIAL): cv.positive_int, vol.Required(CONF_NAME): vol.All(cv.string, vol.Length(min=1)), diff --git a/homeassistant/components/image_upload/__init__.py b/homeassistant/components/image_upload/__init__.py index 59b594561f0a3e..8bb3aca3708097 100644 --- a/homeassistant/components/image_upload/__init__.py +++ b/homeassistant/components/image_upload/__init__.py @@ -21,7 +21,7 @@ from homeassistant.core import HomeAssistant, callback from homeassistant.helpers import collection, config_validation as cv from homeassistant.helpers.storage import Store -from homeassistant.helpers.typing import ConfigType +from homeassistant.helpers.typing import ConfigType, VolDictType import homeassistant.util.dt as dt_util from .const import DOMAIN @@ -32,11 +32,11 @@ VALID_SIZES = {256, 512} MAX_SIZE = 1024 * 1024 * 10 -CREATE_FIELDS = { +CREATE_FIELDS: VolDictType = { vol.Required("file"): FileField, } -UPDATE_FIELDS = { +UPDATE_FIELDS: VolDictType = { vol.Optional("name"): vol.All(str, vol.Length(min=1)), } diff --git a/homeassistant/components/input_boolean/__init__.py b/homeassistant/components/input_boolean/__init__.py index 91c7de96fe0159..57165c5508a1ed 100644 --- a/homeassistant/components/input_boolean/__init__.py +++ b/homeassistant/components/input_boolean/__init__.py @@ -26,7 +26,7 @@ from homeassistant.helpers.restore_state import RestoreEntity import homeassistant.helpers.service from homeassistant.helpers.storage import Store -from homeassistant.helpers.typing import ConfigType +from homeassistant.helpers.typing import ConfigType, VolDictType from homeassistant.loader import bind_hass DOMAIN = "input_boolean" @@ -35,7 +35,7 @@ CONF_INITIAL = "initial" -STORAGE_FIELDS = { +STORAGE_FIELDS: VolDictType = { vol.Required(CONF_NAME): vol.All(str, vol.Length(min=1)), vol.Optional(CONF_INITIAL): cv.boolean, vol.Optional(CONF_ICON): cv.icon, diff --git a/homeassistant/components/input_button/__init__.py b/homeassistant/components/input_button/__init__.py index d6c3644487b593..e70bbacd9338f8 100644 --- a/homeassistant/components/input_button/__init__.py +++ b/homeassistant/components/input_button/__init__.py @@ -22,13 +22,13 @@ from homeassistant.helpers.restore_state import RestoreEntity import homeassistant.helpers.service from homeassistant.helpers.storage import Store -from homeassistant.helpers.typing import ConfigType +from homeassistant.helpers.typing import ConfigType, VolDictType DOMAIN = "input_button" _LOGGER = logging.getLogger(__name__) -STORAGE_FIELDS = { +STORAGE_FIELDS: VolDictType = { vol.Required(CONF_NAME): vol.All(str, vol.Length(min=1)), vol.Optional(CONF_ICON): cv.icon, } diff --git a/homeassistant/components/input_datetime/__init__.py b/homeassistant/components/input_datetime/__init__.py index 11aab52e6a4785..5d2c1e7ff8d535 100644 --- a/homeassistant/components/input_datetime/__init__.py +++ b/homeassistant/components/input_datetime/__init__.py @@ -24,7 +24,7 @@ from homeassistant.helpers.restore_state import RestoreEntity import homeassistant.helpers.service from homeassistant.helpers.storage import Store -from homeassistant.helpers.typing import ConfigType +from homeassistant.helpers.typing import ConfigType, VolDictType from homeassistant.util import dt as dt_util _LOGGER = logging.getLogger(__name__) @@ -59,7 +59,7 @@ def validate_set_datetime_attrs(config): STORAGE_KEY = DOMAIN STORAGE_VERSION = 1 -STORAGE_FIELDS = { +STORAGE_FIELDS: VolDictType = { vol.Required(CONF_NAME): vol.All(str, vol.Length(min=1)), vol.Optional(CONF_HAS_DATE, default=False): cv.boolean, vol.Optional(CONF_HAS_TIME, default=False): cv.boolean, diff --git a/homeassistant/components/input_number/__init__.py b/homeassistant/components/input_number/__init__.py index e37f530b8af936..f55ceabc6f0008 100644 --- a/homeassistant/components/input_number/__init__.py +++ b/homeassistant/components/input_number/__init__.py @@ -25,7 +25,7 @@ from homeassistant.helpers.restore_state import RestoreEntity import homeassistant.helpers.service from homeassistant.helpers.storage import Store -from homeassistant.helpers.typing import ConfigType +from homeassistant.helpers.typing import ConfigType, VolDictType _LOGGER = logging.getLogger(__name__) @@ -64,7 +64,7 @@ def _cv_input_number(cfg): return cfg -STORAGE_FIELDS = { +STORAGE_FIELDS: VolDictType = { vol.Required(CONF_NAME): vol.All(str, vol.Length(min=1)), vol.Required(CONF_MIN): vol.Coerce(float), vol.Required(CONF_MAX): vol.Coerce(float), diff --git a/homeassistant/components/input_select/__init__.py b/homeassistant/components/input_select/__init__.py index 2741c9e21bc9cf..44d2df02a921e7 100644 --- a/homeassistant/components/input_select/__init__.py +++ b/homeassistant/components/input_select/__init__.py @@ -33,7 +33,7 @@ from homeassistant.helpers.restore_state import RestoreEntity import homeassistant.helpers.service from homeassistant.helpers.storage import Store -from homeassistant.helpers.typing import ConfigType +from homeassistant.helpers.typing import ConfigType, VolDictType _LOGGER = logging.getLogger(__name__) @@ -55,7 +55,7 @@ def _unique(options: Any) -> Any: raise HomeAssistantError("Duplicate options are not allowed") from exc -STORAGE_FIELDS = { +STORAGE_FIELDS: VolDictType = { vol.Required(CONF_NAME): vol.All(str, vol.Length(min=1)), vol.Required(CONF_OPTIONS): vol.All( cv.ensure_list, vol.Length(min=1), _unique, [cv.string] diff --git a/homeassistant/components/input_text/__init__.py b/homeassistant/components/input_text/__init__.py index 55b43ee8a1e4e4..3d75ff9f5c2b97 100644 --- a/homeassistant/components/input_text/__init__.py +++ b/homeassistant/components/input_text/__init__.py @@ -24,7 +24,7 @@ from homeassistant.helpers.restore_state import RestoreEntity import homeassistant.helpers.service from homeassistant.helpers.storage import Store -from homeassistant.helpers.typing import ConfigType +from homeassistant.helpers.typing import ConfigType, VolDictType _LOGGER = logging.getLogger(__name__) @@ -50,7 +50,7 @@ STORAGE_KEY = DOMAIN STORAGE_VERSION = 1 -STORAGE_FIELDS = { +STORAGE_FIELDS: VolDictType = { vol.Required(CONF_NAME): vol.All(str, vol.Length(min=1)), vol.Optional(CONF_MIN, default=CONF_MIN_VALUE): vol.Coerce(int), vol.Optional(CONF_MAX, default=CONF_MAX_VALUE): vol.Coerce(int), diff --git a/homeassistant/components/lovelace/const.py b/homeassistant/components/lovelace/const.py index 538bd49d72cd8e..86f47fe2b5c850 100644 --- a/homeassistant/components/lovelace/const.py +++ b/homeassistant/components/lovelace/const.py @@ -13,6 +13,7 @@ ) from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import config_validation as cv +from homeassistant.helpers.typing import VolDictType from homeassistant.util import slugify DOMAIN = "lovelace" @@ -37,12 +38,12 @@ RESOURCE_SCHEMA = vol.Schema(RESOURCE_FIELDS) -RESOURCE_CREATE_FIELDS = { +RESOURCE_CREATE_FIELDS: VolDictType = { vol.Required(CONF_RESOURCE_TYPE_WS): vol.In(RESOURCE_TYPES), vol.Required(CONF_URL): cv.string, } -RESOURCE_UPDATE_FIELDS = { +RESOURCE_UPDATE_FIELDS: VolDictType = { vol.Optional(CONF_RESOURCE_TYPE_WS): vol.In(RESOURCE_TYPES), vol.Optional(CONF_URL): cv.string, } @@ -54,7 +55,7 @@ CONF_REQUIRE_ADMIN = "require_admin" CONF_SHOW_IN_SIDEBAR = "show_in_sidebar" -DASHBOARD_BASE_CREATE_FIELDS = { +DASHBOARD_BASE_CREATE_FIELDS: VolDictType = { vol.Optional(CONF_REQUIRE_ADMIN, default=False): cv.boolean, vol.Optional(CONF_ICON): cv.icon, vol.Required(CONF_TITLE): cv.string, @@ -62,7 +63,7 @@ } -DASHBOARD_BASE_UPDATE_FIELDS = { +DASHBOARD_BASE_UPDATE_FIELDS: VolDictType = { vol.Optional(CONF_REQUIRE_ADMIN): cv.boolean, vol.Optional(CONF_ICON): vol.Any(cv.icon, None), vol.Optional(CONF_TITLE): cv.string, @@ -70,7 +71,7 @@ } -STORAGE_DASHBOARD_CREATE_FIELDS = { +STORAGE_DASHBOARD_CREATE_FIELDS: VolDictType = { **DASHBOARD_BASE_CREATE_FIELDS, vol.Required(CONF_URL_PATH): cv.string, # For now we write "storage" as all modes. diff --git a/homeassistant/components/person/__init__.py b/homeassistant/components/person/__init__.py index 0779140a0913c9..b793f4b33aecc2 100644 --- a/homeassistant/components/person/__init__.py +++ b/homeassistant/components/person/__init__.py @@ -50,7 +50,7 @@ from homeassistant.helpers.event import async_track_state_change_event from homeassistant.helpers.restore_state import RestoreEntity from homeassistant.helpers.storage import Store -from homeassistant.helpers.typing import ConfigType +from homeassistant.helpers.typing import ConfigType, VolDictType from homeassistant.loader import bind_hass from .const import DOMAIN @@ -165,7 +165,7 @@ def entities_in_person(hass: HomeAssistant, entity_id: str) -> list[str]: return person_entity.device_trackers -CREATE_FIELDS = { +CREATE_FIELDS: VolDictType = { vol.Required(CONF_NAME): vol.All(str, vol.Length(min=1)), vol.Optional(CONF_USER_ID): vol.Any(str, None), vol.Optional(CONF_DEVICE_TRACKERS, default=list): vol.All( @@ -175,7 +175,7 @@ def entities_in_person(hass: HomeAssistant, entity_id: str) -> list[str]: } -UPDATE_FIELDS = { +UPDATE_FIELDS: VolDictType = { vol.Optional(CONF_NAME): vol.All(str, vol.Length(min=1)), vol.Optional(CONF_USER_ID): vol.Any(str, None), vol.Optional(CONF_DEVICE_TRACKERS, default=list): vol.All( diff --git a/homeassistant/components/schedule/__init__.py b/homeassistant/components/schedule/__init__.py index e69a6761bc7aa3..08d0b083f7c417 100644 --- a/homeassistant/components/schedule/__init__.py +++ b/homeassistant/components/schedule/__init__.py @@ -33,7 +33,7 @@ from homeassistant.helpers.event import async_track_point_in_utc_time from homeassistant.helpers.service import async_register_admin_service from homeassistant.helpers.storage import Store -from homeassistant.helpers.typing import ConfigType +from homeassistant.helpers.typing import ConfigType, VolDictType from homeassistant.util import dt as dt_util from .const import ( @@ -104,12 +104,12 @@ def serialize_to_time(value: Any) -> Any: return vol.Coerce(str)(value) -BASE_SCHEMA = { +BASE_SCHEMA: VolDictType = { vol.Required(CONF_NAME): vol.All(str, vol.Length(min=1)), vol.Optional(CONF_ICON): cv.icon, } -TIME_RANGE_SCHEMA = { +TIME_RANGE_SCHEMA: VolDictType = { vol.Required(CONF_FROM): cv.time, vol.Required(CONF_TO): deserialize_to_time, } @@ -122,13 +122,13 @@ def serialize_to_time(value: Any) -> Any: } ) -SCHEDULE_SCHEMA = { +SCHEDULE_SCHEMA: VolDictType = { vol.Optional(day, default=[]): vol.All( cv.ensure_list, [TIME_RANGE_SCHEMA], valid_schedule ) for day in CONF_ALL_DAYS } -STORAGE_SCHEDULE_SCHEMA = { +STORAGE_SCHEDULE_SCHEMA: VolDictType = { vol.Optional(day, default=[]): vol.All( cv.ensure_list, [TIME_RANGE_SCHEMA], valid_schedule, [STORAGE_TIME_RANGE_SCHEMA] ) diff --git a/homeassistant/components/tag/__init__.py b/homeassistant/components/tag/__init__.py index af3d06cf2d4bec..97307112f2291f 100644 --- a/homeassistant/components/tag/__init__.py +++ b/homeassistant/components/tag/__init__.py @@ -18,7 +18,7 @@ from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity_component import EntityComponent from homeassistant.helpers.storage import Store -from homeassistant.helpers.typing import ConfigType +from homeassistant.helpers.typing import ConfigType, VolDictType from homeassistant.util import slugify import homeassistant.util.dt as dt_util from homeassistant.util.hass_dict import HassKey @@ -35,7 +35,7 @@ TAG_DATA: HassKey[TagStorageCollection] = HassKey(DOMAIN) -CREATE_FIELDS = { +CREATE_FIELDS: VolDictType = { vol.Optional(TAG_ID): cv.string, vol.Optional(CONF_NAME): vol.All(str, vol.Length(min=1)), vol.Optional("description"): cv.string, @@ -43,7 +43,7 @@ vol.Optional(DEVICE_ID): cv.string, } -UPDATE_FIELDS = { +UPDATE_FIELDS: VolDictType = { vol.Optional(CONF_NAME): vol.All(str, vol.Length(min=1)), vol.Optional("description"): cv.string, vol.Optional(LAST_SCANNED): cv.datetime, @@ -192,8 +192,8 @@ def __init__( storage_collection: TagStorageCollection, api_prefix: str, model_name: str, - create_schema: ConfigType, - update_schema: ConfigType, + create_schema: VolDictType, + update_schema: VolDictType, ) -> None: """Initialize a websocket for tag.""" super().__init__( diff --git a/homeassistant/components/timer/__init__.py b/homeassistant/components/timer/__init__.py index 8927439a6ccefe..3f2b4bd7f43c6d 100644 --- a/homeassistant/components/timer/__init__.py +++ b/homeassistant/components/timer/__init__.py @@ -26,7 +26,7 @@ from homeassistant.helpers.restore_state import RestoreEntity import homeassistant.helpers.service from homeassistant.helpers.storage import Store -from homeassistant.helpers.typing import ConfigType +from homeassistant.helpers.typing import ConfigType, VolDictType import homeassistant.util.dt as dt_util _LOGGER = logging.getLogger(__name__) @@ -66,7 +66,7 @@ STORAGE_KEY = DOMAIN STORAGE_VERSION = 1 -STORAGE_FIELDS = { +STORAGE_FIELDS: VolDictType = { vol.Required(CONF_NAME): cv.string, vol.Optional(CONF_ICON): cv.icon, vol.Optional(CONF_DURATION, default=DEFAULT_DURATION): cv.time_period, diff --git a/homeassistant/components/zone/__init__.py b/homeassistant/components/zone/__init__.py index 0fef996167959d..1c43a79e10e85a 100644 --- a/homeassistant/components/zone/__init__.py +++ b/homeassistant/components/zone/__init__.py @@ -45,7 +45,7 @@ service, storage, ) -from homeassistant.helpers.typing import ConfigType +from homeassistant.helpers.typing import ConfigType, VolDictType from homeassistant.loader import bind_hass from homeassistant.util.location import distance @@ -62,7 +62,7 @@ ICON_HOME = "mdi:home" ICON_IMPORT = "mdi:import" -CREATE_FIELDS = { +CREATE_FIELDS: VolDictType = { vol.Required(CONF_NAME): cv.string, vol.Required(CONF_LATITUDE): cv.latitude, vol.Required(CONF_LONGITUDE): cv.longitude, @@ -72,7 +72,7 @@ } -UPDATE_FIELDS = { +UPDATE_FIELDS: VolDictType = { vol.Optional(CONF_NAME): cv.string, vol.Optional(CONF_LATITUDE): cv.latitude, vol.Optional(CONF_LONGITUDE): cv.longitude, diff --git a/homeassistant/helpers/collection.py b/homeassistant/helpers/collection.py index b999309800310d..036aaacf0e9966 100644 --- a/homeassistant/helpers/collection.py +++ b/homeassistant/helpers/collection.py @@ -26,7 +26,7 @@ from .entity import Entity from .entity_component import EntityComponent from .storage import Store -from .typing import ConfigType +from .typing import ConfigType, VolDictType STORAGE_VERSION = 1 SAVE_DELAY = 10 @@ -515,8 +515,8 @@ def __init__( storage_collection: _StorageCollectionT, api_prefix: str, model_name: str, - create_schema: dict, - update_schema: dict, + create_schema: VolDictType, + update_schema: VolDictType, ) -> None: """Initialize a websocket CRUD.""" self.storage_collection = storage_collection