From fe5e3320d41c462775c1fc22a788021aba81d538 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Mon, 26 Sep 2022 10:22:45 +0200 Subject: [PATCH] Adjust switch as X to inherit entity category (#79081) --- homeassistant/components/switch_as_x/cover.py | 2 ++ .../components/switch_as_x/entity.py | 4 ++- homeassistant/components/switch_as_x/fan.py | 2 ++ homeassistant/components/switch_as_x/light.py | 2 ++ homeassistant/components/switch_as_x/lock.py | 2 ++ homeassistant/components/switch_as_x/siren.py | 2 ++ tests/components/switch_as_x/test_init.py | 35 +++++++++++++++++++ 7 files changed, 48 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/switch_as_x/cover.py b/homeassistant/components/switch_as_x/cover.py index e480151a946..9d7a7bf6178 100644 --- a/homeassistant/components/switch_as_x/cover.py +++ b/homeassistant/components/switch_as_x/cover.py @@ -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, ) ] ) diff --git a/homeassistant/components/switch_as_x/entity.py b/homeassistant/components/switch_as_x/entity.py index 4d478154763..0bd784eacef 100644 --- a/homeassistant/components/switch_as_x/entity.py +++ b/homeassistant/components/switch_as_x/entity.py @@ -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 diff --git a/homeassistant/components/switch_as_x/fan.py b/homeassistant/components/switch_as_x/fan.py index d4f16a93ef6..bfc4d2e037e 100644 --- a/homeassistant/components/switch_as_x/fan.py +++ b/homeassistant/components/switch_as_x/fan.py @@ -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, ) ] ) diff --git a/homeassistant/components/switch_as_x/light.py b/homeassistant/components/switch_as_x/light.py index 1071d6b4480..c8181bf35f8 100644 --- a/homeassistant/components/switch_as_x/light.py +++ b/homeassistant/components/switch_as_x/light.py @@ -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, ) ] ) diff --git a/homeassistant/components/switch_as_x/lock.py b/homeassistant/components/switch_as_x/lock.py index a4e3b2ec180..a0aac15a702 100644 --- a/homeassistant/components/switch_as_x/lock.py +++ b/homeassistant/components/switch_as_x/lock.py @@ -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, ) ] ) diff --git a/homeassistant/components/switch_as_x/siren.py b/homeassistant/components/switch_as_x/siren.py index 591546cd20a..635aa4e2d79 100644 --- a/homeassistant/components/switch_as_x/siren.py +++ b/homeassistant/components/switch_as_x/siren.py @@ -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, ) ] ) diff --git a/tests/components/switch_as_x/test_init.py b/tests/components/switch_as_x/test_init.py index 9c3eec1884c..35c901e2d5e 100644 --- a/tests/components/switch_as_x/test_init.py +++ b/tests/components/switch_as_x/test_init.py @@ -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