Improve mqtt schema typing (#120407)

This commit is contained in:
Marc Mueller 2024-06-25 13:03:18 +02:00 committed by GitHub
parent 62fd692d27
commit 7453b7df63
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
23 changed files with 54 additions and 50 deletions

View file

@ -47,7 +47,7 @@ from homeassistant.core import HomeAssistant, callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.template import Template
from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers.typing import ConfigType, VolSchemaType
from homeassistant.util.unit_conversion import TemperatureConverter
from . import subscription
@ -550,7 +550,7 @@ class MqttClimate(MqttTemperatureControlEntity, ClimateEntity):
_enable_turn_on_off_backwards_compatibility = False
@staticmethod
def config_schema() -> vol.Schema:
def config_schema() -> VolSchemaType:
"""Return the config schema."""
return DISCOVERY_SCHEMA

View file

@ -31,7 +31,7 @@ from homeassistant.core import HomeAssistant, callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.service_info.mqtt import ReceivePayloadType
from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers.typing import ConfigType, VolSchemaType
from homeassistant.util.json import JSON_DECODE_EXCEPTIONS, json_loads
from homeassistant.util.percentage import (
percentage_to_ranged_value,
@ -246,7 +246,7 @@ class MqttCover(MqttEntity, CoverEntity):
_tilt_range: tuple[int, int]
@staticmethod
def config_schema() -> vol.Schema:
def config_schema() -> VolSchemaType:
"""Return the config schema."""
return DISCOVERY_SCHEMA

View file

@ -27,7 +27,7 @@ from homeassistant.const import (
from homeassistant.core import HomeAssistant, callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers.typing import ConfigType, VolSchemaType
from . import subscription
from .config import MQTT_BASE_SCHEMA
@ -103,7 +103,7 @@ class MqttDeviceTracker(MqttEntity, TrackerEntity):
_value_template: Callable[[ReceivePayloadType], ReceivePayloadType]
@staticmethod
def config_schema() -> vol.Schema:
def config_schema() -> VolSchemaType:
"""Return the config schema."""
return DISCOVERY_SCHEMA

View file

@ -19,7 +19,7 @@ from homeassistant.const import CONF_DEVICE_CLASS, CONF_NAME, CONF_VALUE_TEMPLAT
from homeassistant.core import HomeAssistant, callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers.typing import ConfigType, VolSchemaType
from homeassistant.util.json import JSON_DECODE_EXCEPTIONS, json_loads_object
from . import subscription
@ -94,7 +94,7 @@ class MqttEvent(MqttEntity, EventEntity):
_template: Callable[[ReceivePayloadType, PayloadSentinel], ReceivePayloadType]
@staticmethod
def config_schema() -> vol.Schema:
def config_schema() -> VolSchemaType:
"""Return the config schema."""
return DISCOVERY_SCHEMA

View file

@ -30,7 +30,7 @@ from homeassistant.core import HomeAssistant, callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.template import Template
from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers.typing import ConfigType, VolSchemaType
from homeassistant.util.percentage import (
percentage_to_ranged_value,
ranged_value_to_percentage,
@ -226,7 +226,7 @@ class MqttFan(MqttEntity, FanEntity):
_speed_range: tuple[int, int]
@staticmethod
def config_schema() -> vol.Schema:
def config_schema() -> VolSchemaType:
"""Return the config schema."""
return DISCOVERY_SCHEMA

View file

@ -33,7 +33,7 @@ from homeassistant.core import HomeAssistant, callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.template import Template
from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers.typing import ConfigType, VolSchemaType
from . import subscription
from .config import MQTT_RW_SCHEMA
@ -212,7 +212,7 @@ class MqttHumidifier(MqttEntity, HumidifierEntity):
_topic: dict[str, Any]
@staticmethod
def config_schema() -> vol.Schema:
def config_schema() -> VolSchemaType:
"""Return the config schema."""
return DISCOVERY_SCHEMA

View file

@ -20,7 +20,7 @@ from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.httpx_client import get_async_client
from homeassistant.helpers.service_info.mqtt import ReceivePayloadType
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType, VolSchemaType
from homeassistant.util import dt as dt_util
from . import subscription
@ -117,7 +117,7 @@ class MqttImage(MqttEntity, ImageEntity):
MqttEntity.__init__(self, hass, config, config_entry, discovery_data)
@staticmethod
def config_schema() -> vol.Schema:
def config_schema() -> VolSchemaType:
"""Return the config schema."""
return DISCOVERY_SCHEMA

View file

@ -20,7 +20,7 @@ from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers.typing import ConfigType, VolSchemaType
from . import subscription
from .config import MQTT_BASE_SCHEMA
@ -103,7 +103,7 @@ class MqttLawnMower(MqttEntity, LawnMowerEntity, RestoreEntity):
_value_template: Callable[[ReceivePayloadType], ReceivePayloadType]
@staticmethod
def config_schema() -> vol.Schema:
def config_schema() -> VolSchemaType:
"""Return the config schema."""
return DISCOVERY_SCHEMA

View file

@ -10,7 +10,7 @@ from homeassistant.components import light
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers.typing import ConfigType, VolSchemaType
from ..mixins import async_setup_entity_entry_helper
from .schema import CONF_SCHEMA, MQTT_LIGHT_SCHEMA_SCHEMA
@ -33,7 +33,7 @@ from .schema_template import (
def validate_mqtt_light_discovery(config_value: dict[str, Any]) -> ConfigType:
"""Validate MQTT light schema for discovery."""
schemas = {
schemas: dict[str, VolSchemaType] = {
"basic": DISCOVERY_SCHEMA_BASIC,
"json": DISCOVERY_SCHEMA_JSON,
"template": DISCOVERY_SCHEMA_TEMPLATE,
@ -44,7 +44,7 @@ def validate_mqtt_light_discovery(config_value: dict[str, Any]) -> ConfigType:
def validate_mqtt_light_modern(config_value: dict[str, Any]) -> ConfigType:
"""Validate MQTT light schema for setup from configuration.yaml."""
schemas = {
schemas: dict[str, VolSchemaType] = {
"basic": PLATFORM_SCHEMA_MODERN_BASIC,
"json": PLATFORM_SCHEMA_MODERN_JSON,
"template": PLATFORM_SCHEMA_MODERN_TEMPLATE,

View file

@ -39,7 +39,7 @@ from homeassistant.const import (
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers.typing import ConfigType, VolSchemaType
import homeassistant.util.color as color_util
from .. import subscription
@ -249,7 +249,7 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
_optimistic_xy_color: bool
@staticmethod
def config_schema() -> vol.Schema:
def config_schema() -> VolSchemaType:
"""Return the config schema."""
return DISCOVERY_SCHEMA_BASIC

View file

@ -51,7 +51,7 @@ import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from homeassistant.helpers.json import json_dumps
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers.typing import ConfigType, VolSchemaType
import homeassistant.util.color as color_util
from homeassistant.util.json import json_loads_object
from homeassistant.util.yaml import dump as yaml_dump
@ -267,7 +267,7 @@ class MqttLightJson(MqttEntity, LightEntity, RestoreEntity):
_deprecated_color_handling: bool = False
@staticmethod
def config_schema() -> vol.Schema:
def config_schema() -> VolSchemaType:
"""Return the config schema."""
return DISCOVERY_SCHEMA_JSON

View file

@ -31,7 +31,7 @@ from homeassistant.const import (
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.typing import ConfigType, TemplateVarsType
from homeassistant.helpers.typing import ConfigType, TemplateVarsType, VolSchemaType
import homeassistant.util.color as color_util
from .. import subscription
@ -120,7 +120,7 @@ class MqttLightTemplate(MqttEntity, LightEntity, RestoreEntity):
_topics: dict[str, str | None]
@staticmethod
def config_schema() -> vol.Schema:
def config_schema() -> VolSchemaType:
"""Return the config schema."""
return DISCOVERY_SCHEMA_TEMPLATE

View file

@ -53,6 +53,7 @@ from homeassistant.helpers.typing import (
ConfigType,
DiscoveryInfoType,
UndefinedType,
VolSchemaType,
)
from homeassistant.util.json import json_loads
from homeassistant.util.yaml import dump as yaml_dump
@ -247,8 +248,8 @@ def async_setup_entity_entry_helper(
entity_class: type[MqttEntity] | None,
domain: str,
async_add_entities: AddEntitiesCallback,
discovery_schema: vol.Schema,
platform_schema_modern: vol.Schema,
discovery_schema: VolSchemaType,
platform_schema_modern: VolSchemaType,
schema_class_mapping: dict[str, type[MqttEntity]] | None = None,
) -> None:
"""Set up entity creation dynamically through MQTT discovery."""
@ -1187,7 +1188,7 @@ class MqttEntity(
@staticmethod
@abstractmethod
def config_schema() -> vol.Schema:
def config_schema() -> VolSchemaType:
"""Return the config schema."""
def _set_entity_name(self, config: ConfigType) -> None:

View file

@ -11,15 +11,18 @@ from enum import StrEnum
import logging
from typing import TYPE_CHECKING, Any, TypedDict
import voluptuous as vol
from homeassistant.const import ATTR_ENTITY_ID, ATTR_NAME, Platform
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
from homeassistant.exceptions import ServiceValidationError, TemplateError
from homeassistant.helpers import template
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.service_info.mqtt import ReceivePayloadType
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType, TemplateVarsType
from homeassistant.helpers.typing import (
ConfigType,
DiscoveryInfoType,
TemplateVarsType,
VolSchemaType,
)
from homeassistant.util.hass_dict import HassKey
if TYPE_CHECKING:
@ -418,7 +421,7 @@ class MqttData:
platforms_loaded: set[Platform | str] = field(default_factory=set)
reload_dispatchers: list[CALLBACK_TYPE] = field(default_factory=list)
reload_handlers: dict[str, CALLBACK_TYPE] = field(default_factory=dict)
reload_schema: dict[str, vol.Schema] = field(default_factory=dict)
reload_schema: dict[str, VolSchemaType] = field(default_factory=dict)
state_write_requests: EntityTopicState = field(default_factory=EntityTopicState)
subscriptions_to_restore: list[Subscription] = field(default_factory=list)
tags: dict[str, dict[str, MQTTTagScanner]] = field(default_factory=dict)

View file

@ -28,7 +28,7 @@ from homeassistant.const import (
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers.typing import ConfigType, VolSchemaType
from . import subscription
from .config import MQTT_RW_SCHEMA
@ -133,7 +133,7 @@ class MqttNumber(MqttEntity, RestoreNumber):
_value_template: Callable[[ReceivePayloadType], ReceivePayloadType]
@staticmethod
def config_schema() -> vol.Schema:
def config_schema() -> VolSchemaType:
"""Return the config schema."""
return DISCOVERY_SCHEMA

View file

@ -15,7 +15,7 @@ from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers.typing import ConfigType, VolSchemaType
from . import subscription
from .config import MQTT_RW_SCHEMA
@ -84,7 +84,7 @@ class MqttSelect(MqttEntity, SelectEntity, RestoreEntity):
_optimistic: bool = False
@staticmethod
def config_schema() -> vol.Schema:
def config_schema() -> VolSchemaType:
"""Return the config schema."""
return DISCOVERY_SCHEMA

View file

@ -33,7 +33,7 @@ from homeassistant.core import CALLBACK_TYPE, HomeAssistant, State, callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.event import async_call_later
from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers.typing import ConfigType, VolSchemaType
from homeassistant.util import dt as dt_util
from . import subscription
@ -185,7 +185,7 @@ class MqttSensor(MqttEntity, RestoreSensor):
await MqttEntity.async_will_remove_from_hass(self)
@staticmethod
def config_schema() -> vol.Schema:
def config_schema() -> VolSchemaType:
"""Return the config schema."""
return DISCOVERY_SCHEMA

View file

@ -32,7 +32,7 @@ import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.json import json_dumps
from homeassistant.helpers.template import Template
from homeassistant.helpers.typing import ConfigType, TemplateVarsType
from homeassistant.helpers.typing import ConfigType, TemplateVarsType, VolSchemaType
from homeassistant.util.json import JSON_DECODE_EXCEPTIONS, json_loads_object
from . import subscription
@ -142,7 +142,7 @@ class MqttSiren(MqttEntity, SirenEntity):
_optimistic: bool
@staticmethod
def config_schema() -> vol.Schema:
def config_schema() -> VolSchemaType:
"""Return the config schema."""
return DISCOVERY_SCHEMA

View file

@ -22,7 +22,7 @@ from homeassistant.const import (
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers.typing import ConfigType, VolSchemaType
from . import subscription
from .config import MQTT_RW_SCHEMA
@ -121,7 +121,7 @@ class MqttTextEntity(MqttEntity, TextEntity):
_value_template: Callable[[ReceivePayloadType], ReceivePayloadType]
@staticmethod
def config_schema() -> vol.Schema:
def config_schema() -> VolSchemaType:
"""Return the config schema."""
return DISCOVERY_SCHEMA

View file

@ -19,7 +19,7 @@ from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers.typing import ConfigType, VolSchemaType
from homeassistant.util.json import JSON_DECODE_EXCEPTIONS, json_loads
from . import subscription
@ -107,7 +107,7 @@ class MqttUpdate(MqttEntity, UpdateEntity, RestoreEntity):
return super().entity_picture
@staticmethod
def config_schema() -> vol.Schema:
def config_schema() -> VolSchemaType:
"""Return the config schema."""
return DISCOVERY_SCHEMA

View file

@ -35,7 +35,7 @@ import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from homeassistant.helpers.json import json_dumps
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType, VolSchemaType
from homeassistant.util.json import json_loads_object
from . import subscription
@ -281,7 +281,7 @@ class MqttStateVacuum(MqttEntity, StateVacuumEntity):
MqttEntity.__init__(self, hass, config, config_entry, discovery_data)
@staticmethod
def config_schema() -> vol.Schema:
def config_schema() -> VolSchemaType:
"""Return the config schema."""
return DISCOVERY_SCHEMA

View file

@ -28,7 +28,7 @@ from homeassistant.const import (
from homeassistant.core import HomeAssistant, callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers.typing import ConfigType, VolSchemaType
from homeassistant.util.json import JSON_DECODE_EXCEPTIONS, json_loads
from homeassistant.util.percentage import (
percentage_to_ranged_value,
@ -163,7 +163,7 @@ class MqttValve(MqttEntity, ValveEntity):
_tilt_optimistic: bool
@staticmethod
def config_schema() -> vol.Schema:
def config_schema() -> VolSchemaType:
"""Return the config schema."""
return DISCOVERY_SCHEMA

View file

@ -38,7 +38,7 @@ from homeassistant.core import HomeAssistant, callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.template import Template
from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers.typing import ConfigType, VolSchemaType
from homeassistant.util.unit_conversion import TemperatureConverter
from .climate import MqttTemperatureControlEntity
@ -188,7 +188,7 @@ class MqttWaterHeater(MqttTemperatureControlEntity, WaterHeaterEntity):
_attr_target_temperature_high: float | None = None
@staticmethod
def config_schema() -> vol.Schema:
def config_schema() -> VolSchemaType:
"""Return the config schema."""
return DISCOVERY_SCHEMA