Move SignalTypes to util (#114236)

pull/113018/head^2
Marc Mueller 2024-03-27 08:41:44 +01:00 committed by GitHub
parent fbb590ea1f
commit 1d2c2d2055
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 55 additions and 46 deletions

View File

@ -18,7 +18,6 @@ from homeassistant.core import HomeAssistant, callback
from homeassistant.data_entry_flow import FlowResultType
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import (
SignalTypeFormat,
async_dispatcher_connect,
async_dispatcher_send,
)
@ -26,6 +25,7 @@ from homeassistant.helpers.service_info.mqtt import MqttServiceInfo
from homeassistant.helpers.typing import DiscoveryInfoType
from homeassistant.loader import async_get_mqtt
from homeassistant.util.json import json_loads_object
from homeassistant.util.signal_type import SignalTypeFormat
from .. import mqtt
from .abbreviations import ABBREVIATIONS, DEVICE_ABBREVIATIONS, ORIGIN_ABBREVIATIONS

View File

@ -13,6 +13,7 @@ from .helpers.deprecation import (
check_if_deprecated_constant,
dir_with_deprecated_constants,
)
from .util.signal_type import SignalType
APPLICATION_NAME: Final = "HomeAssistant"
MAJOR_VERSION: Final = 2024
@ -1609,7 +1610,9 @@ CAST_APP_ID_HOMEASSISTANT_LOVELACE: Final = "A078F6B0"
# User used by 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.

View File

@ -15,11 +15,8 @@ from homeassistant import core, setup
from homeassistant.const import Platform
from homeassistant.loader import bind_hass
from .dispatcher import (
SignalTypeFormat,
async_dispatcher_connect,
async_dispatcher_send,
)
from ..util.signal_type import SignalTypeFormat
from .dispatcher import async_dispatcher_connect, async_dispatcher_send
from .typing import ConfigType, DiscoveryInfoType
SIGNAL_PLATFORM_DISCOVERED: SignalTypeFormat[DiscoveryDict] = SignalTypeFormat(

View File

@ -3,57 +3,24 @@
from __future__ import annotations
from collections.abc import Callable, Coroutine
from dataclasses import dataclass
from functools import partial
import logging
from typing import Any, Generic, TypeVarTuple, overload
from typing import Any, TypeVarTuple, overload
from homeassistant.core import HassJob, HomeAssistant, callback
from homeassistant.loader import bind_hass
from homeassistant.util.async_ import run_callback_threadsafe
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")
_LOGGER = logging.getLogger(__name__)
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[
SignalType[*_Ts] | str,
dict[

View 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))

View File

@ -6,11 +6,10 @@ import pytest
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import (
SignalType,
SignalTypeFormat,
async_dispatcher_connect,
async_dispatcher_send,
)
from homeassistant.util.signal_type import SignalType, SignalTypeFormat
async def test_simple_function(hass: HomeAssistant) -> None: