| 1 | """Test language intents.""" |
| 2 | |
| 3 | from __future__scip-python python python-stdlib 3.11 __future__/__init__: import annotationsscip-python python python-stdlib 3.11 __future__/annotations.annotations. |
| 4 | |
| 5 | import dataclassesscip-python python python-stdlib 3.11 dataclasses/__init__: |
| 6 | import sysscip-python python python-stdlib 3.11 sys/__init__: |
| 7 | from pathlibscip-python python python-stdlib 3.11 pathlib/__init__: import Pathscip-python python python-stdlib 3.11 pathlib/Path# |
| 8 | from typingscip-python python python-stdlib 3.11 typing/__init__: import Anyscip-python python python-stdlib 3.11 typing/Any., Iterablescip-python python python-stdlib 3.11 typing/Iterable# |
| 9 | |
| 10 | from hassilscip-python python home-assistant-intents 2026.3.3 hassil/__init__: import ( |
| 11 | Expression, |
| 12 | Group, |
| 13 | Intents, |
| 14 | ListReference, |
| 15 | RuleReference, |
| 16 | Sentence, |
| 17 | TextSlotList, |
| 18 | ) |
| 19 | |
| 20 | from . import SENTENCES_DIRscip-python python home-assistant-intents 2026.3.3 `intents.tests`/SENTENCES_DIR. |
| 21 | |
| 22 | |
| 23 | def test_language_commonscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/test_language_common().( |
| 24 | languagescip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/test_language_common().(language): strscip-python python python-stdlib 3.11 builtins/str#, |
| 25 | language_sentences_yamlscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/test_language_common().(language_sentences_yaml): dictscip-python python python-stdlib 3.11 builtins/dict#[strscip-python python python-stdlib 3.11 builtins/str#, Anyscip-python python python-stdlib 3.11 typing/Any.], |
| 26 | ) -> None: |
| 27 | """Test the language common file.""" |
| 28 | common_files = [key for key in language_sentences_yamlscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/test_language_common().(language_sentences_yaml) if key.startswithscip-python python python-stdlib 3.11 builtins/str#startswith().("_")] |
| 29 | |
| 30 | assert common_files == [ |
| 31 | "_common.yaml" |
| 32 | ], "Only _common.yaml is allowed as common file" |
| 33 | |
| 34 | if languagescip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/test_language_common().(language) == "en": |
| 35 | return |
| 36 | |
| 37 | content = language_sentences_yamlscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/test_language_common().(language_sentences_yaml)["_common.yaml"] |
| 38 | |
| 39 | if values := content["lists"].get("color", {}).get("values", []): |
| 40 | for color in values: |
| 41 | assert isinstance( |
| 42 | color, dictscip-python python python-stdlib 3.11 builtins/dict# |
| 43 | ), "Color list should use the in-out format to output English color names for Home Assistant to consume. See sentences/nl/_common.yaml for an example." |
| 44 | |
| 45 | assert ( |
| 46 | "intents" not in content |
| 47 | ), "_common.yaml is a common file and should not contain intents" |
| 48 | |
| 49 | |
| 50 | def do_test_language_sentencesscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/do_test_language_sentences().( |
| 51 | file_namescip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/do_test_language_sentences().(file_name): strscip-python python python-stdlib 3.11 builtins/str#, |
| 52 | intent_schemasscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/do_test_language_sentences().(intent_schemas): dictscip-python python python-stdlib 3.11 builtins/dict#[strscip-python python python-stdlib 3.11 builtins/str#, Anyscip-python python python-stdlib 3.11 typing/Any.], |
| 53 | language_sentences_yamlscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/do_test_language_sentences().(language_sentences_yaml): dictscip-python python python-stdlib 3.11 builtins/dict#[strscip-python python python-stdlib 3.11 builtins/str#, Anyscip-python python python-stdlib 3.11 typing/Any.], |
| 54 | language_sentences_commonscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/do_test_language_sentences().(language_sentences_common): Intents, |
| 55 | ) -> None: |
| 56 | """Ensure all language sentences contain valid slots, lists, rules, etc.""" |
| 57 | if file_namescip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/do_test_language_sentences().(file_name) not in language_sentences_yamlscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/do_test_language_sentences().(language_sentences_yaml): |
| 58 | return |
| 59 | |
| 60 | file_domain, file_intent = Pathscip-python python python-stdlib 3.11 pathlib/Path#(file_namescip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/do_test_language_sentences().(file_name)).stemscip-python python python-stdlib 3.11 pathlib/PurePath#stem()..rsplitscip-python python python-stdlib 3.11 builtins/str#rsplit().("_", maxsplitscip-python python python-stdlib 3.11 builtins/str#rsplit().(maxsplit)=1) |
| 61 | |
| 62 | parsed_sentences_without_common = Intents.from_dict( |
| 63 | language_sentences_yamlscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/do_test_language_sentences().(language_sentences_yaml)[file_namescip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/do_test_language_sentences().(file_name)] |
| 64 | ) |
| 65 | |
| 66 | # Merge common rules with file specific intents. |
| 67 | language_sentences = dataclassesscip-python python python-stdlib 3.11 dataclasses/__init__:.replacescip-python python python-stdlib 3.11 dataclasses/replace().( |
| 68 | language_sentences_commonscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/do_test_language_sentences().(language_sentences_common), intents=parsed_sentences_without_common.intents |
| 69 | ) |
| 70 | |
| 71 | # Add placeholder slots that HA will generate |
| 72 | language_sentences.slot_lists["area"] = TextSlotList(name="area", values=[]) |
| 73 | language_sentences.slot_lists["name"] = TextSlotList(name="name", values=[]) |
| 74 | language_sentences.slot_lists["floor"] = TextSlotList(name="floor", values=[]) |
| 75 | |
| 76 | # Lint sentences |
| 77 | for intent_name, intent in language_sentences.intents.items(): |
| 78 | assert ( |
| 79 | file_intent == intent_name |
| 80 | ), f"File {file_namescip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/do_test_language_sentences().(file_name)} should only contain sentences for intent {file_intent}" |
| 81 | |
| 82 | intent_schema = intent_schemasscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/do_test_language_sentences().(intent_schemas)[intent_name] |
| 83 | slot_schema = intent_schema.get("slots", {}) |
| 84 | |
| 85 | for data in intent.data: |
| 86 | if not data.sentences: |
| 87 | continue |
| 88 | |
| 89 | expansion_rules = language_sentences.expansion_rules | data.expansion_rules |
| 90 | |
| 91 | if file_domain != "homeassistant": |
| 92 | # Domain specific files (ie light_HassTurnOn.yaml) should only match |
| 93 | # sentences for the light domain. |
| 94 | if intent_schemasscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/do_test_language_sentences().(intent_schemas)[file_intent]["domain"] == file_domain: |
| 95 | assert ( |
| 96 | "domain" not in data.slots |
| 97 | ), f"File {file_namescip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/do_test_language_sentences().(file_name)} should only have sentences without a domain slot" |
| 98 | else: |
| 99 | assert (data.slots.get("domain") == file_domain) or ( |
| 100 | data.requires_context.get("domain") == file_domain |
| 101 | ), f"File {file_namescip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/do_test_language_sentences().(file_name)} should only have sentences with a domain slot or context key set to {file_domain}" |
| 102 | |
| 103 | for sentence in data.sentences: |
| 104 | found_slots: setscip-python python python-stdlib 3.11 builtins/set#[strscip-python python python-stdlib 3.11 builtins/str#] = setscip-python python python-stdlib 3.11 builtins/set#() |
| 105 | for expression in _flattenscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_flatten().(sentence.expression): |
| 106 | _verifyscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().( |
| 107 | expression, |
| 108 | language_sentences, |
| 109 | intent_name, |
| 110 | slot_schemascip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().(slot_schema)=slot_schema, |
| 111 | visited_rulesscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().(visited_rules)=setscip-python python python-stdlib 3.11 builtins/set#(), |
| 112 | found_slotsscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().(found_slots)=found_slots, |
| 113 | expansion_rulesscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().(expansion_rules)=expansion_rules, |
| 114 | ) |
| 115 | |
| 116 | # Add inferred slots |
| 117 | found_slots.updatescip-python python python-stdlib 3.11 builtins/set#update().(data.slots) |
| 118 | if data.requires_context: |
| 119 | found_slots.updatescip-python python python-stdlib 3.11 builtins/set#update().(data.requires_context) |
| 120 | |
| 121 | # Check required slots |
| 122 | for slot_name, slot_info in slot_schema.items(): |
| 123 | if slot_info.get("required", False): |
| 124 | assert ( |
| 125 | slot_name in found_slots |
| 126 | ), f"Missing required slot: '{slot_name}', intent='{intent_name}', sentence='{sentence.text}'" |
| 127 | |
| 128 | |
| 129 | def _verifyscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().( |
| 130 | expressionscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().(expression): Expression, |
| 131 | intentsscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().(intents): Intents, |
| 132 | intent_namescip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().(intent_name): strscip-python python python-stdlib 3.11 builtins/str#, |
| 133 | *, |
| 134 | slot_schemascip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().(slot_schema): dictscip-python python python-stdlib 3.11 builtins/dict#[strscip-python python python-stdlib 3.11 builtins/str#, Anyscip-python python python-stdlib 3.11 typing/Any.], |
| 135 | visited_rulesscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().(visited_rules): setscip-python python python-stdlib 3.11 builtins/set#[strscip-python python python-stdlib 3.11 builtins/str#], |
| 136 | found_slotsscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().(found_slots): setscip-python python python-stdlib 3.11 builtins/set#[strscip-python python python-stdlib 3.11 builtins/str#], |
| 137 | expansion_rulesscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().(expansion_rules): dictscip-python python python-stdlib 3.11 builtins/dict#[strscip-python python python-stdlib 3.11 builtins/str#, Sentence], |
| 138 | ) -> None: |
| 139 | if isinstance(expressionscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().(expression), ListReference): |
| 140 | list_ref: ListReference = expressionscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().(expression) |
| 141 | |
| 142 | # Ensure list exists |
| 143 | assert ( |
| 144 | list_ref.list_name in intentsscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().(intents).slot_lists |
| 145 | ), f"Missing slot list: {{{list_ref.list_name}}}. Are you missing a 'lists' entry in _common.yaml?" |
| 146 | |
| 147 | # Ensure slot is part of intent |
| 148 | assert ( |
| 149 | list_ref.slot_name in slot_schemascip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().(slot_schema) |
| 150 | ), f"Intent {intent_namescip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().(intent_name)} does not support slot '{list_ref.slot_name}'. See intents.yaml for supported slots" |
| 151 | |
| 152 | # Track slots for combination check |
| 153 | found_slotsscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().(found_slots).addscip-python python python-stdlib 3.11 builtins/set#add().(list_ref.slot_name) |
| 154 | elif isinstance(expressionscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().(expression), RuleReference): |
| 155 | rule_ref: RuleReference = expressionscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().(expression) |
| 156 | assert ( |
| 157 | rule_ref.rule_name in expansion_rulesscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().(expansion_rules) |
| 158 | ), f"Missing expansion rule: <{rule_ref.rule_name}>. Are you missing an 'expansion_rules' entry in _common.yaml or in the intent data?" |
| 159 | |
| 160 | # Check for recursive rules (not supported) |
| 161 | assert ( |
| 162 | rule_ref.rule_name not in visited_rulesscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().(visited_rules) |
| 163 | ), f"Recursive rule detected: <{rule_ref.rule_name}>" |
| 164 | |
| 165 | # Verify rule body |
| 166 | for body_expression in _flattenscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_flatten().(expansion_rulesscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().(expansion_rules)[rule_ref.rule_name].expression): |
| 167 | visited_rulesscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().(visited_rules).addscip-python python python-stdlib 3.11 builtins/set#add().(rule_ref.rule_name) |
| 168 | _verifyscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().( |
| 169 | body_expression, |
| 170 | intentsscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().(intents), |
| 171 | intent_namescip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().(intent_name), |
| 172 | slot_schemascip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().(slot_schema)=slot_schemascip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().(slot_schema), |
| 173 | visited_rulesscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().(visited_rules)=visited_rulesscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().(visited_rules), |
| 174 | found_slotsscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().(found_slots)=found_slotsscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().(found_slots), |
| 175 | expansion_rulesscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().(expansion_rules)=expansion_rulesscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().(expansion_rules), |
| 176 | ) |
| 177 | visited_rulesscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_verify().(visited_rules).removescip-python python python-stdlib 3.11 builtins/set#remove().(rule_ref.rule_name) |
| 178 | |
| 179 | |
| 180 | def _flattenscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_flatten().(expressionscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_flatten().(expression): Expression) -> Iterablescip-python python python-stdlib 3.11 typing/Iterable#[Expression]: |
| 181 | if isinstance(expressionscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_flatten().(expression), Group): |
| 182 | grp: Group = expressionscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_flatten().(expression) |
| 183 | for item in grp.items: |
| 184 | yield from _flattenscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_flatten().(item) |
| 185 | else: |
| 186 | yield expressionscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/_flatten().(expression) |
| 187 | |
| 188 | |
| 189 | def gen_testscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/gen_test().(test_filescip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/gen_test().(test_file): strscip-python python python-stdlib 3.11 builtins/str#) -> None: |
| 190 | def test_funcscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/gen_test().test_func().( |
| 191 | intent_schemasscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/gen_test().test_func().(intent_schemas): dictscip-python python python-stdlib 3.11 builtins/dict#[strscip-python python python-stdlib 3.11 builtins/str#, Anyscip-python python python-stdlib 3.11 typing/Any.], |
| 192 | language_sentences_yamlscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/gen_test().test_func().(language_sentences_yaml): dictscip-python python python-stdlib 3.11 builtins/dict#[strscip-python python python-stdlib 3.11 builtins/str#, Anyscip-python python python-stdlib 3.11 typing/Any.], |
| 193 | language_sentences_commonscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/gen_test().test_func().(language_sentences_common): Intents, |
| 194 | ) -> None: |
| 195 | do_test_language_sentencesscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/do_test_language_sentences().( |
| 196 | test_filescip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/gen_test().(test_file), |
| 197 | intent_schemasscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/gen_test().test_func().(intent_schemas), |
| 198 | language_sentences_yamlscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/gen_test().test_func().(language_sentences_yaml), |
| 199 | language_sentences_commonscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/gen_test().test_func().(language_sentences_common), |
| 200 | ) |
| 201 | |
| 202 | test_funcscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/gen_test().test_func()..__name__ = f"test_{test_filescip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/gen_test().(test_file).rsplitscip-python python python-stdlib 3.11 builtins/str#rsplit().('.', 1)[0]}" |
| 203 | setattr(sysscip-python python python-stdlib 3.11 sys/__init__:.modulesscip-python python python-stdlib 3.11 sys/modules.modules.[__name__scip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/], test_funcscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/gen_test().test_func()..__name__, test_funcscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/gen_test().test_func().) |
| 204 | |
| 205 | |
| 206 | def gen_testsscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/gen_tests().() -> None: |
| 207 | names = { |
| 208 | test_file.namescip-python python python-stdlib 3.11 pathlib/PurePath#name(). |
| 209 | for test_file in SENTENCES_DIRscip-python python home-assistant-intents 2026.3.3 `intents.tests`/SENTENCES_DIR..globscip-python python python-stdlib 3.11 pathlib/Path#glob().("*/*.yaml") |
| 210 | if test_file.namescip-python python python-stdlib 3.11 pathlib/PurePath#name(). != "_common.yaml" |
| 211 | } |
| 212 | |
| 213 | for name in sortedscip-python python python-stdlib 3.11 builtins/sorted().(names): |
| 214 | gen_testscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/gen_test().(name) |
| 215 | |
| 216 | |
| 217 | gen_testsscip-python python home-assistant-intents 2026.3.3 `intents.tests.test_language_intents`/gen_tests().() |