2017-05-02 16:18:47 +00:00
|
|
|
"""Typing Helpers for Home Assistant."""
|
2024-03-08 15:36:11 +00:00
|
|
|
|
2022-01-12 06:56:35 +00:00
|
|
|
from collections.abc import Mapping
|
2020-12-19 11:46:27 +00:00
|
|
|
from enum import Enum
|
2024-04-06 23:15:30 +00:00
|
|
|
from functools import partial
|
2024-04-08 18:25:34 +00:00
|
|
|
from typing import Any, Never
|
2016-07-31 20:56:57 +00:00
|
|
|
|
2024-06-25 09:58:27 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2024-04-06 23:15:30 +00:00
|
|
|
from .deprecation import (
|
2024-06-11 11:48:12 +00:00
|
|
|
DeferredDeprecatedAlias,
|
2024-04-06 23:15:30 +00:00
|
|
|
all_with_deprecated_constants,
|
|
|
|
check_if_deprecated_constant,
|
|
|
|
dir_with_deprecated_constants,
|
|
|
|
)
|
|
|
|
|
2024-05-17 12:42:21 +00:00
|
|
|
type GPSType = tuple[float, float]
|
|
|
|
type ConfigType = dict[str, Any]
|
|
|
|
type DiscoveryInfoType = dict[str, Any]
|
|
|
|
type ServiceDataType = dict[str, Any]
|
|
|
|
type StateType = str | int | float | None
|
|
|
|
type TemplateVarsType = Mapping[str, Any] | None
|
|
|
|
type NoEventData = Mapping[str, Never]
|
2024-06-25 09:58:27 +00:00
|
|
|
type VolSchemaType = vol.Schema | vol.All | vol.Any
|
|
|
|
type VolDictType = dict[str | vol.Marker, Any]
|
2016-07-31 20:56:57 +00:00
|
|
|
|
2016-08-30 16:22:52 +00:00
|
|
|
# Custom type for recorder Queries
|
2024-05-17 12:42:21 +00:00
|
|
|
type QueryType = Any
|
2020-12-19 11:46:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UndefinedType(Enum):
|
|
|
|
"""Singleton type for use with not set sentinel values."""
|
|
|
|
|
|
|
|
_singleton = 0
|
|
|
|
|
|
|
|
|
2024-05-06 18:33:26 +00:00
|
|
|
UNDEFINED = UndefinedType._singleton # noqa: SLF001
|
2021-05-01 22:15:27 +00:00
|
|
|
|
2022-09-22 05:18:00 +00:00
|
|
|
|
2024-06-11 11:48:12 +00:00
|
|
|
def _deprecated_typing_helper(attr: str) -> DeferredDeprecatedAlias:
|
|
|
|
"""Help to make a DeferredDeprecatedAlias."""
|
|
|
|
|
|
|
|
def value_fn() -> Any:
|
|
|
|
# pylint: disable-next=import-outside-toplevel
|
|
|
|
import homeassistant.core
|
|
|
|
|
|
|
|
return getattr(homeassistant.core, attr)
|
|
|
|
|
|
|
|
return DeferredDeprecatedAlias(value_fn, f"homeassistant.core.{attr}", "2025.5")
|
|
|
|
|
|
|
|
|
2021-05-01 22:15:27 +00:00
|
|
|
# The following types should not used and
|
|
|
|
# are not present in the core code base.
|
|
|
|
# They are kept in order not to break custom integrations
|
|
|
|
# that may rely on them.
|
2024-04-06 23:15:30 +00:00
|
|
|
# Deprecated as of 2024.5 use types from homeassistant.core instead.
|
2024-06-11 11:48:12 +00:00
|
|
|
_DEPRECATED_ContextType = _deprecated_typing_helper("Context")
|
|
|
|
_DEPRECATED_EventType = _deprecated_typing_helper("Event")
|
|
|
|
_DEPRECATED_HomeAssistantType = _deprecated_typing_helper("HomeAssistant")
|
|
|
|
_DEPRECATED_ServiceCallType = _deprecated_typing_helper("ServiceCall")
|
2024-04-06 23:15:30 +00:00
|
|
|
|
|
|
|
# These can be removed if no deprecated constant are in this module anymore
|
|
|
|
__getattr__ = partial(check_if_deprecated_constant, module_globals=globals())
|
|
|
|
__dir__ = partial(
|
|
|
|
dir_with_deprecated_constants, module_globals_keys=[*globals().keys()]
|
|
|
|
)
|
|
|
|
__all__ = all_with_deprecated_constants(globals())
|