2018-02-28 21:39:01 +00:00
|
|
|
"""Tests for the intent helpers."""
|
2018-07-01 15:51:40 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
import pytest
|
2019-12-09 15:52:24 +00:00
|
|
|
import voluptuous as vol
|
2019-04-30 16:20:38 +00:00
|
|
|
|
2018-02-28 21:39:01 +00:00
|
|
|
from homeassistant.core import State
|
2019-12-09 15:52:24 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv, intent
|
2018-07-01 15:51:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MockIntentHandler(intent.IntentHandler):
|
|
|
|
"""Provide a mock intent handler."""
|
|
|
|
|
|
|
|
def __init__(self, slot_schema):
|
|
|
|
"""Initialize the mock handler."""
|
|
|
|
self.slot_schema = slot_schema
|
2018-02-28 21:39:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_async_match_state():
|
|
|
|
"""Test async_match_state helper."""
|
2019-07-31 19:25:30 +00:00
|
|
|
state1 = State("light.kitchen", "on")
|
|
|
|
state2 = State("switch.kitchen", "on")
|
2018-02-28 21:39:01 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = intent.async_match_state(None, "kitch", [state1, state2])
|
2018-02-28 21:39:01 +00:00
|
|
|
assert state is state1
|
2018-07-01 15:51:40 +00:00
|
|
|
|
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
def test_async_validate_slots():
|
|
|
|
"""Test async_validate_slots of IntentHandler."""
|
2019-07-31 19:25:30 +00:00
|
|
|
handler1 = MockIntentHandler({vol.Required("name"): cv.string})
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
with pytest.raises(vol.error.MultipleInvalid):
|
|
|
|
handler1.async_validate_slots({})
|
|
|
|
with pytest.raises(vol.error.MultipleInvalid):
|
2019-07-31 19:25:30 +00:00
|
|
|
handler1.async_validate_slots({"name": 1})
|
2019-04-30 16:20:38 +00:00
|
|
|
with pytest.raises(vol.error.MultipleInvalid):
|
2019-07-31 19:25:30 +00:00
|
|
|
handler1.async_validate_slots({"name": "kitchen"})
|
|
|
|
handler1.async_validate_slots({"name": {"value": "kitchen"}})
|
|
|
|
handler1.async_validate_slots(
|
|
|
|
{"name": {"value": "kitchen"}, "probability": {"value": "0.5"}}
|
|
|
|
)
|
2021-01-27 11:16:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_fuzzy_match():
|
|
|
|
"""Test _fuzzymatch."""
|
|
|
|
state1 = State("light.living_room_northwest", "off")
|
|
|
|
state2 = State("light.living_room_north", "off")
|
|
|
|
state3 = State("light.living_room_northeast", "off")
|
|
|
|
state4 = State("light.living_room_west", "off")
|
|
|
|
state5 = State("light.living_room", "off")
|
|
|
|
states = [state1, state2, state3, state4, state5]
|
|
|
|
|
|
|
|
state = intent._fuzzymatch("Living Room", states, lambda state: state.name)
|
|
|
|
assert state == state5
|
|
|
|
|
|
|
|
state = intent._fuzzymatch(
|
|
|
|
"Living Room Northwest", states, lambda state: state.name
|
|
|
|
)
|
|
|
|
assert state == state1
|