mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 17:27:10 +00:00
Adjust switch as X to inherit entity category (#79081)
This commit is contained in:
parent
87e0c555db
commit
fe5e3320d4
@ -32,6 +32,7 @@ async def async_setup_entry(
|
|||||||
)
|
)
|
||||||
wrapped_switch = registry.async_get(entity_id)
|
wrapped_switch = registry.async_get(entity_id)
|
||||||
device_id = wrapped_switch.device_id if wrapped_switch else None
|
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(
|
async_add_entities(
|
||||||
[
|
[
|
||||||
@ -40,6 +41,7 @@ async def async_setup_entry(
|
|||||||
entity_id,
|
entity_id,
|
||||||
config_entry.entry_id,
|
config_entry.entry_id,
|
||||||
device_id,
|
device_id,
|
||||||
|
entity_category,
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
@ -13,7 +13,7 @@ from homeassistant.const import (
|
|||||||
)
|
)
|
||||||
from homeassistant.core import Event, callback
|
from homeassistant.core import Event, callback
|
||||||
from homeassistant.helpers import entity_registry as er
|
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
|
from homeassistant.helpers.event import async_track_state_change_event
|
||||||
|
|
||||||
|
|
||||||
@ -28,9 +28,11 @@ class BaseEntity(Entity):
|
|||||||
switch_entity_id: str,
|
switch_entity_id: str,
|
||||||
unique_id: str | None,
|
unique_id: str | None,
|
||||||
device_id: str | None = None,
|
device_id: str | None = None,
|
||||||
|
entity_category: EntityCategory | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize Light Switch."""
|
"""Initialize Light Switch."""
|
||||||
self._device_id = device_id
|
self._device_id = device_id
|
||||||
|
self._attr_entity_category = entity_category
|
||||||
self._attr_name = name
|
self._attr_name = name
|
||||||
self._attr_unique_id = unique_id
|
self._attr_unique_id = unique_id
|
||||||
self._switch_entity_id = switch_entity_id
|
self._switch_entity_id = switch_entity_id
|
||||||
|
@ -25,6 +25,7 @@ async def async_setup_entry(
|
|||||||
)
|
)
|
||||||
wrapped_switch = registry.async_get(entity_id)
|
wrapped_switch = registry.async_get(entity_id)
|
||||||
device_id = wrapped_switch.device_id if wrapped_switch else None
|
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(
|
async_add_entities(
|
||||||
[
|
[
|
||||||
@ -33,6 +34,7 @@ async def async_setup_entry(
|
|||||||
entity_id,
|
entity_id,
|
||||||
config_entry.entry_id,
|
config_entry.entry_id,
|
||||||
device_id,
|
device_id,
|
||||||
|
entity_category,
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
@ -23,6 +23,7 @@ async def async_setup_entry(
|
|||||||
)
|
)
|
||||||
wrapped_switch = registry.async_get(entity_id)
|
wrapped_switch = registry.async_get(entity_id)
|
||||||
device_id = wrapped_switch.device_id if wrapped_switch else None
|
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(
|
async_add_entities(
|
||||||
[
|
[
|
||||||
@ -31,6 +32,7 @@ async def async_setup_entry(
|
|||||||
entity_id,
|
entity_id,
|
||||||
config_entry.entry_id,
|
config_entry.entry_id,
|
||||||
device_id,
|
device_id,
|
||||||
|
entity_category,
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
@ -32,6 +32,7 @@ async def async_setup_entry(
|
|||||||
)
|
)
|
||||||
wrapped_switch = registry.async_get(entity_id)
|
wrapped_switch = registry.async_get(entity_id)
|
||||||
device_id = wrapped_switch.device_id if wrapped_switch else None
|
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(
|
async_add_entities(
|
||||||
[
|
[
|
||||||
@ -40,6 +41,7 @@ async def async_setup_entry(
|
|||||||
entity_id,
|
entity_id,
|
||||||
config_entry.entry_id,
|
config_entry.entry_id,
|
||||||
device_id,
|
device_id,
|
||||||
|
entity_category,
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
@ -23,6 +23,7 @@ async def async_setup_entry(
|
|||||||
)
|
)
|
||||||
wrapped_switch = registry.async_get(entity_id)
|
wrapped_switch = registry.async_get(entity_id)
|
||||||
device_id = wrapped_switch.device_id if wrapped_switch else None
|
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(
|
async_add_entities(
|
||||||
[
|
[
|
||||||
@ -31,6 +32,7 @@ async def async_setup_entry(
|
|||||||
entity_id,
|
entity_id,
|
||||||
config_entry.entry_id,
|
config_entry.entry_id,
|
||||||
device_id,
|
device_id,
|
||||||
|
entity_category,
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
@ -18,6 +18,7 @@ from homeassistant.const import (
|
|||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||||
|
from homeassistant.helpers.entity import EntityCategory
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
@ -403,3 +404,37 @@ async def test_reset_hidden_by(
|
|||||||
# Check hidden by is reset
|
# Check hidden by is reset
|
||||||
switch_entity_entry = registry.async_get(switch_entity_entry.entity_id)
|
switch_entity_entry = registry.async_get(switch_entity_entry.entity_id)
|
||||||
assert switch_entity_entry.hidden_by == hidden_by_after
|
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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user