Only load the device entry when it changes in the base entity (#95801)

This commit is contained in:
J. Nick Koston 2023-07-10 03:17:35 -10:00 committed by GitHub
parent 3cc66c8318
commit 039a3bb6e9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 12 deletions

View file

@ -277,6 +277,9 @@ class Entity(ABC):
# Entry in the entity registry # Entry in the entity registry
registry_entry: er.RegistryEntry | None = None registry_entry: er.RegistryEntry | None = None
# The device entry for this entity
device_entry: dr.DeviceEntry | None = None
# Hold list for functions to call on remove. # Hold list for functions to call on remove.
_on_remove: list[CALLBACK_TYPE] | None = None _on_remove: list[CALLBACK_TYPE] | None = None
@ -763,13 +766,7 @@ class Entity(ABC):
if name is UNDEFINED: if name is UNDEFINED:
name = None name = None
if not self.has_entity_name or not self.registry_entry: if not self.has_entity_name or not (device_entry := self.device_entry):
return name
device_registry = dr.async_get(self.hass)
if not (device_id := self.registry_entry.device_id) or not (
device_entry := device_registry.async_get(device_id)
):
return name return name
device_name = device_entry.name_by_user or device_entry.name device_name = device_entry.name_by_user or device_entry.name
@ -1116,22 +1113,26 @@ class Entity(ABC):
ent_reg = er.async_get(self.hass) ent_reg = er.async_get(self.hass)
old = self.registry_entry old = self.registry_entry
self.registry_entry = ent_reg.async_get(data["entity_id"]) registry_entry = ent_reg.async_get(data["entity_id"])
assert self.registry_entry is not None assert registry_entry is not None
self.registry_entry = registry_entry
if self.registry_entry.disabled: if device_id := registry_entry.device_id:
self.device_entry = dr.async_get(self.hass).async_get(device_id)
if registry_entry.disabled:
await self.async_remove() await self.async_remove()
return return
assert old is not None assert old is not None
if self.registry_entry.entity_id == old.entity_id: if registry_entry.entity_id == old.entity_id:
self.async_registry_entry_updated() self.async_registry_entry_updated()
self.async_write_ha_state() self.async_write_ha_state()
return return
await self.async_remove(force_remove=True) await self.async_remove(force_remove=True)
self.entity_id = self.registry_entry.entity_id self.entity_id = registry_entry.entity_id
await self.platform.async_add_entities([self]) await self.platform.async_add_entities([self])
@callback @callback
@ -1153,6 +1154,7 @@ class Entity(ABC):
if "name" not in data["changes"] and "name_by_user" not in data["changes"]: if "name" not in data["changes"] and "name_by_user" not in data["changes"]:
return return
self.device_entry = dr.async_get(self.hass).async_get(data["device_id"])
self.async_write_ha_state() self.async_write_ha_state()
@callback @callback

View file

@ -738,6 +738,8 @@ class EntityPlatform:
) )
entity.registry_entry = entry entity.registry_entry = entry
if device:
entity.device_entry = device
entity.entity_id = entry.entity_id entity.entity_id = entry.entity_id
# We won't generate an entity ID if the platform has already set one # We won't generate an entity ID if the platform has already set one