Skip to content

Commit 625f586

Browse files
arturpragaczfrenck
authored andcommitted
Fix recognition of entity names in default agent with interpunction (home-assistant#151759)
1 parent 7dbeaa4 commit 625f586

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

homeassistant/components/conversation/default_agent.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
)
3636
from hassil.string_matcher import UnmatchedRangeEntity, UnmatchedTextEntity
3737
from hassil.trie import Trie
38-
from hassil.util import merge_dict
38+
from hassil.util import merge_dict, remove_punctuation
3939
from home_assistant_intents import (
4040
ErrorKey,
4141
FuzzyConfig,
@@ -327,12 +327,10 @@ async def async_recognize_intent(
327327

328328
if self._exposed_names_trie is not None:
329329
# Filter by input string
330-
text_lower = user_input.text.strip().lower()
330+
text = remove_punctuation(user_input.text).strip().lower()
331331
slot_lists["name"] = TextSlotList(
332332
name="name",
333-
values=[
334-
result[2] for result in self._exposed_names_trie.find(text_lower)
335-
],
333+
values=[result[2] for result in self._exposed_names_trie.find(text)],
336334
)
337335

338336
start = time.monotonic()
@@ -1263,7 +1261,7 @@ async def _make_slot_lists(self) -> dict[str, SlotList]:
12631261
name_list = TextSlotList.from_tuples(exposed_entity_names, allow_template=False)
12641262
for name_value in name_list.values:
12651263
assert isinstance(name_value.text_in, TextChunk)
1266-
name_text = name_value.text_in.text.strip().lower()
1264+
name_text = remove_punctuation(name_value.text_in.text).strip().lower()
12671265
self._exposed_names_trie.insert(name_text, name_value)
12681266

12691267
self._slot_lists = {

tests/components/conversation/test_default_agent.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,29 @@ async def test_conversation_agent(hass: HomeAssistant) -> None:
231231
)
232232

233233

234+
@pytest.mark.usefixtures("init_components")
235+
async def test_punctuation(hass: HomeAssistant) -> None:
236+
"""Test punctuation is handled properly."""
237+
hass.states.async_set(
238+
"light.test_light",
239+
"off",
240+
attributes={ATTR_FRIENDLY_NAME: "Test light"},
241+
)
242+
expose_entity(hass, "light.test_light", True)
243+
244+
calls = async_mock_service(hass, "light", "turn_on")
245+
result = await conversation.async_converse(
246+
hass, "Turn?? on,, test;; light!!!", None, Context(), None
247+
)
248+
249+
assert len(calls) == 1
250+
assert calls[0].data["entity_id"][0] == "light.test_light"
251+
assert result.response.response_type == intent.IntentResponseType.ACTION_DONE
252+
assert result.response.intent is not None
253+
assert result.response.intent.slots["name"]["value"] == "test light"
254+
assert result.response.intent.slots["name"]["text"] == "test light"
255+
256+
234257
async def test_expose_flag_automatically_set(
235258
hass: HomeAssistant,
236259
entity_registry: er.EntityRegistry,

0 commit comments

Comments
 (0)