Debian Codegraph @

tests/test_filter.py

raw
1 """Tests for filtering intents by Home Assistant info."""
2
3 from hassil import HomeInfo, filter_intents
4 from hassil.intents import Intents
5
6 YAML = """
7 language: "en"
8 intents:
9 HassTurnOn:
10 data:
11 - sentences:
12 - "turn on the lights in {area}"
13 inferred_domain: "light"
14 - sentences:
15 - "open the curtains in {area}"
16 inferred_domain: "cover"
17 - sentences:
18 - "turn on {name}"
19 name_domains:
20 - "light"
21 - "switch"
22 - "fan"
23 - sentences:
24 - "activate the alarm"
25 HassClimateSetTemperature:
26 data:
27 - sentences:
28 - "set the temperature to {temperature}"
29 slots:
30 domain: "climate"
31 lists:
32 area:
33 values:
34 - "kitchen"
35 name:
36 wildcard: true
37 """
38
39
40 def _load() -> Intents:
41 return Intents.from_dict(__import__("yaml").safe_load(YAML))
42
43
44 def test_inferred_and_name_domains_parsed():
45 intents = _load()
46 data = intents.intents["HassTurnOn"].data
47 assert data[0].inferred_domain == "light"
48 assert data[0].domains == {"light"}
49 assert data[2].name_domains == {"light", "switch", "fan"}
50 assert data[2].domains == {"light", "switch", "fan"}
51 # Block with no domain info
52 assert data[3].domains == set()
53 # Domain from slots
54 assert intents.intents["HassClimateSetTemperature"].data[0].domains == {"climate"}
55
56
57 def test_filter_by_domain_keeps_matching_blocks():
58 intents = filter_intents(_load(), domains={"light"})
59 turn_on = intents.intents["HassTurnOn"].data
60 # light (inferred), name_domains has light, and the domainless block remain;
61 # the cover block is dropped.
62 assert len(turn_on) == 3
63 # climate intent is dropped entirely (no light)
64 assert "HassClimateSetTemperature" not in intents.intents
65
66
67 def test_filter_drops_domainless_when_requested():
68 intents = filter_intents(_load(), domains={"light"}, keep_domainless=False)
69 turn_on = intents.intents["HassTurnOn"].data
70 assert len(turn_on) == 2 # domainless "activate the alarm" dropped
71
72
73 def test_filter_by_intent_names():
74 intents = filter_intents(_load(), intent_names={"HassClimateSetTemperature"})
75 assert set(intents.intents) == {"HassClimateSetTemperature"}
76
77
78 def test_filter_with_home_info():
79 home = HomeInfo(domains={"cover"}, intent_names={"HassTurnOn"})
80 intents = filter_intents(_load(), home=home)
81 assert set(intents.intents) == {"HassTurnOn"}
82 turn_on = intents.intents["HassTurnOn"].data
83 # cover (inferred) + domainless block kept; light blocks dropped.
84 assert len(turn_on) == 2
85
86
87 def test_filter_does_not_mutate_original():
88 original = _load()
89 n_before = len(original.intents["HassTurnOn"].data)
90 filter_intents(original, domains={"light"})
91 assert len(original.intents["HassTurnOn"].data) == n_before
92
93
94 def test_no_filters_returns_equivalent():
95 intents = filter_intents(_load())
96 assert set(intents.intents) == {"HassTurnOn", "HassClimateSetTemperature"}