Adjust switch as X to inherit entity category (#79081)

pull/79103/head
Franck Nijhof 2022-09-26 10:22:45 +02:00 committed by GitHub
parent 87e0c555db
commit fe5e3320d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 48 additions and 1 deletions

View File

@ -32,6 +32,7 @@ async def async_setup_entry(
)
wrapped_switch = registry.async_get(entity_id)
device_id = wrapped_switch.device_id if wrapped_switch else None
entity_category = wrapped_switch.entity_category if wrapped_switch else None
async_add_entities(
[
@ -40,6 +41,7 @@ async def async_setup_entry(
entity_id,
config_entry.entry_id,
device_id,
entity_category,
)
]
)

View File

@ -13,7 +13,7 @@ from homeassistant.const import (
)
from homeassistant.core import Event, callback
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity import Entity, ToggleEntity
from homeassistant.helpers.entity import Entity, EntityCategory, ToggleEntity
from homeassistant.helpers.event import async_track_state_change_event
@ -28,9 +28,11 @@ class BaseEntity(Entity):
switch_entity_id: str,
unique_id: str | None,
device_id: str | None = None,
entity_category: EntityCategory | None = None,
) -> None:
"""Initialize Light Switch."""
self._device_id = device_id
self._attr_entity_category = entity_category
self._attr_name = name
self._attr_unique_id = unique_id
self._switch_entity_id = switch_entity_id

View File

@ -25,6 +25,7 @@ async def async_setup_entry(
)
wrapped_switch = registry.async_get(entity_id)
device_id = wrapped_switch.device_id if wrapped_switch else None
entity_category = wrapped_switch.entity_category if wrapped_switch else None
async_add_entities(
[
@ -33,6 +34,7 @@ async def async_setup_entry(
entity_id,
config_entry.entry_id,
device_id,
entity_category,
)
]
)

View File

@ -23,6 +23,7 @@ async def async_setup_entry(
)
wrapped_switch = registry.async_get(entity_id)
device_id = wrapped_switch.device_id if wrapped_switch else None
entity_category = wrapped_switch.entity_category if wrapped_switch else None
async_add_entities(
[
@ -31,6 +32,7 @@ async def async_setup_entry(
entity_id,
config_entry.entry_id,
device_id,
entity_category,
)
]
)

View File

@ -32,6 +32,7 @@ async def async_setup_entry(
)
wrapped_switch = registry.async_get(entity_id)
device_id = wrapped_switch.device_id if wrapped_switch else None
entity_category = wrapped_switch.entity_category if wrapped_switch else None
async_add_entities(
[
@ -40,6 +41,7 @@ async def async_setup_entry(
entity_id,
config_entry.entry_id,
device_id,
entity_category,
)
]
)

View File

@ -23,6 +23,7 @@ async def async_setup_entry(
)
wrapped_switch = registry.async_get(entity_id)
device_id = wrapped_switch.device_id if wrapped_switch else None
entity_category = wrapped_switch.entity_category if wrapped_switch else None
async_add_entities(
[
@ -31,6 +32,7 @@ async def async_setup_entry(
entity_id,
config_entry.entry_id,
device_id,
entity_category,
)
]
)

View File

@ -18,6 +18,7 @@ from homeassistant.const import (
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.entity import EntityCategory
from tests.common import MockConfigEntry
@ -403,3 +404,37 @@ async def test_reset_hidden_by(
# Check hidden by is reset
switch_entity_entry = registry.async_get(switch_entity_entry.entity_id)
assert switch_entity_entry.hidden_by == hidden_by_after
@pytest.mark.parametrize("target_domain", PLATFORMS_TO_TEST)
async def test_entity_category_inheritance(
hass: HomeAssistant,
target_domain: Platform,
) -> None:
"""Test the entity category is inherited from source device."""
registry = er.async_get(hass)
switch_entity_entry = registry.async_get_or_create("switch", "test", "unique")
registry.async_update_entity(
switch_entity_entry.entity_id, entity_category=EntityCategory.CONFIG
)
# Add the config entry
switch_as_x_config_entry = MockConfigEntry(
data={},
domain=DOMAIN,
options={
CONF_ENTITY_ID: switch_entity_entry.id,
CONF_TARGET_DOMAIN: target_domain,
},
title="ABC",
)
switch_as_x_config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(switch_as_x_config_entry.entry_id)
await hass.async_block_till_done()
entity_entry = registry.async_get(f"{target_domain}.abc")
assert entity_entry
assert entity_entry.device_id == switch_entity_entry.device_id
assert entity_entry.entity_category is EntityCategory.CONFIG