diff --git a/homeassistant/components/switch_as_x/entity.py b/homeassistant/components/switch_as_x/entity.py index bc24460a105..ac56b4c6078 100644 --- a/homeassistant/components/switch_as_x/entity.py +++ b/homeassistant/components/switch_as_x/entity.py @@ -17,6 +17,8 @@ from homeassistant.helpers import entity_registry as er from homeassistant.helpers.entity import Entity, ToggleEntity from homeassistant.helpers.event import async_track_state_change_event +from .const import DOMAIN as SWITCH_AS_X_DOMAIN + class BaseEntity(Entity): """Represents a Switch as a X.""" @@ -28,8 +30,8 @@ class BaseEntity(Entity): name: str, switch_entity_id: str, unique_id: str | None, - device_id: str | None = None, - entity_category: EntityCategory | None = None, + device_id: str | None, + entity_category: EntityCategory | None, ) -> None: """Initialize Light Switch.""" self._device_id = device_id @@ -71,6 +73,11 @@ class BaseEntity(Entity): registry = er.async_get(self.hass) 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_options( + self.entity_id, + SWITCH_AS_X_DOMAIN, + {"entity_id": self._switch_entity_id}, + ) class BaseToggleEntity(BaseEntity, ToggleEntity): diff --git a/tests/components/switch_as_x/test_init.py b/tests/components/switch_as_x/test_init.py index 964e0a0d433..a95725999d9 100644 --- a/tests/components/switch_as_x/test_init.py +++ b/tests/components/switch_as_x/test_init.py @@ -438,3 +438,39 @@ async def test_entity_category_inheritance( assert entity_entry assert entity_entry.device_id == switch_entity_entry.device_id 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} + }