Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions homeassistant/components/esphome/assist_satellite.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
{
vol.Required("type"): str,
vol.Required("wake_word"): str,
vol.Required("model"): str,
},
extra=vol.ALLOW_EXTRA,
)
Expand Down Expand Up @@ -904,26 +905,33 @@ def _get_custom_wake_words(
wake_words: dict[str, VoiceAssistantExternalWakeWord] = {}

# Look for config/model files
for config_path in wake_words_dir.glob("*.json"):
wake_word_id = config_path.stem
model_path = config_path.with_suffix(".tflite")
if not model_path.exists():
# Missing model file
continue
for config_path in wake_words_dir.rglob("*.json"):
# Use relative path to deconflict ids:
# custom_wake_words/my_wake_word.json -> my_wake_word
# custom_wake_words/sub_dir/my_wake_word.json -> sub_dir/my_wake_word
wake_word_id = str(config_path.relative_to(wake_words_dir).with_suffix(""))

with open(config_path, encoding="utf-8") as config_file:
config_dict = json.load(config_file)
try:
config = _WAKE_WORD_CONFIG_SCHEMA(config_dict)
except vol.Invalid as err:
# Invalid config
_LOGGER.debug(
"Invalid wake word config: path=%s, error=%s",
config_path,
humanize_error(config_dict, err),
)
continue

model_path = config_path.parent / config["model"]
if not model_path.exists():
_LOGGER.debug(
"Missing custom wake word model file: %s (config=%s)",
model_path,
config_path,
)
continue

with open(model_path, "rb") as model_file:
model_hash = hashlib.sha256(model_file.read()).hexdigest()

Expand All @@ -933,10 +941,11 @@ def _get_custom_wake_words(
# Only intended for the internal network
base_url = get_url(hass, prefer_external=False, allow_cloud=False)

wake_word = config["wake_word"]
wake_words[wake_word_id] = VoiceAssistantExternalWakeWord.from_dict(
{
"id": wake_word_id,
"wake_word": config["wake_word"],
"wake_word": wake_word,
"trained_languages": config_dict.get("trained_languages", []),
"model_type": config["type"],
"model_size": model_size,
Expand Down
4 changes: 2 additions & 2 deletions tests/components/esphome/test_assist_satellite.py
Original file line number Diff line number Diff line change
Expand Up @@ -2229,7 +2229,7 @@ async def test_custom_wake_words(

Expects 2 models in testing_config/custom_wake_words:
- hey_home_assistant
- choo_choo_homie
- choo_choo_homie (in choo_choo_homie sub-directory)
"""
http_client = await hass_client()
expected_config = AssistSatelliteConfiguration(
Expand Down Expand Up @@ -2261,7 +2261,7 @@ async def test_custom_wake_words(

assert {external_wake_words[0].id, external_wake_words[1].id} == {
"hey_home_assistant",
"choo_choo_homie",
"choo_choo_homie/choo_choo_homie",
}

# Verify details
Expand Down
Loading