Skip to content

Commit

Permalink
Make use_device_name a cached_property in the base entity class (#119758
Browse files Browse the repository at this point in the history
)

There is a small risk that _attr_name or entity_description
would not be set before state was first written, but that
would likely be a bug.
  • Loading branch information
bdraco committed Jun 19, 2024
1 parent 6a3778c commit 5ae13c2
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions homeassistant/helpers/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,22 +581,20 @@ def unique_id(self) -> str | None:
"""Return a unique ID."""
return self._attr_unique_id

@property
@cached_property
def use_device_name(self) -> bool:
"""Return if this entity does not have its own name.
Should be True if the entity represents the single main feature of a device.
"""
if hasattr(self, "_attr_name"):
return not self._attr_name

if name_translation_key := self._name_translation_key:
if name_translation_key in self.platform.platform_translations:
return False

if (
name_translation_key := self._name_translation_key
) and name_translation_key in self.platform.platform_translations:
return False
if hasattr(self, "entity_description"):
return not self.entity_description.name

return not self.name

@cached_property
Expand Down

0 comments on commit 5ae13c2

Please sign in to comment.