Load custom sentences for language variations (en-us, etc.) (#88498)

This commit is contained in:
Michael Hansen 2023-02-20 11:28:04 -06:00 committed by GitHub
parent 7119a0f811
commit 5bf3a0b7af
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 21 deletions

View file

@ -321,6 +321,9 @@ class DefaultAgent(AbstractConversationAgent):
intents_dict = lang_intents.intents_dict
loaded_components = lang_intents.loaded_components
# en-US, en_US, en, ...
language_variations = list(_get_language_variations(language))
# Check if any new components have been loaded
intents_changed = False
for component in hass_components:
@ -332,7 +335,7 @@ class DefaultAgent(AbstractConversationAgent):
# Check for intents for this component with the target language.
# Try en-US, en, etc.
for language_variation in _get_language_variations(language):
for language_variation in language_variations:
component_intents = get_intents(
component, language_variation, json_load=json_load
)
@ -343,7 +346,10 @@ class DefaultAgent(AbstractConversationAgent):
# Will need to recreate graph
intents_changed = True
_LOGGER.debug(
"Loaded intents component=%s, language=%s", component, language
"Loaded intents component=%s, language=%s (%s)",
component,
language,
language_variation,
)
break
@ -351,8 +357,9 @@ class DefaultAgent(AbstractConversationAgent):
if lang_intents is None:
# Only load custom sentences once, otherwise they will be re-loaded
# when components change.
for language_variation in language_variations:
custom_sentences_dir = Path(
self.hass.config.path("custom_sentences", language)
self.hass.config.path("custom_sentences", language_variation)
)
if custom_sentences_dir.is_dir():
for custom_sentences_path in custom_sentences_dir.rglob("*.yaml"):
@ -360,16 +367,22 @@ class DefaultAgent(AbstractConversationAgent):
encoding="utf-8"
) as custom_sentences_file:
# Merge custom sentences
merge_dict(intents_dict, yaml.safe_load(custom_sentences_file))
merge_dict(
intents_dict, yaml.safe_load(custom_sentences_file)
)
# Will need to recreate graph
intents_changed = True
_LOGGER.debug(
"Loaded custom sentences language=%s, path=%s",
"Loaded custom sentences language=%s (%s), path=%s",
language,
language_variation,
custom_sentences_path,
)
# Stop after first matched language variation
break
# Load sentences from HA config for default language only
if self._config_intents and (language == self.hass.config.language):
merge_dict(

View file

@ -595,12 +595,18 @@ async def test_custom_sentences(
# Expecting testing_config/custom_sentences/en/beer.yaml
intent.async_register(hass, OrderBeerIntentHandler())
# Don't use "en" to test loading custom sentences with language variants.
language = "en-us"
# Invoke intent via HTTP API
client = await hass_client()
for beer_style in ("stout", "lager"):
resp = await client.post(
"/api/conversation/process",
json={"text": f"I'd like to order a {beer_style}, please"},
json={
"text": f"I'd like to order a {beer_style}, please",
"language": language,
},
)
assert resp.status == HTTPStatus.OK
data = await resp.json()
@ -614,7 +620,7 @@ async def test_custom_sentences(
"speech": f"You ordered a {beer_style}",
}
},
"language": hass.config.language,
"language": language,
"response_type": "action_done",
"data": {
"targets": [],