mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Move SignalTypes to util (#114236)
This commit is contained in:
parent
fbb590ea1f
commit
1d2c2d2055
@ -18,7 +18,6 @@ from homeassistant.core import HomeAssistant, callback
|
|||||||
from homeassistant.data_entry_flow import FlowResultType
|
from homeassistant.data_entry_flow import FlowResultType
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.dispatcher import (
|
from homeassistant.helpers.dispatcher import (
|
||||||
SignalTypeFormat,
|
|
||||||
async_dispatcher_connect,
|
async_dispatcher_connect,
|
||||||
async_dispatcher_send,
|
async_dispatcher_send,
|
||||||
)
|
)
|
||||||
@ -26,6 +25,7 @@ from homeassistant.helpers.service_info.mqtt import MqttServiceInfo
|
|||||||
from homeassistant.helpers.typing import DiscoveryInfoType
|
from homeassistant.helpers.typing import DiscoveryInfoType
|
||||||
from homeassistant.loader import async_get_mqtt
|
from homeassistant.loader import async_get_mqtt
|
||||||
from homeassistant.util.json import json_loads_object
|
from homeassistant.util.json import json_loads_object
|
||||||
|
from homeassistant.util.signal_type import SignalTypeFormat
|
||||||
|
|
||||||
from .. import mqtt
|
from .. import mqtt
|
||||||
from .abbreviations import ABBREVIATIONS, DEVICE_ABBREVIATIONS, ORIGIN_ABBREVIATIONS
|
from .abbreviations import ABBREVIATIONS, DEVICE_ABBREVIATIONS, ORIGIN_ABBREVIATIONS
|
||||||
|
@ -13,6 +13,7 @@ from .helpers.deprecation import (
|
|||||||
check_if_deprecated_constant,
|
check_if_deprecated_constant,
|
||||||
dir_with_deprecated_constants,
|
dir_with_deprecated_constants,
|
||||||
)
|
)
|
||||||
|
from .util.signal_type import SignalType
|
||||||
|
|
||||||
APPLICATION_NAME: Final = "HomeAssistant"
|
APPLICATION_NAME: Final = "HomeAssistant"
|
||||||
MAJOR_VERSION: Final = 2024
|
MAJOR_VERSION: Final = 2024
|
||||||
@ -1609,7 +1610,9 @@ CAST_APP_ID_HOMEASSISTANT_LOVELACE: Final = "A078F6B0"
|
|||||||
# User used by Supervisor
|
# User used by Supervisor
|
||||||
HASSIO_USER_NAME = "Supervisor"
|
HASSIO_USER_NAME = "Supervisor"
|
||||||
|
|
||||||
SIGNAL_BOOTSTRAP_INTEGRATIONS = "bootstrap_integrations"
|
SIGNAL_BOOTSTRAP_INTEGRATIONS: SignalType[dict[str, float]] = SignalType(
|
||||||
|
"bootstrap_integrations"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
# hass.data key for logging information.
|
# hass.data key for logging information.
|
||||||
|
@ -15,11 +15,8 @@ from homeassistant import core, setup
|
|||||||
from homeassistant.const import Platform
|
from homeassistant.const import Platform
|
||||||
from homeassistant.loader import bind_hass
|
from homeassistant.loader import bind_hass
|
||||||
|
|
||||||
from .dispatcher import (
|
from ..util.signal_type import SignalTypeFormat
|
||||||
SignalTypeFormat,
|
from .dispatcher import async_dispatcher_connect, async_dispatcher_send
|
||||||
async_dispatcher_connect,
|
|
||||||
async_dispatcher_send,
|
|
||||||
)
|
|
||||||
from .typing import ConfigType, DiscoveryInfoType
|
from .typing import ConfigType, DiscoveryInfoType
|
||||||
|
|
||||||
SIGNAL_PLATFORM_DISCOVERED: SignalTypeFormat[DiscoveryDict] = SignalTypeFormat(
|
SIGNAL_PLATFORM_DISCOVERED: SignalTypeFormat[DiscoveryDict] = SignalTypeFormat(
|
||||||
|
@ -3,57 +3,24 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import Callable, Coroutine
|
from collections.abc import Callable, Coroutine
|
||||||
from dataclasses import dataclass
|
|
||||||
from functools import partial
|
from functools import partial
|
||||||
import logging
|
import logging
|
||||||
from typing import Any, Generic, TypeVarTuple, overload
|
from typing import Any, TypeVarTuple, overload
|
||||||
|
|
||||||
from homeassistant.core import HassJob, HomeAssistant, callback
|
from homeassistant.core import HassJob, HomeAssistant, callback
|
||||||
from homeassistant.loader import bind_hass
|
from homeassistant.loader import bind_hass
|
||||||
from homeassistant.util.async_ import run_callback_threadsafe
|
from homeassistant.util.async_ import run_callback_threadsafe
|
||||||
from homeassistant.util.logging import catch_log_exception
|
from homeassistant.util.logging import catch_log_exception
|
||||||
|
|
||||||
|
# Explicit reexport of 'SignalType' for backwards compatibility
|
||||||
|
from homeassistant.util.signal_type import SignalType as SignalType # noqa: PLC0414
|
||||||
|
|
||||||
_Ts = TypeVarTuple("_Ts")
|
_Ts = TypeVarTuple("_Ts")
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
DATA_DISPATCHER = "dispatcher"
|
DATA_DISPATCHER = "dispatcher"
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
|
||||||
class _SignalTypeBase(Generic[*_Ts]):
|
|
||||||
"""Generic base class for SignalType."""
|
|
||||||
|
|
||||||
name: str
|
|
||||||
|
|
||||||
def __hash__(self) -> int:
|
|
||||||
"""Return hash of name."""
|
|
||||||
|
|
||||||
return hash(self.name)
|
|
||||||
|
|
||||||
def __eq__(self, other: Any) -> bool:
|
|
||||||
"""Check equality for dict keys to be compatible with str."""
|
|
||||||
|
|
||||||
if isinstance(other, str):
|
|
||||||
return self.name == other
|
|
||||||
if isinstance(other, SignalType):
|
|
||||||
return self.name == other.name
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True, eq=False)
|
|
||||||
class SignalType(_SignalTypeBase[*_Ts]):
|
|
||||||
"""Generic string class for signal to improve typing."""
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True, eq=False)
|
|
||||||
class SignalTypeFormat(_SignalTypeBase[*_Ts]):
|
|
||||||
"""Generic string class for signal. Requires call to 'format' before use."""
|
|
||||||
|
|
||||||
def format(self, *args: Any, **kwargs: Any) -> SignalType[*_Ts]:
|
|
||||||
"""Format name and return new SignalType instance."""
|
|
||||||
return SignalType(self.name.format(*args, **kwargs))
|
|
||||||
|
|
||||||
|
|
||||||
_DispatcherDataType = dict[
|
_DispatcherDataType = dict[
|
||||||
SignalType[*_Ts] | str,
|
SignalType[*_Ts] | str,
|
||||||
dict[
|
dict[
|
||||||
|
43
homeassistant/util/signal_type.py
Normal file
43
homeassistant/util/signal_type.py
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
"""Define SignalTypes for dispatcher."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import Any, Generic, TypeVarTuple
|
||||||
|
|
||||||
|
_Ts = TypeVarTuple("_Ts")
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class _SignalTypeBase(Generic[*_Ts]):
|
||||||
|
"""Generic base class for SignalType."""
|
||||||
|
|
||||||
|
name: str
|
||||||
|
|
||||||
|
def __hash__(self) -> int:
|
||||||
|
"""Return hash of name."""
|
||||||
|
|
||||||
|
return hash(self.name)
|
||||||
|
|
||||||
|
def __eq__(self, other: Any) -> bool:
|
||||||
|
"""Check equality for dict keys to be compatible with str."""
|
||||||
|
|
||||||
|
if isinstance(other, str):
|
||||||
|
return self.name == other
|
||||||
|
if isinstance(other, SignalType):
|
||||||
|
return self.name == other.name
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True, eq=False)
|
||||||
|
class SignalType(_SignalTypeBase[*_Ts]):
|
||||||
|
"""Generic string class for signal to improve typing."""
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True, eq=False)
|
||||||
|
class SignalTypeFormat(_SignalTypeBase[*_Ts]):
|
||||||
|
"""Generic string class for signal. Requires call to 'format' before use."""
|
||||||
|
|
||||||
|
def format(self, *args: Any, **kwargs: Any) -> SignalType[*_Ts]:
|
||||||
|
"""Format name and return new SignalType instance."""
|
||||||
|
return SignalType(self.name.format(*args, **kwargs))
|
@ -6,11 +6,10 @@ import pytest
|
|||||||
|
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.dispatcher import (
|
from homeassistant.helpers.dispatcher import (
|
||||||
SignalType,
|
|
||||||
SignalTypeFormat,
|
|
||||||
async_dispatcher_connect,
|
async_dispatcher_connect,
|
||||||
async_dispatcher_send,
|
async_dispatcher_send,
|
||||||
)
|
)
|
||||||
|
from homeassistant.util.signal_type import SignalType, SignalTypeFormat
|
||||||
|
|
||||||
|
|
||||||
async def test_simple_function(hass: HomeAssistant) -> None:
|
async def test_simple_function(hass: HomeAssistant) -> None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user