2018-02-28 13:39:01 -08:00
|
|
|
"""Tests for the intent helpers."""
|
2018-07-01 17:51:40 +02:00
|
|
|
|
2019-04-30 18:20:38 +02:00
|
|
|
import pytest
|
2019-12-09 16:52:24 +01:00
|
|
|
import voluptuous as vol
|
2019-04-30 18:20:38 +02:00
|
|
|
|
2023-01-07 15:20:21 -06:00
|
|
|
from homeassistant.const import ATTR_FRIENDLY_NAME
|
2018-02-28 13:39:01 -08:00
|
|
|
from homeassistant.core import State
|
2023-01-07 15:20:21 -06:00
|
|
|
from homeassistant.helpers import config_validation as cv, entity_registry, intent
|
2018-07-01 17:51:40 +02: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 13:39:01 -08:00
|
|
|
|
|
|
|
|
2023-01-07 15:20:21 -06:00
|
|
|
async def test_async_match_state(hass):
|
2018-02-28 13:39:01 -08:00
|
|
|
"""Test async_match_state helper."""
|
2023-01-07 15:20:21 -06:00
|
|
|
state1 = State(
|
|
|
|
"light.kitchen", "on", attributes={ATTR_FRIENDLY_NAME: "kitchen light"}
|
|
|
|
)
|
|
|
|
state2 = State(
|
|
|
|
"switch.kitchen", "on", attributes={ATTR_FRIENDLY_NAME: "kitchen switch"}
|
|
|
|
)
|
|
|
|
registry = entity_registry.async_get(hass)
|
|
|
|
registry.async_get_or_create(
|
|
|
|
"switch", "demo", "1234", suggested_object_id="kitchen"
|
|
|
|
)
|
|
|
|
registry.async_update_entity(state2.entity_id, aliases={"kill switch"})
|
2018-02-28 13:39:01 -08:00
|
|
|
|
2023-01-07 15:20:21 -06:00
|
|
|
state = intent.async_match_state(hass, "kitchen light", [state1, state2])
|
2018-02-28 13:39:01 -08:00
|
|
|
assert state is state1
|
2018-07-01 17:51:40 +02:00
|
|
|
|
2023-01-07 15:20:21 -06:00
|
|
|
state = intent.async_match_state(hass, "kill switch", [state1, state2])
|
|
|
|
assert state is state2
|
|
|
|
|
2018-07-01 17:51:40 +02:00
|
|
|
|
2019-04-30 18:20:38 +02:00
|
|
|
def test_async_validate_slots():
|
|
|
|
"""Test async_validate_slots of IntentHandler."""
|
2019-07-31 12:25:30 -07:00
|
|
|
handler1 = MockIntentHandler({vol.Required("name"): cv.string})
|
2019-04-30 18:20:38 +02:00
|
|
|
|
|
|
|
with pytest.raises(vol.error.MultipleInvalid):
|
|
|
|
handler1.async_validate_slots({})
|
|
|
|
with pytest.raises(vol.error.MultipleInvalid):
|
2019-07-31 12:25:30 -07:00
|
|
|
handler1.async_validate_slots({"name": 1})
|
2019-04-30 18:20:38 +02:00
|
|
|
with pytest.raises(vol.error.MultipleInvalid):
|
2019-07-31 12:25:30 -07: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"}}
|
|
|
|
)
|