Use EventType for entity_registry_updated (#115187)
This commit is contained in:
parent
025c6f5e2e
commit
4e94f11665
4 changed files with 35 additions and 15 deletions
|
@ -2,7 +2,7 @@
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable, Mapping
|
||||
from collections.abc import Callable
|
||||
import logging
|
||||
from typing import Any, Self
|
||||
|
||||
|
@ -17,7 +17,6 @@ from homeassistant.components.device_tracker import (
|
|||
)
|
||||
from homeassistant.const import (
|
||||
ATTR_EDITABLE,
|
||||
ATTR_ENTITY_ID,
|
||||
ATTR_GPS_ACCURACY,
|
||||
ATTR_ID,
|
||||
ATTR_LATITUDE,
|
||||
|
@ -246,16 +245,20 @@ class PersonStorageCollection(collection.DictStorageCollection):
|
|||
)
|
||||
|
||||
@callback
|
||||
def _entity_registry_filter(self, event_data: Mapping[str, Any]) -> bool:
|
||||
def _entity_registry_filter(
|
||||
self, event_data: er.EventEntityRegistryUpdatedData
|
||||
) -> bool:
|
||||
"""Filter entity registry events."""
|
||||
return (
|
||||
event_data["action"] == "remove"
|
||||
and split_entity_id(event_data[ATTR_ENTITY_ID])[0] == "device_tracker"
|
||||
and split_entity_id(event_data["entity_id"])[0] == "device_tracker"
|
||||
)
|
||||
|
||||
async def _entity_registry_updated(self, event: Event) -> None:
|
||||
async def _entity_registry_updated(
|
||||
self, event: Event[er.EventEntityRegistryUpdatedData]
|
||||
) -> None:
|
||||
"""Handle entity registry updated."""
|
||||
entity_id = event.data[ATTR_ENTITY_ID]
|
||||
entity_id = event.data["entity_id"]
|
||||
for person in list(self.data.values()):
|
||||
if entity_id not in person[CONF_DEVICE_TRACKERS]:
|
||||
continue
|
||||
|
|
|
@ -2507,7 +2507,9 @@ class EntityRegistryDisabledHandler:
|
|||
)
|
||||
|
||||
@callback
|
||||
def _handle_entry_updated(self, event: Event) -> None:
|
||||
def _handle_entry_updated(
|
||||
self, event: Event[entity_registry.EventEntityRegistryUpdatedData]
|
||||
) -> None:
|
||||
"""Handle entity registry entry update."""
|
||||
if self.registry is None:
|
||||
self.registry = entity_registry.async_get(self.hass)
|
||||
|
@ -2574,7 +2576,9 @@ class EntityRegistryDisabledHandler:
|
|||
|
||||
|
||||
@callback
|
||||
def _handle_entry_updated_filter(event_data: Mapping[str, Any]) -> bool:
|
||||
def _handle_entry_updated_filter(
|
||||
event_data: entity_registry.EventEntityRegistryUpdatedData,
|
||||
) -> bool:
|
||||
"""Handle entity registry entry update filter.
|
||||
|
||||
Only handle changes to "disabled_by".
|
||||
|
|
|
@ -1221,12 +1221,16 @@ def async_setup_cleanup(hass: HomeAssistant, dev_reg: DeviceRegistry) -> None:
|
|||
)
|
||||
|
||||
@callback
|
||||
def _async_entity_registry_changed(event: Event) -> None:
|
||||
def _async_entity_registry_changed(
|
||||
event: Event[entity_registry.EventEntityRegistryUpdatedData],
|
||||
) -> None:
|
||||
"""Handle entity updated or removed dispatch."""
|
||||
debounced_cleanup.async_schedule_call()
|
||||
|
||||
@callback
|
||||
def entity_registry_changed_filter(event_data: Mapping[str, Any]) -> bool:
|
||||
def entity_registry_changed_filter(
|
||||
event_data: entity_registry.EventEntityRegistryUpdatedData,
|
||||
) -> bool:
|
||||
"""Handle entity updated or removed filter."""
|
||||
if (
|
||||
event_data["action"] == "update"
|
||||
|
|
|
@ -47,6 +47,7 @@ from homeassistant.core import (
|
|||
from homeassistant.exceptions import MaxLengthExceeded
|
||||
from homeassistant.loader import async_suggest_report_issue
|
||||
from homeassistant.util import slugify, uuid as uuid_util
|
||||
from homeassistant.util.event_type import EventType
|
||||
from homeassistant.util.json import format_unserializable_data
|
||||
from homeassistant.util.read_only_dict import ReadOnlyDict
|
||||
|
||||
|
@ -65,7 +66,9 @@ if TYPE_CHECKING:
|
|||
T = TypeVar("T")
|
||||
|
||||
DATA_REGISTRY = "entity_registry"
|
||||
EVENT_ENTITY_REGISTRY_UPDATED = "entity_registry_updated"
|
||||
EVENT_ENTITY_REGISTRY_UPDATED: EventType[EventEntityRegistryUpdatedData] = EventType(
|
||||
"entity_registry_updated"
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -879,7 +882,10 @@ class EntityRegistry(BaseRegistry):
|
|||
self.async_schedule_save()
|
||||
|
||||
self.hass.bus.async_fire(
|
||||
EVENT_ENTITY_REGISTRY_UPDATED, {"action": "create", "entity_id": entity_id}
|
||||
EVENT_ENTITY_REGISTRY_UPDATED,
|
||||
_EventEntityRegistryUpdatedData_CreateRemove(
|
||||
action="create", entity_id=entity_id
|
||||
),
|
||||
)
|
||||
|
||||
return entry
|
||||
|
@ -901,7 +907,10 @@ class EntityRegistry(BaseRegistry):
|
|||
unique_id=entity.unique_id,
|
||||
)
|
||||
self.hass.bus.async_fire(
|
||||
EVENT_ENTITY_REGISTRY_UPDATED, {"action": "remove", "entity_id": entity_id}
|
||||
EVENT_ENTITY_REGISTRY_UPDATED,
|
||||
_EventEntityRegistryUpdatedData_CreateRemove(
|
||||
action="remove", entity_id=entity_id
|
||||
),
|
||||
)
|
||||
self.async_schedule_save()
|
||||
|
||||
|
@ -1082,7 +1091,7 @@ class EntityRegistry(BaseRegistry):
|
|||
|
||||
self.async_schedule_save()
|
||||
|
||||
data: dict[str, str | dict[str, Any]] = {
|
||||
data: _EventEntityRegistryUpdatedData_Update = {
|
||||
"action": "update",
|
||||
"entity_id": entity_id,
|
||||
"changes": old_values,
|
||||
|
@ -1531,7 +1540,7 @@ def _async_setup_entity_restore(hass: HomeAssistant, registry: EntityRegistry) -
|
|||
return bool(event_data["action"] == "remove")
|
||||
|
||||
@callback
|
||||
def cleanup_restored_states(event: Event) -> None:
|
||||
def cleanup_restored_states(event: Event[EventEntityRegistryUpdatedData]) -> None:
|
||||
"""Clean up restored states."""
|
||||
state = hass.states.get(event.data["entity_id"])
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue