Use EventType for device_registry_updated (#115188)

This commit is contained in:
Marc Mueller 2024-04-08 20:25:57 +02:00 committed by GitHub
parent 0d18679c8f
commit 2fc0d8494d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 10 deletions

View File

@ -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.core import Event, HomeAssistant, callback, get_release_channel
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.loader import async_suggest_report_issue from homeassistant.loader import async_suggest_report_issue
from homeassistant.util.event_type import EventType
from homeassistant.util.json import format_unserializable_data from homeassistant.util.json import format_unserializable_data
import homeassistant.util.uuid as uuid_util import homeassistant.util.uuid as uuid_util
@ -40,7 +41,9 @@ if TYPE_CHECKING:
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
DATA_REGISTRY = "device_registry" 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_KEY = "core.device_registry"
STORAGE_VERSION_MAJOR = 1 STORAGE_VERSION_MAJOR = 1
STORAGE_VERSION_MINOR = 5 STORAGE_VERSION_MINOR = 5
@ -908,12 +911,11 @@ class DeviceRegistry(BaseRegistry[dict[str, list[dict[str, Any]]]]):
self.async_schedule_save() self.async_schedule_save()
data: dict[str, Any] = { data: EventDeviceRegistryUpdatedData
"action": "create" if old.is_new else "update", if old.is_new:
"device_id": new.id, data = {"action": "create", "device_id": new.id}
} else:
if not old.is_new: data = {"action": "update", "device_id": new.id, "changes": old_values}
data["changes"] = old_values
self.hass.bus.async_fire(EVENT_DEVICE_REGISTRY_UPDATED, data) 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: if other_device.via_device_id == device_id:
self.async_update_device(other_device.id, via_device_id=None) self.async_update_device(other_device.id, via_device_id=None)
self.hass.bus.async_fire( 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() self.async_schedule_save()

View File

@ -51,7 +51,10 @@ from homeassistant.util.json import format_unserializable_data
from homeassistant.util.read_only_dict import ReadOnlyDict from homeassistant.util.read_only_dict import ReadOnlyDict
from . import device_registry as dr, storage 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 .json import JSON_DUMP, find_paths_unserializable_data, json_bytes, json_fragment
from .registry import BaseRegistry, BaseRegistryItems from .registry import BaseRegistry, BaseRegistryItems
from .typing import UNDEFINED, UndefinedType from .typing import UNDEFINED, UndefinedType
@ -903,7 +906,9 @@ class EntityRegistry(BaseRegistry):
self.async_schedule_save() self.async_schedule_save()
@callback @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. """Handle the removal or update of a device.
Remove entities from the registry that are associated to a device when Remove entities from the registry that are associated to a device when