Home Assistant Unofficial Reference 2024.12.1
typing.py
Go to the documentation of this file.
1 """Typing Helpers for Home Assistant."""
2 
3 from collections.abc import Mapping
4 from enum import Enum
5 from functools import partial
6 from typing import Any, Never
7 
8 import voluptuous as vol
9 
10 from .deprecation import (
11  DeferredDeprecatedAlias,
12  all_with_deprecated_constants,
13  check_if_deprecated_constant,
14  dir_with_deprecated_constants,
15 )
16 
17 type GPSType = tuple[float, float]
18 type ConfigType = dict[str, Any]
19 type DiscoveryInfoType = dict[str, Any]
20 type ServiceDataType = dict[str, Any]
21 type StateType = str | int | float | None
22 type TemplateVarsType = Mapping[str, Any] | None
23 type NoEventData = Mapping[str, Never]
24 type VolSchemaType = vol.Schema | vol.All | vol.Any
25 type VolDictType = dict[str | vol.Marker, Any]
26 
27 # Custom type for recorder Queries
28 type QueryType = Any
29 
30 
31 class UndefinedType(Enum):
32  """Singleton type for use with not set sentinel values."""
33 
34  _singleton = 0
35 
36 
37 UNDEFINED = UndefinedType._singleton # noqa: SLF001
38 
39 
40 def _deprecated_typing_helper(attr: str) -> DeferredDeprecatedAlias:
41  """Help to make a DeferredDeprecatedAlias."""
42 
43  def value_fn() -> Any:
44  # pylint: disable-next=import-outside-toplevel
45  import homeassistant.core
46 
47  return getattr(homeassistant.core, attr)
48 
49  return DeferredDeprecatedAlias(value_fn, f"homeassistant.core.{attr}", "2025.5")
50 
51 
52 # The following types should not used and
53 # are not present in the core code base.
54 # They are kept in order not to break custom integrations
55 # that may rely on them.
56 # Deprecated as of 2024.5 use types from homeassistant.core instead.
57 _DEPRECATED_ContextType = _deprecated_typing_helper("Context")
58 _DEPRECATED_EventType = _deprecated_typing_helper("Event")
59 _DEPRECATED_HomeAssistantType = _deprecated_typing_helper("HomeAssistant")
60 _DEPRECATED_ServiceCallType = _deprecated_typing_helper("ServiceCall")
61 
62 # These can be removed if no deprecated constant are in this module anymore
63 __getattr__ = partial(check_if_deprecated_constant, module_globals=globals())
64 __dir__ = partial(
65  dir_with_deprecated_constants, module_globals_keys=[*globals().keys()]
66 )
67 __all__ = all_with_deprecated_constants(globals())
list[str] all_with_deprecated_constants(dict[str, Any] module_globals)
Definition: deprecation.py:356
DeferredDeprecatedAlias _deprecated_typing_helper(str attr)
Definition: typing.py:40