Fix the type of slot schema of intent handlers (#117520)

Fix the slot schema of dynamic intenet handler
This commit is contained in:
Paulus Schoutsen 2024-05-15 16:45:15 -04:00 committed by GitHub
parent a95baf0d39
commit 4aba92ad04
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 35 deletions

View File

@ -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,33 +805,33 @@ 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: slot_schema = {
raise ValueError("Slot schema is not defined") 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,
}
if self.required_slots or self.optional_slots: if self.required_slots:
slot_schema = { slot_schema.update(
**self.slot_schema, {
**{ vol.Required(key[0]): validator
vol.Required(key[0]): schema for key, validator in self.required_slots.items()
for key, schema in self.required_slots.items() }
}, )
**{
vol.Optional(key[0]): schema
for key, schema in self.optional_slots.items()
},
}
else:
slot_schema = self.slot_schema
return vol.Schema( if self.optional_slots:
{ slot_schema.update(
key: SLOT_SCHEMA.extend({"value": validator}) {
for key, validator in slot_schema.items() vol.Optional(key[0]): validator
}, for key, validator in self.optional_slots.items()
extra=vol.ALLOW_EXTRA, }
) )
return slot_schema
@abstractmethod @abstractmethod
def get_domain_and_service( def get_domain_and_service(

View File

@ -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(