Intent script: allow setting description and platforms (#118500)

* Add description to intent_script

* Allow setting platforms
This commit is contained in:
Paulus Schoutsen 2024-05-30 12:53:42 -04:00 committed by GitHub
parent 12215c51b3
commit ae3741c364
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 29 additions and 1 deletions

View file

@ -8,7 +8,7 @@ from typing import Any, TypedDict
import voluptuous as vol
from homeassistant.components.script import CONF_MODE
from homeassistant.const import CONF_TYPE, SERVICE_RELOAD
from homeassistant.const import CONF_DESCRIPTION, CONF_TYPE, SERVICE_RELOAD
from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.helpers import (
config_validation as cv,
@ -24,6 +24,7 @@ _LOGGER = logging.getLogger(__name__)
DOMAIN = "intent_script"
CONF_PLATFORMS = "platforms"
CONF_INTENTS = "intents"
CONF_SPEECH = "speech"
CONF_REPROMPT = "reprompt"
@ -41,6 +42,8 @@ CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: {
cv.string: {
vol.Optional(CONF_DESCRIPTION): cv.string,
vol.Optional(CONF_PLATFORMS): vol.All([cv.string], vol.Coerce(set)),
vol.Optional(CONF_ACTION): cv.SCRIPT_SCHEMA,
vol.Optional(
CONF_ASYNC_ACTION, default=DEFAULT_CONF_ASYNC_ACTION
@ -146,6 +149,8 @@ class ScriptIntentHandler(intent.IntentHandler):
"""Initialize the script intent handler."""
self.intent_type = intent_type
self.config = config
self.description = config.get(CONF_DESCRIPTION)
self.platforms = config.get(CONF_PLATFORMS)
async def async_handle(self, intent_obj: intent.Intent) -> intent.IntentResponse:
"""Handle the intent."""

View file

@ -22,6 +22,8 @@ async def test_intent_script(hass: HomeAssistant) -> None:
{
"intent_script": {
"HelloWorld": {
"description": "Intent to control a test service.",
"platforms": ["switch"],
"action": {
"service": "test.service",
"data_template": {"hello": "{{ name }}"},
@ -36,6 +38,17 @@ async def test_intent_script(hass: HomeAssistant) -> None:
},
)
handlers = [
intent_handler
for intent_handler in intent.async_get(hass)
if intent_handler.intent_type == "HelloWorld"
]
assert len(handlers) == 1
handler = handlers[0]
assert handler.description == "Intent to control a test service."
assert handler.platforms == {"switch"}
response = await intent.async_handle(
hass, "test", "HelloWorld", {"name": {"value": "Paulus"}}
)
@ -78,6 +91,16 @@ async def test_intent_script_wait_response(hass: HomeAssistant) -> None:
},
)
handlers = [
intent_handler
for intent_handler in intent.async_get(hass)
if intent_handler.intent_type == "HelloWorldWaitResponse"
]
assert len(handlers) == 1
handler = handlers[0]
assert handler.platforms is None
response = await intent.async_handle(
hass, "test", "HelloWorldWaitResponse", {"name": {"value": "Paulus"}}
)