diff --git a/homeassistant/helpers/device_registry.py b/homeassistant/helpers/device_registry.py index dc3f4bff434..df2c5b57395 100644 --- a/homeassistant/helpers/device_registry.py +++ b/homeassistant/helpers/device_registry.py @@ -16,6 +16,7 @@ from homeassistant.const import EVENT_HOMEASSISTANT_STARTED, EVENT_HOMEASSISTANT from homeassistant.core import Event, HomeAssistant, callback, get_release_channel from homeassistant.exceptions import HomeAssistantError from homeassistant.loader import async_suggest_report_issue +from homeassistant.util.event_type import EventType from homeassistant.util.json import format_unserializable_data import homeassistant.util.uuid as uuid_util @@ -40,7 +41,9 @@ if TYPE_CHECKING: _LOGGER = logging.getLogger(__name__) DATA_REGISTRY = "device_registry" -EVENT_DEVICE_REGISTRY_UPDATED = "device_registry_updated" +EVENT_DEVICE_REGISTRY_UPDATED: EventType[EventDeviceRegistryUpdatedData] = EventType( + "device_registry_updated" +) STORAGE_KEY = "core.device_registry" STORAGE_VERSION_MAJOR = 1 STORAGE_VERSION_MINOR = 5 @@ -908,12 +911,11 @@ class DeviceRegistry(BaseRegistry[dict[str, list[dict[str, Any]]]]): self.async_schedule_save() - data: dict[str, Any] = { - "action": "create" if old.is_new else "update", - "device_id": new.id, - } - if not old.is_new: - data["changes"] = old_values + data: EventDeviceRegistryUpdatedData + if old.is_new: + data = {"action": "create", "device_id": new.id} + else: + data = {"action": "update", "device_id": new.id, "changes": old_values} self.hass.bus.async_fire(EVENT_DEVICE_REGISTRY_UPDATED, data) @@ -934,7 +936,10 @@ class DeviceRegistry(BaseRegistry[dict[str, list[dict[str, Any]]]]): if other_device.via_device_id == device_id: self.async_update_device(other_device.id, via_device_id=None) self.hass.bus.async_fire( - EVENT_DEVICE_REGISTRY_UPDATED, {"action": "remove", "device_id": device_id} + EVENT_DEVICE_REGISTRY_UPDATED, + _EventDeviceRegistryUpdatedData_CreateRemove( + action="remove", device_id=device_id + ), ) self.async_schedule_save() diff --git a/homeassistant/helpers/entity_registry.py b/homeassistant/helpers/entity_registry.py index b1e92f51c2c..1197e79ad8d 100644 --- a/homeassistant/helpers/entity_registry.py +++ b/homeassistant/helpers/entity_registry.py @@ -51,7 +51,10 @@ from homeassistant.util.json import format_unserializable_data from homeassistant.util.read_only_dict import ReadOnlyDict from . import device_registry as dr, storage -from .device_registry import EVENT_DEVICE_REGISTRY_UPDATED +from .device_registry import ( + EVENT_DEVICE_REGISTRY_UPDATED, + EventDeviceRegistryUpdatedData, +) from .json import JSON_DUMP, find_paths_unserializable_data, json_bytes, json_fragment from .registry import BaseRegistry, BaseRegistryItems from .typing import UNDEFINED, UndefinedType @@ -903,7 +906,9 @@ class EntityRegistry(BaseRegistry): self.async_schedule_save() @callback - def async_device_modified(self, event: Event) -> None: + def async_device_modified( + self, event: Event[EventDeviceRegistryUpdatedData] + ) -> None: """Handle the removal or update of a device. Remove entities from the registry that are associated to a device when