Move thread safety in label_registry sooner (#117026)

This commit is contained in:
J. Nick Koston 2024-05-07 15:15:30 -05:00 committed by GitHub
parent 9557ea902e
commit fc3c384e0a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 49 additions and 3 deletions

View file

@ -121,6 +121,7 @@ class LabelRegistry(BaseRegistry[LabelRegistryStoreData]):
description: str | None = None,
) -> LabelEntry:
"""Create a new label."""
self.hass.verify_event_loop_thread("async_create")
if label := self.async_get_label_by_name(name):
raise ValueError(
f"The name {name} ({label.normalized_name}) is already in use"
@ -139,7 +140,7 @@ class LabelRegistry(BaseRegistry[LabelRegistryStoreData]):
label_id = label.label_id
self.labels[label_id] = label
self.async_schedule_save()
self.hass.bus.async_fire(
self.hass.bus.async_fire_internal(
EVENT_LABEL_REGISTRY_UPDATED,
EventLabelRegistryUpdatedData(
action="create",
@ -151,8 +152,9 @@ class LabelRegistry(BaseRegistry[LabelRegistryStoreData]):
@callback
def async_delete(self, label_id: str) -> None:
"""Delete label."""
self.hass.verify_event_loop_thread("async_delete")
del self.labels[label_id]
self.hass.bus.async_fire(
self.hass.bus.async_fire_internal(
EVENT_LABEL_REGISTRY_UPDATED,
EventLabelRegistryUpdatedData(
action="remove",
@ -190,10 +192,11 @@ class LabelRegistry(BaseRegistry[LabelRegistryStoreData]):
if not changes:
return old
self.hass.verify_event_loop_thread("async_update")
new = self.labels[label_id] = dataclasses.replace(old, **changes) # type: ignore[arg-type]
self.async_schedule_save()
self.hass.bus.async_fire(
self.hass.bus.async_fire_internal(
EVENT_LABEL_REGISTRY_UPDATED,
EventLabelRegistryUpdatedData(
action="update",