Add visible by default property to base Entity (#70370)

This commit is contained in:
Franck Nijhof 2022-04-22 07:06:34 +02:00 committed by GitHub
parent 9761a7310e
commit 7b75a16745
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 1 deletions

View file

@ -230,6 +230,7 @@ class EntityDescription:
device_class: str | None = None
entity_category: EntityCategory | None = None
entity_registry_enabled_default: bool = True
entity_registry_visible_default: bool = False
force_update: bool = False
icon: str | None = None
name: str | None = None
@ -293,6 +294,7 @@ class Entity(ABC):
_attr_entity_category: EntityCategory | None
_attr_entity_picture: str | None = None
_attr_entity_registry_enabled_default: bool
_attr_entity_registry_visible_default: bool
_attr_extra_state_attributes: MutableMapping[str, Any]
_attr_force_update: bool
_attr_icon: str | None
@ -452,6 +454,15 @@ class Entity(ABC):
return self.entity_description.entity_registry_enabled_default
return True
@property
def entity_registry_visible_default(self) -> bool:
"""Return if the entity should be visible when first added to the entity registry."""
if hasattr(self, "_attr_entity_registry_visible_default"):
return self._attr_entity_registry_visible_default
if hasattr(self, "entity_description"):
return self.entity_description.entity_registry_visible_default
return True
@property
def attribution(self) -> str | None:
"""Return the attribution."""

View file

@ -42,7 +42,7 @@ from . import (
service,
)
from .device_registry import DeviceRegistry
from .entity_registry import EntityRegistry, RegistryEntryDisabler
from .entity_registry import EntityRegistry, RegistryEntryDisabler, RegistryEntryHider
from .event import async_call_later, async_track_time_interval
from .typing import ConfigType, DiscoveryInfoType
@ -507,6 +507,10 @@ class EntityPlatform:
if not entity.entity_registry_enabled_default:
disabled_by = RegistryEntryDisabler.INTEGRATION
hidden_by: RegistryEntryHider | None = None
if not entity.entity_registry_visible_default:
hidden_by = RegistryEntryHider.INTEGRATION
entry = entity_registry.async_get_or_create(
self.domain,
self.platform_name,
@ -515,6 +519,7 @@ class EntityPlatform:
config_entry=self.config_entry,
device_id=device_id,
disabled_by=disabled_by,
hidden_by=hidden_by,
entity_category=entity.entity_category,
known_object_ids=self.entities.keys(),
original_device_class=entity.device_class,

View file

@ -1028,6 +1028,11 @@ class MockEntity(entity.Entity):
"""Return if the entity should be enabled when first added to the entity registry."""
return self._handle("entity_registry_enabled_default")
@property
def entity_registry_visible_default(self):
"""Return if the entity should be visible when first added to the entity registry."""
return self._handle("entity_registry_visible_default")
@property
def icon(self):
"""Return the suggested icon."""

View file

@ -1148,6 +1148,25 @@ async def test_entity_disabled_by_device(hass: HomeAssistant):
assert entry_disabled.disabled_by is er.RegistryEntryDisabler.DEVICE
async def test_entity_hidden_by_integration(hass):
"""Test entity hidden by integration."""
component = EntityComponent(_LOGGER, DOMAIN, hass, timedelta(seconds=20))
entity_default = MockEntity(unique_id="default")
entity_hidden = MockEntity(
unique_id="hidden", entity_registry_visible_default=False
)
await component.async_add_entities([entity_default, entity_hidden])
registry = er.async_get(hass)
entry_default = registry.async_get_or_create(DOMAIN, DOMAIN, "default")
assert entry_default.hidden_by is None
entry_hidden = registry.async_get_or_create(DOMAIN, DOMAIN, "hidden")
assert entry_hidden.hidden_by is er.RegistryEntryHider.INTEGRATION
async def test_entity_info_added_to_entity_registry(hass):
"""Test entity info is written to entity registry."""
component = EntityComponent(_LOGGER, DOMAIN, hass, timedelta(seconds=20))