Improve async_track_device_registry_updated_event callback typing (#97125)
This commit is contained in:
@@ -10,7 +10,6 @@ from typing import TYPE_CHECKING, Any, Literal, TypedDict, TypeVar, cast
|
||||
from urllib.parse import urlparse
|
||||
|
||||
import attr
|
||||
from typing_extensions import NotRequired
|
||||
|
||||
from homeassistant.const import EVENT_HOMEASSISTANT_STARTED, EVENT_HOMEASSISTANT_STOP
|
||||
from homeassistant.core import Event, HomeAssistant, callback
|
||||
@@ -97,12 +96,25 @@ DEVICE_INFO_TYPES = {
|
||||
DEVICE_INFO_KEYS = set.union(*(itm for itm in DEVICE_INFO_TYPES.values()))
|
||||
|
||||
|
||||
class EventDeviceRegistryUpdatedData(TypedDict):
|
||||
"""EventDeviceRegistryUpdated data."""
|
||||
class _EventDeviceRegistryUpdatedData_CreateRemove(TypedDict):
|
||||
"""EventDeviceRegistryUpdated data for action type 'create' and 'remove'."""
|
||||
|
||||
action: Literal["create", "remove", "update"]
|
||||
action: Literal["create", "remove"]
|
||||
device_id: str
|
||||
changes: NotRequired[dict[str, Any]]
|
||||
|
||||
|
||||
class _EventDeviceRegistryUpdatedData_Update(TypedDict):
|
||||
"""EventDeviceRegistryUpdated data for action type 'update'."""
|
||||
|
||||
action: Literal["update"]
|
||||
device_id: str
|
||||
changes: dict[str, Any]
|
||||
|
||||
|
||||
EventDeviceRegistryUpdatedData = (
|
||||
_EventDeviceRegistryUpdatedData_CreateRemove
|
||||
| _EventDeviceRegistryUpdatedData_Update
|
||||
)
|
||||
|
||||
|
||||
class DeviceEntryType(StrEnum):
|
||||
|
||||
@@ -40,7 +40,7 @@ from homeassistant.loader import bind_hass
|
||||
from homeassistant.util import dt as dt_util, ensure_unique_string, slugify
|
||||
|
||||
from . import device_registry as dr, entity_registry as er
|
||||
from .device_registry import DeviceEntryType
|
||||
from .device_registry import DeviceEntryType, EventDeviceRegistryUpdatedData
|
||||
from .event import (
|
||||
async_track_device_registry_updated_event,
|
||||
async_track_entity_registry_updated_event,
|
||||
@@ -1146,7 +1146,9 @@ class Entity(ABC):
|
||||
self._unsub_device_updates = None
|
||||
|
||||
@callback
|
||||
def _async_device_registry_updated(self, event: Event) -> None:
|
||||
def _async_device_registry_updated(
|
||||
self, event: EventType[EventDeviceRegistryUpdatedData]
|
||||
) -> None:
|
||||
"""Handle device registry update."""
|
||||
data = event.data
|
||||
|
||||
|
||||
Reference in New Issue
Block a user