mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 10:17:09 +00:00
Store source entity in switch_as_x entity options (#88914)
This commit is contained in:
parent
8f6cfc25c0
commit
39f5f0946e
@ -17,6 +17,8 @@ from homeassistant.helpers import entity_registry as er
|
|||||||
from homeassistant.helpers.entity import Entity, ToggleEntity
|
from homeassistant.helpers.entity import Entity, ToggleEntity
|
||||||
from homeassistant.helpers.event import async_track_state_change_event
|
from homeassistant.helpers.event import async_track_state_change_event
|
||||||
|
|
||||||
|
from .const import DOMAIN as SWITCH_AS_X_DOMAIN
|
||||||
|
|
||||||
|
|
||||||
class BaseEntity(Entity):
|
class BaseEntity(Entity):
|
||||||
"""Represents a Switch as a X."""
|
"""Represents a Switch as a X."""
|
||||||
@ -28,8 +30,8 @@ class BaseEntity(Entity):
|
|||||||
name: str,
|
name: str,
|
||||||
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,
|
||||||
entity_category: EntityCategory | None = None,
|
entity_category: EntityCategory | None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize Light Switch."""
|
"""Initialize Light Switch."""
|
||||||
self._device_id = device_id
|
self._device_id = device_id
|
||||||
@ -71,6 +73,11 @@ class BaseEntity(Entity):
|
|||||||
registry = er.async_get(self.hass)
|
registry = er.async_get(self.hass)
|
||||||
if registry.async_get(self.entity_id) is not None:
|
if registry.async_get(self.entity_id) is not None:
|
||||||
registry.async_update_entity(self.entity_id, device_id=self._device_id)
|
registry.async_update_entity(self.entity_id, device_id=self._device_id)
|
||||||
|
registry.async_update_entity_options(
|
||||||
|
self.entity_id,
|
||||||
|
SWITCH_AS_X_DOMAIN,
|
||||||
|
{"entity_id": self._switch_entity_id},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class BaseToggleEntity(BaseEntity, ToggleEntity):
|
class BaseToggleEntity(BaseEntity, ToggleEntity):
|
||||||
|
@ -438,3 +438,39 @@ async def test_entity_category_inheritance(
|
|||||||
assert entity_entry
|
assert entity_entry
|
||||||
assert entity_entry.device_id == switch_entity_entry.device_id
|
assert entity_entry.device_id == switch_entity_entry.device_id
|
||||||
assert entity_entry.entity_category is EntityCategory.CONFIG
|
assert entity_entry.entity_category is EntityCategory.CONFIG
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("target_domain", PLATFORMS_TO_TEST)
|
||||||
|
async def test_entity_options(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
target_domain: Platform,
|
||||||
|
) -> None:
|
||||||
|
"""Test the source entity is stored as an entity option."""
|
||||||
|
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.options == {
|
||||||
|
DOMAIN: {"entity_id": switch_entity_entry.entity_id}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user