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

View file

@ -595,12 +595,18 @@ async def test_custom_sentences(
# Expecting testing_config/custom_sentences/en/beer.yaml # Expecting testing_config/custom_sentences/en/beer.yaml
intent.async_register(hass, OrderBeerIntentHandler()) 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 # Invoke intent via HTTP API
client = await hass_client() client = await hass_client()
for beer_style in ("stout", "lager"): for beer_style in ("stout", "lager"):
resp = await client.post( resp = await client.post(
"/api/conversation/process", "/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 assert resp.status == HTTPStatus.OK
data = await resp.json() data = await resp.json()
@ -614,7 +620,7 @@ async def test_custom_sentences(
"speech": f"You ordered a {beer_style}", "speech": f"You ordered a {beer_style}",
} }
}, },
"language": hass.config.language, "language": language,
"response_type": "action_done", "response_type": "action_done",
"data": { "data": {
"targets": [], "targets": [],