mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Fix the type of slot schema of intent handlers (#117520)
Fix the slot schema of dynamic intenet handler
This commit is contained in:
parent
a95baf0d39
commit
4aba92ad04
@ -718,9 +718,13 @@ class IntentHandler:
|
|||||||
"""Intent handler registration."""
|
"""Intent handler registration."""
|
||||||
|
|
||||||
intent_type: str
|
intent_type: str
|
||||||
slot_schema: vol.Schema | None = None
|
|
||||||
platforms: Iterable[str] | None = []
|
platforms: Iterable[str] | None = []
|
||||||
|
|
||||||
|
@property
|
||||||
|
def slot_schema(self) -> dict | None:
|
||||||
|
"""Return a slot schema."""
|
||||||
|
return None
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_can_handle(self, intent_obj: Intent) -> bool:
|
def async_can_handle(self, intent_obj: Intent) -> bool:
|
||||||
"""Test if an intent can be handled."""
|
"""Test if an intent can be handled."""
|
||||||
@ -761,14 +765,6 @@ class DynamicServiceIntentHandler(IntentHandler):
|
|||||||
Service specific intent handler that calls a service by name/entity_id.
|
Service specific intent handler that calls a service by name/entity_id.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
slot_schema = {
|
|
||||||
vol.Any("name", "area", "floor"): cv.string,
|
|
||||||
vol.Optional("domain"): vol.All(cv.ensure_list, [cv.string]),
|
|
||||||
vol.Optional("device_class"): vol.All(cv.ensure_list, [cv.string]),
|
|
||||||
vol.Optional("preferred_area_id"): cv.string,
|
|
||||||
vol.Optional("preferred_floor_id"): cv.string,
|
|
||||||
}
|
|
||||||
|
|
||||||
# We use a small timeout in service calls to (hopefully) pass validation
|
# We use a small timeout in service calls to (hopefully) pass validation
|
||||||
# checks, but not try to wait for the call to fully complete.
|
# checks, but not try to wait for the call to fully complete.
|
||||||
service_timeout: float = 0.2
|
service_timeout: float = 0.2
|
||||||
@ -809,34 +805,34 @@ class DynamicServiceIntentHandler(IntentHandler):
|
|||||||
self.optional_slots[key] = value_schema
|
self.optional_slots[key] = value_schema
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def _slot_schema(self) -> vol.Schema:
|
def slot_schema(self) -> dict:
|
||||||
"""Create validation schema for slots (with extra required slots)."""
|
"""Return a slot schema."""
|
||||||
if self.slot_schema is None:
|
|
||||||
raise ValueError("Slot schema is not defined")
|
|
||||||
|
|
||||||
if self.required_slots or self.optional_slots:
|
|
||||||
slot_schema = {
|
slot_schema = {
|
||||||
**self.slot_schema,
|
vol.Any("name", "area", "floor"): cv.string,
|
||||||
**{
|
vol.Optional("domain"): vol.All(cv.ensure_list, [cv.string]),
|
||||||
vol.Required(key[0]): schema
|
vol.Optional("device_class"): vol.All(cv.ensure_list, [cv.string]),
|
||||||
for key, schema in self.required_slots.items()
|
vol.Optional("preferred_area_id"): cv.string,
|
||||||
},
|
vol.Optional("preferred_floor_id"): cv.string,
|
||||||
**{
|
|
||||||
vol.Optional(key[0]): schema
|
|
||||||
for key, schema in self.optional_slots.items()
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
else:
|
|
||||||
slot_schema = self.slot_schema
|
|
||||||
|
|
||||||
return vol.Schema(
|
if self.required_slots:
|
||||||
|
slot_schema.update(
|
||||||
{
|
{
|
||||||
key: SLOT_SCHEMA.extend({"value": validator})
|
vol.Required(key[0]): validator
|
||||||
for key, validator in slot_schema.items()
|
for key, validator in self.required_slots.items()
|
||||||
},
|
}
|
||||||
extra=vol.ALLOW_EXTRA,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if self.optional_slots:
|
||||||
|
slot_schema.update(
|
||||||
|
{
|
||||||
|
vol.Optional(key[0]): validator
|
||||||
|
for key, validator in self.optional_slots.items()
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
return slot_schema
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def get_domain_and_service(
|
def get_domain_and_service(
|
||||||
self, intent_obj: Intent, state: State
|
self, intent_obj: Intent, state: State
|
||||||
|
@ -32,7 +32,12 @@ class MockIntentHandler(intent.IntentHandler):
|
|||||||
|
|
||||||
def __init__(self, slot_schema) -> None:
|
def __init__(self, slot_schema) -> None:
|
||||||
"""Initialize the mock handler."""
|
"""Initialize the mock handler."""
|
||||||
self.slot_schema = slot_schema
|
self._mock_slot_schema = slot_schema
|
||||||
|
|
||||||
|
@property
|
||||||
|
def slot_schema(self):
|
||||||
|
"""Return the slot schema."""
|
||||||
|
return self._mock_slot_schema
|
||||||
|
|
||||||
|
|
||||||
async def test_async_match_states(
|
async def test_async_match_states(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user