diff --git a/tests/components/conversation/snapshots/test_default_agent.ambr b/tests/components/conversation/snapshots/test_default_agent.ambr index 7cdc239363d..d015b19ddc1 100644 --- a/tests/components/conversation/snapshots/test_default_agent.ambr +++ b/tests/components/conversation/snapshots/test_default_agent.ambr @@ -351,24 +351,14 @@ 'card': dict({ }), 'data': dict({ - 'failed': list([ - ]), - 'success': list([ - dict({ - 'id': 'light.kitchen', - 'name': 'kitchen light', - 'type': , - }), - ]), - 'targets': list([ - ]), + 'code': 'no_valid_targets', }), 'language': 'en', - 'response_type': 'action_done', + 'response_type': 'error', 'speech': dict({ 'plain': dict({ 'extra_data': None, - 'speech': 'Turned on the light', + 'speech': 'Sorry, I am not aware of any device called my cool light', }), }), }), @@ -474,6 +464,96 @@ }), }) # --- +# name: test_intent_entity_fail_if_unexposed + dict({ + 'conversation_id': None, + 'response': dict({ + 'card': dict({ + }), + 'data': dict({ + 'code': 'no_valid_targets', + }), + 'language': 'en', + 'response_type': 'error', + 'speech': dict({ + 'plain': dict({ + 'extra_data': None, + 'speech': 'Sorry, I am not aware of any device called kitchen light', + }), + }), + }), + }) +# --- +# name: test_intent_entity_remove_custom_name + dict({ + 'conversation_id': None, + 'response': dict({ + 'card': dict({ + }), + 'data': dict({ + 'code': 'no_valid_targets', + }), + 'language': 'en', + 'response_type': 'error', + 'speech': dict({ + 'plain': dict({ + 'extra_data': None, + 'speech': 'Sorry, I am not aware of any device called kitchen light', + }), + }), + }), + }) +# --- +# name: test_intent_entity_remove_custom_name.1 + dict({ + 'conversation_id': None, + 'response': dict({ + 'card': dict({ + }), + 'data': dict({ + 'failed': list([ + ]), + 'success': list([ + dict({ + 'id': 'light.kitchen', + 'name': 'kitchen light', + 'type': , + }), + ]), + 'targets': list([ + ]), + }), + 'language': 'en', + 'response_type': 'action_done', + 'speech': dict({ + 'plain': dict({ + 'extra_data': None, + 'speech': 'Turned on the light', + }), + }), + }), + }) +# --- +# name: test_intent_entity_remove_custom_name.2 + dict({ + 'conversation_id': None, + 'response': dict({ + 'card': dict({ + }), + 'data': dict({ + 'code': 'no_valid_targets', + }), + 'language': 'en', + 'response_type': 'error', + 'speech': dict({ + 'plain': dict({ + 'extra_data': None, + 'speech': 'Sorry, I am not aware of any device called renamed light', + }), + }), + }), + }) +# --- # name: test_intent_entity_renamed dict({ 'conversation_id': None, diff --git a/tests/components/conversation/test_default_agent.py b/tests/components/conversation/test_default_agent.py index 0d547e98641..d227625e7ce 100644 --- a/tests/components/conversation/test_default_agent.py +++ b/tests/components/conversation/test_default_agent.py @@ -1558,6 +1558,31 @@ async def test_intent_entity_renamed( assert data == snapshot assert data["response"]["response_type"] == "action_done" + +async def test_intent_entity_remove_custom_name( + hass: HomeAssistant, + init_components, + entity_registry: er.EntityRegistry, + snapshot: SnapshotAssertion, +) -> None: + """Test that removing a custom name allows targeting the entity by its auto-generated name again.""" + context = Context() + entity = MockLight("kitchen light", STATE_ON) + entity._attr_unique_id = "1234" + entity.entity_id = "light.kitchen" + setup_test_component_platform(hass, LIGHT_DOMAIN, [entity]) + + assert await async_setup_component( + hass, + LIGHT_DOMAIN, + {LIGHT_DOMAIN: [{"platform": "test"}]}, + ) + await hass.async_block_till_done() + + calls = async_mock_service(hass, LIGHT_DOMAIN, "turn_on") + + # Should fail with auto-generated name + entity_registry.async_update_entity("light.kitchen", name="renamed light") result = await conversation.async_converse( hass, "turn on kitchen light", None, context ) @@ -1578,6 +1603,7 @@ async def test_intent_entity_renamed( assert data == snapshot assert data["response"]["response_type"] == "action_done" + assert len(calls) == 1 result = await conversation.async_converse( hass, "turn on renamed light", None, context @@ -1588,6 +1614,42 @@ async def test_intent_entity_renamed( assert data["response"]["response_type"] == "error" +async def test_intent_entity_fail_if_unexposed( + hass: HomeAssistant, + init_components, + entity_registry: er.EntityRegistry, + snapshot: SnapshotAssertion, +) -> None: + """Test that an entity is not usable if unexposed.""" + context = Context() + entity = MockLight("kitchen light", STATE_ON) + entity._attr_unique_id = "1234" + entity.entity_id = "light.kitchen" + setup_test_component_platform(hass, LIGHT_DOMAIN, [entity]) + + assert await async_setup_component( + hass, + LIGHT_DOMAIN, + {LIGHT_DOMAIN: [{"platform": "test"}]}, + ) + await hass.async_block_till_done() + + calls = async_mock_service(hass, LIGHT_DOMAIN, "turn_on") + + # Unexpose the entity + expose_entity(hass, "light.kitchen", False) + await hass.async_block_till_done(wait_background_tasks=True) + + result = await conversation.async_converse( + hass, "turn on kitchen light", None, context + ) + + data = result.as_dict() + assert data == snapshot + assert data["response"]["response_type"] == "error" + assert len(calls) == 0 + + async def test_intent_entity_exposed( hass: HomeAssistant, init_components, @@ -1611,52 +1673,12 @@ async def test_intent_entity_exposed( {LIGHT_DOMAIN: [{"platform": "test"}]}, ) await hass.async_block_till_done() - entity_registry.async_update_entity("light.kitchen", aliases={"my cool light"}) - await hass.async_block_till_done() calls = async_mock_service(hass, LIGHT_DOMAIN, "turn_on") - result = await conversation.async_converse( - hass, "turn on kitchen light", None, context - ) - assert len(calls) == 1 - data = result.as_dict() - - assert data == snapshot - assert data["response"]["response_type"] == "action_done" - - calls.clear() - result = await conversation.async_converse( - hass, "turn on my cool light", None, context - ) - - assert len(calls) == 1 - data = result.as_dict() - - assert data == snapshot - assert data["response"]["response_type"] == "action_done" - - # Unexpose the entity + # Unexpose, then expose the entity expose_entity(hass, "light.kitchen", False) - await hass.async_block_till_done(wait_background_tasks=True) - - result = await conversation.async_converse( - hass, "turn on kitchen light", None, context - ) - - data = result.as_dict() - assert data == snapshot - assert data["response"]["response_type"] == "error" - - result = await conversation.async_converse( - hass, "turn on my cool light", None, context - ) - - data = result.as_dict() - assert data == snapshot - assert data["response"]["response_type"] == "error" - - # Now expose the entity + await hass.async_block_till_done() expose_entity(hass, "light.kitchen", True) await hass.async_block_till_done() @@ -1664,18 +1686,10 @@ async def test_intent_entity_exposed( hass, "turn on kitchen light", None, context ) - data = result.as_dict() - - assert data == snapshot - assert data["response"]["response_type"] == "action_done" - - result = await conversation.async_converse( - hass, "turn on my cool light", None, context - ) - data = result.as_dict() assert data == snapshot assert data["response"]["response_type"] == "action_done" + assert len(calls) == 1 async def test_intent_conversion_not_expose_new(