mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
Raise translated errors in Assist satellite (#186331)
Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude
parent
f47a81ca13
commit
8fac08b181
@@ -27,11 +27,11 @@ from homeassistant.components.assist_pipeline import (
|
||||
)
|
||||
from homeassistant.components.media_player import async_process_play_media_url
|
||||
from homeassistant.core import Context, callback
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
|
||||
from homeassistant.helpers import chat_session, entity
|
||||
from homeassistant.helpers.entity import EntityDescription
|
||||
|
||||
from .const import PREANNOUNCE_URL, AssistSatelliteEntityFeature
|
||||
from .const import DOMAIN, PREANNOUNCE_URL, AssistSatelliteEntityFeature
|
||||
from .errors import AssistSatelliteError, SatelliteBusyError
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -272,8 +272,9 @@ class AssistSatelliteEntity(entity.Entity):
|
||||
# The Home Assistant built-in agent doesn't support conversations.
|
||||
pipeline = async_get_pipeline(self.hass, self._resolve_pipeline())
|
||||
if pipeline.conversation_engine == conversation.HOME_ASSISTANT_AGENT:
|
||||
raise HomeAssistantError(
|
||||
"Built-in conversation agent does not support starting conversations"
|
||||
raise ServiceValidationError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="builtin_agent_start_conversation_not_supported",
|
||||
)
|
||||
|
||||
if start_message is None:
|
||||
@@ -375,7 +376,10 @@ class AssistSatelliteEntity(entity.Entity):
|
||||
# Wait for response text
|
||||
response_text = await self._ask_question_future
|
||||
if response_text is None:
|
||||
raise HomeAssistantError("No answer from question")
|
||||
raise HomeAssistantError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="no_answer",
|
||||
)
|
||||
|
||||
if not answers:
|
||||
return AssistSatelliteAnswer(id=None, sentence=response_text)
|
||||
@@ -655,7 +659,11 @@ class AssistSatelliteEntity(entity.Entity):
|
||||
|
||||
engine = tts.async_resolve_engine(self.hass, pipeline.tts_engine)
|
||||
if engine is None:
|
||||
raise HomeAssistantError(f"TTS engine {pipeline.tts_engine} not found")
|
||||
raise HomeAssistantError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="tts_engine_not_found",
|
||||
translation_placeholders={"engine": str(pipeline.tts_engine)},
|
||||
)
|
||||
|
||||
tts_options: dict[str, Any] = {}
|
||||
if pipeline.tts_voice is not None:
|
||||
|
||||
@@ -17,7 +17,12 @@ import probatio
|
||||
from homeassistant.auth.permissions.const import CAT_ENTITIES, POLICY_CONTROL
|
||||
from homeassistant.const import ATTR_ENTITY_ID
|
||||
from homeassistant.core import HomeAssistant, ServiceCall, SupportsResponse, callback
|
||||
from homeassistant.exceptions import HomeAssistantError, Unauthorized, UnknownUser
|
||||
from homeassistant.exceptions import (
|
||||
HomeAssistantError,
|
||||
ServiceValidationError,
|
||||
Unauthorized,
|
||||
UnknownUser,
|
||||
)
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
|
||||
from .const import DATA_COMPONENT, DOMAIN, AssistSatelliteEntityFeature
|
||||
@@ -147,8 +152,10 @@ def async_setup_services(hass: HomeAssistant) -> None:
|
||||
satellite_entity_id
|
||||
)
|
||||
if satellite_entity is None:
|
||||
raise HomeAssistantError(
|
||||
f"Invalid Assist satellite entity id: {satellite_entity_id}"
|
||||
raise ServiceValidationError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="invalid_entity_id",
|
||||
translation_placeholders={"entity_id": satellite_entity_id},
|
||||
)
|
||||
|
||||
satellite_entity.async_set_context(call.context)
|
||||
@@ -166,7 +173,10 @@ def async_setup_services(hass: HomeAssistant) -> None:
|
||||
answer = await satellite_entity.async_internal_ask_question(**ask_question_args)
|
||||
|
||||
if answer is None:
|
||||
raise HomeAssistantError("No answer from satellite")
|
||||
raise HomeAssistantError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="no_answer",
|
||||
)
|
||||
|
||||
return asdict(answer)
|
||||
|
||||
|
||||
@@ -66,6 +66,20 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"exceptions": {
|
||||
"builtin_agent_start_conversation_not_supported": {
|
||||
"message": "The built-in conversation agent does not support starting conversations."
|
||||
},
|
||||
"invalid_entity_id": {
|
||||
"message": "Invalid Assist satellite entity ID: {entity_id}."
|
||||
},
|
||||
"no_answer": {
|
||||
"message": "No answer received from the satellite."
|
||||
},
|
||||
"tts_engine_not_found": {
|
||||
"message": "Text-to-speech engine {engine} not found."
|
||||
}
|
||||
},
|
||||
"selector": {
|
||||
"answers": {
|
||||
"fields": {
|
||||
|
||||
@@ -30,7 +30,11 @@ from homeassistant.components.assist_satellite.entity import AssistSatelliteStat
|
||||
from homeassistant.components.media_source import PlayMedia
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import Context, HomeAssistant
|
||||
from homeassistant.exceptions import HomeAssistantError, Unauthorized
|
||||
from homeassistant.exceptions import (
|
||||
HomeAssistantError,
|
||||
ServiceValidationError,
|
||||
Unauthorized,
|
||||
)
|
||||
|
||||
from . import ENTITY_ID
|
||||
from .conftest import MockAssistSatellite
|
||||
@@ -799,7 +803,7 @@ async def test_start_conversation_reject_builtin_agent(
|
||||
entity: MockAssistSatellite,
|
||||
) -> None:
|
||||
"""Test starting a conversation on a device."""
|
||||
with pytest.raises(HomeAssistantError):
|
||||
with pytest.raises(ServiceValidationError) as exc_info:
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
"start_conversation",
|
||||
@@ -807,6 +811,11 @@ async def test_start_conversation_reject_builtin_agent(
|
||||
target={"entity_id": "assist_satellite.test_entity"},
|
||||
blocking=True,
|
||||
)
|
||||
assert exc_info.value.translation_domain == DOMAIN
|
||||
assert (
|
||||
exc_info.value.translation_key
|
||||
== "builtin_agent_start_conversation_not_supported"
|
||||
)
|
||||
|
||||
|
||||
async def test_start_conversation_default_preannounce(
|
||||
@@ -1013,6 +1022,108 @@ async def test_ask_question_requires_entity_permission(
|
||||
)
|
||||
|
||||
|
||||
async def test_ask_question_unknown_entity(
|
||||
hass: HomeAssistant,
|
||||
init_components: ConfigEntry,
|
||||
entity: MockAssistSatellite,
|
||||
) -> None:
|
||||
"""Test ask_question raises a translated error for an unknown entity."""
|
||||
with pytest.raises(ServiceValidationError) as exc_info:
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
"ask_question",
|
||||
{"entity_id": "assist_satellite.unknown", "question": "Anything?"},
|
||||
blocking=True,
|
||||
return_response=True,
|
||||
)
|
||||
assert exc_info.value.translation_domain == DOMAIN
|
||||
assert exc_info.value.translation_key == "invalid_entity_id"
|
||||
assert exc_info.value.translation_placeholders == {
|
||||
"entity_id": "assist_satellite.unknown"
|
||||
}
|
||||
|
||||
|
||||
async def test_ask_question_no_answer(
|
||||
hass: HomeAssistant,
|
||||
init_components: ConfigEntry,
|
||||
entity: MockAssistSatellite,
|
||||
) -> None:
|
||||
"""Test ask_question raises a translated error when there is no answer."""
|
||||
|
||||
async def async_start_conversation(start_announcement):
|
||||
entity._ask_question_future.set_result(None)
|
||||
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.media_source.async_resolve_media",
|
||||
return_value=PlayMedia(
|
||||
url="https://www.home-assistant.io/resolved.mp3",
|
||||
mime_type="audio/mp3",
|
||||
),
|
||||
),
|
||||
patch.object(entity, "async_start_conversation", new=async_start_conversation),
|
||||
pytest.raises(HomeAssistantError) as exc_info,
|
||||
):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
"ask_question",
|
||||
{
|
||||
"entity_id": "assist_satellite.test_entity",
|
||||
"question_media_id": "media-source://given",
|
||||
},
|
||||
blocking=True,
|
||||
return_response=True,
|
||||
)
|
||||
assert exc_info.value.translation_domain == DOMAIN
|
||||
assert exc_info.value.translation_key == "no_answer"
|
||||
assert entity.state == AssistSatelliteState.IDLE
|
||||
|
||||
|
||||
async def test_ask_question_service_no_answer(
|
||||
hass: HomeAssistant,
|
||||
init_components: ConfigEntry,
|
||||
entity: MockAssistSatellite,
|
||||
) -> None:
|
||||
"""Test ask_question service raises a translated error without an answer."""
|
||||
with (
|
||||
patch.object(entity, "async_internal_ask_question", return_value=None),
|
||||
pytest.raises(HomeAssistantError) as exc_info,
|
||||
):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
"ask_question",
|
||||
{"entity_id": "assist_satellite.test_entity", "question": "Anything?"},
|
||||
blocking=True,
|
||||
return_response=True,
|
||||
)
|
||||
assert exc_info.value.translation_domain == DOMAIN
|
||||
assert exc_info.value.translation_key == "no_answer"
|
||||
|
||||
|
||||
async def test_announce_tts_engine_not_found(
|
||||
hass: HomeAssistant,
|
||||
init_components: ConfigEntry,
|
||||
entity: MockAssistSatellite,
|
||||
) -> None:
|
||||
"""Test announce raises a translated error when the TTS engine is missing."""
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.tts.async_resolve_engine",
|
||||
return_value=None,
|
||||
),
|
||||
pytest.raises(HomeAssistantError) as exc_info,
|
||||
):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
"announce",
|
||||
{"message": "Hello"},
|
||||
target={"entity_id": "assist_satellite.test_entity"},
|
||||
blocking=True,
|
||||
)
|
||||
assert exc_info.value.translation_domain == DOMAIN
|
||||
assert exc_info.value.translation_key == "tts_engine_not_found"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("sentence", ["no punctuation!", "[malformed template)"])
|
||||
async def test_ask_question_invalid_sentences(
|
||||
hass: HomeAssistant,
|
||||
|
||||
Reference in New Issue
Block a user