From c85031d9eacb72b138c5867dcadf27e6f8d022c0 Mon Sep 17 00:00:00 2001 From: Abdellatif Anaflous <62770500+hktitof@users.noreply.github.com> Date: Sat, 19 Sep 2026 10:00:49 +0100 Subject: [PATCH] Expose listed emulated_hue entities when expose_by_default is off (#182129) --- .../components/emulated_hue/config.py | 4 +- tests/components/emulated_hue/test_hue_api.py | 49 ++++++++++++++++++- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/emulated_hue/config.py b/homeassistant/components/emulated_hue/config.py index c5cd4a6e76c3..888e527fc81a 100644 --- a/homeassistant/components/emulated_hue/config.py +++ b/homeassistant/components/emulated_hue/config.py @@ -253,7 +253,9 @@ class Config: return not self._entities_with_hidden_attr_in_config[state.entity_id] if not self.expose_by_default: - return False + # Listing an entity is the documented way to expose just a few + # devices when expose_by_default is off + return state.entity_id in self.entities # Expose an entity if the entity's domain is exposed by default and # the configuration doesn't explicitly exclude it from being # exposed, or if the entity is explicitly exposed diff --git a/tests/components/emulated_hue/test_hue_api.py b/tests/components/emulated_hue/test_hue_api.py index dfc0000c2125..370383c2e4b0 100644 --- a/tests/components/emulated_hue/test_hue_api.py +++ b/tests/components/emulated_hue/test_hue_api.py @@ -1769,6 +1769,36 @@ async def test_only_change_hue_or_saturation( ] == (0, 3) +@pytest.mark.usefixtures("base_setup") +@pytest.mark.parametrize( + ("entity_config", "expected_exposed"), + [ + pytest.param({}, True, id="hidden_omitted"), + pytest.param({emulated_hue.CONF_ENTITY_HIDDEN: False}, True, id="hidden_false"), + pytest.param({emulated_hue.CONF_ENTITY_HIDDEN: True}, False, id="hidden_true"), + ], +) +async def test_listed_entity_exposed_with_expose_by_default_off( + hass: HomeAssistant, + hass_client_no_auth: ClientSessionGenerator, + entity_config: dict[str, bool], + expected_exposed: bool, +) -> None: + """Test an explicitly listed entity is exposed with expose by default off.""" + conf = { + emulated_hue.CONF_LISTEN_PORT: BRIDGE_SERVER_PORT, + emulated_hue.CONF_EXPOSE_BY_DEFAULT: False, + emulated_hue.CONF_ENTITIES: {"light.exposed": entity_config}, + } + await _async_setup_emulated_hue(hass, conf) + _mock_hue_endpoints(hass, conf, {"1": "light.exposed"}) + hass.states.async_set("light.exposed", STATE_ON) + await hass.async_block_till_done() + client = await hass_client_no_auth() + result_json = await async_get_lights(client) + assert bool(result_json) is expected_exposed + + @pytest.mark.usefixtures("base_setup") async def test_specificly_exposed_entities( hass: HomeAssistant, hass_client_no_auth: ClientSessionGenerator @@ -1797,10 +1827,27 @@ async def test_specificly_exposed_entities( hass.states.async_set("light.exposed", STATE_ON) await hass.async_block_till_done() result_json = await async_get_lights(client) - assert "1" in result_json +@pytest.mark.usefixtures("base_setup") +async def test_unlisted_entity_not_exposed_with_expose_by_default_off( + hass: HomeAssistant, hass_client_no_auth: ClientSessionGenerator +) -> None: + """Test an entity not listed in the config is not exposed by default off.""" + conf = { + emulated_hue.CONF_LISTEN_PORT: BRIDGE_SERVER_PORT, + emulated_hue.CONF_EXPOSE_BY_DEFAULT: False, + } + await _async_setup_emulated_hue(hass, conf) + _mock_hue_endpoints(hass, conf, {"1": "light.unlisted"}) + hass.states.async_set("light.unlisted", STATE_ON) + await hass.async_block_till_done() + client = await hass_client_no_auth() + result_json = await async_get_lights(client) + assert not result_json + + async def test_get_light_state_when_none( hass_hue: HomeAssistant, hue_client: TestClient ) -> None: